向 RoR 消息收件箱系统添加通知
[英] Add notification to RoR messaging inbox system
本文介绍了向 RoR 消息收件箱系统添加通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有实习消息系统的 rails 应用程序.用户可以向其他用户发送消息.示例:用户 1 发送消息用户 2 可以反之亦然.
一切正常.但我想用通知功能升级这个系统.我想输入通知类型:
1) 在导航栏上2) 邮寄
问题:我不知道我该怎么做.
你能帮我吗?
对话表
class CreateConversations <ActiveRecord::迁移定义改变create_table : 对话做 |t|t.integer :sender_idt.integer :recipient_idt.时间戳结尾结尾结尾
消息表.在这段代码中,我有一个布尔值 :read.我认为解决方案可以在这里.你怎么看?
class CreateMessages 错误的t.时间戳结尾结尾结尾
conversation.rb
类对话:sender_id, class_name: '用户'归属于:收件人,:外国密钥 =>:recipient_id, class_name: '用户'has_many :messages, 依赖: :destroyvalidates_uniqueness_of :sender_id, :scope =>:recipient_id范围:之间,->(sender_id,recipient_id) 做where("(conversations.sender_id = ? AND对话.recipient_id =?) OR (conversations.sender_id = ? AND对话.recipient_id =?)", sender_id,recipient_id,recipient_id, sender_id)结尾结尾
消息.rb
类消息
conversations_controller.rb
class ConversationsController <应用控制器before_action :authenticate_user!# 获取/对话# 获取/conversations.json定义索引@users = User.all# 限制至少有一条消息的对话,并按上次更新排序@conversations = Conversation.joins(:messages).uniq.order('updated_at DESC')结尾# POST/对话# POST/conversations.json定义创建如果 Conversation.between(params[:conversation][:sender_id], params[:conversation][:recipient_id]).present?@conversation = Conversation.between(params[:conversation][:sender_id], params[:conversation][:recipient_id]).first别的@conversation = Conversation.create!(conversation_params)结尾重定向到会话消息路径(@conversation)结尾私人的# 使用回调在动作之间共享公共设置或约束.定义对话参数params.require(:conversation).permit(:sender_id,:recipient_id)结尾结尾
messages_controller.rb
class MessagesController <应用控制器before_action 做@conversation = Conversation.find(params[:conversation_id])结尾定义索引@messages = @conversation.messages如果@messages.length >10@over_ten = 真@messages = @messages[-10..-1]结尾如果参数[:m]@over_ten = 假@messages = @conversation.messages结尾如果@messages.last如果@messages.last.user_id != current_user.id@messages.last.read = true;结尾结尾@message = @conversation.messages.new结尾定义新@message = @conversation.messages.new结尾定义创建@message = @conversation.messages.new(message_params)如果@message.save重定向到会话消息路径(@conversation)结尾结尾私人的定义消息参数params.require(:message).permit(:body,:user_id)结尾结尾
/conversations/index.html.erb
<h3>邮箱</h3><div class="ui list"><div class="item"><% @conversations.each do |conversation|%><% 如果对话.sender_id == current_user.id ||session.recipient_id == current_user.id %><%如果conversation.sender_id == current_user.id %><% 收件人 = User.find(conversation.recipient_id) %><%其他%><% 收件人 = User.find(conversation.sender_id) %><%结束%>Conversation avec <%= link_to receiver.prenom,conversation_messages_path(conversation)%><%结束%><%结束%>
<div class="ui 段"><h3>所有用户</h3><div class="ui list"><% @users.each 做 |user|%><% if user.id != current_user.id %><div class="item"><%= user.prenom %><%= button_to 'Message me',conversations_path(conversation: { sender_id: current_user.id, receiver_id: user.id }), class: 'btn btn-primary m-t' %>
<%结束%><%结束%>
messages/index.html.erb
<% if @over_ten %><%= link_to '显示上一个', "?m=all" %><%结束%><div class="ui 段"><% @messages.each 做 |message|%><% if message.body %><% user = User.find(message.user_id) %><div class="item"><div class="内容"><div class="header"><strong><div class="imageavatarmessage"><%= image_tag user.avatar(:thumb), class:"imageavatarmessage" %></div><%= user.prenom %></strong><%= message.message_time %></div><div class="list"><div class="item"><i class="直角三角形图标"></i><%= message.body %>
<%结束%><%结束%>
<%= form_for [@conversation, @message], html: {class: "ui reply form"} do |f|%>
<%= f.text_area :body, class: "form-control" %>
<%= f.text_field :user_id, value: current_user.id, type: "hidden" %><div><%= f.submit "添加回复", class: "ui 蓝色标记的提交图标按钮" %>
<%结束%>
解决方案
我建议您阅读本教程以构建漂亮的通知系统https://www.devwalks.com/lets-build-instagram-part-6-notifications/
基本上,您必须创建新模型,使用您的 Message
模型设置依赖项并集成到控制器
对于电子邮件通知,它甚至更简单.只需创建新邮件程序并在 messages_controller
中的创建操作上触发它def 创建@message = @conversation.messages.new(message_params)如果@message.saveSendMessageMailer.new_message(@message).deliver_later重定向到会话消息路径(@conversation)结尾结尾
要创建邮件程序,您应该执行以下操作:
rails g mailer SendMessage
转到/app/mailers/send_message_mailer.rb 并添加动作,与控制器的建筑类型相同
def new_message(message)@消息 = 消息邮件(至:@message.user.email,主题:'嘿!这是你错过的')结尾
另外,用erb代码创建一个视图(电子邮件模板)和代码
app/views/send_message_mailer/new_message.html.erb
我不会深入研究这个,我想您可以弄清楚如何传递间隔(假设用户在线或已阅读消息时不发送)并区分接收者/发送者用户
再一次,Mailer 是相同类型的控制器.您可以根据需要传递任意数量的参数,并在邮件控制器和视图中使用模型嵌套
邮递员
def new_message(message, sender, receiver)结尾
控制器
SendMessageMailer.new_message(@message, @message.user, params[:receiver]).deliver_later
I have a rails app with intern messaging system. User can send message to other user. Example : User 1 send message User 2 can respond viceversa.
All works perfectly. But I want to upgrade this system with notfications functionnality. I want to type of notifications :
1) On navbar
2) By mail
Problem : I dont know how i can do this.
Can you help me ?
Conversations table
class CreateConversations < ActiveRecord::Migration
def change
create_table :conversations do |t|
t.integer :sender_id
t.integer :recipient_id
t.timestamps
end
end
end
Messages table. In this code I have a boolean :read. I think solution can be here. What do you think about this ?
class CreateMessages < ActiveRecord::Migration
def change
create_table :messages do |t|
t.text :body
t.references :conversation, index: true
t.references :user, index: true
t.boolean :read, :default => false
t.timestamps
end
end
end
conversation.rb
class Conversation < ActiveRecord::Base
belongs_to :sender, :foreign_key => :sender_id, class_name: 'User'
belongs_to :recipient, :foreign_key => :recipient_id, class_name: 'User'
has_many :messages, dependent: :destroy
validates_uniqueness_of :sender_id, :scope => :recipient_id
scope :between, -> (sender_id,recipient_id) do
where("(conversations.sender_id = ? AND conversations.recipient_id =?) OR (conversations.sender_id = ? AND conversations.recipient_id =?)", sender_id,recipient_id, recipient_id, sender_id)
end
end
Message.rb
class Message < ActiveRecord::Base
belongs_to :conversation
belongs_to :user
validates_presence_of :body, :conversation_id, :user_id
def message_time
created_at.strftime("%m/%d/%y at %l:%M %p")
end
end
conversations_controller.rb
class ConversationsController < ApplicationController
before_action :authenticate_user!
# GET /conversations
# GET /conversations.json
def index
@users = User.all
# Restrict to conversations with at least one message and sort by last updated
@conversations = Conversation.joins(:messages).uniq.order('updated_at DESC')
end
# POST /conversations
# POST /conversations.json
def create
if Conversation.between(params[:conversation][:sender_id], params[:conversation][:recipient_id]).present?
@conversation = Conversation.between(params[:conversation][:sender_id], params[:conversation][:recipient_id]).first
else
@conversation = Conversation.create!(conversation_params)
end
redirect_to conversation_messages_path(@conversation)
end
private
# Use callbacks to share common setup or constraints between actions.
def conversation_params
params.require(:conversation).permit(:sender_id, :recipient_id)
end
end
messages_controller.rb
class MessagesController < ApplicationController
before_action do
@conversation = Conversation.find(params[:conversation_id])
end
def index
@messages = @conversation.messages
if @messages.length > 10
@over_ten = true
@messages = @messages[-10..-1]
end
if params[:m]
@over_ten = false
@messages = @conversation.messages
end
if @messages.last
if @messages.last.user_id != current_user.id
@messages.last.read = true;
end
end
@message = @conversation.messages.new
end
def new
@message = @conversation.messages.new
end
def create
@message = @conversation.messages.new(message_params)
if @message.save
redirect_to conversation_messages_path(@conversation)
end
end
private
def message_params
params.require(:message).permit(:body, :user_id)
end
end
/conversations/index.html.erb
<div class="ui segment">
<h3>Mailbox</h3>
<div class="ui list">
<div class="item">
<% @conversations.each do |conversation| %>
<% if conversation.sender_id == current_user.id || conversation.recipient_id == current_user.id %>
<% if conversation.sender_id == current_user.id %>
<% recipient = User.find(conversation.recipient_id) %>
<% else %>
<% recipient = User.find(conversation.sender_id) %>
<% end %>
Conversation avec <%= link_to recipient.prenom, conversation_messages_path(conversation)%>
<% end %>
<% end %>
</div>
</div>
</div>
<div class="ui segment">
<h3>All Users</h3>
<div class="ui list">
<% @users.each do |user| %>
<% if user.id != current_user.id %>
<div class="item">
<%= user.prenom %> <%= button_to 'Message me', conversations_path(conversation: { sender_id: current_user.id, recipient_id: user.id }), class: 'btn btn-primary m-t' %>
</div>
<% end %>
<% end %>
</div>
</div>
messages/index.html.erb
<% if @over_ten %>
<%= link_to 'Show Previous', "?m=all" %>
<% end %>
<div class="ui segment">
<% @messages.each do |message| %>
<% if message.body %>
<% user = User.find(message.user_id) %>
<div class="item">
<div class="content">
<div class="header"><strong><div class="imageavatarmessage"><%= image_tag user.avatar(:thumb), class:"imageavatarmessage" %></div><%= user.prenom %></strong> <%= message.message_time %></div>
<div class="list">
<div class="item">
<i class="right triangle icon"></i>
<%= message.body %>
</div>
</div>
</div>
</div>
<% end %>
<% end %>
</div>
<%= form_for [@conversation, @message], html: {class: "ui reply form"} do |f| %>
<div class="field">
<%= f.text_area :body, class: "form-control" %>
</div>
<%= f.text_field :user_id, value: current_user.id, type: "hidden" %>
<div>
<%= f.submit "Add Reply", class: "ui blue labeled submit icon button" %>
</div>
<% end %>
解决方案
I would recommend you to go through this tutorial to build nice notification system
https://www.devwalks.com/lets-build-instagram-part-6-notifications/
Basically, you have to create new model, set dependencies with your Message
model and integrate to the controller
For the email notifications, it's even simpler.
Just create new mailer and fire it on create action in messages_controller
def create
@message = @conversation.messages.new(message_params)
if @message.save
SendMessageMailer.new_message(@message).deliver_later
redirect_to conversation_messages_path(@conversation)
end
end
EDIT:
To create mailer, you should do something like this:
rails g mailer SendMessage
Go to /app/mailers/send_message_mailer.rb and add action, same type of building as controllers
def new_message(message)
@message = message
mail(to: @message.user.email, subject: 'Hey! Here is what you missed')
end
Also, create an view (email template) and code with erb code
app/views/send_message_mailer/new_message.html.erb
Im not going deep into this, I guess you can figure out how to pass interval (let's say don't send if user is online or have read the message) and differentiate with receiver/sender users
Mailer, once again, is just same type of controller. You can pass as many params as you need and use model nesting inside mailer controller and views
mailer
def new_message(message, sender, receiver)
end
controller
SendMessageMailer.new_message(@message, @message.user, params[:receiver]).deliver_later
这篇关于向 RoR 消息收件箱系统添加通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文