是否有任何 gem 或库可用于向用户显示一次消息?我特别在寻找可以让我:
- 用户首次登录时显示“欢迎登机”完整覆盖。(而且只在第一次)
- 一个较小的下拉覆盖,通知用户在接下来的 24 小时内可能会有维护,或者他们需要做一些事情(填写他们的个人资料等)。这将显示直到被解雇。
我对此有点坚持,因为我不知道这些消息通常被称为什么。这些一次性消息有名字吗?
是否有任何 gem 或库可用于向用户显示一次消息?我特别在寻找可以让我:
我对此有点坚持,因为我不知道这些消息通常被称为什么。这些一次性消息有名字吗?
there are many ways to implement such a notification system, your requirements are pretty basic so I'll stick with a basic approach using flash
Show Message on the first login:
You should keep track of ther user's last login and maybe count the logins, so what you could do:
Add a field "last_login_at" to your User Model which gets updated every time the user logs in, set it to NULL after registration. When the user logs in you check if the last_login_at
is NULL if yes you set a message to the flash like flash[:first_login] = true
then you can render a special welcome message in your template if this flag is set.
A better approach is to add a field like login_counter
which gets incremented every time the user logs in, so if it is zero the user is logging in for the first time. A login counter can also help you to identify "active" users who log in often etc.
There is a popular authentication system called devise which has these (and many more!) features built in: https://github.com/plataformatec/devise
Show maintenance warnings etc.
I'd create a new model called SystemMessage
which has fields like:
id | message_type | title | body | valid_from | valid_until
The message_type
attribute describes what kind of message it is like "maintenace notice", "new features" etc. dependent on the type of the message you could render different templates and adjust the look of the message.
The valid_from
and valid_until
attributes lets you control in which time frame the message should appear.
On every request you fetch these messages and show them to the user, you can also cache the messages on a regular basis to reduce the amount of queries. Have a look at the rails guide about caching: http://guides.rubyonrails.org/caching_with_rails.html
To inform users that are idle on a page you can implement an AJAX request which fires every five minutes or so and fetches the messages and checks if there are any messages newer than the ones already displayed.
If you need to show user-specific messages (like you got 5 new mails, someone commented on your post etc.) in real-time you could use a pub-sub messaging system like 'Faye' (tutorial: http://railscasts.com/episodes/260-messaging-with-faye or just google for "rails 3 faye")
I hope this is a starting point for you :)