Rails: prepend_before_action 在超类中

Rails: prepend_before_action in superclass

我的 ApplicationController 中有一个我总是想先 运行 的身份验证方法。我在子控制器中也有一个方法,我想在身份验证方法之后 运行,但在另一个 ApplicationController before_actions 之前。换句话说,我想要这个:

ApplicationController
before_action first
before_action third

OtherController < ApplicationController
before_action second

以上导致方法按以下顺序调用:first -> third -> second。 但我希望顺序为:first -> second -> third.

我试过使用 prepend_before_action,像这样:

ApplicationController
prepend_before_action first
before_action third

OtherController < ApplicationController
prepend_before_action second

但这会导致它变为 second -> first -> third

如何让订单成为 first -> second -> third

尝试以下方法,看看是否有效:

class ApplicationController < ActionController::Base
  before_action :first
  before_action :third
end

class OtherController < ApplicationController
  skip_before_action :third
  before_action :second
  before_action :third
end

您可以像这样使用 prepend_before_action

class ApplicationController < ActionController::Base
  before_action :first
  before_action :third
end

class OtherController < ApplicationController
  prepend_before_action :third, :second
end