简体   繁体   English

猴子跳线

[英]Monkey patching Rails

I need to monkey-patch one of the Rails core classes, specifically ActionView::Helpers::UrlHelper::ClassMethods.link_to method. 我需要猴子修补Rails核心类之一,特别是ActionView::Helpers::UrlHelper::ClassMethods.link_to方法。 As far as I remember there are some events fired when parts of Rails are loaded, how to add handlers for them? 据我记得,当部分Rails加载时会触发一些事件,如何为它们添加处理程序? Or should I just put the code into initializer? 还是应该将代码放入初始化程序中?

link_to does not appear to be in ClassMethods. link_to似乎不在ClassMethods中。 From here . 从这里

In config/initializers/url_helper_extensions.rb config/initializers/url_helper_extensions.rb

module ActionView
  module Helpers
    module UrlHelper
      alias_method :_link_to, :link_to
      def link_to

        # Your code ...

        # Call original method if you want.
        _link_to

      end
    end
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM