我正在尝试为我的网站创建一个私人消息系统,目前正在处理消息回复。但是我遇到了ActiveRecord :: AssociationTypeMismatch问题。这是错误消息:
在RepliesController#create中的ActiveRecord :: AssociationTypeMismatch
消息(#58297820)预期得到String(#1635350)
我一直在努力找出问题所在,但一直没有成功。
下面是我的迁移,模型,视图和控制器的代码。
显示会话转储 响应
标头:
在RepliesController#create中的ActiveRecord :: AssociationTypeMismatch
消息(#58297820)预期得到String(#1635350)
我一直在努力找出问题所在,但一直没有成功。
下面是我的迁移,模型,视图和控制器的代码。
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
模型
class Reply < ActiveRecord::Base
belongs_to :message
validates_presence_of :message
cattr_reader :per_page
@@per_page = 10
end
查看
<% 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 %>
控制器
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
非常感谢您提供的任何帮助。我将继续尝试找出问题所在。
谢谢。
更新:堆栈跟踪
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'
请求
参数:
{"commit"=>"Reply",
"reply"=>{"message"=>"sssss",
"message_id"=>"4"},
"authenticity_token"=>"SMVfiolNAVPmLLU0eOWzx2jPFbujMtpyqQcs6A2Mxr0="}
显示会话转储 响应
标头:
{"Content-Type"=>"",
"Cache-Control"=>"no-cache"}