Print valid, non-escaped JSON in a view with Rails

前端 未结 4 594
春和景丽
春和景丽 2021-01-02 00:29

I\'ve tried everything. Every combination of the helpers raw, html_safe to_json including some attempts with ::JSON.encode

4条回答
  •  春和景丽
    2021-01-02 01:02

    Did you try escape_javascript?

    Here is an example from the *.haml file, which I just added to test my answer.

    :javascript
      var foo=$.parseJSON("#{j @albums.to_json}")
    

    Where j is an short alias for escape_javascript.

提交回复
热议问题