Search code examples
ruby-on-railsrubyruby-on-rails-3backlink-to

Rails 3, browser back functionality


I am trying to get a back feature working on a Iphone web application.

I have looked at all the other posts about this issue, but none of them address my specific case.

This is my sequence of actions;

home page -> event page (click of a person name) -> person page

Right now, I have the <%= link_to 'Back', :back %> on both the event page and person page. With this kind of implementation, when I click on back from the person page, it takes me to the event page as expected. But when I click on back from the event page, it goes back to the person page because that is the page I came from.(whereas the expected functionality and the functionality from a browser back button would be to take me to the home page)..

Can anybody help me get this functionality in Rails?


Solution

  • In the rails documentation for the link_to method you can read:

    (...) use :back to link to the referrer - a JavaScript back link will be used in place of a referrer if none exists

    And you can see that in the code of url_for method:

    when :back
      controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
    else
    

    In your case, the referrer for the event page is the person page. Like @Zepplock pointed, you probably want to hardcode the javascript:history.back(), this is the same as clicking the Back button.