2012-08-03 65 views
9

有人能告訴我爲什麼這個PUT方法不起作用請。Ruby on rails - 更新ajax上的PUT方法

$.ajax({ 
     type: "PUT", 
     dataType: "script", 
     url: '/resources/35', 
     data: 
     { 
      resource : { pos_y: 45, pos_x: 50 } 
     } 
    }).done(function(msg) 
      { 
       alert("Data Saved: " + msg); 
      }); 

服務器說,我已經使用了GET方法但在我的Ajax請求我有類型: 「PUT」

Started GET "/resources/35?resource%5Bpos_y%5D=45&resource%5Bpos_x%5D=50&_=1344001820350" for 192.168.1.89 at 2012-08-03 15:50:20 +0200 
Processing by ResourcesController#show as */* 
    Parameters: {"resource"=>{"pos_y"=>"45", "pos_x"=>"50"}, "_"=>"1344001820350", "id"=>"35"} 
    User Load (0.3ms) SELECT "users".* FROM "users" 
    Resource Load (0.1ms) SELECT "resources".* FROM "resources" WHERE "resources"."id" = ? LIMIT 1 [["id", "35"]] 
    Rendered resources/show.html.erb within layouts/login (2.3ms) 
Completed 200 OK in 238ms (Views: 235.9ms | ActiveRecord: 0.4ms) 

resources_controller.rb

# PUT /resources/1                                        
    # PUT /resources/1.json                                       
    def update 
    @resource = Resource.find(params[:id]) 

    respond_to do |format| 
     if @resource.update_attributes(params[:resource]) 
     format.html { redirect_to @resource, notice: 'successfully updated.' } 
     format.js 
     else 
     format.html { render action: "edit" } 
     format.js 
     end 
    end 
    end 

我已嘗試添加_method:'put'但它仍然是一樣的

$.ajax({ 
     type: "PUT", 
     dataType: "script", 
     url: '/resources/35', 
     data: 
     { 
      resource : { pos_y: 45, pos_x: 50 }, 
      _method: 'put' 
     } 
    }).done(function(msg) 
      { 
       alert("Data Saved: " + msg); 
      }); 

服務器:

「資源%5Bpos_y%5D = 45 &資源%5Bpos_x%5D = 50 & 方法=把 & = 1344004390840」

Started GET "/resources/35?resource%5Bpos_y%5D=45&resource%5Bpos_x%5D=50&_method=put&_=1344004390840" for 192.168.1.89 at 2012-08-03 16:33:10 +0200 
Processing by ResourcesController#show as */* 
    Parameters: {"resource"=>{"pos_y"=>"45", "pos_x"=>"50"}, "_"=>"1344004390840", "id"=>"35"} 
    User Load (0.3ms) SELECT "users".* FROM "users" 
    Resource Load (0.1ms) SELECT "resources".* FROM "resources" WHERE "resources"."id" = ? LIMIT 1 [["id", "35"]] 
    Rendered resources/show.html.erb within layouts/login (0.8ms) 
Completed 200 OK in 93ms (Views: 90.5ms | ActiveRecord: 0.4ms) 

我d感謝任何幫助。

+0

你用什麼瀏覽器? – Vodun 2012-08-03 14:05:08

+0

你的意思是POST,而不是PUT? .... – 2012-08-03 14:07:30

+0

我更新了我的答案。你可以嘗試:-) – 2012-08-03 15:15:06

回答

25

Rails通過值爲'put'的參數_method確定放入請求。

由於並非所有瀏覽器都支持put方法,因此rails會在form_tag中作弊。它把這個輸出的PUT形式:

<input type='hidden' name='_method' value='put'> 

所以,你必須做的是這樣的:

$.ajax({ 
    type: "POST", 
    dataType: "script", 
    url: '/resources/35', 
    contentType: 'application/json', 
    data: JSON.stringify({ resource:{pos_y:45,pos_x:50}, _method:'put' }) 
}).done(function(msg) 
     { 
      alert("Data Saved: " + msg); 
     }); 
+0

真棒,'_method:'把''解決了我的問題! – gamov 2015-06-11 04:53:22

+2

嘿,這很好。你能向我解釋爲什麼我們要把哈希轉換成JSON字符串嗎? 'JSON.stringify' – jmoon90 2015-10-08 18:23:30