我的 ApplicationController 中有一个身份验证方法,我总是想先运行它。我在子 Controller 中还有一个方法,我想在身份验证方法之后运行,但在其他 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 的订单?

最佳答案

您可以像这样使用 prepend_before_action:

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

class OtherController < ApplicationController
  prepend_before_action :third, :second
end

10-07 20:47