Adding css class to option tags generated by collection_select?

I know that passing :class => “foo” in the html_options argumnet to
collection_select will generate a select tag with a class assigned to
it (e.g., )…but I really need my options tags to
have a class of their own which I can determine using live data (e.g.,
style the options in different colors determined by state at
runtime).

I’m doing this manually now but am hoping there’s a more Rails way to
do this.

1 Like

I found this post from 2007! 15 years ago someone was asking the same question I am asking today, and didn’t get an answer. This is the only article I’ve found which asks:

How can I add a class to each option generated by collection_select

I have tried variations of

things = collection_select :page, :thing_ids,
                                              things,
                                              :id,
                                             :as_selection_item,  {class: :status} ,
                                              { multiple: true }

where thing.as_selection_item concats a few key values from a thing.

I really want a way to signal the status of each item without cluttering the text. I could add an icon, I suppose, but I would normally do that based on the class anyway.