An Entire Rolodex App
Using the same Rails scripts and default implementation, default scaffolding, and CRUD activity can also be created for contacts and contact addresses. Simply run the four scripts below to set up the controller and models for contacts and contact addresses:
ruby script\generate model Address
ruby script\generate model Contact
ruby script\generate controller Address
ruby script\generate controller Contact
The code for the Address and Contact models gets a little more interesting because you need to associate the Contact to the ContactType you created above and you need to associate Contact instances with Addresses. Below is the code for both the Address and Contact models:
class Address < ActiveRecord::Base
class Contact < ActiveRecord::Base
This code causes the generation of Rail Association methods that tie the objects together through associative relationships. Ruby generates methods to navigate from Contact to Address and ContactType and from Address to Contact using the foreign keys in the database. There are special methods for belongs_to, has_one, has_many
, and has_and_belongs_to
. With these code additions, you will find that a Contact's address is available through a call to @contact.address
. To then get the Address's street, simply continue to follow the object/attribute chain: @contact.address.street
On the Contact and Address controller side of things, there are a few more operations that you will want override. For example, when editing an existing Contact model object through the Contact controller, you want to set an attribute on the controller to the current Contact model object being edited. This can be found using a contact id parameter returned from the Contact's list view and the Contact model’s default find method. This allows the Contact controller to pass the currently select contact to the appropriate view. You will also want to set another attribute on the Contact controller to the list of current Contact Types to pass contact types to the edit view and fill a Contact Type drop down list on the view.
@contact = Contact.find(@params['id'])
@contact_types = ContactType.find_all
Perhaps the trickiest of the controller override methods is the Address controller. When creating a new Address for a contact, the id for the Contact selected is used to find the contact and appropriately associate and save the Contact model object with the new Address model object when the Address is saved.
@address = Address.new(@params['address'])
@contact = Contact.find(@params['contact_id'])
@contact.address_id = @address.id
redirect_to :controller => 'contact', :action => 'list'
The complete Contact and Address controller classes are shown in Listing 1
The default Web interface provided by the scaffolding is a little stark. In order to dress it upeven a little bitwith drop down lists (for selecting the contact type for a Contact), HTML tables (for displaying the contact and address lists), or navigation to various parts of the Ruby Rolodex Web site, you need to override the edit, new, list, and show views for Contact and Address. Listing 2 shows the Contact list.rhtml view.
Most of that code goes to formatting the HTML output, but you will notice the fetch of the current contact information from the Ruby calls to
This code also associates the returned contact firstname to an HTML reference link that takes the user to the Contact "show" or display view and passing the contact id as a parameter.
Listing 3 shows the Contact edit view (edit.rhtml), which demonstrates the use of a Ruby script to iterate through the contact types (stored in the Contact controller's @contact_types attribute) in order to provide the options (and selected value) to the HTML select/option value list. Note the highlighted Ruby script in the middle of the page.
Like with JSP or ASP script code, you can interweave Ruby code into HTML code. For instance, to create a drop down list containing the selection of contact types for the Contact, just use a simple Ruby iterator on the collection of ContactType model objects with HTML select option code.
The code for the complete Ruby Rolodex Web application is provided in the zip file attached to this article. Simply download it and when you unzip it, it will unfold into a Rails project directory called rolodex by default. While feature complete, I am sure you can make the application feature rich in no time using Ruby and Ruby on Rails.
Additional Rails Features
Though the power behind metaprogramming is inspiring, it's like those ad campagins sauy. . . "But wait! There's more!" Rails comes with many additional features you will want to explore once you get the basics of Web application development down. This includes, but is not limited to, security, transactions, unit test features, and caching. And because Rails is Ruby, there are a number of additional libraries like Rake and even an AJAX for Rails project that come in very handy, making Web applications even faster to build and more flexibleif that even seems possible.
During this exercise, you were able to do all your development using the command prompt and a simple text editor. For bigger, real-world Web-application projects, a good IDE can be a real time saver. RadRails is an integrated development environment for Ruby on Rails development built on top of Eclipse. Along with the capability to edit Ruby code files and .rhtml files, RadRails uses a graphical user interface (a set of wizards) to drive the generator scripts and it also comes with a built in WEBrick server for testing the application. Once you get past simple applications, it would be worth your time to investigate this or other Rails IDEs.
The Express Train?
While Ruby is over ten years old, Ruby on Rails is quite new. It was first released in July 2004. The current version (version 1.1) was just released at the end of March 2006. Even given its relative youth, I hope this article has helped you realize that Ruby on Rails offers considerable promise to those looking for productivity improvements in Web application development. According to Ruby on Rail developers, a number of commercial sites use Ruby on Rails in production to include Basecamp, Ta-Da List, Odeo, and CD Baby. So when considering your next Web site, consider an express ride on the railsRuby on Rails.