Skip to content

Instantly share code, notes, and snippets.

@jlindley
Created July 22, 2008 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlindley/608 to your computer and use it in GitHub Desktop.
Save jlindley/608 to your computer and use it in GitHub Desktop.
Merb/jQuery: add delete method to destroy links.
// Make merb resource route url link_to's with class deletable fake delete HTTP method.
jQuery.fn.deletable = function() {
return this.each(function() {
var delete_link = $(this);
var hidden_form = document.createElement('form');
hidden_form.style.display = 'none';
delete_link.append(hidden_form);
hidden_form.method = 'POST';
hidden_form.action = delete_link.attr('href');
var method_input = document.createElement('input');
method_input.setAttribute('type', 'hidden');
method_input.setAttribute('name', '_method');
method_input.setAttribute('value', 'delete');
hidden_form.appendChild(method_input);
this.form = hidden_form;
delete_link.click(function() {
if (confirm("Are you sure?")){
this.form.submit();
}
return false;
});
});
};
$(document).ready(function() {
$('a.deletable').deletable();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment