我有两个模型:
class Question < ActiveRecord::Base
has_many :answers, :dependent => :destroy
accepts_nested_attributes_for :answers
end
class Answer < ActiveRecord::Base
belongs_to :question
end
以下控制器:
class QuestionsController < ApplicationController
def new
@question = Question.new
4.times { @question.answers.build }
end
end
当我尝试加载new.html.haml
视图时,我收到此错误:
Unknown attribute: question_id
视图如下所示:
= simple_form_for @question do |f|
= f.input :title
= f.simple_fields_for :answers do |a|
= a.input :text
= f.submit
答案 0 :(得分:1)
似乎您忘记在Answers表中创建question_id
字段。
两个模型Question and Answer
之间存在1-M关系。确保Answers表中有foreign_key question_id
。