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 ?

Categories: ,

Updated:

Comments