0

Rails link_to code: <%= link_to "<i class='fa fa-trash'></i>".html_safe, resource_path(id: id), method: :delete, data: { confirm: "Are you sure...?" }, class: "text-center dropdown-item text-danger bg-light-danger", title: "Delete"%>

Html version of above link_to tag: <a data-confirm="Are you sure...?" class="text-center dropdown-item text-danger bg-light-danger" title="Delete" rel="nofollow" data-method="delete" href="/resource/1"><i class="fa fa-trash"></i></a>

The above code is taking always to show instead of destroy. may be because of some jquery or other js library. same code is working in some of the projects but not working in some of the projects. checked with different rails version but it's not because of rails version.

Tried with using different Jquery CDN. also installed jquery using yarn in latest rails 6 and 7 version but issue is same. Some reference solutions that didn't work in my case. Issue with delete action , Using link_to with delete action in rails , Why Rails "link_to" does not work for delete action?

1 Answers1

0

Both

method: :delete

and

data: { confirm: "Are you sure" }

rely on rails-ujs to work correctly. This needs to be initialised in either sprockets, webpack, esbuild, rollup or importmap depending on which you are using.

If you are using Rails 7 / Turbo then you might want to change your code to use something like the following:

<%= button_to resource_path(resource), method: :delete,
    form: { data: { turbo_confirm: "Are you sure...?" } },
    class: "text-center dropdown-item text-danger bg-light-danger", title: "Delete" do %>
  <i class='fa fa-trash'></i>
<% end %>
Ben Trewern
  • 1,583
  • 1
  • 10
  • 13