Rails ActiveAdmin嵌套表格has_one accepts_attributes_for表格问题 [英] rails ActiveAdmin nested form has_one accepts_attributes_for formtastic issue
问题描述
我正在使用ActiveAdmin和Rails 3.1-在理解以下内容是否是错误或是否有某种方法可以正确执行我不理解的问题时遇到了问题.我正在尝试使用具有一个关系的嵌套模型,以便可以一步创建一个页面并填写其元数据. -- (页面has_one meta_data,accepts_nested_attributes_for meta_data)
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)
示例1) 在此示例中,当我单击新页面时,存在元数据部分,但没有输入字段-同样,如果我编辑记录,它会正确显示,但是该字段集在第二部分中重复...并且如果我删除了包裹语义_field_for的f.inputs(这很有意义),然后它完全破裂,在元数据区域中什么也没有显示...
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...
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)我如何解决它,但似乎很笨拙
<%= 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)
推荐答案
我为您找到了更好的解决方案.您可以在inputs
助手中使用:for
选项.
I found a better solution for you. You can use :for
option in inputs
helper.
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]
这篇关于Rails ActiveAdmin嵌套表格has_one accepts_attributes_for表格问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!