How can I make Rails 3 localize my date formats?

前端 未结 5 1334
终归单人心
终归单人心 2021-02-01 02:03

I am working on a Rails 3 project where there is place for date input within a form. The text field with the date uses a date picker so there is no concern about the date being

5条回答
  •  日久生厌
    2021-02-01 02:29

    What I found to be the best solution is this:

    • I localize date formats in my locale file like you do
    • In my forms I localize the date by setting the value directly

    <%= f.text_field :publish_date, :value => (@model.publish_date.nil? ? nil : l(@model.publish_date)) %>

    It is not perfect sadly, but at least this way I can use my form for both new and existing records. Also the app will stay compatible with multiple locales compared to changing the default format with initializers. If you fully want to comply with DRY you could always write a custom helper.

提交回复
热议问题