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

半腔热情 提交于 2019-11-30 12:49:59

The problem here is with the "=" string. As it's considered unsafe, it taints the other string.

You can probably do either:

raw("=" + @campaign.to_json)

or

"= #{@campaign.to_json}".html_safe

which are roughly the same.

Since ActiveSupport 2.3.3 you have been able to do .as_json

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.

Try this with utility method

var campaignData<%=h " =#{raw @campaign.to_json}" if @campaign %>;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!