I am trying to create a private messaging system for my website, and I am currently working on message replies. However, I ran into an ActiveRecord :: AssociationTypeMismatch problem. This error message is:
ActiveRecord :: AssociationTypeMismatch in RepliesController # create
Message (# 58297820) expected to get a String (# 1635350)
I tried to find out what the problem is now, with no luck.
Below you will find my code for my migration, model, view and controller.
Migration
def self.up create_table :replies do |t| t.integer :message_id, :null => false t.integer :sender_id, :null => false t.text :message, :null => false t.timestamps end end
Model
class Reply < ActiveRecord::Base belongs_to :message validates_presence_of :message cattr_reader :per_page @@per_page = 10 end
View
<% form_for(@reply) do |f| %> <table> <tr> <td><%= f.text_area :message %></td> </tr> <%= f.hidden_field :message_id, :value => @message.id %> <tr> <td><%= f.submit 'Reply', :id => 'replySubmit' %></td> </tr> </table> <% end %>
Controller
def create account = Account.getAccountById(session[:user]) message = Message.find( params[:reply][:message_id], :conditions => ["messages.account_id=? or messages.sender_id=?", account.id, account.id] ) if message @reply = Reply.new @reply.message_id = message.id @reply.sender_id = account.id @reply.message = params[:reply][:message] if @reply.save flash[:message] = "Reply successfully submitted." redirect_to(messages_path) else flash[:warning] = "Message cannot be blank." redirect_to(messages_path) end else redirect_to(messages_path) end rescue ActiveRecord::RecordNotFound render :template => "error" end
I would be grateful for the help provided. I will try to find out what the problem is.
Thanks.
Update: Stacktrace
RAILS_ROOT: C:/Users/redbush/Desktop/biomixr Application Trace | Framework Trace | Full Trace C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record /associations/association_proxy.rb:259:in `raise_on_type_mismatch' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations/belongs_to_association.rb:22:in `replace' C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.8/lib/active_record/associations.rb:1287:in `message=' C:/Users/redbush/Desktop/biomixr/app/controllers/replies_controller.rb:14:in `create'
Request
Options:
{"commit"=>"Reply", "reply"=>{"message"=>"sssss", "message_id"=>"4"}, "authenticity_token"=>"SMVfiolNAVPmLLU0eOWzx2jPFbujMtpyqQcs6A2Mxr0="}
Show Session Dump Response
Headers:
{"Content-Type"=>"", "Cache-Control"=>"no-cache"}