我想使用 rails collection_select
创建一个选择标签或 options_for_select
使用不是基于模型的数组项。选项的值应该是每个项目的数组索引。
示例:['first', 'second', 'third']
的期望输出:
<select id="obj_test" name="obj[test]">
<option value="0">first</option>
<option value="1">second</option>
<option value="2">third</option>
</select>
最佳答案
如果 the_array = ['first', 'second', 'third']
,您也可以将其作为一行执行
<%= select "obj", "test", the_array.each_with_index.map {|name, index| [name,index]} %>
我早在 Rails 2.3.14 就已经测试过了。
关于ruby-on-rails - 在使用 rails 的选择中将选项值设置为数组索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4007615/