2013-06-26 50 views
2

我試圖顯示,當用戶無法登錄閃光警報然而,會有兩種不同的情況:Rails + Devise:自定義登錄失敗的flash消息?

  1. 由用戶輸入的電子郵件已存在,但密碼是錯誤的。我希望此消息顯示:The email address or password you entered is incorrect.

  2. 用戶輸入的電子郵件不存在。我想這個消息顯示:The email address and password you entered do not match our records. (You provided <span class='black'>#{params[:email]}</span>)<br/><br/> <a href='/accounts/forgot_password' class='decorate'>Having trouble accessing your account?</a>

是否有類似after_sign_in_path在那裏我可以修改基於這些條件閃光燈鉤?

UPDATE

所以我發現了這個。但是,如何僅通過電子郵件變量傳遞not_found_in_database

en: 
    devise: 
    failure: 
     already_authenticated: "You are already signed in." 
     invalid: "The email address or password you entered is incorrect." 
     not_found_in_database: "The email address and password you entered do not match our records. (You provided <span class='black'>%{email}</span>)<br/><br/><a href='/accounts/forgot_password' class='decorate'>Having trouble accessing your account?</a>." 
+0

我正在尋找同樣的事情。初始failure.invalid和failure.not_found_in_database具有相同的消息(所以我在登錄頁面看到相同的失敗消息),這讓我認爲Devise沒有區分這兩種情況。 –

回答

3

我最終用這樣的:http://guides.rubyonrails.org/i18n.html#passing-variables-to-translations

這:

   <% if !flash[:alert].blank? %> 
        <% if flash[:alert][/The email address and password you entered do not match our records/] %> 
         <div class="flashNotice col last"> 
          <%= t('devise.failure.not_found_in_database', resource_name: "#{params[:user][:email]}").html_safe %> 
         </div> 
        <% else %> 
        <!-- Style for any warning message --> 
         <div class="flashNotice col last"><%= flash[:alert]%></div> 
        <% end %> 
       <% end %> 
+0

你可以分開密碼和電子郵件的消息..所以它是一個不同的信息兩個? –

2

我不會建議你區分這兩種情況。如果您提供此信息,可以從您的頁面挖掘電子郵件地址。他們只需要用隨機地址和虛擬密碼來轟炸你的系統,他們最終會找到一些有效的電子郵件。如果它是一個小頁面並不重要,但最好避免這個問題。

+1

通常,默認情況下在創建帳戶時會泄露此信息(它可以防止重複的電子郵件地址註冊),即使在偏執狂模式下也是如此。從註冊表單中挖掘電子郵件地址是可能的。所以,除非你也停止了泄漏,否則你並沒有真的失去任何真正的安全。 –

相關問題