为Devise成功/失败定制Flash消息

问题描述 投票:0回答:1

我试图根据用户会话成功或登录失败生成不同的外观闪存消息。 (主要通过改变jped图像)

我有部分视图处理flash消息并检查密钥然后显示不同的图像:

_flash_message.html.erb

<% if flash.present? %>
    <% case flash.first[0] %>

<% when "devise_deconnexion" %>

        <div id="flash-message">
            <div id="image">
                <%= image_tag "deconnecte.svg" %>
            </div>
            <div id="affiche">
                <div id="message">
                    <h1>Succès</h1>
                    <h2><%= flash.first[1] %></h2>
                </div>
                <div id="check" style="background-color: #00e691;">
                    <%= image_tag "check_blanc.svg" %>
                </div>
            </div>
        </div>

...

在上面的位中,我检查密钥是否与“devise_deconnexion”字符串匹配,以便在flash消息中显示不同的图像。

我已经能够通过为每个Devise模型生成设计会话控制器并按以下方式进行更改来进行调整:

sessions_controller.rb

def destroy
    #   super    
    signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
    set_flash_message! :devise_deconnexion, :signed_out if signed_out
    yield if block_given?
    respond_to_on_destroy
  end

它很棒。

虽然在用户键入错误密码的情况下我很难更改图像。我不知道在哪里修改闪存密钥。

这是Devise会话#create code from github:

 # POST /resource/sign_in
  def create
    self.resource = warden.authenticate!(auth_options)
    set_flash_message!(:notice, :signed_in)
    sign_in(resource_name, resource)
    yield resource if block_given?
    respond_with resource, location: after_sign_in_path_for(resource)
  end

我只看到与:notice消息一起使用的:signed_in密钥。

我无法看到“错误密码或用户名”的flash消息被触发的位置(虽然输入错误密码时确实收到了flash消息)

ruby-on-rails devise
1个回答
1
投票

当用户键入错误的密码时,控制器中的代码执行将停在此行:

self.resource = warden.authenticate!(auth_options)

之后,失败的请求由所谓的“设计失败应用程序”处理。

https://github.com/plataformatec/devise/blob/master/lib/devise/failure_app.rb

您可以使用自定义的应用程序替换该Failure应用程序。

1)创建自定义失败应用程序:

class CustomFailureApp < Devise::FailureApp
    # your custom code goes here ....
end

2)告诉Devise使用您的自定义故障应用程序

# initializers/devise.rb
Devise.setup do |config|
  config.warden do |manager|
    manager.failure_app = CustomFailureApp
  end
end

如何自定义Devise's Failure应用程序以实现您的目标是您要弄清楚的。祝好运!

© www.soinside.com 2019 - 2024. All rights reserved.