# Redirect back to where I was before action?

**URL:** <https://www.ruby-forum.com/t/redirect-back-to-where-i-was-before-action/80459>\
**Category:** Rails\
**Created:** [December 27, 2006, 5:02am UTC](https://www.ruby-forum.com/t/redirect-back-to-where-i-was-before-action/80459 "2006-12-27T05:02:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 27, 2006, 5:02am UTC](https://www.ruby-forum.com/t/redirect-back-to-where-i-was-before-action/80459/1 "2006-12-27T05:02:21Z")

</div>

For some reason I cannot find the documentation for this. How can I  
store the current URI/URL so that I can return with a redirect\_to?

session[last\_action] = ???

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 27, 2006, 5:09am UTC](https://www.ruby-forum.com/t/redirect-back-to-where-i-was-before-action/80459/2 "2006-12-27T05:09:22Z")

</div>

Pulling some code from Rick O.'s acts\_as\_authenticated, roll  
something akin to this following code. This requires that you’re using  
sessions.

def redirect\_back\_or\_default(default)  
session[:return\_to] ? redirect\_to\_url(session[:return\_to]) :  
redirect\_to(default)  
session[:return\_to] = nil  
end

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 27, 2006, 5:11am UTC](https://www.ruby-forum.com/t/redirect-back-to-where-i-was-before-action/80459/3 "2006-12-27T05:11:24Z")

</div>

Note, you’d want to put this in a file that’s accessible to your  
controller or view. So application.rb or a global helper or in a lib  
file called on load would be two good bets.

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 27, 2006, 5:15am UTC](https://www.ruby-forum.com/t/redirect-back-to-where-i-was-before-action/80459/4 "2006-12-27T05:15:25Z")

</div>

Taylor S. wrote:

> For some reason I cannot find the documentation for this. How can I  
> store the current URI/URL so that I can return with a redirect\_to?
> 
> session[last\_action] = ???

You might mean session[:last\_action]= perhaps.

If you want to go back to the previous page, you can use redirect\_to  
:back.  
I find it useful when POST validation fails and need to redisplay the  
form.

Long  
[http://edgesoft.ca/blog/read/2](http://edgesoft.ca/blog/read/2)

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 27, 2006, 5:21am UTC](https://www.ruby-forum.com/t/redirect-back-to-where-i-was-before-action/80459/5 "2006-12-27T05:21:18Z")

</div>

I cannot use redirect\_to :back because I need to refresh the page. In  
either case presented, what is the rails method to get the current  
action so that I can store it in the session?

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [December 27, 2006, 6:11am UTC](https://www.ruby-forum.com/t/redirect-back-to-where-i-was-before-action/80459/6 "2006-12-27T06:11:36Z")

</div>

Hmmm, I have used :back with good success so far. My form gets  
refreshed. I wonder if this is a browser setting thing.

Anyway, try action\_name within the action.

Long
