1

I monkey patched the polymorphic_path method, because I wanted to add a nil locale whenever the locale was the same as the default locale.

The method looks like this:

module ActionDispatch
    module Routing
        module PolymorphicRoutes
            alias_method :old_polymorphic_path, :polymorphic_path
            def polymorphic_path(record_or_hash_or_array, options = {})
                opt = options.clone
                if I18n.locale == I18n.default_locale
                    opt = opt.merge({locale: nil})
                end
                old_polymorphic_path(record_or_hash_or_array, opt)
            end
        end
    end
end

Everything works like a charm, except if I call a new instance of polymophic_path in any given controller or helper. And then change the params the server crashes with "stack level too deep.

It is calling the "old_polymorphic_path(record_or_hash_or_array, opt)" a million times.This does not happen in the views however.

Is this the proper way to monkey patch the method? What is causing this "stack level too deep" error and how to fix it?

0 Answers0