Changing default date format in Rails

Use Rails I18n for this.

In config/locales/en.yml:

en:   date:     formats:       default: "%m/%d/%Y"

In views:

<%= l some_model.date_field %>

And what about helpers? I would like to let users to enter date in the text field (faster then 3x select), so value for some_object.date_attribute should in f.text_field(:date_attribute) be "21.7.2011" (not "2011-07-21").

I am using virtual atributes for now:

def date_attribut_to_s    if self.date_attribute.kind_of?(Date)     return self.date_attribute.strftime("%d.%m.%Y")    else      return self.date_attribute    end end

def date_attribut_to_s=(maybe_date)     date=nil

    if maybe_date.blank?       self.date_attribute=nil     elsif mybe_date.kind_of?(Date)       self.date_attribute=maybe_date     else       begin         #some more complex date parsing required?         date = Date.strptime(maybe_date, "%d.%m.%Y")       rescue         self.errors.add(:date_attribute,"Invalid date")       end       self.date_attribute = date unless date.blank?    end end

and use it in view as f.text_field(:date_attribute_to_s) .

Foton

Valentine B. wrote in post #989611:

So what is the question?

Colin