Newbie: Rails Controller Attribute Scope

T

tim.hennekey

I'm having a hard time figguring out what is wrong with my controller.
I create an attribure, @processes in the method index. I then want to
access the attribute in the same class, in another method
process_status. I get an error:

NoMethodError in Processes#process_status
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.sizeRAILS_ROOT:
../script/../config/..

So it looks to me that the instance of ProcessController is changing
and the attribute is nil? Is there a way around this? Why does Rails
create a new controller for each invocation of a method?
 
T

Tanner Burson

------=_Part_3627_1815098.1141851025760
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

I'm having a hard time figguring out what is wrong with my controller.
I create an attribure, @processes in the method index. I then want to
access the attribute in the same class, in another method
process_status. I get an error:

NoMethodError in Processes#process_status
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.sizeRAILS_ROOT:
../script/../config/..

So it looks to me that the instance of ProcessController is changing
and the attribute is nil? Is there a way around this? Why does Rails
create a new controller for each invocation of a method?
This is probably better answered on the Rails list, but I'll take a shot. =
I
assume by new invocation of a method, you mean new request, correct? If so=
,
then the answer is simply because on each request the entire rails
environment is setup, and torn down. For any value to survive between
requests it needs to either be in the flash, session, request, or external
storage. This is probably a somewhat surprising behavior if you come from =
a
servlet world, but in the PHP or PERL world, this is fairly common.


--
=3D=3D=3DTanner Burson=3D=3D=3D
(e-mail address removed)
http://tannerburson.com <---Might even work one day...

------=_Part_3627_1815098.1141851025760--
 
M

Marcin Mielżyński

I'm having a hard time figguring out what is wrong with my controller.
I create an attribure, @processes in the method index. I then want to
access the attribute in the same class, in another method
process_status. I get an error:

NoMethodError in Processes#process_status
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.sizeRAILS_ROOT:
./script/../config/..

So it looks to me that the instance of ProcessController is changing
and the attribute is nil? Is there a way around this? Why does Rails
create a new controller for each invocation of a method?

In Rails unlike in Struts a new (statefull) instance of your Controller
is created on each request, so you can acces instance variables between
methods only in a single request process.
If you want to save a state (i.e. share values between requests) use
session instead.

I prefer this type of state management since you dont have to pass
patameters between methods, and creation of new instance on each request
lets you forget about purging them :D (GC wouldnt be able to collect them)



lopex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top