0

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?" class="button_to" method="post" action="/en/parks/7"><input type="hidden" name="_method" value="delete" autocomplete="off"><button class="btn btn-danger" 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.

Rebecca
  • 88
  • 1
  • 9
  • Hi, Please have a look at the following link. https://stackoverflow.com/a/70671361/12111186 This will help you – Amol Mohite Sep 15 '22 at 07:43
  • Thanks @AmolMohite I previously saw this thread, uninstalled and reinstalled turbo-rails 1.1.1 and it doesn't fix the issue. – Rebecca Sep 15 '22 at 07:50

3 Answers3

1

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 %>
Divyaraj Solanki
  • 351
  • 1
  • 10
  • Where to add the javascript? When I add it to the view file it just gets rendered as text. I'm a beginner thanks for your patience. – Rebecca Sep 15 '22 at 07:54
  • 1
    add java script at app>assets>javascripts>application.js – Divyaraj Solanki Sep 15 '22 at 08:58
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – primo Sep 21 '22 at 04:37
0

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

Rebecca
  • 88
  • 1
  • 9
  • If I am using tailwind, is it normal to not get a rails pop up confirmation? – xor Nov 27 '22 at 19:41
  • I'm using bootstrap so not sure about tailwind but I'm pretty sure you should still be getting the browser confirmation popup (not a tailwind component) – Rebecca Dec 17 '22 at 16:58
0
<%= link_to fa_icon('trash'), @blog, data: { turbo_method: :delete,
                          turbo_confirm: "Are you sure you want to delete this post" } %>

This worked for me. The confirmation message pops up and the link to delete works.

Here's the controller for destroy:

def destroy
    @blog.destroy

    respond_to do |format|
      format.html { redirect_to blogs_url, status: :see_other }
    end
  end

The "turbo_method: :delete" and the "status: :see_other" are the crucial parts. sorry if I'm late to answer this, but this is working for me on my rail 7 app. It took a lot of googling before I finally got past the need for "button_to". I hope this helps someone.

DebakeDSP94
  • 35
  • 1
  • 7