Rails 3表格中的日期格式 [英] Rails 3 Date format in forms

查看:151
本文介绍了Rails 3表格中的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于这个SO问题,但它并没有回答我的问题:



Rails 3没有UTC的默认datetime格式



我知道我可以添加

 时间:: 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%肯定的。但答案很可能没有。这个栏杆可以让您获取数据库中的任何值。



这里是检索值的地方,供您参考:



http://api.rubyonrails。 org / classes / ActionView / Helpers / InstanceTag.html#method-i-to_input_field_tag



没有特殊的处理方法,例如 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表格中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆