Chapter 8: Example Project – Build a Blog App with CRUD

Chapter 8: Example Project – Build a Blog App with CRUD

AI Reading

Quick summary of this article

This chapter walks through building a complete blog app with create, read, update, and delete (CRUD) features using Rails. The main tool is the scaffold command, which automatically generates all the necessary code for a resource, including routes, a controller, and a model. By following the steps, you get a fully working app where you can add, view, edit, and remove posts.

  • Use the command rails generate scaffold Post title:string body:text followed by rails db:migrate to create the entire data structure and interface for posts.
  • Start the app with rails server and visit http://localhost:3000/posts to see the empty list and use the "New post" link to create entries.
  • A single line in the routes file, resources :posts, automatically creates all seven standard RESTful URLs for the blog, such as GET /posts and DELETE /posts/:id.
  • The controller's create action builds a new post from form data, saves it to the database, and either redirects to the new post or re-renders the form if saving fails.
  • The model file is very small because Active Record handles the database interactions, and you can add validations like validates :title, presence: true to ensure every post has a title.

Chapter 8: Example Project – Build a Blog App with CRUD

For the second example, we will build a simple blog where visitors can create, view, edit, and delete posts. Rails has a command called scaffold that generates all the code for a resource, which is perfect for learning how CRUD applications are structured.

✅ Step 1: Generate the Scaffold

Generate a scaffold for a Post model with a title and body, then migrate the database:

rails generate scaffold Post title:string body:text
rails db:migrate

✅ Step 2: Start the Server

Start the server and open the posts index page:

rails server

Visit http://localhost:3000/posts. You will see an empty list with a “New post” link. Click it, create a post with a title and body, and save it. The post appears in the list immediately. You can also open, edit, and delete posts.

🔗 How the Routes Work

Open config/routes.rb and you will see a single line that created all the URLs:

# config/routes.rb
resources :posts

This one line gives you the standard RESTful routes: GET /posts, GET /posts/new, POST /posts, GET /posts/:id, GET /posts/:id/edit, PATCH/PUT /posts/:id, and DELETE /posts/:id.

🔧 Understanding the Controller

Look at the create action in app/controllers/posts_controller.rb to see how a new record is saved:

# app/controllers/posts_controller.rb
def create
  @post = Post.new(post_params)

  if @post.save
    redirect_to @post, notice: "Post was successfully created."
  else
    render :new, status: :unprocessable_entity
  end
end

This method builds a new Post from the form data, tries to save it to the database, and then either redirects to the new post or re-renders the form if validation failed.

💾 The Model

The model file app/models/post.rb is surprisingly small because Active Record handles the database:

# app/models/post.rb
class Post < ApplicationRecord
end

🏠 Practice Exercises

# 1. Add a validation so every post needs a title
class Post < ApplicationRecord
  validates :title, presence: true
end
# 2. In the console, test the validation
post = Post.new(body: "No title here")
post.valid?  # => false
post.save    # => false

Now you have a complete CRUD application! In the next chapter you will learn about testing and what to study next.

Leave a Reply

Your email address will not be published. Required fields are marked *