Instance variable, ajax and partial

i get an error message 'you might have expexted an instance of Array,
error occurred while evaluating nil

i have created @selected_team = Array.new in a previous def that has
been called.
if i put @selected_team in the def below, it works but it also zeros
all the data each time it is called.
am i missing something basic here. arn’t instance variable available
thru all def in a controller as well as the views?
the instance variable is working in the view via partial!

def roster_selection
if params[:cvalue]
@selected_team << [ params[:full_name], params[:user_id] ]
else
# take out name
end
render :partial => “selected”
end

On 18 Apr 2008, at 23:13, rushnosh wrote:

the instance variable is working in the view via partial!

I think you’re going to have to show us a little more than this.

Fred

def show
@club = Club.find params[:id]
rosternew(@club.id, params[:sort_by] )
@selected_team = Array.new # @selected_array
variable.
end

everything works well.
i have a onclick on a checkbox, when clicked it calls
roster_selection, the name and the user_id is passed and i can see
them in the logs.
but i get a nil error when @selected_array << [ x , y] as in the
code.

if i directly put @selected = Array.new in the def called everything
runs but of course my array is initialized and contains only one name.
let me know if there is something specific you need to see. i have
even tried rjs, but i have now narrowed that out as well because the
error is the same.
i can not fill this array for some reason.

On Apr 18, 3:54 pm, Frederick C. [email protected]

On 19 Apr 2008, at 00:59, rushnosh wrote:

roster_selection, the name and the user_id is passed and i can see
them in the logs.
but i get a nil error when @selected_array << [ x , y] as in the
code.

That’s because each action is handled by a separate controller
instance (which could be in a separate mongrel, or even on a different
machine if your app is deployed across multiple machines). setting an
instance variable in your show action has no effect on whether what
exists when your roster_selection action is called.

Fred

That’s because each action is handled by a separate controller
instance (which could be in a separate mongrel, or even on a different
machine if your app is deployed across multiple machines). setting an
instance variable in your show action has no effect on whether what
exists when your roster_selection action is called.

i read this a couple of times, both def are in the same controller.
both actions as you refer to it.

i even tried this inside the view <%= @team << [ ‘hello’, ‘goodbye’]
same error message.
i am going to look a little bit closer at the local variable in
partials. i have a feeling it has something to do with that.

this is a trivial problem so i can’t believe that it can not be done
in ruby using javascript/ajax.

On Apr 18, 5:09 pm, Frederick C. [email protected]

fredirick, i don’t think i have explained my self. i have used
instance variables across def without a problems. this has something
to do with the call from javascript. i wanted the user to be able to
click on a check box and that item appear in a div (and i need to keep
track of the items selected).

i think i will have to back to the old way doing this, user clicks all
the check box wanted then press a button, then the items appear in a
div.

it would have been really nice to do this with ajax. i am missing
something fundamental here.

On Apr 18, 5:39 pm, Frederick C. [email protected]

On 19 Apr 2008, at 01:36, rushnosh wrote:

The code runs in response to separate http requests (unless i’ve
completely misunderstood your requests). Instance variables are not
preserved across requests.

Fred

On 19 Apr 2008, at 01:56, rushnosh wrote:

it would have been really nice to do this with ajax. i am missing
something fundamental here.

I think you need to read up on some of your object oriented
programming. Instance variables are specific to one instance (that’s
why they’re called what they are). If you’ve got an instance then all
the the methods of that object can see its instance variables (which
is what underpins your " i have used instance variables across def").

However 2 different instances of an object have completely separate
instance variables. The instance of controller that handles your show
action and the one that handles your ajax request are different and
thus cannot see each other’s instance variables. If you want to
preserve state across requests then you need to stick it somewhere
like the database, the session, or just manage it all client side

Fred

On 19 Apr 2008, at 02:05, Frederick C. wrote:

why they’re called what they are). If you’ve got an instance then
all the the methods of that object can see its instance variables
(which is what underpins your " i have used instance variables
across def").

However 2 different instances of an object have completely separate
instance variables. The instance of controller that handles your
show action and the one that handles your ajax request are different
and thus cannot see each other’s instance variables. If you want to
preserve state across requests then you need to stick it somewhere
like the database, the session, or just manage it all client side

All that said, an easier way here might be a Form observer that will
send all of your checkboxes to your app whenever any of them are toggled

Fred

thanks for your help. you cleared up a lot for me here. hopefully
other newbies will gain from it as well.

On Apr 18, 6:19 pm, Frederick C. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs