有没有办法在 Rails 中为确认字段自定义消息?例如在设计中我必须输入密码和 password_confirmation 并且错误消息是:
Password confirmation doesn't match Password
我可以更改事件记录语言环境消息(“不匹配”),但它会在该语言环境消息的开头和结尾输出密码确认和密码,所以我得到如下内容:
"Password confirmation must match Password"
有没有办法将其更改为不同的字符串?
Password confirmation and Password must match.
编辑
另一件事是拥有完全自定义的消息,例如:
'Set password' and 'Confirm password' must match.
最佳答案
那些错误信息属于activerecord .
只需创建一个具有该结构的新语言文件并替换您需要的内容即可。
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"
请根据您的要求在此链接上阅读更多内容 https://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml
关于ruby-on-rails - 在 Rails 中自定义 "Password confirmation doesn' t 匹配密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19277377/