Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 754
Oded Harth Avatar asked Aug 29 '15 13:08

Oded Harth


1 Answers

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:

  • Base64 encode in ruby
  • Base64 decode in JavaScript
like image 88
Robin Avatar answered Sep 20 '22 01:09

Robin