Rails – Setup Devise for Rails

Devise is a Rails gem which could help you managing the authentication of your Rails project.

This post followed the steps in Railscasts – Introducing Devise. I made my own notes here such that i don’t need to watch the tutorial again. It is highly recommended to go through the video.

1. Create a new empty project.

  • rails new <devise_trial>

 

2. Add the following line in Gemfile to include the Devise gem.

  • gem ‘devise’

 

3. Install the newly added gems

  • bundle install

 

4. Generate the Devise files

  • rails g devise:install

 

5. Add the config.action_mailer.default_url_options in <project_root>/config/environments/development.rb.

...
  # Only use best-standards-support built into browsers
  config.action_dispatch.best_standards_support = :builtin

  # Add the config.action_mailer.default_url_options
  config.action_mailer.default_url_options = { :host => 'localhost:3000' }
end

 

6. Generate the Authentication model which is User in this example.

  • rails g devise User

 

7. Migrate the database. A users table is created.

  • rake db:migrate

 

8. Add the root route in <project_root>/config/routes.rb

...
  # The following line is created by Devise automatically
  devise_for :users

  # Let's add the root route
  root :to => "home#index"
...

 

9. Check the routes by the rake routes command. There should be a list of /users… routes as depicted as follow.

 

10. Start your application by rails s and register a new account @ http://localhost:3000/users/sign_up.
 

11. Open rails console and input User.first to check your newly registered account
 

Done =)

Reference:

12 thoughts on “Rails – Setup Devise for Rails”

  1. worked perfectly! other solutions had all kinds of workarounds to get it working, this was nice & straight forward.

    Like

  2. Very simple to follow. I got this up and running on my local host within minutes.

    I was actually looking for some help. I want to run this on Heroku and I figured changing the host in config/environments/development.rb would work if it was i.e: http://my-heroku-app.com but after i deployed my heroku after updating my gemfile as well as running heroku rake db:migrate, all my models were created but the problem is:

    going to http://my-heorku-app.com/users/sign_up wouldn’t show up. any suggestions? Thank you.

    Like

  3. This is a fantastic solution! It worked perfectly. Would you happen to have example for setting up mulitiple models to use one form?

    Like

Leave a reply to maurice Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.