Rails 3 + Devise:user_signed_in?为数据库中的不同用户? [英] Rails 3 + Devise: user_signed_in? for a different user in the database?

查看:138
本文介绍了Rails 3 + Devise:user_signed_in?为数据库中的不同用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,我想为给定的用户添加一个在线状态。

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.

在用户表中有一个is_online列的问题是很难看到谁是活动的在网站上我将添加一个名为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 + Devise:user_signed_in?为数据库中的不同用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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