Rails - 返回选定标签的月份数组

发布于 2024-10-31 01:58:39 字数 618 浏览 2 评论 0原文

我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

逆光飞翔i 2024-11-07 01:58:39

试试这个!

@months = [['-', '']]
(1..12).each {|m| @months << [Date::MONTHNAMES[m], m]}

Try this!

@months = [['-', '']]
(1..12).each {|m| @months << [Date::MONTHNAMES[m], m]}
野侃 2024-11-07 01:58:39
Date::MONTHNAMES.each_with_index.collect{|m, i| [m, i]}
=> [[nil, 0], 
    ["January", 1], 
    ["February", 2], 
    ["March", 3], 
    ["April", 4], 
    ["May", 5], 
    ["June", 6], 
    ["July", 7], 
    ["August", 8], 
    ["September", 9], 
    ["October", 10], 
    ["November", 11], 
    ["December", 12]]

使用默认选项的缩写选择

Date::ABBR_MONTHNAMES.compact.each_with_index.collect{|m, i| [m, i+1]}
                     .insert(0, ['Please Select', nil])
=> [["Please Select", nil], 
    ["Jan", 1], 
    ["Feb", 2], 
    ["Mar", 3], 
    ["Apr", 4], 
    ["May", 5], 
    ["Jun", 6], 
    ["Jul", 7], 
    ["Aug", 8], 
    ["Sep", 9], 
    ["Oct", 10], 
    ["Nov", 11], 
    ["Dec", 12]]
Date::MONTHNAMES.each_with_index.collect{|m, i| [m, i]}
=> [[nil, 0], 
    ["January", 1], 
    ["February", 2], 
    ["March", 3], 
    ["April", 4], 
    ["May", 5], 
    ["June", 6], 
    ["July", 7], 
    ["August", 8], 
    ["September", 9], 
    ["October", 10], 
    ["November", 11], 
    ["December", 12]]

Abbreviated selection with default option

Date::ABBR_MONTHNAMES.compact.each_with_index.collect{|m, i| [m, i+1]}
                     .insert(0, ['Please Select', nil])
=> [["Please Select", nil], 
    ["Jan", 1], 
    ["Feb", 2], 
    ["Mar", 3], 
    ["Apr", 4], 
    ["May", 5], 
    ["Jun", 6], 
    ["Jul", 7], 
    ["Aug", 8], 
    ["Sep", 9], 
    ["Oct", 10], 
    ["Nov", 11], 
    ["Dec", 12]]
清风夜微凉 2024-11-07 01:58:39

您可以使用 Rails 助手 select_month,例如:

select_month(Date.today)

You can use rails helper select_month, like:

select_month(Date.today)
冷︶言冷语的世界 2024-11-07 01:58:39

这是我为您提供的解决方案,如果您正在处理需要多语言功能的基于 I18n 的项目

def month_array
  # Gets the first day of the year
  date = Date.today.beginning_of_year
  # Initialize the months array
  months = {}

  # Iterate through the 12 months of the year to get it's collect
  12.times do |i| # from 0 to 11
    # Get month name from current month number in a selected language (locale parameter)
    month_name = I18n.l(date + i.months, format: '%B', locale: :en).capitalize
    months[month_name] = i + 1
  end
  return months
end

# => {"January"=>1, "February"=>2, "March"=>3, "April"=>4, "May"=>5, "June"=>6, "July"=>7, "August"=>8, "September"=>9, "October"=>10, "November"=>11, "December"=>12}

此致

This is my solution for you, in case you work with I18n-based projects, that requires multilingual features:

def month_array
  # Gets the first day of the year
  date = Date.today.beginning_of_year
  # Initialize the months array
  months = {}

  # Iterate through the 12 months of the year to get it's collect
  12.times do |i| # from 0 to 11
    # Get month name from current month number in a selected language (locale parameter)
    month_name = I18n.l(date + i.months, format: '%B', locale: :en).capitalize
    months[month_name] = i + 1
  end
  return months
end

# => {"January"=>1, "February"=>2, "March"=>3, "April"=>4, "May"=>5, "June"=>6, "July"=>7, "August"=>8, "September"=>9, "October"=>10, "November"=>11, "December"=>12}

Regards

围归者 2024-11-07 01:58:39

这是一种性感的获取月份的方法,前提是这就是你想要的:

Date::MONTHNAMES.slice(1..-1).map(&:to_sym)

Here's a sexy way to get the months only if that's all you want:

Date::MONTHNAMES.slice(1..-1).map(&:to_sym)
软糖 2024-11-07 01:58:39

为 ROR 4 工作

select_month(0 , prompt: 'Choose month')

Working for ROR 4

select_month(0 , prompt: 'Choose month')
紫竹語嫣☆ 2024-11-07 01:58:39

如果您希望将其翻译为所选语言。

t("date.month_names")

If you want it to be translated to the selected language.

t("date.month_names")
毁梦 2024-11-07 01:58:39

找到了这种巧妙的方法来移去第一个 nil 但返回新的月份集合。您不能对其使用shift,因为这会尝试修改冻结的变量。

Date::MONTHNAMES.drop(1)

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.

Date::MONTHNAMES.drop(1)
半枫 2024-11-07 01:58:39

尝试一下

<% (1..12).each do |month|%>
  <%= link_to t('date.month_names')[month], '#' %>
<% end %>

Try it

<% (1..12).each do |month|%>
  <%= link_to t('date.month_names')[month], '#' %>
<% end %>
想挽留 2024-11-07 01:58:39
# alternative 1 
Date::MONTHNAMES.compact.zip(1.upto(12)).to_h

# alternative 2
Date::MONTHNAMES.compact.zip([*1..12]).to_h

输出:

{“一月”=>0、“二月”=>1、“三月”=>2、“四月”=>3、“五月”=>4、
“六月”=>5、“七月”=>6、“八月”=>7、“九月”=>8、“十月”=>9、
“11 月”=>10,“12 月”=>11}

示例:

# using simple_form
f.input :month, collection:  Date::MONTHNAMES.compact.zip(1.upto(12)).to_h
# alternative 1 
Date::MONTHNAMES.compact.zip(1.upto(12)).to_h

# alternative 2
Date::MONTHNAMES.compact.zip([*1..12]).to_h

Outputs:

{"January"=>0, "February"=>1, "March"=>2, "April"=>3, "May"=>4,
"June"=>5, "July"=>6, "August"=>7, "September"=>8, "October"=>9,
"November"=>10, "December"=>11}

Example:

# using simple_form
f.input :month, collection:  Date::MONTHNAMES.compact.zip(1.upto(12)).to_h
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文