6

I would like to return html content via jbuilder:

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

But it's returns escaped html:

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

How can it return unescaped html?

Oded Harth
  • 4,367
  • 9
  • 35
  • 62

2 Answers2

1

You can use html_safe to disable the escape feature. Probably you run into some problems, because " won't be escaped as well and it's in use to define a value in JSON.

I think the best approach is to encode it somehow, for example with base64:

Robin
  • 8,162
  • 7
  • 56
  • 101
0

I believe the answer is to not retrieve the value via extract! I think this should do the trick.

json.array!(@articles) do |article|
  json.extract! article, :id, :title
  json.html_content article.html_content
end
MCB
  • 2,021
  • 1
  • 18
  • 32