# How to Build a Ruby on Rails Engine

DevFeed: [How to Build a Ruby on Rails Engine](<https://devfeed.tech/articles/how-to-build-a-ruby-on-rails-engine-21068.md>)

Original publisher: [Read original article](<https://jakeyesbeck.com/2016/03/20/how-to-build-a-ruby-on-rails-engine/>)

Published: 2016-03-20T12:00:00Z

Content type: tutorial

Language: en

Sources: [Jake Yesbeck](<https://devfeed.tech/sources/jake-yesbeck.md>)

Topics: [Rails](<https://devfeed.tech/topics/rails.md>), [Ruby](<https://devfeed.tech/topics/ruby.md>), [App](<https://devfeed.tech/topics/app.md>), [Routing (disambiguation)](<https://devfeed.tech/topics/routing.md>)

Tags: [app](<https://devfeed.tech/tags/app.md>), [applications](<https://devfeed.tech/tags/applications.md>), [building](<https://devfeed.tech/tags/building.md>), [generate](<https://devfeed.tech/tags/generate.md>), [generators](<https://devfeed.tech/tags/generators.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [plugin](<https://devfeed.tech/tags/plugin.md>), [rails](<https://devfeed.tech/tags/rails.md>), [routing](<https://devfeed.tech/tags/routing.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [ruby-on-rails](<https://devfeed.tech/tags/ruby-on-rails.md>), [structure](<https://devfeed.tech/tags/structure.md>)

## AI overview

A tutorial on building a Ruby on Rails Engine, a miniature application that supplements a larger Rails application. It covers generating an Engine or creating its directories and files manually, defining the Engine, isolating its namespace, and organizing it as a gem.

## Source excerpt

Ruby on Rails Engines are miniature applications whose purpose is to supplement a larger Ruby on Rails application. If functionality can exist independent from a main application, an Engine can provide a wonderful degree of encapsulation. Recently, I created the and "gemified" the Passages Ruby on Rails Engine to help alleviate some routing frustration. This gem will be the be used as the example for demonstrating what goes into creating a Ruby on Rails Engine. Revving Up There are two ways to start building an Engine. One option is to use the built in generators to create directories and dummy classes. These generators behave in the same way as standard Rails generators. To generate an Engine in this way, use the plugins built in generator: $ rails plugin new passages --mountable Note: Engines and Plugins are not exactly the same in the Ruby on Rails world but the --mountable flag tells the plugin generator to generate a full Engine. The other approach is to simply create the needed directories and files by hand. This is was the way the Passages Engine was built, resulting in the following directory structure: |-app |--controllers |---passages |----<controller directories> |--views |---passages |----<views directories> |-config |--routes.rb |--initializers |---assets.rb |-lib |--passages |---engine.rb Some directories that the rails generator would have added are missing (i.e. models, helpers, mailers). These directories might be necessary for some projects, but the Passages Engine did not have use for them. The file at the heart of it all is engine.rb. This file is responsible for defining the engine and will also be utilized later to add optional enhancements an Engine can take advantage of: module Passages class Engine < ::Rails::Engine isolate_namespace(Passages) end end An interesting line in this file is the isolate_namespace method call. This method helps ensure encapsulation for the Engine by isolating its controllers, helpers, views, routes, and any other