rails 4.0.0 で年月の選択に input type="month"
だとブラウザごとの実装の違いが大きくて使いにくかったので、無難に select
で実装したのですが、選択肢を生成する部分で Date.new(2013,1).step(Date.today, 1.month)
のような感じで step
を使おうとしてもうまくいかなかったので、 while
ループにしてしまったのですが、何かもっと良い方法はないでしょうか?
def self.year_month_collection(start_month=Date.new(2013, 1), end_month=Date.today)
collection = [[nil, "未選択"]]
month = start_month
while month <= end_month
collection << [month.strftime("%Y-%m"), format_month(month)]
month >>= 1
end
collection
end