2011-10-17 85 views
0

我有2個控制器Rails 3.控制器中的繼承回調方法

OauthController和Admin :: OauthController。 Admin :: OauthController從OauthController繼承時。

在OauthController實現的方法「回調」,這是公開的。

聯繫:: OauthController樣子:

class Admin::OauthController < OauthController 
end 

然後,如果我去的鏈接 「/管理/ facebook_oauth /回調」 得到錯誤

Unknown action 
The action 'callback' could not be found for Admin::OauthController 

但如果我加上「回調'在Admin :: OauthController中調用super的方法都可以工作。

class Admin::OauthController < OauthController  
    def callback 
    super 
    end 
end 

此行爲在遷移到Rails 3之後出現。其他方法繼承OauthController沒有問題。

回答

0

錯誤 「提高ActionNotFound」 提出,如果我們沒有在 「action_methods」

一個AbstractController :: Base的#internal_methods

# A list of method names that should be considered actions. This 
# includes all public instance methods on a controller, less 
# any internal methods (see #internal_methods), adding back in 
# any methods that are internal, but still exist on the class 
# itself. Finally, #hidden_actions are removed. 
# 
# ==== Returns 
# * <tt>array</tt> - A list of all methods that should be considered actions. 
def action_methods 
    @action_methods ||= begin 
    # All public instance methods of this class, including ancestors 
    methods = public_instance_methods(true).map { |m| m.to_s }.to_set - 
     # Except for public instance methods of Base and its ancestors 
     internal_methods.map { |m| m.to_s } + 
     # Be sure to include shadowed public instance methods of this class 
     public_instance_methods(false).map { |m| m.to_s } - 
     # And always exclude explicitly hidden actions 
     hidden_actions 

    # Clear out AS callback method pollution 
    methods.reject { |method| method =~ /_one_time_conditions/ } 
    end 
end 

不要進入該列表中所需的操作最終列表「AbstractController :: Base#internal_methods」

# A list of all internal methods for a controller. This finds the first 
    # abstract superclass of a controller, and gets a list of all public 
    # instance methods on that abstract class. Public instance methods of 
    # a controller would normally be considered action methods, so methods 
    # declared on abstract classes are being removed. 
    # (ActionController::Metal and ActionController::Base are defined as abstract) 
    def internal_methods 
    controller = self 
    controller = controller.superclass until controller.abstract? 
    controller.public_instance_methods(true) 
    end 

最後最有趣:

>> ActionController::Base.instance_methods.include?('callback') 
=> true