Recently, I build some Rails engines. And I found it’s a good way to organize and share reusable code across a number of applications.
Rails Engine is not like Ruby gem. It’s more like a rails app, it has its own controllers, views and migrations. And basically it has two type of engine, full or mountable.
Full vs Mountable Engine
Full Engine
With a full engine, the parent application inherits the routes from the engine. It is not necessary to specify anything in the main_app’s 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:
|
|
No namespacing of models, controllers, etc. These are immediately accessible to the parent application.
Mountable Engine
The engine’s namespace is isolated by default:
|
|
With a mountable engine, the routes are namespaced and the parent app can bundle this functionality under a single route:
|
|
Models, controllers, etc are isolated from the parent application.
Tips:--full
for the full engine and --mountable
for the mountable engine