Skip to content

Instantly share code, notes, and snippets.

@micc83
Created September 25, 2014 14:55
Show Gist options
  • Save micc83/4a33c550f37972f84221 to your computer and use it in GitHub Desktop.
Save micc83/4a33c550f37972f84221 to your computer and use it in GitHub Desktop.
Ruby like delete links on laravel
/**
* Restfulizer
*
* Restfulize any hiperlink that contains a data-method attribute by
* creating a mini form with the specified method and adding a trigger
* within the link.
* Requires jQuery!
*
* Ex:
* <a href="post/1" data-method="delete">destroy</a>
* // Will trigger the route Route::delete('post/(:id)')
*
*/
$(function () {
var makeRequest = function (target) {
var html = '<form action="'+target+'" method="POST">';
html += '<input type="hidden" name="_method" value="DELETE">';
html += '</form>';
$(html).appendTo('body').submit();
};
$('a').on('click', function (e) {
var a = $(this);
if(a.data('method') === 'delete') {
e.preventDefault();
confirm("Are you sure?") && makeRequest(a.attr('href'));
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment