# Going back to where you came from

**URL:** <https://www.ruby-forum.com/t/going-back-to-where-you-came-from/49196>\
**Category:** Rails\
**Created:** [December 1, 2005, 7:37am UTC](https://www.ruby-forum.com/t/going-back-to-where-you-came-from/49196 "2005-12-01T07:37:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kitesurfer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kitesurfer](https://www.ruby-forum.com/u/kitesurfer)\
**Post date:** [December 1, 2005, 7:37am UTC](https://www.ruby-forum.com/t/going-back-to-where-you-came-from/49196/1 "2005-12-01T07:37:34Z")

</div>

Hi,

I have a page (A) that contains a table with links in one column.  
When the user hits a link it goes to another page (B) which is a read  
only form page with a done button. This page (B) is being reused in  
various parts of the app read pages © and (D). Thus when the user  
clicks ‘Done’ it needs to return back to where she comes from ie either  
(A), © or (D). I thought with the new Rails 0.14.3 I could just do  
redirect\_to :back and all my ills would be cured.  
But… this redirects me back to where I currently am ie (B).

I am sure this is a very common problem out there. In my java life I  
would create something in the session and consume it when this exact  
situation arises.

Is there a magic rails spell that I could use for this situation ? I  
am not sure how to get the URL of page (A) so I can stach it in the  
session for later retrieval or somehow push both the controller and  
action of the page where the request was originated (??)

Sample code please…

Thank you all !!

---

<div class="post-metadata">

**Author:** ![kitesurfer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kitesurfer](https://www.ruby-forum.com/u/kitesurfer)\
**Post date:** [December 1, 2005, 9:22am UTC](https://www.ruby-forum.com/t/going-back-to-where-you-came-from/49196/2 "2005-12-01T09:22:20Z")

</div>

On 1.12.2005, at 8.37, KiteSurfer KiteSurfer wrote:

> But… this redirects me back to where I currently am ie (B).  
> action of the page where the request was originated (??)
> 
> Sample code please…

def authenticate  
unless session[:user\_id]  
session[:return\_to] = @request.request\_uri  
redirect\_to :controller =\> “login”  
return false  
end  
end

# login\_controller.rb

def login  
if @user = User.authenticate(params[:name], params[:password])  
session[:user\_id] = @user.id  
if session[:return\_to]  
redirect\_to(session[:return\_to])  
session[:return\_to] = nil  
end  
end  
end

(lots of lines omitted warning)

//jarkko

---

<div class="post-metadata">

**Author:** ![kitesurfer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kitesurfer](https://www.ruby-forum.com/u/kitesurfer)\
**Post date:** [December 1, 2005, 3:29pm UTC](https://www.ruby-forum.com/t/going-back-to-where-you-came-from/49196/3 "2005-12-01T15:29:39Z")

</div>

Good stuff. Thank you both !!

---

<div class="post-metadata">

**Author:** ![kitesurfer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kitesurfer](https://www.ruby-forum.com/u/kitesurfer)\
**Post date:** [December 1, 2005, 9:38am UTC](https://www.ruby-forum.com/t/going-back-to-where-you-came-from/49196/4 "2005-12-01T09:38:29Z")

</div>

here is what we did - i think you can easily adapt it to fort your  
particular issue

[http://albert.bagasie.com/RailsTips/JumpBack](http://albert.bagasie.com/RailsTips/JumpBack)

---

<div class="post-metadata">

**Author:** ![kitesurfer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/e480ec/32.png) [@kitesurfer](https://www.ruby-forum.com/u/kitesurfer)\
**Post date:** [December 2, 2005, 2:38am UTC](https://www.ruby-forum.com/t/going-back-to-where-you-came-from/49196/5 "2005-12-02T02:38:25Z")

</div>

On Dec 1, 2005, at 12:18 AM, Jarkko L. wrote:

> > either  
> > situation ? I  
> > am not sure how to get the URL of page (A) so I can stach it in the  
> > session for later retrieval or somehow push both the controller and  
> > action of the page where the request was originated (??)
> > 
> > Sample code please…

def store\_location  
@session[‘return-to’] = @request.request\_uri  
end

# move to the last store\_location call or to the passed default one

def redirect\_back\_or\_default(default)  
if @session[‘return-to’].nil?  
redirect\_to default  
else  
redirect\_to\_url @session[‘return-to’]  
@session[‘return-to’] = nil  
end  
end

Then you can put a call to store location before you redirect to a  
new page. Then once they finish with that page you call  
redirect\_back\_or\_default to send them to the location stored in the  
session.

Cheers-

-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732
