Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redirect_to from "destroy" to "index"

Problem appears when I want to remove element from paginated table in "ajaxized" way. My tasks controller calls its destroy method in response to [DELETE] /tasks/1234, but at the end I want to redirect to index to get the list automatically refreshed.

Unfortunately, what redirect_to tasks_url at this point does is [DELETE] /tasks request.

Is there any way to force GET request instead of DELETE while redirecting from inside of destroy ?

like image 381
Michal Avatar asked Apr 12 '12 10:04

Michal


1 Answers

Use redirect_to with status 303

def destroy
  @task = Task.find(params[:id])
  @task.destroy
  redirect_to :action => :index, status: 303     
end

redirect_to documentation says:

http://api.rubyonrails.org/classes/ActionController/Redirecting.html

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.

like image 153
vitor.caetano Avatar answered Sep 28 '22 08:09

vitor.caetano