rails ActiveAdmin嵌套表单has_one accepts_attributes_for formtastic issue

时间:2022-07-19 15:59:02

I am using ActiveAdmin and Rails 3.1 -- having problem understanding whether the following is a bug, or if there is some way to do it correctly that I am not understanding. I am trying to use a nested model with a has one relationship, so that I can create a page and fill out it's meta data in 1 step. -- (page has_one meta_data, accepts_nested_attributes_for meta_data)

我正在使用ActiveAdmin和Rails 3.1 - 有问题了解以下是否是一个错误,或者是否有某种方法正确地做到我不理解。我试图使用具有一个关系的嵌套模型,这样我就可以创建一个页面并在一步中填写它的元数据。 - (页面has_one meta_data,accepts_nested_attributes_for meta_data)

Example 1) in this example, when I click new page, meta data section is there but there are no input fields -- also, if I edit the record, it shows up correctly, however the fieldset is duplicated in the second section... and if I remove the f.inputs wrapping semantic_field_for (which would make sense), then it breaks completely and shows nothing in the meta data area...

示例1)在此示例中,当我单击新页面时,元数据部分存在,但没有输入字段 - 另外,如果我编辑记录,它会正确显示,但是字段集在第二部分中重复。如果我删除包含semantic_field_for的f.inputs(这是有意义的),那么它会完全中断并在元数据区域中不显示任何内容......

form do |f|
  f.inputs "Page Information" do
    f.input :name
    f.input :uri
    f.input :view
    f.input :body, :as => :text
    f.input :active
  end

  f.inputs "Meta Data" do
    f.semantic_fields_for :meta_data do |meta_form|
      meta_form.inputs :title, :description, :keywords, :name => "Meta Information"
    end
  end  
end

I understand the meta data probably isn't being instantiated, but I am not sure how I am supposed to do that in the form block? (or if I can even do it) -- The only way I am able to get this to work is by doing using a custom form, and building the meta data in the view, which looks like this

我理解元数据可能没有被实例化,但我不确定我应该如何在表单块中执行此操作? (或者,如果我甚至可以这样做) - 我能够让它工作的唯一方法是使用自定义表单,并在视图中构建元数据,看起来像这样

2) How I am working around it, but seems hacky

2)我是如何解决这个问题的,但看起来很糟糕

<%= semantic_form_for [:admin, @page] do |f| %>
  <% @page.build_meta_data %>
  <%= f.inputs :name => "Page Information" do  %>
    <%= f.input :name %>
    <%= f.input :uri %>
    <%= f.input :view %>
    <%= f.input :body, :as => :text %>
    <%= f.input :active %>
  <% end %>
  <%= f.semantic_fields_for :meta_data do |meta_form| %>
    <%= meta_form.inputs :title, :description, :keywords, :name => "Meta Information" %>
  <% end %>

  <%= f.buttons %>
<% end %>

Thanks in advance for any help or clarification.

在此先感谢任何帮助或澄清。

(note to moderators I started another thread on this but was not as clear and didn't have the workaround solution I do now yet, so if one of the questions should be deleted please delete the other)

(请注意,对于主持人我开始了另一个线程,但不是很清楚,我现在还没有解决方法,所以如果其中一个问题应该被删除请删除另一个)

3 个解决方案

#1


51  

I found a better solution for you. You can use :for option in inputs helper.

我找到了一个更好的解决方案。您可以在输入助手中使用:for option。

f.inputs "Meta Data", for: [:meta_data, f.object.meta_data || MetaData.new] do |meta_form|
  meta_form.input :title
  meta_form.input :description
  meta_form.input :keywords
end

I think this might work too, but I didn't check

我认为这也可能有用,但我没有检查

f.inputs :title, :desctiption, :keywords, 
  name: "Meta Data",
  for: [:meta_data, f.object.meta_data || MetaData.new]

#2


19  

In rails 4, this is something that works, with a nice design

在rails 4中,这是一个有效的设计

e.g., A customer has one account

例如,客户有一个帐户

model/customer.rb

模型/ customer.rb

accepts_nested_attributes_for :account

admin/customer.rb

管理员/ customer.rb

form do |f|
  f.inputs do
    f.input :user, input_html: { disabled: true }
      f.input :name
      f.input :address
      f.input :city
      f.input :country, as: :string
    end
    f.buttons

    f.inputs "Account Information", for: [:account, f.object.account] do |s|
      s.input :active, as: :boolean
      s.input :subscription, as: :boolean
      s.input :expires_on, as: :datepicker

      s.actions
    end
  end

  controller do
    def permitted_params
      params.permit!
    end
  end
end

#3


1  

i was having the same problem, i worked in your hack and got it working. i then moved <% @page.build_meta_data %> to a custom new method like this

我有同样的问题,我在你的黑客工作,并让它工作。然后我将<%@ page.build_meta_data%>移动到这样的自定义新方法

  controller do
    def new
      @tenant = Tenant.new
      @tenant.build_tenant_configurable
    end
  end

hope this helps

希望这可以帮助

#1


51  

I found a better solution for you. You can use :for option in inputs helper.

我找到了一个更好的解决方案。您可以在输入助手中使用:for option。

f.inputs "Meta Data", for: [:meta_data, f.object.meta_data || MetaData.new] do |meta_form|
  meta_form.input :title
  meta_form.input :description
  meta_form.input :keywords
end

I think this might work too, but I didn't check

我认为这也可能有用,但我没有检查

f.inputs :title, :desctiption, :keywords, 
  name: "Meta Data",
  for: [:meta_data, f.object.meta_data || MetaData.new]

#2


19  

In rails 4, this is something that works, with a nice design

在rails 4中,这是一个有效的设计

e.g., A customer has one account

例如,客户有一个帐户

model/customer.rb

模型/ customer.rb

accepts_nested_attributes_for :account

admin/customer.rb

管理员/ customer.rb

form do |f|
  f.inputs do
    f.input :user, input_html: { disabled: true }
      f.input :name
      f.input :address
      f.input :city
      f.input :country, as: :string
    end
    f.buttons

    f.inputs "Account Information", for: [:account, f.object.account] do |s|
      s.input :active, as: :boolean
      s.input :subscription, as: :boolean
      s.input :expires_on, as: :datepicker

      s.actions
    end
  end

  controller do
    def permitted_params
      params.permit!
    end
  end
end

#3


1  

i was having the same problem, i worked in your hack and got it working. i then moved <% @page.build_meta_data %> to a custom new method like this

我有同样的问题,我在你的黑客工作,并让它工作。然后我将<%@ page.build_meta_data%>移动到这样的自定义新方法

  controller do
    def new
      @tenant = Tenant.new
      @tenant.build_tenant_configurable
    end
  end

hope this helps

希望这可以帮助