我如何使用' optgroup' f.select中的html标签?
在搜索表单中,我有以下字段:
<%= f.select :location, ["San Francisco, USA", "Mountain View, USA", "London, UK", "Stockholm, Sweden"] %>
如何在其中添加optgroup,因此它会变成:
<select>
<optgroup label="Europe">
<option value="London, UK">London, UK</option>
<option value="Stockholm, Sweden">Stockholm, Sweden</option>
</optgroup>
<optgroup label="USA">
<option value="San Francisco, USA">San Francisco, USA</option>
<option value="Mountain View, USA">Mountain View, USA</option>
</optgroup>
</select>
答案 0 :(得分:2)
试试这个:
<%= grouped_options = [['Europe',["London, UK", "Stockholm, Sweden"]], ['USA', ["San Francisco, USA", "Mountain View"]]] %>
<%= f.select :location, grouped_options_for_select(grouped_options) %>