How do I dynamically generate a unique name for each option within an options_from_collection_for_select?

我怕爱的太早我们不能终老 提交于 2019-12-24 12:28:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!