Render HTML as string from Rails ERB template, no Unicode characters

I have a snippet below that works without error, however it returns Unicode characters in the response:

if params[:template] == 'Application Acknowledgement'
  render json: { :template => render_to_string(:template => "template.erb") }
end

      

ERB sends HTML template with dynamic content, but below HTML looks like this:

HTML in template:

<!doctype html>
<html>
  <head>
  </head>
</html>

      

HTML returned by the above snippet:

\u003c!doctypehtml\u003e\n\u003chtml\u003e\n\t\u003chead\u003e

      

How can I return normal UTF-8 HTML from a controller as a string in Rails?


Adding

The problem is converting the unicode string to json, for example:

"".to_json

      

becomes

"\"\\u0410\\u0411\\u0412\""

      

While it JSON::dump('')

returns "\"\""

, each of the Unicode characters passed in render json:

becomes escaped.

How to avoid escaping unicode characters when rendering json?

+3


source to share





All Articles