# Doing redirection on a remote call to a function

**URL:** <https://www.ruby-forum.com/t/doing-redirection-on-a-remote-call-to-a-function/178493>\
**Category:** Rails\
**Created:** [November 5, 2009, 12:35pm UTC](https://www.ruby-forum.com/t/doing-redirection-on-a-remote-call-to-a-function/178493 "2009-11-05T12:35:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [November 5, 2009, 12:35pm UTC](https://www.ruby-forum.com/t/doing-redirection-on-a-remote-call-to-a-function/178493/1 "2009-11-05T12:35:13Z")

</div>

Hi All

In my application,i have a form which contains a select box for  
“categories”  
And when the user selects a category,a remote function is called which  
fetches a list of subcategories and populates in another select box on  
the form

Category:\<%= collection\_select :category, :id, @categories, :id , :name, {}, {:size =\> 10, :onchange =\> remote\_function( :update =\> "subcat", :url =\> { :controller =\> "categories", :action =\> "get\_subcategories"}, :position =\> "replace", :with =\> "'category\_id='+this.value" ) } %\>

I have a div with id "subcat" on the form.

I also have set a session timeout of 10 mins.  
I am getting my page redirected to login page if not the session is  
present/timed out.I am doing this by having the function called  
“login\_required” in the application.rb controller and in all controllers  
I am adding  
before\_filter :login\_required

But this is working only in case of Normal Http requests.

In the above case,if the session has expired,and the user is trying to  
populate the subcategory list using remote call,I am getting the error  
message updated in the div with id subcat .  
But I want the page to be redirected to login page,even when there are  
remote calls to the function if session has timed out.

I am in need of a solution at the earliest.Kindly help me

Thanks  
Charanya

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [November 5, 2009, 1:22pm UTC](https://www.ruby-forum.com/t/doing-redirection-on-a-remote-call-to-a-function/178493/2 "2009-11-05T13:22:03Z")

</div>

Thanks Denis…  
this really helped…

---

<div class="post-metadata">

**Author:** ![charanya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/b5e925/32.png) [@charanya](https://www.ruby-forum.com/u/charanya)\
**Post date:** [November 5, 2009, 12:50pm UTC](https://www.ruby-forum.com/t/doing-redirection-on-a-remote-call-to-a-function/178493/3 "2009-11-05T12:50:17Z")

</div>

You can specify Javascript callbacks for the various phases of the  
remote call and also for specific response status or status codes (see  
the doc for link\_to\_remote at [http://tinyurl.com/s47df](http://tinyurl.com/s47df) ; remote\_function  
takes the same arguments).

So it might be something like this (off the top of my head):

remote\_function( :update =\> “subcat”, :url =\> { :controller =\>  
“categories”, :action =\> “get\_subcategories”}, :position =\> “replace”,  
:with =\> “‘category\_id=’+this.value”, 302 =\>  
“handleRedirection(request)” )

where handleRedirection() is a Javascript function you’ve written to do  
the redirection (I’m assuming the response Lcation: header is available  
there, but haven’t checked).

hth,

dwh
