# Redirect back to last page?

**URL:** <https://www.ruby-forum.com/t/redirect-back-to-last-page/102182>\
**Category:** Rails\
**Created:** [August 12, 2006, 6:02am UTC](https://www.ruby-forum.com/t/redirect-back-to-last-page/102182 "2006-08-12T06:02:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [August 12, 2006, 6:02am UTC](https://www.ruby-forum.com/t/redirect-back-to-last-page/102182/1 "2006-08-12T06:02:14Z")

</div>

I have a few pages where a user may do something (add tags, login, etc)  
and I would like to redirect them back to the last page they were at  
before calling that action. Is there an easy way to do this?

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [August 12, 2006, 6:22am UTC](https://www.ruby-forum.com/t/redirect-back-to-last-page/102182/2 "2006-08-12T06:22:51Z")

</div>

Vince W. wrote:

> I have a few pages where a user may do something (add tags, login, etc)  
> and I would like to redirect them back to the last page they were at  
> before calling that action. Is there an easy way to do this?

I have used this since last year, but I am thinking they put something  
into core that handles this now and I haven’t changed anything. I could  
be wrong though…

application.rb:

```
def redirect_back(default)
	if @session['prevpage'].nil?
		if default
			redirect_to default
		else
		 	redirect_to :controller => "", :action => ""
		end
	else
		if @session['prevpage'].length > 4
			redirect_to_url @session['prevpage']
		else
			redirect_to default
		end
	end
end

```

within your controller:

```
redirect_back("action => 'list'")

```

If memory serves me correctly, I added the .length \> 4 but can’t for the  
life of me remember why! (that’s why you are supposed to comment your  
code)

Anyway, hope this helps but don’t be surprised if someone gives you  
something better.

Regards,

Michael

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [August 15, 2006, 2:17pm UTC](https://www.ruby-forum.com/t/redirect-back-to-last-page/102182/3 "2006-08-15T14:17:56Z")

</div>

On Aug 12, 2006, at 6:02, Vince W. wrote:

> I have a few pages where a user may do something (add tags, login,  
> etc)  
> and I would like to redirect them back to the last page they were at  
> before calling that action. Is there an easy way to do this?

redirect\_to :back

… as specified in the API reference for redirect\_to, who’d have  
guessed? 😉

–  
Jakob S. - [http://mentalized.net](http://mentalized.net)

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/redirect-back-to-last-page/102182/4 "2006-08-15T14:18:08Z")

</div>

Do you happen to know why the “Rails Recipes” book gave the following  
code to redirect back to the page? (This is in the “authenticating  
users” section, and when we are redirected to, for example, /home/login  
from /home)

session[:intended\_action] = action\_name  
session[:intended\_controller] = controller\_name

Then after signing in:

redirect\_to :action =\> session[:intended\_action], :controller =\>  
session[:intended\_controller]

Wouldn’t redirect\_to :back do the same thing?

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/redirect-back-to-last-page/102182/5 "2006-08-15T14:18:16Z")

</div>

Rob G. wrote:

> redirect\_to :action =\> session[:intended\_action], :controller =\>  
> session[:intended\_controller]
> 
> Wouldn’t redirect\_to :back do the same thing?

In some cases, sure. But consider this usage pattern:

1. Request a page, store it’s URL in session
2. Redirect to Login page
3. Post Login information
4. Request Forgot password page
5. Process Forgot password procedure, which might include multiple  
requests
6. Request Login page
7. Post Login information, then redirect to the stored URL

In this case, redirect\_to :back in step 7 would take the user to the  
login page, or in best case one of the steps in the forgot password  
procedure. Whereas redirecting to the URL stored in session would take  
the user to where he “left” the normal flow.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/redirect-back-to-last-page/102182/6 "2006-08-15T14:18:54Z")

</div>

I believe ‘redirect\_to :back’ isn’t as flexible. What if you send a  
user to  
the login page, but he has to then go and create a new account? I’m  
also  
not sure how it would handle the situation in that wrong username /  
passwords are entered, and the user is sent to a different error page.

If you take the extra steps to store the intended location, you can  
always  
make sure you get back to their.

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [August 15, 2006, 2:18pm UTC](https://www.ruby-forum.com/t/redirect-back-to-last-page/102182/7 "2006-08-15T14:18:01Z")

</div>

“Vince W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> I have a few pages where a user may do something (add tags, login, etc)  
> and I would like to redirect them back to the last page they were at  
> before calling that action. Is there an easy way to do this?

store the url in session variable(session[:back\_to]) . Looking at code  
of  
authentication engines/plugins will give you an idea of how this is  
done.

## Cheers,

Surendra S.  
[http://ssinghi.kreeti.com](http://ssinghi.kreeti.com), [http://www.kreeti.com](http://www.kreeti.com)  
Read my blog at: [http://cuttingtheredtape.blogspot.com/](http://cuttingtheredtape.blogspot.com/)  
,----  
| “All animals are equal, but some animals are more equal than others.”  
| – Orwell, Animal Farm, 1945  
`----

---

<div class="post-metadata">

**Author:** ![vince](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vince](https://www.ruby-forum.com/u/vince)\
**Post date:** [August 15, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/redirect-back-to-last-page/102182/8 "2006-08-15T14:19:39Z")

</div>

2006/8/12, Rob G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> session[:intended\_action] = action\_name  
> session[:intended\_controller] = controller\_name
> 
> Then after signing in:
> 
> redirect\_to :action =\> session[:intended\_action], :controller =\>  
> session[:intended\_controller]
> 
> Wouldn’t redirect\_to :back do the same thing?

No. redirect\_to :back uses the referer header provided by the browser,  
so  
its just 1 step back. the session thing you mentioned goes back a  
minimum of  
2:

1. request private page
2. redirect to login
3. post login
4. go to 1

–  
Michael S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

[www.stellar-legends.de](http://www.stellar-legends.de) - Weltraum-Browsergame im Alpha-Stadium
