With Turbo Drive, why must a successful Rails form submission redirect with 303 See Other while a failed one renders with a 4xx status?
answer
- non-GET form must redirect
- 200 render: console error, page unchanged
- 4xx/5xx renders in place
- 303 means follow with GET
- :unprocessable_content on Rack 3.1+
basics
~20 sTurbo Drive treats a non-GET form as a visit that must end in a redirect, which it follows and renders. A 4xx or 5xx response is rendered in place, so a failed save re-renders with 422; a plain 200 render is refused.
solid answer
~50 sTurbo Drive submits forms with `fetch`. For a non-GET submission it requires a redirect: `fetch` follows it, Turbo renders the final page and points the address bar at it. A 4xx or 5xx response is the exception: Turbo renders that HTML in place without changing the URL, which is why a failed save renders the form with `status: :unprocessable_content` (422; `:unprocessable_entity` is the obsolete Rack name). A 200 render without a redirect is refused: Turbo logs "Form responses must redirect to another location" and the page does not change. The reason is reload safety: Turbo cannot reproduce the browser's resubmit-on-reload dialog, so it will not leave a POST result on screen. Turbo only checks that a redirect happened; 303 See Other is the status that tells any client to follow with a GET whatever the method, so the Rails scaffold uses it for update and destroy.
code
ruby · 15 linesclass ArticlesController < ApplicationController
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article, notice: "Saved.", status: :see_other
else
render :edit, status: :unprocessable_content
end
end
private
def article_params
params.expect(article: [:title, :body])
end
endgo deeper
Remember the pairing: redirect after a successful save, render the form with 422 after a failed one. A plain 200 render from a POST does nothing under Turbo.
Explain the reasons: Turbo cannot reproduce the resubmit-on-reload dialog, so it insists on Post/Redirect/Get, and 4xx or 5xx responses are rendered in place without touching the URL.
Explain why 303 is the portable choice, how turbo-rails tunnels non-GET verbs through _method, and diagnose a dead form from the console error rather than from the server log.
Treat the rule as a contract every form endpoint must honour, and make the generated controller shape the team default so new endpoints cannot silently break it.
## How Turbo Drive submits a form When Turbo Drive handles a `<form>`, it cancels the native submission, builds a request with `fetch` (method, body and the `X-CSRF-Token` header for non-GET requests) and fires `turbo:submit-start`. When the response arrives it decides what to render. For a **non-GET** ("stateful") submission, Turbo applies a rule that a plain browser does not: the response must be a redirect, unless it reports an error. ## The three outcomes | Response to a non-GET Drive form | What Turbo does | |---|---| | A redirect, which `fetch` follows to a 200 page | Starts a visit to the final URL and renders the HTML it already fetched; the address bar shows the new URL | | 4xx (for example 422) | Renders the returned HTML in place; the URL stays where it was; scroll goes to the top unless the page asks to preserve it | | 5xx | Renders the returned HTML as an error page | | 200 with no redirect | Logs `Form responses must redirect to another location` with `console.error` and leaves the page untouched | A `<turbo-stream>` response (`text/vnd.turbo-stream.html`) is handled separately by the streams machinery and is outside this rule. Turbo also clears its snapshot cache after a successful non-GET submission, because the data behind cached pages may have changed. ## Why a 200 render is refused Without Turbo, a browser that renders the result of a POST keeps the POST in its history; pressing reload brings up the "resubmit the form?" dialog. Turbo cannot recreate that dialog. If it showed the POST's HTML and pushed the form's action URL into the address bar, a reload would send a GET to that URL, which often has no GET route at all. So Turbo stays on the current URL and insists on **Post/Redirect/Get**: write, redirect, render a page that is safe to reload. The error statuses are exempt because they describe a page that is not the result of a completed write. A **422** re-render of the form with validation errors is exactly what the user should see, and the URL staying on the form page is correct. ## Why 303 and not just any redirect Turbo itself never looks at the redirect's status code; it only sees that `fetch` was redirected. The status matters to the client that follows the redirect: - **303 See Other** means "fetch the new location with GET", whatever the original method was. - **302 Found** turns only a POST into a GET; for other methods the follow-up may repeat the original method. - `turbo-rails` tunnels PATCH, PUT and DELETE submissions as a POST carrying a `_method` parameter, which `Rack::MethodOverride` turns back into the real verb on the server. A 302 therefore usually lands as well in a Rails app, but only because of that tunnelling. That is why Turbo's handbook asks for 303 after a stateful submission, and why the Rails 8.1 scaffold controller passes `status: :see_other` to `redirect_to` in `update` and `destroy`. Its `create` action keeps the default 302, which a POST follows as a GET anyway. ## The Rails side of the rule A Rails 8.1 controller that follows the rule looks like the generated scaffold: - On success, `redirect_to @record` (with `status: :see_other` for update and destroy). - On failure, `render :new, status: :unprocessable_content` (or `:edit`). On Rack 3.1 and later `:unprocessable_content` is the name for 422; `:unprocessable_entity` is Rack's obsolete name and warns on Rack 3.2. - A GET form, such as a search box, may render a 200 directly; the redirect rule applies only to non-GET submissions. ## Common symptoms 1. **The form does nothing after a failed save.** The action renders `:new` with the default 200, so Turbo refuses it and only the console shows the error. 2. **Validation errors vanish.** The action redirects back to the form on failure, so the errors in the unsaved object are lost. 3. **A 500 replaces the whole page.** Expected: Turbo renders error responses so the user sees that something broke. Keeping the controller on the scaffold's shape avoids all three. ## Diagnosing a dead form When a form "does nothing", the evidence sits in two places. The browser's network panel shows the POST and its status: a 200 with no redirect points straight at the rule. The console shows Turbo's `Form responses must redirect to another location` error. On the server, the log line for the action reports `Completed 200 OK` where `302`, `303` or `422` was expected. The fix is in the controller, never in the form markup: switching the form to `data-turbo="false"` hides the symptom by giving the submission back to the browser, and loses Turbo for that form.
- What happens when a Turbo Drive form submission gets a 500 response?Turbo treats 5xx like 4xx in that it renders the returned HTML instead of refusing it, but it renders it as an error page that replaces the current page. The URL stays where it was, so the user sees the error page the server sent rather than a silently dead form.
- Does the redirect rule apply to a search form that uses GET?No. The rule applies only to non-GET submissions. A GET form is a normal visit to the action URL with the fields as query parameters, so the server simply renders a 200 page. Rendering it into a frame or updating the URL is configured with `data-turbo-frame` and `data-turbo-action`.
- Why does Turbo clear its page cache after a successful form submission?A non-GET submission usually changes data that cached snapshots display. Turbo clears the snapshot cache so a later Back or a preview does not show a page from before the write.
- Rendering the form again with the default 200 status is fine; Turbo will show the errors.
- :unprocessable_entity is the current Rails 8.1 name for a 422 response.
- Turbo reads the redirect status itself and rejects any redirect that is not 303.
- On failure you should redirect back to the form so Turbo can follow it.
- Adding data-turbo-stream to the form is what makes redirects work under Turbo.
- A 500 response is ignored by Turbo and the form simply stays on screen.
Assumes Turbo 8 with Rails 8.1 on Rack 3.1+, where 422 is :unprocessable_content and :unprocessable_entity is the obsolete Rack name.