How to Install and Configure Devise in a Rails App
Learn how to use Devise to give users the ability to log in and logout of the application, including all of the necessary security mechanisms such as encrypting the password.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

In this video, we are going to setup authentication using the devise library.

The first thing I do with almost every gem is to use the best available version. So, go to rubygems.org and check the latest version of devise. Go to your Gemfile, add this version against your gem and run the command bundle. This will ensure that your application is using the latest version.

Next, install devise with the command

rails generate devise:install

large

As you'll see, it prints out instructions when you run the generator and we're going to follow them to ensure the system is configured properly.

The first one is to update the development environment in config file, and to do that, go to development.rb and paste the code given in the instruction.

We already have the second instruction, and we are going to skip the third one because we'll design our own custom alerts.

Follow the fourth instruction by pasting that code in command line. This will generate the views for us.

large

We'll have to modify these views later as we don't want certain functionalities like user registration.

Next, we need to update the mailer, and for this, open config/initializers/devise.rb. In this file, find a variable called config.mailer_sender. Update the value with a valid email address from a domain you own as it requires authentication. If you're going to use it for just learning purposes, then you don't have to make any changes.

large

In the next step, we are going to create our user model with the code:

rails g devise User first_name:string last_name: string type:string

In this command, we are asking Rails to generate a User model with three attributes, namely: first_name, last_name and type.

large

In the next step, open the migration file that was created by this command.

large

You'll see that it has created a table along with the attributes you mentioned. Essentially, that command has created a file for us with all the information we want. Now, if you think you misspelled a word while creating, you can always change it here. Let's say, you mistyped "lst" for "last", then you can make the change in the file.

Next, run this command:

bundle exec rake db:migrate

This will create a schema file for us. Open the schema.rb and here you'll find the table and all the fields in it. You can also go to routes.rb to see how devise has added the routes for you.

medium

Lastly, start the rails server and open an incognito page in the browser since we haven't created a logout button yet.

To test, open the URL localhost:3000/users/sign_up:

large

Enter your details and click on sign in, and user account will be created though you can't see anything. To verify, you can verify the details in the terminal session:

large

and this will have the database commands that created the user. Alternately, we can open the rails console with the command rails c. Then, pull up the last record that was created with the command User.last

medium

So, everything is working now. We now have the ability to have users on our system. In the next guide, we'll learn about single table inheritance.

Resource