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
@stage = 1
session[:wizard_data] = new Wizard # Or whatever model is
storing your stuff
@stage = params[:stage].to_i
@next_stage = @stage + 1
@wizard_data = session[:wizard_data]
render :template => 'stage' + @stage.to_s
Then each view template gets named app/views/wizard/stageN.rhtml, and
to use <%= start_form_tag :action => ‘wizard’, :stage => @next_stage %>.
Since you’ll want to use layouts to make everything look consistent, you
put that tag line into the layout and then you’re sorted. Each stage
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
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
one-action-per-stage method, and issue redirects if you need to go back
Hell, you could even go really nuts and implement a flexible state
but that’s definitely only for particularly energetic programmers.