RailsCasts Pro episodes are now free!

Learn more or hide this

yanhuanle's Profile

GitHub User: yanhuanle

Comments by

Avatar

jQuery version:

Javascript
function confirm_destroy(event, element, options) {
    if (confirm("Are you sure?")) {
        var f = $("<form>", {
            display: 'none',
            method: 'POST',
            action: options.action
        });
        $(element).parent().append(f);

        var s = $("<input>", {
            type: 'hidden',
            name: '_method',
            value: 'delete'
        }).appendTo(f);

        if (options.token_name && options.token_value) {
            var t = $("<input>", {
                type: 'hidden',
                name: options.token_name,
                value: options.token_value
            }).appendTo(f);
        }

        f.submit();
    }
    event.preventDefault();
    return false;
}
Avatar

Thanks for your code, it works great! Except one place need to change, the last 3rd line should be " event.preventDefault(); ", otherwise it causes an "no method stop()" error in the console.