I have a controller action that handles JSON requests mainly for handling datatables grid redrawing. But I have this before_filter that checks if the session has been destroyed and should redirect back to the logout action. When the request is an ajax request, the redirect happens fine. But if the request is JSON, the logs shows “Filter chain halted as :authorize rendered or redirected” but does not redirect the page at all. Any clues?
I see that the logs show that it’s processing the action as JSON:
Processing by UserController#datatable_redrawings as JSON
My Contoller method:
before_filter :auth def auth if session[:username].blank? flash[:error] = "Please login." if request.xhr? render :js => "window.location = '/logout'" else respond_to do |format| format.html { redirect_to :action => "logout" } end end end end
A redirection isn’t really RESTful, nor is sending JS when the client is expecting a JSON response. You should send an Unauthorized (401) HTTP status code or something similar.