In a large Rails app's config/routes.rb, how do draw and mount help organise routing, and what does each one actually hand off?
answer
- draw loads config/routes/*.rb
- inlined where it is called
- mount takes a Rack app
- prefix match, every verb
- missing file raises ArgumentError
basics
~10 sdraw :admin evaluates config/routes/admin.rb in place, inside the current scope, so a big routes file can be split; mount hands every request under a path prefix, for any verb, to another Rack application.
solid answer
~40 s`draw` and `mount` solve different problems. `draw :admin` reads `config/routes/admin.rb` and evaluates it with the same mapper at the point of the call, so the file's lines inherit any surrounding `namespace` or `scope` and take that position in match order; a missing file raises `ArgumentError`. It only splits source: the result is one route table. `mount` hands a URL prefix to a separate Rack application: `mount app, at: "/wire"` (or `mount app => "/wire"`) matches the prefix rather than the whole path, accepts every verb by default, and passes the request to anything that responds to `call`, such as an engine or a small Rack app. Rails routing stops at the prefix; the mounted app does its own routing below it.
code
ruby · 10 lines# config/routes.rb
Rails.application.routes.draw do
root "stories#index"
namespace :admin do
draw :admin # evaluates config/routes/admin.rb here
end
mount WireIntake::App.new, at: "/wire"
endgo deeper
Know that draw loads another routes file from config/routes and that mount sends a URL prefix to another Rack app such as an engine.
Explain that drawn files share the current scope and match order, and that mounts match a prefix for every verb without a format suffix.
Split a large routes file by area with draw, place draw and mount calls with match order in mind, and protect mounted dashboards.
Decide which parts of the app deserve a separately mounted engine and which only need their routes split into files.
## Two different kinds of "splitting" A newsroom CMS's `config/routes.rb` grows with each desk: public stories, the editors' admin, a wire-service intake, an analytics dashboard. Two DSL methods help keep it manageable, and they are easy to confuse: - **`draw`** splits the **source** of the route table into several files. Everything still ends up in one Rails router. - **`mount`** hands a URL prefix to **another Rack application**, which routes the rest of the path itself. ## draw ```ruby # config/routes.rb Rails.application.routes.draw do root "stories#index" resources :stories, only: [:index, :show] namespace :admin do draw :admin end end ``` ```ruby # config/routes/admin.rb resources :stories do post :publish, on: :member end resources :desks ``` How it behaves: 1. `draw :admin` looks for `admin.rb` in the app's `config/routes` directory and evaluates its contents **with the current mapper**, exactly as if the lines were typed at the call site. 2. Because of that, the file **inherits the surrounding scope**: here its stories become `/admin/stories`, `admin_stories_path` and `Admin::StoriesController`, without the file mentioning `admin` at all. 3. The file's routes take the **position** of the `draw` call in match order. Moving the call moves them. 4. A name with no matching file raises `ArgumentError` listing the directories searched. `draw` is a code-organisation tool only. It changes nothing at request time, and `bin/rails routes` lists the combined table. ## mount ```ruby mount WireIntake::App.new, at: "/wire" # equivalent: mount WireIntake::App.new => "/wire" ``` How it behaves: - The target must respond to **`call`**, which is the Rack application contract; Rails raises `ArgumentError` otherwise. - The route matches the **prefix**: `/wire`, `/wire/items` and `/wire/items/7` all go to the mounted app. - It accepts **every HTTP verb** by default, and adds **no** `(.:format)` suffix. - Rails stops routing at the prefix: the mounted app receives the request with the prefix moved out of the path it sees, and routes the remainder with its own rules. - An engine, or an app passed as a class, gets a route name derived from it (or set with `as:`); an engine's name is also how the host app reaches that engine's own route helpers. An app passed as an instance gets a name only through `as:`. Typical targets are Rails engines (admin dashboards, job monitors) and small Rack apps. Mounting an engine, and the Rack contract the mounted app receives, are their own topics. ## Choosing between them | Need | Use | |---|---| | A long routes file, split by area, all handled by this app's controllers | `draw` | | Routes that must sit inside a namespace defined in the main file | `draw` inside the namespace block | | A separate application (engine or Rack app) owning everything under a prefix | `mount` | | Protecting a mounted dashboard | a constraint or authentication around the `mount` line | ## Pitfalls seen in production code - **Expecting a drawn file to be a sandbox.** It is not; a `namespace` around `draw` applies to every line in the file, and a stray route in the file shadows later routes in the main file. - **Ordering after a catch-all.** A `draw` call placed after a catch-all route never gets a request, because its routes sit after the catch-all in match order. - **Mounting at a prefix the app already uses.** Because a mount matches the whole prefix, `mount ..., at: "/stories"` would steal `/stories/42` from `StoriesController` if it came first. - **Unprotected mounts.** A mounted dashboard is reachable by anyone who knows its path unless the mount is wrapped in an authentication check. The senior signal in an answer is knowing that `draw` is source splitting with shared scope and order, while `mount` is a hand-off of a whole prefix to code Rails does not route.
- config/routes/admin.rb declares resources :desks, and draw :admin is called inside namespace :admin. Which controller handles /admin/desks?`Admin::DesksController`. `draw` evaluates the file with the current mapper, so the namespace's path, helper prefix and module apply to every line in it. The file itself never mentions `admin`, which is why such files read as if they were top-level and why reviewers should check where they are drawn.
- Why does a mounted app see /items when the browser requested /wire/items?A mount matches the prefix and passes the request on with that prefix moved out of the path the app sees, so the app can route as if it were mounted at the root. Links it builds should account for the prefix, which Rails engines do through their own route helpers.
- draw :admin creates a separate router with its own match order
- Routes in a drawn file ignore the namespace around the draw call
- mount only matches the exact path, not paths below it
- A mounted app receives only GET requests unless via: is set
- mount works with a controller class such as StoriesController