Shishir Srivastava
2007-Sep-19 09:42 UTC
AJAX call creates new instance of controller class
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Shishir Srivastava wrote:> 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 > ~ShishirSave whatever you need to persist between AJAX calls to the session object. -- Michael Wang --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Shishir Srivastava 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 > ~ShishirHiya 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---