74

Can the 'Back' browser functionality be invoked from a Rails 'Back' link?

alamodey
  • 14,320
  • 24
  • 86
  • 112

10 Answers10

216

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.

Tilendor
  • 48,165
  • 17
  • 52
  • 58
  • 11
    It doesn't work 'exactly' like a Back button. All the inputted form data is cleared. – alamodey Mar 07 '09 at 01:04
  • 1
    Yeah, 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
  • 4
    This will not use `javascript:history.back()` when `request.referer` is present. – rthbound Oct 27 '15 at 20:43
  • 7
    If 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
  • 1
    This 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
27

In Rails 3 and earlier:

link_to_function "Back", "history.back()"

In Rails 4, this method has been removed. See Andreas's comment.

Sophie Alpert
  • 139,698
  • 36
  • 220
  • 238
  • 6
    Be 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
  • enjoy a `undefined method 'link_to_function' for #<# – Jay Killeen May 01 '15 at 04:09
  • 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
21

This is working in Rails 5.1 along with Turbolinks.

link_to 'Back', 'javascript:history.back()'
HarlemSquirrel
  • 8,966
  • 5
  • 34
  • 34
12

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.

Community
  • 1
  • 1
Jay Killeen
  • 2,832
  • 6
  • 39
  • 66
11

You can use link_to("Hello", :back) to generate <a href="javascript:history.back()">Hello</a>.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
cpm
  • 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
3

This will work similarly as browser back button try this

<%= link_to 'Back', 'javascript:history.go(-1);' %>

Pradeep Agrawal
  • 307
  • 2
  • 10
2

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

Community
  • 1
  • 1
philler82
  • 59
  • 7
0

Rails <= 4.0

Using

link_to_function "Back", "history.back()"

seems to be exactly like hitting the back button in the browser. All inputted form data is still there when you get back.

notapatch
  • 6,569
  • 6
  • 41
  • 45
Mark A
  • 1
0

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.

Dan Andreasson
  • 15,380
  • 5
  • 29
  • 30
0

You can use js function window.history.back()

 = link_to 'Back', onclick: "window.history.back();"
Vlad Hilko
  • 1,104
  • 12
  • 17