3

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?

zacropetricopus
  • 407
  • 1
  • 5
  • 12
  • 1
    Make sure you have JavaScript enabled. Could you please post the HTML code of your page. It should be something like: javascript:history.back() – Zepplock Feb 25 '11 at 06:13
  • 1
    Thanks Zepplock. When I changed <%= link_to "Back", :back %> to <%= link_to "Back", 'javascript:history.back()' %> things worked fine. I didn't expect it to be this simple. The HTML for <%= link_to "Back", :back %> was not the one with javascript, it was the url itself. I have Javascript enabled in chrome. I'm not sure why this is the case though. – zacropetricopus Feb 25 '11 at 06:37

1 Answers1

11

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.

Filipe Miguel Fonseca
  • 6,400
  • 1
  • 31
  • 26