Back action in Ruby on Rails

Can I activate the browser back function using the back link?

+42
browser ruby-on-rails
Mar 07 '09 at 0:13
source share
10 answers

In Rails 3 and earlier:

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

In Rails 4, this method is removed. See Commentary by Andreas.

+25
Mar 07 '09 at 0:20
source share

Using

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

This is indicated in the RDoc here.

This creates some Javascript to move backward. I just tested it and it works.

+161
Mar 07 '09 at 0:17
source share

In Rails 4.2, I got this to work with this:

 <a href="javascript:history.back()">Refine Search</a> 

I stole this with @cpms answer , except that link_to("Refine Search", :back) did not complete the task that I wanted while pasting into the generated code <a href="javascript:history.back()">Refine Search</a> did it great.

+8
May 01 '15 at 4:18
source share

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

+6
Mar 07 '09 at 0:27
source share

This works in Rails 5.1 along with Turbolinks.

 link_to 'Back', 'javascript:history.back()' 
+2
Oct 06 '17 at 15:21
source share

This will work similar to the browser button, try this

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

+1
Jul 30 '15 at 7:15
source share

Check out this comment from rthbound ! As he notes, link_to with the :back symbol does not always generate a "real" event back, as if the user had clicked on the browsers' back button. It could also be a retransmission of the action that loaded the current view.

The documentation for Rails 4.2.6 talks about this link_to and the :back symbol:

Using the :back symbol instead of the options hash will result in a link to referrer (JavaScript backlink will be used instead of the referrer if it does not exist).

+1
Mar 18 '16 at 7:28
source share

Using

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

similar to how to click the back button in a browser. All form data entered still exists when you return.

0
Jan 11 '13 at 17:28
source share

If you like it, I donโ€™t need the behavior of link_to "cancel", :back , you can implement a helper method that either refers to the record index path or the path to it. (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 %> in the form, for example.

0
Oct 18 '16 at 14:17
source share

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

  = link_to 'Back', onclick: "window.history.back();" 
0
May 11 '17 at 1:46 pm
source share