ruby-on-rails - select_tag 正在排序(奇怪地)[Rails]

标签 ruby-on-rails html-select

我有一个看起来像这样的选择框(在 form_for 中)

  <%=f.select(:whatever_id, {"blah"=>0, "blah2"=>1, "blah3"=>2, "blah4"=>3}, {:include_blank => true}) %>

输出很好,但是很奇怪……像这样:

<select id="personal_information_whatever_id" name="personal_information[whatever_id]"><option value=""></option>
<option value="1">blah2</option>

<option value="2">blah3</option>
<option value="0">blah</option>
<option value="3">blah4</option></select>

但我希望它按顺序进行......发生了什么,我该如何纠正它?

编辑:我觉得答案与此有关

You can never be guaranteed of any order with hashes. You could try .sort() to sort the values in alphabetical order.

除了散列之外,还有什么我可以使用的吗?

最佳答案

是的,你应该使用数组的数组。你的例子最简单的方法是这样的:

<%=f.select(:whatever_id, [["blah", 0], ["blah2", 1], ["blah3", 2], ["blah4", 3]], {:include_blank => true}) %>

这应该足够了。查看 api.rubyonrails.com 上的文档也是。

关于ruby-on-rails - select_tag 正在排序(奇怪地)[Rails],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/529068/

相关文章:

ruby-on-rails - Rails3 - oauth 插件问题

ruby-on-rails - 如何在选择下拉列表中发生 onchange 时自动提交 Rails formhelper(从 DB 填充)

css - 在每次迭代的不同位置显示信息

javascript - 脚本中没有被编译

google-apps-script - 从 HTML 输入返回应用程序脚本中的数据

html - 如何在选择元素上使用 inset box-shadow?

jQuery 选择 : change event doesn't fire from AJAX call

jquery - 在带有 Turbolink 的 Rails 中使用 jQuery 进行长轮询

html - 在 asp.net 下拉列表控件中垂直对齐文本

ruby-on-rails - Rails + SendGrid 不允许未经身份验证的发件人