显示上次登录的使用设计细节的Rails 3 [英] Display last logged in details using Devise in Rails 3

查看:124
本文介绍了显示上次登录的使用设计细节的Rails 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rails 3应用程序和它的使用制定了认证。

I have a Rails 3 application and it's using Devise for the authentication.

我想显示用户的管理表中的每个用户最后登录的日期和时间。

I would like to display the date and time each user last logged in within the administration table of users.

我已经基于下列应用程序的应用

I have based the application on the following application:

https://github.com/dannymcc/rails3-base

我已经通过制定GitHub的维基阅读并注意到它提到用户事件是可追踪的,但我不能找到有关访问的信息等任何信息。

I have read through the Devise GitHub wiki and notice that it mentions that user events are trackable, but I can't find any information regarding accessing the information etc.

任何帮助/建议将不胜AP preciated!

Any help/advice would be greatly appreciated!

谢谢,

丹尼

推荐答案

文件概述了可跟踪模块,它会做什么你要。在您的用户模型,包括:可追踪模块,像这样:

The Devise documentation outlines the trackable module which will do what you want. In your user model, include the :trackable module like so:

  devise :database_authenticatable, 
      ...
      :trackable

和确保您的数据库具有正确的领域。不知道怎么做,如果你已经有一个用户表,但添加字段与正确的名称和类型应该做的伎俩。我的迁移创建我的用户表看起来像这样:

And make sure your database has the right fields. Not sure how do this if you already have a user table, but adding fields with the right names and types should do the trick. My migration to create my users table looks like so:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :name
      t.string :email

      t.database_authenticatable :null => false
      t.recoverable
      t.rememberable
      t.trackable

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

t.trackable 将添加正确的字段。在我的用户模型中,他们是如下:

The t.trackable will add the correct fields. In my user model, they're as follows:

sign_in_count: integer, 
current_sign_in_at: timestamp, 
last_sign_in_at: timestamp, 
current_sign_in_ip: string, 
last_sign_in_ip: string

然后,你可以做 user.last_sign_in_at 和检查的的strftime 对文档如何输出的时候你想要的格式。

Then you can just do user.last_sign_in_at and check the strftime documentation on how to output the time in the format you want.

这篇关于显示上次登录的使用设计细节的Rails 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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