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

18 %>
20 <%= debug(params) %>

Note: The two-digit line numbers on the very left edge of the listing are for explanation purposes; they don’t appear in the code.

That’s it! In about 10 lines of Ruby code, this view has been embellished with Ajax functionality. Behind the scenes, RoR and the prototype library handles all the JavaScript. Let’s go through these 20 lines of code.

Line 01 instructs RoR to include the Prototype and Scriptaculous JavaScript libraries. Lines 03-12 are the same as before. Line 13 uses the observe_field method from the Prototype library. observe_field is a helper method in the PrototypeHelper class. In plain language, lines 13-17 say to check the zip5 input field every two seconds. If the zip5 input field has user input, then call the action cityStateSearch in the current controller, which is addressadmin_controller.rb. This logic is being run by JavaScript within the user’s browser. When the zip5 input field is changed, an Ajax call is made from the user’s browser to the server. Notice the correlation between line 09 and line 15: Line 15 identifies what to do with the response from the action cityStateSearch. The response of the action, if any, updates the <div> tag named ajaxLookup. Line 09 has the div tag with the ID equal to ajaxLookup. So the response from the action cityStateSearch is passed into line 10. Line 17 explains what name and value pair to send to the action. So in this example, the string zip5=90210 is passed to addressadmin_controller’s action named cityStateSearch.

Next, you begin work on the controller functionality. The last step specified that the user’s browser would make an asynchronous call to the action named cityStateSearch when it detected a change in the ZIP code (that is, zip5) input field. Here are the main pieces of functionality that you have to code on the server side:

  • Validate the ZIP code.
  • Build the XML to call the Web service.
  • Call the Web service.
  • Parse the response from the Web service.
  • Send the response back to the user’s Web browser.

Step 4: Validate the ZIP code
There’s no sense calling the USPS Web service with a invalid ZIP code. With a little effort you can eliminate most invalid ZIP codes. In the U.S., the ZIP code is five numbers. Listing 5 shows some code that checks to see if the zip5 parameter consists of five numbers.

Listing 5. Validate the ZIP code (addressadmin_controller.rb)

01 def cityStateSearch
03 if params[:zip5].nil?
04 logger.debug(“zip5 is null”)
05 elsif !(params[:zip5] =~ /\d{5}/)
06 logger.debug(“We have a bad ZIP code — not 5 digits.”)
07 logger.debug(“zip5 = #{params[:zip5]}”)
08 else
09 logger.debug(“We have a good 5-digit ZIP code.”)
10 logger.debug(“zip5 = #{params[:zip5]}”)

June 2008 | Java Jazz Up | 18
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,

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