嵌套表单未知属性:question_id

时间:2014-03-23 03:54:14

标签: ruby-on-rails ruby-on-rails-4 nested-forms

我有两个模型:

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

1 个答案:

答案 0 :(得分:1)

似乎您忘记在Answers表中创建question_id字段。

两个模型Question and Answer之间存在1-M关系。确保Answers表中有foreign_key question_id