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 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.
Postgres is currently required. See Issue #1.
Carrierwave and Rmagick for file uploads. Geocoder to geocode address fields.
gem 'formbuilder-rb', require: 'formbuilder'
rake formbuilder:install:migrations
rake db:migrate
# responses :hstore
# responses_text :text
class Entry < ActiveRecord::Base
include Formbuilder::Entry
end
class MovieTheater < ActiveRecord::Base
has_one :form, as: :formable, class_name: 'Formbuilder::Form'
end
<%= Formbuilder::FormRenderer.new(@form, @entry).to_html %>
@entry = Entry.new(form: @form)
@entry.save_responses(params[:response_fields], @form.response_fields) # validates automatically
@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
MIT