Including Rails.application.routes.url_helpers From a Module

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

1
undefined 'default_url_options'

when running my specs. The issue was that I was including a module himself including Rails.application.routes.url_helpers.

1
2
3
4
5
6
module PathBarHelper

  include Rails.application.routes.url_helpers
  ...

end

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

1
2
3
4
5
6
7
8
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 completly. Tell me if you did ?

Comments