[英]How to incorporate Rails Engine ApplicationController methods in a main app?
如何將Rails引擎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
上面的內容發布在一種從引擎到應用程序中添加before_filter的方法上 ,但是我的理解是,助手僅是視圖可用的,而我需要在控制器中訪問它們。
我之前曾對名為dynamic_menu的rails寶石進行過此操作
基本上看起來像
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文件的需求,然后只需將模塊發送到ActionController :: Base
在github上查看我的gem,它本質上很簡單,也許可以提供一些指導。 評論,我可以進一步解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.