# Help redirecting to the same page

**URL:** <https://www.ruby-forum.com/t/help-redirecting-to-the-same-page/160888>\
**Category:** Rails\
**Created:** [February 20, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/help-redirecting-to-the-same-page/160888 "2009-02-20T17:26:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [February 20, 2009, 5:26pm UTC](https://www.ruby-forum.com/t/help-redirecting-to-the-same-page/160888/1 "2009-02-20T17:26:09Z")

</div>

I have a login controller and a link so I can login my app.  
I would like to have something to come back to the page where I clicked  
that link. So, when I click the link I go to the login page and after I  
would like to return to the page where I clicked the link.  
I have uses request\_uri in other apps, but I need something different  
now and be able to use it all around the web.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [February 20, 2009, 7:44pm UTC](https://www.ruby-forum.com/t/help-redirecting-to-the-same-page/160888/2 "2009-02-20T19:44:15Z")

</div>

Hi John,

On Fri, 2009-02-20 at 17:26 +0100, John S. wrote:

> I have a login controller and a link so I can login my app.  
> I would like to have something to come back to the page where I clicked  
> that link. So, when I click the link I go to the login page and after I  
> would like to return to the page where I clicked the link.  
> I have uses request\_uri in other apps, but I need something different  
> now and be able to use it all around the web.

Depending on the complexity of your authentication processing, you may  
be able to do something as simple as:

redirect\_to :back

I’m not sure what you mean by ‘need something different now’ wrt  
request\_uri. An alternative to the above would be to capture the source  
of the request in your login controller method and store it in the  
session for future use.

There are other alternatives, of course. Maybe if you say more about  
what you mean by ‘be able to use it all around the web’ you’ll get some  
more helpful responses.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [February 20, 2009, 11:21pm UTC](https://www.ruby-forum.com/t/help-redirecting-to-the-same-page/160888/3 "2009-02-20T23:21:35Z")

</div>

On Feb 20, 2009, at 1:43 PM, bill walton wrote:

> > now and be able to use it all around the web.  
> > session for future use.
> 
> There are other alternatives, of course. Maybe if you say more about  
> what you mean by ‘be able to use it all around the web’ you’ll get  
> some  
> more helpful responses.
> 
> Best regards,  
> Bill

Note that if you do use :back, you need to rescue RedirectBackError in  
case there is no HTTP\_REFERER to go back to. It might be simpler to  
look at what redirect\_to does with :back and do that yourself  
(exercise for the reader 😉

begin  
redirect\_to :back  
rescue RedirectBackError  
redirect\_to home\_url # or something appropriate for your app.  
end

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [February 24, 2009, 11:05pm UTC](https://www.ruby-forum.com/t/help-redirecting-to-the-same-page/160888/4 "2009-02-24T23:05:45Z")

</div>

You’ll probably want to adopt an approach similar to restful-  
authetication’s method:

def store\_location  
session[:return\_to] = request.request\_uri  
end

(call this from your action that shows users the login page - see

> <https://github.com/technoweenie/restful-authentication/blob/5799593a28bb88e98b8cfe32d2f207c753a2bd9b/generators/authenticated/templates/authenticated_system.rb>

for details)

–Matt J.

On Feb 24, 3:08 am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [February 24, 2009, 9:08am UTC](https://www.ruby-forum.com/t/help-redirecting-to-the-same-page/160888/5 "2009-02-24T09:08:51Z")

</div>

Thanks, but I can not do redirect\_to :back, because it redirects to the  
login page. I’ll need something different. One possibility is to use a  
session variables for my controller and method, but the problem is that  
sometimes I need also and id, like ‘posts/11’. I would like something  
more general. I would like to be able to save all the url in a session  
variable and then be able to redirect back to it. Is it possible?
