What are Rails engines and how are they used?

Answer

A Rails engine is a self-contained mini-application that can be mounted inside a Rails app, providing isolated controllers, models, views, migrations, and routes. rails plugin new my_engine --mountable creates a mountable engine. Mount with: mount MyEngine::Engine, at: '/my_engine' in the host app's routes. Engines are used to package reusable functionality — Devise, ActiveAdmin, Spree Commerce, and Sidekiq's web UI are all engines. Namespacing prevents class name collisions. Shared tables are handled via isolate_namespace. Engines can have their own migrations, which the host app runs separately.