English 中文(简体)
发送通知的用户收到私人信息
原标题:how to send a notification a user received a private message

i 创立了一个信息发布模式,用户可以向另一个用户发出私人信息。 然而,不能确定如何通知用户他/她收到新的信息。 是否有办法做到这一点? 或者是否有简单的解决办法?

    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 能够告诉发送人,发出了私人信息,但无法确定如何通知接收人新的私人信息。

希望得到帮助。 页: 1

最佳回答

首先,你可以提高你的控制力,如:

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模型允许你储存不同“events”的用户通知。 您甚至可以储存通知是否读到,或者在_create <>/code>后建立,以便向通知用户发送电子邮件。

The migration for this Notification model will 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

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html” rel=“nofollow”>。

问题回答

通知接收人有多种方式。 你可以有一个工人程序,发出电子邮件通知,或者在你的网站上列入“盒子”,表明有人等待的信息有多少。

You could also show a "flash" message to the recipient. You would do this by for instance including on a base template some code to check if there are any unread messages that have not had a notification delivered yet; if there aren t, nothing happens, and if there are, then a notification is displayed and the fact that the notif was displayed is recorded so that it won t be displayed a second time.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...