2017-10-16 16 views
0

我有這樣的代碼:在ApplicationController中調用超軌道

class ApplicationController < ActionController::Base 
    def authenticate_user!(*_args) 
    super 
    end 
end 

authenticate_user!我打電話super,這應該叫authenticate_user!ActionController::Base,這是一個抽象類。

我找不到authenticate_user!裏面ActionController::Base,所以我想有一些元編程涉及(?)。

Devise的authenticate_user!怎麼樣在ActionController::Base?或者在ApplicationController中使用super調用的任何其他方法?

回答

0

authenticate_user!是一種由devise動態構建的id的方法,所以你必須猴子補丁devise。助手在兩個控制器和視圖可用,所以這可能是你爲什麼不打ActionController::Base

如果你需要重寫功能,你需要猴子補丁這個類:https://github.com/plataformatec/devise/blob/88724e10adaf9ffd1d8dbfbaadda2b9d40de756a/lib/devise/controllers/helpers.rb#L148

可以覆蓋authenticate_user!通過定義你自己的方法。首先,你必須創建目錄lib/devise/controllers,添加一個名爲helpers.rb文件,您可以向其中添加:

模塊設計 模塊控制器 模塊助手

module ClassMethods 
    def authenticate_user! 
     # if something 
     # do something 
     # else 
     super 
     # end 
    end 
    end 

如果你不使用devise ,發表評論,我會刪除這個答案