A way to add before_filter from engine to applicat

2020-02-05 10:36发布

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条回答
Root(大扎)
2楼-- · 2020-02-05 10:50

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
查看更多
登录 后发表回答