Primer Form Builder

Build forms with Primer markup using Rails form builders.


Add this line to your application's Gemfile:

gem 'primer_form_builder'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install primer_form_builder


Tell form_with to use PrimerFormBuilder::Builder when building a form:

<%= form_with model: @user, builder: PrimerFormBuilder::Builder do |form| %>
  <%= :username do %>
    <%= form.label :username %>
    <div class="form-group-body">
      <%= form.text_field :username %>
  <% end %>
<% end %>

This will generate the following HTML:

<form action="/users/1" accept-charset="UTF-8" method="post">
  <input type="hidden" name="_method" value="patch" autocomplete="off">
  <input type="hidden" name="authenticity_token" value="[redacted]" autocomplete="off">

  <div class="form-group">
    <div class="form-group-header">
      <label for="user_username">Username</label>
    <div class="form-group-body">
      <input class="form-control" type="text" value="my_user" name="user[username]" id="user_username" />

The current supported methods are:

  • label
    • sr_label (generates a <label> tag with a class of .sr-only)
  • text_field
  • email_field
  • url_field
  • number_field
  • telephone_field
  • password_field
  • text_area
  • datetime_select
  • check_box_single (generates a check box wrapped in a div.form-checkbox)
  • note (generates p.note)
    • error_note (as above, but p.note.error)
  • button
  • submit
  • group (generates div.form-group)

If you use a method not in the above list, it will pass back to the default Action View FormBuilder.

Validation errors

If your form's model responds to errors, a will have the class .errored applied, and an error note (p.note.error) containing the error message will be appended to the form group.


After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.


The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the PrimerFormBuilder project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.