Rails escape_javascript creates invalid JSON by escaping single quotes

前端 未结 5 1350
悲&欢浪女
悲&欢浪女 2021-02-07 05:54

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

For

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-07 06:34

    Already there is an issue in github/rails https://github.com/rails/rails/issues/8844

    Fix to mark the string as html_safe

    <%= escape_javascript("I'm here".html_safe) %>
    

    or even better you can sanitize the string

    <%= sanitize(escape_javascript("I'm here")) %>
    <%= escape_javascript(sanitize("I'm here")) %>
    

提交回复
热议问题