Ruby on rails 在应用程序中扩展ActionController::Base

Ruby on rails 在应用程序中扩展ActionController::Base,ruby-on-rails,ruby-on-rails-4,devise,actioncontroller,sorcery,Ruby On Rails,Ruby On Rails 4,Devise,Actioncontroller,Sorcery,我想向我的控制器和视图公开一个方法,就像Desive和Sorcery等插件公开当前用户的方法一样。事实上,我是在利用这个功能。然而,我试图为这个魔法找到正确的语法却没有成功。这是我到目前为止得到的 # config/initializers/extra_stuff.rb module ExtraStuff class Engine < Rails::Engine initializer "extend Controller with extra stuff" do |app|

我想向我的控制器和视图公开一个方法,就像Desive和Sorcery等插件公开当前用户的
方法一样。事实上,我是在利用这个功能。然而,我试图为这个魔法找到正确的语法却没有成功。这是我到目前为止得到的

# config/initializers/extra_stuff.rb
module ExtraStuff
  class Engine < Rails::Engine
    initializer "extend Controller with extra stuff" do |app|
      ActionController::Base.send(:include, ExtraStuff::Controller)
      ActionController::Base.helper_method :current_username
    end
  end
end

module ExtraStuff
  module Controller
    def self.included(klass)
      klass.class_eval do
        include InstanceMethods
      end
    end

    module InstanceMethods
      def current_username
        current_user.username
      end
    end
  end
end
这个方法的目的是特定于应用程序的,我不需要制作插件。我之所以提到这一点,是因为我迄今为止所挖掘的参考资料仅从构建Rails引擎/插件的角度讨论了这个问题。当然,在这一点上,这正是我的代码所做的,但它仍然不起作用。o、 o

行车轨道4.2

更新:我能够通过将
Rails::Engine
中的内容向上和向外移动来实现功能

module ExtraStuff
  module Controller
    def current_username
      current_user.username
    end
  end
end

ActionController::Base.send(:include, ExtraStuff::Controller)
ActionController::Base.helper_method :current_username
但是我不明白为什么这是必要的。Rails引擎应该已经使用
ActionController::Base
的扩展进行了初始化。我错过了什么

module ExtraStuff
  module Controller
    def current_username
      current_user.username
    end
  end
end

ActionController::Base.send(:include, ExtraStuff::Controller)
ActionController::Base.helper_method :current_username