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
json ruby ruby-on-rails encoding unicode


source to share


No one has answered this question yet

See similar questions:

52
How to get rid of non-ascii characters in ruby
18
JSON encoding is mistakenly escaped (Rails 3, Ruby 1.9.2)
3
Rails UTF-8 answer

or similar:

1215
Why is Java code executed in comments with some Unicode characters?
1210
What symbols can be used for up / down triangle (arrow without stem) to display in HTML?
328
What is the difference between <%, <% =, <% # and -%> in ERB in Rails?
6
If you escape strings with HTML entities, can I safely skip encoding characters above Unicode 127 if I'm using UTF-8?
6
PHP Unicode Character Printing
2
How to stop Django's render_to_string template from trying to encode unicode to ascii?
1
Convert Unicode Escaped JSON Strings for JavaScript Objects
1
Rails render_to_string () function to convert HTML to characters
0
Rails to_json method: problems with Unicode characters
0
Rendering ERB HTML from Rails to Angular, Angular tries to interpret as JSON



All Articles
Loading...
X
Show
Funny
Dev
Pics