Installing devise gem

In the Gemfile, we add the ‘devise’ gem.

gem 'devise'

And install the gem to system via bundle.

$ bundle install

Execute the devise installation script.

$ rails generate devise:install
    create  config/initializers/devise.rb
    create  config/locales/devise.en.yml
=============================================================

Some setup you must do manually if you haven't yet:

  1. Ensure you have defined default url options in your environments files. Here
     is an example of default_url_options appropriate for a development environment
     in config/environments/development.rb:

       config.action_mailer.default_url_options = { :host => 'localhost:3000' }

     In production, :host should be set to the actual host of your application.

  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:

       root :to => "home#index"

  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:

       <p class="notice"><%= notice %></p>
       <p class="alert"><%= alert %></p>

  4. If you are deploying on Heroku with Rails 3.2 only, you may want to set:

       config.assets.initialize_on_precompile = false

     On config/application.rb forcing your application to not access the DB
     or load models when precompiling your assets.

  5. You can copy Devise views (for customization) to your app by running:

       rails g devise:views

==========================================================

The setup generates two files.

  1. config/initializers/devise.rb that contains all the devise setting.
  2. config/locales/devise.en.yml that describes the English wordings.

And now we can add devise to the User model. If the model isn’t existed, it will create one.

$ rails generate devise User
    invoke  active_record
    create    db/migrate/20131011153539_devise_create_users.rb
    create    app/models/user.rb
    invoke    test_unit
    create      test/unit/user_test.rb
    create      test/fixtures/users.yml
    insert    app/models/user.rb
     route  devise_for :users

The key file is the app/models/user.rb file. There is a devise method that set the enabled modules.

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me
  # attr_accessible :title, :body
end

Devise add a route setting to the routes.rb file.

Let’s take a look at the routes by running rake routes.

$ rake routes
          new_user_session GET    /users/sign_in(.:format)                    devise/sessions#new
              user_session POST   /users/sign_in(.:format)                    devise/sessions#create
      destroy_user_session DELETE /users/sign_out(.:format)                   devise/sessions#destroy
             user_password POST   /users/password(.:format)                   devise/passwords#create
         new_user_password GET    /users/password/new(.:format)               devise/passwords#new
        edit_user_password GET    /users/password/edit(.:format)              devise/passwords#edit
                           PUT    /users/password(.:format)                   devise/passwords#update
  cancel_user_registration GET    /users/cancel(.:format)                     devise/registrations#cancel
         user_registration POST   /users(.:format)                            devise/registrations#create
     new_user_registration GET    /users/sign_up(.:format)                    devise/registrations#new
    edit_user_registration GET    /users/edit(.:format)                       devise/registrations#edit
                           PUT    /users(.:format)                            devise/registrations#update
                           DELETE /users(.:format)                            devise/registrations#destroy
              album_photos GET    /albums/:album_id/photos(.:format)          photos#index
                           POST   /albums/:album_id/photos(.:format)          photos#create
           new_album_photo GET    /albums/:album_id/photos/new(.:format)      photos#new
          edit_album_photo GET    /albums/:album_id/photos/:id/edit(.:format) photos#edit
               album_photo GET    /albums/:album_id/photos/:id(.:format)      photos#show
                           PUT    /albums/:album_id/photos/:id(.:format)      photos#update
                           DELETE /albums/:album_id/photos/:id(.:format)      photos#destroy
                    albums GET    /albums(.:format)                           albums#index
                           POST   /albums(.:format)                           albums#create
                 new_album GET    /albums/new(.:format)                       albums#new
                edit_album GET    /albums/:id/edit(.:format)                  albums#edit
                     album GET    /albums/:id(.:format)                       albums#show
                           PUT    /albums/:id(.:format)                       albums#update
                           DELETE /albums/:id(.:format)                       albums#destroy
                      root        /                                           pages#index

Do you see the registration routes. If you try to remove the :registerable option in the User model class, you route becomes the following without the registrable path.

$ rake routes
      new_user_session GET    /users/sign_in(.:format)                    devise/sessions#new
          user_session POST   /users/sign_in(.:format)                    devise/sessions#create
  destroy_user_session DELETE /users/sign_out(.:format)                   devise/sessions#destroy
          album_photos GET    /albums/:album_id/photos(.:format)          photos#index
                       POST   /albums/:album_id/photos(.:format)          photos#create
       new_album_photo GET    /albums/:album_id/photos/new(.:format)      photos#new
      edit_album_photo GET    /albums/:album_id/photos/:id/edit(.:format) photos#edit
           album_photo GET    /albums/:album_id/photos/:id(.:format)      photos#show
                       PUT    /albums/:album_id/photos/:id(.:format)      photos#update
                       DELETE /albums/:album_id/photos/:id(.:format)      photos#destroy
                albums GET    /albums(.:format)                           albums#index
                       POST   /albums(.:format)                           albums#create
             new_album GET    /albums/new(.:format)                       albums#new
            edit_album GET    /albums/:id/edit(.:format)                  albums#edit
                 album GET    /albums/:id(.:format)                       albums#show
                       PUT    /albums/:id(.:format)                       albums#update
                       DELETE /albums/:id(.:format)                       albums#destroy
                  root        /                                           pages#index

Since this is a new model, the database should reflect the new table. Before we start running the server, we need migrate the database.

$ rake db:migrate
==  DeviseCreateUsers: migrating ==============================================
-- create_table(:users)
   -> 0.0331s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0026s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0015s
==  DeviseCreateUsers: migrated (0.0377s) =====================================

If we want to manage our own login view, we can generate the view from the gem and it will be used by rails.

$ rails generate devise:views
    invoke  Devise::Generators::SharedViewsGenerator
    create    app/views/devise/shared
    create    app/views/devise/shared/_links.erb
    invoke  form_for
    create    app/views/devise/confirmations
    create    app/views/devise/confirmations/new.html.erb
    create    app/views/devise/passwords
    create    app/views/devise/passwords/edit.html.erb
    create    app/views/devise/passwords/new.html.erb
    create    app/views/devise/registrations
    create    app/views/devise/registrations/edit.html.erb
    create    app/views/devise/registrations/new.html.erb
    create    app/views/devise/sessions
    create    app/views/devise/sessions/new.html.erb
    create    app/views/devise/unlocks
    create    app/views/devise/unlocks/new.html.erb
    invoke  erb
    create    app/views/devise/mailer
    create    app/views/devise/mailer/confirmation_instructions.html.erb
    create    app/views/devise/mailer/reset_password_instructions.html.erb
    create    app/views/devise/mailer/unlock_instructions.html.erb

Now we get all the devise view files so we can change whatever we want.

What’s next? We’re going to take a look at “Authenticating user”.

overlaied image when clicked on thumbnail

Makzan | Ruby on rails 101 | Table of Content