我希望从现在开始只有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]}
答案 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
帮助程序