Show recent data using Devise in Rails 3

I have a Rails 3 application and it uses Devise for authentication.

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

I based the application in the following application:

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

I read the Devise GitHub wiki and noticed that it mentions that user events are being tracked, but I cannot find any information about access to information, etc.

Any help / advice would be greatly appreciated!

Thanks,

Danny

+4
source share
2 answers

The Devise documentation describes a trackable module that will do what you want. In your user model, enable the :trackable module as follows:

  devise :database_authenticatable, ... :trackable 

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

 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 will add the correct fields. In my user model, they look like this:

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

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

+9
source

Instead of user.last_sign_in_at use current_user.sign_in_at , since you want the last character to be entered into the user's system.

0
source

Source: https://habr.com/ru/post/1340960/


All Articles