Select Options
:single_select and :multi_select form fields get their options in one of four ways: a static collection, the model's enum, a helper_method, or fetch_config β a model scope queried as the user types, for lists too large to render up front.
Quick exampleβ
section "Mode Of Transport" do
form_field :status, input_type: :single_select, collection: [["Vehicle", "vehicle"], ["Bike", "bike"]]
form_field :transport_type_id, input_type: :single_select, helper_method: :transport_type_options
form_field :route_id, input_type: :single_select,
fetch_config: { model: "Route", scope_name: "catalyst_search" }
end
def transport_type_options(_record, _field_name)
Constant.where(type: "transport_type").map { |item| [item.full_name, item.id] }
end
Optionsβ
The first match wins:
| Source | Option | Format |
|---|---|---|
| Remote scope | fetch_config: {...} | Options fetched remotely as the user types. See Remote options. |
| Helper method | helper_method: :name | Method in Catalyst::CustomHelper called with (record, field_name); returns [[label, value], ...]. |
| Static list | collection: [...] | [[label, value], ...] or [value, ...]. |
| Enum | none | Used automatically when the field is an enum: [[key.titleize, key], ...]. |
Remote optionsβ
fetch_config loads options remotely, keeping the list out of the page. The select queries Catalyst's fetch_options endpoint with what the user typed and loads results in pages of 10. The config travels as a signed payload, so only the declared model, scope and methods are reachable.
fetch_config works the same way on :single_select and :multi_select filters.
Just a model nameβ
model: is the only required key β scope_name defaults to "all", so this offers every User:
form_field :user_id, input_type: :single_select, fetch_config: { model: "User" }
Typing narrows the list through User's index :search filters (see How search is applied) β if the model has none, every option shows regardless of what is typed. The model must define ct_display_name for the option labels β or set title_method.
Config keysβ
| Key | Type | Default | Description |
|---|---|---|---|
model | String | required | Model that provides the options. |
scope_name | Symbol | "all" | Scope called on the model. |
dependent_fields | Array | [] | Other fields or filters whose current values are passed to the scope. |
title_method | Symbol | :ct_display_name | Method called on each record for the option label. |
value_method | Symbol | :id | Method called on each record for the option value. |
override_search | Boolean | false | Skip the model's index :search filters and let the scope own searching. |
What the scope receivesβ
The scope is called as Model.<scope_name>(**args) with these keywords:
| Keyword | On forms | On filters | Value |
|---|---|---|---|
search: | yes | yes | What the user typed β "" on first open. |
<dependent name>: | yes | yes | One keyword per dependent_fields entry: the other control's current value. Always sent, nil when empty. |
object_class: | yes | β | Class name of the record the form edits ("Student", or the nested record's class inside nested_form_field). |
object_id: | yes | β | The record's id. Only sent when the record is persisted β give it a nil default so new forms work too. |
- A dependent value arrives as a scalar for one selection or an array for a
:multi_selectβ useArray()to normalize. - Declare every keyword the config can send (
->(search:, brand_id: nil, object_class: nil, object_id: nil)) or catch all of them (->(**args)). A scope that rejects any keyword is called with no arguments β it still works, but losessearch:too. A scope taking one positional argument receives the whole hash. object_class:/object_id:let a scope load the record being edited and tailor options to it β see Scope options to the record being edited.
scope :catalyst_search, ->(search:, ct_role_id: nil, object_class: nil, object_id: nil) {
records = where("first_name ILIKE :q OR last_name ILIKE :q OR email ILIKE :q", q: "%#{search}%")
records = records.where(ct_role_id:) if ct_role_id.present?
records
}
Dependent fieldsβ
dependent_fields wires one control to another: when the named control's value changes, this select clears its selection and re-queries the scope with the new value.
- On a
form_fieldit names siblingform_fields of the same object. - On a
filterit names other filters'db_column_names β see Remote options. - Inside
nested_form_field, prefix a name withparent.to read a field from the parent form. The scope receives it with dots flattened to underscores (parent.vendor_idβparent_vendor_id:).
How search is appliedβ
Typed text reaches the records in two places:
- The scope gets it in
search:β handle it there or ignore it. - The results are then narrowed through the fetch model's
index:searchfilters β the same columns its index search uses. A scope that ignoressearch:(-> { order(:name) }) still becomes searchable when the model has a:searchfilter.
Set override_search: true to skip step 2 β the scope alone decides what matches. See Own the search in the scope.
Labels for already-selected values are looked up with value_method when it names a database column (Model.where(value_method => ids)); when it doesn't, collection or helper_method supplies them.
Recipesβ
Refill options when another field changesβ
section "Vehicle" do
form_field :brand_id, input_type: :single_select, fetch_config: { model: "Brand" }
form_field :product_id, input_type: :single_select,
fetch_config: { model: "Product", scope_name: "catalyst_search",
dependent_fields: [:brand_id] }
end
scope :catalyst_search, ->(search:, brand_id: nil) {
products = where("name ILIKE ?", "%#{search}%")
brand_id.present? ? products.where(brand_id:) : products.none
}
Changing the brand clears the product selection and reloads the list β products.none keeps it empty until a brand is picked.
Depend on a parent field inside nested_form_fieldβ
Inside a nested form, dependent_fields resolves against the nested row β prefix with parent. to read a field of the record the form edits. Here each order line's products are limited to the order's chosen vendor:
section "Order" do
form_field :vendor_id, input_type: :single_select, fetch_config: { model: "Vendor" }
nested_form_field :line_items do
form_field :product_id, input_type: :single_select,
fetch_config: { model: "Product", scope_name: "catalyst_search",
dependent_fields: ["parent.vendor_id"] }
form_field :quantity
end
end
scope :catalyst_search, ->(search:, parent_vendor_id: nil) {
products = where("name ILIKE ?", "%#{search}%")
parent_vendor_id.present? ? products.where(vendor_id: parent_vendor_id) : products
}
Own the search in the scopeβ
override_search: true skips the index :search filters so the scope alone decides what the typed text matches β for example when it searches an associated table:
form_field :assignee_id, input_type: :single_select,
fetch_config: { model: "User", scope_name: "catalyst_search_assignable", override_search: true }
scope :catalyst_search_assignable, ->(search:) {
joins(:profile).where(active: true)
.where("profiles.display_name ILIKE :q OR users.email ILIKE :q", q: "%#{search}%")
}
Scope options to the record being editedβ
On forms the scope also receives object_class: and object_id: β the class and id of the record the form edits (object_id is absent on new). Load the record to tailor options β here a shipment's hubs are limited to its own region:
form_field :hub_id, input_type: :single_select,
fetch_config: { model: "Hub", scope_name: "catalyst_search" }
scope :catalyst_search, ->(search:, object_class: nil, object_id: nil) {
hubs = where("name ILIKE ?", "%#{search}%")
if object_class == "Shipment" && object_id.present?
hubs = hubs.where(region_id: ::Shipment.find(object_id).region_id)
end
hubs
}
Custom labels and valuesβ
title_method and value_method pick what shows in the option and what is submitted:
form_field :country_id, input_type: :single_select,
fetch_config: { model: "Country", scope_name: "catalyst_search",
title_method: :name_with_flag }
def name_with_flag = "#{flag_emoji} #{name}"
Keep value_method on a database column (like the default :id) β selected values are re-fetched with Model.where(value_method => ids) to render their labels. With a non-column method, existing selections can't resolve their label; add collection or helper_method as the fallback.
Different options per recordβ
Pass a lambda instead of a hash β it receives the form's record and returns the config (or nil to fall back to helper_method/collection):
form_field :category_id, input_type: :single_select,
fetch_config: ->(record) { { model: "Category", scope_name: record.published? ? "published_categories" : "catalyst_search" } }