How to Create a Blog from Scratch Using Ruby on Rails

This tutorial shows you how to create a basic blog using Ruby on Rails. By covering the design aspects of a Rails web application, it makes it easier to understand the concepts behind Rails and how they fit together.

Blogging Application

The first part of this article Getting Started with Ruby on Rails: Installation, shows you how to install Ruby on Rails.

Brief Overview of Ruby on Rails

Ruby on Rails is a framework for the Ruby language, which is used for rapid development of web applications. It does this by allowing you to concentrate on solving your clients’ problems and not getting caught up in building an infrastructure to support your clients problem.

Let’s face it, you don’t want to build database access layer every time you start a new project. Neither will you want to implement a fully functioning MVC pattern on a regular basis. This is the whole point of frameworks; they give you a starting point from which you can build upon. This allows you to concentrate on the clients needs, not the so-called yak shaving tasks, which can hold you back.

A Few Words About MVC

It’s worth mentioning MVC (Model-View-Controller) because it can be one of the factors that discourages people from trying frameworks like Rails. MVC is a relatively advanced programming topic, however, I’m not going to suggest you learn how to implement the MVC pattern, just learn how to make use of it. With this in mind let look what each of the letters in the acronym M.V.C. means and how they fit into web development.


The model layer is where you define classes for the data your application will use/store. For instance, if you want to store posts for a blog, you will have a “Post” model. The model has the capability to interact with the database, to retrieve and store data. This functionality is gained by inheriting it from the ActiveRecord super class. Any methods, which act upon this data, should also be placed in the model.


The view layer has one main purpose – to return the relevant HTML to be rendered on the users browser. In Rail a view is held in an erb (Embedded Ruby) file, which contains both HTML and embedded Ruby statements.


Without the controller, nothing would happen. The controller interacts with the model to retrieve and store data. It will then pass any data, acquired from the model, to the view. The view returns the resulting HTML to the controller and the controller sends this back to the users browser.

MVC Summary

This is quite a concise overview of MVC. I highly recommend that you read up on this topic, as it is the key to cracking Rails. This article from is a great resource for learning MVC in Rails. However, the best way to learn is, install the framework and start playing with it.

Creating a new Rails Project for your Blog

Before you can do anything, you need to create a new project. In Rails, a project is a folder structure that is used to store all the files, which are relevant to you web application. To create a project, key the following command at the command prompt:

  > rails myblog -d mysql

Note in this case we are using MySQL as the DBMS. This command will create a folder called myblog that holds all the relevant project folders.

Important Files and Folders

File/Folder Purpose
config\database.yml This file is a YAML (stands for – YAML Ain’t a Markup Language) file. It contains details about your DBMS provider and the authentication details for your DBMS.
config\routes.rb This is a ruby file. It allows you to configure how HTTP requests will be routed.
app\controllers\ Contains controller files. Controllers are ruby files.
app\helpers\ Contains helper files. Helper files are ruby files which are used to define logic which doesn’t belong in the model but is too long-winded to be embeded in the view.
app\models\ Contains model files. Model files are ruby files. They inherit most of there functionality from ActiveRecord but you can also build upon this functionality by writing your own methods.
app\views\ Contains a sub folder for each controller file that exists. For instance, if there is a controller called “posts_contoller.rb” there will be a view sub-folder called “posts”.
public\images\ Contains any image files that belong to your website.
public\javascripts\ Contains any javascript files that are used by your website.
public\stylesheets\ Contains any CSS files that belong to your website.
db\migrate Contains “migration” files, used to migrate a database from one state to another (more on this later)

Application Design

App design.

In order to create a Rails application you have to think about how the constituent data structures will relate to each other. There are different levels of design that you can carry out, however, for the sake of keeping things simple, we shall… keep things simple.

So you’re designing a blog application; where should you start? First, grab a pen and some paper and write a small paragraph about what your applcation is and what it should do. After you’re finished, highlight/underline the nouns that are required to store information to the database. For our simple blog application, a sentence will be suffice:

“My application is a web log (blog). It will allow me to write posts and will allow people to comment on them.”

So we need models for posts, people (or users) and comments. Again, in an effort to keep things simple, we can forget about people/users. So we need to have a model for both, posts and comments.

Next, you will need to think about how the models should relate to each other. The most common relationships in Rails are defined as follows:

You have to ask yourself what the relationship is between the given models. With posts and comments – a given post has many comments and a comment belongs to one post. This logic suggests a One-to-Many relationship. We will visit relationships later.



Scaffolding can be used for building rapid prototypes/mock-ups for your client. They can also be very useful for learning how to use Rails. In one simple command you can create a model, controller and several views, effectively building a large chunk of functionality in one fell swoop.

Creating Posts using Scaffolding

To create the relevant scaffold files for posts, you can generate them with a single command:

  > ruby script/generate scaffold post title:string body:text

This command creates lots of files. It creates a controller for posts called posts_controller.rb and a model called post.rb (note, not plural). It also creates a sub-folder in the views folder called posts. This contains four files, index.html.erb, edit.html.erb, show.html.erb, new.html.erb. You also may have noticed that we have specified the fields that are required i.e. title and body in addition to their their types “string” and “text”, respectively.

Creating Comments using Scaffolding

Similarly, to create the relevant scaffold for comments, run this command:

  > ruby script/generate scaffold comment name:string body:text post:references

At the end of this command, you might have noticed the text “post:references”. This will be used to create the foreign key field for linking to the primary key of the posts table.

Relating the Post and Comment Models

Now that you have two models you need to create a relational link between them. To do this, you need to add a line of code to both the post and comment models, as follows:


class Post < ActiveRecord::Base
  has_many :comments


class Comment < ActiveRecord::Base
  belongs_to :post


This makes it really easy to find associated records. You will see how easy later when we need to display comments, which are related to a given post.

How do I create the Database and Tables?

Now that you have created the models, you need to create the database then create tables to hold information about posts and comments.

To create the database you need to add the username and password into database.yml file, similar to below.

  adapter: mysql
  encoding: utf8
  reconnect: false
  database: myblog_development
  pool: 5
  username: root

  password: yourrootpassword
  host: localhost

After you have modified the database.yml file with your database logon details, you need to run the following rake commend from a command prompt. Also, be sure to set your current directory to the project folder first:

  > cd myblog
  > rake db:create

The database should now be created.

Database Migration

In Rails, a Migration is the name given to the process of moving your database from one state to another. Some examples of migrating your database from one state to another are:

When you created the two models for posts and comments using scaffold, several files were created automatically. In particular, migration files were created in the db\migrate folder. These files are used to create the tables for the post and comment models. To build the tables using the migration files you should run the following command:

  > rake db:migrate

The “posts” and “comments” tables should now be created.

Let’s See How Things Look!

Ok, lets fire up Webrick, Rails built-in web server, and see how things look.

  > ruby script/server

Now browse to http://localhost:3000/posts on your favourite browser and you should see a screen that resembles the below screenshot. By default this URL request forwards control to the “index” method in the posts controller, then the “index.html.erb” view is rendered.

Let's See How Things Look!

If you click the “New Post” link, highlighted, you will be directed to the URL, http://localhost:3000/posts/new, where you will be presented with a form for creating new posts. This URL request calls the “new” method in the posts controller, then the “new.html.erb” view is rendered.

Let's See How Things Look!

Click on the “Create” button to save your post, this will direct you to the URL, http://localhost:3000/posts/show/1, displaying the post details. This URL request calls the “show” method in the posts controller. It also passes one parameter, “1”, to the show method. Finally, it renders the “show.html.erb” view.

Let's See How Things Look!

Setting up a Home Page

Home page.

You’ll likely want to have the root URL (http://localhost:3000) direct the user towards your posts, effectively making it your home page. To do this you will first need to delete the public/index.html file.

The second thing you need to do is set up a route in your config\routes.rb file. Open this file in notepad (or your favourite editor) and add a new line to the end using map.root, as shown below.

ActionController::Routing::Routes.draw do |map|
  map.resources :comments
  map.resources :posts
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
  map.root :controller => "post"


Note: This route file has all the commented lines removed for the sake of clarity and conciseness. For more on routes, try the Rails API Documentation

Allow the User to Comment!

Allow the User to Comment!

Modify the Route

Before making any changes to the view, you need to scope the “comments” route within the “posts”. Modify the route.rb file to look like the code below.

ActionController::Routing::Routes.draw do |map|
  map.resources :posts, :has_many => :comments

  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'
  map.root :controller => "post"

Modify the View

Now modify the views/posts/show.html.erb file to be the same as the code below. Here, we are rendering the post, showing any related comments and then displaying a form to add new comments.

  <%=h @post.title %>


  <%=h @post.body %>


<% @post.comments.each do |c| %>
    <b><%=h %> said:</b><br />
    <%= time_ago_in_words(c.created_at) %> ago

    <%=h c.body %>
<% end %>

<% form_for [@post,] do |f| %>

    <%= f.label :name, "Author" %><br />
    <%= f.text_field :name %><br />
    <%= f.label :body, "Comment Description" %><br />
    <%= f.text_area :body %>

    <%= f.submit "Add Comment" %>
<% end %>

Modify the Comments Controller

Remove all the methods that were automatically created in the comments_controller.rb file, as they are not required for our web app. Add one new method called “create” as shown in the code below. This simply creates a comment record in the “comments” table, which is related to a given post.

class CommentsController < ApplicationController
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.create!(params[:comment])
    redirect_to @post

Refresh the browser (make sure your server is still running at the command prompt). You should get something similar to the below screenshot.

Allow the User to Comment!

Now you can try to add a comment. Enter a comment and click "Add Comment". The new comment should appear under the post as shown below.

Allow the User to Comment!


What have you Learnt?

You have learnt the basic concepts behind application design and translating the design into a working Rails web application. Having, at least, a basic understanding of application design is important when learning Rails and is the key to succeeding.

How can you improve the Blog?

What we covered in this tutorial was just enough to get the message across. There are plenty of things you could do to improve things. Adding some CSS or using some AJAX to make things run a bit smoother are just a few suggestions.

What Next?

Try learning MVC, so you really understand the concepts behind it. Try to understand the advantages of Rails and MVC; this will motivate you to learn the framework. However, while reading up on the theory is good, you really need to just get stuck in and play with the framework. Download and Install Rails at take your first steps towards more productive and enjoyable web development.

Related content

About the Author

Phil McClure is a Software Developer from Belfast, Northern Ireland. His main interests are software architecture, design patterns and how these can be applied to web development. Phil blogs at Follow him on Twitter.

This was published on Jun 5, 2009


Techie Jun 05 2009

Nice article for start up on ruby..

Rene Zammit Jun 05 2009

Great post!! My blog on my site is made by ASP, but it needs some improvement.

Mayank Ralhan Jun 05 2009

Good Post!!

Plz also Give Tips on Visual Studio ASP

Tommy Jun 05 2009

The code for the routes file you gave is wrong (or it was for me at least), it should be:

map.root :controller => “posts”

KevinBrown Jun 05 2009

Superb! Thanks a lot!

Scott O'Hara Jun 05 2009

Very cool. Going to look at this a bit more in depth later. I’ve been wanting to learn more about Ruby on Rails. Thanks!

Kamic Jun 05 2009

helped thanks! :)

_lowell Jun 05 2009

hey cool! it’s just like DHH’s 15 minute rails blog tutorial, rbates’ 15 minute rails blog tutorial, fabio akita’s 15 minute rails blog tutorial and every other 15 minute rails blog tutorial on the web. what would we do without all this cool original content?

Crackin Jun 06 2009

Lowell here’s an awesome idea — go fuck yourself! What have you contributed you stupid looking fool.

Thanks for the comments everyone!

@ _lowell Yes, this is another “blog” tutorial… and I must admit, I deliberated whether it would be of any value since there are several blog tutorials out there. However, I came to the conclusion that it would be, and this is the reason why…

When I was learning Rails I used some of the mentioned “blog” tutorials, and some were better than others when it came to expaining certain aspects of the framework. So, in the end, I was glad that there were many tutorials, of the same type, to pick from because I could cross reference them against each other when I was having trouble with learning something. This proved invaluable to me when starting out. So, I hope that my explanations will help people in the same way.

I will be writting a three part tutorial for on how to create a basic twitter web app, this will cover some other aspects of Rails, which are not covered in the “usual” blog tutorials – so maybe this will float your boat more?

That said, if you’re not happy with the standard of Rails tutorials out there, why not write one on your blog or contact Jacob to write one for Six Revisions. This will benefit us all.

@Tommy – It works for me… When using “map.resources”, several “named routes” are created – named routes can be used in the “root” call. So, you should be able to code as follows:

map.resources :posts, :has_many => :comments
map.root :post

Note, the “root” call has to be after the “resources” call.

If it still doesn’t work, what version of Rails are you running?

Pjero Jun 06 2009

It doesn’t make it less true. Next time write something original, rails tutorials are always welcome.

Style)r Jun 06 2009

really nice guide for start up on rails, thanks!

Mikez Jun 06 2009

Thanks for the article! As a ROR newbie its a big help!

PS .. _lowell sucks!

Nice write up but… when I run the command:
“ruby script/generate scaffold post title:string body:text”

i get an error back saying
“wrong constant name Title:stringController”

any thoughts???

@ Tommy – Sorry made a typo in my last comment, should be:

map.root :posts

Let me know if it works.

@ Greg – What version of Rails are you running?

Chang Huang Jun 07 2009

map.root :post didn’t work for me neither, map.root :controller => ‘post’ did the trick. Running Rails 2.3.2.

Thanks for the post.

@Chang Huang & @Tommy – Ok, I’ll see if Jacob can change this for me… Thanks for letting me know guys.

Mark Fulton Jun 07 2009

Thanks Phil, I would LOVE to see a simple Twitter app tutorial. Can’t wait. I have shared this one with my followers.

I still find it too cumbersume. they need to remove all the extra hardcoded stuff.

Jordan Jun 08 2009

Hey Phil,
I started going through this tutorial on my mac and came across an error when I tried to run this:

ruby script/generate scaffold post title:string body:text

The error is:

C:\Users\Jordan>ruby script/generate scaffold post title:string body:text
ruby: No such file or directory — script/generate (LoadError)

I thought it must be because I was doing it in OSX but when I tried on my pc the same thing happened. I am running rails 2.3.2 on both machines. I’ve tried a few things that I have found from similar problems but none of them have worked…

@Mark Fulton – Thanks, the first part will be up soon!

@owen – What hardcoded stuff?

@Jordon – There are two problems here. First you need to change you current driectory to the project directory i.e.


Second, for unix based systems like OS X and linux you need to omit the “ruby” command, so it would be:-

script/generate scaffold post title:string body:text

Let me know how it goes.

Daniel Niklaus Jun 09 2009

May I introduce you a alternative? Still Beta but workes without coding and comments are welcome. (It’s in German, hope seeing works)

Adrian Jun 14 2009

I can’t migrate the comments table, but posts worked the first time. (i took a screenshot):

I have tried a lot of things. Can anyone fix this?

Adrian again Jun 19 2009


Scott Raine Jun 23 2009

@Adrian, are you sure that their isnt a typo as the output says refrences which should be references.

Midnite Jun 23 2009

Adrian, I guess the problem is that you misspelled “references” when creating the scaffold for comments.

Adrian Jun 23 2009

I checked for typos. there were none, except something i noticed was that in the “Relating the Post and Comment Models” section, ‘:post’ is not plural but ‘:comments’ is. If it is supposed to be that way, there are no typos. Another thing i noticed was that when i tried the first time, it made an app/helpers folder. Could that be affecting it?

Adrian Jun 23 2009

@Midnite i’ll try again now…

Adrian Jun 23 2009

I deleted the ‘myblog’ folder and started again. It gave me the same error when i got to that step, and i was very cautious for typos.

@Adrain – Contact me through the contact form on my site ( and I’ll give you my email address so you can send your code to me.


Thanks for the guide. I’m trying to change it for my own means. Is there a way to redo a scaffold or add to a scaffold once a new mode has been made? For instance I want to create a punter model that references a joint model and the joint needs to reference a punter, so when I start with

ruby script/generate scaffold punter username:string joint:references

I get an error because joint isn’t there yet. But if I do joint first then punter isn’t there yet.

Thanks for your help.

Sorry you can delete this comment and the last one. I forgot to type in the scaffold bit which was fucking everything up. Thanks for the guide!

Larry Jul 17 2009

I’m on a Mac with OS 10.5.7 using the version of Rails that came installed.

I had the same error as Greg:
wrong constant name Title:stringController

As per Phil’s suggestion, I tried omitting the ruby and just typed
script/generate scaffold post title:string body:text

and got the same error.

Anybody know what to do?

Larry Jul 17 2009

About the error: wrong constant name Title:stringController

It must have been the version of Rails. I got it to work by following the Rails install instructions at . I also had to do the corresponding MySQL installation,

Finally got my first Rails program up and running! Thanks Phil.

Larry Jul 17 2009


Like Tommy, I too noticed that Under Setting Up a Homepage, the line in routes.rb had to use the plural “posts” instead of “post” as in:

map.root :controller => “posts”

If I use “post”, I get an error page that says NameError in PostController#index, etc. etc.

Is this normal/correct, or did I do something wrong? I’m running Rails 2.3.2.

Theokie Aug 01 2009

Thanks for the tutorial! Really helped. One thing though, could you explain this part better map.resources :posts, :has_many => :comments

what exactly does that do as I have a hard time understanding routings in Ruby despite looking at a few tutorials.



Theokie Aug 01 2009

I meant Rails not Ruby ;)

Sheriff Md Aug 07 2009

Good tutorial, Phil. Like, Tommy, Larry and Theokie, I ran into the same issue, because, I wrote “post” (not the missing ‘s’) like in your post. If you can correct the typo in your tutorial too (We can see you made the same type in your own comment too :). Other than that, good one. Really helped me. Keep doing more simpler tutorials like this.

Class tutorial. Very well written


Kevin Aug 13 2009

Nice tutorial. I’m still new to RoR. Other tutorials I’ve seen use sqlite 3 as their DBMS. In this tutorial, would sqlite 3 affect any of the settings, in database.yml for instance?


Aziz Light Sep 07 2009

Thanks a lot for this tutorial! It would be really nice if you could create some “more in depth” ones too :)

I’ve just gone through this tutorial (very helpful thank you) corrected the post/posts typo as described in the comments.
When I refresh it displays the homepage as views/posts/index, but you said to make the changes to views/posts/show. Is this a mistake in your guide (i.e should I have made those changes to views/posts/index) or have I made a mistake somewhere else which is preventing things from working properly?

Hire developer Sep 21 2009

Indeed This article provides all in and outs for creating blogs in ruby on rails thanks for information.

Great tutorial! It has been a huge help for me. :)

Question: In the website I’m working on, I want to do the equivalent of adding a “Show” link next to each comment. When you click on “Show”, it will take you to a new page with the following url:

(where the “1” is the post_id, and the “3” is the comment_id for the 3rd comment)

And looks something like this:
Post 1 > Comment 3

Phil said:
12 minutes ago

My third comment!

It may seem kind of pointless in this example, but I plan on adding more fields to the “Comment” model and have them display on this Show page. I’m assuming I need to have a “Show” method in the Comment controller and a Comment view for “Show” as well. Any help is appreciated!! Cheers.

gustavo Oct 14 2009

Excelent Job. I like the way that you write this post. Step by steap plus images. Thank you.

I changed the line
map.root :controller => “post” to
map.root :controller => “posts”
in routes.rb

Guillermo Oct 24 2009

Very nice article!!!
Thank you very much!!

Rohit Nov 23 2009

Thank you for the awesome tutorial. I do have a question though. How do I write code to delete a comment for a given post.
I could not create a delete link on any page that will redirect to the posts list page.

Great! thanks. It’s easy to follow for me as a newbie.

Don S. Jan 07 2010

I like the way you went step by step with images. Great job. Is there more help I need how do you delete a single comment from a post and how do you delete a post with all of it comments, and a way to search for a post or a comment.

sara M Jan 17 2010

Thanks a lot. can you make a django blog tutorial???

manadhii Feb 11 2010

Indeed This article provides all in and outs for creating blogs in ruby on rails thanks for information

mooman Feb 28 2010

you look like mobey

James Mar 04 2010

hey thanks. This is the best introduction to ruby on rails I have found. Thanks for assuming your audience is educated, and providing information that is actually useful! Hopefully within the next couple weeks my website will be running on rails!

Thanks for this article! It really, really helped me :) I learn more through trial and error.

Kinoman Apr 20 2010

Really this article provides all in and outs for creating blogs in ruby. Thanks a lot for this tutorial!

Good and Helpful Tutorial!
Only issue: After modifying my show.html.erb file to duplicate the one in the example, then running the browser, I get an Action Controller: Exception.
“undefined method `name’ for #”
Extracted source (around line #29):

Any ideas?

i have the same problem as brad. where do i define the method?

Great tutorial, only one problem

When I get around to creating the comments section I get this error when trying to post a comment

No action responded to 3. Actions: create, destroy, edit, index, new, show, and update

Other than that, great!

syafrin Jan 20 2011

a complete tutorial and a very nice pack. This is very useful for me. thank you, you are a person who enjoys sharing knowledge with the good. I like it

Kypros Basileiou Jan 28 2011

Excellent tutorial. But in order to do this one needs to work.. So Why not use a readymade blog like WordPress..?!?

ZachRe Feb 04 2011

Dude, incredible tutorial. I very much do not want to use Bloggr or WordPress or some other such thing for my blog, so this is very cool indeed.

One problem I’m encountering, however, is with the comments. When I hit ‘submit’ for a comment I’ve just written (when i’m on /post/2 for example)
I get the following error:

ActiveRecord::RecordNotFound in CommentsController#create

Couldn’t find Post without an ID

The create method in my CommentsController is exactly what you have. Is it possible that for some reason there isn’t a post_id in my params hash?
Many thanks again.

Chromatix Feb 15 2011

Awesome tutorial. We prefer WordPress for blogs but because Ruby on Rails generally lacks resources to it which is why your post is GREAT for resource and a definite keeper.

I tried this tutorial, I’m new to rails so I know this may be a real stupid muck up of my own but, at the 1st screen shot, when you first say to go to http://localhost:3000/posts my window just displays a message saying “we’re sorry but something went wrong, We’ve been notified about this issue and we’ll take a look at it shortly…..any ideas why this has happened???????

Ismail N May 08 2011

sounds great. thank you for sharing. cheers.

keith May 30 2011

I can’t get passed

“ruby script/generate scaffold post title:string body:text”


“ruby: no such file or directory — script/generate ”

I’m running on

ruby 1.9.2p180
gem 1.5.2
rails 3.0.7

Aroary Jun 27 2011

Good tutorial to create simple blog using ruby on rails. It helped me to create a blogs.

Thanks you.

fgroups8 Jul 03 2011

Couldn’t find Post without an ID

jordi Jul 18 2011

Thank you for such a good tutorial!!
is reallly confuse to start developing,then I appreciate very much this things… any ideas for more useful practices? something that I can try with rails?? thanks a lot!!

shyam Aug 04 2011

very helpfull…

Janvi Oct 18 2011

Thank you…It worked…:)

Razvypp Oct 27 2011

tnx very much.

birendranath Oct 29 2011

This is quite a good idea. You don’t need an art degree to do art- if you have talent and if you work on it. You can teach yourself.

This comment section is closed. Please contact us if you have important new information about this post.