我正在開發一個項目,我將在一節的結尾處測試用戶。使用嵌套模型表格我希望users
能夠選擇answers
並將其存儲。我正在努力爲自己完善自己,並且可以使用來自更有經驗的開發人員的建議,以便如何最好地實現這一目標。Rails 4:使用嵌套模型表單保存用戶選擇
我認爲這是多對多的關係,我需要一個加入表格,但我不清楚如何表面允許users
選擇他們的answers
。我需要爲這個新的加入表創建一個控制器還是我誤解了這種情況下的ActiveRecord
?
我的模型是:如何最好地實現我的目標
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
class Test < ActiveRecord::Base
has_many :questions, :dependent => :destroy
accepts_nested_attributes_for :questions
end
class Question < ActiveRecord::Base
belongs_to :test
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers
end
class Answer < ActiveRecord::Base
belongs_to :question
end
任何有識之士/建議將非常感激。
觀看關於'nested-forms'的視頻http://railscasts.com/episodes/196-nested-model-form-part-1 – Emu