Including Rails.application.routes.url_helpers from a module

less than 1 minute read

When I migrated from rails 2.0 to rails 3.0, I had to change inclusion of ActionController::UrlWriter to Rails.application.routes.url_helpers. I started to get strange errors like undefined 'default_url_options' when running my specs. The issue was that I was including a module himself including Rails.application.routes.url_helpers.

module PathBarHelper

  include Rails.application.routes.url_helpers
  ...

end

As if at module definition time, url_helpers was not yet completely ready. I changed the code to include url helpers through a hook :

module PathBarHelper

  def self.included(base)
   base.send :include Rails.application.routes.url_helpers
  end
  ...

end

That did the trick, but I must admit I did not dig the issue completely. Tell me if you did ?

I usually write about 15 minutes worth of reading per month. I won't transfer your email. No Spam, unsubscribe whenever you want.

As a gift for subscribing, you'll receive an illustrated mini-ebook "How to start a team coding dojo"!

Categories: ,

Updated:

Leave a comment