简体   繁体   中英

How to add a class on an option tag that belongs to a select tag in rails?

I am trying to add a class to the option tag generated by the following code:

=f.datetime_select :clock_out, {class: 'option-large'}

I am not sure how to get this to work. Does this helper provide a way to assign a class to the option tags generated?

What about options_for_select ? You need to generate yourself the options as well but you can set html attributes too.

So it would be something like

select_options = options.map do |option|
                   [option.display_value,   # <option>this</option>
                    option.value,           # <option value="this"></option>
                    option.html_attributes] # a hash here <option class="option"></option>
                 end
=f.datetime_select :clock_out,
                   options_for_select(select_options,
                                      :selected => _your_default_value_)
                   {class: 'option-large'}

You could otherwise add a class using Ruby/Javascript once the HTML code is generated.
In Ruby (server side), you could store the result of datetime_select and gsub it.
In Javascript (client side), you select your options and add class to the one you want.

from ruby api:

datetime_select(object_name, method, options = {}, html_options = {})

so:

html_options = {:class => "option-large"}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM