# Session\[:return\_to\] for going back

**URL:** <https://www.ruby-forum.com/t/session-return-to-for-going-back/94657>\
**Category:** Rails\
**Created:** [May 10, 2006, 9:20am UTC](https://www.ruby-forum.com/t/session-return-to-for-going-back/94657 "2006-05-10T09:20:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 10, 2006, 9:20am UTC](https://www.ruby-forum.com/t/session-return-to-for-going-back/94657/1 "2006-05-10T09:20:43Z")

</div>

Hi,

Is session[:return\_to] something Rails maintains to quickly go back to  
the  
previous uri without having to keep track of the action name? Or, do we  
have to actually set it in our actions?

Thanks.

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 10, 2006, 12:25pm UTC](https://www.ruby-forum.com/t/session-return-to-for-going-back/94657/2 "2006-05-10T12:25:37Z")

</div>

I am not sure if this is applicable for you but I use the following exta  
‘link’ functions:

#links back to page where it came with back\_name  
def link\_back(back\_name, html\_options = nil,  
\*parameters\_for\_method\_reference)  
request.referer or return ‘’  
if html\_options  
html\_options = html\_options.stringify\_keys  
convert\_options\_to\_javascript!(html\_options)  
tag\_options = tag\_options(html\_options)  
else  
tag\_options = nil  
end  
“\<a href=”#{request.referer}"#{tag\_options}\>#{back\_name ||  
request.referer}"  
end

#links back to page where it came with back\_name; otherwise falls back  
to link\_to  
def link\_back\_or\_to(back\_name, name, options = {}, html\_options = nil,  
\*parameters\_for\_method\_reference)  
ret = link\_back(back\_name, html\_options,  
parameters\_for\_method\_reference)  
return ret if !ret.empty?  
link\_to(name, options, html\_options,  
parameters\_for\_method\_reference)  
end

for redirection you can use redirect\_to :back

Not sure if this is what you are after.

Cheers,  
Anton

---

<div class="post-metadata">

**Author:** ![samonderous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3da27b/32.png) [@samonderous](https://www.ruby-forum.com/u/samonderous)\
**Post date:** [May 10, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/session-return-to-for-going-back/94657/3 "2006-05-10T18:24:33Z")

</div>

On May 10, 2006, at 12:17 AM, Sam D. wrote:

> Is session[:return\_to] something Rails maintains to quickly go back  
> to the previous uri without having to keep track of the action  
> name? Or, do we have to actually set it in our actions?

(Assuming you’re referring to the earlier url\_back code sample.)

You must set it yourself. Rails can’t tell which page you wish to  
return to or when such a setting is appropriate.

I like embedding return\_to in form parameters because the user can  
work with multiple tabs/windows without having one tab return to the  
other’s :return\_to.

Best,  
jeremy
