1

我現在坐在這裏8小時,以弄清楚它是如何工作的:我試圖將http://asciicasts.com/episodes/196-nested-model-form-part-1中的示例修改爲一對一的關係。嵌套的屬性與一對一的關係

class Survey < ActiveRecord::Base 
    has_one :question, :dependent => :destroy 
    accepts_nested_attributes_for :question 
end 

class Question < ActiveRecord::Base 
    belongs_to :survey 
end 

控制器:

def new 
    @survey = Survey.new 
    @survey.questions.build 
end 

如果我使用像一個一對多的關係,它的偉大工程:

class Survey < ActiveRecord::Base 
    has_many :questions, :dependent => :destroy 
    accepts_nested_attributes_for :questions 
end 

我在做什麼錯?

+0

什麼是失敗和在哪裏?你可以發佈你的視圖代碼或一些錯誤? –

回答

1

Try @ survey.build_question而不是@ survey.questions.build。

我認爲這是在您使用一對一關係時建立問題的正確方法。

+0

這對我有效! – thedanotto