Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

a Redirect_to from Destroy action always gets DELETE verb whatever :method I declare

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?

like image 987
Perseoh Avatar asked May 06 '12 17:05

Perseoh


2 Answers

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

like image 100
sequielo Avatar answered Nov 14 '22 12:11

sequielo


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 :)

like image 42
Perseoh Avatar answered Nov 14 '22 14:11

Perseoh