如何在主应用程序中合并 Rails Engine ApplicationController 方法? [英] How to incorporate Rails Engine ApplicationController methods in a main app?

查看:37
本文介绍了如何在主应用程序中合并 Rails Engine ApplicationController 方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 Rails 引擎 ApplicationController(它的方法)合并到主应用程序中?我需要访问这些引擎控制器方法,而且我希望在主应用程序的 ApplicationController 中不使用Include".

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

以上发布于一种将 before_filter 从引擎添加到应用程序,但我的理解是助手只能查看可用,而我需要在我的控制器中访问它们.

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.

推荐答案

我之前为一个名为 dynamic_menu 的 rails gem 做过这个

I did this before for a rails gem called dynamic_menu

基本上看起来像

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

所以我假设你想要的是

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

你想添加你在 lib 中使用的 .rb 文件的 require,然后只需要将模块发送到 ActionController::Base

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

github 上查看我的 gem,它本质上非常简单,可能会提供一些指导.评论,我可以解释更多.

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.

这篇关于如何在主应用程序中合并 Rails Engine ApplicationController 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆