【问题标题】:return unescaped html with jbuilder使用 jbuilder 返回未转义的 html
【发布时间】:2015-11-24 00:59:32
【问题描述】:

我想通过 jbuilder 返回 html 内容:

json.array!(@articles) do |article|
  json.extract! article, :id, :title, :html_content
end

但它返回的是转义的 html:

{
    "id": 2,
    "title": "",
    "html_content": "\u003cp\u003e\u003cimg alt=\"\" src=\"#\" /\u003e\u003c/p\u003e\r\n"
}

它如何返回未转义的html?

【问题讨论】:

    标签: html ruby-on-rails ruby json jbuilder


    【解决方案1】:

    您可以使用html_safe 禁用转义功能。可能你遇到了一些问题,因为" 也不会被转义,而是用于在 JSON 中定义一个值。

    我认为最好的方法是以某种方式对其进行编码,例如使用base64

    【讨论】:

      【解决方案2】:

      我相信答案是不通过extract! 检索值我认为这应该可以解决问题。

      json.array!(@articles) do |article|
        json.extract! article, :id, :title
        json.html_content article.html_content
      end
      

      【讨论】:

        猜你喜欢
        • 2018-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-14
        • 1970-01-01
        • 2016-02-19
        • 2011-09-16
        • 1970-01-01
        相关资源
        最近更新 更多