2011-10-23 148 views
0

是否有任何可能的方式使用nested_attributes_for在下面顯示的方式? 基本上我想創建一個人,一個或多個汽車,併爲每輛車添加細節。這只是一個模擬,不是一個非常現實的例子。因爲它尚未創建,所以在試圖構建汽車的細節時會受到阻礙。accepted_nested_attributes_for - belongs_to,has_many,fields_for

型號:

class Person < ActiveRecord::Base 
    has_many :cars 
    accepts_nested_attributes_for :car 
end 

class Car < ActiveRecord::Base 
    belongs_to :person 
    has_many :details 
    accepts_nested_attributes_for :details 
end 

class Detail < ActiveRecord::Base 
    belongs_to :car 
end 

形式:

form_for @person do |f| 
    #fields 
    f.fields_for :car do |car| 
    #fields 
    car.fields_for :details |detail| 
     =detail.text_field :content 
    end 
    end 
end 

回答