Carl-Gustaf Harroch
2006-Aug-29 15:25 UTC
Instance variables best solution for wizard type
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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Carl-Gustaf Harroch
2006-Aug-29 15:26 UTC
Re: Instance variables best solution for wizard type
Hello Again, Should I use accessors by any chance? Any exemple would be much appreciated. Carl On 8/29/06, Carl-Gustaf Harroch <charroch-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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? > > 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 >-- Carl-Gustaf Harroch --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Aug-29 15:42 UTC
Re: Instance variables best solution for wizard type
Hi -- On Tue, 29 Aug 2006, Carl-Gustaf Harroch 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?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 | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Carl-Gustaf Harroch 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Carl-Gustaf Harroch 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---