当表单有错误时,如何正确地使用rails来格式化日期

时间:2012-07-11 07:44:44

标签: ruby-on-rails formatting

如果表单有错误,如何正确设置rails来格式化日期

我正在尝试设置rails以使用以下格式接受日期类型的输入字段:dd / mm / yyyy

这正常工作,因为rails接受我的日期格式作为输入。

我遇到的问题是,当我提交带有错误的表单时,日期的格式会更改为rails default(yyyy-mm-dd)

说明问题:

我有一个看起来像这样的模型: 人(姓名:字符串,生日:日期) 两个字段在新视图中设置为text_field

提交表格时无错误

when I visit person/new
fill the date field with "24/07/2012", fill name with "Mike"
submit the form
it works as expected, on person/show the birthdate is "24/07/2012"

提交表单并提供错误

when I visit person/edit for the previous record
the birthdate is "24/07/2012" (as expected)
change the name to be empty
submit the form
I get an error: name can't be blank (as expected)
birthdate format is 2012-07-24 (incorrect) should be 24/07/2012

我的环境:

我正在使用Rails 3.0.3,Ruby 1.8.7运行我有一个格式化初始化器,如下所示:

日期:: DATE_FORMATS [:默认] =“%d /%m /%Y”

知道为什么会这样吗?我怎样才能按预期工作?

更新: 我正在使用标准的东西,我的观点看起来像这样

<%= f.label :name, "Name" %>
<%= f.text_field :name %> 
<%= f.label :birthdate, "Birthdate"%>
<%= f.text_field :birthdate %> 

0 个答案:

没有答案
相关问题