Skip to content

Instantly share code, notes, and snippets.

@otobrglez
Last active August 29, 2015 14:10
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 otobrglez/373da265fd655ff38585 to your computer and use it in GitHub Desktop.
Save otobrglez/373da265fd655ff38585 to your computer and use it in GitHub Desktop.
Patching Rails UJS
/*
When tinymce f* u over!
*/
jQuery.rails.href = function(element) {
var href = element.attr('href');
if(typeof(href) == "string") {
return href;
}
var e = element[0];
if(e !== null && typeof(e['href']) != "undefined") {
return e['href'];
}
return null;
};
jQuery.rails.handleMethod = function(link) {
var href = jQuery.rails.href(link),
method = link.data('method');
target = (typeof(link.attr('target')) == 'object' ? link[0]['target'] : link.attr('target')),
csrfToken = jQuery('meta[name=csrf-token]').attr('content'),
csrfParam = jQuery('meta[name=csrf-param]').attr('content'),
form = jQuery('<form method="post" action="' + href + '"></form>'),
metadataInput = '<input name="_method" value="' + method + '" type="hidden" />';
if (csrfParam !== undefined && csrfToken !== undefined) {
metadataInput += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />';
}
if (target) { form.attr('target', target); }
form.hide().append(metadataInput).appendTo('body');
form.submit();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment