Last active
August 29, 2015 14:10
-
-
Save otobrglez/373da265fd655ff38585 to your computer and use it in GitHub Desktop.
Patching Rails UJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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