DEV Community

Cover image for How To Set Up Rails Devise
Raymundo Alva
Raymundo Alva

Posted on

How To Set Up Rails Devise

Usually when I implement authentication I build it from scratch. Most of my projects have been like that. Even though I heard about devise I just felt comfortable building it this way because I felt that I had more choice. Finally after looking for more things to learn about I decided to give devise a try. I was very surprised with how easy it is to implement devise into rails. It makes authentication so painless. Today I am going to show how I set up devise using rails.

First we are going to start with a new application so run rails new like this.

$ rails new rails\_devise rails
Enter fullscreen mode Exit fullscreen mode

First we are going to go inside of our Gemfile and add the devise gem and then bundle. After that run the generator.

$ bundle install
$ rails g devise:install
Enter fullscreen mode Exit fullscreen mode

When you run the second command you will get a set of instructions that will need to be completed. First you will have to define default url options in your environment file. This is the example that is given in the instructions and will work just fine. Make sure to copy and paste it in config/environments/development.rb

config.action\_mailer.default\_url\_options = { host: 'localhost', port: 3000 }
Enter fullscreen mode Exit fullscreen mode

Next we have to make sure that we define a root route. So go to routes.rb and add this.

root to: "home#index"
Enter fullscreen mode Exit fullscreen mode

Next we have to add flash messages in our views layout. This is to make sure we get an alert if something is not right when entering the information into our form. Add this line of code in the body of app/views/layouts/application.html.erb

<p class="notice"><%= notice %></p>  
<p class="alert"><%= alert %></p>
Enter fullscreen mode Exit fullscreen mode

The final instruction is optional but I am going to use it to set up the devise views. This will make it easier to get started and show how devise works.

$ rails g devise:views
Enter fullscreen mode Exit fullscreen mode

We don’t have a home controller yet so let’s get that set up before we run the server.

$ rails g controller Home index  
$ rails s
Enter fullscreen mode Exit fullscreen mode

We have a home controller with and index action now. Now to finally start signing in users let’s generate a user model with the devise command and migrate.

$ rails g devise User  
$ rails db:migrate
Enter fullscreen mode Exit fullscreen mode

Now we have to look for the appropriate route. To easily see the needed route for the sign up page type this into the terminal.

$ rails routes | grep users
Enter fullscreen mode Exit fullscreen mode

Travel to the new_user_registration path and you will see a fully functioning sign up form thanks to devise views.

That was very straightforward and painless. I will be using devise for many of my projects in the future. Thanks for reading and happy coding! 😎

Top comments (0)