2017-07-17 91 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