2017-03-17 62 views
0

我有parent有很多children关系。我想轻松管理特定父母的孩子。活动管理 - 为has_many父亲编辑表格

我想是这样的:

form do |f| 
    f.inputs "Parent" do 
    f.input :name 
    end 

    f.inputs 'Children' do 
    f.has_many :children, new_record: true do |c| 
     c.input :name 
    end 
    end 
    f.actions 
end 

但我得到:

未定义的方法`new_record“?为零:NilClass

我有Rails 5.有没有更好的方法来使这项工作?什么是允许用户管理子对象的最佳方式?

回答

1
f.has_many :children do |c| 
    c.inputs "Children" do 
    c.input :name 
    #repeat as necessary for all fields 
    end 
end 

确保有这个在你的父模型:

accepts_nested_attributes_for :children 
+0

accepts_nested_attributes_for是一个解决方案!感谢一百万次的人! :d – knagode