2011-02-04 60 views
0

我正在使用Rails 3和Devise。Rails 3,在認證失敗時設計不顯示錯誤消息

我在{rails_root} /lib/custom_failure.rb和我的設計初始值設定項中加載了所需的代碼。

class CustomFailure < Devise::FailureApp 

    def redirect_url 
    root_path 
    end 

    def respond 
    if http_auth? 
     http_auth 
    else 
     redirect_to root_url 
    end 
    end 
end 

該文件被加載,並且當認證失敗時用戶被重定向到root_url。問題是沒有錯誤消息顯示身份驗證失敗時,但所有其他設計消息工作正常(成功登錄等)。

我有這個在我的應用程序佈局

<% flash.each do |name, msg| %> 
    <%= content_tag :div, msg, :id => "flash_#{name}" %> 
    <% end -%> 

編輯: 最後的工作代碼。原來我最大的錯誤是沒有重啓服務器。 CustomFailure是一個lib,只加載一次。

def respond 
    if http_auth? 
     http_auth 
    else 
     flash[:notice] = "Error message goes here" 
     redirect_to root_url 
    end 
    end 

回答

2

難道是閃光消息被重定向吃掉了嗎?該閃光燈僅適用於下一個請求,所以也許重定向正在獲取閃光燈消息,但它在下一個請求(root_url)上消失。您可能必須執行內部重定向或將閃光變量傳遞給下一個請求。

我可以問爲什麼你在做失敗時的外部重定向,而不是再次顯示錯誤消息的登錄表單?

+0

我想你可能是正確的有關Flash被重定向被吃掉,可惜我不知道制定內部不夠好,找出正確的將它們傳遞給下一個請求。重定向完成是因爲客戶端可能不合理,並且通常不知道其具有良好的可用性:D – Marc 2011-02-04 01:14:46

2

您也可以只添加flash[:alert]在響應行動:

class CustomFailure < Devise::FailureApp 
    def redirect_url 
     root_url 
    end 
    def respond 
     if http_auth? 
      http_auth 
     else 
      flash[:alert] = i18n_message unless flash[:notice] 
      redirect_to root_url 
     end 
    end 
end