Misha Moroshko
Misha Moroshko

Reputation: 171341

Rails: How to control how Date field is rendered in a form?

My object got by default today's date:

class MyController < ApplicationController
  def new
    @obj.my_date = Date.today
  end
end

and then, in my "new object" form it is displayed like this:

<%= f.text_field(:my_date) %>

It looks like this: 2011-02-24

I guess it is because of the default to_s method of Date.

Is that possible to render it like 24/02/2011 without overriding the to_s method of Date ?

Upvotes: 0

Views: 1975

Answers (1)

DanS
DanS

Reputation: 18463

This is best done using I18n.

So in your locale:

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

And in your view:

<%= l Date.today, :format => :slashes %>

Upvotes: 2

Related Questions