Yash
2006-Apr-02 06:15 UTC
[Rails] Looking for helpers/components/controllers for form wizards
Hi, In our project, we have a number of forms to be filled by the users. The forms are presented as wizards - each form as a number of steps. I would like to know whether there is an existing helper developed by the Rails community that can let me create wizards easily. We also want all wizards to have the same look and feel. Thanks, Yash -- Posted via http://www.ruby-forum.com/.
Matthew Palmer
2006-Apr-02 07:17 UTC
[Rails] Re: Looking for helpers/components/controllers for form wizards
On Sun, Apr 02, 2006 at 08:15:12AM +0200, Yash wrote:> In our project, we have a number of forms to be filled by the users. The > forms are presented as wizards - each form as a number of steps. I would > like to know whether there is an existing helper developed by the Rails > community that can let me create wizards easily. We also want all > wizards to have the same look and feel.This doesn''t seem like something that would need a helper -- it''s only a couple of lines of code. You could implement it as a single controller: class WizardController < ApplicationController def wizard if params[:stage].nil? @stage = 1 session[:wizard_data] = new Wizard # Or whatever model is storing your stuff else @stage = params[:stage].to_i end @next_stage = @stage + 1 @wizard_data = session[:wizard_data] render :template => ''stage'' + @stage.to_s end end Then each view template gets named app/views/wizard/stageN.rhtml, and needs to use <%= start_form_tag :action => ''wizard'', :stage => @next_stage %>. Since you''ll want to use layouts to make everything look consistent, you can put that tag line into the layout and then you''re sorted. Each stage page really just needs to have the questions for that stage in it; if there''s decision making that needs to be done, you''ve got all of the previous answers in your model object @wizard_data to look at. Validation is the only other consideration; for that, I''d probably add stage-specific checks to the controller and issue a redirect or just bump back the stage counter. This can get awfully messy, so if there''s major decision making that needs to be done, it might be better to switch to the one-action-per-stage method, and issue redirects if you need to go back a stage. Hell, you could even go really nuts and implement a flexible state machine, but that''s definitely only for particularly energetic programmers. - Matt