3

I have the snippet below that runs without errors, however, it returns unicode characters in the response:

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

The ERB sends back the HTML template with the dynamic content, but the 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 the controller as a string in Rails?


Addition

The problem lies in converting unicode string to json e.g.:

"абв".to_json

becomes

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

Whereas JSON::dump('АБВ') returns "\"АБВ\"", every single one of unicode characters passed to render json: becomes escaped.

How to avoid escaping unicode symbols in rendering json?

Nick Roz
  • 3,918
  • 2
  • 36
  • 57
Noah
  • 437
  • 1
  • 4
  • 11
  • Why do you render it as json? – Sunil D. May 28 '15 at 17:23
  • Because there's other data rendered back to the client in addition to the HTML. – Noah May 28 '15 at 17:25
  • I just didn't include it in the question as it's irrelevant to the problem. – Noah May 28 '15 at 17:26
  • It works with render template, but I need to send back extra data to the client outside of just the template. – Noah May 28 '15 at 17:33
  • Seems that problem lies in [encoding non ascii character in ruby](http://stackoverflow.com/questions/1268289/how-to-get-rid-of-non-ascii-characters-in-ruby). There is another good answer to [json encoding question](http://stackoverflow.com/questions/5123993/json-encoding-wrongly-escaped-rails-3-ruby-1-9-2) – Nick Roz Jun 05 '15 at 12:42
  • The question should be about converting unicode string to json. May I edit your question? – Nick Roz Jun 05 '15 at 12:55
  • Also (rails uft 8 response)[http://stackoverflow.com/questions/10342552/rails-utf-8-response] question might be useful – Nick Roz Jun 05 '15 at 13:36

0 Answers0