Skip to content

Instantly share code, notes, and snippets.

@dstrelau
Created July 29, 2008 15:14
Show Gist options
  • Save dstrelau/3103 to your computer and use it in GitHub Desktop.
Save dstrelau/3103 to your computer and use it in GitHub Desktop.
var ConfirmedDeleteButton = Class.create({
initialize:function(anchor) {
this.anchor = anchor;
if (this.anchor.readAttribute('alt').blank()) {
this.confirmationText = 'Are you sure?';
} else {
this.confirmationText = this.anchor.readAttribute('alt');
}
this.anchor.observe('click', this.deleteFormSubmit.bindAsEventListener(this));
},
deleteFormSubmit: function(ev) {
ev.stop();
if (confirm(this.confirmationText)) {
var f = document.createElement('form');
f.style.display = 'none';
this.anchor.parentNode.appendChild(f);
f.method = 'POST';
f.action = this.anchor.href.gsub(/\/delete$/, '');
var m = document.createElement('input');
m.setAttribute('type', 'hidden');
m.setAttribute('name', '_method');
m.setAttribute('value', 'delete');
f.appendChild(m);
var t = document.createElement('input');
t.setAttribute('type', 'hidden');
t.setAttribute('name', 'authenticity_token');
t.setAttribute('value', authenticityToken);
f.appendChild(t);
f.submit();
}
}
});
document.observe('dom:loaded',function(){
$$('a.confirmed-delete').each(function(a) {
new ConfirmedDeleteButton(a);
});
});
map.resources :post, :member => {:delete => :get}
<%= link_to 'Delete', delete_post_path(@post),
:class => "confirmed-delete",
:alt => 'Are you sure you want to permanently delete this post?' %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment