我正在使用rails 4,bootstrap 3和Devise
我正在按照本教程将flash错误消息与devise集成 https://github.com/plataformatec/devise/wiki/How-To:-Integrate-I18n-Flash-Messages-with-Devise-and-Bootstrap
我在用户登录页面sessions/new.html.haml
= devise_error_messages!
这就是该方法存在的地方
/app/helpers/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
html = <<-HTML
<div class="alert alert-error alert-block"> <button type="button"
class="close" data-dismiss="alert">x</button>
#{messages}
</div>
HTML
html.html_safe
end
end
我的签到表格看起来像这样
= render 'layouts/messages'
= devise_error_messages!
%h4.header.blue.lighter.bigger
%i.ace-icon.fa.fa-coffee.green
Please Enter Your Information
.space-6
= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f|
%fieldset
%label.block.clearfix
%span.block.input-icon.input-icon-right
= f.email_field :email, class: 'form-control', autofocus: true, 'placeholder' => "email@example.com"
%i.ace-icon.fa.fa-user
%label.block.clearfix
%span.block.input-icon.input-icon-right
= f.password_field :password, class: 'form-control', autocomplete: "off", 'placeholder' => 'Password'
%i.ace-icon.fa.fa-lock
.space
-if devise_mapping.rememberable?
.clearfix
%label.inline
= f.check_box :remember_me, class: 'ace'
%span.lbl Remember Me
= button_tag( :class => 'width-35 pull-right btn btn-sm btn-primary' ) do
%i.ace-icon.fa.fa-key
Login
=render 'layout/messages'
有效,所以我知道有错误消息
但=devise_error_messages
没有。我意识到这一行
return "" if resource.errors.empty?
正在检查资源(在我的情况下是User)是否有任何错误。为什么我收到Flash错误消息,但资源上没有任何错误消息?