我在提交表单后收到ActiveRecord::UnknownAttributeError
。行unknown attribute: conversation_id
@question = @conversation.questions.new(params[:question])
我在属性中添加了conversation_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
用户模型:
attr_accessible :role, :notification_id, :sender_id, :receiver_id, :conversation_id, :no_email, :average_response_time, :response_rate, :response_total, :name, :time_zone, :code, :lat, :lon, :city, :age, :age_end, :password_confirmation, :about_me, :feet, :inches, :password, :birthday, :career, :children, :education, :email, :ethnicity, :gender, :height, :name, :password_digest, :politics, :religion, :sexuality, :user_drink, :user_smoke, :username, :zip_code
问题模型:
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
答案 0 :(得分:2)
您正在接收以下行<{1}}
ActiveRecord::UnknownAttributeError - unknown attribute: conversation_id
因为您没有在@question = @conversation.questions.new(params[:question])
表格中创建conversation_id
字段,因为您在questions
和Conversation
模型之间设置了1-M关联,所以这是必需的。
要解决此错误,您需要在Question
表格中添加conversation_id
字段。