rails - 设计 - 处理 - devise_error_messages [英] rails - Devise - Handling - devise_error_messages
问题描述
在我的用户编辑页面,有一行如下:
<%= devise_error_messages!%>
问题是这不会像应用程序的其余部分那样以标准方式输出错误:
<% flash.each do |key, value|%><div class="flash <%= key %>"><%= value %></div><%结束%>
我的问题是,如何让设计错误消息像其他使用 flash.each 的人一样工作?
谢谢.
我正在尝试自己解决这个问题.我刚刚在 Github 上发现了这个问题 https://github.com/plataformatec/设计/问题/问题/504/#comment_574788
Jose 说 devise_error_messages!
方法只是一个 存根(尽管它包含实现),我们应该覆盖/替换它.如果在维基的某个地方指出这一点就好了,这就是为什么我猜有一些像我们这样的人一直在猜测.
所以我将尝试重新打开模块并重新定义方法,有效地覆盖默认实现.我会告诉你进展如何.
更新
是的,这有效.我创建了 app/helpers/devise_helper.rb
并像这样覆盖它:
module DeviseHelperdef devise_error_messages!咔嚓!"结尾结尾
知道了这一点,我可以修改方法以按照我想要的方式显示错误消息.
帮助您解决最初的问题:这是原始的<代码>devise_helper.rb 在 Github 上.看看错误消息是如何被遍历的:
messages = resource.errors.full_messages.map { |msg|content_tag(:li, msg) }.join
这应该可以帮助您入门.:)
另一个更新
resource
对象实际上是设计使用的模型(见图).
resource.class #=>用户resource.errors.class #=>活动模型::错误
它似乎也被定义在更高的范围内(可能来自控制器),因此可以在各种地方访问它.
Helper 中的任何位置
module DeviseHelperdef devise_error_messages1!resource.errors.full_messages.map { |msg|content_tag(:li, msg) }.join结尾def devise_error_messages2!resource.errors.full_messages.map { |msg|content_tag(:p, msg) }.join结尾结尾
你的观点
<%=resource.errors.inspect %>
in my user edit page, there is a line as follows:
<%= devise_error_messages! %>
The problem is this does not output errors the standard way that the rest of the app does:
<% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
My question is, how do I get the devise error message to work like the others that use the flash.each?
Thanks.
I'm trying to figure this out myself. I just found this issue logged on Github https://github.com/plataformatec/devise/issues/issue/504/#comment_574788
Jose is saying that devise_error_messsages!
method is just a stub (though it contains implementation) and that we're supposed to override/replace it. It would have been nice if this was pointed out somewhere in the wiki, which is why i guess there are a few people like us that have been guessing.
So I'm going to try reopening the module and redefine the method, effectively overriding the default implementation. I'll let you know how it goes.
Update
Yep, that works. I created app/helpers/devise_helper.rb
and overrode it like so:
module DeviseHelper
def devise_error_messages!
'KABOOM!'
end
end
So knowing this, I can modify the method to display error messages the way I want it to.
To help you solve your original problem: Here's the original devise_helper.rb
on Github. Take a look at how the error messages are being traversed:
messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
That should help you get started. :)
Another update
The resource
object is actually the model that is being used by devise (go figure).
resource.class #=> User
resource.errors.class #=> ActiveModel::Error
It also appears to be defined in a higher scope (probably coming from the controller), so it can be accessed in a variety of places.
Anywhere in your Helper
module DeviseHelper
def devise_error_messages1!
resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
end
def devise_error_messages2!
resource.errors.full_messages.map { |msg| content_tag(:p, msg) }.join
end
end
Your View
<div><%= resource.errors.inspect %></div>
这篇关于rails - 设计 - 处理 - devise_error_messages的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!