How to localize the date format in rails, generate a form and error messages?

I spent most of the day trying to localize dates in my application.

Reading this http://guides.rubyonrails.org/i18n.html seems like the I18n.l (date) method is a way to do this.

This is all very well if all I want to do is visualize date objects right in my view. However, many times I want to display the date in the form field:

= form_for @object do |f|
  .field
    = f.label :date
    = f.text_field :date

It seems to call to_s in the date object and use it without localization.

The first workaround I tried was for the monkey to fix the date class for using I18n.l:

class Date
  def to_s
    I18n.l(self)
  end
end

, , I18n.l , . , , , !

, :

class Date
  def to_s(type = nil)
    if(type == :db)
      self.strftime("%Y-%m-%d")
    else
      I18n.l(self)
    end
  end
end

STILL - , , , db.

- , , ?

+3
1

Date to_s -.

, - ;)

, , , , date.formats.default , ( text_field).

+1

All Articles