While getting to grips with Rails I followed a number of tutorials and screencasts, many of which were confusing and ended with me deleting my Rails projects and starting again. What I’m hoping to do with this series of blogs is combine all of the best bits from the tutorials I used to guide you through the process of creating an actual application.

For this guide I’m going to assume that you’ve got Rails set up and I’m going to be using the environment detailed in my earlier post although you can do much of this with just a basic setup.

So, what are we going to create?

The first tutorial I embarked on was creating a simple blog. This consisted of a main homepage listing recent posts as well as pages for managing and creating posts, I later added the facility to comment on posts and started looking at styling, templates and Javascript to lift the application.

What we’re going to create here is an application called “Gabber”–a simple collaborative micro-blog (a bit like Twitter) that allows people to post a short message and allow everyone to add comments. The reason it’s collaborative is because we’re not going to look at authentication so it’ll be an open system.

You can view the final application at http://gabber.d13design.co.uk/gabber/public/posts.

Right then, MVC–what’s that?

Before we get too into the development it’s probably best to understand the development approach for Rails a little more. MVC stands for model, view and controller and is the name given to the development methodology the Rails uses. It really is quite simple and (hopefully) should only take a few moments to explain.

  • The model is really the data that the application will use. Think of an address book system that holds details of people you know–”Person” would be the model. Each person would have details about their name, address and telephone number which would all be stored within the database. The great thing about this approach is that you don’t need to worry about how that data is stored so it’s dead easy for a non-technical developer to get up-and-running with a database driven system.
  • The view is the aspects of the application that deal with displaying content to a user via their web browser. It is, essentially, a bunch of templates that the application uses to format and render content to the screen.
  • The controller is what deals with interpreting a request from a user, manipulating data in the model and then triggering a view to render information back to the user.

Simple! Don’t worry if you’re still a little in the dark, Rails makes it easy to get started and it should all come into focus as me move on.

Getting started

The first thing to do is set up a new Rails project called “gabber”. If you’re using Aptana you can just do File > New > Rails project and set it up to use a MySQL database and a WEBrick server or you could do it the hardcore way and use a Rails console and type the following:

rails gabber -d mysql

What this statement does is creates a new Rails application called “gabber” and sets it up to use a MySQL database. I actually found it incredibly simple to use the Rails console for a lot of the following tasks so I encourage you to do the same. If you’re using Aptana you can easily open a console window by doing Window > Show view > Console and then clicking the little “open console” icon and choosing “Rails shell”.

Open a new Rails shell

Defining the first model

At this point you’ll start to see just how easy Rails makes development. We’ll use what’s called a “scaffold” to help us create a model, a controller, a database migration and some views for our core object–a micro-blog post.

So, we’ll run the scaffold script and tell it to create the Post model and give it a title (marked as a string) and a poster (also a string). Make sure your gabber project is selected and run the following statement in your Rails console:

script/generate scaffold Post title:string poster:string

When you hit enter, your rails console should list all of the files that it automatically generated for you–including the model, views and controller. What the scaffold also created is a database migration file, you can find this in your application under “db/migrate/” and it’ll be called something like “001_create_posts.rb”. This little file saves you ever having to play with your database and, if you open it, you’ll see some contents similar to the following:

class CreatePosts < ActiveRecord::Migration
 def self.up
  create_table :posts do |t|
   t.string :title
   t.string :poster

   t.timestamps
  end
 end

 def self.down
  drop_table :posts
 end
end

Just by reading through this you can see that this file is essentially telling the system to create a table with a title column and a poster column. You’ll also see that it’s added a column for timestamps–easy huh?

Before we can run this migration and create our table we need to set up our database connection.

Setting up the database

First we need to actually create a database to use, so fire up your MySQL application and create a database called “gabber_development”. You can use the following SQL statement to do this:

CREATE DATABASE gabber_development

Next, open the file “config/database.yml” in Aptana and you’ll see your database connections defined. Rails has 3 deployment environments–development, test and production. We’re only really interested in the development set for now so you can ignore that part in this file that defines the testing connection. Edit this file to use the correct information about your MySQL server and tell it to use the “gabber_development” database.

development:
  adapter: mysql
  encoding: utf8
  database: gabber_development
  username: your_db_username
  password: your_db_password
  host: your_db_host

Save this file and return to your Rails console.

Running the migration

Now that the database is setup you can run the migration and let the migrate file set up your table. To do this just run the following command in your Rails console:

rake db:migrate

If you’re lucky, and everything’s worked, you’ll see an output telling you that a table was generated:

== 1 CreatePosts: migrating ===================================================
-- create_table(:posts)
   -> 0.0750s
== 1 CreatePosts: migrated (0.0750s) ==========================================

Running your application

Now that the database and it’s tables are ready to go you can start your application. Using Aptana, right-click on the project folder in the “project” window and select “Run as Rails application”.

After a few moments a small browser should open within Aptana showing a success screen–this lets you know that Rails is running.

Because the main controller we built using the scaffold is called “posts” (and remember that the controllers handle requests from the user) we need to point the browser at “/posts”. So, if your server is running at “http://localhost:3000/” then point your browser at “http://localhost:3000/posts/”.

The way Rails deals with URLs and controllers is fascinating but too much to cover in these blogs so, for now, be happy that it works and mark it as an area to read up on!

Because we used a scaffold to create the Post model it handily built view files and controller elements to allow us to add, view, edit and destroy posts–amazing!

Have a play with your new application and add your first micro-blogs.

What’s next?

Next we’ll look at how the view files can be edited to create an “attractive” micro-blog and we’ll also look at creating another model to handle commenting.