How can I make Rails 3 localize my date formats?

前端 未结 5 1333
终归单人心
终归单人心 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:22

    You could override your getters for the :publish_date attribute.

    i.e. in your model:

    def date( *format)
        self[:publish_date].strftime(format.first || default)
    end
    

    and in your view you could do either

    @income.date("%d/%m/%Y")
    

    or

    @income.date
    

    This would cause strftime to use the passed format string unless it was nil, in which case it would fall back to your default string.

    Note: I used the splat operator to add support for getting the date without an argument. There may be a cleaner way to do that.

提交回复
热议问题