我正在以一种形式在索引视图中编辑父模型的多个实例,如 Railscasts #198 中所示。每个父节点 has_many :children 和 accept_nested_attributes_for :children,如 Railscasts #196 和 #197
<%= form_tag %>
<% for parent in @parents %>
<%= fields_for "parents[]", parent do |f|
<%= f.text_field :job %>
<%= f.fields_for :children do |cf| %>
<% cf.text_field :chore %>
<% end %>
<% end %>
<% end %>
<% end %>
给定 parent.id==1
f.text_field :job 正确生成
<input id="parents_1_job" type="text" value="coding" size="30" name="parents[1][job]">
但是 cf.text_field :chore 生成没有父索引的 ID 和名称。
id="parents_children_attributes_0_chore"
name="parents[children_attributes][0][chore]"
如果我尝试像这样将特定的子对象传递给 f.fields_for:
<% for child in parent.children %>
<%= f.fields_for :children, child do |cf| %>
<%= cf.text_field :chore %>
<% end %>
<% end %>
我也一样。如果我将方法从 :children 更改为 "[]children" 我得到
id="parents_1___children_chore"
它得到正确的 parent_index 但不为子索引提供数组槽。
“[]children[]”也不对:id="parents_1__children_3_chore"
因为我期待attributes_0_chore 而不是3_chore。
我是否需要直接修改 FormBuilder 对象的属性或 FormBuilder 的子类才能使其工作,或者是否有适合这种情况的语法?
感谢您的任何想法。