Ruby on rails 定制";密码确认不需要';“密码不匹配”;在轨

Ruby on rails 定制";密码确认不需要';“密码不匹配”;在轨,ruby-on-rails,ruby,validation,devise,Ruby On Rails,Ruby,Validation,Devise,有没有办法在Rails中为确认字段定制消息?例如,在Desive中,我必须输入密码并确认密码,错误消息为: 密码确认与密码不匹配 我可以更改活动记录区域设置消息(“不匹配”),但它会在该区域设置消息的开头和结尾输出密码确认和密码,因此我得到如下结果: “密码确认必须与密码匹配” 有没有办法把它改成不同的字符串 密码确认和密码必须匹配 编辑 另一件事是完全定制消息,例如: “设置密码”和“确认密码”必须匹配 这些错误消息属于 只需创建一个具有该结构的新语言文件,并替换所需内容 activereco

有没有办法在Rails中为确认字段定制消息?例如,在Desive中,我必须输入密码并确认密码,错误消息为:

密码确认与密码不匹配

我可以更改活动记录区域设置消息(“不匹配”),但它会在该区域设置消息的开头和结尾输出密码确认和密码,因此我得到如下结果:

“密码确认必须与密码匹配”

有没有办法把它改成不同的字符串

密码确认和密码必须匹配

编辑

另一件事是完全定制消息,例如:

“设置密码”和“确认密码”必须匹配


这些错误消息属于

只需创建一个具有该结构的新语言文件,并替换所需内容

activerecord:
  errors:
    messages:
      confirmation: "and Password must match."
您可以为模型或模型属性定义自己的错误。 值:model、:attribute和:value始终可用于插值

  For example,
   models:
       user:
         blank: "This is a custom blank message for %{model}: %{attribute}"
         attributes:
           login:
             blank: "This is a custom blank message for User login"
请根据您的要求阅读更多有关此链接的信息

ActiveRecord en.yml是我建议您更改Desive的验证消息的答案

这里是en.yml的外观

en:
  activerecord:
    errors:
      models:
        user:
          attributes:
            email:
              blank: "Please Specify an Email id"
              taken: "Please use a different Email id"
              invalid: "Please Specify a valid Email id"
            password:
              blank: "Please Specify a Password"
              confirmation: "Password does not match"
            password_confirmation:
              blank: "Please Specify a Password Confirmation"
            first_name:
              blank: "Please Specify First Name"
            last_name:
              blank: "Please Specify Last Name"
        pdf:
          attributes:
            name:
              blank: "Please Specify name to PDF"
              taken: "Please use different name for PDF"
            attachment:
              blank: "Please Upload a PDF Attachment"
        data_element:
          attributes:
            name:
              blank: "Please give Element a desired name"
              taken: "Already Created Element with given name"
            color:
              blank: "Please assign a color to Element"
        template:
          attributes:
            name:
              blank: "Please Specify a Name"
              taken: "Please use a different name"
我建议您以这种方式定义,而不是定制Desive验证模块

因为我认为您遵循上述方法,所以您可能会跳过一两个验证位置

例如,我删除了上面的设计验证模块,然后在用户模型中替换您自己的模块

然后,所有的验证都可以工作,但您将错过更改密码中的验证


即使从未提供密码,也从未给出密码,您仍然可以登录。但我想完全定制。请参阅我的编辑。问题是,我要自定义的错误是虚拟的(密码确认),它不存在于数据库中。@即使我从Desive中将其作为虚拟属性,但它不在模式中,上述解决方案(来自结构的泛型1,即I18n.t('activerecord.errors.messages.confirmation'))对我有效。