2017-07-17 84 views
0

我试图创建一个与N个模型关联的嵌套表单。Rails 4 - 多个嵌套表单

这是架构:

schema

,我需要一个编辑表格穿过的所有对象重复,直到最后一个:

enter image description here

交付对象外观像这样:

class Delivery < ActiveRecord::Base 
    has_many :boxes 

,这是框对象

class Box < ActiveRecord::Base 

    belongs_to :box, :polymorphic => true, :inverse_of => :box 
    has_many :boxes 
    accepts_nested_attributes_for :boxes 
+0

你尝试过什么到目前为止,什么结果呢? –

+0

我试过这个:'= f.fields_for:boxes do | f |',在这个'f.fields_for:boxes do | ff |'中,但是这段代码只显示了第二层不是以下内容...... – larz

+0

你需要实例化盒子 – Snake

回答

0

在交付控制器:

def new 
    @delivery = Delivery.new 
    # if you want 3 instantiated objects 
    3.times { @delivery.boxes.build } 
end