Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails redirect to previous page (back) after delete

Is there a way to include a redirect in a link_to? I want to just refresh the current page after a delete. But, you can delete the same record from multiple views in the app.

This is the link_to:

<%= link_to 'Delete', expense, confirm: 'Are you sure?', method: :delete, :class => 'btn btn-mini btn-danger' %>

If not, does it make sense to save the current_url in flash[:fromurl] then put that in the Controller destroy section like this:

   respond_to do |format|
     format.html { redirect_to flash[:fromurl] }
     format.json { head :no_content }
   end

Thanks for the help!

like image 394
Reddirt Avatar asked May 01 '13 17:05

Reddirt


Video Answer


1 Answers

You can use the redirect_to :back :

respond_to do |format|
  format.html { redirect_to :back }
  format.json { head :no_content }
end

It uses the header "HTTP_REFERER" from the request:

redirect_to :back
# is a shorthand for:
redirect_to request.env["HTTP_REFERER"]
like image 118
MrYoshiji Avatar answered Sep 27 '22 23:09

MrYoshiji