トップ «前の日記(2005-03-21) 最新 次の日記(2005-03-24)» 編集

3 日坊主日記


2005-03-23 [長年日記]

_ [Rails] Ajax してみました

郵便番号を設定したら都道府県と住所を更新します。 (WinXP IE6 しか試してません)

  • customer/edit.rhtml の JavaScript:
 <script>
 /*
   Using the XML HTTP Request object
   http://jibbering.com/2002/4/httprequest.html
 */
 var xmlhttp = false
 /*@cc_on @*/
 /*@if (@_jscript_version >= 5)
 // JScript gives us Conditional compilation, we can cope with old IE versions.
 // and security blocked creation of the objects.
  try {
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP")
  } catch (e) {
   try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
   } catch (E) {
    xmlhttp = false
   }
  }
 @end @*/
 if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
   xmlhttp = new XMLHttpRequest()
 }

 function set_zip() {
   var form = document.forms[0]
   var url = "/customer/set_zip/" + form.elements['customer[zip]'].value
   xmlhttp.open("GET", url, true)
   xmlhttp.onreadystatechange = function() {
     if (xmlhttp.readyState == 4) {
       var root, pref, addr, opts, opt, i
       root = xmlhttp.responseXML.documentElement
       pref = root.selectSingleNode("//pref").text
       addr = root.selectSingleNode("//addr").text
 //    for (opt in form.elements['customer[pref]'].options) {
 //    }
       opts = form.elements['customer[pref]'].options
       for (i = 0; i < opts.length; i++) {
         opt = opts[i]
         if (opt.text == pref) {
           opt.selected = true
 //        form.elements['customer[pref]'].selectedIndex = opt.index
           break
         }
       }
       form.elements['customer[addr]'].value = addr
 /*
       form.elements['customer[addr]'].value = root.getElementsByTagName("addr").item(0).text
       form.elements['customer[addr]'].value = xmlhttp.responseText
 */
     }
   }
   xmlhttp.send()
   return false
 }
 </script>
  • customer/edit.rhtml の郵便番号:
 <tr>
   <th>郵便番号</th>
   <td><%= text_field "customer", "zip", "size" => 10 %>
     <input type="button" name="update_zipcode_button" value=" 適用 " onclick="set_zip()" />
   </td>
 </tr>
  • customer_controller.rb:
 def set_zip
   zipcode = Zipcode.find_first(["zip7 = ?", @params["id"]]) || Zipcode::UNKNOWN

   # templates/*.rxml を使ったほうがきれいに書けそうだけど
   # action_view/base.rb では UTF-8 固定ぽい。

   xm = Builder::XmlMarkup.new
   xm.instruct! :xml, :encoding => "Shift_JIS"
   render_text \
     xm.zipcode {|xm|
       xm.pref zipcode.pref
       xm.addr zipcode.addr
     }
   @headers["Content-Type"] = 'text/xml'
 end
  • customer_controller_test_set_zip.rb:
 require File.dirname(__FILE__) + '/../test_helper'
 require 'customer_controller'

 # Re-raise errors caught by the controller.
 class CustomerController; def rescue_action(e) raise e end; end

 class CustomerControllerTest_find_ret < Test::Unit::TestCase
 # fixtures :shops, :customers

   def setup
     create_fixtures :shops, :customers
     @controller = CustomerController.new
     @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
     @request.session["shop_id"] = 1
   end

   def test_set_zip
     get "set_zip", "id"=>"7600013"
     assert_success
     assert_template_xpath_match "//pref", "香川県"
     assert_template_xpath_match "//addr", "高松市扇町"
   end

   def test_set_zip_unknown
     get "set_zip", "id"=>"760"
     assert_success
     assert_template_xpath_match "//pref", nil
     assert_template_xpath_match "//addr", nil
   end

 end

Ajax イイ!

_ [Rails] assert_template_xpath_match encoding

上の test は失敗する。 REXML の Element#text (= Text#value) は XMLDecl#encoding に合わせてくれないみたい。

本日のツッコミ(全1件) [ツッコミを入れる]
# sayvoraykc (2007-10-08 00:20)

Hello! Good Site! Thanks you! fdjpgayzezgjx

[]