为Devise添加日期,现在出现错误

时间:2010-11-06 13:39:16

标签: ruby-on-rails devise

我最近在我的设计注册页面添加了一个出生日期字段,并使用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

1 个答案:

答案 0 :(得分:2)

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