skip to content

Browsers submit forms only as GET or POST, so what HTML does Rails' form_with emit to reach a PATCH or DELETE route?

level: middleimportance: must knowfreq 60%

answer

  1. the form tag still says post
  2. a hidden input carries the real verb
  3. name="_method" value="patch"
  4. method: overrides persisted?
  5. GET forms: no _method, no token

basics

~10 s

form_with keeps method="post" on the form tag and adds a hidden input named _method whose value is patch, put or delete. Rails' method-override middleware reads it, so routing sees the intended verb.

solid answer

~40 s

HTML forms only know GET and POST, so Rails tunnels the other verbs. For a persisted record, or any explicit `method: :patch`, `:put` or `:delete`, `form_with` renders `<form method="post">` and puts `<input type="hidden" name="_method" value="patch">` first inside it, before the authenticity token. The `Rack::MethodOverride` middleware in a full Rails stack reads that parameter from the POST body and rewrites the request method, so `resources :pets` routes it to `update` or `destroy`. The `method:` option wins over the `persisted?` inference. `method: :get` is the other branch: the form tag says get, and neither `_method` nor the token is emitted. With `load_defaults 8.1` these hidden inputs no longer carry `autocomplete="off"`.

code

erb · 15 lines
erb
<%# Update form for a saved pet: PATCH inferred from persisted? %>
<%= form_with model: @pet do |f| %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

<%# Archive form: explicit verb wins over the inference %>
<%= form_with model: @pet, method: :delete do |f| %>
  <%= f.submit "Archive profile" %>
<% end %>

<%# Filter form: GET, so no _method and no token %>
<%= form_with url: pets_path, method: :get do |f| %>
  <%= f.search_field :species %>
<% end %>

go deeper

for a junior

Recall the trick: the form says post, a hidden _method input says patch or delete, and Rails routes by the hidden value.

for a middle

Walk through the precedence: method: first, then persisted?, then POST. Know the GET branch drops both _method and the token, and that the middleware rewrites the verb before routing.

for a senior

Expect to debug an update that hits create: check the rendered _method input, the method: option, and whether the app runs as API-only. Mention the 8.1 autocomplete change when markup-matching tests break on upgrade.

for a principal

Weigh verb tunnelling against designing HTML-first flows around POST-only actions, and decide where API clients should get real verbs instead of form conventions.

## Why Rails needs a workaround An HTML `<form>` element accepts only `get` and `post` as its `method`. REST-style routes in Rails, however, expect **PATCH** (or PUT) for updates and **DELETE** for removals: `resources :pets` maps `PATCH /pets/:id` to `update` and `DELETE /pets/:id` to `destroy`. Rails bridges the gap by **tunnelling** the intended verb inside an ordinary POST. ## What `form_with` writes Action View decides the verb first and then renders the form tag and the hidden fields that go with it. The rules, taken from `extra_tags_for_form` in `FormTagHelper`, are: | Requested verb | `<form method>` | Hidden `_method` input | Authenticity token input | |---|---|---|---| | none or `:post` | `post` | none | yes | | `:patch`, `:put`, `:delete` | `post` | `value="patch"` etc. | yes | | `:get` | `get` | none | none | Where the requested verb comes from: 1. An explicit **`method:`** option, if you passed one. 2. Otherwise, with `model:`, **PATCH** when `model.persisted?` is true. 3. Otherwise POST, the default. Because the explicit option is checked first, `form_with model: @pet, method: :delete` produces a deletion form even though the pet is persisted and would otherwise get PATCH. The hidden field itself is produced by `method_tag`: `<input type="hidden" name="_method" value="patch">`. It is placed at the top of the form, ahead of the token and your own fields. ## What happens on the server - The browser sends an ordinary POST whose body contains `_method=patch` next to `pet[name]=...`. - `Rack::MethodOverride`, which a full Rails app puts in its middleware stack, reads `_method` from the form body of a POST and replaces the request method before routing runs. - The router then matches `PATCH /pets/7` and calls `PetsController#update`, and `request.request_method` reports PATCH. - An app generated with `--api` leaves that middleware out, so a `_method` field there is just another parameter; API clients send real PATCH and DELETE requests instead. ## The GET branch `method: :get` is used for filters and searches, such as a form that lists pets by species. The tag gets `method="get"`, the fields travel in the query string, and Rails emits neither `_method` nor the authenticity token. A token would leak into URLs, logs and the browser history, and GET requests are not checked for one. ## The Rails 8.1 change Apps on `load_defaults 8.0` or earlier render the `_method` input, the authenticity token input and the hidden companions of some field helpers with `autocomplete="off"`. **`config.action_view.remove_hidden_field_autocomplete`**, set to `true` by `load_defaults 8.1`, drops that attribute. A new 8.1 app therefore renders `<input type="hidden" name="_method" value="patch">` with no `autocomplete`; an upgraded app keeps the attribute until it adopts the 8.1 defaults. Tests or system specs that match the exact hidden-field markup can break during that upgrade, which is the practical reason to know about it. ## Debugging an update that lands in the wrong action 1. View the page source and look for the hidden `_method` input; if it is missing, the record was not persisted or an explicit `method:` replaced the inference. 2. Read the server log line `Started PATCH "/pets/7"`; if it says POST, the override did not run. 3. Check `config.api_only`, which removes the override middleware. 4. List the routes the controller exposes with `bin/rails routes -c pets` to confirm PATCH and DELETE map where you expect. ## Common misreadings - **"The form tag says `method="patch"`."** It never does; Rails would be relying on browser support that does not exist. - **"`_method` works on any request."** The middleware only honours it on POST requests with a form body. - **"Passing `url:` turns PATCH back into POST."** The verb comes from `persisted?` or `method:`; `url:` changes only the action. - **"`data-method` does the same job."** That attribute is written by `link_to ... method:` and only acts when rails-ujs is loaded, which a new app does not include; Turbo reads `data-turbo-method` on links, and forms carry the verb in `_method`. ## What to say in an interview Name the HTML limit, the hidden `_method` input on a POST form, the middleware that rewrites the verb, and the route it reaches. Then add the precedence (`method:` before `persisted?`), the GET branch with no token, and, for an 8.1 app, the missing `autocomplete="off"`.

  • Why does a _method field do nothing in an app generated with --api?
    With `config.api_only = true`, Rails leaves `Rack::MethodOverride` out of the middleware stack, so nothing rewrites the request method and `_method` arrives as an ordinary parameter. API clients are expected to send real PATCH and DELETE requests; HTML forms are not the interface there.
  • A persisted pet's form should create a copy rather than update the original. What is the smallest change?
    Override both inferred parts: pass `url: pets_path` and `method: :post`. The explicit `method:` suppresses the `_method=patch` field, and the URL points at `create`. Usually it is clearer to build a new record with `@pet.dup` in the controller and let `form_with model:` infer a create form from it.

It is like a letter that must travel as standard post: the envelope says POST because that is all the mail service accepts, but the first line inside reads "treat this as an amendment", and the clerk who opens it files it accordingly.

  • form_with renders method="patch" on the form tag for a saved record.
  • Passing url: to form_with switches a persisted record's form back to POST.
  • A method: :get form still carries the authenticity token and a _method field.
  • data-method on a form tells Rails which verb to route.
  • The _method parameter is honoured on GET requests too.

Assumes Rails 8.1 with load_defaults 8.1, which sets remove_hidden_field_autocomplete so the hidden _method and token inputs no longer carry autocomplete="off".