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.
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.
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.
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:
password_digest attribute is used by Devise to store the hashed password.
Once the command completes, run the following command to create the database:
This will create a new database for our application.
Next, we'll install Devise by adding it to our
Gemfile and running
Add the following line to your
Then, run the following command to install the gem:
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.
Now that we've installed Devise, we need to configure it to work with our User model.
config/initializers/devise.rb file and uncomment the following lines:
config.secret_key = 'YOUR_SECRET_KEY'
config.mailer_sender = 'firstname.lastname@example.org'
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.
Now that we've configured Devise, we need to update our User model to work with Devise.
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
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:
Now that we've configured Devise and updated our User model, let's add authentication to our application.
config/routes.rb file and add the following lines:
root to: 'home#index'
This tells Rails to use Devise for user authentication and sets the root route to the
Next, create a new controller by running the following command:
rails generate controller Home index
This will create a new
HomeController with an
app/controllers/home_controller.rb file and add the following line to the
This line tells Rails to authenticate the user before allowing access to the
Now, start the Rails server by running the following command:
Open your web browser and navigate to
http://localhost:3000/. You should see a login page generated by Devise.
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.
Finally, let's update the views to display the user's name instead of their email.
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.
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!