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

def back
redirect_to :back
end

then i’d add the following helper to your application_helper

def link_back(text=‘back’)
link_to text, :controller => ‘welcome’, :action => ‘back’
end

and then you can use it in your views as:

<%= link_back ‘go back where you came from’ %>

Solution #2

session[:cache] ||= []
session[:cache] << params

def back
if session[:cache] && last_params = session[:cache].pop
redirect_to last_params
end
end

On 2/7/07, newtorails. [email protected] wrote:

redirect_to :back

and then you can use it in your views as:

def back
if session[:cache] && last_params = session[:cache].pop
redirect_to last_params
end
end

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.
#
# We can return to this location by calling 

#redirect_back_or_default.
def store_location
session[:return_to] = request.request_uri
end

# Redirect to the URI stored by the most recent store_location call 

or
# to the passed default.
def redirect_back_or_default(default)
session[:return_to] ? redirect_to_url(session[:return_to]) :
redirect_to(default)
session[:return_to] = nil
end

You may need to play with this a bit to get it to do what you want.

An after filter that sets the current location in your application
controller should set the last link for you. An after filter is
required so
that the current uri is stored after your finished serving the current
page,
making it the previous uri by the time it’s stored.
eg in you ApplicationController.rb

after_filter store_location

Then you can have a helper method that returns the uri for you

def last_uri
session[:return_to] || url_for( :controller => “my_controller”,
:action =>
“index” )
end

This will look in the session and grab whatever uri is in there, or
provide
you with some default value.

This should give you the functionality that your after if I’ve
understood
you correctly.

In your view
<%= link_to “Go Back”, :url => last_uri %>

I hope that’s not too far off track.

Cheers
Daniel

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…

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

On 2/7/07, newtorails. [email protected] wrote:

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?