从Rails ERB模板中将HTML呈现为字符串,没有unicode字符

时间:2015-05-28 17:13:42

标签: ruby-on-rails ruby json unicode encoding

我在下面的代码段运行时没有错误,但是,它会在响应中返回unicode字符:

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

ERB使用动态内容发回HTML模板,但下面的HTML如下所示:

模板中的HTML:

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

以上代码段返回的HTML:

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

如何从控制器中将正常的UTF-8 HTML作为Rails中的字符串返回?

加成

问题在于将unicode字符串转换为json,例如:

"абв".to_json

变为

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

JSON::dump('АБВ')返回"\"АБВ\"",而传递给render json:的每一个unicode字符都会被转义。

如何避免在渲染json时转义unicode符号?

0 个答案:

没有答案