Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[PATCH] Fix generated html for remote_function so that ampersands can be used in passed in options

#5138

The following code before this patch produced the following html...

<%=
tmp = "'object[name]=' + result + '&object[attr1]=' + attr1 + '&object[attr2]=' + attr2"
remote_function :url => objects_path, :method => :post,
  :with => tmp.html_safe
%>
new Ajax.Request('/objects', {
  asynchronous:true, evalScripts:true, method:'post',
  parameters:
    'object[name]=' + result + '&amp;object[attr1]=' + 
    attr1 + '&amp;object[attr2]=' + attr2 + '&amp;authenticity_token=' +
    encodeURIComponent('YAq5IJ6Cbfcy3Faebh3gJFKXiba9L87RB0i7m2fEmpg=')
})

Note the escaping of &'s to &amp;

Without the patch the entire "Ajax..." string is unsafe due to concatenations of various options and strings even if we pass our parameter string as html_safe; an html_safe string concatenated with an unsafe string results in an unsafe string. Since our end result will always be an unsafe string the resulting javascript is escaped causing the invalid results to be produced.

With the patch, because the entire javascript string is made html_safe, we don't need to specify html_safe for our own string, so the following code...

<%=
remote_function :url => objects_path, :method => :post,
  :with => "'object[name]=' + result + '&object[attr1]=' + attr1 + '&object[attr]=' + attr2"
%>

now results in the correct output of...

new Ajax.Request('/objects', {
  asynchronous:true, evalScripts:true, method:'post',
  parameters:
    'object[name]=' + result + '&object[attr1]=' + attr1 + 
    '&object[attr2]=' + attr2 + '&authenticity_token=' +
    encodeURIComponent('YAq5IJ6Cbfcy3Faebh3gJFKXiba9L87RB0i7m2fEmpg=')
})

Reported by Andrew Kaspick · July 17th, 2010 @ 09:44 AM

State: resolved
Milestone: 3.x
Assigned to: Santiago Pastorino Santiago Pastorino
Importance: Low

Activity

  1. Mislav
    Mislav

    Tests are definitely in order. The fact that there were no tests previously is probably what lead to this breakage.

    July 17th, 2010 @ 12:58 PM

  2. Andrew Kaspick
    Andrew Kaspick

    Updated patch with tests.

    July 17th, 2010 @ 09:05 PM

  3. Andrew Kaspick
    Andrew Kaspick

    Updated patch again with a bit more detail to test the generated string as well.

    July 17th, 2010 @ 11:25 PM

  4. Andrew Kaspick
    Andrew Kaspick
    • Tag changed from remote_function patch to remote_function patch, tests

    July 18th, 2010 @ 10:23 PM

  5. Andrew Kaspick
    Andrew Kaspick
    • Tag changed from remote_function patch, tests to patch, remote_function, tests

    July 18th, 2010 @ 10:24 PM

  6. Neeraj Singh
    Neeraj Singh
    • Importance changed from to Low

    @Andrew if you could edit the ticket so that code is not all in one line then that would be nice.

    July 21st, 2010 @ 04:17 AM

  7. Andrew Kaspick
    Andrew Kaspick

    Done... broke the code lines up a bit more.

    July 21st, 2010 @ 04:29 AM

  8. Neeraj Singh
    Neeraj Singh
    • Milestone set to 3.x
    • State changed from new to open
    • Tag changed from patch, remote_function, tests to rails 3, patch, remote_function, tests
    • Assigned user set to Santiago Pastorino

    patch looks good to me. But I haven't worked with html_safe much. Assigning it to Santiago.

    July 21st, 2010 @ 04:50 AM

  9. Andrew Kaspick
    Andrew Kaspick

    This patch can be closed now as it has now been committed. Thanks guys.

    July 22nd, 2010 @ 01:40 AM

  10. Santiago Pastorino