Can the 'Back' browser functionality be invoked from a Rails 'Back' link?
10 Answers
Use
<%= link_to 'Back', :back %>
This is specificied in the RDoc here
This generates some Javascript to navigate backward. I've just tested it, and it works.

- 48,165
- 17
- 52
- 58
-
11It doesn't work 'exactly' like a Back button. All the inputted form data is cleared. – alamodey Mar 07 '09 at 01:04
-
1Yeah, I feel like this only really gets most people half way there. If you truly want emulate the back button, then you also want all the previous params intact. – ohhh Sep 11 '15 at 19:50
-
4This will not use `javascript:history.back()` when `request.referer` is present. – rthbound Oct 27 '15 at 20:43
-
7If you have this back button on two pages it will cause the user to just go back and forth between those two pages. It doesn't actually traverse through the history. – wuliwong Jul 22 '17 at 18:51
-
1This can create a nightmare of a user experience in the case of failed form submissions and cases of similar nature that screw up where the user expects the back button to bring them. – karns Oct 30 '20 at 12:27
In Rails 3 and earlier:
link_to_function "Back", "history.back()"
In Rails 4, this method has been removed. See Andreas's comment.

- 139,698
- 36
- 220
- 238
-
6Be aware that link_to_function is depreciated in Rails 4.1. More info: http://stackoverflow.com/questions/14324919/status-of-rails-link-to-function-deprecation – Andreas Oct 27 '13 at 11:08
-
-
I'm using Rails 4.2.5.1 at the moment. I found that I needed to send the Javascript in the options hash, as `onclick: 'history.back()`, rather than in the URL parameter (which didn't work, at least when I tried it with Firefox). – starfry May 24 '16 at 20:04
This is working in Rails 5.1 along with Turbolinks.
link_to 'Back', 'javascript:history.back()'

- 8,966
- 5
- 34
- 34
In Rails 4.2, I got it to work with this:
<a href="javascript:history.back()">Refine Search</a>
I stole this off of @cpm’s answer, except that link_to("Refine Search", :back)
didn’t do the job I wanted while pasting in the generated code <a href="javascript:history.back()">Refine Search</a>
did it perfectly.

- 1
- 1

- 2,832
- 6
- 39
- 66
-
1Yes, `link_to("Refine Search", :back)` will just save the last action w/o any params and put you back there where as your solution uses js – ohhh Sep 11 '15 at 19:56
-
@ohhh it doesn't work for me either: undefined method `stringify_keys' for :back:Symbol – facundofarias Oct 19 '15 at 14:19
-
@facundofarias, that sounds unrelated, what is the exact syntax of your `link_to`? – ohhh Oct 19 '15 at 14:23
-
-
You can use link_to("Hello", :back)
to generate <a href="javascript:history.back()">Hello</a>
.

- 29,210
- 11
- 96
- 131

- 1,543
- 10
- 16
-
[In most cases, it will use the referrer to go back. If there is no referrer, then it will do `javascript:history.back()`.](https://github.com/rails/rails/blob/v6.1.4/actionview/lib/action_view/helpers/url_helper.rb#L33-L58). Checked the code from Rails 3.0 - 6.1. – notapatch Sep 17 '21 at 10:26
This will work similarly as browser back button try this
<%= link_to 'Back', 'javascript:history.go(-1);' %>

- 307
- 2
- 10
Pay attention to this comment from the user rthbound! As he notes, link_to
with the symbol :back
does not always generate a “real” back event as if the user clicked on their browser’s Back button. It can also be a resubmit of the action that loaded the current view.
The documentation for Rails 4.2.6 says this about link_to
and the :back
symbol:
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).
If you like me do not want the behaviour of link_to "cancel", :back
you could implement a helper method which either links to the records index path or show path. (i.e teams_path
or team_path(@team)
module CancelFormButtonHelper
def cancel_button(record)
index_path = record.class.to_s.pluralize.downcase + "_path"
path = record.persisted? ? record : eval(index_path)
link_to "Cancel", path
end
end
which can then be used as <%= cancel_button @team %>
within a form for example.

- 15,380
- 5
- 29
- 30
You can use js function window.history.back()
= link_to 'Back', onclick: "window.history.back();"

- 1,104
- 12
- 17