Skip to content

JuddL333/bolero

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bolero: Multistep Forms for Rails

Bolero is a new way to implement Multistep or "Wizard"-type forms and workflows in Rails. The goal of Bolero is to keep your controllers simple, to avoid storing partially created objects in your database, and to make it easy to make changes to the order or the contents of various steps of your forms.

Getting Started

Install the Gem

Add this to your Gemfile

gem "bolero", github: "bitpeel/bolero"

Then run bundle install to install the gem.

Run the generator and migrate your database

rails g bolero:install
rake db:migrate

Define your routes

Define your routes in routes.rb using any convention you like.

...
namespace :signup do
  get "username", to: "username#new"
  post "username", to: "username#create"

  get "details", to: "details#new"
  post "details", to: "details#create"
  
  ...
end
...

Create your controllers

# app/controllers/signup/username_controller.rb

class Signup::UsernameController < ApplicationController
  def new
    @step = UsernameStep.new(session: session)
  end
  
  def create
    @step = UsernameStep.new(session: session)
    @step.assign_attributes(username_params)
    
    if @step.save
      redirect_to @step.next_step_path
    else
      render :new
    end
  end
  
  private
    def username_params
      params.require(:username_step).permit(:username, :email, :password)
    end
end

Create your views

# app/views/signup/username/new.html.haml

  <%= simple_form_for @step, url: signup_username_path, method: :post do |f| %>
    <%= f.input :username %>
    <%= f.input :password %>
    <%= f.input :email %>
    <%= f.button :submit %>
  <% end %>

Create your step

# app/multisteps/signup/username_step.rb

class Signup::UsernameStep
  include Bolero::Step
  
  attr_bolero_accessor :username, :password, :email
  
  validates :username, presence: true, length: { minimum: 4, maximum: 30 }
  validates :email, presence: true
  validates :password, presence: true
  
  def path
    url_helpers.signup_username_path
  end

  def next_step_path
    url_helpers.signup_details_path
  end
end

Viewing Persisted Steps

  Bolero::PersistedStep.all

About

Multistep Forms in Rails

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%