在Rails 3中自定义Devise错误消息?

时间:2022-10-10 10:44:45

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.

但是当电子邮件出现问题时,显示的消息如下:无效。这不是非常用户友好,但我无法找到此消息的设置位置。它似乎不是在devise.en.yml,但也许我忽略了一些东西。

Any idea where I can customize the error messages?

知道我可以在哪里定制错误消息吗?

Thanks!

谢谢!

3 个解决方案

#1


8  

These validations are all defined in the validations module, 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" 

#2


28  

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 Messages section, in the article.

有关更详细的说明,请查看此URL(带截图)。文章中的“自定义错误消息”部分。

#3


9  

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

如果您想更改设备添加的海关验证的消息,请查看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 guides and 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.yml in order to change is invalid with a custom message for email errors is activerecord.errors.models.user.attributes.email.invalid (where user is the name of the model):

对于此特定问题,您需要在config / locales / en.yml中添加以便更改的密钥无效,因为电子邮件错误的自定义消息是activerecord.errors.models.user.attributes.email.invalid(其中user是模型的名称):

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

#1


8  

These validations are all defined in the validations module, 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" 

#2


28  

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 Messages section, in the article.

有关更详细的说明,请查看此URL(带截图)。文章中的“自定义错误消息”部分。

#3


9  

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

如果您想更改设备添加的海关验证的消息,请查看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 guides and 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.yml in order to change is invalid with a custom message for email errors is activerecord.errors.models.user.attributes.email.invalid (where user is the name of the model):

对于此特定问题,您需要在config / locales / en.yml中添加以便更改的密钥无效,因为电子邮件错误的自定义消息是activerecord.errors.models.user.attributes.email.invalid(其中user是模型的名称):

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