Can the 'Back' browser functionality be invoked from a Rails 'Back' link?
Can the 'Back' browser functionality be invoked from a Rails 'Back' link?
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.
In Rails 3 and earlier:
link_to_function "Back", "history.back()"
In Rails 4, this method has been removed. See Andreas's comment.
This is working in Rails 5.1 along with Turbolinks.
link_to 'Back', 'javascript:history.back()'
You can use link_to("Hello", :back) to generate <a href="javascript:history.back()">Hello</a>.
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.