views:

36

answers:

1

I need to use function "image_path" in my lib class. I tried this (and couple of other variations):

class CustomHelpers::Base
  include ActionView::Helpers::AssetTagHelper
  def self.image_url(source)
    abs_path = image_path(source)
    unless abs_path =~ /^http/
      abs_path = "#{request.protocol}#{request.host_with_port}#{abs_path}"
    end
    abs_path
  end
end

But it didn't work. Am I doing it right?

Another question is, how do I find the right class to include? For example if I look at this module: http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html is there a rule of thumb how to include that module in a model / library / class / anything else ?

+2  A: 

You include a module, so all methods on it are in InstanceMethods.

But you try call it by ClassMethods.

So try extend ActionView::Helpers::AssetTagHelper, not include it

shingara
Thanks for your answer, much appreciated. However I got this error: undefined method `image_path' for ActionView::Helpers::AssetTagHelper:Module what does it mean?
jaycode
it's if you extend AssetTagHelper on your class ?
shingara
Thanks! I now know the difference between extend and include, your answer now made sense for me. Learning the fundamentals is very important, apparently
jaycode