简体   繁体   中英

Customizing Devise error messages in Rails 3?

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.

Any idea where I can customize the error messages?

Thanks!

You can configure the error messages in the locales file at: /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.

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

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.

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):

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:

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

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

You can override these in your model.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM