# Instance variables best solution for wizard type

**URL:** <https://discuss.rubyonrails.org/t/instance-variables-best-solution-for-wizard-type/714>\
**Category:** rubyonrails-talk\
**Created:** [August 29, 2006, 3:25pm UTC](https://discuss.rubyonrails.org/t/instance-variables-best-solution-for-wizard-type/714 "2006-08-29T15:25:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carl\_H](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@Carl\_H](https://discuss.rubyonrails.org/u/Carl_H)\
**Post date:** [August 29, 2006, 3:25pm UTC](https://discuss.rubyonrails.org/t/instance-variables-best-solution-for-wizard-type/714/1 "2006-08-29T15:25:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Carl\_H](https://avatars.discourse-cdn.com/v4/letter/c/e9a140/32.png) [@Carl\_H](https://discuss.rubyonrails.org/u/Carl_H)\
**Post date:** [August 29, 2006, 3:26pm UTC](https://discuss.rubyonrails.org/t/instance-variables-best-solution-for-wizard-type/714/2 "2006-08-29T15:26:52Z")

</div>

Hello Again,

Should I use accessors by any chance? Any exemple would be much appreciated.

Carl

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://discuss.rubyonrails.org/u/David_A_Black1)\
**Post date:** [August 29, 2006, 3:42pm UTC](https://discuss.rubyonrails.org/t/instance-variables-best-solution-for-wizard-type/714/3 "2006-08-29T15:42:20Z")

</div>

Hi --

> 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?

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):

&nbsp;&nbsp;&nbsp;c = UserController.new # create an instance of the controller &nbsp;&nbsp;&nbsp;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
