Rails 3 + 设计:user_signed_in?对于数据库中的不同用户? [英] Rails 3 + Devise: user_signed_in? for a different user in the database?
问题描述
我正在构建一个应用程序,我想在其中为给定用户添加在线状态.
I'm building an app where I want to add an online status for a given user.
我知道 Devise 有一个方法 user_signed_in?内置以检查使用该应用程序的用户是否已登录.但是当我尝试将它用于不同的用户时:
I know that Devise has a method user_signed_in? built in to check if the user who is using the app is signed in or not. But when I try to use it for a different user like this:
user_signed_in?(user)
user.user_signed_in?
我显然得到了一个未定义的方法错误.
I obviously get an undefined method error.
Devise 有这方面的方法还是我必须自己编写?一种方法是在用户模型中存储给定用户的在线状态.对此的最佳解决方案是什么?
Does Devise have a method for this or do I have to write my own? One approach was to store the online status of a given user in the user model. What's the best solution to this?
推荐答案
我不是 Devise 的作者,但据我所知,Warden/Devise 都没有跟踪谁登录.
I'm not the author of Devise, but from what I can tell of Warden / Devise neither keep track of who is logged in.
在 User 表中有一个 is_online 列的问题是很难看到谁在网站上处于活动状态.我会在您的 User 模型中添加一个名为 last_seen 的列作为日期时间,并在每次用户请求页面时使用 Devise 更新它.然后,您可以轻松添加 User.online_count 方法,或者查看用户是否在过去 5 分钟内访问过网站.
The problem with having an is_online column in the User table is that it is difficult to see who is active on the website. I would add a column to your User model called last_seen as a date-time, and update that with Devise every time the user requests a page. You could then easily add a User.online_count method or also see if a user has been seen at the website in the last 5 minutes.
这篇关于Rails 3 + 设计:user_signed_in?对于数据库中的不同用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!