43

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.

Prabhakaran
  • 3,900
  • 15
  • 46
  • 113

10 Answers10

66

Try this

<%= link_to 'Delete',url_for(action: :delete,id: @user.id),method: :delete, data: {confirm: "Are you sure?"} %>
MZaragoza
  • 10,108
  • 9
  • 71
  • 116
userxyz
  • 1,100
  • 8
  • 14
  • 1
    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? – August Miller Jun 29 '14 at 01:10
  • 1
    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" – TWA Dec 13 '14 at 05:25
  • Did they change this to data-confirm in Rails 4? – Weston Ganger Sep 11 '15 at 22:33
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.

Jai Chauhan
  • 4,035
  • 3
  • 36
  • 62
Torsten
  • 425
  • 4
  • 7
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?'} %>
installero
  • 9,096
  • 3
  • 39
  • 43
4
link_to('Delete', {controller: :controller_name, id: id, action: :action_name}, confirm: "Are you sure you want to delete this?", method: :delete)
LHH
  • 3,233
  • 1
  • 20
  • 27
2

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 %>
Guido Tarsia
  • 1,962
  • 4
  • 27
  • 45
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

Marek Takac
  • 3,002
  • 25
  • 30
  • 1
    Tried but no working when i inspect i can see `data-confirm="Are you sure?"` and `data-method="delete"` – Prabhakaran Oct 25 '13 at 11:16
  • 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' %>. – Marek Takac Oct 25 '13 at 11:27
1

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>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
sivamani
  • 465
  • 7
  • 10
1

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?" }} %>
equivalent8
  • 13,754
  • 8
  • 81
  • 109
0

Check the following link:

<%= link_to 'Delete',url_for(action: :delete,id: @user.id), confirm: "Are you sure?" %>
Ganesh Kunwar
  • 2,643
  • 2
  • 20
  • 36
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.

kristinalim
  • 3,459
  • 18
  • 27