Giter VIP home page Giter VIP logo

bolero's Introduction

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

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_path
    else
      render :new
    end
  end
  
  private
    def username_params
      params.require(:username_step).permit(:username, :email, :password)
    end
end

Create your views

  <%= simple_form_for @step, url: signup_username_path, method: :post do |f| %>
    <%= f.input :username %>
    <%= f.input :password %>
    <%= f.input :email %>
  <% 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 }
  validate :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

bolero's People

Contributors

makaio avatar juddl333 avatar

Stargazers

Matías Mascazzini avatar  avatar  avatar

Watchers

 avatar  avatar James Cloos avatar Veasna Char avatar

Forkers

juddl333

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.