ruby-on-rails - 验证失败时设计自定义消息

标签 ruby-on-rails ruby ruby-on-rails-3 devise

我在我的应用程序中使用设计作为身份验证引擎。 当设计验证失败时,有没有办法使用自定义消息。 当密码为空时,Devise 会向我提供以下消息:Password can't be blank,但我需要另一条消息。我该怎么做?

最佳答案

ActiveRecord en.yml 是我建议的答案,如果您想更改 Devise 的验证消息

这是 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"

我建议您以这种方式定义而不是自定义设计验证模块

因为如果您遵循上述方法,您可能会跳过一两个地方的验证

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

那么所有的验证都可以工作,但是你会错过更改密码中的验证

即使从未提供也从未给出密码,但您仍然可以登录

也保持循环

欢呼

问候

关于ruby-on-rails - 验证失败时设计自定义消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9325176/

相关文章:

javascript - 指定每个表的不同样式

ruby-on-rails - 如何使用 mocha 在 Rails Controller 中模拟类

ruby-on-rails - 如何获取使用无效数据提交的 Rails 表单以重新填充用户输入的字段

ruby - 谁能告诉我现在 payflowpro 的确切 URL 是什么,Url "https://payflowpro.paypal.com"现在对我不起作用

ruby-on-rails - 使用 Rails 和 Postgres 设置 travis.ci

ruby-on-rails - 初始化后在 ruby​​ 中访问变量

Ruby 和 SOAP 创建 Web 服务代理和命名空间

ruby-on-rails - 如何正确处理 Rails 中的 ActiveRecord::RecordNotUnique

ruby - 使用if子句在ruby中重复数字

activerecord - 如何从 Rails 的查询中排除 id 数组(使用 ActiveRecord)?