newbie alert! Making back links work

Hello all, I am trying to make "Back" link functionality work in my app. Basically the referring page has a list of items and on clicking I display the details on the items. But when the user clicks "Back", the user should go back to the list display. I would also like to preserve the pagination in the referring page display meaning go back to the same page because the user would hate to start all over again and again after going through a couple of pages of items..

Is there a way to do it? I found the following two solutions in the previous forum postings. First one didn't work for me. Second one worked but couldn't preserve the page number of the referrer. So the user goes back to the first page of list display. I am posting both the suggested solutions for your reference. I would greatly appreciate your insights and comments. Solution #1

Various authentication plugins use the session as a place to store the previous request for this purpose. Techno-weenie’s restful_authentication has this functionality built in so that if someone requests a page before they’re logged in they can be re-directed there.

from

http://svn.techno-weenie.net/projects/plugins/restful_authentication/generators/authenticated/templates/authenticated_system.rb

# Store the URI of the current request in the session.

Daniel, thanks for your answer. But correct me if I am wrong- if the previous page is paginated list display and the user is on the 3rd page. Last_uri would still take the user to the first page of the list display. Isnt' it? I want the user to go back to the same page # in the pagination scheme as well..

It’s really difficult for me to give you a concrete answer at the moment, since I don’t have ruby or rails with me at the moment, but request.request_uri, the method used in the store_location method should include the full uri that requested the page. So whatever pagination parameters and included in the page generation should be included, provided the parameters are included in the request_uri.

The way it would seem to work would be ( I can’t test this at the moment sorri)

request 1st page www.example.com/stuff

session[:return_to] #=> nil

render request #=> Last link will be default

set session[:return_to] = www.example.com/stuff

2nd request www.example.com/stuff?page=2

session[:return_to] #=> www.example.com/stuff

render request #=> Last link will be www.example.com/stuff

set session[:return_to] = www.example.com/stuff?page=2

3rd request www.example.com/stuff?page=3

session[:return_to] #=> www.example.com/stuff?page=2

render request Last link will be www.example.com/stuff?page=2

set session[:return_to] = www.example.com/stuff?page=3

This should take you to whatever the previous page is, whether it’s 1,2 or 3. Is this not the way that it is working?

In your list action save off your current URL params as follows:

session[:last_list_url] = url_for(params)

Assuming that you’re using a non-AJAX pagination scheme, otherwise you’ll have to come up with a mechanism to save that state on each AJAX call so that when you return to your list you get the same page you were on.

If it helps I blogged about a sortable table implementation I abstracted out of my current project. You can find it here:

http://javathehutt.blogspot.com/2006/11/rails-realities-part-20-sortable.html

If you’re displaying a has_many collection (e.g. @user.posts) and are on rails 1.1.6 ping me and I’ll give you a patch for AR that will fix a bug in the has_many association class’s count method that gets used by the plugin I created.

Best, Michael

http://javathehutt.blogspot.com