ghostrifle
ghostrifle

Reputation: 1019

alertify.js - rails custom confirm dialog

How can I customize my rails confirm dialog with alertify? I tried this code and regarding to the jquery_ujs it should work:

$.rails.confirm = function(msg){
  alertify.confirm(msg, function (e) {
    if (e) {
        return true;
    } else {
       return false;
    }
  });
};

example rails call:

<%= link_to system_communication_gallery_video_path(@gallery.id, video.id), method: :delete, remote: true, confirm: "Are you sure?" do %>

Upvotes: 1

Views: 1649

Answers (1)

Yoeran
Yoeran

Reputation: 71

I am fiddling with this override as well and stumbled on this question. This snippet does not work because the result of alertify.confirm does not get returned to $.rails.confirm.

Update:

After some searching I found a demo by rors.

Important note: In your HTML you will have to have two data-attributes: data-confirm and data-method. Where data-method can be a RESTful method (GET, POST, PUT, PATCH, DELETE).

Javascript:

$.rails.allowAction = function(element){
    if( undefined === element.attr('data-confirm') ){
        return true;
    }

    $.rails.showConfirmDialog(element);
    return false;
};

$.rails.confirmed = function(element){
    element.removeAttr('data-confirm');
    element.trigger('click.rails');
};

$.rails.showConfirmDialog = function(element){
    var msg = element.data('confirm');
    alertify.confirm(msg, function(e){
        if(e){
            $.rails.confirmed(element);
        }
    })
};

Haml:

= link_to 'Link title', root_path, {data: {confirm: 'Are you sure you want to go home?', method: 'get'}}

Upvotes: 2

Related Questions