Rails: using helper methods from config
How would I go about using helper methods from a config file?
This may be unorthodox but it makes sense in the context that I need it. The helper method I need is used to properly format URLs in one of my views. The config file is the initializer for the sitemap_generator Gem, where I need the helper method to format some of the URLs.
I tried require, which does indeed include the module, but get undefin开发者_开发技巧ed method for module when I call it.
This is how you include your helper in the SitemapGenerator config :
SitemapGenerator::Interpreter.send :include, ApplicationHelper
Does your method need to be in a helper? Can't you put it in a class in your lib
folder?
# lib/my_url_helper.rb
class MyUrlHelper
def self.proper_format(...)
...
end
end
# app/helpers/application_helper.rb
def proper_format(*args)
MyUrlHelper.proper_format(*args)
end
Tip / personal experience: Most of the time you do something "unorthodox" you are trying to solve the wrong problem.
精彩评论