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

发布于 2024-12-10 17:23:40 字数 701 浏览 0 评论 0原文

我有 2 个控制器

OauthController 和 Admin::OauthController。当Admin::OauthController继承自OauthController时。

在 OauthController 中实现了“callback”方法,该方法是公共的。

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 的“回调”方法,则一切正常。

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

这种行为是迁移到Rails 3后出现的。其他方法继承OauthController没有问题。

I have 2 controllers

OauthController and Admin::OauthController. When Admin::OauthController inherited from OauthController.

In OauthController implemented method "callback", which is public.

Admin::OauthController looks like:

class Admin::OauthController < OauthController
end

Then if I go to the link "/admin/facebook_oauth/callback" get error

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

But if I add 'callback' method with call super in Admin::OauthController all works.

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

This behavior appears after migration to Rails 3. Other methods are inherited OauthController without problems.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扮仙女 2024-12-17 17:23:40

如果我们在“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

Error "raise ActionNotFound" raised if we do not have the desired action in the list of "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

Do not get into the final list "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

And finally the most interesting:

>> ActionController::Base.instance_methods.include?('callback')
=> true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文