Posted on: Written by: K-Sato
⚠️ This article was posted over 2 years ago. The information might be outdated. ⚠️

Table of Contents

What are engines

Rails Engines can be considered miniature applications that provide the functionality to their host applications.

Generate an engine

Generate the foundation of the engine

Run the command below to generate an engine called blorph.

The --mountable option tells the generator that you want to create a “mountable” and namespace-isolated engine.

More about options here.

$ rails plugin new engine_name --mountable

Install dependencies

Fix TODOs in .gemspec and run bundle install.

Adding functionalities

You can add models or controllers to the engine using the commands below.

$ rails g model
$ rails g controller

Hooking into Applications

Mounting the engine

It is as easy as installing a gem in your application. Simply, add the engine to your Gemfile and run bundle install.

# .Gemfile
gem 'engine_name'

To make the engine’s functionality accessible from within an application, it needs to be mounted in that application’s config/routes.rb file.

# config/routes.rb
mount ENGINE_NAME::Engine, at: "/engine_name", as: 'engine'

Engine setup

You can run migration for the engine like the command below.

$ rails engine_name:install:migrations

References

About the author

I am a web-developer based somewhere on earth. I primarily code in TypeScript, Go and Ruby at work. React, RoR and Gin are my go-to Frameworks.