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'), '年', '月', '日')
もしくは、time_separator
も組み合わせれば、
!= sprintf(f.datetime_select(:created_at, use_month_numbers: true, date_separator: '%s', datetime_separator: '%s', time_separator: '%s'), '年', '月', '日', '時') + '分'
ハックっぽいやり方なので、用法用量を(ry