A Beginner"s Guide to Creating a Blog in Ruby

106 24
Starting the Blog

The classic first project for a Rails programmer to tackle is a blog. Since the first demonstration screencasts, the "15 Minute Blog" has become infamous. A blog is a fitting first project: it's a simple database-driven site that's quite easy to build incrementally.

This tutorial series is written for Ruby on Rails version 2.1. Though everything in these tutorials may work on later or earlier versions, it's recommended that you install version 2.1 of the Ruby on Rails gem.

To install the specific version of the Ruby on Rails gem (and all its dependencies), run the following command.
$ gem install rails --version "= 2.1.0"

If newer versions have been released since this article was written, this will downgrade Rails to the earlier version. If you have an older version installed, this will upgrade Ruby on Rails to version 2.1.

Building Incrementally

Rails allows you to build web applications incrementally. This allows you to have a somewhat vague plan in the beginning and refine it as you go. This doesn't mean you shouldn't have a plan, since having a plan of which features should be built in each increment is essential. Each article in this series will cover one increment.

The goal for this increment is to get a working interface to create and view blog posts.

To do this, you first have to define what a blog post is. At its most simple level, a blog post has a title and a body. Because we're starting as simply as possible and working up, that's all that will be created.

Generating a Rails Project

To generate a new Rails project, simply run the rails command from the command-line with the name of your project as an argument. Ruby on Rails will then create a basic application and supporting scripts in that directory. The name of this project will be blog.
$ rails blog
      create app/controllers
      create app/helpers
      create app/models
      create app/views/layouts
      create config/environments
      create config/initializers
      create db
      create doc
      create lib
      create lib/tasks
      create log
      create public/images
... snip ...

The rails command will tell you which files it's creating. When it's finished, you'll be back at the command prompt. Change directory to your new blog directory.
$ cd blog

As of Rails 2.0, the default database driver is sqlite3. If you have sqlite3 installed and configured, no database configuration is required. For a development system, sqlite3 is recommended as it's the most convenient. For production systems, a faster SQL server such as MySQL is recommended, but you don't have to worry about that just yet, since sqlite3 will be used.


Rails provides an interface to get web applications up and running quickly. The scaffolding generator will create a model, controller and migration that can be edited to your needs. Pass as arguments to the scaffold generator the name of the model you'd like to create, as well as the names and types of fields the model should have.
blog$ script/generate scaffold Post title:string body:text
      exists app/models/
      exists app/controllers/
      exists app/helpers/
      create app/views/posts
      exists app/views/layouts/
      exists test/functional/
      exists test/unit/
      exists public/stylesheets/
      create app/views/posts/index.html.erb
      create app/views/posts/show.html.erb
      create app/views/posts/new.html.erb
      create app/views/posts/edit.html.erb
      create app/views/layouts/posts.html.erb
... snip ...

The Post model has now been created. In the migration, you can see that the posts SQL table will be created with two columns--a title as a string and a body as text. The difference between string and text is the length allowed. String should be short, no more than 200 characters or so. That's fine for the title, but the body of the blog posts need more space. The text column type has no limits on length, and thus are much more appropriate for blog post bodies.

What the Scaffold Generator Created

The scaffold generator did quote a lot for you.
  • Generated a Post migration - The migration is what creates database tables and columns. Look for the new posts migration in app/db/migrations. If you wish to add any more database columns related to a post, add them to this migration.
  • Generated a Post model - The Post model is where you should add any validations to data. You can find the model generated by the scaffold generator at app/models/post.rb.

  • Generated a Post controller - The Post controller contains all actions to view, edit and create new blog posts. You can find it at app/controllers/posts_controller.rb.
  • Generated views for the Post controller - These can be found in app/views/posts.

Run the Migration

The scaffold generator created a migration which will create the posts table when run. Before we can start up the server and see what the posts controller looks like, you have to run the migration. To run the migration, run the db:migraterake task from the blog directory.
blog$ rake db:migrate
== 20080702134850 CreatePosts: migrating ==========
-- create_table(:posts)
   -> 0.0697s
== 20080702134850 CreatePosts: migrated (0.0700s) =

Start the Webrick server by running the script/server command from your blog directory. Once the server is started, you can start running actions and creating blog posts right away.
blog$ script/server
=> Booting WEBrick...
=> Rails 2.1.0 application started on
=> Ctrl-C to shutdown server; call with --help for options
[2008-07-02 09:58:55] INFO WEBrick 1.3.1
[2008-07-02 09:58:55] INFO ruby 1.8.6 (2007-09-24) [i486-linux]
[2008-07-02 09:58:55] INFO WEBrick::HTTPServer#start: pid=8231 port=3000

Open your web browser and go to http://localhost:3000/posts. You should see a rather blank page with the text Listing Posts, an empty table of the existing posts and a link named New Post. Click on the New Post link, fill out the form and hit the Create button. A new post has just been created and added to the database. All the work of creating forms and actions to create new posts has already been done for you by the scaffold generator.

This iteration didn't make anything close to what you would call a blog. You can create posts, view posts, etc, but it still doesn't look anything like a blog. The next iteration will work on the views to make the application look a bit more like a blog. The basic blog layout with a center column will be used with formatted blog posts displayed.


Leave A Reply

Your email address will not be published.