Official repository of the bootstrap_form gem, a Rails form builder that makes it super easy to create beautiful-looking forms using Bootstrap 5.
Official repository of the bootstrap_form gem, a Rails form builder that makes it super easy to create beautiful-looking forms using Bootstrap 5.
bootstrap_form is a Rails form builder that makes it super easy to integrate Bootstrap v5-style forms into your Rails application. It provides form helpers that augment the Rails form helpers. bootstrap_forms's form helpers generate the form field and its label and all the Bootstrap mark-up required for proper Bootstrap display. bootstrap_form also provides:
bootstrap_form's validation error handling and do it yourself. Note that this applies to Rails-generated validation messages. HTML 5 client-side validation and Rails validation out of the box don't really work well together. One discussion of the challenges and some solutions is hererequired attribute on required fields.bootstrap_form can't do.Some other nice things that bootstrap_form does for you are:
.erb files.bootstrap_form works like the standard Rails form helpers, and this README assumes you know how they work. You start a form with one of bootstrap_form_with, bootstrap_form_for, or bootstrap_form_tag in a view file. You get a form builder that calls the bootstrap_form helpers instead of the standard Rails helpers. You use that form builder in the view file to render one or more form fields.
bootstrap_form supports at a minimum the currently supported versions of Ruby and Rails:
This gem needs Bootstrap. The gem itself doesn't really care how Bootstrap is included in your application. There are many ways to install Bootstrap in a Rails application. Covering them all is beyond the scope of this README. However, here are some hints for a couple of common options.
If you're creating a new Rails app, create it with at least the following options:
rails new --javascript=esbuild --css=bootstrap <application-name>esbuild can be any of the options, except the default importmaps.
If you have an existing project, and it's processing CSS and JavaScript with some sort of front-end preprocessor, then install Bootstrap for your front-end preprocessor. That's probably something like this:
yarn add bootstrap popper.jsYou can use this gem with other ways of installing Bootstrap, but how to do so is outside the scope of this README.
Once Bootstrap is installed, add the bootstrap_form gem to your Gemfile:
gem "bootstrap_form", "~> 5.6"Then:
bundle install
bootstrap_form uses a very small number of its own CSS styles. These styles are used in HTML generated by the error_summary and alert_message error helpers, and the date_select, time_select, and datetime_select helpers. If you're not using those helpers, you don't need to install the bootstrap_form CSS styles.
If you do need the CSS styles, add them to your CSS bundle (usually your application.scss file). The way to do this depends on whether you're using Propshaft (the Rails 8 default), or Sprockets (pre-Rails 8). (Check your Gemfile to see whether you're using sprockets-rails or propshaft.)
If you're using Propshaft, add the styles to your CSS bundle like this:
@use "rails_bootstrap_forms";If you're using Sprockets, add the styles to your CSS bundle like this:
@import "rails_bootstrap_forms.css";If you're using Sprockets, you can also consider using the bootstrap gem to your Gemfile, as a way of installing Bootstrap itself. However, we haven't used this approach for a few years. Please report an issue if it doesn't work:
gem "bootstrap", "~> 5.0"And follow the remaining instructions in the official bootstrap installation guide for setting up application.scss and application.js.
To get started, use the bootstrap_form_for helper in place of the Rails form_for helper. Here's an example:
<%= bootstrap_form_for(@user) do |f| %>
<%= f.email_field :email %>
<%= f.password_field :password %>
<%= f.check_box :remember_me %>
<%= f.submit "Log In" %>
<% end %>This generates the following HTML:
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
<input class="btn btn-secondary" data-disable-with="Log In" name="commit" type="submit" value="Log In">
</form>Note: All examples in this README are generated with the configuration option group_around_collections set to true. See the Configuration section.
If your form is not backed by a model, use the bootstrap_form_tag. Usage of this helper is the same as bootstrap_form_for, except no model object is passed in as the first argument. Here's an example:
<%= bootstrap_form_tag url: '/subscribe' do |f| %>
<%= f.email_field :email, value: '[email protected]' %>
<%= f.submit %>
<% end %>This generates:
<form accept-charset="UTF-8" action="/subscribe" method="post">
<input class="btn btn-secondary" data-disable-with="Save " name="commit" type="submit" value="Save ">
</form>To get started, just use the bootstrap_form_with helper in place of form_with. Here's an example:
<%= bootstrap_form_with(model: @user, local: true) do |f| %>
<%= f.email_field :email %>
<%= f.password_field :password, help: 'A good password should be at least six characters long' %>
<%= f.check_box :remember_me %>
<%= f.submit "Log In" %>
<% end %>This generates:
<form accept-charset="UTF-8" action="/users" method="post">
<input class="btn btn-secondary" data-disable-with="Log In" name="commit" type="submit" value="Log In">
</form>bootstrap_form_with supports both the model: and url: use cases
in form_with.
form_with has some important differences compared to form_for and form_tag, and these differences apply to bootstrap_form_with. A good summary of the differences can be found at: https://m.patrikonrails.com/rails-5-1s-form-with-vs-old-form-helpers-3a5f72a8c78a, or in the Rails documentation.
Adding fields for a different object without nesting can be achieved using the bootstrap_fields_for and
bootstrap_fields helpers in the same way it is done in Rails.
<%= bootstrap_form_with model: @user do |f| %>
<%= f.email_field :email %>
<%= bootstrap_fields_for :parent do |pf| %>
<%= pf.email_field :email, label: 'Parent email' %>
<% end %>
<%= bootstrap_fields @user.address do |af| %>
<%= af.text_field :street_name %>
<% end %>
<%= f.primary "Save" %>
<% end %>Generated HTML:
<form accept-charset="UTF-8" action="/users" method="post">
<input class="btn btn-primary" data-disable-with="Save" name="commit" type="submit" value="Save">
</form>bootstrap_form can be used out-of-the-box without any configuration. However, bootstrap_form does have an optional configuration file at config/initializers/bootstrap_form.rb for setting options that affect all generated forms in an application, or for enabling or disabling new functionality that might not be compatible with your application.
The current configuration options are:
| Option | Default value | Description |
|---|---|---|
default_form_attributes |
{} | bootstrap_form versions 3 and 4 added a role="form" attribute to all forms. The W3C validator will raise a warning on forms with a role="form" attribute. bootstrap_form version 5 drops this attribute by default. Set this option to { role: "form" } to make forms non-compliant with W3C, but generate the role="form" attribute like bootstrap_form versions 3 and 4. |
group_around_collections |
false | Historically, bootstrap_form generated a wrapper around collection_checkboxes and collection_radio_buttons using the same form_group as individual controls used. This markup caused accessibility problems. Setting group_around_collections = true will generate collections of checkboxes and radio buttons wrapper in a <fieldset> with the text as a <legend> (https://www.w3.org/WAI/tutorials/forms/grouping/). This will make visible changes to pages that use the collection methods.The default for this option will be changed to true in a future version. |
Example:
# config/initializers/bootstrap_form.rb
BootstrapForm.configure do |c|
c.default_form_attributes = { role: "form" } # to make forms non-compliant with W3C.
endbootstrap_form provides its own version of the following Rails form helpers:
…By default, the helpers generate a label tag, and an input, select, or textarea tag, by calling the Rails label helper, and then the Rails helper with the same name as the bootstrap_form helper.
The bootstrap_form helpers accept the same options as the standard Rails form helpers, and pass those options through to the Rails helper. They also accept additional options, described in the following section.
Many of the helpers accept the same options. The exceptions are:
button, check_box, collection_check_boxes, collection_radio_buttons, collection_select, date_select, datetime_select, file_field, grouped_collection_select, hidden_field, radio_button, rich_text_area, select, submit, time_select, time_zone_select
The options for the form helpers that aren't in the exceptions list are described in the following sub-sections:
Use the label option if you want to specify the field's label text:
<%= f.password_field :password_confirmation, label: "Confirm Password" %>This generates:
To hide a label, use the hide_label: true option. This adds the visually-hidden
class, which keeps your labels accessible to those using screen readers.
<%= f.text_area :comment, hide_label: true, placeholder: "Leave a comment..." %>This generates:
To add custom classes to the field's label:
<%= f.emaiNo open issues yet, or sync has not completed.