In Rails 8.1, what does redirect_back_or_to do when the request has no Referer header or one pointing at another host?
answer
- Referer header, not session state
- fallback is the first argument
- foreign host falls back under new defaults
- options pass through to redirect_to
- redirect_back is the soft-deprecated alias
basics
~20 sIt redirects to the fallback location given as its first argument. redirect_back_or_to uses request.referer only when the header exists and its host is allowed; with load_defaults 8.1 a Referer from another host is not allowed, so the fallback wins.
solid answer
~30 s`redirect_back_or_to(fallback_location, allow_other_host: ..., **options)` reads `request.referer`. If the header is present and either other hosts are allowed or the Referer's host matches the request's (or the allowed-hosts list), it calls `redirect_to request.referer`; otherwise it calls `redirect_to fallback_location`. The `allow_other_host` default comes from configuration: with `load_defaults` 7.0 or later, which includes 8.1, `action_on_open_redirect` is `:raise`, so the default is false and a foreign Referer quietly falls back. Every other option, such as `status: :see_other` or `notice:`, is passed to `redirect_to`. `redirect_back(fallback_location:)` is the older keyword form, now a soft-deprecated alias.
code
ruby · 8 linesclass Tickets::AssignmentsController < ApplicationController
def create
ticket = Ticket.find(params.expect(:ticket_id))
ticket.update!(assignee: Current.user)
redirect_back_or_to ticket, notice: "Assigned to you.", status: :see_other
end
endgo deeper
Recall that redirect_back_or_to sends the user to the Referer page or, when there is none, to the fallback you pass as its first argument.
Explain where the Referer comes from, why it can be missing, how a foreign host falls back under 8.1 defaults, and that status: and notice: pass through to redirect_to.
Show you avoid it after destroy and on self-referring forms, pass :see_other on writes, and know the allow_other_host default follows action_on_open_redirect.
Decide where 'go back' navigation is worth relying on a client header, and where an explicit return_to parameter or fixed destination is the more predictable contract.
## What the method is for Some actions are reached from several pages. In a helpdesk, an agent can **assign a ticket to themselves** from the ticket list, the ticket page or a dashboard. After the assignment, the natural answer is "go back where you were". `redirect_back_or_to` does that by redirecting to the URL in the request's **`Referer` header**, the page that sent the request, and to a **fallback** when it cannot. ```ruby def create @ticket.update!(assignee: Current.user) redirect_back_or_to @ticket, notice: "Assigned to you.", status: :see_other end ``` ## How the decision is made The Rails 8.1 source does this, in order: 1. Read `request.referer`. If it is **missing**, redirect to `fallback_location`. 2. If it is present, follow it only when **`allow_other_host`** is true **or** the Referer's host is allowed: it equals `request.host`, or it is in `config.action_controller.allowed_redirect_hosts`. 3. Otherwise redirect to `fallback_location`. Both branches call `redirect_to`, so everything about `redirect_to` applies: the default **302**, the `status:` option, the flash options `notice:`, `alert:` and `flash:`, and `AbstractController::DoubleRenderError` if the action already answered. The fallback can be anything `redirect_to` accepts: a record, a route helper, a path string or a hash. ## The allow_other_host default in Rails 8.1 The keyword's default is not a literal; it is computed from configuration: - If `action_on_open_redirect` is `:raise` (or the older `raise_on_open_redirects` is true), the default is **false**. - Otherwise it is **true**. `load_defaults "7.0"` sets `action_on_open_redirect = :raise`, and every later default set, including **8.1**, inherits it. So in a new Rails 8.1 app a Referer such as `https://partner.example/inbox` is ignored and the user lands on the fallback, without an error. An app still on older defaults follows the foreign Referer. Open-redirect policy as a whole is a security topic; here the point is only which way the method falls. ## Why the Referer is not dependable | Situation | `request.referer` | |---|---| | Normal same-site navigation | the full previous URL | | A typed URL, bookmark or new tab | absent | | A page with `Referrer-Policy: no-referrer` | absent | | A link or form on another site | usually that site's origin, a foreign host | | A script or API client | whatever it sends, often nothing | Rails' default `Referrer-Policy` header is `strict-origin-when-cross-origin`, which keeps the full URL for same-origin requests, so in-app navigation normally carries it. That is why the fallback is a **required** argument. ## Choosing the fallback and the cases to avoid - **Point the fallback at a page that always exists**: the ticket or the ticket list. - **Do not use it after destroy** when the previous page was the deleted record: going back to `/tickets/42` after deleting ticket 42 ends in a 404. Redirect to `tickets_path` explicitly. - **Do not use it after a form whose previous page was the form itself** unless returning to the form is what you want; an edit form would send the agent back to `edit` after saving. - **Pass `status: :see_other`** when the action handles PATCH or DELETE, for the same reason as with `redirect_to`. ## Testing both branches An integration test can set the header itself, so both paths are easy to pin: ```ruby post ticket_assignment_url(@ticket), headers: { "HTTP_REFERER" => tickets_url } assert_redirected_to tickets_url post ticket_assignment_url(@ticket) assert_redirected_to ticket_url(@ticket) ``` The second request carries no Referer, so it exercises the fallback. A third case with a foreign Referer documents the 8.1 default. ## The older spelling `redirect_back(fallback_location: tickets_path)` is the keyword form. In Rails 8.1 it is a **soft-deprecated alias**: it calls `redirect_back_or_to` with the same arguments and emits no warning. New code uses `redirect_back_or_to`, whose positional fallback reads naturally.
- How do redirect_back and redirect_back_or_to differ in Rails 8.1?Only in how the fallback is passed. `redirect_back(fallback_location: path)` takes a keyword and simply calls `redirect_back_or_to path` with the remaining options. It is a soft-deprecated alias, so it still works without a warning, but new code uses `redirect_back_or_to`.
- Why can redirect_back_or_to send a user to a 404 after a destroy?If the delete button sat on the record's own page, the Referer is that record's URL. After the record is gone, redirecting back requests a URL whose `find` now fails. Destroy actions should redirect to an explicit index path instead.
- redirect_back_or_to remembers the previous page in the session
- with no Referer, redirect_back_or_to raises an UnsafeRedirectError
- redirect_back_or_to always follows a foreign Referer in a Rails 8.1 app
- redirect_back was removed in Rails 8 and now raises NoMethodError
- redirect_back_or_to ignores status: and always answers 302
Assumes Rails 8.1 with load_defaults 8.1, where action_on_open_redirect is :raise (set since the 7.0 defaults), so a foreign Referer is not followed.