Rolling with Ruby on Rails
Pages: 1, 2, 3, 4, 5

Creating the recipes Table

Our cookbook will contain recipes, so let's create a table in our database to hold them.

In the left-hand pane of MySQL-Front, right click on the cookbook database you just created and select New>Table... from the pop-up menu (Figure 21).

creating a new table
Figure 21. Creating a new table

Name the table recipes (Figure 22).

the Add Table dialog box
Figure 22. The Add Table dialog box

Important note: MySQL-Front will automatically create a primary key named Id, but Rails prefers to call it id (all lowercase). I'll explain more later, but for now just change it. In the left pane, select the recipes table you just created. In the right pane, right-click on the Id field, select Properties (Figure 23), and change the name to id.

renaming the primary key
Figure 23. Renaming the primary key

Adding Recipe Fields

Now that we have a recipes table, we can start adding fields (columns) to hold recipe data. Let's start by creating title and instructions fields. Eventually, we will need more fields, but this is a good place to start.

With the recipe table selected, right click in a blank area of the right pane and select New>Field... (Figure 24).

adding a new field
Figure 24. Adding a new field

Create the recipe title field as a varchar(255) with nulls not allowed, so that every recipe must have title text. Figure 25 shows the options in the pop-up window.

adding the title field
Figure 25. Adding the title field

Repeat the above procedure to create an instructions field as text, as shown in Figure 26.

adding the instructions field
Figure 26. Adding the instructions field

The recipe table should now resemble Figure 27.

the modified recipe table
Figure 27. The modified recipe table

The Excitement Begins

Everything we have done up to this point has been pretty short and painless, but not particularly exciting. This is where that changes. We can now have the very beginnings of our cookbook application up and running in record time!

Create the Model

First, create a Recipe model class that will hold data from the recipes table in the database. Figure 28 shows where it should live.

the Recipe model class
Figure 28. The Recipe model class

Open a command window to the cookbook directory (c:\rails\cookbook) and run the command:

ruby script\generate model Recipe

This will create a file named recipe.rb containing a skeleton definition for the Recipe class. Right-click on this file and choose Edit to look inside (Figure 29).

the contents of recipe.rb
Figure 29. The contents of recipe.rb

This seemingly empty class definition is the recipe business object that Rails maps to the recipes table in the database. You will see more concretely what I mean by this in a moment. Right now, I want to point out that this little bit of programming magic happened because we used a Rails naming convention: a singular model class name (Recipe) maps to a plural database table (recipes). Rails is smart about English pluralization rules, so Company maps to companies, Person maps to people, and so forth.

Further, Rails dynamically populates the Recipe class with methods for accessing the rows in the recipes table and an attribute for each column in the table.

Very shortly, you will see a dramatic demonstration of this dynamic connection between the Recipe class and the recipes table.

We are now very close to seeing something work. We need to create a recipe controller (Figure 30) with actions to manipulate the recipes in the database via the standard CRUD operations: create, read, update, and delete. Rails makes this easier than you might think.

the Recipe controller in its native environment
Figure 30. The Recipe controller in its native environment

Open a command window in the cookbook directory (c:\rails\cookbook) and run the command:

ruby script\generate controller Recipe

This will create a file named recipe_controller.rb containing a skeleton definition for the RecipeController class. Right-click on this file, choose Edit, and add the line scaffold :recipe as shown in Figure 31.

one line of code in RecipeController
Figure 31. One line of code in RecipeController

This single line of code will bring the database table to life. It defines actions for all CRUD operations, immediately allowing us to create, read, update, and delete recipes in our database!

Open a browser and navigate to http://127.0.0.1:3000/recipe/new. You should see something like Figure 32.

creating a new recipe page
Figure 30. Creating a new recipe page

Now this is pretty cool! We haven't done much of anything and we can already start to populate our database. Don't do that just yet, though. Let's add a few more fields to the recipe table first.

Use MySQL-Front to add description and date fields between the title and instructions fields (Figures 33 and 34).

adding the description field
Figure 33. Adding the description field

adding the date field
Figure 34. Adding the date field

Refresh your browser to see a page similar to Figure 35.

a new recipe page with the new fields
Figure 35. A new recipe page with the new fields

Now, that is way beyond cool--it's awesome!

OK, calm down and enter a test recipe. Fill in the fields as shown in Figure 36 and click on the Create button.

a new recipe
Figure 36. A new recipe

You should see the results, as in Figure 37.

a listing of all recipes
Figure 37. A listing of all recipes

Add another one by clicking the "New recipe" link and entering the data, as in Figure 38.

another new recipe
Figure 38. Another new recipe

After you click Create you should see something like Figure 39.

a fuller list of all recipes
Figure 39. A fuller list of all recipes

We now have an amazing amount of functionality, by merely building a database table and typing in a single line of code. It may not be pretty yet, but we'll fix that soon enough.

In the meantime, play around with adding, deleting, and editing recipes. Go ahead; I'll wait for you in the next section.

Pages: 1, 2, 3, 4, 5

Next Pagearrow