开发者

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

开发者 https://www.devze.com 2023-02-13 07:01 出处:网络
My object got by default today\'s date: class MyController < ApplicationController def new @obj.my_date = Date.today

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 l开发者_如何学JAVAike 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 ?


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 %>
0

精彩评论

暂无评论...
验证码 换一张
取 消