定制Rails 3中的Devise错误消息? [英] Customizing Devise error messages in Rails 3?

查看:90
本文介绍了定制Rails 3中的Devise错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用devise来处理身份验证。总的来说,我喜欢它,但我想自定义错误显示一点。现在我看到了以下内容:

 < div class =field<%if resource.errors [:email] .present?%> error<%end%>> 
<%= f.label:email,电子邮件:%>< br />
<%如果resource.errors [:email] .present? %GT;
< ul>
<%resource.errors [:email] .each do | msg | %GT;
< li><%= msg%>< / li>
<%end%>
< / ul>
<%end%>
<%= f.text_field:email,:class => text%>
< / div>

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



任何想法,我可以自定义错误消息?



谢谢!

解决方案

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



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

  validates_format_of:email,,with => email_regexp ,:allow_blank => true,message =>此处的新错误消息


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!

解决方案

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" 

这篇关于定制Rails 3中的Devise错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆