Ruby on Rails Engine vs Mountable App

Posted on Jul 26, 2019

Full Engine

With a full engine, the parent application inherits the routes from the engine. It is not necessary to specify anything in parent_app/config/routes.rb. Specifying the gem in Gemfile is enough for the parent app to inherit the models, routes etc. The engine routes are specified as:

# new_engine/config/routes.rb 
Rails.application.routes.draw do 
  # whatever 

No namespacing of models, controllers, etc. These are immediately accessible to the parent application.

Mountable Engine

The engine’s namespace is isolated by default.

# new_engine/lib/new_engine/engine.rb
module NewEngine 
  class Engine < Rails::Engine 
    isolate_namespace NewEngine 

With a mountable engine, the routes are namespaced and the parent app can bundle this functionality under a single route:

# new_engine/config/routes.rb 
NewEngine::Engine.routes.draw do 

# parent_app/config/routes.rb 
ParentApp::Application.routes.draw do 
    mount NewEngine::Engine => "/new_engine", :as => "namespaced" 

Models, controllers, etc are isolated from the parent application – although helpers can be shared easily.

These are the main differences I have spotted. My impression is that since a full engine does not isolate itself from the parent application, it is best used as a standalone application adjacent to the parent app.

A mountable engine could be used in situations where you want to avoid name conflicts and bundle the engine under one specific route in the parent application. For example, I am working on building my first engine designed for customer service. The parent application could bundle it’s functionality under a single route such as:

mount Help::Engine => "/help", :as => "help"