Chris Gaunt
Chris Gaunt

Reputation: 773

method: => :delete not working and :confirm option ignored

After returning to an old Rails project I found none of the destroy/delete links worked and clicking cancel on the confirmation popup would still submit the link.

Example of my code is:

<%= link_to 'Delete', admin_user_path(@user), :confirm => 'Are you sure?', :method => :delete %>

Upvotes: 8

Views: 7993

Answers (7)

John
John

Reputation: 1

Try to use

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script> 

before

<%= javascript_include_tag "application" %> 

in your layout, and also delete

//= require jquery

line in your application.js.

This was the case for me. No idea why it didn't work with original rails jquery.js file.

Upvotes: 0

Ameen
Ameen

Reputation: 84

Here is the quick solution - Just replace conform with data-confirm

<%= link_to 'Delete', admin_user_path(@user), 'data-confirm' => 'Are you sure?', :method => :delete %>

Upvotes: 0

Anderson
Anderson

Reputation: 46

This problem occurs if you are using jQuery, and if not, then look for something like that:

In my case, I was using:

javascript_include_tag :all %>

And it was not working, but when I put it like:

javascript_include_tag :defaults %>

It worked!

Upvotes: 3

Sooth
Sooth

Reputation: 3104

Most of the time, this happens because you are calling jQuery code before it's been loaded, or before the elements in the page exist.

To combat this use something like:

$(document).ready(function () {
  $('*[data-confirm]').click(function(){
    return confirm($(this).attr('data-confirm'));
  });
});

Upvotes: 0

Jimmy Dee
Jimmy Dee

Reputation: 1100

Here's a way to use jQuery to provide the same confirmation for all delete buttons:

;(function($){
  $(function(){
    $('input.destroy').click(function(){
      return confirm($(this).attr('data-confirm'));
    });
  });
})(jQuery);

You just have to be sure to add class="destroy" to each element, e.g.:

= button_to 'Delete', polygon, :method => :delete, :class => "destroy", :confirm => "Are you sure?"

Upvotes: 0

Mat Schaffer
Mat Schaffer

Reputation: 1704

In my case it seemed to be an issue with an old combination of prototype and rails.js. The answer from this questions led me to that: :confirm option in Rails being ignored while using Rails 3 and jQuery UJS

So I downloaded the latest rails.js from https://github.com/rails/prototype-ujs/raw/master/src/rails.js and the latest prototype from https://ajax.googleapis.com/ajax/libs/prototype/1.7.0.0/prototype.js.

Now everything seems to work as expected.

Upvotes: 0

mjnissim
mjnissim

Reputation: 3112

Chris, Hi. I had the same problem. I deleted Firebug and the problem persisted. I read from another user that he needed to restart Firefox, that didn't work either. Some said try Safari, that didn't work either.

In the end it was a novice thing:

I have helpers to insert an icon, like edit_icon, which return a nice image tag for the edit icon, etc.

One of these helper methods was the delete_icon method, where I wrote like this:

def delete_icon(object=nil) link_to image_tag('icons/delete.jpg', :width => 20, :border=>0), object, :confirm => 'Are you sure?', :method => :delete end

It was a (good) attempt at DRY, but better it would have been had I just had def delete_icon(object), and not object=nil, because it makes it clearer when I call it that I must pass it the object to destroy. Calling the same method with the object to be deleted worked for me.

In short: double check your helper methods if you're trying to DRY.

Upvotes: 0

Related Questions