【问题标题】:Added date to Devise and now getting errors向设计添加日期,现在出现错误
【发布时间】:2010-11-06 13:39:16
【问题描述】:

我最近在我的设计注册页面中添加了一个出生日期字段,并使用 validates_timeliness 检查日期是否正确。但是,如果日期留空,我会收到以下错误,不知道如何解决。

* Date of birth translation missing: en, activerecord, errors, models, user, attributes, date_of_birth, invalid_date
* Date of birth translation missing: en, activerecord, errors, models, user, attributes, date_of_birth, invalid_date

控制器

  #validates_timeliness gem is used for validates_date
  validates_date :date_of_birth, :before => lambda { 18.years.ago }, :before_message => "must be at least 18 years old"
  validates_date :date_of_birth, :after => lambda { 106.years.ago }, :before_message => "Seriously, dude. You ain't that old."
  validates_acceptance_of :terms_of_service, :accept => true
  validates_presence_of :gender_id

如果您需要查看表格,请在实际操作中查看。 http://hangwith.me/account/register

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    Devise 已完全国际化,并希望您定义用户在输入无效日期时应收到的错误消息。

    要解决此问题,您需要在 i18n 配置中定义相关字符串,如下所示:

    en:
      activerecord:
        errors:
          models:
            user:
              attributes:
                date_of_birth:
                  invalid_date: 'You must be over 18 to use HangWith.me (and not too old either)!'
    

    此外,如果您希望允许用户在不输入日期的情况下继续操作(您的问题不太清楚),您应该在 validates_timeliness 验证中设置 :allow_blank

    【讨论】:

    • 完美,正是我所需要的。谢谢!
    猜你喜欢
    • 2022-01-17
    • 2018-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-11
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多