How to safely embed JSON with [removed] in HTML document?

后端 未结 4 1962
陌清茗
陌清茗 2020-12-30 01:16

In a Rails 3.1 app, how can I safely embed some JSON data into an HTML document?

Suppose I have this in a controller action:

@tags = [
    {name:\"ta         


        
4条回答
  •  猫巷女王i
    2020-12-30 01:23

    Your code using just @tags.to_json works in rails3, if you enable it with:

       ActiveSupport.escape_html_entities_in_json = true
    

    Otherwise, your other option is this:

       var tags_list = <%= raw @tags.to_json.gsub(";
    

    This saves the client having to parse the whole thing through $

提交回复
热议问题