设计自定义错误消息不起作用

时间:2014-10-27 16:14:37

标签: ruby-on-rails ruby flash devise twitter-bootstrap-3

我正在使用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错误消息,但资源上没有任何错误消息?

0 个答案:

没有答案