Magazine
 
Quick Review:Ajax
 
Automate data entry with Web services and Ajax

The code in Listing 11 iterates through the XML response to find the city (element[1]) and the state (element[2]). element[0] is the zip5 value. As mentioned earlier, the Web service processes up to five ZIP code lookups in one request. A future refinement of this code should consider looping through those values. But in this simple example, you’re always only passing in one ZIP code value per request. This code could also do a better job of handling no city/state response values. The point here is to get minimal function working so you can refine it per the constraints or demands of your particular project. If you have questions about the use of the Builder library or the REXML module, see the RoR API documentation., which has plenty of examples.

If you’re familiar with the way other languages create or parse XML, you’ll immediately recognize how easy it is in RoR. One of the messages that RoR is quietly shouting is, It doesn’t have to be a huge pain in the butt! XML creation—no sweat. Ajax—with one hand behind my back. XML parsing— easy peasy. RoR frequently reminds me that the objective is to get real work done, real quick, for real end users. It’s a nice feeling.

Recapping the action, you validated the ZIP code, created the XML request to call the Web service, parsed the XML response, and put the city and state fields in the @address object. From the point of view of the user, all of this processing has been done asynchronously and in about one second. The user’s cursor is still in the ZIP code field on the html form of the Web browser. Before they know it, this action is returning the @address object with the correct city and state values. And the information is sent to the partial _cityStateFields.rhtml and populated in the user’s browser. Hopefully it’s a good surprise to the user—and you’ve given the feeling that you’re on their side and are here to help them out. You’ve anticipated their needs and done your best to make their lives a little easier.

Listing 12 shows the full code for the cityStateLookup action in the file addressadmin_controller.rb.

Listing 12. Full code for action cityStateLookup (addressadmin_controller.rb)

require ‘open-uri’
require ‘uri’
require ‘rubygems’
require_gem ‘builder’
require “rexml/document”
class AddressadminController < ApplicationController
<!— other methods/actions —>
def cityStateSearch
if params[:zip5].nil?
logger.debug(“zip5 is null”)
elsif !(params[:zip5] =~ /\d{5}/)
logger.debug(“We have a bad ZIP code — not 5 digits.”)
logger.debug(“zip5 = #{params[:zip5]}”)
else
logger.debug(“We have a good 5-digit ZIP code.”)
logger.debug(“zip5 = #{params[:zip5]}”)
if params[:address].nil?
@address = Address.new
else

June 2008 | Java Jazz Up | 22
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,   Download PDF