Stepper

Stepper is multistep form (wizard) solution for Rails 3.1. Stepper allows you to split up your large form into series of pages that users can navigate through to complete the form and save it state.

Installation

You can use it with Rails 3.1:

gem install stepper

Getting Started

Configuring model

Create migration for model:

add_column :companies, :current_step, :string
add_index  :companies, :current_step

Setup names of steps that you want to have:

class Company < ActiveRecord::Base
  has_steps :steps => %w{ description kind address }
end

Setup validation for each step if necessary, method should have name like validate_#{step_name}:

def validate_description
  self.validates_presence_of :name
  self.validates_presence_of :desc
end

def validate_address
  self.validates_presence_of :city
  self.validates_presence_of :country
  self.validates_presence_of :address
end

def validate_kind
  self.validates_presence_of :kind
end

Now Your model ready for multistep form!

Configuring controller

Stepper use update, create, new and next_step actions.

For controller you need just add has_steps method:

class CompaniesController < ApplicationController
  has_steps
end

And you should have show action because stepper redirects to it when last step finished by default. For more options see method documentation.

Configuring view

Add stepper helper method into the form in view that rendered by new action:

<%= form_for(@company) do |f| %>
  <%= stepper f %>
<% end %>

stepper helper renders partial according to the current step of form. Partials should have name like #{step_name}_step:

name_step.html.erb

<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :desc %>
<%= f.text_field :desc %>

city_step.html.erb

<%= f.label :city %>
<%= f.text_field :city %>
<%= f.label :country %>
<%= f.text_field :country %>
<%= f.label :city %>
<%= f.text_field :city %>

kind_step.html.erb

<%= f.label :kind %>
<%= f.text_field :kind %>

stepper helper creates buttons “Next”, “Previous”, “Save” and “Finish” as well. You can change button names just add to your locales:

en:
  stepper:
    next_step: 'Next step'
    previous_step: 'Previous step'
    save: 'Finish later'
    finish: 'Finish'

next_step button validates, saves current step and renders next step of form; previous_step saves current step and renders previous step of form; save save current step and redirects to index page; finish is showed only for last step instead of next_step button and it validates, saves last step and redirects to show.

If you want to have other partial for buttons than add partial to: app/views/stepper/_fields.html.erb

Contributing to stepper

  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it

  • Fork the project

  • Start a feature/bugfix branch

  • Commit and push until you are happy with your contribution

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2011 Anton Versal. See LICENSE.txt for further details.