Rails + Devise:自定义登录失败flash消息?

时间:2013-06-26 17:55:47

标签: ruby-on-rails

我正在尝试显示用户何时无法登录的快速提醒。但是,会有两种不同的情况:

  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>

  3. 是否有类似于after_sign_in_path的钩子,我可以根据这些条件修改闪光灯?

    更新

    所以我找到了这个。但是,如何仅传递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>."
    

2 个答案:

答案 0 :(得分: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 %>

答案 1 :(得分:2)

我不建议你区分这两种情况。如果您提供此信息,则可以从您的页面中挖掘电子邮件地址。他们只需要用随机地址和伪密码轰炸您的系统,他们最终会找到一些有效的电子邮件。如果它是一个小页面并不重要,但最好避免这个问题。