Last Updated: February 25, 2016
·
573
· dinoreic

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.