A Rails plugin to create multi page form
Ruby
Switch branches/tags
Nothing to show
Pull request Compare This branch is 2 commits ahead, 3 commits behind adkron:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
tasks
test
MIT-LICENSE
README
Rakefile
init.rb
install.rb
uninstall.rb

README

sorcerer
============
Helper plugin for building a multi pages form that is typical of "wizards form".

Get Start
=========
1. generate a model for managing multi pages.
2. append back and forth button instead of submit button on view template
3. create some replacing subpages in template.
4. append switching code on controller

1. generate a model for managing multi pages.
  It have a page field and some foreign keys. And add multi page difinition.

    # db/xxxx_create_article_wizards.rb
    create_table :article_wizards do |t|
      t.integer :page
      t.integer :article_id
      t.integer :author_id
    end

    # app/model/article_wizard.rb
    class ArticleWizard < ActiveRecord::Base
      belongs_to :article
      belongs_to :author
      accepts_nested_attributes_for :article
      accepts_nested_attributes_for :author

      acts_as_wizard "p1", "p2", "p3"
    end

2. append back and forth button instead of submit button on view template

    # app/view/article_wizard.html.erb
    <h1>New article_wizard</h1>
    
    <% form_for(@article_wizard) do |f| %>
      <%= f.error_messages %>

      <!-- mutli page enabled -->
      <fieldset> 
        <%= render_wizard_partial @article_wizard, f %> 
      </fieldset>

      <%= f.hidden_field :page %>
      <table class="controls"> 
        <tr> 
          <td>
            <%= previous_wizard_button( @article_wizard )  %>
          </td> 
          <td> 
            <%= next_wizard_button( @article_wizard ) %> 
          </td> 
        </tr> 
      </table> 
      <!-- /mutli page enabled -->
    <% end %>

3. create some replacing subpages in template.

    # app/view/article_wizard/sub_pages/_p1.html.erb
    <h2>page 1<h2>
    <% main_form.fields_for :article do |f| %>
        <%= f.label :title %>
        <%= f.text_field :title %>
    <% end %>

    # app/view/article_wizard/sub_pages/_p1_hidden.html.erb
    <% main_form.fields_for :article do |f| %>
        <%= f.hidden_field :title %>
    <% end %>

    # app/view/article_wizard/sub_pages/_p2.html.erb
    <h2>page 2<h2>

    # app/view/article_wizard/sub_pages/_p3.html.erb
    <h2>page 3<h2>

4. append switching code on controller

    # app/controllers/article_wizards_controller.rb
    def new
      @article_wizard = ArticleWizard.new
      @article_wizard.article = Article.new
  
      respond_to do |format|
        format.html # new.html.erb
        format.xml  { render :xml => @article_wizard }
      end
    end

    def create
      @article_wizard = ArticleWizard.new(params[:article_wizard])
      @article_wizard.article ||= Article.new
      @wizard = @article_wizard
  
      respond_to do |format|
        format.html {
          if params[:direction] == "next!" 
            success = @wizard && @wizard.valid?
            if success
              if @wizard.next_page.nil?
                @wizard.save!
                redirect_to(@wizard) and return
              else
                @wizard.switch_wizard_page(params[:direction])
              end
            else
            end
          else
            @wizard.switch_wizard_page(params[:direction])
          end
          render :new
        }
  
        format.xml  { 
          if @article_wizard.save
            flash[:notice] = 'ArticleWizard was successfully created.'
            render :xml => @article_wizard, :status => :created, :location => @article_wizard
          else
            render :xml => @article_wizard.errors, :status => :unprocessable_entity
          end
        }
      end
    end

Thank You
=========
Amos L. King for a original acts_as_wizard plugin.

Copyright (c) 2008 Amos L. King, released under the MIT license
Copyright (c) 2009 Beyond, released under the MIT license