我怎样才能达到这样的目标?
结构是=>测试有很多问题,问题有很多答案。
我在控制器中有questions = @test.questions.build
和questions.answers.build
。
form_for @test do |f|
f.fields_for :questions do |question_f|
question_f.fields_for :answers do |answer_f|
# answer form here
直到fields_for:answers。
我错过了什么?谢谢!
答案 0 :(得分:3)
如果您想使用嵌套表单,还应将accepts_nested_attributes_for
放入Test and Question模型中:
class Test < ActiveRecord::Base
attr_accessible :questions_attributes
has_many :questions, dependent: :destroy
accepts_nested_attributes_for :questions
end
class Question < ActiveRecord::Base
attr_accessible :answers_attributes
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :answers
end
试试这个:
form_for ([@test, @question]) do |f|
并在控制器的新操作中:
@test = Test.new
@question = Question.new
@test.questions.build
@question.answers.build