简体   繁体   English

如何在主应用程序中合并Rails Engine ApplicationController方法?

[英]How to incorporate Rails Engine ApplicationController methods in a main app?

How do I incorporate a Rails engine ApplicationController (it's methods) into a main app? 如何将Rails引擎ApplicationController(它的方法)合并到主应用程序中? 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. 我需要访问这些引擎控制器方法,并且我想在主应用程序的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. 上面的内容发布在一种从引擎到应用程序中添加before_filter的方法上 ,但是我的理解是,助手仅是视图可用的,而我需要在控制器中访问它们。

I did this before for a rails gem called dynamic_menu 我之前曾对名为dynamic_menu的rails宝石进行过此操作

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 您想添加在lib中找到的正在使用的.rb文件的需求,然后只需将模块发送到ActionController :: Base

See my gem on github it is pretty simple in nature and may be able to give some guidance. github上查看我的gem,它本质上很简单,也许可以提供一些指导。 Comment and I can explain it more. 评论,我可以进一步解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM