问题描述
我的问题类似于这个SO问题,但它并没有回答我的问题:
我知道我可以添加
时间:: DATE_FORMATS [:default] =%Y /%m /%d
到我的 environment.rb
,它将相应地更改我的默认时间格式。 / p>
所以当我做 Time.now.to_s
我得到正确的格式。
但是,我的问题是,这对于form_builder来说无法使用。例如:
<%= f.text_field:date%>
将返回一个完整的UTC时间戳: 2013-01-25 07:45 :21
我知道我可以这样做
<%= f.text_field:date,:value => @ post.date.to_s%>
它会给我正确的格式。
但是这个解决方案对我来说似乎是黑客。这是真的唯一的办法吗?
我不是100%肯定的。但答案很可能没有。这个栏杆可以让您获取数据库中的任何值。
这里是检索值的地方,供您参考:
没有特殊的处理方法,例如 localize
或 to_s
,这是应用格式的地方。
My question is similar to this SO problem, but it does not answer my question:
Rails 3 default datetime format without UTC
I understand that I can add
Time::DATE_FORMATS[:default] = "%Y/%m/%d"
to my environment.rb
and it will change my default time format accordingly.
So when I do Time.now.to_s
I get the correct format.
However, my question is, how come this does not work with a form_builder. For example:
<%= f.text_field :date %>
will return a full UTC timestamp: 2013-01-25 07:45:21
I am aware that I can do this
<%= f.text_field :date, :value => @post.date.to_s %>
And it will give me the correct format.
But this solution seems hacky to me. Is this really the only way to do it?
I am not 100% sure. But the answer is most likely no. The rails formhelper just gives you whatever value that is in your database.
Here is where the value is retrieved for your reference:
http://api.rubyonrails.org/classes/ActionView/Helpers/InstanceTag.html#method-i-to_input_field_tag
There is no special processing of it, such as localize
or to_s
, which is where formatting is applied.
这篇关于Rails 3表格中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!