如何在Rails 5中更改设计中的验证错误消息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Rails 5中更改设计中的验证错误消息相关的知识,希望对你有一定的参考价值。

我已将密码的默认最小长度从@@ password_length = 5..128更改为6..128。当我在注册期间对密码进行验证失败时,验证错误显示为“太短(最小为5个字符)”。如何将此错误消息更改为“最少​​6个字符”。我在stackoverflow中读了一些他们说要查看devise.views.en.yml的anuswers。但仍无法更改错误信息。

我的devise.views.en.yml(密码字段)看起来像:

passwords:
  edit:
    change_my_password: Change my password
    change_your_password: Change your password
    confirm_new_password: Confirm new password
    new_password: New password
  new:
    forgot_your_password: Forgot your password?
    send_me_reset_password_instructions: Send me reset password instructions
  no_token: You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided.
  send_instructions: You will receive an email with instructions on how to reset your password in a few minutes.
  send_paranoid_instructions: If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes.
  updated: Your password has been changed successfully. You are now signed in.
  updated_not_active: Your password has been changed successfully.

建议最受欢迎

答案

您必须以下列方式覆盖devise.en.yml文件

en:
  activerecord:
    errors:
      models:
        user:
          attributes:
            password:
              too_short: "Password is too short (minimum is %{count} characters)"

以上是关于如何在Rails 5中更改设计中的验证错误消息的主要内容,如果未能解决你的问题,请参考以下文章

Rails 5 和设计:如何在不更改默认策略的情况下禁用基于令牌的策略上的会话

如何在 Rails 验证错误中突出显示字段

Rails - 从模型中获取不是验证错误的错误消息

在 Rails 5.1 之后,attribute_changed 是如何变化的?更改“验证”回调?

jquery验证插件不在rails中显示错误消息

模型文件中的 Ruby on Rails IBAN 验证