Rails:超类中的prepend_before_action

时间:2016-02-22 18:01:45

标签: ruby-on-rails controller before-filter operator-precedence

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

ApplicationController
before_action first
before_action third

OtherController < ApplicationController
before_action second

以上原因导致按以下顺序调用方法:first - &gt; third - &gt; second。 但是我想要订单:first - &gt; second - &gt; third

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

ApplicationController
prepend_before_action first
before_action third

OtherController < ApplicationController
prepend_before_action second

但这导致它second - &gt; first - &gt; third

如何将订单设为first - &gt; second - &gt; third

2 个答案:

答案 0 :(得分:8)

您可以像这样使用prepend_before_action

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

class OtherController < ApplicationController
  prepend_before_action :third, :second
end

答案 1 :(得分:1)

尝试以下操作,看看它是否有效:

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