为什么设计不在登录页面上显示身份验证错误? [英] Why is devise not displaying authentication errors on sign in page?
问题描述
我有一个名为devise_helper.rb的帮助文件
模块DeviseHelper
def devise_error_messages!
return如果resource.errors.empty?
messages = resource.errors.full_messages.map {| msg | content_tag(:li,msg)} .join
sentence = I18n.t(errors.messages.not_saved,
count:resource.errors.count,
resource:resource.class。 model_name.human.downcase)
html =< -HTML
< div class =row>
< div class =large-12 columns>
< div data-alert class =alert-box alert radius>
< h4>#{sentence}< / h4>
< ul>#{messages}< / ul>
< / div>
< / div>
< / div>
HTML
html.html_safe
end
end
自定义错误消息,它适用于注册和密码页面,但不适用于会话页面。为什么是这样?我知道我可以添加如下内容:
< div class =row>
<%if通知%>
< div data-alert class =alert-box info radius>
<%= notice%><%= link_toX,'#',:class => 'close'%>
< / div>
<%end%>
<%如果警报%>
< div data-alert class =alert-box alert radius>
<%= alert%><%= link_toX,'#',:class => 'close'%>
< / div>
<%end%>
< / div>
对我的application.html.erb文件和错误消息将显示在那里,但我不明白为什么我必须补充说,当我有设备帮手已经。
对于密码和注册,我只需要添加<%= devise_error_messages! %>但是会话页面似乎不会这样工作。我不知道这是只是如何工作,还是有缺少的东西。
编辑:
我生成会话控制器,但是我从来没有改变任何内容。从我的理解,设计只会使用其默认控制器,直到我更改我生成的。我的密码控制器也是这样的。我已经对注册控制器进行了一些修改以配置允许的参数。
class Users :: SessionsController< Devise :: SessionsController
end
p>具有空白/错误字段的登录不会在模型上触发(您的)验证,因此不会显示验证错误!
如果您使用 byebug
(在您的视图的第一行)进行调试,您会注意到
resource.errors.count#=> 0
flash#=> .... @ flash = {alert=>无效的电子邮件或密码。}
Devise使用特定的登录错误消息填充警报闪存,这个登录的上下文是唯一的。
为什么看不到所有的模型验证错误消息?因为没有意义:假设你的模型有一个强制性的:gender
属性与 validates_presence_of:gender
。如果添加了正常模型错误,那么您的用户尝试以错误的登录名登录时,您还会看到错误列表中的性别不能为空白:oops:。
devise_error_messages!
是一种特定的设计方法,旨在显示这些特定的错误。您可以将其视为对用于登录的字段(并在您的设计配置文件中定义)的部分验证。
替代方法: / strong>
如果您真的想显示所有错误消息,您可以明确地运行验证:
开头的 devise_error_messages!
resource.validate#将生成错误并填充`resource.errors`
我相信不应该搞砸其他动作这已经很好了(注册等)
I'm using rails 4.2
I have a helper file called devise_helper.rb
module DeviseHelper
def devise_error_messages!
return "" if resource.errors.empty?
messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
sentence = I18n.t("errors.messages.not_saved",
count: resource.errors.count,
resource: resource.class.model_name.human.downcase)
html = <<-HTML
<div class="row">
<div class="large-12 columns">
<div data-alert class="alert-box alert radius">
<h4>#{sentence}</h4>
<ul>#{messages}</ul>
</div>
</div>
</div>
HTML
html.html_safe
end
end
to customize error messages and it's working for registrations and passwords pages, but not for sessions pages. Why is this? I know that I can add something like this:
<div class="row">
<% if notice %>
<div data-alert class="alert-box info radius">
<%= notice %><%= link_to "X", '#', :class => 'close' %>
</div>
<% end %>
<% if alert %>
<div data-alert class="alert-box alert radius">
<%= alert %><%= link_to "X", '#', :class => 'close' %>
</div>
<% end %>
</div>
To my application.html.erb file and error messages will display there, but I don't understand why I have to add that when I have the devise helper already. For the passwords and registrations, I just had to add <%= devise_error_messages! %> but the sessions pages don't seem to work that way. I'm not sure if this is just how devise works or if there's something I'm missing.
EDIT: I generated the sessions controller but I never changed anything in it since generating it. From what I understand, devise will just use its default controller until I change the one i generated. My passwords controller is like this as well. I did make some changes to the registrations controller to configure permitted parameters.
class Users::SessionsController < Devise::SessionsController
end
A login with blank/wrong fields does not trigger (your) validations on the model, and therefore won't show your validation errors !
if you debug with byebug
(in the first line of your view for example), you'll notice
resource.errors.count # => 0
flash # => ....@flashes={"alert"=>"Invalid email or password."}
Devise populates the "alert flash" with specific sign in error messages unique to this context of sign-in.
Why do you not see all model validation error messages ? Because that wouldn't make sense : suppose your model has a mandatory :gender
attribute with validates_presence_of :gender
. If normal model errors were added, then you would also see "gender cannot be blank" in the list of errors when your user tries to sign in with a wrong login :oops:.
devise_error_messages!
is a specific devise method meant to show those specific errors. You can think of it as a partial validation on the fields that are used for sign in (and that are defined in your devise config file)
WORKAROUND :
If you really want to show all your error messages, you could just explicitely run the validations :
at the beginning of devise_error_messages!
resource.validate # It will generate errors and populate `resource.errors`
I believe it shouldn't mess up with other actions that already work well (register, etc.)
这篇关于为什么设计不在登录页面上显示身份验证错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!