Skip to content

Rails Engines

An engine is a Rails application inside another one: its own models, controllers, views, routes, migrations and assets, namespaced and mountable at a path. It is the mechanism behind Devise, Spree and Administrate.

“An engine is a gem with a Rails::Engine subclass. rails plugin new my_engine --mountable generates one with its own namespace, routes and an isolated Engine class that also isolates views, helpers and routes so they cannot collide with the host. You mount it in the host’s config/routes.rb with mount MyEngine::Engine => "/my_engine". Everything inside is namespaced under MyEngine, and migrations are copied into the host with rails my_engine:install:migrations. Use an engine when the code is genuinely reused across applications or has a hard versioned boundary; do not use one to organise a single application, where a namespace plus a directory does the same job with far less ceremony.”

my_engine/
my_engine.gemspec
lib/my_engine.rb
lib/my_engine/engine.rb
app/controllers/my_engine/application_controller.rb
app/models/my_engine/application_record.rb
config/routes.rb
test/dummy/ # a throwaway Rails app the engine's own tests boot
lib/my_engine/engine.rb
module MyEngine
class Engine < ::Rails::Engine
isolate_namespace MyEngine
end
end

isolate_namespace is the important line: route helpers, controllers, models, jobs and mailers all resolve inside the engine’s namespace, so the engine’s posts_path is prefixed and the host’s is unaffected.

Generator Routes Isolation Use
--mountable Own routes.rb, mountable Full Reusable feature with its own UI
(default) Shares host routes Partial Library code that adds models only
--full Own routes, gemspec, test scaffolding Full A gem you will release standalone
app/controllers/my_engine/posts_controller.rb
module MyEngine
class PostsController < ApplicationController
def index
@posts = Post.all
end
end
end

Inside the isolated namespace, Post resolves to MyEngine::Post and ApplicationController to MyEngine::ApplicationController, which inherits from ActionController::Base — not from the host’s ApplicationController. The engine therefore does not get the host’s current_user, authenticate_user! or rescue_from handlers. Either give the engine its own controller layer with a documented configuration hook, or drop isolation and reopen classes, which couples the gem to one host.

Prefix the tables:

db/migrate/..._create_my_engine_posts.rb
create_table :my_engine_posts do |t|
t.string :title, null: false
t.timestamps
end
module MyEngine
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
self.table_name_prefix = "my_engine_"
end
end
config/routes.rb (host)
Rails.application.routes.draw do
mount MyEngine::Engine => "/my_engine", as: "my_engine"
end
config/routes.rb (inside the engine)
MyEngine::Engine.routes.draw do
resources :posts
root "posts#index"
end

The mount path is arbitrary; /admin is common. Mounting at / works but competes with every other route, so mount last. From the host, helpers are prefixed: my_engine.posts_path.

Terminal window
bin/rails my_engine:install:migrations
bin/rails db:migrate

Migrations are copied into the host’s db/migrate with a timestamp prefix, and only new ones are installed on a later run. That is deliberate: the host owns its schema history. For assets and JavaScript, add the engine’s paths in its initializer; with importmaps, list the engine’s JS in the host’s map, because the browser resolves bare specifiers from a single map.

Good reasons: a genuinely reusable product maintained across several applications; a hard boundary with its own team and release cycle; a documented plugin surface for third parties.

Bad reasons:

  • Organising a single application. app/modules/billing/ plus a Billing:: namespace gives the same structure with no gemspec, no migration copying and no dummy app.
  • “Microservices lite.” An engine is still one process, one database and one deploy. It buys compile-time modularity, not operational isolation.
  • Hiding a dependency you do not control. A fork of an engine is a fork you now maintain.
  1. What does isolate_namespace actually change?
  2. Why does the engine not inherit the host’s ApplicationController?
  3. Why are engine migrations copied rather than linked?
  4. When would you use a plain gem instead of an engine?
  5. What breaks if the host and the engine both define a Post model?
  • Expecting host helpers inside the engine. current_user is not there unless you provide it.
  • Unprefixed table names. posts in an engine collides with the host’s posts.
  • Forgetting install:migrations. Works in development against the dummy app, fails on deploy.
  • Mounting at / first. It swallows the host’s routes.
  • Reopening engine classes from the host. Works until the gem changes, then stops silently.
  • Using an engine as an architecture. If it is not published, versioned and reused, a namespace is cheaper.