如何从Ruby on Rails`date_select`表单助手中排除月份?

时间:2011-03-11 17:44:59

标签: ruby-on-rails date helper

我希望从现在开始只有date_select来支持我。

因此,如果我们在三月,它只会显示:


- March
- April
- May
- June
- July
- August
- September
- October
- November
- December

现在我有了这个:


f.date_select :meeting_date,{:order=>[:month,:day]}

是否有类似start_month之类的东西,就像这个助手有start_year属性一样?所以我可以使用类似的东西:


f.date_select :meeting_date,{:start_month=>Time.now.month,:order=>[:month,:day]}

2 个答案:

答案 0 :(得分:2)

如果您只想要几个月,那么您可以制作自定义助手。

config/initializers/constants.rb

中使月份保持不变
MONTHS = ['Jan', 'Feb', 'Marh'] #etc...

如果你有一个来自|f|的来自你可以有一个月建设者:

def month_select_builder(f, attr)
    f.select attr.symbolize, options_for_select(MONTHS)
end

如果您只有一个没有构建器的form_tag,那么:

def month_select(attr)
    select_tag attr.symbolize, options_for_select(MONTHS)
end

attr是模型上:meeting_date字段的名称。只需将其作为字符串传递即可。

答案 1 :(得分:-1)

months = %w(Jan Feb March ... )
f.date_select :meeting_date, :use_month_names => months[(Date.today.month - 1)..-1]

但它会插入空白项,因为:use_month_names期望Array包含12个项目。要删除它们,您应该编写自己的date_select帮助程序

相关问题