Script & CSS

2008年3月18日 星期二

xml_http_request 與 Rails2

在練習 Agile Web Development with Rails 2/e 第 13.4 節的應用程式整合測試時,AJAX 驗證測試程式一直無法成功通過,程式碼如下
xml_http_request "/store/add_to_cart" , :id => ruby_book.id

經過查詢 Rais2 的 API 文件之後,發現原來是 xml_http_request 的參數已經做了變更,詳細的參數與說明如下所示(節錄於 Rails2 API 文件)

xml_http_request(request_method, path, parameters = nil, headers = nil)

Performs an XMLHttpRequest request with the given parameters, mirroring a request from the Prototype library.

The request_method is :get, :post, :put, :delete or :head; the parameters are nil, a hash, or a url-encoded or multipart string; the headers are a hash. Keys are automatically upcased and prefixed with ‘HTTP_’ if not already.
This method is also aliased as xhr

看完文件後可發現 Rails 1.x 與 Rails 2.x 參數的差異為 request_method 這個參數,所以只要將第一個參數補上去,書上的範例即可成功通過測試,程式碼如下
xml_http_request :post, "/store/add_to_cart" , :id => ruby_book.id

沒有留言: