我正在我的应用程序中实现我自己的用户消息传递功能,但我很难保留receiver_id
.
用户型号:
class User < ActiveRecord::Base
has_many :tickets
has_many :sent_messages, :class_name => 'Message', :foreign_key => 'sender_id', :dependent => :destroy
has_many :received_messages, :class_name => 'Message', :foreign_key => 'receiver_id', :dependent => :destroy
end
消息模型:
class Message < ActiveRecord::Base
belongs_to :sender, :class_name => 'User', :foreign_key => 'sender_id'
belongs_to :receiver, :class_name => 'User', :foreign_key => 'receiver_id'
end
消息控制器:
def new
@user = User.find( params[:user_id] )
@message = Message.new(
:receiver => @user )
end
def create
@message = current_user.sent_messages.create( params[:message] )
redirect_to( tickets_path, :message => 'Message has been sent.' )
end
receiver_id
在数据库中保持为空。
谢谢您的帮助!
编辑
此处的路由问题 -票证有可以发送消息的用户 - rails 消息传递