在 Destroy_User_Session_Path 之前设计行动

Devise Action Before Destroy_User_Session_Path

当用户点击 'logout' 时,我的应用 运行ning destroy_user_session_path 运行良好。但是,在 destroy_user_session_path 发生之前,我需要 运行 来自 carts_controller 的 destroy 方法。单击注销按钮时,运行 这两个操作的最佳方法是什么? (我在应用程序中找不到设计控制器或会话控制器)

application.html.erb - 销毁注销按钮调用

<%= link_to "Logout", destroy_user_session_path, method: :delete, :class => 'navbar-link boldLinks btn btn-primary'  %>

carts/show.html.erb - Remove/empty 单击一个按钮时销毁前需要购物车调用

<%= link_to 'Empty Cart', @cart, method: :delete, data: {confirm: 'Are you sure you want to empty your cart?'}, :class => 'btn btn-danger whiteText' %>

我认为你可以用这种方式扩展设计 SessionsController。

编辑后的答案:

创建子class 设计会话控制器,首先在routes.rb 中告诉设计您想要使用自己的会话控制器:

devise_for :users, controllers: { sessions: 'users/sessions' }

并在您的控制器中创建一个文件,例如 sessions_controller.rb

class Users::SessionsController < Devise::SessionsController

  before_action :destroy_cart, only: :destroy

  def destroy_cart
    # your command here
  end
end

this link就是我们要用的class