Why does a link in a Rails mailer template need config.action_mailer.default_url_options, and what does a new Rails 8.1 app set it to?
answer
- no request, so no host
- Missing host to link to!
- path helpers are not included in mailers
- localhost:3000 in development
- example.com placeholder in production
basics
~20 sA mailer runs without an HTTP request, so URL helpers have no host to build absolute links from; default_url_options supplies it. A new 8.1 app sets localhost:3000 in development and the placeholder example.com in test and production.
solid answer
~40 sA controller borrows the host from the current request; a mailer has no request, often not even a web process, since `deliver_later` renders in a worker. So `order_url(@order)` needs a host from `config.action_mailer.default_url_options`; without one it raises `ArgumentError` ("Missing host to link to!"). The generated environments set `{ host: "localhost", port: 3000 }` in development and `{ host: "example.com" }` in test and production, and the production value is a placeholder you must replace. Mailer views get only the `*_url` helpers: the `*_path` helpers are not included, because a relative link means nothing in a mail client. Images need `config.action_mailer.asset_host`, which falls back to `config.asset_host`.
code
ruby · 3 lines# config/environments/production.rb
config.action_mailer.default_url_options = { host: "shop.example", protocol: "https" }
config.action_mailer.asset_host = "https://shop.example"go deeper
Use *_url helpers in mailer views and know that config.action_mailer.default_url_options must hold the host.
Explain why mailers have no request, what the generated values are per environment, and the ArgumentError a missing host raises.
Treat the production placeholder as a launch check, drive hosts from per-environment config, and separate link hosts from asset hosts.
For multi-domain or white-label shops, decide whether hosts come from per-mailer overrides or per-tenant data, and make that rule testable.
## Why mailers have no host In a controller, URL helpers build links from the **current request**: its protocol, host and port are known, so `order_url(@order)` produces `https://shop.example/orders/42` without any configuration. A mailer is different. `ActionMailer::Base` is not tied to a request at all, and with `deliver_later` the template is rendered inside a background worker that never saw one. The mailer therefore needs to be told which host to put in links. That is the job of **`default_url_options`** for Action Mailer, a hash of URL options merged into every URL helper call made by mailers and their views: ```ruby # config/environments/production.rb config.action_mailer.default_url_options = { host: "shop.example", protocol: "https" } ``` ## What a new Rails 8.1 app generates | Environment file | Generated value | Note | |---|---|---| | `development.rb` | `{ host: "localhost", port: 3000 }` | matches `bin/rails server` | | `test.rb` | `{ host: "example.com" }` | links render in mailer tests | | `production.rb` | `{ host: "example.com" }` | a placeholder, under a comment saying it is the host for mailer links | The production line is the classic bug: an app goes live, order emails render fine, and every link points at `example.com`. Read it as a to-do the generator left you. ## What fails, and how 1. **No host at all.** If the option is removed and the call passes no `host:`, `order_url(@order)` raises `ArgumentError` with "Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true". In a `deliver_later` job, that error surfaces in the worker, not in the request that enqueued the email. 2. **A path helper.** Mailers include the application's route helpers with path helpers switched off, so `order_path(@order)` in a mailer view raises `NoMethodError`. This is intentional: a relative `/orders/42` has no meaning inside a mail client. 3. **A wrong host.** No error at all, just broken or misdirected links, which is why the production value deserves a test or a preview check. ## Ways to supply the host - **Per environment**, in `config/environments/*.rb`, as above. This is the normal place. - **Per mailer**, by defining a `default_url_options` method on the mailer, for example to point a `WholesaleMailer` at a trade subdomain. - **Per call**, `order_url(@order, host: "trade.shop.example")`, which overrides the defaults for that one link. - **Protocol.** Add `protocol: "https"`, or rely on Rails using HTTPS for generated host URLs when `config.force_ssl` is true. ## Images and assets Links and images are configured separately. `image_tag "sofa.jpg"` in a mailer view produces a path unless an **asset host** is set, so set `config.action_mailer.asset_host = "https://shop.example"`. If you leave it unset, Action Mailer falls back to the application's `config.asset_host`. Images embedded as inline attachments are the exception: they use `cid:` references and need no host. ## Checking it - Open the mailer preview and hover the links: in development they should point at `localhost:3000`. - In a mailer test, assert that the body includes the full URL you expect. - In production, read the generated `production.rb` line once before launch and replace `example.com`, ideally from an environment variable so staging and production differ. ## Common setups - **Several deployed environments.** Each `config/environments/*.rb` file sets its own host, so a staging server's emails link to staging rather than to production. A custom environment such as `staging.rb` that starts with `require_relative "production"` inherits the production host unless it overrides it. - **Ports and subpaths.** `port:` belongs in the hash whenever the app is not on 80 or 443, as in the generated development value. An app mounted under a subpath also needs `script_name:` or a relative URL root, or links lose the prefix. - **Tests.** The generated `test.rb` uses `example.com`, so a mailer test that checks for `http://example.com/orders/42` in the body is checking the real code path, not a stub. - **Background workers.** Workers load the same environment file as the web process, so one setting covers both `deliver_now` and `deliver_later`; there is no separate worker configuration to keep in sync.
- Why does the error show up in the worker logs rather than the checkout request?`deliver_later` enqueues `MailDeliveryJob` without rendering. The template, and with it `order_url`, is rendered when the worker runs the action, so a missing host raises there. The checkout request already returned successfully.
- How would one mailer link to a different host than the rest of the app?Define a `default_url_options` method on that mailer returning the other host, or pass `host:` to individual URL helper calls. The per-mailer method overrides the environment-wide `config.action_mailer.default_url_options` for every link that mailer renders.
- Mailers reuse the host of the request that enqueued the email.
- order_path works in mailer views and mail clients resolve it.
- The generated production default_url_options is ready to use as is.
- Setting default_url_options also fixes image URLs in mailers.
- A missing host just renders a relative link.