我在我的应用程序中使用设计作为身份验证引擎。
当设计验证失败时,有没有办法使用自定义消息。
当密码为空时,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/