[英]Rails messaging
I m create a messaging between users. 我在用户之间创建消息传递。 Take info from here .
从这里获取信息。
But then I go for new message, I have a error. 但是后来我收到新消息,我遇到了错误。
NoMethodError in MessagesController#new
undefined method `relation_delegate_class' for "Message":String
def new
@message = current_user.sent_messages.new
end
How can I fix it? 我该如何解决? Thanks.
谢谢。
Message model: 消息模型:
class Message < ActiveRecord::Base
belongs_to :sender, :class_name=>'User', foreign_key: 'sender_id'
belongs_to :recipient, :class_name=>'User', foreign_key: 'receiver_id'
end
User model: 用户模型:
class User < ActiveRecord::Base
has_many :sent_messages, class: 'Message', foreign_key: 'sender_id'
has_many :messages, class: 'Message', foreign_key: 'receiver_id'
end
Message controller: 消息控制器:
class MessagesController < ApplicationController
def new
@message = current_user.sent_messages.new
end
def create
@message = current_user.messages.new message_params
@message.receiver_id = @recipient.id
@message.save
end
private
def message_params
params.require(:message).permit(:text, :receiver_id, :sender_id)
end
def set_recipient
@recipient = User.find(params[:id])
end
end
In class
attribute on associations you should specify actual class , a constant like Message
. 在关联的
class
属性中,应指定实际的类 ,如Message
类的常量。 You could use "Message"
too, but only in class_name
. 您也可以使用
"Message"
,但只能在class_name
。
In fact, what the message says is "you're doing something unexpected with a String", the solution is to look what strings you may have in the wrong places. 实际上,该消息说的是“您正在使用字符串做意外的事情”,解决方案是查看在错误的位置可能有哪些字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.