Bootstrap Forms is a nice Rails generator that makes working with Bootstrap (by Twitter) even easier on Rails.
Forms with Bootstrap are crowded with additional layout markup. While it's necessary, you shouldn't have to type it every time you create a form! That's why I created Bootstrap Forms.
There were major changes in the release of version 0.1.0
:
- The gem name has officially changed from
bootstrap-forms
tobootstrap_forms
to match gem naming conventions. The old gem still exists on rubygems for legacy applications, however, you should update to the new gem as quickly as possible. It's faster and more stable. The old gem is no longer maintained. form_for
is no longer overridden by default. There were multiple users who were concerned that this behavior was ill advised. Instead, a new form helper,bootstrap_form_for
has been created. This is in line with other form building libraries.- The gem is now a Rails 3 Engine. As such, Bootstrap Forms will not work in < Rails 3.0. The engine is automatically mounted when including the gem in your
Gemfile
.
Add it to your Gemfile
:
gem 'bootstrap_forms'
Don't forget to run the bundle
command. The gem will add 2 methods bootstrap_form_for
and bootstrap_fields_for
for use in your project. This is different from bootstrap_forms < 0.1.0
. In previous versions, the default form builders were overridden by default. With backlash from various community members, this is no longer the default.
Be sure to restart your Rails server after installing the gem.
With Bootstrap, you would need the following code for a form:
/ using haml
= form_for @model do |f|
.clearfix
%label MyLabel
.input
= f.text_area :field, :opts => {...}
<!-- using ERB -->
<%= form_for @model do |f| %>
<div class="clearfix">
<label>MyLabel</label>
<div class="input">
<%= f.text_area :field, :opts => {...} %>
</div>
</div>
<% end %>
Using Bootstrap Forms, this is much simpler:
/ using HAML
= bootstrap_form_for @model do |f|
= f.text_area :field, :opts => {...}
<!-- using ERB -->
<%= bootstrap_form_for @model do |f| %>
<%= f.text_area :field, :opts => {...} %>
<% end %>
The custom form builder will automatically wrap everything for you. This helps clean up your view layer significantly!
Just when you thought you were done... Bootstrap Forms includes additional form helpers that make life a lot easier! For example, the markup required for a list of checkboxes is quite cumbersome... well, it used to be.
collection_check_boxes
behaves very similarly to collection_select
:
= f.collection_check_boxes :category_ids, Category.all, :id, :name
See description above...
= f.collection_radio_buttons :primary_category_id, Category.all, :id, :name
Bootstrap Forms adds another helper method that generates the necessary markup for uneditable fields:
= f.uneditable_field :name
yields:
<div class="clearfix">
<label for="organization_name">Organization Name</label>
<div class="input">
<span class="uneditable-input">The Variety Hour</span>
</div>
</div>
Bootstrap Forms also adds a default actions panel when you call f.submit
:
= f.submit
generates:
<div class="actions">
<input type="submit" value="..." class="btn primary" />
<a href="..." class="btn">Cancel</a>
</div>
Pretty swell if you ask me.
You can add as many options to any form helper tag. If they are interpreted by Bootstrap Forms, they are interpreted and rendered in the output. If not, they are passed along as values to the final HTML form object.
Name | Description | Usage |
---|---|---|
help_inline | Add inline help text | = f.text_field :name, :help_inline => 'help me!' |
help_block | Add block help text (below) | = f.text_field :name, :help_block => 'help me!' |
error | Styles the field as error (red) | = f.text_field :name, :error => 'This is an error!' |
success | Styles the field as success (green) | = f.text_field :name, :success => 'This checked out OK' |
warning | Styles the field as warning (yellow) | = f.text_field :name, :warning => 'Take a look at this...' |
prepend | Adds special text to the front of the input | = f.text_field :name, :prepend => '@' |
append | Adds special text at the end of the input | = f.text_field :name, :append => '@' |
Copyright (c) 2012 Seth Vargo
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.