Reputation: 1195
The documentation says what needs to be done like this:
url_for(:back)
But the problem is that this example will work only once, and then there will be a two-page loop. That is, it turns out that the :back
does not match the browser history, but only matches the previous route.
But how to implement "Back" in accordance with the history of the browser?
Upvotes: 0
Views: 580
Reputation: 1968
:back
uses the HTTP_REFERER
header which causes exactly the behavior you've described.
If you want to use the history from the browser, you have to use the browser's facilities, because it is then the only reliable source of your history. You can do so by simply using history.back()
. Here's an example how to use it inline:
<%= link_to "Back", "javascript:history.back()" %>
Depending on how you've structured your frontend, which libraries (e.g. jQuery) you're using etc., you might want to put it into a separate JS file and call it from there. Here's an example how you could do this with jQuery:
<%= link_to "Back", "#", class: "back-button" %>
$(document).on("click", ".back-button", function(event) {
event.preventDefault()
history.back()
})
Upvotes: 3