Skip to content

Step07: Adding Devise

Lev Brie edited this page Aug 2, 2013 · 8 revisions

Installing Devise


Note: These are the mose frequently used helper methods (assuming a User model) available to you after install: user_signed_in?, current-user, and user_session

  1. Make sure you have gem 'devise' in your Gemfile.

  2. $ rails g devise:install

  3. Add config.action_mailer.default_url_options = { :host => 'localhost:3000' } to config/environment/development.rb

  4. Add root to: 'static_pages#home' if you haven't yet (or whatever your root is).

  5. Flash messages should already be enabled if you followed the Bootstrap setup, otherwise add %= notice and %= alert to a shared partial (If you're confused about this see #9 of Step06).

  6. Add the following line to config/application.rb:

    # FOR DEVISE ON HEROKU!!!
    config.assets.initialize_on_precompile = false
  7. Run $ rails g devise user to create the User model

  8. Set config.scoped_views = true inside config/initializers/devise.rb to allow for multiple models in the future.

  9. Inside your migration file - db/migrate/..._devise_create_users.rb - uncomment out the lines below ## Confirmable, ## Lockable, and ## Token authenticatable if you wish to provide email confirmation and verification, lockouts after a certain number of failed attempts, and the ability to authenticate using a single access token (read more about single access tokens). Also uncomment the corresponding indexes.

  10. Run $ rake db:migrate

  11. Optionally add name and username to User model with $ rails g migration add_names_to_users name:string username:string, then $ rake db:migrate. If you plan on having a lot of users, also add indices to these columns with rails generate migration add_indices_to_users and then in the generated migration file:

    class AddIndicesToUsers < ActiveRecord::Migration
      def change
      end
      add_index :users, :name 
      add_index :users, :username, :unique => true
    end

If you added anything from #9


  • Add a comma followed by :lockable, :confirmable, :token_authenticatable to the devise call in app/models/user.rb.

  • Add Lockable: In config/initializers/devise.rb, uncomment the lines # config.lock_strategy = :failed_attempts, # config.unlock_keys = [:email], # config.unlock_strategy = :both and # config.maximum_attempts = 20 (we set it to 8).

  • Add Confirmable: In addition to what we've already done, uncomment config.allow_unconfirmed_access_for = 2.days in config/initializers/devise.rb to allow users to access the website without confirming for a specified amount of time (change to whatever you like). Set config.mailer_sender to the email address you want to be shown in your Devise::Mailer, i.e. config.mailer_sender = "info@rangular.io" Additional setup is still necessary. If you wish to perform this now, please go to Step08 of the Wiki.

  • Add Token Authenticatable: In config/initializers/devise.rb uncomment out # config.token_authentication_key = :auth_token if you plan to implement the versioned RESTful API for communicating with our Angular App.

Optional: Generate Desvise Views to Be Modefied for Your Model


  1. Run $ rails g devise:views users to generate our first scoped view.