我正在使用 device which storecurrent_sign_in_at
and last_sign_in_at
日期时间。
但是假设用户一个月前登录,但上次查看页面是在 5 分钟前?
有什么办法可以显示(“用户最后一次出现是在 5 分钟前”).
这个怎么样:
-
创建迁移以向用户添加新字段来存储用户上次查看的日期和时间:
rails g migration add_last_seen_at_to_users last_seen_at:datetime
-
将之前的操作回调添加到您的应用程序控制器:
before_action :set_last_seen_at, if: proc { user_signed_in? }
private
def set_last_seen_at
current_user.update_attribute(:last_seen_at, Time.current)
end
这样,在当前用户执行的每个请求(即活动)中,他/她最后一次看到的 at 属性都会更新为当前时间。
但请注意,如果您有许多登录用户,这可能会占用您应用程序的一些资源,因为这将在登录用户请求的每个控制器操作之前执行。
如果性能是一个问题,请考虑在步骤 2 中添加以下限制机制(在本示例中,限制为 15 分钟):
before_action :set_last_seen_at, if: proc { user_signed_in? && (session[:last_seen_at] == nil || session[:last_seen_at] < 15.minutes.ago) }
private
def set_last_seen_at
current_user.update_attribute(:last_seen_at, Time.current)
session[:last_seen_at] = Time.current
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)