如何发送通知,用户收到私人消息 [英] how to send a notification a user received a private message
问题描述
def create
@message = current_user.messages.build
@message.to_id = params[:message][:to_id]
@message.user_id = current_user.id
@message.content = params[:message][:content]
if @message.save
flash[:success ] = "Private Message Sent"
end
redirect_to user_path(params[:message][:to_id])
end
我可以告诉发送者私人消息已发送,但是我不确定我如何通知接收者新的私人消息.
帮助将不胜感激.谢谢=)
首先,您可以像这样改善您的控制器:
def create
@message = current_user.messages.new(params[:message])
if @message.save
flash[:message] = "Private Message Sent"
end
redirect_to user_path(@message.to_id)
end
然后,在您的模型中:
# app/models/message.rb
class Message < ActiveRecord::Base
belongs_to :user
belongs_to :recipient, class_name: 'User', foreign_key: :to_id
has_many :notifications, as: :event
after_create :send_notification
private
def send_notification(message)
message.notifications.create(user: message.recipient)
end
end
# app/models/user.rb
class User < ActiveRecord::Base
has_many :messages
has_many :messages_received, class_name: 'Message', foreign_key: :to_id
has_many :notifications
end
# app/models/notification.rb
class Notification < ActiveRecord::Base
belongs_to :user
belongs_to :event, polymorphic: true
end
此Notification
模型允许您存储用户针对不同事件"的通知.您甚至可以存储是否已阅读通知,或者设置after_create
回调以向已通知的用户发送电子邮件.
此Notification
模型的迁移将是:
# db/migrate/create_notifications.rb
class CreateNotifications < ActiveRecord::Migration
def self.up
create_table :notifications do |t|
t.integer :user_id
t.string :event_type
t.string :event_id
t.boolean :read, default: false
t.timestamps
end
end
def self.down
drop_table :notifications
end
end
您可以在此处了解有关Rails关联选项的信息.. >
i created a messaging model where a user can send a private message to another user. however im not sure how to go about notifying the user he/she got a new message. does anyone have a way to go about doing this? or if there was a simple solution?
def create
@message = current_user.messages.build
@message.to_id = params[:message][:to_id]
@message.user_id = current_user.id
@message.content = params[:message][:content]
if @message.save
flash[:success ] = "Private Message Sent"
end
redirect_to user_path(params[:message][:to_id])
end
i can tell the sender that a private message was sent, but im not sure how i can notify the receiver a new private message was created.
help would be appreciated. thanks = )
First, you can improve your controller like this:
def create
@message = current_user.messages.new(params[:message])
if @message.save
flash[:message] = "Private Message Sent"
end
redirect_to user_path(@message.to_id)
end
Then, in your models:
# app/models/message.rb
class Message < ActiveRecord::Base
belongs_to :user
belongs_to :recipient, class_name: 'User', foreign_key: :to_id
has_many :notifications, as: :event
after_create :send_notification
private
def send_notification(message)
message.notifications.create(user: message.recipient)
end
end
# app/models/user.rb
class User < ActiveRecord::Base
has_many :messages
has_many :messages_received, class_name: 'Message', foreign_key: :to_id
has_many :notifications
end
# app/models/notification.rb
class Notification < ActiveRecord::Base
belongs_to :user
belongs_to :event, polymorphic: true
end
This Notification
model allows you to store a user's notifications for different "events". You can even store whether a notification has been read or not, or set an after_create
callback in order to send an email to the notified user.
The migration for this Notification
model would be:
# db/migrate/create_notifications.rb
class CreateNotifications < ActiveRecord::Migration
def self.up
create_table :notifications do |t|
t.integer :user_id
t.string :event_type
t.string :event_id
t.boolean :read, default: false
t.timestamps
end
end
def self.down
drop_table :notifications
end
end
You can read about the Rails associations options here.
这篇关于如何发送通知,用户收到私人消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!