Access rails helper methods anywhere [Rails4]
Add helper.rb to lib
class Helper
for el in Dir["#{Rails.root}/app/helpers/*.rb"]
extend el.split('/').last.split('.').first.classify.constantize
end
def self.method_missing(m, *args, &block)
ActionController::Base.helpers.send(m, *args, &block)
end
end
after that if you have
module FooHelper
def bar_123
...
you will be able to access it via
Helper.bar_123
We could put methods from helpers in separate namespace so it could look like this
Helper.foo.bar_123
but rails mixes all helpers in same soup, so that would not be helpful.
Written by Dino Reić
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Rails
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#