【问题标题】:Rails + Devise: How to catch the event of logging out a user?Rails + Devise:如何捕捉注销用户的事件?
【发布时间】:2021-04-27 19:49:03
【问题描述】:

我正在记录用户的活动,当用户注销时,我想调用一个方法。我该怎么做?

在路线中,我有以下内容:

devise_scope :user do
  get 'logout',                             to: 'devise/sessions#destroy'
end

我需要为此创建一个 SessionController 还是有其他方法?

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    因为 Rails 是一个 MVC 框架而不是面向事件的,所以你并没有真正“抓住事件”。

    相反,您将控制器子类化并使用块调用 super 以“接入”原始实现:

    devise_scope :user, controllers: {
      sessions: 'my_sessions'
    }
    
    class MySessionController < ApplicationController
      def destroy
        super do
          # do your thing
          # this block is called after the user is signed out 
          # but before the response is set. 
        end
      end
    end
    

    这从the Devise controllers yield开始有效。

    【讨论】:

      猜你喜欢
      • 2020-08-22
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多