15 Apr 2008

Yet another rails tutorial. There are tons of these but I felt like making my own. My rails version at time of writing is 2.0.2. In this tutorial series we’ll be creating a basic cookbook website. The objective is to give the reader an introduction to software development in Ruby on Rails.

For windows be sure to change all slashes / to backslashes \.

First we start by creating the project:

$ rails cookbook
 ... bunch of files generated ...
$  cd cookbook

Let’s create the scaffold for the Cookbooks. A “scaffold” is the bare bones you need for functionality. A model, a migration script, a controller, 4 views and a layout. It also creates tests and fixtures.

$ ruby script/generate scaffold Cookbook title:string author:string

This command will use the migration script generated above and create the database table “cookbooks” – Table names are plural.

$ rake db:migrate

Open new terminal window and navigate to cookbook directory and start the server:

$ ruby script/server

Point Browser to http://localhost:3000/cookbooks. You should see an empty list of all cookbooks. Create a new cookbook by clicking the New Cookbook link.

Okay, now let’s create the scaffold for the Recipes and migrate the database:

$ ruby script/generate scaffold Recipe name:string ingredients:text description:text number_of_servings:integer

Now update the database:

$ rake db:migrate

Point Browser to http://localhost:3000/recipes and create a new recipe.

Great! Now we have a functional Cookbook and Recipe model. But now we realize that we need to make recipes belong to cookbooks. We’ll do that in Rails Lesson #2.