# Inability to do a redirect\_to when an ajax call is made duri

**URL:** <https://www.ruby-forum.com/t/inability-to-do-a-redirect-to-when-an-ajax-call-is-made-duri/88253>\
**Category:** Rails\
**Created:** [March 9, 2007, 7:12am UTC](https://www.ruby-forum.com/t/inability-to-do-a-redirect-to-when-an-ajax-call-is-made-duri/88253 "2007-03-09T07:12:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan\_kuwachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dfb087/32.png) [@dan\_kuwachi](https://www.ruby-forum.com/u/dan_kuwachi)\
**Post date:** [March 9, 2007, 7:12am UTC](https://www.ruby-forum.com/t/inability-to-do-a-redirect-to-when-an-ajax-call-is-made-duri/88253/1 "2007-03-09T07:12:07Z")

</div>

I have before\_filters set up doing authentication. In my  
before\_filters, I have some code that expires idle sessions when a  
controller action happens, and when it meets the idle criteria i do  
a :redirect to the login page.

However, I have run into the strange situation where when an ajax call  
is performed when the session goes idle, the :redirect to the login  
page doesn’t work. In the log, it shows the redirection taking place,  
but nothing is displayed on the screen.

The business logic works when a non-ajax call is invoked when the  
session goes idle. Any idea of why a redirect isn’t possible here???

thx in advance.

---

<div class="post-metadata">

**Author:** ![dan\_kuwachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dfb087/32.png) [@dan\_kuwachi](https://www.ruby-forum.com/u/dan_kuwachi)\
**Post date:** [March 9, 2007, 9:08am UTC](https://www.ruby-forum.com/t/inability-to-do-a-redirect-to-when-an-ajax-call-is-made-duri/88253/2 "2007-03-09T09:08:17Z")

</div>

> However, I have run into the strange situation where when an ajax call  
> is performed when the session goes idle, the :redirect to the login  
> page doesn’t work. In the log, it shows the redirection taking place,  
> but nothing is displayed on the screen.
> 
> The business logic works when a non-ajax call is invoked when the  
> session goes idle. Any idea of why a redirect isn’t possible here???

If it’s an ajax call, your browser is not expecting a new page, so the  
response will not be sent directly to the browser, but just some  
javascript code that will execute on the current document context.

So… the way to redirect after an ajax call, is by doing it client-side,  
with a javascript redirect. You have a clean way of doing that by using  
in your action the rjs capabilities of rails and doing something like  
render :update do |page|  
page.redirect\_to your\_url\_for\_parameters\_or\_string  
end

This will cause a javascript redirect to be sent to the browser (by  
changing the windows.href.location property of the caller), so the  
redirect should be working fine.

regards,

javier ramirez

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![dan\_kuwachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dfb087/32.png) [@dan\_kuwachi](https://www.ruby-forum.com/u/dan_kuwachi)\
**Post date:** [March 9, 2007, 9:41am UTC](https://www.ruby-forum.com/t/inability-to-do-a-redirect-to-when-an-ajax-call-is-made-duri/88253/3 "2007-03-09T09:41:21Z")

</div>

> This will cause a javascript redirect to be sent to the browser (by  
> changing the windows.href.location property of the caller), so the  
> redirect should be working fine.

So I have something like  
navigation\_controller  
before\_filter :=\> “login\_check”

def login\_check…  
expire\_check?  
end

def expire\_check  
if (expire )  
//current code  
redirect\_to :controller=\>“account”, :action=\>“login”

```
    ////do i put the rjs redirect here????

```

end

My next question would be is how do i tell the difference between on  
the server whether to do a regular redirect or a rjs redirect call…

thx a lot  
-dan

---

<div class="post-metadata">

**Author:** ![dan\_kuwachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dfb087/32.png) [@dan\_kuwachi](https://www.ruby-forum.com/u/dan_kuwachi)\
**Post date:** [March 9, 2007, 10:45am UTC](https://www.ruby-forum.com/t/inability-to-do-a-redirect-to-when-an-ajax-call-is-made-duri/88253/4 "2007-03-09T10:45:29Z")

</div>

> My next question would be is how do i tell the difference between on  
> the server whether to do a regular redirect or a rjs redirect call…

that you can do by checking request.xhr?. It will return true only if  
ajax is being used (it checks an http header called XML\_HTTP\_REQUEST or  
something like that)

> ```
> //current code
> redirect_to :controller=>"account", :action=>"login"
> 
> ////do i put the rjs redirect here????
> 
> ```
> 
> end

If you are going to use the action both from ajax and normal request,  
then you have to use a condition

def expire\_check  
if (expire )  
url\_params={:controller=\>“account”, :action=\>“login”}  
if request.xhr?  
render :update do |page|  
page.redirect\_to url\_params  
end  
else  
//current code  
redirect\_to url\_params  
end  
end  
end

if you are just going to use it from ajax, you can put directly the  
render update and forget about asking for xhr?

regards,

javier ramirez

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![dan\_kuwachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dfb087/32.png) [@dan\_kuwachi](https://www.ruby-forum.com/u/dan_kuwachi)\
**Post date:** [March 9, 2007, 11:35pm UTC](https://www.ruby-forum.com/t/inability-to-do-a-redirect-to-when-an-ajax-call-is-made-duri/88253/5 "2007-03-09T23:35:25Z")

</div>

thx guys… your answers were dead on… my session expiration works  
now 🙂  
-dan

---

<div class="post-metadata">

**Author:** ![dan\_kuwachi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/dfb087/32.png) [@dan\_kuwachi](https://www.ruby-forum.com/u/dan_kuwachi)\
**Post date:** [March 9, 2007, 3:03pm UTC](https://www.ruby-forum.com/t/inability-to-do-a-redirect-to-when-an-ajax-call-is-made-duri/88253/6 "2007-03-09T15:03:09Z")

</div>

Hi Dan,

> The business logic works when a non-ajax call is invoked when the  
> session goes idle. Any idea of why a redirect isn’t possible here???

The problem is in what the browser’s expecting as a reponse: html or js.  
Your log shows you that the problem is that the browser doesn’t know  
what to  
do with what it’s getting. Without seeing a little code I can only say  
that  
you’ll probably need to test the request type (i.e., using responds\_to)  
and  
then either do your existing redirect\_to for html requests or a:  
render :update do |page|  
page.redirect\_to :action =\> whatever  
end

hth,  
Bill
