2017-03-05 79 views
0

的has_many关系,所以,如果我有两个模型是这样的:创建一个从形式

#parent.rb 
class Parent < ApplicationRecord 
    has_many :children 
end 

#children.rb 
class Child < ApplicationRecord 
    belongs_to :parent 
end 

你会如何创建一个表单,允许您在创建父窗体创建多个孩子?

回答

1

Cocoon [0]很好地解决了这个问题,并有一个很好的示例应用程序。

滚落在后端,扔accepts_nested_attributes_for :childrenParent模型,做一些fields_for(或simple_fields_for)的东西,在你的形式,并确保您可以通过添加children_attributes: [:name, :age]parent_params分配的属性。

[0] https://github.com/nathanvda/cocoon