Explain Codes LogoExplain Codes Logo

How do I set the HTML options for collection_select in Rails?

html
form-builders
accessibility
best-practices
Nikita BarsukovbyNikita Barsukov·Dec 2, 2024
TLDR

To customize HTML attributes using collection_select in Rails, use the html_options hash right after the options hash. To set a class and id, for instance:

<%= f.collection_select :value_method, @values, :value_method, :text_method, {}, { class: 'my-select', id: 'unique-id'} %>

The placeholders :value_method, @values, :value_method, and :text_method should be substituted with your details. The HTML attributes are placed here { class: 'my-select', id: 'unique-id' }.

Decoding collection_select

Understanding collection_select arguments

collection_select is an efficient Rails helper for creating standard dropdown menus. Here are the primary elements:

collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
  • object: The infallible hipster model your selection spins around.
  • method: The secret message in a bottle, carrying your selected value.
  • collection: An array of potential band members ready to grace the stage of your dropdown concert.
  • value_method: The magic spell each option whispers to set its value attribute.
  • text_method: The gossip that shapes each option's public image.

Adding default and prompt options

[code_joke_1] Here's a neat trick, if your options feels lonely, you can include keys like :prompt or :include_blank:

<%= f.collection_select :attribute, @collection, :value_method, :text_method, {prompt: 'Choose wisely, Padawan'}, { class: 'jedi-select' } %>

Adding a prompt gives your users a nudge in the right direction.

Boosting readability with explicit variables

[code_joke_2] Got loads of options in your bag? Alleviate the chaos by assigning explicit variables before they huddle into collection_select:

options = { include_blank: true } html_options = { class: 'my-select', id: 'unique-id' } <%= f.collection_select :attribute, @collection, :value_method, :text_method, options, html_options %>

Savor the peace in your code. Silence is golden, comments are platinum.

Leveraging data attributes for UI interactivity

Add some data-* attributes to mutate your dropdown into a transformer:

<%= f.collection_select :attribute, @collection, :value_method, :text_method, {}, { data: { toggle: 'dropdown' } } %>

The {data: { toggle: 'dropdown' }} can join forces with libraries like Bootstrap, to keep your UI game strong.

Enhancing accessibility with ARIA attribute

Here's an underrated personal favorite, accessibility:

<%= f.collection_select :attribute, @collection, :value_method, :text_method, {}, { 'aria-labelledby' => 'label-id' } %>

The { 'aria-labelledby' => 'label-id' } aids screen readers unravel the mystery of your select tag.

Integrating with form builders

Finally, in a nested form or with custom form builders, remember to keep this best buddy f around:

<%= f.collection_select :attribute, @collection, :value_method, :text_method, {include_blank: true}, { class: 'my-select' } %>

This ensures the music of your select tag syncs with the orchestra of form objects.