# AJAX call creates new instance of controller class

**URL:** <https://www.ruby-forum.com/t/ajax-call-creates-new-instance-of-controller-class/114967>\
**Category:** Rails\
**Created:** [September 19, 2007, 11:42am UTC](https://www.ruby-forum.com/t/ajax-call-creates-new-instance-of-controller-class/114967 "2007-09-19T11:42:22Z")\
**Posts on this page:** 4\
**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:** [September 19, 2007, 11:42am UTC](https://www.ruby-forum.com/t/ajax-call-creates-new-instance-of-controller-class/114967/1 "2007-09-19T11:42:22Z")

</div>

Hi All,

I have a problem with the AJAX requests in rails. Consider a  
controller class C with some methods M1, M2, M3. For this controller  
class I have a view V.rhtml.

Within V.rhtml I have a periodically\_call\_remote AJAX request script  
to call the method M1 after every 5 seconds say.

However, when I do this , I observe that for every new AJAX request a  
new instance of my controller class C gets created with default values  
of the instance variables, whereas I need the values of variables from  
my current instance of the Class C .

Any clues as to how can I maintain a single class instance for all of  
my AJAX requests from a particular client.

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:** [September 19, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/ajax-call-creates-new-instance-of-controller-class/114967/2 "2007-09-19T12:20:22Z")

</div>

Shishir S. wrote:

> However, when I do this , I observe that for every new AJAX request a  
> new instance of my controller class C gets created with default values  
> of the instance variables, whereas I need the values of variables from  
> my current instance of the Class C .
> 
> Any clues as to how can I maintain a single class instance for all of  
> my AJAX requests from a particular client.
> 
> TIA  
> ~Shishir

Hiya Shishir,

You need to remember that HTTP is essentially a stateless protocol -  
every ajax call you make is similar to just doing a reload in your  
webpage.

The same principles will apply to keep your current state i.e. add  
get/post parameters to your request, use session or use cookies so that  
you can determine the current state in your controller.

Hope that helps.

Luke

---

<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:** [September 19, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/ajax-call-creates-new-instance-of-controller-class/114967/3 "2007-09-19T12:28:27Z")

</div>

Shishir S. wrote:

> new instance of my controller class C gets created with default values  
> of the instance variables, whereas I need the values of variables from  
> my current instance of the Class C .
> 
> Any clues as to how can I maintain a single class instance for all of  
> my AJAX requests from a particular client.
> 
> TIA  
> ~Shishir

Save whatever you need to persist between AJAX calls to the session  
object.

–  
Michael W.

---

<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:** [September 20, 2007, 8:55am UTC](https://www.ruby-forum.com/t/ajax-call-creates-new-instance-of-controller-class/114967/4 "2007-09-20T08:55:18Z")

</div>

I am trying to use the session[:object] but the mongrel server throws  
some error. ☹

Can you give some example code as to how to implement the same.

Thanks,  
Shishir

On Sep 19, 3:20 pm, Luke P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
