In this article, you will learn how to use devise gem.
How to use devise gem
Devise is a popular authentication gem for Ruby on Rails applications that provides out-of-the-box functionality for user registration, login, and other authentication-related features. Here’s a brief guide on how to use Devise in your Rails application:
Add Devise to your Gemfile and install it:
Run bundle install to install the gem.
Generate a Devise configuration file by running:
rails generate devise:install
Generate a User model using Devise by running:
rails generate devise User
Run the database migrations to create the User table:
Configure the available authentication modules for Devise in the config/initializers/devise.rb file. For example, you can enable email confirmation, password complexity rules, and rememberable sessions.
Add authentication filters to your controllers by using the before_action method. For example:
This will ensure that only authenticated users can access the protected actions.
Use the Devise helpers in your views to display authentication-related content, such as links to the login and registration pages. For example:
<%= link_to "Login", new_user_session_path %> <%= link_to "Register", new_user_registration_path %>
Customize the Devise views to match your application’s design by running:
rails generate devise:views
This will generate the Devise views that you can modify as needed.