4

我在我的ApplicationController中有一個認證方法,我總是想先運行。我也有一個子控制器中的方法,我想在認證方法之後運行,但在另一個ApplicationController before_actions之前。換句話說,我想這一點:Rails:超類prepend_before_action

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

回答

6

可以使用prepend_before_action這樣的:

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

class OtherController < ApplicationController 
    prepend_before_action :third, :second 
end 
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 
+0

不幸的是,我的所有子類我不得不盡管添加此解決方法。我希望有一些方法可以修改ApplicationController來實現結果。 – Robert

+0

如果它在大多數控制器中,則可以在父項中運行:second_action並跳過少數子項。 –