Home > Mobile >  "Are you sure?" confirmation popup not showing for Delete button, Rails 7
"Are you sure?" confirmation popup not showing for Delete button, Rails 7

Time:09-15

When admins click the "Delete park" button, I want an "Are you sure?" confirmation dialogue to pop up before the park is deleted.

I've read lots of other people using Rails 7 had success by adding the confirm message to form, like this:

<% provide(:title, @park.name) %>

<%= render 'park_details' %>

<div>
  <%- if current_user && current_user.admin? %>
    <%= link_to "Edit this park", edit_park_path(@park) %> |
  <% end %>

  <%= link_to "Back to parks", parks_path %>

  <%- if current_user && current_user.admin? %>
    <%= button_to "Delete park", @park, method: :delete,
        class: "btn btn-danger",
        form: { data: { turbo_confirm: "Are you sure?" } } %>
  <% end %>

</div>

The above doesn't work for me. The park gets deleted without confirmation message.

The html for the form is rendered like this:

<form data-turbo-confirm="Are you sure?"  method="post" action="/en/parks/7"><input type="hidden" name="_method" value="delete" autocomplete="off"><button  type="submit">Delete park</button><input type="hidden" name="authenticity_token" value="XuQsxUyS0LiyYyP_xm1f7XFv9iCkBejRLnSu6DwWOxwQZQVDvkAI_NMRPTuAhLplMbcDZwldwOzmIq_5LqiGnw" autocomplete="off"></form>

In addition, if I use link_to instead of button_to, the park doesn't get deleted at all (not sure if this is relevant but including it in the description just in case).

Could it be an issue with javascript, as someone suggested here? Ruby on rails: <%= link_to 'Destroy'... doesn't work, but <%= button_to 'Destroy'... does work perfectly

I'm a beginner so not sure what config/code to check. Let me know if there are more details I can post to help solve this issue.

CodePudding user response:

try to do with javascript

$(document).on 'click', '.park', ->
  if confirm('Are you sure?')
    delete_park($(this).data('url'))
  return



  <%- if current_user && current_user.admin? %>
    <%= button_to "Delete park", @park, method: :delete,
        class: "btn btn-danger del_park",
        form: { data: { turbo_confirm: "Are you sure?" } } %>
  <% end %>

CodePudding user response:

Solved it by reinstalling Turbo, as suggested by michael-reeves here: https://github.com/rails/rails/issues/44170

$ rails importmap:install

$ rails turbo:install stimulus:install

  • Related