5
<%= link_to "Destroy", article_path(@article), data: {
                turbo_method: :delete,
                turbo_confirm: "Are you sure?"
              } %>

This is listed on the official website of Ruby on Rails as a simple way of deleting an Active Record. I have the same controller method as they do. This does not delete, nor does it confirm. I have found other methods that successfully delete, but none that confirm. I would like to understand why this doesn't delete, and how I can confirm.

  • Its almost guarenteed to be either that you don't even have turbo present or its a javascript error. Guess this is the new version of the age old broken Rails UJS question which is generally inanswerable as there are so many potential causes. – max Feb 21 '22 at 23:37
  • @mechnicov rails version 7.0.2.2 – thelovedolphin Feb 22 '22 at 02:39
  • @max how can I rectify that? I followed the guide step by step and installed anything they told me to – thelovedolphin Feb 22 '22 at 02:40
  • 1
    `<%= button_to "Delete", @article, method: :delete %>` @mechnovic this works without confirmation – thelovedolphin Feb 22 '22 at 02:42
  • 1
    Ah, I see if you're talking about the [Working with JavaScript in Rails guide](https://guides.rubyonrails.org/working_with_javascript_in_rails.html) it has not been updated since Rails 6 and it still shows the obsolete setup for Rails UJS and not Turbo. You'll have to use the docs for turbo instead https://turbo.hotwire.dev/handbook/streams – max Feb 22 '22 at 02:53
  • 1
    The entire approach to JS changed in Rails 7 with new libraries replacing UJS and turbolinks and the removal of webpacker and it will take a while for the guides to catch up. – max Feb 22 '22 at 02:56
  • https://stackoverflow.com/a/70994323/10608621 `button_to` with confirmation – mechnicov Feb 22 '22 at 12:10
  • @mechnicov the provided code from that thread still does not confirm for me XD – thelovedolphin Feb 23 '22 at 15:39
  • You have some problems with JS in your project but your question does not represent it – mechnicov Feb 23 '22 at 18:18
  • @mechnicov my project is the exact same as the one in this guide https://guides.rubyonrails.org/getting_started.html can you tell me where their error is? – thelovedolphin Feb 23 '22 at 19:29

3 Answers3

1
<ul>
<li><%= link_to "Edit", edit_article_url(@article) %> </li>
<li><%= button_to "Destroy", article_path(@article), method: :delete,
            data: { method_turbo_confirm: "Are you sure?" }
 %></li>

Changing link_to to button_to and adding method: :delete worked for me. Although the "Are you sure?" pop up is still not working.

Will
  • 1,123
  • 1
  • 9
  • 22
  • Do you know why `button_to` is required? Using : `turbo_confirm: "Are you sure?"` got the popup working. – fatfrog Aug 14 '23 at 17:20
0

This SO post addresses a similar problem and answered by @json fb. Quoting statements from the SO post

This happens if you had locally installed gem versions 7.1.0 or 7.1.1 for turbo-rails

These gem numbers were pushed by accident to Rubygems in October, then yanked. However, since bundler will default to the highest number of your Rails gem when it sets up your new rails app, it will pick turbo-rails version 7.1.0 or 7.1.1 , which will display this flaw

The gems were yanked, so this only affects you if you were developing rails apps between October 2021 and the yank date. TO FIX YOUR COMPUTER: gem uninstall turbo-rails

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • removing the turbo-rails gem just caused rails to not work anymore. I had to reinstall the gem and run bundle install for it to start working again. And the issue with turbo still wasn't fixed – ShadowCrafter_01 Sep 05 '22 at 07:03
0

try to add status to redirect like here "redirect_to root_path, status: :see_other" For more clarification https://guides.rubyonrails.org/getting_started.html#deleting-an-article

def destroy
    @article = Article.find(params[:id])
    @article.destroy

    redirect_to root_path, status: :see_other
  end
Bidan
  • 31
  • 3