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