Since I have a long weekend due to the holidays, I thought I would spend some time getting familiar with ruby on rails. RoR just came out with version 2.0 this past month and pretty much all tutorials and resources are centered around previous versions and a lot has changed. RoR is built around the MVC (model, viewer, controller) pattern which is somewhat new to me as well. I wanted to get more familiar with this pattern to better understand ruby but also because the ASP.NET MVC Framework is coming out. I was pretty impressed with scaffolding in RoR. It also demonstrates why RoR is used in many agile environments. You can get a simple web application functionality and database schema by running three commands. This allows you to have working software right away and gradually build upon it. So you can see this in action I thought I would post the steps below. This is assuming you already have ruby is installed. Since I am using a Mac it is already installed and I just had to run a few commands to upgrade. I am using TextMate (30 day trial) which is a popular editor to use when developing in Ruby. You can also work with about 30 other modules for other languages. You can even blog from it.
Create website structure. Open a command line. Type rails movies. This will create an application named movies.

When you open this up in TextMate you will see the structure that was automatically generated for you. Notice that you have folders from controllers, models and views. Also notice that you have a folder called tests for unit tests.
Now you are going to create the movie scaffolding. This will create the classes needed and EVEN the code that will generate the table for storage. To get scaffolding for Movie type the following command as shown:
script/generate scaffold Movie name:string description:text year:integer (Make sure the types are lower cased)
This also specifies the properties of a movie and the type. This will determine the types in the database table and also the fields on the web page.

Notice the new files that were created below.

In the picture above you notice there is a file in db/migrate. This file was generated automatically with scaffolding and this code will generate our database table. You actually code the table in Ruby so that way our application isn’t specific to the type of database. I am using SQLite, however you can use many others such as MySql. Below is the code that is in this file.

To create the database type the following in to the command prompt:
rake db:create

Following that we will run the migration files which is the code that was created two steps up. After you do this the table will be created as defined in the migration files. To create the table enter the following into the command prompt:
rake db:migrate

Our application is now created with basic CRUD functionality for movies. You must start the server though to serve the pages. To do this enter the following into the command prompt:
script/server

Then go to the following address in a browser window and you will see the following windows:
http://0.0.0.0:3000/movies



As you can see with scaffolding, you can get up and running pretty fast once you understand it. Scaffolding, however, is not meant to be used to build your whole website, just enough to get a site that is functional fast. I plan on posting more once I learn more.
There are not many resources right now for the 2.0 version although. Please feel free to let me know of any resources that you have found useful.
For some basic language/syntax check out:
Leave a Reply