Rolling with Ruby on Rails
Pages: 1, 2, 3, 4, 5
Let's Write Code
We'll create an online collaborative cookbook for holding and sharing everyone's favorite recipes. We want our cookbook to:
- Display a list of all recipes.
- Create new recipes and edit existing recipes.
- Assign a recipe to a category (like "dessert" or "soup").
You can create your cookbook application in any directory you like, but I used c:\rails\cookbook. All paths used in this article assume this base directory path. If you choose a different location, be sure to make the necessary adjustments as you see application paths in this article.
If you like, you can download the complete Rails cookbook example and follow along. If you do this, look in the cookbook/db/ directory for a file to use to rebuild the database.
Creating an Empty Rails Web Application
Rails is both a runtime web app framework and a set of helper scripts that automate many of the things you do when developing a web application. In this step, we will use one such helper script to create the entire directory structure and the initial set of files to start our cookbook application.
- Open a command window and navigate to where you want to create this cookbook web application. I used c:\rails.
Run the command:
rails cookbookThis will create a cookbook subdirectory containing a complete directory tree of folders and files for an empty Rails application.

Figure 6. A newly created Rails application
directory
Testing the Empty Web Application
A Rails web application can run under virtually any web server, but the most convenient way to develop a Rails web application is to use the built-in WEBrick web server. Let's start this web server and then browse to our cookbook application.
- In your open command window, move into the cookbook directory.
Run the command:
ruby script\serverto start the server (Figure 7).
- Now open your browser and browse to
http://127.0.0.1:3000/. You should see something like Figure 8. [Editor's note: Unless you're following along with the article, these links probably won't work for you. Don't panic--127.0.0.1 is a special address reserved for the local machine.]

Figure 7. Starting the WEBrick server
Leave the command window open and the web server running, as we will be using it as we proceed.

Figure 8. The Rails default page
A Rails Application's Directory Structure
Rails tries very hard to minimize the number of decisions you have to make
and to eliminate unnecessary work. When you used the rails helper
script to create your empty application, it created the entire directory
structure for the application (Figure 9). Rails knows where to find things it
needs within this structure, so you don't have to tell it. Remember, no
configuration files!

Figure 9. A Rails application directory
structure
Most of our development work will be creating and editing files in the c:\rails\cookbook\app subdirectories. Here's a quick rundown of how to use them.
- The controllers subdirectory is where Rails looks to find controller classes. A controller handles a web request from the user.
- The views subdirectory holds the display templates to fill in with data from our application, convert to HTML, and return to the user's browser.
- The models subdirectory holds the classes that model and wrap the data stored in our application's database. In most frameworks, this part of the application can grow pretty messy, tedious, verbose, and error-prone. Rails makes it dead simple!
- The helpers subdirectory holds any helper classes used to assist the model, view, and controller classes. This helps to keep the the model, view, and controller code small, focused, and uncluttered.
Controllers and URLs
In a moment, we will create our cookbook database and begin developing our application. First, it's important to understand how controllers work in Rails and how URLs map into (and execute) controller methods.
Controller classes handle web requests from the user. The URL of the request maps to a controller class and a method within the class. How does this work?
Leave your existing command window open with the web server running, open a second command window, and navigate to the application's base directory, c:\rails\cookbook. It will look like Figure 10, at least in a moment.

Figure 10. The cookbook controller directory
We will use another Rails helper script to create a new controller class for us. In the command window, run the command:
ruby script\generate controller MyTest
This will create a file named my_test_controller.rb containing a
skeleton definition for the class MyTestController.
In the c:\rails\cookbook\controllers directory, right-click on this file and choose Edit. The file should resemble Figure 11.

Figure 11. Editing
MytestController
What happens if you browse to something that you know does not
exist? Try http://127.0.0.1:3000/garbage/.
Figure 12 shows the results.

Figure 12. Browsing to an unknown controller
That's not too surprising. Now try http://127.0.0.1:3000/My_Test/,
shown in Figure 13.

Figure 13. Browsing to the new controller
Hmmm. Now that's different. The Mytest part of the URL maps
to the newly created controller. Now it seems that Rails tried to find an
action named index in this controller but couldn't.
Let's fix that. Add an index method to your controller class as in Figure
14.

Figure 14. The index method of
MytestController
Refresh your browser, and you should now see something more like Figure 15.

Figure 15. The result of the index method
You will have the same results with http://127.0.0.1:3000/My_Test/index,
too.
Let's add another action to the controller just to make sure you have the
idea. Add the dilbert method from Figure 16.

Figure 16. The dilbert method
Now browse to http://127.0.0.1:3000/My_Test/dilbert
and you'll see something like Figure 17.

Figure 17. The output of the dilbert
method
I think you have the idea.
Let's create our database now and work on some real pieces of our cookbook application.
Creating the Cookbook Database
It's time to create the cookbook database and tell Rails how to find it. (This is the only configuration that you will find in Rails.)
Start MySQL-Front and log in to your locally running MySQL instance (
localhost) as root, using an empty password. You should see something like Figure 18.
Figure 18. MySQL-FrontThere are two existing databases,
mysqlandtest. Create a new database namedcookbook. Execute the menu commandDatabase>New>Database...and enter the database namecookbook, as Figure 19 illustrates.
Figure 19. Creating a new databaseClick Ok to create the database.
To tell Rails how to find the database, edit the file c:\rails\cookbook\config\database.yml and change the database name to
cookbook. Leave the username as root and the password empty. When you finish, it should look something like Figure 20.
Figure 20. The database.yml configuration file
Rails lets you run in development mode, test mode, or production mode, using different databases. This application uses the same database for each.
Editor's note: A recent change in Rails requires that you restart the webserver, or else Rails will never see the new database and the subsequent steps will fail. Hit Ctrl-C or close the window as appropriate and relaunch the web server at this point.
