2

In a Rails 7 app, I am trying to ask the user to confirm before destroying a resource.

In my erb view, I have the following tag:

<%= button_to 'Destroy', account_path(@account), method: :delete, data: { turbo_confirm: "Are you sure?" } %>

That generates the following HTML

<form class="button_to" method="post" action="/accounts/483786005">
  <input type="hidden" name="_method" value="delete" autocomplete="off">
  <button data-turbo-confirm="Are you sure?" type="submit">Destroy</button>
  <input type="hidden" name="authenticity_token" value="5shu7zu1uzqtFf-XZryoyLjXXmheOP6lWxSOPvxYhmjX7Pa1m9RxGKzvb9BeehbHqs4s_o4_SHWSBDwSi5Hr3A" autocomplete="off">
</form>

But when I click the button, the resource is destroyed without the prompt.

What can be wrong? How can I debug this?

Extra info:

  • I am using Rails 7.0.2.3 with turbo-rails 1.0.1
  • Turbo tags works fine, so the js library is being called.
Victor
  • 23,172
  • 30
  • 86
  • 125
  • https://stackoverflow.com/a/70994323/12219679 – Lam Phan Apr 19 '22 at 03:39
  • @LamPhan I have the same package versions (and issue) as Victor. I attempted to use both suggestions in that SO link and the behavior was unchanged. Is there something aside from the syntax change in the view to make this work? – dgg Apr 19 '22 at 19:25
  • @dan you should make sure that the generated `
    ` tag contains `data-turbo-confirm`, not the `
    – Lam Phan Apr 20 '22 at 02:59
  • @LamPhan I did check that and oddly enough it was generating the turbo-confirm where you said it should. However, I was able to solve this by wrapping the data/confirm with form: {} - going to post as a potential answer shortly :D – dgg Apr 25 '22 at 18:17

1 Answers1

2

I was able to solve this issue locally with the following changes:

  1. What value I supplied to the path
  2. Wrapping the data arg with a form wrapper

Changing the code in the question:

<%= button_to 'Destroy', account_path(@account), method: :delete, data: { turbo_confirm: "Are you sure?" } %>

To this:

 <%= button_to "Destroy", @account, method: :delete, form: { data: {turbo_confirm: 'Are you sure?'} } %>
dgg
  • 333
  • 3
  • 11