skip to content

In a mountable Rails engine, why does `root_path` in its layout misroute or fail, and how do `main_app` and the engine's route proxy fix it?

level: middleimportance: should knowfreq 33%

answer

  1. two route sets, two helper sets
  2. engine controllers get engine URL helpers
  3. main_app reaches the host
  4. proxy named after mount's as:
  5. form_with(model: [billing, @invoice])

basics

~20 s

Isolated engine controllers and views use the engine's URL helpers, so root_path is the engine's root or undefined. main_app.root_path always targets the host; the host reaches the engine through the proxy named by mount's as:, billing by default.

solid answer

~30 s

A mountable engine has its own route set, and controllers under `Billing` include that set's URL helpers. Inside the engine, `root_path` is the engine's root, `/billing/`, and a host-only helper such as `new_session_path` raises `NoMethodError`. Calling it through `main_app`, as in `main_app.root_path`, uses the host's routes. In the other direction the host gets a proxy method named after the mount's `as:` option, which defaults to the engine name: `billing.invoices_path` returns `/billing/invoices`, with the mount point applied. For polymorphic URLs, pass the proxy first: `form_with(model: [billing, @invoice])`. Writing the proxy explicitly in shared layouts and partials removes the ambiguity entirely.

code

erb · 6 lines
erb
<%# engines/billing/app/views/layouts/billing/application.html.erb %>
<nav>
  <%= link_to "HR home", main_app.root_path %>
  <%= link_to "Invoices", invoices_path %>  <%# engine route %>
</nav>
<%= yield %>

go deeper

for a junior

Remember main_app for host links from an engine and the billing proxy for engine links from the host.

for a middle

Explain why bare helpers resolve against the renderer's route set, and where the proxy name and mount prefix come from.

for a senior

Spot the shared-layout bug quickly and make proxies explicit in partials that both sides render; cover polymorphic forms with [billing, @invoice].

for a principal

Use explicit proxies as part of the engine's contract with its host, so a renamed mount or a moved route breaks loudly in tests.

## Two route sets in one process When a host mounts an isolated engine there are two `ActionDispatch::Routing::RouteSet` objects: `Rails.application.routes` and `Billing::Engine.routes`. Each generates its own **URL helpers** (`invoices_path`, `root_url` and so on). Which set a bare helper call resolves against depends on the class that renders: - controllers in the host include the host set's helpers; - controllers whose module parents include an isolated namespace (`Billing::InvoicesController`) include the **engine** set's helpers instead; - views and layouts inherit the helpers of the controller that renders them. So the same ERB line can mean different things depending on who renders it. The engines guide puts it plainly: `articles_path` would point to the application's route from an app template and to the engine's from an engine template. ## The failure modes 1. **Wrong destination.** The engine defines `root to: "invoices#index"`; a shared header partial calls `root_path` expecting the HR dashboard and links to `/billing/` instead. 2. **`NoMethodError`.** The engine's layout calls a host-only route helper, for example `new_session_path`. The engine's route set has no such route, so the helper is undefined. 3. **Configured layouts.** An engine told to render the host's layout (`layout "application"`) runs that layout with the engine's helpers, so every host link in it breaks the same way. ## The two proxies **`main_app`** is a routing proxy defined for the host application during boot (the `define_main_app_helper` initializer). From anywhere, including engine views, `main_app.root_path` builds a URL from the host's route set. **The engine proxy** is defined when the host mounts the engine. Its name is the `as:` option of `mount`, and the default `as:` is the engine's `engine_name`, so `mount Billing::Engine, at: "/billing"` defines `billing`. With `as: "payments"` the proxy is `payments` instead. | Where the code runs | Wants host route | Wants engine route | |---|---|---| | Host controller or view | `root_path` | `billing.invoices_path` | | Engine controller or view | `main_app.root_path` | `invoices_path` (or `billing.invoices_path`) | | Polymorphic helpers | `[main_app, @employee]` | `[billing, @invoice]` | The proxy also applies the **mount point**: the engine's `invoices_path` is `/invoices` inside its own set, but `billing.invoices_path` from the host returns `/billing/invoices` because the proxy carries the script name of the mount. ## Polymorphic routes and forms `form_with(model: @invoice)` in a host view asks the host's route set for `invoice_path` and fails. Passing the proxy as the first array element tells Rails which set to use: `form_with(model: [billing, @invoice])` calls `billing.invoice_path(@invoice)`. The same works with `link_to "Invoice", [billing, @invoice]`. ## Testing engine routes In an engine's integration tests the dummy app's routes are the default. The engines guide sets `@routes = Engine.routes` in `setup` and includes `Engine.routes.url_helpers`, so a bare `invoices_url` resolves against the engine. ## Debugging a broken link When an engine page raises `NoMethodError` for a `_path` helper or a link lands in the wrong place, work through these steps: 1. Find which controller rendered the template: its module decides the route set. 2. Run `bin/rails routes` and check which section the route appears in, the application's or the engine's. 3. If the route is in the other set, prefix the call with `main_app.` or with the engine proxy. 4. If the failing call sits in a partial shared by both sides, make the prefix explicit there rather than in its callers. 5. In tests, confirm whether `@routes` points at the engine's set before assuming the helper is missing. The same reasoning applies to `url_for` with a hash of options and to `redirect_to` in engine controllers: both resolve against the controller's route set, so a redirect to a host page needs `main_app` as well. ## Good habits - In any engine view that might be shared, call the proxy explicitly: `main_app.` for host links, `billing.` for engine links. - Keep host-dependent helpers out of the engine's layout, or inject them through a documented configuration point. - When renaming the mount with `as:`, search for the old proxy name; the engine name and the proxy name diverge from then on. - Use `bin/rails routes` to read both sets; it prints the engine's routes in their own section after the application's.

  • Where does the name of the engine proxy come from?
    From `mount`'s `as:` option. When `as:` is omitted, `mount` uses the engine's `engine_name`, which `isolate_namespace Billing` sets to `billing`; a non-isolated `Billing::Engine` would default to `billing_engine`.
  • Why does billing.invoices_path include /billing while the engine's own invoices_path in a test may not?
    The proxy built by `mount` carries the mount point as the script name. Calling the engine set's URL helpers directly, outside a request that came through the mount, has no script name to prepend.

An office phone system with its own extensions: inside the billing office a short number reaches a billing colleague, and reaching the head office takes the outside-line prefix every time, which is main_app; head office in turn dials the billing prefix, the engine proxy, to reach an extension there.

  • Engine views automatically see every host route helper
  • main_app only works inside host controllers
  • The engine proxy needs as: passed explicitly on mount
  • billing.invoices_path returns /invoices without the mount point
  • form_with(model: @invoice) in the host finds engine routes on its own