【问题标题】:how to use before_action with after_sign_in_path_for and after_inactive_sign_up_path_for methods?如何将 before_action 与 after_sign_in_path_for 和 after_inactive_sign_up_path_for 方法一起使用?
【发布时间】:2016-09-06 01:21:39
【问题描述】:

我想使用 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方法应该不会影响这两个方法吧?但是,在使用前操作和应用程序控制器中的两种方法时,我会重定向到错误的路由

标签: ruby-on-rails ruby model-view-controller devise before-filter


【解决方案1】:

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

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

【讨论】:

  • 这个我知道,但是好像before_action影响了after_sign_in_path_for方法的重定向,请问应该如何解决呢?
  • 请贴一些代码,否则真的很难帮助
  • 我发布了代码以及我收到的错误消息
  • 按照教程中的说明执行after_sign_in_paths。关于authenticate! 方法我不确定,因为我不知道current_customercurrent_vendor 在调用什么。这些方法是在哪里定义的?
  • 我也没有看到用户在登录和退出后被重定向到仪表板/编辑他的帐户的感觉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
  • 2020-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多