加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Ruby on Rails ActiveRecord :: AssociationTy

发布时间:2020-12-17 03:01:08 所属栏目:百科 来源:网络整理
导读:我正在尝试为我的网站创建一个私人消息系统,我目前正致力于消息回复.我遇到了ActiveRecord :: AssociationTypeMismatch问题.这是错误消息: RepliesController #create中的ActiveRecord :: AssociationTypeMismatch 消息(#58297820)预期,得到字符串(#1635350
我正在尝试为我的网站创建一个私人消息系统,我目前正致力于消息回复.我遇到了ActiveRecord :: AssociationTypeMismatch问题.这是错误消息:

RepliesController #create中的ActiveRecord :: AssociationTypeMismatch

消息(#58297820)预期,得到字符串(#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

我很感激提供的任何帮助.我会一直试图找出问题所在.

谢谢.

更新: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'

请求

参数:

{"commit"=>"Reply","reply"=>{"message"=>"sssss","message_id"=>"4"},"authenticity_token"=>"SMVfiolNAVPmLLU0eOWzx2jPFbujMtpyqQcs6A2Mxr0="}

显示会话转储
响应

头:

{"Content-Type"=>"","Cache-Control"=>"no-cache"}

解决方法

您的问题是您与消息有关系,并且您还希望在名为message的同一模型上使用文本字段.创建关系时,您可以访问一些新方法,这些方法将踩到其他getter和setter的脚下.

要解决此问题,请将文本字段的名称更改为其他名称,并反映控制器中的更改.

如果您需要更多细节,请告诉我.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读