Bootstrap Forms =============== [![Build Status](https://secure.travis-ci.org/sethvargo/bootstrap_forms.png?branch=master)](http://travis-ci.org/sethvargo/bootstrap_forms) Bootstrap Forms is a nice Rails generator that makes working with [Bootstrap (by Twitter)](http://twitter.github.com/bootstrap) 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. Bootstrap 2.0 Compliant! ------------------------ A super special thanks to [vincenzor](https://github.com/vincenzor) for updating `bootstrap_forms` to comply with the new methods and features in Twitter Bootstrap 2.0. To get these new features, ensure you are using `bootstrap_forms ~> 2.0.0`. Note/Caution/Warning -------------------- There were **major** changes in the release of version `0.1.0`: 1. The gem name has officially changed from `bootstrap-forms` to `bootstrap_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. 2. `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. 3. 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`. Installation ------------ Add it to your `Gemfile`: gem 'bootstrap_forms' Don't forget to run the `bundle` command. The gem will add the method `bootstrap_form_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. Why? ---- With Bootstrap, you would need the following code for a form: ```haml / using HAML = form_for @model do |f| .clearfix %label MyLabel .input = f.text_area :field, :opts => {...} ``` Using Bootstrap Forms, this is **much** simpler: ```haml / using HAML = bootstrap_form_for @model do |f| = f.text_area :field, :opts => {...} ``` The custom form builder will automatically wrap everything for you. This helps clean up your view layer significantly! Additional Form Methods ----------------------- 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 `collection_check_boxes` behaves very similarly to `collection_select`: ```haml = f.collection_check_boxes :category_ids, Category.all, :id, :name ``` You can set the `inline` option to build inline checkboxes: ```haml = f.collection_check_boxes :category_ids, Category.all, :id, :name, :inline => true ``` ### collection_radio_buttons See description above... ```haml = f.collection_radio_buttons :primary_category_id, Category.all, :id, :name ``` You can set the `inline` option to build inline radios: ```haml = f.collection_radio_buttons :primary_category_id, Category.all, :id, :name, :inline => true ``` ### check_box Also supports `inline` option: ```haml = f.check_box :enabled, :inline => true ``` ### radio_buttons ```haml = f.radio_buttons :published, { "Published" => true, "Unpublished" => false } ``` You can set the `:inline` option to build inline radios. ```haml = f.radio_buttons :published, { "Published" => true, "Unpublished" => false }, { :inline => true } ``` You can set radio options by passing a hash instead of a value: ```haml = f.radio_buttons :published, { "Published" => true, "Unpublished" => {:value => false, :disabled => true} } ``` Ruby 1.8 doesn't guarantee hashes are ordered. If you care, pass in nested arrays or `ActiveSupport::OrderedHash`. Uneditable Input ---------------- Bootstrap Forms adds another helper method that generates the necessary markup for uneditable inputs: ```haml = f.uneditable_input :name ``` yields: ```html
Name | Description | Usage | Default |
---|---|---|---|
summary_errors | Show summary errors at the top | = bootstrap_form_for @thing, summary_errors: false | true |
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 => '@' |
append_button |
Adds the given button to the end of the input. The value is a hash. :label is the button label :icon adds an icon before the label :class has a default value of 'btn' :type has a default value of 'button' Any other entries are passed directly to Rails's tag helper. |
= f.text_field :name, :append_button => { :label => 'Button label', :icon => 'icon-plus', :type => 'button' } |
label | Customize the field's label. Pass false to have no label. | = f.text_field :name, :label => 'Other name' |
control_group | Pass false to remove the control group and controls HTML, leaving only the label and input. | = f.text_field :name, :control_group => false |
:required => false | Pass false to ignore presence validation checks that add a required attribute on the generated element. | = f.text_field :name, :required => false |