【问题标题】:How to fix ActionView::Template::Error (incompatible character encodings: ASCII-8BIT and UTF-8)如何修复 ActionView::Template::Error(不兼容的字符编码:ASCII-8BIT 和 UTF-8)
【发布时间】:2016-04-14 15:14:12
【问题描述】:

我收到以下错误 [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 数据运行查询以查看导致问题的特殊字符?

【问题讨论】:

  • config/application.rb 应该包含config.encoding = "utf8"
  • Encoding.default_external = Encoding::UTF_8 Encoding.default_internal = Encoding::UTF_8 这两个应该与开发或生产.rb文件一起配置
  • 你的cmets我都做了,没有变化?

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


【解决方案1】:

你可以试试下面

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

在那个特定的文件中

希望它能解决这个问题

【讨论】:

  • 在我的 erb 文件中使用 # encoding: utf-8 会导致错误,它需要是
  • 在 erb 中不需要这个,但在 .rb 文件中需要它
  • 我的模型 .rb 文件中有 # encoding: utf-8,但控制器文件中没有。
  • 好的,你重启了吗?更改环境文件或应用程序后
  • 是的,我有。我也做了你上面所说的一切?我不知道我还没有更新应用程序,因为它运行良好一周。
猜你喜欢
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 2016-02-12
  • 2011-07-14
  • 1970-01-01
  • 2011-10-17
相关资源
最近更新 更多