In a Rails 8.1 view, why should a Delete control for a record be built with button_to rather than link_to?
answer
- an anchor is always a GET
- post form plus hidden _method
- Rack::MethodOverride rewrites the verb
- authenticity_token travels inside the form
- data-method needs rails-ujs
basics
~20 slink_to renders an <a> tag, and following a link is a GET, so it cannot safely or correctly reach destroy. button_to renders a small POST form with a hidden _method=delete field and a CSRF token, which Rails routes to destroy.
solid answer
~40 s`link_to` builds an `<a href>`, and the browser follows it with GET. GET is meant to be safe, so prefetchers and crawlers may follow it, and `GET /listings/42` routes to `show`, not `destroy`. `button_to "Delete", listing_path(listing), method: :delete` builds `<form class="button_to" method="post">` holding the `<button>`, a hidden `_method=delete` input and the `authenticity_token` input. `Rack::MethodOverride` turns that POST into a DELETE, the router sends it to `destroy`, and CSRF protection passes. Passing `method: :delete` to `link_to` only writes `data-method="delete"` and `rel="nofollow"`, which only rails-ujs ever read, and a new Rails 8.1 app does not ship rails-ujs, so the click stays a GET. On `button_to`, `params:` adds hidden fields, `data:` adds attributes to the button, and `form:` / `form_class:` style the form.
code
erb · 9 lines<%# app/views/listings/_listing.html.erb %>
<article id="<%= dom_id(listing) %>">
<h2><%= link_to listing.title, listing_path(listing) %></h2>
<%= button_to "Delete", listing_path(listing),
method: :delete,
params: { return_to: "catalog" },
form_class: "inline",
data: { action: "listing#confirm" } %>
</article>go deeper
Know that links are GETs and that state-changing actions go through button_to or a form, and be able to write button_to with method: :delete.
Explain the generated form: post method, hidden _method, authenticity token, Rack::MethodOverride, and what params:, data:, form: and form_class: each touch.
Spot the rails-ujs leftovers in an upgraded app, the nested-form bug, and GET routes that change state, and explain why prefetchers make them dangerous.
Set the convention for destructive controls across the app, forms over JavaScript-driven links, so behaviour does not depend on a script loading.
## Two helpers, two HTML elements A vinyl-record shop lists its **listings** (`Listing` records) with a price, a relative timestamp and a **Delete** control for the seller. Action View offers two helpers that look interchangeable but produce different HTML: - `link_to(name, url, html_options)` renders an **anchor**, `<a href="...">`. A browser follows an anchor with an HTTP **GET**. - `button_to(name, url, html_options)` renders a **form** containing one submit button. The form submits with **POST**, and Rails can turn that POST into DELETE, PATCH or PUT. Deleting is a state change, so it must not ride on GET. GET is meant to be **safe**: browsers prefetch links, crawlers follow them, and a user can bookmark or reload one. With `resources :listings`, a GET to `/listings/42` is routed to `show` anyway, so the link never reaches `destroy`. ## What button_to renders ```erb <%= button_to "Delete", listing_path(listing), method: :delete, params: { return_to: "catalog" }, form_class: "inline" %> ``` produces, in a Rails 8.1 app: ```html <form class="inline" method="post" action="/listings/42"> <input type="hidden" name="_method" value="delete"> <button type="submit">Delete</button> <input type="hidden" name="authenticity_token" value="..."> <input type="hidden" name="return_to" value="catalog"> </form> ``` Three mechanisms make it work: 1. HTML forms only submit GET or POST, so `button_to` writes `method="post"` on the form and adds a hidden **`_method`** input for `patch`, `put` or `delete`. 2. `Rack::MethodOverride`, part of the default middleware stack of a non-API app, reads `_method` on a POST and changes the request method to DELETE before routing. 3. For any POST form, `button_to` adds the **`authenticity_token`** hidden input, so `protect_from_forgery` accepts the request. When the target is a model instead of a path, `button_to "Publish", listing` builds the URL with `url_for` and picks the verb from the record: **PATCH** for a persisted record, **POST** for a new one. With a plain path and no `method:`, the form simply POSTs. ## Why link_to with method: no longer deletes anything `link_to` still accepts `method:`, but all it does is write two attributes: `data-method="delete"` and `rel="nofollow"`. Turning those into a request was the job of **rails-ujs**, a JavaScript library that was the default before Rails 7; the Rails 8.1 generators do not add it. Without it the browser ignores `data-method`, and the "Delete" link performs a GET. Turbo has its own link attribute for non-GET visits, but the `link_to` documentation recommends a form for non-GET requests, and the Rails JavaScript guide adds that real buttons and forms are better for accessibility. ## The options that matter | Option | Where it lands | Use | |---|---|---| | `method:` | hidden `_method` input (patch, put, delete) | the verb the router sees | | `params:` | one hidden input per key; nested hashes become `country[name]` | extra values such as `return_to` | | `data:` | `data-*` attributes on the **button** | hooks for Stimulus or Turbo attributes | | `form:` | attributes on the **form** element | an id, a `data-*` on the form | | `form_class:` | the form's `class` | replaces the default `button_to` class | | `disabled:` | the button | a disabled control | A block replaces the label, so the button can hold markup: `button_to(listing_path(listing), method: :delete) { tag.span("Delete") }`. ## Traps interviewers listen for - **Nesting.** `button_to` emits its own `<form>`, and HTML does not allow a form inside a form. Placed inside a `form_with` block, the inner form tag is ignored by the parser, so the button and its hidden `_method` input join the edit form, and the inner closing tag ends the edit form early. Render it outside the edit form. - **Styling.** Because the output is a form, a row of controls often needs CSS for the `button_to` class (or `form_class:`) to sit inline. - **GET buttons.** `button_to "Filter", listings_path, method: :get` makes a GET form, and no token is added because none is needed. - **Version details.** Since `load_defaults 7.0` the control is a `<button>` rather than an `<input type="submit">`; under `load_defaults 8.1` the hidden inputs no longer carry `autocomplete="off"`. ## Mistakes interviewers listen for | Belief | What Rails actually does | |---|---| | `link_to ..., method: :delete` sends DELETE | it writes `data-method`, which nothing reads without rails-ujs | | the form carries `method="delete"` | the form posts; the verb travels in `_method` | | a button is exempt from CSRF | `button_to` adds the `authenticity_token` input for every POST form | | `data:` styles the form | `data:` lands on the button; `form:` and `form_class:` target the form | A real `<button>` inside a real form also needs no JavaScript to work, is reachable by keyboard, and is announced as a button by screen readers, which is the accessibility reason the Rails JavaScript guide gives for preferring it.
- What goes wrong if you put button_to inside a form_with block for the same listing?`button_to` emits its own `<form>`, and HTML forbids nested forms: the parser ignores the inner form tag, so the Delete button and its hidden `_method=delete` input join the edit form, and the inner closing tag ends the edit form early. Submissions then carry the wrong fields or verb. Render the delete control outside the edit form.
- What verb does button_to use when you pass a model, as in button_to "Publish", listing?It builds the URL with `url_for(listing)` and, without `method:`, picks the verb from the record: PATCH (a hidden `_method=patch`) when `listing.persisted?`, POST for a new record. With a plain path string and no `method:`, the form just POSTs.
- How do params: values reach the controller, and how are nested hashes named?Each pair becomes a hidden input inside the generated form, so `params: { return_to: "catalog" }` arrives as `params[:return_to]`. Nested hashes are flattened to bracketed names, so `{ filter: { genre: "jazz" } }` becomes an input named `filter[genre]`.
- link_to with method: :delete sends a DELETE in a default Rails 8.1 app
- button_to puts method="delete" on the form element itself
- button_to skips the CSRF token because it is only a button
- button_to's data: option sets attributes on the form element
- A delete link is safe as GET because only signed-in sellers see it
Assumes Rails 8.1 with load_defaults 8.1: button_to renders a <button> (a 7.0 default) and its hidden inputs no longer get autocomplete="off" (an 8.1 default); new apps ship no rails-ujs.