# Redirect on AJAX request

**URL:** <https://www.ruby-forum.com/t/redirect-on-ajax-request/116344>\
**Category:** Rails\
**Created:** [October 4, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/redirect-on-ajax-request/116344 "2007-10-04T17:17:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [October 4, 2007, 5:17pm UTC](https://www.ruby-forum.com/t/redirect-on-ajax-request/116344/1 "2007-10-04T17:17:39Z")

</div>

Hi,

Is there a way to redirect the page through a ajax request. I tried  
using link\_to\_remote tag as the logout link on a page and used the  
‘redirect\_to index’ in the called method. But I get the whole page in  
the request.reponseText as normal text and the browser page doesn’t  
change.

Has anyone implemented this succesfully.?

TIA  
~Shishir

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [October 4, 2007, 5:39pm UTC](https://www.ruby-forum.com/t/redirect-on-ajax-request/116344/2 "2007-10-04T17:39:23Z")

</div>

Shishir S. wrote:

> Is there a way to redirect the page through a ajax request. I tried  
> using link\_to\_remote tag as the logout link on a page and used the  
> ‘redirect\_to index’ in the called method. But I get the whole page in  
> the request.reponseText as normal text and the browser page doesn’t  
> change.

Just send JS that sets the window.location:

render :update do |rjs|  
rjs \<\< “window.location = #{ url\_for something };”  
end

I wrote ‘rjs’ where everyone else writes ‘page’ because that was a bad  
idea for a variable name. There are way too many variables out there  
called ‘page’!

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
^ assert\_xpath

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [October 4, 2007, 5:40pm UTC](https://www.ruby-forum.com/t/redirect-on-ajax-request/116344/3 "2007-10-04T17:40:36Z")

</div>

> Just send JS that sets the window.location:
> 
> render :update do |rjs|  
> rjs \<\< “window.location = #{ url\_for something };”

url\_for(something).inspect, for free quotes and escapes!

–  
Phlip

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [October 4, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/redirect-on-ajax-request/116344/4 "2007-10-04T22:21:31Z")

</div>

a bit confused…

1. render :update do |rjs|  
rjs \<\< “window.location = #{ url\_for something };”  
end

2. page.redirect\_to

which one of these is correct…or both of them are…??

~Shishir

On Oct 4, 9:00 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [October 4, 2007, 6:01pm UTC](https://www.ruby-forum.com/t/redirect-on-ajax-request/116344/5 "2007-10-04T18:01:11Z")

</div>

On 4 Oct 2007, at 16:39, Phlip wrote:

> > Just send JS that sets the window.location:
> > 
> > render :update do |rjs|  
> > rjs \<\< “window.location = #{ url\_for something };”
> 
> url\_for(something).inspect, for free quotes and escapes!

page.redirect\_to will do all that for you

Fred

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [October 4, 2007, 10:31pm UTC](https://www.ruby-forum.com/t/redirect-on-ajax-request/116344/6 "2007-10-04T22:31:27Z")

</div>

On 10/4/07, Shishir S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> a bit confused…
> 
> 1. render :update do |rjs|  
> rjs \<\< “window.location = #{ url\_for something };”  
> end
> 
> 2. page.redirect\_to
> 
> which one of these is correct…or both of them are…??

They both are. #2 generates the same javascript as is shown in #1.

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [October 4, 2007, 10:56pm UTC](https://www.ruby-forum.com/t/redirect-on-ajax-request/116344/7 "2007-10-04T22:56:01Z")

</div>

unfotunately am getting this error

NameError  
in LoginController#logout

```
undefined local variable or method `page' for
#<LoginController:0x7f2950c0>
```

`RAILS_ROOT: script/../config/..`

i’ve added it like this in my LoginController#logout method

def logout  
session[:sess].sign\_out  
page.redirect\_to “index”  
end

where ‘index’ is my target page

~Shishir

---

<div class="post-metadata">

**Author:** ![shishir\_s](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f07891/32.png) [@shishir\_s](https://www.ruby-forum.com/u/shishir_s)\
**Post date:** [October 4, 2007, 11:00pm UTC](https://www.ruby-forum.com/t/redirect-on-ajax-request/116344/8 "2007-10-04T23:00:07Z")

</div>

On 4 Oct 2007, at 21:55, Shishir S. wrote:

> i’ve added it like this in my LoginController#logout method
> 
> def logout  
> session[:sess].sign\_out  
> page.redirect\_to “index”  
> end

Read up on rjs. page is the name usually given to the instance of  
javascript generator you use. If you have an rjs template it will be  
defined in there. If you have a render :update then it is the thing  
that is yielded to the block. Either plonk it in logout.rjs or do  
some pike  
render :update do |page|  
…  
end

Fred
