Rails - 返回选定标签的月份数组
我使用的是 Rails 2.3.8 上的应用程序,需要返回要插入 options_for_select 语句的月份名称和数字数组。到目前为止我所得到的只是工作,但不是真的。我这样做的原因是因为 select 语句需要一个提示,在 2.3.8 中默认情况下你不能给出 options_for_select (至少据我所知)。
到目前为止,这是我所拥有的:
@months = [['-', '']]
(1..12).each {|m| @months << [[Date::MONTHNAMES[m], m]]}
所以我希望返回的是这样的选项:
<option value="1">January</option>
<option value="2">February</option>
但是,我得到的是:
<option value="January1">January1</option>
<option value="February2">February2</option>
我缺少什么?
I'm in an app that is on Rails 2.3.8, and need to return an array of month names and numbers to be plugged into an options_for_select statement. What I've got so far is kind of working, but not really. The reason I'm doing things this way is because the select statement needs a prompt, which you can't give options_for_select by default in 2.3.8 (at least to my knowledge).
Here is what I have so far:
@months = [['-', '']]
(1..12).each {|m| @months << [[Date::MONTHNAMES[m], m]]}
So what I'm looking to get returned are options like this:
<option value="1">January</option>
<option value="2">February</option>
However, instead I get:
<option value="January1">January1</option>
<option value="February2">February2</option>
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
试试这个!
Try this!
使用默认选项的缩写选择
Abbreviated selection with default option
您可以使用 Rails 助手 select_month,例如:
You can use rails helper select_month, like:
这是我为您提供的解决方案,如果您正在处理需要多语言功能的基于 I18n 的项目:
此致
This is my solution for you, in case you work with I18n-based projects, that requires multilingual features:
Regards
这是一种性感的获取月份的方法,前提是这就是你想要的:
Here's a sexy way to get the months only if that's all you want:
为 ROR 4 工作
Working for ROR 4
如果您希望将其翻译为所选语言。
If you want it to be translated to the selected language.
找到了这种巧妙的方法来移去第一个
nil
但返回新的月份集合。您不能对其使用shift,因为这会尝试修改冻结的变量。Found this nifty way to shift off the first
nil
but return a new collection of months. You cannot use shift on it since that would try to modify the frozen variable.尝试一下
Try it
输出:
示例:
Outputs:
Example: