2017-03-05 109 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