31

I have a listing page, then a form, then a thank you page. I need to put a link on the thank you page that takes the user back to the page they were on before the form which always varies. I've tried using this:

= link_to "Back", :back

But this only takes them back to the previous page, so the form.

wurde
  • 2,487
  • 2
  • 20
  • 39
user1738017
  • 609
  • 2
  • 12
  • 29
  • You can have a look at this question: http://stackoverflow.com/questions/3304597/rails-redirect-two-pages-back – siekfried Apr 25 '13 at 11:29
  • Does your form have error handling? If so, what happens when you make an error? If not, fair enough. – atw Apr 03 '15 at 11:02

6 Answers6

56

Try this

<%= link_to 'Back', url_for(:back) %>
# if request.env["HTTP_REFERER"] is set to "http://www.example.com"
# => http://www.example.com

here is more details.

panicoper
  • 1,034
  • 1
  • 12
  • 15
  • 3
    This solution does not work after submitting the form. Plus, what is the difference between `url_for(:back)` and `:back`? – atw Apr 03 '15 at 08:43
  • 11
    `<%= link_to 'Back', :back %>` is enough – AlexLeung Apr 04 '16 at 10:09
  • 1
    @atw [link_to second param is the url of the link. If you call link_to with `url_for(:back)` you pass in the URL. If you call link_to with :back it calls `url_for` internally. In other words, they are the same.](https://github.com/rails/rails/blob/v6.1.4/actionview/lib/action_view/helpers/url_helper.rb#L202) – notapatch Sep 17 '21 at 08:49
21

Well, you can set a method in the form page to collect that url. The basic idea is to use a custom session variable to store previous url and keep it to next session.

Suppose your form's action is SomeController#new, then

class SomeController < ApplicationController
  after_action "save_my_previous_url", only: [:new]

  def save_my_previous_url
    # session[:previous_url] is a Rails built-in variable to save last url.
    session[:my_previous_url] = URI(request.referer || '').path
  end

end

Then in the thank you page, you can get this my_previous_url by

 session[:my_previous_url]

This should be able to suit your case, the previous url two pages ago.

Disclaimer: This is not verified. Idea only.

Add

Session belongs to controller. It is not a helper you can use directly in view. You need to define an instance variable in controller and then you can use it in view. Like this

# Controller
@back_url = session[:my_previous_url]
 
# View
<%= link_to "Back", @back_url %>
notapatch
  • 6,569
  • 6
  • 41
  • 45
Billy Chan
  • 24,625
  • 4
  • 52
  • 68
  • It must be great to have a form that does not return any errors!? If, like me, your form does, then the back button will bring you back to ........ the form. – atw Apr 03 '15 at 10:44
  • Thanks @billy-chan your solution helps me. Only one change instead of after_filter, probably would be better use before_filter so in your current action you have the proper value of the previous path – Darkaico Sep 05 '15 at 12:58
  • Hey @Darkaico, thanks for the feedback! This answer was two years ago. From my today's perspective it's more like a hack :) Today I think it's better not to add something like that into codebase. Instead, if that back url is used in once, it might be better to hard code it directly in the thank you page. – Billy Chan Sep 05 '15 at 17:10
  • I got this by doing `session[:previous_url] = request.fullpath` – Mauro Dias May 11 '16 at 14:32
14

You can use the example from Rails API:

<%= link_to "Back", :back %>

Rails API Doc for link_to

Using a :back Symbol instead of an options hash will generate a link to the referrer (a JavaScript back link will be used in place of a referrer if none exists).

ilgam
  • 4,092
  • 1
  • 35
  • 28
1

Since you saying,it might be different page before form, probably request_url can help you. you can save your request_url in a param and redirect to param_url if there is.

here is a source that you can take for reference. http://programming-tut.blogspot.com/2010/06/ruby-on-rails-request-url.html

Nich
  • 1,112
  • 1
  • 14
  • 29
0

If use in Controller, you can direct use like this:

def some_action
  # some code
  redirect_to :back
end
zw963
  • 1,157
  • 15
  • 11
  • 1
    This directs the user back to the form they just submitted. The OP wants to know how to jump back two pages basically getting to the page before the new/edit form. – bkunzi01 Apr 17 '17 at 12:01
0

This works for me:

In controller from previous view:

cookies[:original_referrer] = request.orignal_url

to set a cookie on the browser with the URL of the originating page

In the controller from the current view:

redirect_to cookies[:original_referrer]
Sheldon Hage
  • 414
  • 3
  • 8