2014-05-09 54 views
1

我使用Devise爲我的登錄。除了一個例外,一切正常。如果用戶在登錄頁面輸入錯誤或沒有值,則什麼都不會發生。沒有錯誤或錯誤信息(使用正確的數據登錄正常工作)。沒有驗證錯誤與設計

我只在/ session/new頁面上有這個問題。

這是我的看法

.container 


.row.text_content_top 
    .alert.alert-danger 
     %button.close{ type: "button", "data-dismiss" => "alert"} × 
     = devise_error_messages! 
    .row.text_content_top 
    .col-md-4.col-md-offset-4 
     %h2 Sign in 
    .row.text_content 
    .col-md-4.col-md-offset-4.well 
     = form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| 
     = devise_error_messages! 
     .input-group.has-feedback 
      %span.input-group-addon 
      %i.fa.fa-envelope-o.fa-fw 
      = f.label :email, class: 'sr-only' 
      = f.email_field :email, autofocus: true, class: 'form-control', placeholder: 'E-Mail' 
     .input-group.has-feedback.top-buffer-10 
      %span.input-group-addon 
      %i.fa.fa-key.fa-fw 
      = f.label :password, class: 'sr-only' 
      = f.password_field :password, autocomplete: 'off', class: 'form-control', placeholder: 'Password' 
     = f.submit "Login", class: 'btn btn-success btn-large top-buffer-10' 
     = link_to "Forgot your password?", new_password_path(resource_name), class: 'top-buffer-10' 

而且我的模型

class User < ActiveRecord::Base 
    has_many :orders 

    validates :email, presence: true, uniqueness: true 

    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :lockable 

end 

我在控制器或其他自定義代碼

THX無碼提前

+2

[這個答案](http://stackoverflow.com/a/4640633/2856441)的問題,問同樣的事情爲我工作。 –

+0

@ JKen13579的鏈接也是我們使用的! Devise不使用Rails中的標準Flash對象 –

+0

Thx JKen13579這可以正常工作 – ThreeFingerMark

回答

1

在光@JKen13579的回答,你需要注意Devise不會存儲錯誤信息以傳統方式登錄的年齡(使用errors object)。相反,Devise使用flash,這是一些人關閉。


有一種方法來integrate Devise error messages到您的應用程序,使用this教程:

對於色器件,你需要重寫色器件處理提示信息的方式。 在「app/helpers/devise_helper.rb」中創建一個名爲devise_helper的文件。

在文件中,您必須創建一個名爲 的方法devise_error_messages!,這是告訴 設計如何處理Flash消息的文件的名稱。

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 

<%= devise_error_messages! %>