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啊,这是正确的!简单地看一下!谢谢