Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails ActiveRecord::提交表单时出现未知属性错误_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails ActiveRecord::提交表单时出现未知属性错误

Ruby on rails ActiveRecord::提交表单时出现未知属性错误,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,提交表单后,我收到一个ActiveRecord::UnknownAttributeError。错误是行@question=@conversation.questions.new(params[:question])上的未知属性:conversation\u id 我在属性中添加了conversation\u id,但没有做任何更改。不确定错误还可能指向什么 问题: def create @conversation = Conversation.create @question

提交表单后,我收到一个
ActiveRecord::UnknownAttributeError
。错误是行
@question=@conversation.questions.new(params[:question])上的
未知属性:conversation\u id

我在属性中添加了
conversation\u id
,但没有做任何更改。不确定错误还可能指向什么

问题:

  def create
    @conversation = Conversation.create
    @question = @conversation.questions.new(params[:question])
      if @question.save
        @message = current_user.messages.new(:subject => "You have a question from #{@question.sender_id}",
                               :body => @question.question)
        @question.message = @message
        @question.save
        redirect_to :back, notice: 'Your question was saved successfully. Thanks!'
      else
        render :new, alert: 'Sorry. There was a problem saving your question.'
      end
    end
  end
对话模式:

class Conversation < ActiveRecord::Base
  acts_as_messageable

  attr_accessible :answer, :question, :sender_id, :recipient_id, :conversation_id

  has_many :questions

end
class Question < ActiveRecord::Base
  attr_accessible :answer, :question, :sender_id, :recipient_id, :conversation_id
  belongs_to :user

  belongs_to :sender,:class_name => 'User',:foreign_key => 'sender_id'

  belongs_to :recipient,:class_name => 'User',:foreign_key => 'recipient_id'

  belongs_to :message

  belongs_to :conversation


  end
问题模式:

class Conversation < ActiveRecord::Base
  acts_as_messageable

  attr_accessible :answer, :question, :sender_id, :recipient_id, :conversation_id

  has_many :questions

end
class Question < ActiveRecord::Base
  attr_accessible :answer, :question, :sender_id, :recipient_id, :conversation_id
  belongs_to :user

  belongs_to :sender,:class_name => 'User',:foreign_key => 'sender_id'

  belongs_to :recipient,:class_name => 'User',:foreign_key => 'recipient_id'

  belongs_to :message

  belongs_to :conversation


  end
类问题'User',:foreign\u-key=>'sender\u-id'
属于:收件人,:类名称=>'User',:外键=>'recipient\u id'
属于:消息
属于:对话
结束

您将在下一行收到
ActiveRecord::UnknownAttributeError-未知属性:对话id

@question = @conversation.questions.new(params[:question])
因为您没有在
questions
表中创建
conversation\u id
字段,这是必需的,因为您已经在
conversation
Question
模型之间建立了1-M关联


要解决此错误,您需要在
问题
表中添加
conversation\u id
字段。

您是否忘记在
问题
表中添加
conversation\u id
字段?函数中的参数散列是什么?@kirthorat啊,这是正确的!简单地看一下!谢谢