# Best Practice for Returning to Calling Page?

**URL:** <https://www.ruby-forum.com/t/best-practice-for-returning-to-calling-page/58156>\
**Category:** Rails\
**Created:** [April 19, 2006, 12:24am UTC](https://www.ruby-forum.com/t/best-practice-for-returning-to-calling-page/58156 "2006-04-19T00:24:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [April 19, 2006, 12:24am UTC](https://www.ruby-forum.com/t/best-practice-for-returning-to-calling-page/58156/1 "2006-04-19T00:24:34Z")

</div>

I have a partial, it’s a shopping cart display, it’s designed to appear  
on several different pages of my site.

One of the buttons on the shopping cart empties the cart, and then I  
want the calling page to redisplay.

In other words, the cart controller needs to return the visitor to the  
same page – however since the cart could appear on any given page, this  
has to be dynamic, not hard-coded.

So the solution I’m considering is to use params[:controller],  
params[:action], and params[:id] as a unique identifier of the calling  
page. I’m assuming this will work but I want to be sensitized to any  
‘gotchas’ before I code this.

If anyone can suggest a better practice I’d appreciate it, thanks.

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [April 19, 2006, 12:43am UTC](https://www.ruby-forum.com/t/best-practice-for-returning-to-calling-page/58156/2 "2006-04-19T00:43:54Z")

</div>

@request.request\_uri

The above will hold the uri of the calling page.

charlie bowman  
[www.recentrambles.com](http://www.recentrambles.com)

Gaudi Mi wrote:

> I have a partial, it’s a shopping cart display, it’s designed to appear  
> on several different pages of my site.
> 
> One of the buttons on the shopping cart empties the cart, and then I  
> want the calling page to redisplay.
> 
> In other words, the cart controller needs to return the visitor to the  
> same page – however since the cart could appear on any given page, this  
> has to be dynamic, not hard-coded.
> 
> So the solution I’m considering is to use params[:controller],  
> params[:action], and params[:id] as a unique identifier of the calling  
> page. I’m assuming this will work but I want to be sensitized to any  
> ‘gotchas’ before I code this.
> 
> If anyone can suggest a better practice I’d appreciate it, thanks.

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [April 19, 2006, 1:12am UTC](https://www.ruby-forum.com/t/best-practice-for-returning-to-calling-page/58156/3 "2006-04-19T01:12:49Z")

</div>

charlie bowman wrote:

> @request.request\_uri
> 
> The above will hold the uri of the calling page.
> 
> charlie bowman  
> [www.recentrambles.com](http://www.recentrambles.com)
> 
> Gaudi Mi wrote:
> 
> > I have a partial, it’s a shopping cart display, it’s designed to appear  
> > on several different pages of my site.
> > 
> > One of the buttons on the shopping cart empties the cart, and then I  
> > want the calling page to redisplay.
> > 
> > In other words, the cart controller needs to return the visitor to the  
> > same page – however since the cart could appear on any given page, this  
> > has to be dynamic, not hard-coded.
> > 
> > So the solution I’m considering is to use params[:controller],  
> > params[:action], and params[:id] as a unique identifier of the calling  
> > page. I’m assuming this will work but I want to be sensitized to any  
> > ‘gotchas’ before I code this.
> > 
> > If anyone can suggest a better practice I’d appreciate it, thanks.

url\_for :overwrite\_params =\> {}

it’s intended to return the current uri with only specific parts  
changed, the action or id or what ever… but don’t pass any changes 😉

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [April 20, 2006, 12:04am UTC](https://www.ruby-forum.com/t/best-practice-for-returning-to-calling-page/58156/4 "2006-04-20T00:04:09Z")

</div>

For those that are interested, I found that the redirect\_to method can  
be used like this:

redirect\_to :back

which will take one back to the requesting page.

Michael G. wrote:

> charlie bowman wrote:
> 
> > @request.request\_uri
> > 
> > The above will hold the uri of the calling page.
> > 
> > charlie bowman  
> > [www.recentrambles.com](http://www.recentrambles.com)
> > 
> > Gaudi Mi wrote:
> > 
> > > I have a partial, it’s a shopping cart display, it’s designed to appear  
> > > on several different pages of my site.
> > > 
> > > One of the buttons on the shopping cart empties the cart, and then I  
> > > want the calling page to redisplay.
> > > 
> > > In other words, the cart controller needs to return the visitor to the  
> > > same page – however since the cart could appear on any given page, this  
> > > has to be dynamic, not hard-coded.
> > > 
> > > So the solution I’m considering is to use params[:controller],  
> > > params[:action], and params[:id] as a unique identifier of the calling  
> > > page. I’m assuming this will work but I want to be sensitized to any  
> > > ‘gotchas’ before I code this.
> > > 
> > > If anyone can suggest a better practice I’d appreciate it, thanks.
> 
> url\_for :overwrite\_params =\> {}
> 
> it’s intended to return the current uri with only specific parts  
> changed, the action or id or what ever… but don’t pass any changes 😉

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [April 20, 2006, 5:02am UTC](https://www.ruby-forum.com/t/best-practice-for-returning-to-calling-page/58156/5 "2006-04-20T05:02:22Z")

</div>

However this requires the HTTP\_REFERER to be set in the request. I’ve  
seen cases where it isn’t. Don’t know if this is only a webrick thing  
(where I just reproduced it to be sure I’m not hallucinating…) or  
not. Here’s part of the error message returned to the browser (in  
development mode):

No HTTP\_REFERER was set in the request to this action, so redirect\_to  
:back could not be called successfully. If this is a test, make sure  
to specify @request.env[“HTTP\_REFERER”].

Anyone know when and if this is set and “guaranteed” to be available?

dean

> > redirect\_to :back

–  
Dean W.  
[http://www.aspectprogramming.com](http://www.aspectprogramming.com)

> **[NewAspects.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=newaspects.com)**
>
> Premium domains add authority to your site. Transparent pricing. 1 year WHOIS privacy included. 30-day money back guarantee.

[http://www.contract4j.org](http://www.contract4j.org)

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [April 20, 2006, 1:15am UTC](https://www.ruby-forum.com/t/best-practice-for-returning-to-calling-page/58156/6 "2006-04-20T01:15:12Z")

</div>

Gaudi Mi wrote:

> For those that are interested, I found that the redirect\_to method can  
> be used like this:
> 
> redirect\_to :back
> 
> which will take one back to the requesting page.

DOH!!!

That just saved me loads of time

Thanks Dude!

---

<div class="post-metadata">

**Author:** ![gaudimila](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/87869e/32.png) [@gaudimila](https://www.ruby-forum.com/u/gaudimila)\
**Post date:** [April 20, 2006, 10:30am UTC](https://www.ruby-forum.com/t/best-practice-for-returning-to-calling-page/58156/7 "2006-04-20T10:30:05Z")

</div>

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

> However this requires the HTTP\_REFERER to be set in the request. I’ve  
> seen cases where it isn’t. Don’t know if this is only a webrick thing

## It’s a web browser thing. The referrer information is entirely in the hands of the client, and can never be trusted or relied upon to exist. Some browsers (like Firefox) have the ability to turn it off built in, and for other browsers some people run local proxies that strip the information out.

```
	     Calle D. <removed_email_address@domain.invalid>
	 http://www.livejournal.com/users/cdybedahl/

```

“Now I want to hear the GPL done as a Gregorian chant. Or maybe a  
rapper’s  
interpretation of the BSD license.” – Russ Allbery, BofhNet
