Setup User Authentication with Devise in Rails 7
Meta Description (155 characters): Easily add user registration, login, and logout to your Ruby on Rails 7 app with the Devise gem. Step by step authentication guide.
🧠 What You Will Learn in This Guide
In this guide, you will learn how to set up a user registration, login and logout system in your Ruby on Rails 7 application using the Devise gem. Devise eliminates the need to write user session management from scratch. It offers ready-made features such as password reset, e-mail verification, and session recall.
Purpose: To establish a secure, production-ready authentication system in Rails projects. Result: Users can register, log in, and manage their sessions.
⚙️ Prerequisites
Before you start, have the following:
- Ruby 3.0.2+ and Rails 7.0.3+
- Node.js (required for Rails Asset Pipeline)
- Basic knowledge of Ruby on Rails
- Active internet connection
1️⃣ Creating a New Rails App
Start a new Rails project:
rails new blog
This command creates a new project folder named
blog.
Change to the project directory:
cd blog
Start the Rails server:
bundle exec rails server
You can see the welcome page by going to http://localhost:3000.
2️⃣ Creating a Custom Home Page
To change the default page, specify the root route:
nano config/routes.rb
Add the following line:
Rails.application.routes.draw do
root to: "home#index"
end
Redirects the main URL to the
indexaction in the HomeController.
Create HomeController:
rails g controller Home index
Creates the necessary controller and view files.
Open app/views/home/index.html.erb:
<h1>Merhaba GenixNode Kullanıcısı!</h1>
Check by refreshing http://localhost:3000.
3️⃣ Installing and Configuring the Devise Gem
Stop the server (CTRL + C) and add Devise to the Gemfile:
gem "devise"
Install the gem:
bundle install
Start the Devise installation:
bundle exec rails g devise:install
This command creates the files
config/initializers/devise.rbandconfig/locales/devise.en.yml.
Adding Turbo Stream Compatibility
Edit the devise.rb file to make Devise compatible with Rails 7:
nano config/initializers/devise.rb
Add the following line into the Devise.setup block:
config.navigational_formats = ['*/*', :html, :turbo_stream]
This ensures Devise works error-free with Turbo Stream (Hotwire).
Add Flash Messages
To show notification messages:
nano app/views/layouts/application.html.erb
Add just above the line <%= yield %>:
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
You can show successful login or error messages.
4️⃣ Creating User Model and Database Migration
Create the User model with Devise:
bundle exec rails g devise user
This command creates the migration file for the
Usermodel and theuserstable. Additionally,devise_for :usersis automatically added to theconfig/routes.rbfile.
Update the database:
bundle exec rails db:migrate
Restart the Rails server:
bundle exec rails server
Test the registration form by going to http://localhost:3000/users/sign_up in the browser.
5️⃣ Adding Login / Logout Links to the Home Page
Edit the home view file:
nano app/views/home/index.html.erb
Add the following content:
<% if user_signed_in? %>
<div>Hoş geldin <%= current_user.email %>!</div>
<%= button_to "Çıkış Yap", destroy_user_session_path, method: :delete %>
<% else %>
<%= button_to "Giriş Yap", new_user_session_path %>
<%= button_to "Kayıt Ol", new_user_registration_path %>
<% end %>
<h1>Merhaba GenixNode Kullanıcısı!</h1>
Description:
- user_signed_in? → Checks whether the user is logged in or not.
- current_user → Returns the data of the user in the session.
Refresh the page: If you are not registered, you will see “Login / Register” buttons. Once you register, your email address will be displayed.
❓ Frequently Asked Questions (FAQ)
1. What modules does Devise include?
database_authenticatable, registerable, recoverable, rememberable and validatable. Additionally the confirmable and lockable modules can also be activated.
2. Why is Turbo Stream important?
In Rails 7, Devise works integrated with Hotwire (Turbo). Without this setting, you may receive the “undefined method user_url” error.
3. How do I list Devise routes?
You can see all user paths with the following command:
bundle exec rails routes
4. Can I customize devise forms?
Yes, you can edit views by copying them into the project:
bundle exec rails g devise:views
5. Why do I need to restart the server?
The devise initializer file is only loaded when Rails is started. It is necessary to restart the server for the new settings to take effect.
🎯 Result
Your Rails 7 app now has a secure, production-ready authentication system running Devise. Users can register, log in, and manage their sessions.
💡 You can now try performance and security together by hosting your application on the GenixNode platform.

