First published by IBM developerWorks at http://www.ibm.com/developerWorks. Visit ibm.com/ developerWorks for more tutorials on open standard technologies , IBM products, and more.
Deploy Web 2.0 technologies to save time and ensure data accuracy Level: Advanced
Norbert (Norb) R. Ryan, III (nryan@us.ibm.com), Software Engineer, IBM
14 Feb 2008
Let’s cut through the chatter and find out how a Web service and Asynchronous JavaScript + XML
(Ajax) can improve an application, in this case a Ruby on Rails (RoR) application. This article shows
you how to spruce up a common Web activity—entering a street address—with Ajax and a call to a
Web service. Learn a few tricks to combining these fundamental Web 2.0 components.
A little background on the idea
The United States Postal Service (USPS) has made several Web services available (see the USPS
Web tools sidebar). One of those Web services accepts a ZIP code and returns the corresponding
city and state. In this example application, you make use of this CityStateLookupRequest to save
the user some typing. This feature also gives you better address data in your database, because
you reduce the chances of typing errors.
Prerequisites and assumptions
David Heinemeier Hansson, the ideological spirit and creator of Ruby on Rails, is a smart guy! In
RoR, he implemented a number of great ideas, ideas that make developing Web applications much
easier and, as a friend of mine remarked, “It makes programming fun again!” There’s little doubt in
my mind that other frameworks and programming paradigms will espouse these ideas. However,
this isn’t a tutorial on how to create an RoR application. (See the Resources section at the end of
this article for links to good tutorials and reference information.)
The assumption here is that you’ve created an RoR application that has an HTML input form for an
address (for example, 590 Madison Ave, New York, NY 10022). This Rails application also has a
model named address and a corresponding database table.
Furthermore, let’s assume that you:
- Understand basic design principles of Web application development.
- Have created an RoR application.
- Understand the basic parts of an RoR application: ActiveSupport, ActiveRecord, ActionView,
ActionController, Migrations, and so on.
- Have a database (like IBM® DB2® or MySQL) configured to work with the RoR application.
- Connect with users by anticipating their needs and know the importance of saving them
time.
Table 1. Assuming you have an RoR application with these objects
edit.rhtml |
../app/views/addressadmin |
View for editing an address |
_form.rhtml |
../app/views/addressadmin |
Partial used by edit.rhtml |
addressadmin_controller.rb |
./app/ |
controllersController called by the HTML input form |
address.rb |
../app/models |
ActiveRecord object |
001_create_addresses.rb |
../db/migrate |
Script for creating the Addresses database table |
|