Rails 3.控制器中的继承回调方法

时间:2011-10-17 10:17:14

标签: ruby-on-rails ruby-on-rails-3 inheritance

我有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没有问题。

1 个答案:

答案 0 :(得分:0)

如果我们在“action_methods”列表中没有所需的操作,则会引发错误“引发ActionNotFound”

<强>一个AbstractController ::基地#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