Hello All,
This is my first post so if you could not crucify me publicly I would
appreciate.
I beleive I have a simple question but do not manage to find the answer.
Basically I want to create a wizard type of system where the customer
enters some info on the first page then goes to step 2, some more
info, then step 3 and finishes. Each steps represent an action in the
same controller. I have declared an instance variable (@step1) in the
first step and needs it in the last finishing step.
If I try to use that variable, it gives me a null pointer exception.
The lifespan of a instance variable is between the controller and the
view and dies thereafter? Can it be for the entire lifespan of the
controller?
More concrete exemple:
— step 1 —
name , address ect… + internal info set within the action
— step 2 —
depends on info entered in step 1 +
favorite cat
– step 3 —
depends on step2 + favorite dog
– step 4 –
finishes -> saves into the db === > name, address, internal info set
by action “step1” + favorite cat + favorite dog
How can in action “step4” access the data set internaly un “step1”
plus the data entered by the client in the further 3 actions?
Hope it is clear engough. I heard of acts_as_wizard but beleive this
situation do not require a plugin - good for my learning curve as
well…
Cherio,
Carl
Hi –
On Tue, 29 Aug 2006, Carl-Gustaf H. wrote:
info, then step 3 and finishes. Each steps represent an action in the
same controller. I have declared an instance variable (@step1) in the
first step and needs it in the last finishing step.
If I try to use that variable, it gives me a null pointer exception.
The lifespan of a instance variable is between the controller and the
view and dies thereafter? Can it be for the entire lifespan of the
controller?
It is
The thing is, though, the lifespan of the controller is
only one action.
The controller is actually an instance of your controller class. When
you run an action – say, ‘login’ for a user controller – what
happens is (in simplified form):
c = UserController.new # create an instance of the controller
c.login # call a method (“action”) on that instance
On the next action, a new instance of your controller is created. And
so on.
Every object has its own instance variables; therefore, each
controller instance has its own instance variables.
Have you looked at the special ‘session’ hash? You can use it to
store data between actions.
David
–
David A. Black | [email protected]
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] Ruby for Rails | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
Carl-Gustaf H. wrote:
If I try to use that variable, it gives me a null pointer exception.
The lifespan of a instance variable is between the controller and the
view and dies thereafter? Can it be for the entire lifespan of the
controller?
Hi Carl,
A new instance of the controller is creates for each request, so
variables do not persist. The best way would be to store the info you
have so far in a DB record.
Ideally an extra column or somesuch to indicate what ‘page’ you are up
to for that record, and simply update the record as you go.
The share-nothing approach lets Rails apps scale easily by adding other
appservers etc.
Alan
Hello Again,
Should I use accessors by any chance? Any exemple would be much
appreciated.
Carl
On 8/29/06, Carl-Gustaf H. [email protected] wrote:
same controller. I have declared an instance variable (@step1) in the
name , address ect… + internal info set within the action
plus the data entered by the client in the further 3 actions?
Hope it is clear engough. I heard of acts_as_wizard but beleive this
situation do not require a plugin - good for my learning curve as
well…
Cherio,
Carl
–
Carl-Gustaf H.
Carl-Gustaf H. wrote:
Hello All,
This is my first post so if you could not crucify me publicly I would
appreciate.
I beleive I have a simple question but do not manage to find the answer.
Basically I want to create a wizard type of system where the customer
enters some info on the first page then goes to step 2, some more
info, then step 3 and finishes. Each steps represent an action in the
same controller. I have declared an instance variable (@step1) in the
first step and needs it in the last finishing step.
If I try to use that variable, it gives me a null pointer exception.
The lifespan of a instance variable is between the controller and the
view and dies thereafter? Can it be for the entire lifespan of the
controller?
The internet is “stateless”. If you want to reuse data between actions,
or page requests, you have to save it in a session or cookie or the
database.
def step1
session[:step1] = params[:step1]
end
def step2
@step1 = session[:step1]
end
or something to that effect