Automate data entry using Web services and Ajax

Source: Internet
Author: User
Tags naming convention valid web services ruby on rails

Some background knowledge of programming thought

The United States Postal Service (USPS) provides multiple WEB services. One of these WEB services accepts the postal area code and returns the corresponding city and state names. In the sample application in this article, you will use this citystatelookuprequest to omit some of the user's input operations. This feature also provides better address data for your database because it reduces the chance of typing errors.

Prerequisites and hypothetical circumstances

David Heinemeier Hansson, who conceived and created Ruby on Rails, is indeed a Sundance! In RoR, he realized a lot of good ideas that made it easier to develop WEB applications, as one of my friends said, "It makes programming more interesting!" "I think that other frameworks and programming paradigms will undoubtedly support these ideas. However, this tutorial does not describe how to create an RoR application.

The assumption in this article is that you have 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. Moreover, we also assume that you:

Learn some basic design principles for WEB application development.

A RoR application has been created.

Learn the following basic sections of the RoR application: Activesupport, ActiveRecord, Actionview, Actioncontroller, and migrations.

There is a database (such as ibm®db2® or MySQL) that is configured to RoR applications.

Communicate with users by anticipating the needs of their users and know the importance of saving their time.

Table 1. Let's say you have a RoR application with these objects

Ruby on Rails files Directory Describe
Edit.rhtml .. /app/views/addressadmin Edit the view of an address
_form.rhtml .. /app/views/addressadmin Fragments used by edit.rhtml
Addressadmin_controller.rb .. /app/controllers A controller for the monotony of an HTML input table
Address.rb .. /app/models ActiveRecord objects
001_create_addresses.rb .. /db/migrate To create a script for Addresses database tables

Solution Overview

The following table lists the steps involved in completing this solution. (Don't worry, the remainder of this article describes each of these steps.) Note that the word fragment is a term in Ruby on Rails. It is a reusable piece of code that is related to what is displayed in the Web browser. Most modern frameworks include some type of template and the fragment functionality used to dynamically assemble templates to generate Web pages. Fragments provide practical convenience for application developers and greatly help them mitigate the burden of development. The RoR naming convention is preceded by an underscore (such as _addressform.rhtml) in front of the fragment.

Modify the _form.rhtml fragment to display the postal area code before the city and the state.

Add a fragment (_citystate.rhtml) to display the city and state input fields.

Modify the _form.rhtml fragment to "Listen" for changes to the postal Area Code field and issue an Ajax call to the server.

Modify the controller to verify the postal area code (5 digits). If it is not valid, an empty Ajax response is returned to the client.

Modify the controller to create a valid XML request and send it to the USPS Web services.

Modify the controller to receive and parse the XML response from USPS Web services.

Modify the Ajax response to populate the value of the WEB services for the _citystate fragment.

Point out some ways to improve the solution and send your suggestions to the author by e-mail.

Figure 1. Solution Overview

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.