2012-08-03 13 views
9

Qualcuno potrebbe dirmi perché questo metodo PUT non funziona, per favore.Ruby on rails - Metodo PUT in aggiornamento ajax

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

Server dice che ho usato il metodo GET ma nella mia richiesta AJAX ho tipo: "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 

I ho provato ad aggiungere _method: 'put' Ma è sempre lo stesso

$.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); 
      }); 

Server:

"risorsa% 5Bpos_y% 5D = 45 & risorsa% 5Bpos_x% 5D = metodo di 50 & = messo & = 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) 

I' d apprezzo qualsiasi aiuto.

+0

Che browser utilizzate? – Vodun

+0

Intendevi POST, non PUT? .... –

+0

Ho aggiornato la mia risposta. Potresti provarlo :-) –

risposta

25

Rails determina la richiesta di inserimento tramite il parametro _method con il valore 'put'.

Poiché non tutti i browser supportano il metodo put, i cheat di rails nel form_tag. sua messa questa uscita in una forma PUT:

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

Quindi quello che dovete fare è questo:

$.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

Fantastico, '_method:' put '' risolto il mio problema! – gamov

+2

hey che funziona alla grande. Puoi spiegarmi perché stiamo convertendo l'hash in una stringa JSON? 'JSON.stringify' – jmoon90