rails confirm before delete
Asked Answered
T

10

45

Here is my rails link_to

<%= link_to 'Delete',url_for(action: :delete,id: @user.id),data: {confirm: "Are you sure?"} %>

I tried the above method but it is directly deleting without any alert message. What wrong I made. Can any one correct it.

Thresathresh answered 25/10, 2013 at 11:2 Comment(2)
is jquery.js is included in your app?Prouty
@Prouty Yes includedThresathresh
H
66

Try this

<%= link_to 'Delete',url_for(action: :delete,id: @user.id),method: :delete, data: {confirm: "Are you sure?"} %>
Hayrack answered 25/10, 2013 at 11:10 Comment(3)
For what it's worth, the data hash is the only option in the provided solutions here that ended up working for me. Rails 4.1.2, Ruby 2.1.2p95, jquery-rails 3.1.1. That said, in other apps, I've used the confirm option outside the data hash, as an argument to link_to, and it has totally worked. What gives?Ahvenanmaa
This stumped me for a while in Rails 4.1, without putting the confirm in the data hash the attribute is rendered as "confirm" instead of the proper "data-confirm"Tot
Did they change this to data-confirm in Rails 4?Gracioso
R
11

Answer for rails 4.1.8 (question doesn't include version)

and standard resource (also not specified)

  1. //= jquery_ujs must be included in application.js (possibly missing)
  2. url is user_path(@user)
  3. specify :method => :delete (missing)
  4. confirm message within :data (was good)
  5. destroy method in controller (not delete)

= link_to t(:delete) , user_path(@user), :method => :delete, :class => "btn btn-danger", :data => {:confirm => t(:are_you_sure )}

The url in the question seems to create a GET, rather than a DELETE method. This may work if :method was specified. imho it is unfortunate that there is no seperate named url helper for this.

Rubirubia answered 30/12, 2014 at 15:6 Comment(1)
//= jquery_ujs added after //= jquery and it works like charm nowDogfight
S
10

Before Rails 7

<%= link_to 'Delete', url_for(action: :delete, id: @user.id),
  method: :delete, data: {confirm: "Are you sure?"} %>

Rails 7 (with Turbo, out of the box)

<%= link_to 'Delete', url_for(action: :delete, id: @user.id),
  data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>
Sicard answered 4/2, 2022 at 15:4 Comment(1)
this answer help me save a lot of timeRena
S
6

The answer by @installero didn't work for me in rails 7.0.3, I achieved a similar thing using button_to:

<%= button_to "Delete", @category, form: { data: { turbo_confirm: "Are you sure?" } }, method: :delete %>
Sletten answered 5/6, 2022 at 1:59 Comment(1)
This is the one for the button_to helper, the asnwer was about link_to thoughSicard
R
4
link_to('Delete', {controller: :controller_name, id: id, action: :action_name}, confirm: "Are you sure you want to delete this?", method: :delete)
Reneareneau answered 25/10, 2013 at 13:24 Comment(0)
L
3

since Rails 7 Hotwire Turbo was introduced

<%= link_to "Delete", @post, class: 'btn btn-danger',data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>


<%= button_to "Destroy this post", @post, method: :delete, class: 'btn btn-danger',  form: { data: { turbo_confirm: "Are you sure?" }} %>
Likely answered 28/10, 2022 at 11:47 Comment(0)
S
2

Rails confirmation popup box for destroy action:

    <%= link_to 'Show', article_path(article),{:class=>'btn btn-success show_article' }%>
    <%= link_to 'Edit', edit_article_path(article),{:class=>'btn btn-warning edit'} %>
    <%= link_to 'Destroy', '#', "data-toggle"=>"modal", "data-target" => "#delete-#{article.id}",:class=>'btn btn-danger' %>
    <div class="modal fade" id="delete-<%= article.id %>" role="dialog">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">Confirmation Box</button>
          </div>
          <div class="modal-body">
            <p>Are you sure to delete this article</p>
          </div>
          <div class="modal-footer">
            <div class="modal-footer">
              <%= link_to 'Delete', article_path(article), method: :delete, :class => 'btn btn-danger' %>
                <a href="#" data-dismiss="modal" class="btn btn-warning">Cancel</a>
            </div>
          </div>
        </div>
      </div>
   </div>
</td>
Scarabaeid answered 24/5, 2019 at 11:7 Comment(0)
P
1
  1. I think you should use :method option

    <%= link_to 'Delete',url_for(action: :delete,id: @user.id), method: :delete, confirm: "Are you sure?" %>

  2. It's probably better to use button (and form) for this kind of action

Pocketbook answered 25/10, 2013 at 11:10 Comment(2)
Tried but no working when i inspect i can see data-confirm="Are you sure?" and data-method="delete"Thresathresh
Okay, I'm pretty sure, that syntax is right - check again please. It's supposed to be <%= link_to 'Text', '/some/path', method: :delete, confirm: 'Some text' %>.Pocketbook
O
0

Check the following link:

<%= link_to 'Delete',url_for(action: :delete,id: @user.id), confirm: "Are you sure?" %>
Oleo answered 25/10, 2013 at 11:7 Comment(0)
S
0

Make sure you have both the jQuery library and the jQuery driver for Rails included. The jquery-rails gem will take care of both of these in the later versions of Rails, from what I know.

Also, for better chances of compatibility with future versions of the jQuery driver, move the :confirm option outside :data and let the jquery-rails gem decide what to do with it.

Spiny answered 27/10, 2013 at 8:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.