Usage
<%= form_with url: root_path do |f| %>
<%= f.check_box :newsletter %>
<%= f.check_box :terms, checked: true %>
<%= f.check_boxes_collection(:country_id, countries, :id, :name) %>
<% end %>Props
| Prop | Type | Default | Description |
|---|---|---|---|
label | String | false | humanized attribute name | Custom label text. Set to `false` to hide the label. |
hint | String | nil | Helper text displayed below the checkbox. |
error | String | nil | Manual error message. ActiveRecord errors are displayed automatically. |
checked | Boolean | false | Pre-check the checkbox. |
disabled | Boolean | false | Disable the checkbox. |
Also accepts any HTML attributes via **options (e.g., id:, data:, aria:). class: is also supported for custom styling.
Examples
Basic Checkbox
<%= form_with url: root_path do |f| %>
<%= f.group class: "form__checkbox_collection" do %>
<%= f.check_box :newsletter %>
<%= f.check_box :free_shipping, checked: true %>
<% end %>
<% end %>CheckBox Collection
<%= form_with url: root_path do |f| %>
<%= f.check_boxes_collection(:country_id, ISO3166::Country.find_all_countries_by_subregion("Northern America"), :alpha2, :common_name) %>
<% end %>Label customization
<%= form_with url: root_path do |f| %>
<%= f.group class: "form__checkbox_collection" do %>
<%= f.check_box :custom_label, label: "Custom Label Text" %>
<%= f.check_box :with_link, label: "I agree to the #{link_to 'terms and conditions', '#', class: 'link'}".html_safe %>
<%= f.check_box :default_label %>
<% end %>
<% end %>Hint messages
<%= form_with url: root_path do |f| %>
<%= f.check_box :receive_mails, hint: "Receive marketing emails" %>
<% end %>Disabled State
<%= form_with url: root_path do |f| %>
<%= f.group class: "form__checkbox_collection" do %>
<%= f.check_box :disabled_checkbox, disabled: true %>
<%= f.check_box :disabled_checkbox_checked, checked: true, disabled: true %>
<% end %>
<% end %>Validation
<% user = User.new %>
<% user.errors.add(:terms_and_conditions, "is required") %>
<%= form_with model: user, url: root_path do |f| %>
<%= f.check_box :terms_and_conditions, label: "I agree to the terms and conditions" %>
<%= f.check_box :custom_error, error: "Custom error" %>
<% end %>