Ruby on Rails – Tutorial 1 – Creating a Rails Project

I heard about Rails back when I was a programmer at BlueHost / Hostmonster since they are one of the “preferred” web hosting companies for Ruby on Rails type applications. There weren’t very many clients who used it, but when questions came in about it, I always seemed to be approached with them. I guess tech support people think that if you are generally willing to help them out and you know a programming language, you know them all. Well, I eventually got sick of saying I don’t know and got some basic knowledge going of Ruby on Rails, and was pleasantly surprised!

Ruby on rails is probably one of the best kept secrets in the world of Object Oriented Programming in a web world. And I am still not sure why it is kept a secret. I guess they figure that it will remain an elite language if they do that. Well, I disagree and so decided to give you the basics of this great language and framework.

I am going to assume you have already installed ruby, gems, and rails. If you have not, Google would be overjoyed to provide you with a plethora of commands, download locations, and configuration instructions for any architecture you wish to run ruby and rails on. I am furthermore going to assume you are using a database. I myself am doing this on CentOS 5 with MySQL 4.1.22 and Ruby 1.8.6 running Rails 2.1.0. I don’t imagine that that would be too far from the “norm” in the Ruby community. Well, so much for the preliminaries, now on to the main event

Installing a skeleton rails application is really quite simple. Using the rails command is quite simple and fairly straight forward. You can see the help for it by typing

# rails --help

Basically, you type in rails [path to wherever you want your rails app to be] –[whatever options you want]. My demo was created like so:

rails /home/jay/railstest --database=mysql

So I put the gave it the path to where I wanted my rails app to be (/home/jay/railstest) and told it which database I wanted to use (–database=mysql). There are some other options, take a look at rails –help to see them. But really, there aren’t many. It’s pretty simple, very straight forward, as it should be.

The Rails command creates the directory that you specified if it doesn’t already exist and fills it with a bunch of stuff, the framework skeleton. The structure therein is as follows:

  • app – The app directory is where most of the fun is. It is where you will put most all your code specific to your application. So get used to the idea. This directory contains several sub directories of note.
    • controllers – Here you put all the controller classes you create to control your models. They need to be named [modelname]_controller.rb since there is some automatic URL mapping that goes on and it follows that naming convention. All Controller classes inherit from ActionController::Base.
    • models – Here you put all the model class definitions and code. Most of the time, these classes inherit from ActiveRecord::Base
    • views – The template files for whatever views you have. You should also put stylesheets, images, etc here and if need be sym-link them to your public areas.
    • helpers – Here you put view helpers, and they need to be named [viewname]_helper.rb
    • apis – Here you put API classes for web services and such
  • config – This folder holds, surprisingly, configuration files for the Rails Environment, routing maps, database configs, and other like materials.
  • components – Here are the components that really self-contained mini-apps that are used to link controllers, models and views together.
  • db – This folder has a file called schema.rb that holds your database schema and also migrations for your schema
  • lib – As most developers would assume, this folder holds any code or libraries that are don’t fit into the other categories (models, controllers, views) that need to be included.
  • public – This is where the web server looks to. It has images, stylesheets, javascript files, static HTML pages, etc (or symlinks thereto).
  • script – Here we have some helper scripts for automation, generation, etc.

And there you have your default app. Next time we’ll start playing with setting up the database.

Leave a Reply