简体   繁体   中英

Monkey patching Rails

I need to monkey-patch one of the Rails core classes, specifically ActionView::Helpers::UrlHelper::ClassMethods.link_to method. As far as I remember there are some events fired when parts of Rails are loaded, how to add handlers for them? Or should I just put the code into initializer?

link_to does not appear to be in ClassMethods. From here .

In 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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