设计错误显示两次
使用 Devise 进行授权,我将其配置为通过电子邮件或登录进行授权。 已删除可验证的驱动程序。我在我的用户模型中编写了验证。 另外,我在 app/helpers/devise_helper.rb 中覆盖了 devise helper:
module DeviseHelper
def devise_error_messages!
return "" if resource.errors.empty?
errors = Array.new
resource.errors.each do |field, msg|
errors.push(msg)
end
flash[:alert] = errors
end
end
在视图 (devise/registrations/new.html.erb) 中,我替换
<%= devise_error_messages! %>
为
<% devise_error_messages! %>
So, Problem。 当字段为空(或其他)时,会显示闪存错误。然后我进入主页,错误再次显示。仅在注册界面出现。当授权就可以了。 请帮忙。
更新。
部分视图(_block_errors.html.erb):
<% flash.each do |name, msg| %>
<% if msg.class == Array %>
<% msg.each do |message| %>
<%= content_tag :p, message, :class => "#{name}" %>
<% end %>
<% else %>
<%= content_tag :p, msg, :class => "#{name}" %>
<% end %>
<% end %>
在布局中,它的调用方式如下:
<%= render 'layouts/block_errors' %>
已解决!
在重载方法devise_error_messages中!需要替换字符串:
flash[:alert] = errors
希望
flash.now[:alert] = errors
它可以帮助任何人:)
Using Devise for authorization I configured it to authorize by email or login.
Validatable driver deleted. I wrote validates in my user model.
Also I overwrite devise helper in app/helpers/devise_helper.rb:
module DeviseHelper
def devise_error_messages!
return "" if resource.errors.empty?
errors = Array.new
resource.errors.each do |field, msg|
errors.push(msg)
end
flash[:alert] = errors
end
end
In view (devise/registrations/new.html.erb) I replace
<%= devise_error_messages! %>
to
<% devise_error_messages! %>
So, problem.
When fields are empty (or else), flash errors are showing. Then I go to main page, and errors show again. It's appear only in registration interface. When authorization thats all right.
Please help.
Updated.
partial view (_block_errors.html.erb):
<% flash.each do |name, msg| %>
<% if msg.class == Array %>
<% msg.each do |message| %>
<%= content_tag :p, message, :class => "#{name}" %>
<% end %>
<% else %>
<%= content_tag :p, msg, :class => "#{name}" %>
<% end %>
<% end %>
In layout it's called like:
<%= render 'layouts/block_errors' %>
Solved!
In overloaded method devise_error_messages! need to replace string:
flash[:alert] = errors
to
flash.now[:alert] = errors
Hope it helps anybody :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定您的应用中的其他地方没有显示
alert
吗?应该看起来像这样
Are you sure you don't have
alert
displayed anywhere else in your app?Should look something like this
我通过删除 index.html.erb 文件中的
<%= note %>
解决了该问题
I resolved the problem by deleting
<p id="notice"><%= notice %></p>
in my the index.html.erb file