Monday, August 6, 2007

Rails Tip: TinyMCE, Unicode, and encodeURIComponent

Here's something I learned today: when sending input from a TinyMCE editor back to your Rails application, Prototype's encode() function will fail you if your TinyMCE'd textarea contains Unicode characters (in my case, they were "curly" quotes, probably generated by Word). Use Javascript's encodeURIComponent() instead. In other words, where you might have written something like this:

function mailSave() {
  <%= remote_function(:url => { :action => 'save' },
    :before => 'tinyMCE.triggerSave(true,true)',
    :with => "'subject=' + encode($('descr').value)
      + encode($('tiny_mce').value)") %>;
}


Instead, use this:

function mailSave() {
  <%= remote_function(:url => { :action => 'save' },
    :before => 'tinyMCE.triggerSave(true,true)',
    :with => "'subject=' + encodeURIComponent($('descr').value)
      + encode
URIComponent($('tiny_mce').value)") %>;
}


One of our bigger clients needed to send a mailing and was going to switch services if this bug wasn't fixed by 5:00 this afternoon, so plenty of bullets were sweat while I figured this out. Many thanks to Firebug, as usual.

No comments: