Get updated model values in Controller.

Hi All,

I have a model instance M1 , which has a dynamically changing instance variable say V. I want to access the value of V in my view through controller C using AJAX requests.

The problem is that for every new AJAX request a new controller instance gets created. This new controller instance does not have any reference to my already existing Model object M1 and therefore to the variable V.

I have tried using session[:M1] but I get server error when I run the page.

Can I not keep the model object instances in session ?? Can anyone point out if I am doing anything wrong, or maybe suggest an alternative solution.

TIA Shishir

Hi Jean,

You are right, the problem however is that my model object M1 is not serializable and hence cannot be dumped by the session. In fact I don't want to store it even in a db since it's a non -AR model.

Here's my model code: class Login

  def connect id, password     @jid = id     @password = password     @cl = Jabber::Client::new(@jid)     @cl.connect(host='im.oracle.com', port=5223)     @cl.auth_nonsasl(@password, false)     @cl.add_message_callback do |m|     if m.type != :error       assign_message(m.body,m.from)       if m.body == 'exit'         @cl.close       end     end     end   end

        def assign_message msg_b, msg_f          @mesage =Jabber::Message::new(msg_f, msg_b).set_type(:normal).set_id('1')   end

        def throw     return @mesage.body   end end

Now this is basically a connection object which maintains my stream connection to a server.

Here's the controller part.

class LoginController < ApplicationController

model :Login

def index end

def sign_in

@jid = Jabber::JID::new(@params['user_name']+'/ruby') @jid.domain='oracle.com' @password = params['pwd'] #testing Login model creation @log_in = Login.new @log_in.connect(@jid,@password) session[:sess]= @log_in session[:sess].connect

puts('signed in to oracle') end

def throw_msg   render_text "message received : #{session[:sess].throw}" end

end

This controller gets created everytime I send an AJAX request calling the throw_msg method in the controller to receive the new messages coming in the model. But since the model object cannot be persisted it doesn't know of the messages received.

Can you please suggest any alternative method.

~shishir

If you have no way to serialize, or even better persist, the info into a db, then I think you are stuck maybe having to look at a daemon process that handles the messaging and you can query THAT in with your call to the "throw_msg" method.

I am not really into threads and daemon processes. :(. What I am wondering here is that unlike java where I can maintain session for such objects why can i not do that on rails.

Or else, and I'm really just brainstorming here,

what about a class variable that is shared across all instances of your controller and have that route your messages?

Not possible, there are two problems with that approach.

First the shared class variable will not be able to maintain more than 1 user session. ( Basically when say N users connect I need N instances of my model object.) Second the class variable will also get initialized every time the controller class get instantiated because I create the model object within the controller class.

Stuck badly.

:frowning: ~Shishir