Create a Blog Application with Ruby on Rails 7. First Part: Create the User Resource

In this series, we'll create a simple blog application using Ruby on Rails 7. We'll have a Post resource and a User resource. We'll implement authentication for our users. Users will be able to create new posts, publish them, update them, and delete them.

All Parts:

  1. Create a Blog Application with Ruby on Rails 7. First Part: Create the User Resource
  2. Create a Blog Application with Ruby on Rails 7. Second Part: Create a Post Model and a Posts Controller
  3. Create a Blog Application with Ruby on Rails 7. Third Part: Implement User Authentication
  4. Create a Blog Application with Ruby on Rails 7. Fourth Part: Performing Operations on the Post Resource
  5. Create a Blog Application with Ruby on Rails 7. Fifth Part: Implement Authorization

Create a new application

Let's get started by creating a new application. In the terminal, we type:

rails new my_blog

Then we change directories into this new project folder:

cd my_blog

In this project, we will use the has_secure_password method, which adds methods to encrypt a password using the bcrypt algorithm. For the has_secure_password to work, the User model must have a password_digest attribute. The encrypted form of the password is saved in this field. The password that the user sets is never saved to the database.

The bcrypyt gem is not installed by default, so, first, let us uncomment this line in the Gemfile:

gem "bcrypt", "~> 3.1.7"

Then we run:

bin/bundle install

in the terminal.

Create the user model

Now we generate the User model in the terminal:

bin/rails g model User name:string email:string password_digest:string

After that, we run the migration:

bin/rails db:migrate

Now we can add the has_secure_password to our model. ln the app/models/user.rb file, put this inside the User class:

has_secure_password

Let's also add validations, because we don't want users without names and emails:

validates_presence_of :name, :email
validates :email, uniqueness: true

GENERATE A USERS CONTROLLER

Next, we will generate a Users controller:

bin/rails g controller Users

Let's select the strong parameters in the private user_params method inside our Users controller:

private
def user_params
  params.require(:user).permit(:name, :email, :password, :password_confirmation)
end

add controller actions and views

Let's add a show method to the Users controller:

def show
  @user = User.find(params[:id])
end

Next, we'll create its corresponding view, show.html.erb, inside the app/views/users/ folder.  Let's add information about the user in this view:

<p style="color: green"><%= notice %></p>
<h1>My account</h1>
<h2><%= @user.name %></h2>
<p><%= @user.email %></p>

Of course, we have to implement the ability for users to sign up. So, let's add a new method in our controller:

def new
  @user = User.new
end

And then, we'll create its corresponding view (new.html.erb) in the app/views/users/ folder. In this view, we'll have our sign up form:

<h1>Sign Up</h1>
<%= form_with(model: @user) do |form| %>
  <% if @user.errors.any? %>
     <div style="color: red">
        <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
        <ul>
          <% @user.errors.each do |error| %>
            <li><%= error.full_message %></li>
          <% end %>
        </ul>
      </div>
  <% end %>
  <div>
    <p>
      <%= form.label :name %>
      <%= form.text_field :name %>
    </p>
    <p>
      <%= form.label :email %>
      <%= form.email_field :email %>
    </p>
    <p>
      <%= form.label :password %>
      <%= form.password_field :password %>
    </p>
    <p>
      <%= form.label :password_confirmation %>
      <%= form.password_field :password_confirmation %>
    </p>
    <p>
      <%= form.submit 'Sign Up' %>
    </p>
  </div>
<% end %>

Next, we will define our create method, which will allow us to save the user to the database:

def create
  @user = User.new(user_params)
    respond_to do |format|
      if @user.save
        format.html {redirect_to user_path(@user), notice: 'User was successfully created.'}
      else
        format.html { render :new, status: :unprocessable_entity }
      end
    end
end

SET THE ROUTES

Last thing we'll do will be to set routes to this actions in the config/routes.rb file. We'll use the resource routing for the show and create actions:

resources :users, only: [:show, :create]

And for the new action, we'll create a custom route:

get 'sign-up', to: 'users#new'

Now, we can start the server:

bin/rails s

And we can visit localhost:3000/sign-up and add users.

Post last updated on Dec 17, 2023