Install Devise on a Rails App

Techie     February 2022

Definition

Devise is an authentication gem for rails applications. It allows you to create users that can log in and out of your application.


1. Add devise gem

a). Open up your Gemfile and add this line

#/Gemfile

gem 'devise'
  

and run:

$ bundle install
	

Or:

b).Run:

$ bundle add devise
 	


2. Set up devise in your app

	
$ rails g devise:install


3. Configure Devise

Ensure you have defined default url options in your environments files. Open up config/environments/development.rb and add this line before the ‘end’ keyword:

# /config/environments/development.rb
   
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
   


4. Add the notification alert

Open up app/views/layouts/application.html.erb and add the following right above <%= yield %> :

 <!-- app/views/layouts/application.html.erb -->
 
 <% if notice %>
  <p class="alert alert-success"><%= notice %></p>
 <% end %>
 <% if alert %>
  <p class="alert alert-danger"><%= alert %></p>
 <% end %>
 


5. Setup the User model

$ rails g devise user
$ rake db:migrate


In order to do that go to, app/views/layouts/application.html.erb and add:

<!-- app/views/layouts/application.html.erb -->

<p class="navbar-text pull-right">
 <% if user_signed_in? %>
  Logged in as <strong><%= current_user.email %></strong>.
  <%= link_to 'Edit profile', edit_user_registration_path, :class => 'navbar-link' %> |
  <%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link'  %>
 <% else %>
  <%= link_to "Sign up", new_user_registration_path, :class => 'navbar-link'  %> |
   <%= link_to "Login", new_user_session_path, :class => 'navbar-link'  %>
  <% end %>
</p>


7. Redirect the User

Finally, force the user to redirect to the login page if the user was not logged in. Open up app/controllers/application_controller.rb and add the following after the protect_from_forgery with: :exception. :

 # /controllers/application_controller.rb
 
 # protect_from_forgery with: :exception

 before_action :authenticate_user!
  

Make sure your rails server is running, open http://localhost:3000/users/sign_up and create your user account.


8. Customize Devise Views

You can style the devise views according to your preference. Run the command below to generate the views and then provide css style for them:

$ rails generate devise:views


Thanks for reading, see you in the next one!