# Ajax substitute for "redirect\_to"

**URL:** <https://www.ruby-forum.com/t/ajax-substitute-for-redirect-to/89597>\
**Category:** Rails\
**Created:** [March 21, 2007, 2:03pm UTC](https://www.ruby-forum.com/t/ajax-substitute-for-redirect-to/89597 "2007-03-21T14:03:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wotanabi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a5b964/32.png) [@wotanabi](https://www.ruby-forum.com/u/wotanabi)\
**Post date:** [March 21, 2007, 2:03pm UTC](https://www.ruby-forum.com/t/ajax-substitute-for-redirect-to/89597/1 "2007-03-21T14:03:28Z")

</div>

######## Short version:

I need an ajax version of “redirect\_to” that either  
triggers the browser to do a new XHR (XML-HTTP-Request)  
or  
triggers rails to behave as if a clean new XHR had come in.

######## Long version:

I have an existing non-ajax rails website. I want to change it in the  
way  
that I can toggle a switch and the site uses link\_to\_remote instead of  
link\_to. This job is done, it works fine. The solution uses .rjs  
templates  
which make the changes by calling “page.replace\_html” for the dynamic  
parts  
of the page. So far so good.

Now I need an ajax-version for all the “redirect\_to”'s in the existing  
code.  
I see two possible solutions here:

#### 

1. Tell the browser to do a new XHR

The helper “link\_to\_remote” produces someting like:

dummy

I need an rjs template that, if interpreted by rails and received by the  
browser, tells the browser to behave as if the user klicked on the above  
“link\_to\_remote”.

Again with other words: what does an .rjs template look like that tells  
the  
browser to do an XHR immediately?

#### 

1. Tell rails to forget everything it did so far and behave as if there  
was  
a new request x coming in

Is it possible and if how to tell rails: Reset everything you did for  
the  
current browser request and then handle the following action call as if  
it  
was produced by a clean new browser request (including everything,  
especially the rendering)?

######## Call for help

I followed both ways for hours but didn’t succeed. My knowledge of rails  
internals isn’t experienced enough by now. Any help is very much  
appreciated.

---

<div class="post-metadata">

**Author:** ![wotanabi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a5b964/32.png) [@wotanabi](https://www.ruby-forum.com/u/wotanabi)\
**Post date:** [March 21, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/ajax-substitute-for-redirect-to/89597/2 "2007-03-21T15:18:19Z")

</div>

In your controller -

render :update do |page|  
page.redirect\_to ‘new\_url’  
end

This is equivalent to window.location.href = ‘new\_url’

Heiner Bunjes wrote:

> ######## Short version:
> 
> I need an ajax version of “redirect\_to” that either  
> triggers the browser to do a new XHR (XML-HTTP-Request)  
> or  
> triggers rails to behave as if a clean new XHR had come in.

---

<div class="post-metadata">

**Author:** ![wotanabi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a5b964/32.png) [@wotanabi](https://www.ruby-forum.com/u/wotanabi)\
**Post date:** [March 21, 2007, 3:45pm UTC](https://www.ruby-forum.com/t/ajax-substitute-for-redirect-to/89597/3 "2007-03-21T15:45:35Z")

</div>

Chet Sanders wrote:

> In your controller -
> 
> render :update do |page|  
> page.redirect\_to ‘new\_url’  
> end
> 
> This is equivalent to window.location.href = ‘new\_url’

Thanks, but this does not at all help me. Maybe the subject was  
misleading. I don’t want the browser to do a normal http-request, I want  
it to do an XML-HTTP-Request.

---

<div class="post-metadata">

**Author:** ![wotanabi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/a5b964/32.png) [@wotanabi](https://www.ruby-forum.com/u/wotanabi)\
**Post date:** [March 23, 2007, 8:03pm UTC](https://www.ruby-forum.com/t/ajax-substitute-for-redirect-to/89597/4 "2007-03-23T20:03:44Z")

</div>

This discussion is going on at:

[http://railsforum.com/viewtopic.php?pid=17413#p17413](http://railsforum.com/viewtopic.php?pid=17413#p17413)

So if you are interested, have a look there!

Any help very much appreciated.
