Install Devise for Authentication in a Rails Application
Securing an application is important because you don't want all users to access all parts of the application, and you can control this access with their credentials like username and password. In this lesson, we are going to be working on this authentication.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

Securing an application is important because you don't want all users to access all parts of the application, and you can control this access with their credentials like username and password. In this lesson, we are going to be working on this authentication. You can also use this Devise installation guide as a reference that you can look back at in the future when you need to integrate authentication into a Rails application.

To get started, open your Gemfile, and add a gem called Devise. To do that, type:

gem 'devise'

Next, go to your console and run the command bundle and this will build your application including all the dependencies. After building, you'll have to run the devise generator, and to do that, run:

rails generate devise:install

This will generate quite a bit of information for you.

large

If you see, it has created two files, with the first one being an initializer and the second one being a locales. Now, we have to customize the devise.rb file, but we will not be changing any locales in the devise.en.yml file.

This is how your devise.rb file looks like:

large

It has a lot of information, and it may be a good idea to read through it all for you to get an understanding. In this project though, we will not be changing anything except the config.mailer_sender. If you see the existing string itself says that you should change it at the time of initialization, and it should contain a valid email id from which an email would be sent out if a user needs a link for a new password.

For now, I'm going to change it to `do-not-reply@reif.io' because I want the password link to be sent out to the user from this email id.

large

There is nothing else to change here, though you can change the config.password_length field if you want to customize password validation. Also, you can change the config.reset_password_within if you want the password change link to be valid only for a certain duration.

Now, go back to the terminal and you can see a set of instructions for us.

large

The first item talks about the configuration changes you need to make if you want to send out emails. But for this project, we are not going to do this. We have completed the second item too, and I hope you remember how to set routes.

To do the third item on the list, go to application.html.erb.

large

Now, go to the shared folder and create a file called _alerts.html.erb. Paste the code under item 3 in the terminal to this file. Go to your application.html.erb file, and add this partial with the code:

<%= render "shared/alerts" %>

Going back to the instructions on the terminal, we don't have to worry about item 4 because it is related to pre-compiling assets. The last item will generate all our view codes including the pages needed for registration and edits. When you run this command, this is what you can see:

You can see all these files under views/devise.