如何编辑Devise错误消息? [英] How do you edit Devise error messages?
问题描述
要清楚,我知道如何编辑config / locales / devise.en.yml中的错误消息,但我指的是样式这些类型的错误消息:
To be clear, I know how to edit the error messages in config/locales/devise.en.yml but I'm referring to styling these type of error messages:
2 errors prohibited this user from being saved:
- 电子邮件不能为空
- 密码不能为空
-
所有我看到的是<%= devise_error_messages! %>在注册页面,但我不知道如何实际编辑错误消息本身。
All i see is <%= devise_error_messages! %> on the sign-up page, but I don't know how to actually edit the error messages themselves.
在我的情况下,邮件显示在左侧,我的注册位于中心(看起来很奇怪),我也不喜欢邮件的红色,会更喜欢不同的颜色。
In my case the messages appear on the left, and my sign up is centred (which looks odd), I also don't like the red color of the messages and would prefer a different color.
所以我的问题是,如何风格的错误消息?中心和更改颜色。
So my question is, how can I style the error message? centre it, and change the color.
不确定要包含哪些其他控制器或内容,所以一旦你问,我会更新OP与他们如果需要。
Not sure which other controllers or contents to include so as soon as you ask, i'll update the OP with them if need be.
推荐答案
查看devise_error_messages的源代码!方法,位于 https://github.com/plataformatec/devise/blob/master/app/ helpers / devise_helper.rb 。
See the sources for the devise_error_messages! method at https://github.com/plataformatec/devise/blob/master/app/helpers/devise_helper.rb.
所有错误均在
<div id="error_explanation">
,所以你可以在CSS中使用这个事实。里面它只使用基本样式:h2为标题消息,ul为单个错误。请参阅此#oSorExplanation样式的SO示例,例如:如何在rails中美化验证< a>。只要不要忘记在示例中用#error_explanation替换#errorExplanation。
so you can use that fact in your CSS. Inside it uses only basic styling: h2 for the header message, ul for the individual errors. See this SO example for #errorExplanation styling, for example: how to beautify validations in rails. Just don't forget to replace #errorExplanation with #error_explanation in the example.
但是你最好的方法可能还是重写这个方法或者编写和使用你自己的,
But your best approach would probably still be to rewrite this method or write and use your own, and there apply all the styling you like.
我个人建议在他们所属的字段旁边显示错误。看到这个SO线程,例如,如何做: Rails:接近表单中特定字段的错误。
I'd personally recommend displaying errors next to the fields they belong to. See this SO thread, for example, on how to do that: Rails: Errors close to particular fields in forms.
另一个改进是切换到你的表单的simple_form(并获得错误 - 免费的next-to-fields)。请参见,例如,一个很好的Railscast: http://railscasts.com/episodes/234-simple-form。有一个最近修订的Railscast,但不知道你是否是一个普通用户。
Another improvement would be switching to simple_form for your forms (and getting errors-next-to-fields for free). See, for example, an excellent Railscast on that: http://railscasts.com/episodes/234-simple-form. There's a more recent revised Railscast, but not sure if you're a Pro subscriber there.
这篇关于如何编辑Devise错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!