Forum: Ruby on Rails Variable Scoping Problem

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ben Lewis (Guest)
on 2006-01-23 05:47
I am having some problems with variable scoping.

I need to be able to set a variable that is accessable by other methods
within the class (or instance) (i.e not global).

An example is 2 pages that change a class variable:

class AjtestController < ApplicationController

	def initialize
		@@variable = "init"
	end

	def show
		@display = @@variable
		@@variable = "change 1"
	end

	def list
		@display2 = @@variable
		@@variable = "change 2"
	end
end

When I view 'show' the page prints "init" (the page just prints
@display)
When I view 'list' the page prints "init" (the page just prints
@display2)
This tells me that the class is being initialized (instantiated?) each
time I go to the 'show' or 'view' page.

If i remove the initialize method, i get an
"uninitialized class variable @@variable in AjtestController" error.

Is there something I am doing wrong here?  I should be able to pass
variables between methods in the same class.

Thanks,
~Ben
Kevin O. (Guest)
on 2006-01-23 06:22
Ben Lewis wrote:
> I am having some problems with variable scoping.
>
> I need to be able to set a variable that is accessable by other methods
> within the class (or instance) (i.e not global).
>
> An example is 2 pages that change a class variable:
>
> class AjtestController < ApplicationController
>
> 	def initialize
> 		@@variable = "init"
> 	end
>
> 	def show
> 		@display = @@variable
> 		@@variable = "change 1"
> 	end
>
> 	def list
> 		@display2 = @@variable
> 		@@variable = "change 2"
> 	end
> end
>
> When I view 'show' the page prints "init" (the page just prints
> @display)
> When I view 'list' the page prints "init" (the page just prints
> @display2)
> This tells me that the class is being initialized (instantiated?) each
> time I go to the 'show' or 'view' page.
>
> If i remove the initialize method, i get an
> "uninitialized class variable @@variable in AjtestController" error.
>
> Is there something I am doing wrong here?  I should be able to pass
> variables between methods in the same class.
>
> Thanks,
> ~Ben

The problem is that the object does not persist between page requests.
Everytime you make a request the server starts from scratch, so all your
objects and classes get set up as if there were no prior state.

Consequently, your class variable gets wiped out.  Try putting stuff in
the session hash, that does persist.

session[:variable] = "init"

_Kevin
Ezra Z. (Guest)
on 2006-01-23 06:38
(Received via mailing list)
On Jan 22, 2006, at 8:22 PM, Kevin O. wrote:

>>
>> 		@display2 = @@variable
>> time I go to the 'show' or 'view' page.
> The problem is that the object does not persist between page requests.
> _Kevin
And you would be better off putting the variable creation in a
before_filter rather then using initialize:


>> class AjtestController < ApplicationController

      before_filter :set_var

      def set_var
            @@var = 'init'
      end
end

cheers-

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
removed_email_address@domain.invalid
509-577-7732
Ben Lewis (Guest)
on 2006-01-23 06:39
>
> session[:variable] = "init"
>

Thanks for the quick reply Kevin.

I had seen this method previously.  While this is an option, I would
prefer to not use session variables as this makes the variable available
to multiple pages at once - in the case of multi-tab browsers.

Is there other options for achieving this functionality?
Kevin O. (Guest)
on 2006-01-23 06:49
Ben Lewis wrote:
>>
>> session[:variable] = "init"
>>
>
> Thanks for the quick reply Kevin.
>
> I had seen this method previously.  While this is an option, I would
> prefer to not use session variables as this makes the variable available
> to multiple pages at once - in the case of multi-tab browsers.
>
> Is there other options for achieving this functionality?

I suppose you could stuff it into a database table and retrieve it using
a before_filter like Ezra mentioned.

I must admit, I don't fully understand your issues with the session
hash.  If you use a good naming scheme, you shouldn't have conflicts
between actions or controllers trying to use data they shouldn't.

Sometimes I will do stuff like this..

session["#{controller_name}_variable"] = "init"

you can also do sub hashes like

session["#{controller_name}"]["variable"] = "init"

but I don't like this much because you run into trouble if the
controller_name hash hasn't been created yet.

@variable = session["#{controller_name}_variable"] || "default"

will always work

@variable = session["#{controller_name}"]["variable"] || "default"

will throw a nil exception if the controller_name hash doesn't exist.
Ben Lewis (Guest)
on 2006-01-23 07:02
> I must admit, I don't fully understand your issues with the session
> hash.  If you use a good naming scheme, you shouldn't have conflicts
> between actions or controllers trying to use data they shouldn't.

The problem is not exactly a naming issue.

The issue would arise when an operator has 2 tabs open (on the same
controller).

Using session variables, the session data of the second tab may corrupt
the session data of the first tab (normally not an issue for static
pages, but I am using AJAX to modify the variables).

To be honest - this is not a huge problem.  I would just prefer not to
have issues like this if i could help it.

~Ben
This topic is locked and can not be replied to.