Preparation

Prerequisites

A working version of Rails 3.2 which you can install using Rails Installer for Windows or Mac.

Sublime Text 2. If you prefer another text editor like vim, emacs or TextMate that's fine too but these instructions will specifically mention Sublime.

Next steps

Open two command prompts. One of these you'll use to run your local Rails server and the other you'll use for entering all other commands.
Whenever you need to start or restart the rails server use one of these prompts, for all other command line work you can use the other.

Setting up our Rails app

rails new quick_blog -T

Entering this command into your command prompt will cause Rails to generate a new application and begin to install depedencies for your application. This process may take a few minutes, so you should let it continue. Once it has finished type:

cd quick_blog

To change into the folder where your application is stored. If you look at the contents of this folder you'll see:

This is the standard structure of a new Rails application. Once you learn this structure it makes working with Rails easier since everything is in a standard place. Next we'll run this fresh application to check that our Rails install is working properly. Type:

Now that you've created the Rails application you should open this folder using Sublime. Open up Sublime, then open the quick_blog folder that was just generated.

Creating basic functionality

Now we're ready to get started building an actual blog. In your command prompt press Ctrl-c to stop the Rails server, or open a new command prompt and navigate to your Rails application folder. Then you can use a Rails generator to build some code for you:

rails g scaffold Post title body:text

You'll be presented with something that looks like:

An important file that was generated was the migration file: db/migrate/20130422001725_create_posts.rb Note that you will have a different set of numbers for in yours.

This file is some Ruby code that is a database agnostic way to manage your schema. You can see that this code is to create a table called Posts and to create two columns in this table, a title column and a body column. Finally we need to instruct Rails to apply this to our database. Type:

rake db:migrate

Once this command has run you can start up your Rails server again with rails server and then navigate to http://localhost:3000/posts to see the changes you've made to your application.

From here you can play around with your application. Go ahead and create a new blog post.

You'll notice you can create new posts, edit or delete them. We're going to add in some functionality to our new Rails app which enforces a rule that every post must have a title. Open app/models/post.rb and add the line:

At this point you can refresh the show post page in your browser to see the changes you've made.

We'll also want to make our blog listing prettier too, we'll use a Rails partial (a partial is simply a reusuable block of HTML code. It's part of a web page) to achieve this. We want our listing and the individual blog pages to look the same so first we'll create a file: app/views/posts/_post.html.erb The underscore in front of the filename here tells Rails that this is a partial. We'll take

Access control

One huge problem with our blog is that anyone can create, edit and delete blog posts. Lets fix that. We'll use HTTP Basic authenticate to put a password on actions we don't want everyone accessing. Open app/controllers/posts_controller.rb and add before_filter :authenticate, :except => [ :index, :show ] on line 2 just below the class declaration. At the bottom of your file put the following code:

With that code in place you can try to add a new post and you'll be prompted to enter a username and password.

Adding comments

Creating a database model and routing

No blog is complete without comments. Lets add them in. On our command prompt shut down your rails server by hitting Ctrl-C in your command prompt and then type in:

rails generate resource Comment post:references body:text

Then you'll want to update your database here to reflect the schema change you've just made:

rake db:migrate

After this you'll need to inform Rails that your Posts will potentially have many Comments. Open app/models/post.rb and add the line: has_many :comments somewhere inside the class. This should look like:

The back-end for your comments is almost complete, we only need to configure the url that is used to create your comments. Since comments belong to a post we'll make the URL reflect this. Right now you can see all the configured URLs by typing rake routes in your command prompt. If you do this now you'll get something like the following:

Your URLs (or routes) are configured in all Rails applications in a file config/routes.rb, open it now and remove the line resources :comments. Re-run rake routes and you'll notice that all the URLs for comments have disappeared. Update your routes.rb file to look like the following:

Because comments will be visible from the show Post page along with the form for creating them, we don't need to have URLs for displaying comment listings, or individual comments. When you rerun rake routes now you'll see the following line:

post_comments POST/posts/:post_id/comments(.:format) comments#create

Before we're finished with the backend for our commenting system we need to write the action that will create our comments. For more information on actions please read the Rails Guide on ActionController.

Open app/controllers/comments_controller.rb and and make your code look like the following:

Putting comments into your HTML view

You've creating the database model for your comments, migrated your database, informed Rails of the relationship between comments and posts, configured a URL that lets you create your comments and created the controller action that will create the comments. Now you need to display any comments that have been submitted for a post, and allow users to submit comments. Open app/views/posts/show.html.erb and make it look like:

Comments are now working (if they aren't make sure you restart your rails server) so go ahead and browse to your post and add a new comment.

Publishing your Blog on the internet

Heroku is a fantastically simple service that can be used to host Ruby on Rails applications. You'll be able to host your blog on Heroku on their free-tier but first you'll need a Heroku account. Head to https://www.heroku.com/, click Sign Up and create an account. The starter documentation for Heroku is available at: https://devcenter.heroku.com/articles/quickstart. You've already got an account so you'll need to download the Toolbelt from https://toolbelt.heroku.com/ and set it up on your computer.

About databases

Up until this point we've been using SQLite as our database, but unfortunately Heroku doesn't support the use of SQLite. So we're going to be running Postgres instead. Setting this up is easy you'll need to open the Gemfile and make your Gemfile look like:

After this run the command bundle install --without=production on your command line.

Regarding version control

Heroku also requires that every application is placed under version control before it is deployed. Simply run the following commands on the command prompt to make sure your application is properly controlled:

git init
git add .
git commit -m "initial blog commit"

Deploying your application

In the same command prompt you should be ready to deploy your application. First we create our Heroku application:

heroku create

Now we push our application to Heroku:

git push heroku master

Finally we setup our database:

heroku run rake db:setup

This setup should only need to take place the first time you deploy to heroku. Afterwards you may need to run db:migrate instead.

Finally you should be able to browse to the URL that Heroku has given you and check to see that your blog has been deployed properly!

Welcome to Ruby on Rails. If you're this far along you should definitely head on over to Part 2 which goes more in depth with Rails and begins to add more features to the blogging engine.