我想为集合制作一个选择框。我使用方法options_for_select
但它只生成<option></option>
字段,而不是外部<select></select>
。
这是我的代码:
<% categories_array = Category.all.map{|category| [category.name, category.id]} %>
<%= options_for_select(categories_array) %>
结果如下:
<option value="5483c910485559047a000000">Programming</option>
<option value="5483c921485559047a010000">Business</option>
<option value="5483c92b485559047a020000">Game Programming</option>
但我期待:
<select id = "categoryId">
<option value="5483c910485559047a000000">Programming</option>
<option value="5483c921485559047a010000">Business</option>
<option value="5483c92b485559047a020000">Game Programming</option>
</select>
此外,我可以在将此表单发布到服务器时获得此值。我怎么能这样做
谢谢:)
答案 0 :(得分:2)
您可以在此处查看select和options帮助器的组合方式:
http://guides.rubyonrails.org/form_helpers.html#making-select-boxes-with-ease
如上所述:
<%= select_tag(:city_id, options_for_select(...)) %>