如何将 before_action 与 after_sign_in_path_for 和 after_inactive_sign_up_path_for 方法一起使用?

how to use before_action with after_sign_in_path_for and after_inactive_sign_up_path_for methods?

我想使用 after_sign_in_path_forafter_inactive_sign_up_path_for 方法将用户重定向到某个特定页面,我会将这两个方法和 before_action :authenticate_user! 全部放在应用程序控制器中,但是,当 before_action 方法 运行 时,所有操作都会调用它,这会将我的应用程序重定向到错误的路由。我应该使用 before_action :authenticate_user!, except: [:after_sign_in_path_for, :after_inactive_sign_up_path_for] 跳过这两种方法的身份验证吗?

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  before_action :authenticate!


  def after_sign_in_path_for(user)
      if user && user.is_a?(Vendor)
          return edit_vendor_registration_path
          elsif user && user.is_a?(Customer)
          return dashboard_path
      end
  end

  def after_sign_out_path_for(user)
      if user && user.is_a?(Vendor)
          return root_path
          elsif user && user.is_a?(Customer)
          return root_path
      end
  end


  def authenticate!
      if @current_user == current_customer
          :authenticate_customer!
          elsif @current_user == current_vendor
          :authenticate_vendor!
      end
  end
end

我 运行 进入这个错误 Filter chain halted as :require_no_authentication rendered or redirected,我相信该程序以某种方式创建了一个不断重定向到 dashboard_path.

的无限循环

我认为你混淆了一些东西。

before_action :authenticate_user! 用于您希望对用户进行身份验证的每个控制器操作,以便他可以继续他的请求。

例如after_sign_in_path_for 是设计控制器的一种方法,可以像这样重写:https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-up-%28registration%29