In Rails, why does the scaffold's update action pass status: :see_other to redirect_to instead of keeping redirect_to's default status?
answer
- redirect_to defaults to 302 Found
- 302 may keep PATCH or DELETE
- 303 always means follow with GET
- status: takes an integer or Rack symbol
- create keeps 302; update, destroy send 303
basics
~20 sredirect_to answers 302 Found by default, and a client that sent a real PATCH or DELETE may repeat that method when following a 302. 303 See Other tells every client to follow with GET, so the scaffold uses status: :see_other on update and destroy.
solid answer
~40 sWith no `status:` option `redirect_to` sends **302 Found**. Clients only reliably turn a 302 after a POST into a GET; a script that sent a real `PATCH` or `DELETE` (through `fetch` or XHR) may follow a 302 with the same method, and Rails' own `redirect_to` docs warn this can produce a double DELETE. **303 See Other** means "fetch the result with GET" for any method, so the Rails 8.1 scaffold adds `status: :see_other` to the redirects in `update` and `destroy`, while `create`, which arrives as a POST, keeps the default. `status:` accepts an integer or a Rack status symbol such as `:found`, `:see_other` or `:moved_permanently`; the status must be 3xx for the browser to follow the `Location` header at all.
code
ruby · 12 linesdef update
if @ticket.update(ticket_params)
redirect_to @ticket, notice: "Ticket updated.", status: :see_other
else
render :edit, status: :unprocessable_content
end
end
def destroy
@ticket.destroy!
redirect_to tickets_path, notice: "Ticket deleted.", status: :see_other
endgo deeper
Remember that redirect_to sends 302 unless you pass status:, and that the scaffold uses status: :see_other after update and destroy.
Explain how 301/302 differ from 303 and 307/308 when following a non-GET request, and why _method forms behave like POST in the browser.
Show you audit write actions for 303, avoid caching 301s by accident, and know load_defaults 8.1 turns path-relative redirect strings into an UnsafeRedirectError.
Set a team rule for redirect statuses across HTML and script clients, and plan the load_defaults 8.1 switch so path-relative redirects are found before they raise.
## What redirect_to sends by default `redirect_to` sets three things on the response: the `Location` header, an empty body and a **status code**. In Rails 8.1 the status comes from `status:` if you pass one (in the options hash or the second argument) and is **302** otherwise. The value can be an integer or a status **symbol** from Rack's table, which Rails converts with `ActionDispatch::Response.rack_status_code`: | Symbol | Code | Typical use | |---|---|---| | `:moved_permanently` | 301 | an old URL replaced for good | | `:found` | 302 | the default, a temporary redirect | | `:see_other` | 303 | "go GET the result" after a write | | `:temporary_redirect` | 307 | retry the same method elsewhere | | `:permanent_redirect` | 308 | permanent, method preserved | An unknown symbol raises `ArgumentError` ("Unrecognized status code"). Rails does **not** check that the code is 3xx: `redirect_to ticket_path(@ticket), status: :ok` sends a 200 with a `Location` header that no browser follows, which is why the docs say the status must be a 3xx for redirection to happen. ## Why 302 is risky after PATCH and DELETE HTTP clients do not treat every redirect the same way after a non-GET request: 1. **301 and 302**: browsers change a **POST** into a GET when they follow it, for historical reasons. Other methods are not rewritten, so a `fetch` that sent `DELETE /tickets/42` and got `302 Location: /tickets` may send **`DELETE /tickets`** next. 2. **303 See Other**: the client follows with **GET** whatever the original method was (HEAD stays HEAD). 3. **307 and 308**: the client must repeat the **same method and body**. A plain HTML form only sends GET or POST. Rails forms that need PATCH or DELETE send a POST with a hidden `_method` field, and `Rack::MethodOverride` rewrites the method on the server. Those submissions arrive in the browser's eyes as POSTs, so a 302 is followed with GET. The trouble starts with JavaScript that sends a **real** PATCH or DELETE; Rails' `redirect_to` documentation calls out exactly this case, warns that it can produce a double DELETE, and suggests answering 303 instead. ## What the Rails 8.1 scaffold writes The scaffold controller template keeps the three cases apart: - `create` (a POST): `redirect_to @ticket, notice: "..."`, default 302. - `update` (PATCH/PUT): `redirect_to @ticket, notice: "...", status: :see_other`. - `destroy` (DELETE): `redirect_to tickets_path, notice: "...", status: :see_other`. Following that pattern by hand in a helpdesk app means every action that changes or deletes a ticket answers with **303**, so whatever client sent the request, the next request is a GET of a page that is safe to refresh. ## Other things redirect_to checks - `redirect_to nil` raises `ActionController::ActionControllerError` ("Cannot redirect to nil!"). - Calling it after `render`, `head` or another `redirect_to` raises `AbstractController::DoubleRenderError`. - With **load_defaults 8.1**, `config.action_controller.action_on_path_relative_redirect` is `:raise`, so a string without a leading slash, such as `redirect_to "tickets"`, raises `ActionController::Redirecting::UnsafeRedirectError` (the subclass `PathRelativeRedirectError`) instead of only logging. Use a route helper or an absolute path. - The flash options `notice:`, `alert:` and `flash:` are taken out of the options before the status is read, so they can sit beside `status:`. ## Pinning the status in a test An integration test that only says `assert_redirected_to ticket_url(@ticket)` passes for **any** 3xx, because the assertion's default status check is `:redirect`, the whole 300-399 range. To keep a later refactor from silently dropping back to 302, pass the status: ```ruby patch ticket_url(@ticket), params: { ticket: { priority: "high" } } assert_redirected_to ticket_url(@ticket), status: :see_other ``` `assert_response :see_other` does the same check on its own. Both take the same Rack symbols as `redirect_to`. ## How to choose - **After a successful write that a script might send with its real method**, use `:see_other`. - **For a moved page**, use `:moved_permanently` (or `:permanent_redirect` if the method must be kept), and remember browsers cache permanent redirects. - **Keep 302** for plain temporary redirects of GET requests, such as sending a signed-out user to the sign-in page.
- What does redirect_to "tickets" (no leading slash) do in a Rails 8.1 app?With `load_defaults 8.1`, `action_on_path_relative_redirect` is `:raise`, so it raises `ActionController::Redirecting::UnsafeRedirectError` (specifically `PathRelativeRedirectError`). Older defaults only logged a warning and produced a malformed URL such as `https://helpdesk.example.comtickets`. Use `tickets_path` or `"/tickets"`.
- When would you choose 307 or 308 over 303?When the client must repeat the same method and body at the new URL, for example an API endpoint that moved: 307 for a temporary move, 308 for a permanent one. After a form-style write you want the opposite, a GET of the result, which is what 303 says.
- Does Rails reject redirect_to with a non-3xx status?No. `redirect_to` sets whatever status you pass, so `status: :ok` produces a 200 with a `Location` header that browsers ignore. Only an unrecognized symbol fails, with `ArgumentError` from Rack's status lookup.
- redirect_to answers 303 See Other by default in Rails 8.1
- every client turns any redirect into a GET, so the status never matters
- status: :moved_permanently is the right choice after updating a record
- Rails rejects a non-3xx status passed to redirect_to with an error
- status: :see_other keeps the original PATCH method on the next request
Assumes Rails 8.1 with load_defaults 8.1; the scaffold's :see_other and the path-relative redirect check are as generated in 8.1.