2014-06-27 29 views
1

提交表单后我收到ActiveRecord::UnknownAttributeError。错误是unknown attribute: conversation_id就行@question = @conversation.questions.new(params[:question])提交表单时ActiveRecord :: UnknownAttributeError

我加了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 
+1

你忘了在'questions'表中添加'conversation_id'字段吗? –

+0

什么是进入函数的参数哈希值? – MCBama

+0

@KirtiThorat啊那会是正确的!简单地看看!谢谢 – pwz2000

回答

2

你在下面一行

@question = @conversation.questions.new(params[:question]) 

接收ActiveRecord::UnknownAttributeError - unknown attribute: conversation_id因为你没有在你有ConversationQuestion模型之间设置1-M协会,要求questions表中创建conversation_id场。

要解决此错误,您需要在questions表中添加conversation_id字段。