Awesome
Alert: This gem is used internally at DOBT, and might not be perfect for your implementation. We recommend forking this repository and using it as a starting point. It's hard to gem-ify this stuff without integrating it into your app completely.
Formbuilder.rb <a href='https://coveralls.io/r/dobtco/formbuilder-rb'><img src='https://coveralls.io/repos/dobtco/formbuilder-rb/badge.png' alt='Coverage Status' /></a>
Formbuilder.rb is a Rails Engine that's designed as a compliment to Formbuilder.js, a library that lets your users create their own webforms inside of your application.
Since Formbuilder.rb is a fairly non-trial piece of software, it's important to understand its components and how it works:
- We add
ResponseField
,EntryAttachment
, andForm
models to your application. (Each type of response field (text, checkboxes, dropdown, etc.) uses STI, inheriting from theResponseField
model.) - You
include Formbuilder::Entry
in an existing model. - We add a few classes to help you render forms and entries.
Note: All Formbuilder models and classes are namespaced within the Formbuilder
module.
If you have a few moments, consider reading the source, especially the Rails app in spec/dummy
, as it should give you a good idea of how Formbuilder integrates.
Requirements
Postgres is currently required. See Issue #1.
Carrierwave and Rmagick for file uploads. Geocoder to geocode address fields.
Installation
1) In your Gemfile
gem 'formbuilder-rb', require: 'formbuilder'
2) Create the migrations for the Formbuilder models
rake formbuilder:install:migrations
rake db:migrate
3) The Entry
model gets mixed in to an existing model in your application
# responses :hstore
# responses_text :text
class Entry < ActiveRecord::Base
include Formbuilder::Entry
end
4) Associate a form with an existing model (optional)
class MovieTheater < ActiveRecord::Base
has_one :form, as: :formable, class_name: 'Formbuilder::Form'
end
Usage
Render a form
<%= Formbuilder::FormRenderer.new(@form, @entry).to_html %>
Save an entry
@entry = Entry.new(form: @form)
@entry.save_responses(params[:response_fields], @form.response_fields) # validates automatically
Validate an entry
@entry.valid?
# => false
@entry.error_for(form.response_fields.first)
# => "can't be blank"
Integrate with the Formbuilder.js frontend
# config/routes.rb
resources :forms, only: [:update]
# app/controllers/forms_controller.rb
class FormsController < ApplicationController
include Formbuilder::Concerns::FormsController
end
License
MIT