12

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!

MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
Reddirt
  • 5,913
  • 9
  • 48
  • 126

3 Answers3

28

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"]
MrYoshiji
  • 54,334
  • 13
  • 124
  • 117
  • MrYoshiji, I was wondering if you would be willing to look at this questions for me? I would really appreciate it. http://stackoverflow.com/questions/16279090/rails-passing-information-from-a-view-to-a-form – Reddirt May 01 '13 at 20:12
  • 3
    MrYoshiji - the redirect_to :back works if you delete from any page except the show page. Because, the back tries to take you back to show a record you just deleted. I've posted a new question. – Reddirt May 03 '13 at 16:57
  • @Reddirt did you every find out how to not get `:back` to try and send you to the record that was just removed? – Gcap Jan 16 '16 at 00:59
  • @Gcap You should not `redirect_to :back` after a successfull destroy action, but do a `redirect_to action: :index` instead (for example) – MrYoshiji Jan 17 '16 at 20:04
5

In Rails 5 (Official Docs) you can use the updated: redirect_back(fallback_location: root_path)

Using this redirects the user to the referring page (previous page, the same as redirect_to :back). If this isn't possible, it then redirects to a fallback location specified in the controller.

An example of a method in your controller (this redirects the user to the root path as a fallback location):

def some_method
    #Your Code Here
    redirect_back(fallback_location: root_path)
end

An example of a method for deleting an expense and redirecting back, with a fallback to the root_path:

def destroy
    @expense.destroy
    redirect_back(fallback_location: root_path)
end

Below are some other examples of the fallback_location's that can be used to redirect the browser back to a specific page, if the referring page cannot be redirected to:

redirect_back fallback_location: "http://www.google.com"      #Redirects to Google (or any website you specify)
redirect_back fallback_location:  "/images/screenshot.jpg"    #Redirects to a local image
redirect_back fallback_location:  posts_path                  #Redirects to the 'posts' path
redirect_back fallback_location:  new_user_path               #Redirects to the new user path 
Graham
  • 7,431
  • 18
  • 59
  • 84
Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
1

You can also try this one.

render :nothing => true