I have the following method in a controller named tareas_controller
def destroy
@tarea = Tarea.find(params[:id])
@tarea.destroy
respond_to do |format|
format.html { redirect_to tareas_url }
format.json { head :ok }
format.js { redirect_to :controller => "clientes", :action =>"show", :id => @tarea.cliente, :format => :js, :method=>:get}
end
end
The record gets deleted ok, after that I get the following code on the server:
Redirected to http://127.0.0.1:3000/clientes/12.js?method=get Completed 302 Found in 174ms
Started DELETE "/clientes/12.js?method=get" for 127.0.0.1 at 2012-05-06 19:20:07 +0200 Processing by ClientesController#destroy as JS Parameters: {"method"=>"get", "id"=>"12"} Cliente Load (0.0ms) SELECT "clientes".* FROM "clientes" WHERE "clientes"."id" = ? LIMIT 1 [["id", "12"]] SQL (2.0ms) DELETE FROM "clientes" WHERE "clientes"."id" = ? [["id", 12]] Completed 406 Not Acceptable in 131ms
It seems to send the request with a DELETE verb to the new controller and I cant find a way to change that to a GET request to the new controller.
Can someone give me advise as to how to resolve this issue?
You should redirect with a status 303.
If you are using XHR requests other than GET or POST and redirecting after the request then some browsers will follow the redirect using the original request method. This may lead to undesirable behavior such as a double DELETE. To work around this you can return a
303 See Other
status code which will be followed using a GET request.redirect_to posts_url, status: :see_other redirect_to action: 'index', status: 303
Source: http://api.rubyonrails.org/classes/ActionController/Redirecting.html#method-i-redirect_to
Solved though not in a nice way..
created the following route:
match 'mostrar_cliente/:id' => 'clientes#show', :via => :delete
then I rewrote the redirect as:
redirect_to "/mostrar_cliente/#{@tarea.cliente}", :format => :js
not a very clean solution, but working as intended :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With