How can I extend ApplicationController in a gem?

后端 未结 4 964
小鲜肉
小鲜肉 2020-12-15 20:32

I thought I\'d come up with a slick way to extend ApplicationController in a Rails 3.x gem.

In my gem\'s lib/my_namespace/my_controller.rb, I had:

4条回答
  •  自闭症患者
    2020-12-15 21:21

    Truth is much much simpler and flexible.

    Add to lib/engine.rb this: class Engine < Rails::Engine; end

    And then simply use:

    ActionController::Base.class_eval do
    
      include SomethingFromMineGemModule
    
      # or:
      def hello_from_gem
        'Hey people!'
      end
    
    end
    

提交回复
热议问题