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).
Figure 21. Creating a new table
Name the table
recipes (Figure 22).
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
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
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).
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.
Figure 25. Adding the
Repeat the above procedure to create an
instructions field as
text, as shown in Figure 26.
Figure 26. Adding the
The recipe table should now resemble Figure 27.
Figure 27. The modified
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
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).
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
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
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.
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.
Figure 31. One line of code in
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
You should see something like Figure 32.
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
instructions fields (Figures
33 and 34).
Figure 33. Adding the
Figure 34. Adding the
Refresh your browser to see a page similar to Figure 35.
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.
Figure 36. A new recipe
You should see the results, as in Figure 37.
Figure 37. A listing of all recipes
Add another one by clicking the "New recipe" link and entering the data, as in Figure 38.
Figure 38. Another new recipe
After you click Create you should see something like Figure 39.
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.