在 Rails 中做通知 [英] Doing Notification in Rails

查看:39
本文介绍了在 Rails 中做通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的事情与人们听到 Rails 通知时的想法略有不同,因为我已经阅读了很多关于它的信息,但似乎没有帮助.

我想要做的是在特定表中创建一个新条目我希望某些用户(管理员)在他们的主页或他们所在的任何页面中收到通知,因为通知设备将在导航栏.

我该怎么做?我是一名中级 Rails 开发人员,我使用的是 rails 3.2.13.

提前致谢!

解决方案

您需要做几件事(我同意 japed 提出的方法):

  1. 创建具有必填字段的通知模型,我建议使其具有多态性,以便您可以访问稍后触发通知的对象(例如:具有 notifiable_type 和 notifiable_id 字段,如果文章创建触发了通知创建然后您可以执行诸如notification.notifiable 之类的操作并获取创建的文章)

  2. 放置将生成通知的回调.例如:如果您想在每次用户创建文章时通知管理员,您可以在文章模型的 after_create 方法中创建通知.

  3. 有办法知道管理员是否已阅读通知(例如,您可以向他显示未读通知的计数器).为此,我建议使用未读 gem:https://github.com/ledermann/unread

  4. 在管理员阅读通知后(例如:点击未读计数后)更新通知的状态(当然使用未读 gem,而不是直接在通知记录上),可能使用 ajax 请求.

What i want to do is a little different from what people think when they hear rails notification cause i've been reading up a lot about it but it didn't seem to help.

What i want to do is on creation of a new entry in a particular table i want certain users (admins) to get a notification in their home page or whatever page they're in, as the notification apparatus would be in the navigation bar.

How would i go about doing this ? i'm an intermediate rails developer, and i'm using rails 3.2.13.

Thanks in advance!

解决方案

There are a couple of things you'll want to do (I agree with the approach proposed by japed):

  1. Create the notification model with the required fields, I would suggest make it polymorphic so you can have access to the object that triggered the notification later (for example: having notifiable_type and notifiable_id fields, if an article creation triggered the notification creation you can then do something like notification.notifiable and get the article that was created)

  2. Place the callbacks that will generate the notification in place. for example: if you want to notify an admin every time a user creates an article, you can create the notification in the after_create method on the Article model.

  3. Have a way to know if the admin has read the notification or not (so you can show him a counter of unread notifications for example). For this I would recommend using the unread gem: https://github.com/ledermann/unread

  4. After the admin reads the notification (eg: after clicking on the unread counted) update the status of the notification (using the unread gem of course, not directly on the notification record), maybe with an ajax request.

这篇关于在 Rails 中做通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆