Ruby-on-rails 在 Rails 3 中自定义设计错误消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5839071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:02:58  来源:igfitidea点击:

Customizing Devise error messages in Rails 3?

ruby-on-railsrubyruby-on-rails-3devise

提问by Joshua Clanton

I am using devise to handle authentication. Overall I like it, but I'd like to customize the error display a bit. Right now I've got the following in my view.

我正在使用设计来处理身份验证。总的来说,我喜欢它,但我想稍微自定义一下错误显示。现在我有以下观点。

<div class="field <% if resource.errors[:email].present? %>error<% end %>">
  <%= f.label :email, "Email:" %><br />
  <% if resource.errors[:email].present? %>
    <ul>
      <% resource.errors[:email].each do |msg| %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  <% end %>
  <%= f.text_field :email, :class => "text" %>
</div>

But when there is a problem with the email, the message displayed is as follows: is invalid. That's not very user friendly, but I can't find where this message is being set. It doesn't appear to be in devise.en.yml, but perhaps I'm overlooking something.

但是当邮件出现问题时,显示的信息如下:is invalid. 这对用户不是很友好,但我找不到设置此消息的位置。它似乎不在 devise.en.yml 中,但也许我忽略了一些东西。

Any idea where I can customize the error messages?

知道在哪里可以自定义错误消息吗?

Thanks!

谢谢!

采纳答案by robzolkos

These validations are all defined in the validationsmodule, and use the default Rails error messages.

这些验证都在验证模块中定义,并使用默认的 Rails 错误消息。

You can override these in your model.

您可以在模型中覆盖这些。

validates_format_of :email, :with=>email_regexp, :allow_blank => true, :message=>"new error message here" 

回答by Christian Fazzini

You can configure the error messages in the locales file at: /config/locales/devise.en.yml

您可以在以下位置的语言环境文件中配置错误消息:/config/locales/devise.en.yml

Which should have something like below code and which you can easily modify to your liking:

其中应该有类似下面的代码,您可以根据自己的喜好轻松修改:

en:  
  errors:  
    messages:  
      not_found: "not found"  
      already_confirmed: "was already confirmed"  
      not_locked: "was not locked"  

  devise:  
    failure:  
      unauthenticated: 'You need to sign in or sign up before continuing.'  
      unconfirmed: 'You have to confirm your account before continuing.'  
      locked: 'Your account is locked.'  
      invalid: 'OH NOES! ERROR IN TEH EMAIL!'  
      invalid_token: 'Invalid authentication token.'  
      timeout: 'Your session expired, please sign in again to continue.'  
      inactive: 'Your account was not activated yet.'  
    sessions:  
      signed_in: 'Signed in successfully.'  
      signed_out: 'Signed out successfully.'  

For a more detailed explanation, check out this url(with screenshots). The Customizing Error Messagessection, in the article.

有关更详细的解释,请查看此url(带有屏幕截图)。文章中的自定义错误消息部分。

回答by akhanubis

If you want to change the messages for the customs validations added by Device, check Christian's answer.

如果您想更改 Device 添加的海关验证消息,请查看Christian 的回答

Otherwise, if the validation you want to customize is a standard validation like email format, you don't need to remove the Devise validations and replace them with your own. A better way of handling this is to make use of the default error messages precedence listed in the Rails guidesand override the error message for a particular field and a particular validation.

否则,如果您要自定义的验证是电子邮件格式等标准验证,则无需删除设计验证并将其替换为您自己的验证。处理此问题的更好方法是利用Rails 指南中列出的默认错误消息优先级,并覆盖特定字段和特定验证的错误消息。

For this particular question, the key that you need to add in config/locales/en.ymlin order to change is invalidwith a custom message for email errors is activerecord.errors.models.user.attributes.email.invalid(where useris the name of the model):

对于这个特定问题,您需要添加config/locales/en.yml以更改is invalid电子邮件错误的自定义消息的关键是activerecord.errors.models.user.attributes.email.invaliduser模型的名称在哪里):

en:
  activerecord:
    errors:
      models:
        user:
          attributes:
            email:
              invalid: "custom invalid message"

Rails will search for a message to show for a validation in the following order:

Rails 将按以下顺序搜索要显示的消息以进行验证:

activerecord.errors.models.[model_name].attributes.[attribute_name]
activerecord.errors.models.[model_name]
activerecord.errors.messages
errors.attributes.[attribute_name]
errors.messages