# Ajax.Request w/standard redirect doesn't render

**URL:** <https://www.ruby-forum.com/t/ajax-request-w-standard-redirect-doesnt-render/84242>\
**Category:** Rails\
**Created:** [March 20, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/ajax-request-w-standard-redirect-doesnt-render/84242 "2006-03-20T18:20:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 20, 2006, 6:20pm UTC](https://www.ruby-forum.com/t/ajax-request-w-standard-redirect-doesnt-render/84242/1 "2006-03-20T18:20:42Z")

</div>

I am doing an Ajax call in my page to a controller method like so:

new Ajax.Request(’\<%= url\_for(:action =\> “target\_list\_add” )%\>’, {  
asynchronous:true });"

In my controller method “target\_list\_add”, I do something and then I  
say:

redirect\_to( :action =\> ‘target\_list\_upload’, :layout =\> false )

I know for certain that this line of code is being reached.

Why does the redirect not occur?

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 20, 2006, 7:18pm UTC](https://www.ruby-forum.com/t/ajax-request-w-standard-redirect-doesnt-render/84242/2 "2006-03-20T19:18:58Z")

</div>

Wes,

Since the Ajax call in the background, you need to perform the  
redirect with JavaScript, so the easiest way would be for you to use  
inline RJS in your controller like follows:

def target\_list\_add  
render :update do |page|  
page.redirect\_to( :action =\> ‘target\_list\_upload’)  
end  
end

You’ll need Edge Rails or the RJS plugin to do that, though.

If you don’t want to use RJS, you could do it the long way like this:

def target\_list\_add  
@headers[‘Content-Type’] = ‘text/javascript’  
render :text =\> “window.location.href = ‘#{url\_for(:action =\>  
‘target\_list\_upload’)}’”  
end

On 3/20/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I know for certain that this line of code is being reached.  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Cody F.  
[http://www.codyfauser.com](http://www.codyfauser.com)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 20, 2006, 7:26pm UTC](https://www.ruby-forum.com/t/ajax-request-w-standard-redirect-doesnt-render/84242/3 "2006-03-20T19:26:17Z")

</div>

Cody,

Thanks for your prompt reply.

Can you go into more detail on what “Since the Ajax call in the  
background” means?

I do understand Ajax conceptually, but I’m trying to understand the  
nature of a HTTP response in an Ajax context.

Is the root of my issue that there is no HTTP response to give back to  
the browser and that is why the Ajax.Request + controller redirect  
doesn’t render anything?

Thanks,  
Wes

Cody F. wrote:

> Wes,
> 
> Since the Ajax call in the background, you need to perform the  
> redirect with JavaScript, so the easiest way would be for you to use  
> inline RJS in your controller like follows:
> 
> def target\_list\_add  
> render :update do |page|  
> page.redirect\_to( :action =\> ‘target\_list\_upload’)  
> end  
> end
> 
> You’ll need Edge Rails or the RJS plugin to do that, though.
> 
> If you don’t want to use RJS, you could do it the long way like this:
> 
> def target\_list\_add  
> @headers[‘Content-Type’] = ‘text/javascript’  
> render :text =\> “window.location.href = ‘#{url\_for(:action =\>  
> ‘target\_list\_upload’)}’”  
> end
> 
> On 3/20/06, Wes G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I know for certain that this line of code is being reached.  
> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> –  
> Cody F.  
> [http://www.codyfauser.com](http://www.codyfauser.com)

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 21, 2006, 4:24am UTC](https://www.ruby-forum.com/t/ajax-request-w-standard-redirect-doesnt-render/84242/4 "2006-03-21T04:24:41Z")

</div>

Wes G. wrote:

> Is the root of my issue that there is no HTTP response to give back to  
> the browser and that is why the Ajax.Request + controller redirect  
> doesn’t render anything?

The HTTP request was made by the XMLHTTP component (or equivalent), and  
the XMLHTTP component handles the resulting HTTP response.

If the response specifies a redirect, it is the XMLHTTP component that  
should be trying again at the redirect address - not the browser.

regards

Justin

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 20, 2006, 8:34pm UTC](https://www.ruby-forum.com/t/ajax-request-w-standard-redirect-doesnt-render/84242/5 "2006-03-20T20:34:23Z")

</div>

Wes, the root of the issue is that what you get back from your request  
needs  
to be processed by javascript to be actioned upon. You only get XML data  
back and your browser will not process it unless told to do that. This  
is  
what Cody meant by “call in background”.  
Therefore, if you want to do a conditional redirect you need to analyze  
received data and if it meets required cretirea do a javascript  
redirect.
