破いて捨てたノート

Webやテクノロジーやそれ以外に関する思いつき

Rails の`datetime_select`でつくるセレクトボックスを「年」「月」「日」「時」「分」で区切る

日付に関するセレクトボックスを「年」「月」「日」のように日本語で区切りたいことってよくありますよね。
dateに関するセレクトボックスに関しては、 id:inouetakuya さんの「Rails の date_select でつくるセレクトボックスを「年」「月」「日」で区切る」という記事のお陰でそれを実現することができます。
上記記事の方法を少し発展させれば、datetimeに関するセレクトボックスも「年」「月」「日」「時」「分」等で区切ることが可能になるので、やり方をメモしておく。
基本方針は全く同じです。datetime_separatorを使って、

!= sprintf(f.datetime_select(:birth_date, use_month_numbers: true, date_separator: '%s', datetime_separator: '%s'), '', '', '')

f:id:yachibit:20150125152229p:plain

もしくは、time_separatorも組み合わせれば、

  != sprintf(f.datetime_select(:created_at, use_month_numbers: true, date_separator: '%s', datetime_separator: '%s', time_separator: '%s'), '', '', '', '') + ''

f:id:yachibit:20150125152805p:plain

ハックっぽいやり方なので、用法用量を(ry