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.
The 60-second answer
Section titled “The 60-second answer”“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.”
Creating one
Section titled “Creating one”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 bootmodule MyEngine class Engine < ::Rails::Engine isolate_namespace MyEngine endendisolate_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 |
Namespacing
Section titled “Namespacing”module MyEngine class PostsController < ApplicationController def index @posts = Post.all end endendInside 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:
create_table :my_engine_posts do |t| t.string :title, null: false t.timestampsendmodule MyEngine class ApplicationRecord < ActiveRecord::Base self.abstract_class = true self.table_name_prefix = "my_engine_" endendMounting
Section titled “Mounting”Rails.application.routes.draw do mount MyEngine::Engine => "/my_engine", as: "my_engine"endMyEngine::Engine.routes.draw do resources :posts root "posts#index"endThe 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.
Migrations and assets
Section titled “Migrations and assets”bin/rails my_engine:install:migrationsbin/rails db:migrateMigrations 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.
When to use one
Section titled “When to use one”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 aBilling::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.
Follow-up questions
Section titled “Follow-up questions”- What does
isolate_namespaceactually change? - Why does the engine not inherit the host’s
ApplicationController? - Why are engine migrations copied rather than linked?
- When would you use a plain gem instead of an engine?
- What breaks if the host and the engine both define a
Postmodel?
Common traps
Section titled “Common traps”- Expecting host helpers inside the engine.
current_useris not there unless you provide it. - Unprefixed table names.
postsin an engine collides with the host’sposts. - 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.