1

我怎样才能实现这样的目标?
结构是=>一个测试有很多问题,一个问题有很多答案。

我有questions = @test.questions.buildquestions.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。

我错过了什么?谢谢!

4

1 回答 1

3

accepts_nested_attributes_for如果您想使用嵌套形式,您还应该放入您的测试和问题模型:

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 
于 2012-11-05T04:53:57.820 回答