Choices

Enhanced select field with search functionality, multiple selection support, and custom options using Choices.js library.

Usage

<%= form_with url: root_path do |f| %>
  <%= f.choices :state, options_for_select([["California", "CA"], ["New York", "NY"]]), { include_blank: "Select..." } %>
  <%= f.choices :languages, ["English", "Spanish"], {}, { multiple: true } %>
<% end %>
Props
PropTypeDefaultDescription
sizeSymbol
:sm | :md | :lg
:mdSize of the select field.
multipleBooleanfalseEnable multiple selection.
labelString | falsehumanized attribute nameCustom label text. Set to `false` to hide the label.
hintStringnilHelper text displayed below the field.
errorStringnilManual error message. ActiveRecord errors are displayed automatically.
disabledBooleanfalseDisable the field.
data-searchStringnilURL for async search functionality.
data-search_enabledBooleanfalseEnable local search within options.
data-max_item_countIntegernilMaximum number of items that can be selected (multiple mode).

Also accepts any HTML attributes via **options (e.g., id:, data:, aria:). class: is also supported for custom styling.

Examples

Basic Choices

<%= form_with url: root_path do |f| %>
  <%= f.choices :state, options_for_select([["California", "CA"], ["New York", "NY"], ["Texas", "TX"]]), { include_blank: "Select state..." } %>
<% end %>

Choices sizes

<%= form_with url: root_path do |f| %>
  <%= f.choices :large_field, ["Yes", "No"], { }, { size: :lg } %>
  <%= f.choices :default_field, ["Yes", "No"], { } %>
  <%= f.choices :small_field, ["Yes", "No"], { }, { size: :sm } %>
<% end %>

Multiple Selection

Max 2 items

<%= form_with url: root_path do |f| %>
  <%= f.choices :languages, ["English", "Spanish", "French", "German", "Italian"], { hint: "Choose languages you speak" }, { multiple: true } %>
  <%= f.choices :skills, ["JavaScript", "Ruby", "Python", "Go", "Rust"], {}, { multiple: true, data: { max_item_count: 2 }, hint: "Max 2 items" } %>
<% end %>

Async Search

<%= form_with url: root_path do |f| %>
  <%= f.choices :country, [], { include_blank: "Select country..." }, { data: { search: countries_path(format: :json) } } %>
<% end %>

With Search Field

<%= form_with url: root_path do |f| %>
  <%= f.choices :gender, ["Male", "Female", "Other"], {}, { data: { search_enabled: true } } %>
<% end %>

Label customization

<%= form_with url: root_path do |f| %>
  <%= f.choices :custom_label, ["Option 1", "Option 2"], { include_blank: "Select option..." }, { label: "Custom Label Text" } %>
  <%= f.choices :no_label, ["Yes", "No"], {}, { label: false } %>
<% end %>

Hint message

Select an option

<%= form_with url: root_path do |f| %>
  <%= f.choices :with_hint, ["Yes", "No"], {}, { hint: "Select an option" } %>
<% end %>

Disabled State

<%= form_with url: root_path do |f| %>
  <%= f.choices :disabled_field, ["Option 1", "Option 2"], { selected: "Option 1" }, { disabled: true } %>
<% end %>

Validation

Is required

This field has a custom error

<% user = User.new %>
<% user.errors.add(:state, "is required") %>
<%= form_with model: user, url: root_path do |f| %>
  <%= f.choices :state, options_for_select([["California", "CA"], ["New York", "NY"], ["Texas", "TX"]]), { include_blank: "Select state..." } %>
  <%= f.choices :state, options_for_select([["California", "CA"], ["New York", "NY"], ["Texas", "TX"]]), { include_blank: "Select state..." }, { error: "This field has a custom error" } %>
<% end %>