'turbo_confirm not working in a Rails 7 app with turbo-rails 1.0.1
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.
Solution 1:[1]
I was able to solve this issue locally with the following changes:
- What value I supplied to the path
- 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?'} } %>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dan |
