问题
I want to generate an option tag like this:
<option name="option_1" value="1">Small</option>
<option name="option_2" value="2">Medium</option>
Using my call like this:
<%= select_tag option.name, options_from_collection_for_select(option.option_values, "id", "name"), include_blank: true %>
Gets me half-way there, by generating this:
<option value="1">Small</option>
<option value="2">Medium</option>
How do I add the name
attribute to the option tag, using this structure? Is that even possible?
回答1:
select_tag option.name, options_for_select(option.option_values.map{ |o| [o.name, o.id, {:name_or_smth_else => "option_#{o.id}"}] })
options_for_select
回答2:
The option tag doesn't have a name
attribute. The name
attribute should be defined in the select
tag:
<select name="name">
<option value="1">Small</option>
<option value="2">Medium</option>
</select>
See this page for more info - http://www.w3schools.com/tags/tag_option.asp
来源:https://stackoverflow.com/questions/15382088/how-do-i-dynamically-generate-a-unique-name-for-each-option-within-an-options-fr