Rails notification system

隐身守侯 提交于 2020-01-01 18:47:07

问题


I need to create a facebook-like notification system for an app I am working on. I was wondering what suggestions any of you have for how to go about doing this? If possible, I would like to avoid using the database for the notifications, if this is feasible I'd love to know how.

Thanks in advance


回答1:


It wasn't clear in the question if notices needed to persist across sessions or user-to-user (who may not be online at the same time); however, I had a similar need on a Rails app and implemented it through a Notice ActiveRecord model. It's used for broadcasting downtime and other pending events coming up across every page on the site. Notices will show ahead and during their scheduled times.

class Notice < ActiveRecord::Base
  validates_presence_of  :header
  validates_presence_of  :body
  validates_presence_of  :severity
  validates_presence_of  :start_time
  validates_presence_of  :end_time
  validates_inclusion_of :severity, :in => %( low normal high )
end

Since it needed to be displayed everywhere, a helper method was added to ApplicationHelper for displaying it consistently:

module ApplicationHelper
  def show_notifications
    now = DateTime.now.utc
    noticeids = Notice.find(:all, :select=>"id", :conditions=>['start_time >= ? or (start_time <= ? and end_time >= ?)', now, now, now])
    return "" if noticeids.count == 0 # quick exit if there's nothing to display
    # ...skipping code..
    # basically find and return html to display each notice
  end
end

Lastly, in the application layout there's a tiny bit of code to use the ApplicationHelper#show_notifications method.




回答2:


Rails has a default notification system - the flash. You could pass on your own messages around in the flash and then display them in any way you want on the front-end with some CSS and JS. Do google around for some plugins to see if they suit your requirements, though it should not be very hard to implement on on your own.



来源:https://stackoverflow.com/questions/7552834/rails-notification-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!