Oded Harth
Oded Harth

Reputation: 4406

return unescaped html with jbuilder

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?

Upvotes: 6

Views: 1299

Answers (2)

MCB
MCB

Reputation: 2073

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

Upvotes: 0

Robin
Robin

Reputation: 8498

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:

Upvotes: 1

Related Questions