问题描述
没有很多具体的代码,这只是模糊的,但我会提供我能提供的.
Without a lot of specific code, this is just vague, but I'll provide what I can.
给定一个 rails 引擎,一个基本引擎,而不是它自己空间中的可安装引擎,我如何使来自引擎的方法可用于应用程序作为应用程序控制器的 before_filter?
Given a rails engine, a basic engine and not a mountable engine in its own space, how do I make methods from the engine available to the application as a before_filter for the applications controllers?
我一直在浏览 Devise 代码,因为我想要的内容与我使用 Devise 的经验相似,但我承认我不太了解我正在阅读的内容.
I've been looking through the Devise code, because what I want to is similar from my experience with Devise, but I'll admit I don't understand a lot of what I'm going over.
如果我将 include Myengine::Mymodule
放在我希望方法可用于过滤的应用程序控制器中,我可以做到这一点,但我希望可以只使用这些方法而不必包括模块.
I can do this if I put include Myengine::Mymodule
in the apps controllers where I want to have the methods available for filtering, but I want it possible to just use the methods without having to include the modules.
这是尝试将 rails 应用程序转变为供多个 rails 应用程序使用的引擎的最新障碍,不胜感激任何有关处理正确命名空间、模块配置等的指导.
This is the latest roadblock in trying to turn a rails application into an engine to be used by multiple rails apps, and any guidance on getting a handle on proper namespacing, module config, etc., is appreciated.
推荐答案
如果我理解正确,您可以使用初始化程序,例如:
If I understood you correctly you can use initializer, for example:
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 从引擎添加到应用程序的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!