Forum: Ruby on Rails Looking for helpers/components/controllers for form wizards

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
548240c1431285c235ecf711293a7311?d=identicon&s=25 Yash (Guest)
on 2006-04-02 08:15
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
55428cbf149e35dd4b65f1d019d04139?d=identicon&s=25 Matthew Palmer (Guest)
on 2006-04-02 09:19
(Received via mailing list)
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
This topic is locked and can not be replied to.