如何修复ActionView :: Template :: Error(不兼容的字符编码:ASCII-8BIT和UTF-8)

时间:2016-01-10 05:37:44

标签: ruby-on-rails mongodb encoding utf-8

我收到以下错误[ActionView :: Template :: Error(不兼容的字符编码:ASCII-8BIT和UTF-8)]

这是日志...

    Completed 500 Internal Server Error in 318ms 
    Jan 09 23:29:19 burro app/web.1:  ActionView::Template::Error (incompatible character encodings: ASCII-8BIT and UTF-8): 
    Jan 09 23:29:19 burro app/web.1:       97:         <!-- <td><%= row.notes.gsub("\n", "<br>").html_safe %></td> --> 
    Jan 09 23:29:19 burro app/web.1:       98:       </tr> 
    Jan 09 23:29:19 burro app/web.1:       99:     <% end %> 
    Jan 09 23:29:19 burro app/web.1:      100:   </tbody> 
    Jan 09 23:29:19 burro app/web.1:      101: </table> 
    Jan 09 23:29:19 burro app/web.1:      102:  

这是代码:

<td><%= row.notes.force_encoding("utf-8") %></td>
    <!-- <td><%= row.notes.gsub("\n", "<br>").html_safe %></td> -->
  </tr>
<% end %>

我在正确的rb和erb文件中有所有这些吗?

<%# encoding: utf-8 %>


config.encoding = "utf-8"

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

@data.each do |sr|
    sr.notes = sr.notes.to_s.force_encoding("UTF-8")
  end

数据库中的数据是否已加密,因此在显示记录时,我无法对MongoDB数据运行查询以查看导致问题的特殊字符?

1 个答案:

答案 0 :(得分:0)

您可以尝试以下

config/application.rb应该包含

  config.encoding = "utf8"

同样在您的环境文件中

config/environments/development.rbconfig/environments/production.rb中的

应该

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

也可以在.rb文件中使用以下内容

# encoding: utf-8

It tells ruby to interpret the source of the file as utf-8, even if it doesn't contain any non-ascii characters

在该特定文件中

希望它能解决这个问题