在 Rails 3 中自定义设计错误消息? [英] Customizing Devise error messages in Rails 3?

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

问题描述

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

<%= f.label :email, "Email:" %><br/><% if resource.errors[:email].present?%><ul><% resource.errors[:email].each do |msg|%><li><%=msg%></li><%结束%><%结束%><%= f.text_field :email, :class =>文本"%>

但是当邮件出现问题时,显示的信息如下:is invalid.这对用户不是很友好,但我找不到设置此消息的位置.它似乎不在 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 中自定义设计错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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