2014-03-04 57 views
1

我正在使用Devise 3.2.3和Rails 4.1.0.rc1,而我只是想在錯誤的登錄發生時顯示錯誤。設計錯誤無效?

這裏是形式:

<div class="row"> 
    <div class="login-box"> 
    <h2>Sign in</h2> 

    <%= form_for resource, :as => resource_name, :url => session_path(resource_name), html: { class: "form-horizontal" } do |f| %> 
     <%= devise_error_messages! %> 

     <fieldset> 
     <%= f.email_field :email, class: "input-large col-xs-12", placeholder: "Email", :autofocus => true %> 

     <%= f.password_field :password, class: "input-large col-xs-12", placeholder: "Password" %> 

     <div class="clearfix"></div> 
     <div class="field-spacer"></div> 

     <%#= f.password_field :password_confirmation, class: "input-large col-xs-12", placeholder: "Confirm Password" %> 

     <%= f.check_box :remember_me %><%= f.label :remember_me, "Remember Me?" %> 

     <div class="field-spacer"></div> 

     <%= f.submit "Sign in", class: "btn btn-primary col-xs-12" %> 

     <div class="clearfix"></div> 

     <div class="text-center margin-top-10"><a href="<%= new_user_registration_path %>" class="underline">No account? Sign up here.</a></div> 

     <div class="clearfix"></div> 
     <div class="field-spacer"></div> 

     </fieldset> 
    <% end %> 

    <hr> 
    <h3>Forgot Password?</h3> 
    <p> 
     No problem, <a href="<%= new_user_password_path %>" class="underline">click here</a> to get a new password. 
    </p> 
    </div> 
</div><!--/row--> 

    </div><!--/row--> 

這裏是跟蹤:

Started POST "https://stackoverflow.com/users/sign_in" for 127.0.0.1 at 2014-03-03 17:43:50 -0600 
Processing by Devise::SessionsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"QuDKO+mP/iupA5k3ofeBO4Hr3mvFNZmyZooaBxNE0K4=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"} 
    User Load (1.4ms) SELECT "users".* FROM "users" WHERE "users"."email" = '[email protected]' ORDER BY "users"."id" ASC LIMIT 1 
Completed 401 Unauthorized in 7ms 
Processing by Devise::SessionsController#new as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"QuDKO+mP/iupA5k3ofeBO4Hr3mvFNZmyZooaBxNE0K4=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Sign in"} 
    Rendered devise/sessions/new.html.erb within layouts/application (1.1ms) 
    Rendered shared/_head.html.erb (1.2ms) 
    Rendered shared/_errors.html.erb (0.0ms) 
    Rendered shared/_header.html.erb (0.6ms) 
    Rendered shared/_footer.html.erb (0.1ms) 
Completed 200 OK in 69ms (Views: 8.8ms | ActiveRecord: 0.0ms) 

任何想法,這是爲什麼不返回錯誤的視圖?

更新1:

我把這個文件:

module DeviseHelper 
    def devise_error_messages! 
    debugger 
    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 

我使用的調試,看看發生了什麼事。當我在電子郵件或密碼字段中提交時沒有任何信息(應該引發設計錯誤)時,該助手方法中的resource沒有任何錯誤。但是,在調試器控制檯中,如果我運行resource.valid?,然後讓它完成運行,它會正確地創建錯誤。這裏有什麼問題?

回答

0

設計錯誤信息被添加到閃爍,只要你application.html.erb文件包括他們應該表現出,你可以閱讀瑞安貝茨優秀railscast細節

http://railscasts.com/episodes/209-devise-revised?view=asciicast

也看看這個後 Devise not displaying error messge during an authentication failure?

+0

他們實際上沒有被添加到Flash消息。 – trevorhinesley

+1

噢好吧,以防萬一你沒有看到這個http://stackoverflow.com/questions/4112103/devise-not-displaying-error-messge-during-an-authentication-failure – archie

+0

這就是我的朋友,可以你編輯你原來的帖子來指向那個鏈接?如果是這樣的話,我會將你標記爲贏家:) – trevorhinesley