Topic: Included helper method doesn't work

Hello community,

i have a self written class (located in lib directory) which uses the javascript_include_tag helper.
This is my code:

class DocHead

  include Singleton
  extend ActionView::Helpers::AssetTagHelper
  
  def self.add_script(script, options = {})
    @@output << javascript_include_tag(script, options) << "\n"
  end

end 

This code worked in Rails 2 but in version 3 I get the error message "undefined local variable or method `config' for DocHead:Class"

The framework told me that the issue must be in:

actionpack (3.0.3) lib/action_view/helpers/asset_tag_helper.rb:351:in `javascript_include_tag'

I suppose that the rails application config is missing there. Am I right?
What do I need to do to provide the config to this method?

Regards Krani

Re: Included helper method doesn't work

Is your 3.0 app otherwise running?   

A lot of times uninitialized 'config' errors can be traced back to a bad environment.rb file.

Did you muck around much with environment.rb when you upgraded to 3.0?

The next thing I'd do is see how app initialization changed in 3.0,  maybe something changed about how the lib dir is processed at startup?

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: Included helper method doesn't work

Hi, thanks for that hint...
After removing the corresponding class, the app runs as good as possible (the purpose of the class DocHead is generating the html headers, so it doesn't look nice without it ;-)

In Rails 3 nearly the complete config part moved from environment.rb to config/application.rb
Also the classe from lib directory don't get loaded automatically in Rails 3. So I needed to add this to config/application.rb:

config.autoload_paths += %W(#{config.root}/lib)

Maybe my issue is caused by this? Please help me...

Re: Included helper method doesn't work

Not an ideal solution, but would this do as a workaround:

class DocHead

  include Singleton

  def self.helpers
    ApplicationController.helpers
  end

  def self.add_script(script, options = {})
    @@output << helpers.javascript_include_tag(script, options) << "\n"
  end

end 

It means you're now depending on application controller but at least you can let that take care of the config.

A quick google found a similar idea here:
http://www.ruby-forum.com/topic/243913

Rob Anderton
TheWebFellas

Re: Included helper method doesn't work

Hi rob-twf,
your solution works, the linked solution does not. Thanks a lot!

I don't understand this configuration part.
config is alway a local variable in these methods which has been nowhere instantiated. How does this work?

Re: Included helper method doesn't work

config is defined in ActionView::Base and if the view has been initialised with a controller it will inherit its config from there. Take a look at the source and search for "config" to see what I mean: ActionView::Base

Looking at the code another option would be to define a config attribute in your DocHead class, but you'd have to experiment with that big_smile

Rob Anderton
TheWebFellas