我有三个模型以多模型形式呈现。Rails 3多模型窗体与多个新的子模型实例
class Parent < ActiveRecord::Base
has_many :children
accepts_nested_attributes_for :children
end
class Child < ActiveRecord::Base
belongs_to :parent
has_many :other_children
accepts_nested_attributes_for :other_children
end
class OtherChild < ActiveRecord::Base
belongs_to :child
end
= form_for @parent do |f|
# fields for parent
= f.fields_for :children, @parent.children do |cf|
= cf.fields_for :other_children, @parent.children do |ocf|
# fields_for other child
= cf.fields_for :other_children, @parent.children.new do |ocf|
# fields_for other child
这有效,除非我通过jquery复制第二组子域。更清楚的是,新的other_child模型的fields_for有一个创建按钮,它触发了一些jQuery,如$(new_child_form).clone().insertBefore($(new_child_form))
允许在同一个表单中添加多个子模型。我知道我可以通过ajax单独提交每个子表单,但这不是我想要的。
Rails越来越多parent[children_attributes][0][other_children_attributes][1]
,似乎只使用最后一个。有任何想法吗?