Rails escape_javascript creates invalid JSON by escaping single quotes

Posted by Arrel on Stack Overflow See other posts from Stack Overflow or by Arrel
Published on 2010-04-26T18:48:14Z Indexed on 2010/04/26 20:23 UTC
Read the original article Hit count: 380

Filed under:
|
|

The escape_javascript method in ActionView escapes the apostrophe ' as backslash apostrophe \', which gives errors when parsing as JSON.

For example, the message "I'm here" is valid JSON when printed as:

{"message": "I'm here"}

But, <%= escape_javascript("I'm here") %> outputs "I\'m here", resulting in invalid JSON:

{"message": "I\'m here"}

Is there a patch to fix this, or an alternate way to escape strings when printing to JSON?

© Stack Overflow or respective owner

Related posts about rails

Related posts about JSON