Rails - 格式化 date_field [英] Rails - format date_field

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

问题描述

如何设置 date_field 的日期格式?

how can I style the date format of a date_field?

我得到了以下表格:

<%= form_for([@user, @vehicle]) do |f| %>
    <%= f.label :name, "Vehicle name" %>
    <%= f.text_field :name, class: 'form-control' %>
    <%= f.label :matriculation_date %>
    <%= f.date_field :matriculation_date, class: 'form-control' %>
    <%= f.submit "Add", class: "btn btn-primary" %>
<% end %>

这是为日期字段呈现以下 HTML:

this is rendering the following HTML for the date field:

日期字段接受以下格式的输入:mm/dd/yyyy.我希望它接受像 dd/mm/yyyy 这样的格式.

The date field accept input in the following format: mm/dd/yyyy. I want it to accept format like dd/mm/yyyy.

我试图编辑 it.yml 文件,但没有任何运气.

I tried to edit it.yml file without any luck.

it:
  date:
      formats:
        default: ! '%d/%m/%Y'

  datetime:
      formats:
        default: ! '%d/%m/%Y %H:%M'

有什么线索吗?谢谢

建议的解决方案都没有解决我的问题,所以我认为我的问题不是很清楚.我想要的只是设置 date_field 如何提示用户的样式(有关更多详细信息,请参见图像).目前表单显示mm/dd/yyyy,我希望它显示dd/mm/yyyy.

None of the proposed solutions solve my problem, so I think my question was not very clear. What I want is just to style how the date_field prompt the user (see image for more details). At the moment the form show mm/dd/yyyy, and I want it to show dd/mm/yyyy.

不确定这可能是有用的信息,但我正在使用 Bootstrap 进行样式设置.

Not sure it might be a useful information, but I'm using Bootstrap for styling.

推荐答案

您可以:

<%= f.date_field :matriculation_date, as: :date, value: f.object.try(:strftime,"%m/%d/%Y"), class: 'form-control' %> 

这篇关于Rails - 格式化 date_field的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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