Yes, I know!
Helpers are modules that provide methods which are automatically usable in your view.
But sometimes you want to use it in controllers ;) This is my solution. Helpers are available in module named 'Helpers', so they don't brake anything. All rails helpers and named routes are included by default. This is simple usage:
class ExampleController < ApplicationController
ApplicationHelper, PicturesHelper
include_helper def show
...:notice] = Helpers::flash_with_picture('Hello from helper')
flash[
...end
end
And method to add to ApplicationController class.
class ApplicationController < ActionController::Base
def self.include_helper(*args)
'action_controller/integration'
require class_eval do
'Helpers') ? const_get('Helpers') : Module.new do
helpers = const_defined?(@@controller = ActionController::Integration::Session.new
def self.method_missing(method, *args, &block)
if @@controller && method.to_s =~ /_path$|_url$/
@@controller.send(method, *args, &block)
else
NoMethodError, "undefined method `#{method}' for #{self}"
raise end
end
end
ActionView::Helpers.constants.each do |constant|
ActionView::Helpers.const_get(constant) if ActionView::Helpers.const_get(constant).instance_of?(Module)
helpers.extend end
if args.instance_of?(Array)
do |helper|
args.each if helper.instance_of?(Module)
helpers.extend helper end
elsif args.instance_of?(Module)
helpers.extend argsend
'Helpers', helpers ) unless const_defined?('Helpers')
const_set( end
end
end