簡體   English   中英

Rails 3.控制器中的繼承回調方法

[英]Rails 3. Inheritance callback method in the controller

我有2個控制器

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

在OauthController中實現的方法“回調”是公開的。

Admin :: OauthController看起來像:

class Admin::OauthController < OauthController
end

然后,如果我轉到鏈接“ / admin / facebook_oauth / callback”,則會出現錯誤

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

但是,如果我在Admin :: OauthController中添加帶有call super的'callback'方法,那么所有方法都可以使用。

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

遷移到Rails 3后,將出現此行為。其他方法繼承自OauthController,沒有問題。

如果我們在“ action_methods”列表中沒有所需的動作,則會引發錯誤“ raise ActionNotFound”

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM