Pregunta

I'm trying to add a class to the select drop down populated using options_from_collection_for_select

Here's the snippet I'm working with:

<%= select_tag "instructor", options_from_collection_for_select(@instructors, "id", "full_name"), :include_blank => true %>

Where do html_options go in this instance?

Desired result:

<select id="example_select" name="example_select" class="example_class">
    <option value></option>
    <option value="1">Example option 1</option>
    <option value="2">Example option 2</option>
</select>

I've tried to follow syntax for options_for_select and have only found questions relating to that tag specifically; it doesn't work as I'd expect.

Any advice for this question, and for select tag templating questions like this in general would be appreciated.

¿Fue útil?

Solución

The answer is actually simple:

<%= select_tag "instructor", options_from_collection_for_select(@instructors, "id", "full_name"), :include_blank => true, :class => "example_class" %>

Will produce:

<select id="example_select" name="example_select" class="example_class">
    <option value></option>
    <option value="1">Example option 1</option>
    <option value="2">Example option 2</option>
</select>

Thanks Jesse Goodfellow

Reference: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-options_for_select

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top