Search code examples
javascriptjqueryruby-on-railssubmitdestroy

Submit button confirm for delete not working in rails 3


I'm still new to rails and trying to understand something when deleting multiple items. When I click submit, it works and I can delete multiple items, however, it deletes regardless of my confirmation response.

I am using javascript to detect onclick event of my submit button, my code is:

$('.delete').on('click', function(){
    checked = countChecked();
    if (checked > 1) {
        confirm('Are you sure you want to delete these ' + checked + ' entries?');
    } else {
        confirm('Are you sure you want to delete this entry?');
    };
});

My submit button is:

<%= submit_tag "Delete Selected", { :class => 'delete'} %>

Why is the confirmation not doing the right thing? It deletes even if I press cancel...


Solution

  • Try this

    $('.delete').on('click', function(){
        checked = countChecked();
        if (checked > 1) {
           return confirm('Are you sure you want to delete these ' + checked + ' entries?');
        } else {
           return confirm('Are you sure you want to delete this entry?');
        };
    });