First we have migration, db/migrate/20160511101503_devise_create_users.rb
class DeviseCreateUsers < ActiveRecord::Migration
create_table(:users) do |t|
t.string :name // Manually Included
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
t.integer :sign_in_count, default: 0, null: false
t.string :unconfirmed_email # Only if using reconfirmable
t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
t.string :unlock_token # Only if unlock strategy is :email or :both
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
add_index :users, :confirmation_token, unique: true
add_index :users, :unlock_token, unique: true
In migration file uncomment everything, because we are going to need everything. Except the headings
we have included one extra fields in our migration, this will going to include in the users table, when we migrate our database.
Now we have app/models/user.rb file,
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :lockable
It consists of various methods
1. database_authenticatable: This method will authenticate user, It checks whether the User is present in our database or not. 2. registrable: This method gives the privilege to user to get signup and can update or delete their profile too. 3. recoverable: when User could not able to login and want to reset their password, then this method comes into play. 4. rememberable: It stores your credentials in the browser cookie, when next time you try to login it just fill your credentials automatically. 5. trackable: It keeps track, that how many time you logged in, from which IP address and the Time of login 6. validatable: This method checks whether the email is in correct format or not, whether your password is upto the characters you have set. you can customize it. 7. confirmable: If you want to confirm that user had provided the correct email or not, then this method is used. 8. lockable: It lock-out the user if the number of unsuccessful attempts exceeds.
Now, we have enough interaction with model and migration, We are going to run it.
this command will create a table in your database named as users.
CREATE TABLE users
id serial NOT NULL,
name character varying,
email character varying NOT NULL DEFAULT ''::character varying,
encrypted_password character varying NOT NULL DEFAULT ''::character varying,
reset_password_token character varying,
reset_password_sent_at timestamp without time zone,
remember_created_at timestamp without time zone,
sign_in_count integer NOT NULL DEFAULT 0,
current_sign_in_at timestamp without time zone,
last_sign_in_at timestamp without time zone,
confirmation_token character varying,
confirmed_at timestamp without time zone,
confirmation_sent_at timestamp without time zone,
unconfirmed_email character varying,
failed_attempts integer NOT NULL DEFAULT 0,
unlock_token character varying,
locked_at timestamp without time zone,
created_at timestamp without time zone,
updated_at timestamp without time zone
We are going to generate views for the devise.
rails g devise:views users
Devise give you the privilege to generate views for the particular method.
rails g devise:views -v mailer registrations
As we have generated views for devise, we can also generate its controllers.