inability to do a redirect_to when an ajax call is made during a before_filter

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

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.

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

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.

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

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)

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   

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

Hi Dan,

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???

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

thx guys.. your answers were dead on.. my session expiration works now :slight_smile: -dan