skip to content

In a Rails 8.1 app on Turbo, how do data-turbo-method and data-turbo-confirm make a link send a confirmed DELETE in place of rails-ujs?

level: middleimportance: should knowfreq 50%

answer

  1. the link becomes a hidden form
  2. requestSubmit, removed on submit-end
  3. turbo-rails tunnels the verb via _method
  4. confirm needs a method or stream
  5. Turbo.config.forms.confirm returns a Promise

basics

~20 s

On a click, Turbo turns a link with data-turbo-method into a hidden form and submits it with that method; data-turbo-confirm asks window.confirm first. They replace rails-ujs's data-method and data-confirm, though button_to is the accessible choice.

solid answer

~40 s

Turbo watches clicks on links that carry `data-turbo-method` (or `data-turbo-stream`). It builds a hidden `<form>` with the link's URL as the action and the method from the attribute, copies `data-turbo-frame`, `data-turbo-action`, `data-turbo-confirm` and `data-turbo-stream` onto it, appends it to the body, calls `requestSubmit()` and removes it on `turbo:submit-end`. From there it is an ordinary Drive form submission: the CSRF token header, the redirect rule, and turbo-rails sending PATCH or DELETE as a POST with `_method`. `data-turbo-confirm` is read from the form or submitter, so on a link it only works together with `data-turbo-method`; the default dialog is `window.confirm`, replaceable with `Turbo.config.forms.confirm`, which should return a Promise of a boolean. Both replace rails-ujs's `data-method` and `data-confirm`, which stopped being the default in Rails 7. A real `button_to` form is still the better choice for accessibility.

code

erb · 5 lines
erb
<%= link_to "Delete comment", comment_path(@comment),
      data: { turbo_method: :delete, turbo_confirm: "Delete this comment?" } %>

<%= button_to "Delete comment", comment_path(@comment), method: :delete,
      form: { data: { turbo_confirm: "Delete this comment?" } } %>

go deeper

for a junior

Know the two attributes, what they replaced (rails-ujs's data-method and data-confirm) and that button_to renders a real form for the same job.

for a middle

Walk through the hidden form, requestSubmit and turbo:submit-end, the _method tunnelling and why confirm only fires on links that also carry data-turbo-method.

for a senior

Catch the failure modes in review: method links under data-turbo="false", leftover UJS attributes, and destructive links that screen-reader users cannot identify as actions.

for a principal

Set the team default to button_to for state changes and keep method links as a documented exception, so behaviour does not depend on JavaScript loading.

## Why links needed help A click on an `<a>` always sends a GET. RESTful Rails routes need DELETE for `destroy` and PATCH for `update`, and a destructive link should ask before acting. Before Rails 7 the default answer was **rails-ujs**, reading `data-method`, `data-confirm` and `data-remote`. New apps no longer ship it: Turbo provides `data-turbo-method` and `data-turbo-confirm` instead, and the Rails guide recommends Turbo for these jobs. ## What happens on the click Turbo's form-link observer handles a click on a link that has `data-turbo-method` or `data-turbo-stream`, provided the link is navigable (Drive enabled, no `data-turbo="false"` above it). It then: 1. Creates a `<form hidden>` with `data-turbo="true"`, `action` set to the link's URL, and any query parameters moved into hidden inputs. 2. Sets the form's `method` attribute to the value of `data-turbo-method`. 3. Copies `data-turbo-frame`, `data-turbo-action`, `data-turbo-confirm` and `data-turbo-stream` from the link to the form. 4. Appends the form to `<body>`, calls `requestSubmit()` and removes the form when `turbo:submit-end` fires. Everything after that is an ordinary Turbo form submission: - for a non-GET method Turbo adds the `X-CSRF-Token` header from the `csrf-token` meta tag; - turbo-rails rewrites PATCH, PUT and DELETE into a POST carrying `_method`, and `Rack::MethodOverride` (in every non-API Rails app's middleware) restores the verb so the request reaches `destroy`; - the response must redirect (303 is the safe status) or render with 4xx. ## How confirmation works `data-turbo-confirm` is read from the **submitter or the form**, not from links in general. That is why it only takes effect on a link that also has `data-turbo-method` (or `data-turbo-stream`), because only those links become forms. A plain GET link with `data-turbo-confirm` and no method navigates without asking. | Setting | Effect | |---|---| | `data-turbo-confirm="Delete this?"` on the form, submitter or method link | asks before submitting | | default confirm method | the browser's `confirm()`, wrapped in a resolved Promise | | `Turbo.config.forms.confirm = fn` | `fn(message, form, submitter)` should return a Promise that resolves to `true` or `false`; Turbo awaits it | | `Turbo.setConfirmMethod(fn)` | deprecated in Turbo 8; prints a warning and sets `Turbo.config.forms.confirm` | A custom confirm method is how teams swap the native dialog for an in-page `<dialog>` element. ## Traps - **Drive disabled means GET.** Under `data-turbo="false"`, or with `Turbo.session.drive = false` and no `data-turbo="true"`, Turbo ignores the method link and the browser follows it as a plain GET, typically landing on the `show` page instead of deleting anything. - **Leftover UJS attributes.** `data-method` and `data-confirm` do nothing without rails-ujs. Turbo also never hover-prefetches links carrying them, nor links with a non-GET `data-turbo-method` or with `data-turbo-confirm`. - **Accessibility.** A link announces itself as navigation. Screen readers and keyboard users expect a button for an action, so the Turbo handbook and the Rails guide both suggest a real form, which Rails renders with `button_to`. Its confirmation goes on the generated form: `form: { data: { turbo_confirm: "..." } }`. - **Query strings move.** Turbo copies the link's query parameters into hidden inputs, so they travel in the request body; Rails merges both into `params`, but anything reading the raw query string misses them. - **Missing token, rejected request.** Turbo reads the token from the `csrf-token` meta tag that `csrf_meta_tags` renders. A layout without it sends no `X-CSRF-Token`, and Rails rejects the DELETE with `ActionController::InvalidAuthenticityToken`. ## Moving off rails-ujs Apps upgraded from Rails 6 often still carry UJS markup. The usual mapping: | rails-ujs | Turbo 8 replacement | |---|---| | `data-method="delete"` | `data-turbo-method="delete"`, or a `button_to` form | | `data-confirm="..."` | `data-turbo-confirm="..."` on the form, submitter or method link | | `data-remote="true"` on a form | nothing: Turbo already submits forms with `fetch` | | `data-disable-with="Saving..."` | `data-turbo-submits-with="Saving..."` on the submitter; Turbo disables the submitter by default | Because the hidden form also receives `data-turbo-frame` and `data-turbo-action`, a method link can target a frame or choose how history changes, exactly like a form would. ## When to use which Use `button_to` for destructive or state-changing actions by default. Keep `data-turbo-method` for places where markup must stay a link, such as a menu built from anchors, and accept that it needs JavaScript to work at all: without Turbo it is just a GET link.

  • A destroy link with data-turbo-method="delete" sits inside a section marked data-turbo="false". What happens on click?
    Turbo treats the link as not navigable, so it never builds the hidden form. The browser follows the href as a plain GET, which in a RESTful app usually renders the record's `show` page. Nothing is deleted, and no confirmation appears.
  • How does a DELETE from a Turbo method link reach the destroy action?
    turbo-rails rewrites the fetch into a POST with `_method=delete` in the body, and `Rack::MethodOverride`, present in the middleware of every non-API Rails app, turns it back into a DELETE before routing. Turbo also sends the `X-CSRF-Token` header from the page's meta tag.

  • data-method="delete" still works in a new Rails 8.1 app without adding rails-ujs.
  • data-turbo-confirm on a plain GET link pops up a confirmation before navigating.
  • Turbo.setConfirmMethod is the current way to install a custom confirm dialog.
  • A method link inside data-turbo="false" still sends a DELETE, just without Turbo.
  • data-turbo-method links are as accessible as buttons, so button_to is unnecessary.

Assumes Turbo 8 with turbo-rails 2.0.x on Rails 8.1; Turbo.setConfirmMethod is deprecated in favour of Turbo.config.forms.confirm, and rails-ujs is no longer in new apps.