# Redirect to where I was

**URL:** <https://www.ruby-forum.com/t/redirect-to-where-i-was/51884>\
**Category:** Rails\
**Created:** [January 8, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/redirect-to-where-i-was/51884 "2006-01-08T19:07:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [January 8, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/redirect-to-where-i-was/51884/1 "2006-01-08T19:07:43Z")

</div>

I wonder if there is a standard Rails way to redirect from an action  
back to a previously viewed page. As a quick fix I created two little  
methods in the controllers/application.rb. Does this seem like an ok  
way to go?

Thanks,  
Peter

class ApplicationController \< ActionController::Base

def i\_was\_here  
session[:i\_was\_here] = request.env[‘REQUEST\_URI’]  
end

def redirect\_to\_where\_i\_was  
redirect\_to session[:i\_was\_here]  
end

end

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [January 8, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/redirect-to-where-i-was/51884/2 "2006-01-08T19:52:05Z")

</div>

Peter M. wrote:

> I wonder if there is a standard Rails way to redirect from an action  
> back to a previously viewed page. As a quick fix I created two little  
> methods in the controllers/application.rb. Does this seem like an ok  
> way to go?
> 
> Thanks,  
> Peter
> 
> class ApplicationController \< ActionController::Base
> 
> def i\_was\_here  
> session[:i\_was\_here] = request.env[‘REQUEST\_URI’]  
> end
> 
> def redirect\_to\_where\_i\_was  
> redirect\_to session[:i\_was\_here]  
> end
> 
> end

One of the login systems used a similar approach. You could even add a  
before\_filter on certain actions to run the ‘i\_was\_here’ method  
automagically. You do run the danger of having a  
redirect\_to\_where\_i\_was go back to itself, thus creating an infinite  
loop. You might want to do a little checking to make sure the current  
location and the target location aren’t the same.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [January 8, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/redirect-to-where-i-was/51884/3 "2006-01-08T21:56:51Z")

</div>

There’s also:

redirect\_to :back

but bear in mind that it will only work if you got to the current page  
by actually clicking a link (i.e. where your browser gave some referer  
information)

- james

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [January 9, 2006, 12:15am UTC](https://www.ruby-forum.com/t/redirect-to-where-i-was/51884/4 "2006-01-09T00:15:45Z")

</div>

Why use session?

class ApplicationController \< ActionController::Base  
def i\_was\_here  
request.env[‘REQUEST\_URI’]  
end

```
 def redirect_to_where_i_was
   redirect_to i_was_here
 end

```

end

But…what’s this about???

redirect\_to :back

–  
– Tom M.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [January 9, 2006, 3:12am UTC](https://www.ruby-forum.com/t/redirect-to-where-i-was/51884/5 "2006-01-09T03:12:48Z")

</div>

Tom M. wrote:

> Why use session?
> 
> class ApplicationController \< ActionController::Base  
> def i\_was\_here  
> request.env[‘REQUEST\_URI’]  
> end
> 
> ```
> def redirect_to_where_i_was
> redirect_to i_was_here
> end
> 
> ```
> 
> end
> 
> But…what’s this about???
> 
> redirect\_to :back
> 
> –  
> – Tom M.

If you use a session, the place you go back to can be 3 or 4 pages  
earlier and/or set programmatically.
