Rails rails console 抛出 "\x9C" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)

bill997603 · November 05, 2019 · Last by lyb124553153 replied at November 07, 2019 · 4246 hits

这个问题是我在 ubuntu 环境中的 rails console 中输入了中文,这个有什么办法可以解决呢

是不是用了 pry-rails?

我也碰到了类似的问题,有没有高手讲一下这个 ASCII-8BIT 到底是怎么编码的? 如果直接 str.encode("utf-8","gbk") 提示上说

"\xE5" to UTF-8 in conversion from ASCII-8BIT to UTF-8 to GBK (Encoding::UndefinedConversionError)

而 str.encode("gbk","utf-8"),则提示

incompatible character encodings: UTF-8 and GBK (Encoding::CompatibilityError)

上面的 str 用 str.encoding 检查都是 ASCII-8BIT 的。

但是我在另外一个例子中,使用第一个 str.encode("utf-8","gbk") 是可以编解码的,如下图

Reply to Leslieranaa

试试 force_encoding('utf-8')

L10 试试 f.set_encoding("ASCII-8BIT:utf-8")

shell 运行 echo $LANG 试试,先确认系统编码是不是 utf-8 ,然后再打开终端设置,看一下终端的编码是不是 utf-8 .

Reply to liuminhan

是的 用了 pry-rails

Reply to cxh116

shell 和 系统都是 utf-8

把那个 pry-rails 去掉了试试吧

Gemfile 里是否有 rb-readline?有的话考虑去掉

之前遇到过类似的问题,后来发现是环境变量的问题。在你的运行环境下看看环境变量 LC_CTYPE

我遇到过这种问题 ruby 生成文件的时候出现这个 用 Encoding.default_internal = nil

解决的

bill997603 closed this topic. 10 Nov 14:02
You need to Sign in before reply, if you don't have an account, please Sign up first.