[email protected]


Let’s say we have a simple app with only users:

bin/rails generate scaffold user name

Then bin/rails db:migrate

Now you can launch your bin/rails server and go to localhost:3000/users.

Let’s create an engine to handle payments for our app:

bin/rails plugin new payments --mountable

We need to clean and fill a bit the gemspec of the engine (payments/payments.gemspec).

Let’s have something like:

require_relative "lib/payments/version" do |spec| = "payments"
  spec.version = Payments::VERSION
  spec.authors = ["Dorian Marié"] = ["[email protected]"]
  spec.license = "MIT"
  spec.summary = "handles payments"
  spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", ""]

  spec.add_dependency "rails", "~> 6.1.3", ">="

We will need to add it to our config/routes.rb

mount Payments::Engine => "/payments"

In the payments/ directory:

bin/rails generate scaffold payment user:references amount:integer

Then the migration needs to be copied to the main app:

rake payments:install:migrations

(this is just an example, amount should probably be in cents and have a currency)

Then bin/rails db:migrate

(I needed to do gem update --system)

Now you need to link the engine css from the main app, in app/assets/stylesheets/application.css:

 *= link payments/application.css

And you can go to localhost:3000/payments/payments