A way to add before_filter from engine to application

亡梦爱人 提交于 2019-11-27 03:36:59

问题


Without a lot of specific code, this is just vague, but I'll provide what I can.

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?

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.

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.

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.


回答1:


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


来源:https://stackoverflow.com/questions/7323327/a-way-to-add-before-filter-from-engine-to-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!