Engine routes in Application Controller

前端 未结 5 2128
予麋鹿
予麋鹿 2020-12-29 10:20

I have a before_filter hook in my main app\'s application controller that does something like: (It doesn\'t just put a link in the flash, there is a message, but it isn\'t r

5条回答
  •  萌比男神i
    2020-12-29 10:53

    Mountable engines are designed to work like this, that is isolate the main app routes and the engine routes.

    If you want the two sets of routes to be merged, you can use a non-isolated engine. The first step is removing the isolated_namespace method call in your engine definition:

    module MyEngine
      class Engine < Rails::Engine
        isolate_namespace MyEngine # remove this line
      end
    end
    

    The second step is to convert your routes in my_engine/config/routes.rb, you should go from this:

    MyEngine::Engine.routes.draw do
      # stuff that routes things
    end
    

    to this:

    Rails.application.routes.draw do
      # stuff that routes things
    end
    

    and remove the mount method call in your application's routes:

    App::Application.routes.draw do
      mount MyEngine::Engine => "/engine" # remove this line
    end
    

    The main advantages of doing it this way would be:

    1. No need to monkey-patch rails. I know devise does this, but this could be a leftover from the days when engines didn't exist in rails.

    2. No need to mount the engine in the application routes. On the other hand, this could backfire if you'd like to control more precisely the insertion point as all you engine routes would be called after (or before, I don't have the answer to this question) your main routes.

    If you're looking for documentation on engines, the rails docs for the Engine class are a pretty good starting point. I'd strongly recommend that you read them (in case you haven't yet) if you're interested in the subject.

提交回复
热议问题