2

How do I incorporate a Rails engine ApplicationController (it's methods) into a main app? I need to access these engine controller methods, and I'd like to do it without using an 'Include' in my main app's ApplicationController.

module MyEngine
  class Engine < Rails::Engine
    initializer  "myengine.load_helpers" do
      ActiveSupport.on_load(:action_controller) do
        include MyEngine::Helpers
      end
    end
  end
end

The above was posted on A way to add before_filter from engine to application, but my understanding was that helpers are only view-usable, while I need need to access them in my controllers.

Community
  • 1
  • 1
Carson Cole
  • 4,183
  • 6
  • 25
  • 35

1 Answers1

7

I did this before for a rails gem called dynamic_menu

basically it looks like

require 'dynamic_menu'

module DynamicMenu
  class Engine < Rails::Engine
    initializer "dynamic_menu.menu_items" do |app|
      ActionController::Base.send :include, DynamicMenu::MenuItems
    end
  end
end

So I would assume the one you would want would be

require 'myEngine'
class Engine < Rails::Engine
  initializer "myengine.load_helpers" do |app|
    ActionController::Base.send :include, MyEngine::Helpers
  end
end

You want to add the require of the .rb file you are using found in lib, then it just involves sending the module to the ActionController::Base

See my gem on github it is pretty simple in nature and may be able to give some guidance. Comment and I can explain it more.

Jamon Holmgren
  • 23,738
  • 6
  • 59
  • 75
Travis Pessetto
  • 3,260
  • 4
  • 27
  • 55
  • Ooops, meant to say 'without', not 'with using an "Include"'. Edited my post with the fix. So I definitely want an engine and want it seamlessly incorporated into my main app. – Carson Cole Jun 20 '12 at 17:13
  • @C Cole that makes quite a bit more sense. I will update the answer – Travis Pessetto Jun 20 '12 at 17:18