rails - Devise - 处理 - devise_error_messages [英] rails - Devise - Handling - devise_error_messages
问题描述
在我的用户编辑页面中,有一行如下:
in my user edit page, there is a line as follows:
<%= devise_error_messages! %>
问题是这不会输出错误的其他应用程序的支持方式: p>
The problem is this does not output errors the stand way that the rest of the app does:
<% flash.each do |key, value| %>
<div class="flash <%= key %>"><%= value %></div>
<% end %>
我的问题是,如何让设计错误消息像其他使用闪存的人一样工作。每?
My question, is how to I get the devise error message to work like the others that use the flash.each?
谢谢
推荐答案
我想自己弄清楚。我刚刚发现这个问题登录在Github https://github.com/plataformatec/设计/问题/问题/ 504 /#comment_574788
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
何塞说, devise_error_messsages!
方法是只是一个存根(虽然它包含实现),我们应该覆盖/替换它。如果这是在维基的某个地方指出的话,这将是很好的,这就是为什么我想有一些像我们这样的人已经猜到了。
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.
所以我' m尝试重新打开模块并重新定义方法,有效地覆盖默认实现。我会让你知道它是如何进行的。
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.
我创建了 app / helpers / devise_helper.rb
,并且像这样覆盖:
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.
为帮助您解决原始问题:这是原始的 devise_helper.rb
。看看错误信息如何被遍历:
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. :)
资源
实际上是被设计使用的模型(去图)。
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.
你的助手中的任何地方
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
您的查看
<div><%= resource.errors.inspect %></div>
这篇关于rails - Devise - 处理 - devise_error_messages的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!