# Newbie alert! Making back links work

**URL:** <https://www.ruby-forum.com/t/newbie-alert-making-back-links-work/84690>\
**Category:** Rails\
**Created:** [February 7, 2007, 1:16am UTC](https://www.ruby-forum.com/t/newbie-alert-making-back-links-work/84690 "2007-02-07T01:16:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![newtorails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@newtorails](https://www.ruby-forum.com/u/newtorails)\
**Post date:** [February 7, 2007, 1:16am UTC](https://www.ruby-forum.com/t/newbie-alert-making-back-links-work/84690/1 "2007-02-07T01:16:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![newtorails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@newtorails](https://www.ruby-forum.com/u/newtorails)\
**Post date:** [February 7, 2007, 1:31am UTC](https://www.ruby-forum.com/t/newbie-alert-making-back-links-work/84690/2 "2007-02-07T01:31:34Z")

</div>

On 2/7/07, newtorails. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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

---

<div class="post-metadata">

**Author:** ![newtorails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@newtorails](https://www.ruby-forum.com/u/newtorails)\
**Post date:** [February 7, 2007, 1:46am UTC](https://www.ruby-forum.com/t/newbie-alert-making-back-links-work/84690/3 "2007-02-07T01:46:35Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![newtorails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@newtorails](https://www.ruby-forum.com/u/newtorails)\
**Post date:** [February 7, 2007, 2:01am UTC](https://www.ruby-forum.com/t/newbie-alert-making-back-links-work/84690/4 "2007-02-07T02:01:36Z")

</div>

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](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](http://javathehutt.blogspot.com)

---

<div class="post-metadata">

**Author:** ![newtorails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/f4b2a3/32.png) [@newtorails](https://www.ruby-forum.com/u/newtorails)\
**Post date:** [February 7, 2007, 1:59am UTC](https://www.ruby-forum.com/t/newbie-alert-making-back-links-work/84690/5 "2007-02-07T01:59:36Z")

</div>

On 2/7/07, newtorails. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://www.example.com/stuff)  
session[:return\_to] #=\> nil  
render request #=\> Last link will be default  
set session[:return\_to] = [www.example.com/stuff](http://www.example.com/stuff)

2nd request [www.example.com/stuff?page=2](http://www.example.com/stuff?page=2)  
session[:return\_to] #=\> [www.example.com/stuff](http://www.example.com/stuff)  
render request #=\> Last link will be [www.example.com/stuff](http://www.example.com/stuff)  
set session[:return\_to] = [www.example.com/stuff?page=2](http://www.example.com/stuff?page=2)

3rd request [www.example.com/stuff?page=3](http://www.example.com/stuff?page=3)  
session[:return\_to] #=\> [www.example.com/stuff?page=2](http://www.example.com/stuff?page=2)  
render request Last link will be [www.example.com/stuff?page=2](http://www.example.com/stuff?page=2)  
set session[:return\_to] = [www.example.com/stuff?page=3](http://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?
