Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call confirm prompt using button_to in Rails with Turbo

Previously in Rails when using the button_to tag, it was possible to use a confirmation dialog like this

<%= button_to 'Destroy', @post, method: :delete, data: { confirm: 'Are you sure?' } %>

data: { confirm: 'Are you sure?' } is Rails magic data attribute that was used by @rails/ujs library under the hood

Following Rails 7, this library is no longer on by default. Instead of this Rails use Turbo library

And now this code does not work

There is no information in official Rails docs and Turbo handbook

What I tried

<%= button_to 'Destroy', @post, method: :delete, data: { turbo_confirm: 'Are you sure?' } %>
<%= button_to 'Destroy', @post, method: :delete, data: { 'turbo-confirm': 'Are you sure?' } %>

But there is no result

I didn't find any solution on SO but found on Hotwire forum. This solution with Stimulus action. I just improve it a little

<%= form_with model: @post, method: :delete, data: { controller:  'confirmation', message: 'Are you sure?', action: 'submit->confirmation#confirm' } do |f| %>
  <%= f.submit 'Destroy' %>
<% end %>
// app/javascript/confirmation_controller.js
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
  confirm(event) {
    if (!(window.confirm(this.element.dataset.message))) {
      event.preventDefault()
    }
  }
}

It works but it's quite difficult and looks ugly, and we are used to Rails being cool

like image 207
mechnicov Avatar asked Jan 22 '26 04:01

mechnicov


2 Answers

In Rails with Turbo without rails-ujs to call confirmation popup window with button_to we need to use code like this

<%= button_to 'Destroy', @post, method: :delete, form: { data: { turbo_confirm: 'Are you sure?' } } %>

or

<%= button_to 'Destroy', @post, method: :delete, form: { data: { 'turbo-confirm': 'Are you sure?' } } %>

Both generate data-turbo-confirm attribute

So we need to add this attribute not to submit button (like in rails-ujs) but directly to form containing this button (let me remind you this tag generates a form with button)

like image 98
mechnicov Avatar answered Jan 23 '26 19:01

mechnicov


This is a bit complicated in 7.0.3, if it is a page that is using turbo it looks like this:

  <%= button_to "Delete",
                  user_path(user),
                  method: :delete,
                  class: "button tight danger",
                  form: { 
                   data: { 
                     turbo_confirm: "Are you sure you want delete?"
                   }
                  } %>

This makes a little form. Now if you are using turbo, but not on that specific page, you no longer get the simple comfirm: 'message' from the old rails ujs. Instead you have to use stimulus controllers.

# app/javascript/controllers/confirmation_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  static values = { message: String };

  confirm(event) {
    if (!(window.confirm(this.messageValue))) {
      event.preventDefault();
      event.stopImmediatePropagation();
    };
  };
}

then

# app/javascript/controllers/index.js
import ConfirmationController from "./confirmation_controller"
application.register("confirmation", ConfirmationController)

then

    <%= button_to "Delete",
                  user_path(user),
                  method: :delete,
                  class: "button danger",
                  form: {
                    data: {
                      turbo: false,
                      controller: "confirmation",
                      action: 'submit->confirmation#confirm',
                      confirmation_message_value: "Are you sure you want to delete?",
                    }
                  } %>

It is a bummer that rails removed functionality, but if you want to use hotwire you need to commit to full buy in to the whole ecosystem.

like image 32
waiting4op2deliver Avatar answered Jan 23 '26 19:01

waiting4op2deliver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!