简体   繁体   English

Rails 4:内部消息传递系统

[英]Rails 4: internal messaging system

I'm trying to write a simple messaging system for signed up users. 我正在尝试为注册用户编写一个简单的消息传递系统。 I realize there are gems for this but I'm trying to roll my own simpler one. 我意识到这是有宝石的,但我正在尝试推出自己更简单的宝石。

I've set up my models as follows: 我已经按照以下步骤设置了模型:

class Conversation < ActiveRecord::Base
  has_many :messages, dependent: :destroy
  belongs_to :sender, class_name: "User"
  belongs_to :receiver, class_name: "User"
  validates_presence_of :sender_id, :receiver_id
end

class Message < ActiveRecord::Base
  belongs_to :conversation
end

class User < ActiveRecord::Base
  has_many :conversations, foreign_key: "sender_id"
  has_many :recipients, through: :conversations, source: :receiver
end

I've set my routes and controller up so that I can make new conversations, and add messages to those conversations. 我已经设置了路由和控制器,以便可以进行新的对话,并向这些对话添加消息。 However, I'm trying to figure out how I can make it so that only a logged in user can start a conversation with ONE other user. 但是,我试图弄清楚如何做到这一点,以便只有登录的用户才能与另一个用户开始对话。 No other users should be able to access that conversation. 没有其他用户应该能够访问该对话。

Is this a permissions (cancan) thing or should this be defined by some controller logic? 这是权限(cancan)的事情,还是应该由某些控制器逻辑定义?

Thanks! 谢谢!

This should be defined in controller logic such that only users who are conversing get access to the conversation between them. 这应该在控制器逻辑中定义,以便只有正在交谈的用户才能访问他们之间的对话。 The two users info (mainly their user ids) should be stored along with conversation, so that the restriction can be applied. 应该将两个用户信息(主要是其用户ID)与对话一起存储,以便可以应用限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM