本文介绍了解析表单中文本字段的日期时,如何更改 ActiveRecord 期望的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我有一个带有 Date 属性的 Ruby on Rails 模型.
  2. 在此模型的表单中,我使用带有 JQuery 日期选择器的单个文本字段来表示此属性(而不是像 Rails 自定义那样为年、月和日提供下拉菜单).
  3. 日期选择器以 mm/dd/yyyy 格式插入日期.
  4. Rails 需要 dd/mm/yyyy 格式的日期.
  1. I have a Ruby on Rails model with a Date attribute.
  2. In the form for this model, I am using a single text field with a JQuery datepicker to represent this attribute (not a drop down for each of year, month, and day, as is the Rails custom).
  3. The datepicker inserts dates with a mm/dd/yyyy format.
  4. Rails is expecting dates with a dd/mm/yyyy format.

示例

  • 如果用户选择 March 12th, 2012,日期选择器将输入 03/12/2012,Rails 将其解释为 2012 年 12 月 3 日em>.
  • 如果用户选择 March 20th, 2012,日期选择器将输入 03/20/2012,Rails 将其解释为 20 日的第 3 天2012 年的月份.由于该日期不存在,Rails 将其转换为 nil 值(我认为).
  • Examples

    • If a user selects March 12th, 2012, the datepicker puts 03/12/2012, which is interpreted by Rails as December 3rd, 2012.
    • If a user selects March 20th, 2012, the datepicker puts 03/20/2012, which is interpreted by Rails as the 3rd day of the 20th month of 2012. Since this date doesn't exist, Rails casts this to a nil value (I think).
    • 如何更改 Rails 在解析此日期文本字段时使用的日期格式?

      How do I change the date format Rails uses when parsing this date text field?

      注意事项:

      1) 我不想更改日期选择器插入文本字段的日期格式,

      1) I do not want to change the format of the date the datepicker inserts into the text field,

      2) 我不是问在视图中显示我的日期属性.

      2) I am not asking about displaying my date attribute in a view.

      推荐答案

      我最初认为这可以通过 Rails 国际化功能解决,但事实证明我错了.

      I initially thought this could be solved through the Rails internationalization features, but it turns out I was wrong.

      从Ruby 1.9开始,日期解析的标准格式是dd/mm/yyyy,以便更好地适应国际用户.更多细节可以在这个SO答案中找到.

      Ever since Ruby 1.9, the standard format for date parsing is dd/mm/yyyy, so as to better accomodate international users. More details can be found in this SO answer.

      该标准在 Rails 中维护,因为 Date.parse 现在用于处理来自表单输入的数据.使用 before_validation 回调将不起作用,因为该字段将被回调方法接收为 nil.

      That standard is maintained in Rails, as Date.parse is now used to process data from form inputs. Using a before_validation callback won't work because the field is going to be received as nil by the callback method.

      现在有两个 gem 处理这个特定问题,即 Rails 中的日期解析不遵循 I18n.locale 中的区域设置.两者似乎都运行良好.

      Right now there are two gems dealing with this specific issue, namely that date parsing in Rails does not follow the locale settings from I18n.locale. Both seem to work well.

      1. delocalize,由 clemens - 似乎已经在相当数量或多个项目中成功申请,目前拥有最多的星星.

      1. delocalize, by clemens - Seems to have been applied successfully in a decent number or projects and has the highest number of stars at the moment.

      i18n_alchemy by carlosantoniodasilva - 这个已经发布最近.作者是 Rails 核心团队成员,在这方面非常活跃.绝对值得一看.

      i18n_alchemy by carlosantoniodasilva - This one has been released more recently. The author is a Rails core team member, and a very active one at that. Definitely deserves a look.

      这篇关于解析表单中文本字段的日期时,如何更改 ActiveRecord 期望的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 19:16