Rails:超类中的prepend_before_action

Rob*_*ert 5 controller ruby-on-rails before-filter operator-precedence

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

ApplicationController
before_action first
before_action third

OtherController < ApplicationController
before_action second
Run Code Online (Sandbox Code Playgroud)

以上原因导致按以下顺序调用方法:first- > third- > second.但是我想要命令:first- > second- > third.

我尝试过使用prepend_before_action,如下所示:

ApplicationController
prepend_before_action first
before_action third

OtherController < ApplicationController
prepend_before_action second
Run Code Online (Sandbox Code Playgroud)

但这会导致它second- > first- > third.

如何获得订单first- > second- > third

Sam*_* P. 8

你可以这样使用prepend_before_action:

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

class OtherController < ApplicationController
  prepend_before_action :third, :second
end
Run Code Online (Sandbox Code Playgroud)