Hi, I'm Nicholas Johnson!

software engineer / trainer / AI enthusiast

Blog integration exercise

Create a new Rails App using:


We’re going to put everything together and make a simple blog.

Create the model

First create a BlogPost model and give it some attributes. You can use the following generator as a jumping off point. You will need to add a content:text and probably also a date:datetime too.

  rails g model blog_post title:string

Create the controller

Next we’ll need a BlogPost controller. Use a generator like this:

  rails g controller blog_post

Create your routes

Use resources to create the standard CRUD routes. Like this:

resources: blog_post;

Create the CRUD methods

Review your scaffold code and create the standard Rails CRUD actions in your controller (index, show, new, create, edit, update, destroy). Create views to go along with them.


Use validates_presence_of :title to validate that the blog_post has a title. It is now not possible to save a blog_post without a title. Add validation for the content.


Set blog_post#index as the homepage

Friendly URLs

Add a slug attribute to the blog_post. Do a find_by_slug instead of a regular find in your show method.

You can use a migration to add the field, something like:

rails g migration add_slug_to_blog_post

Within the migration you will want to do:

add_column :blog_posts, :slug, :string, index: true


We’re going to create a Comment model. Comments belong to BlogPosts and blog_posts have many comments. You’ll need something like the following:

class BlogPost
  has_many :comments

class Comment
  belongs_to :blog_post

Comment will need a blog_post_id:integer attribute.

Use the generator to create the post.

Routes. We’ll use a nested route to generate the resources. Modify your routes.rb file like so:

resources :blog_posts do
resources :comments

rake routes to check the routes you have made.

Comment Controller

Make a comment controller with a new and create method. It should receive a comment and create it. Make a matching view and form.


Integrate the comment form into the BlogPost#show view. Integrate the list of comments into this view.

Optional finishing up