Building A Ruby On Rails App With Devise For Authentication

May 05, 2023

Ruby on Rails is a popular web development framework that allows developers to create web applications quickly and easily. One of the most important aspects of any web application is user authentication, which ensures that only authorised users can access the application's resources. Devise is a powerful authentication library for Ruby on Rails that simplifies the authentication process and provides a lot of built-in functionality.

In this guide, I'll walk through the process of building a Ruby on Rails application with Devise for user authentication. I'll assume that you have no prior experience with web development or the Ruby programming language, and I'll start from the very beginning.

Prerequisites

Before we get started, you'll need a few things:

  • A text editor: I recommend using Visual Studio Code or Atom.
  • Ruby and Rails installed on your computer: Follow the instructions on the Ruby on Rails website to install Ruby and Rails on your computer.
  • Basic knowledge of the command line: I'll be using the command line to run commands throughout this guide.

Step 1: Create a new Rails application

To create a new Rails application, open your terminal and navigate to the directory where you want to create your application. Then, run the following command:

rails new myapp

This will create a new Rails application in a directory called myapp. It may take a few minutes to complete.

Step 2: Create a User model

Now that we have a new Rails application, let's create a User model. In Rails, models represent the data in our application and are responsible for interacting with the database.

To create a new model, run the following command:

rails generate model User email:string password_digest:string

This will generate a new User model with two attributes: email and password_digest. The password_digest attribute is used by Devise to store the hashed password.

Once the command completes, run the following command to create the database:

rails db:create

This will create a new database for our application.

Step 3: Install Devise

Next, we'll install Devise by adding it to our Gemfile and running bundle install.

Add the following line to your Gemfile:

gem 'devise'

Then, run the following command to install the gem:

bundle install

Once the installation is complete, run the following command to generate the Devise configuration files:

rails generate devise:install

This will generate a config/initializers/devise.rb file that we'll use to configure Devise.

Step 4: Configure Devise

Now that we've installed Devise, we need to configure it to work with our User model.

Open the config/initializers/devise.rb file and uncomment the following lines:

config.secret_key = 'YOUR_SECRET_KEY'
config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com'

Replace YOUR_SECRET_KEY with a secret key of your choice. This key is used to encrypt session data and should be kept secret.

Next, we need to generate the Devise views. Run the following command:

rails generate devise:views

This will generate a set of views that we'll use for user registration, login, and password reset.

Step 5: Update the User model

Now that we've configured Devise, we need to update our User model to work with Devise.

Open the app/models/user.rb file and add the following line:

devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

This line tells Devise which authentication modules to use for our User model. In this case, we're using the database_authenticatable module for email/password authentication, the registerable module for user registration, the recoverable module for password reset, the rememberable module for session management, and the validatable module for email and password validation.

Next, run the following command to create a migration for the User model:

rails generate migration AddFieldsToUser name:string

This will create a migration file that adds a name attribute to the User model.

Open the migration file (db/migrate/xxxxxxxxxx_add_fields_to_user.rb) and add the following line to the change method:

add_column :users, :name, :string

This will add a name column to the users table in the database.

Then, run the following command to run the migration and update the database:

rails db:migrate

Step 6: Add authentication to the application

Now that we've configured Devise and updated our User model, let's add authentication to our application.

Open the config/routes.rb file and add the following lines:

devise_for :users
root to: 'home#index'

This tells Rails to use Devise for user authentication and sets the root route to the home#index action.

Next, create a new controller by running the following command:

rails generate controller Home index

This will create a new HomeController with an index action.

Open the app/controllers/home_controller.rb file and add the following line to the index action:

before_action :authenticate_user!

This line tells Rails to authenticate the user before allowing access to the index action.

Now, start the Rails server by running the following command:

rails server

Open your web browser and navigate to http://localhost:3000/. You should see a login page generated by Devise.

Step 7: Create a user account

To create a user account, click the "Sign up" link on the login page and fill out the registration form. Once you submit the form, you should be redirected to the home page.

Step 8: Logout and login

To logout, click the "Logout" link in the navigation bar. Then, try to access the home page again. You should be redirected to the login page.

To login, fill out the login form on the login page with the email and password you used to create your account. Once you submit the form, you should be redirected to the home page.

Step 9: Update the views

Finally, let's update the views to display the user's name instead of their email.

Open the app/views/layouts/application.html.erb file and replace the following line:

<%= current_user.email %>

With the following line:

<%= current_user.name %>

This will display the user's name instead of their email in the navigation bar.

Conclusion

Congratulations, you've successfully built a Ruby on Rails application with Devise for user authentication! We covered a lot of ground in this guide, from creating a new Rails application to configuring Devise and adding authentication to our application.

While this guide is a great starting point, there is still a lot more to learn about Ruby on Rails and web development in general. I encourage you to continue exploring and experimenting with Ruby on Rails, as it's a powerful and flexible framework with a lot of potential.

If you have any questions or feedback, feel free to get in touch. Happy coding!

Are you looking to take the next step as a developer? Whether you're a new developer looking to break into the tech industry, or just looking to move up in terms of seniority, book a coaching session with me and I will help you achieve your goals.