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:
- Validation error messages below the field they correspond to, by default. You can also put the error messages after the label, or turn off
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 here - Automatic mark-up for the
requiredattribute on required fields. - An easy way to consistently show help text on fields.
- Mark-up for Bootstrap horizontal forms (labels to the left of their fields, like a traditional desktop application), if that's what you want.
- Many options to modify or augment the generated mark-up.
- A way to escape to the Rails form helpers if you need to do something that
bootstrap_formcan't do.
Some other nice things that bootstrap_form does for you are:
- Reduces the amount of code in your
.erbfiles. - Gets you going faster with Bootstrap, because you don't need to learn all the rules of Bootstrap form mark-up to get started.
- Reduces errors, because you're doing less typing.
- Makes it easier to see the logic of the form, because it's not mixed in with the Bootstrap mark-up.
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:
- Ruby 3.2+ (https://www.ruby-lang.org/en/downloads/branches/)
- Rails 7.2+ (https://guides.rubyonrails.org/maintenance_policy.html)
- Bootstrap 5.0+
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">
<div class="mb-3">
<label class="form-label required" for="user_email">Email</label>
<input class="form-control" id="user_email" name="user[email]" required="required" type="email" value="steve@example.com">
</div>
<div class="mb-3">
<label class="form-label" for="user_password">Password</label>
<input class="form-control" id="user_password" name="user[password]" type="password">
</div>
<div class="form-check mb-3">
<input name="user[remember_me]" type="hidden" value="0">
<input class="form-check-input" id="user_remember_me" name="user[remember_me]" type="checkbox" value="1">
<label class="form-check-label" for="user_remember_me">Remember me</label>
</div>
<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: 'name@example.com' %>
<%= f.submit %>
<% end %>This generates:
<form accept-charset="UTF-8" action="/subscribe" method="post">
<div class="mb-3">
<label class="form-label" for="email">Email</label>
<input class="form-control" id="email" name="email" type="email" value="name@example.com">
</div
