[英]invalid byte sequence in US-ASCII (Ruby 1.9 + rails 2.3.8 + mongodb + mongo_mapper)
My setup is: 我的设置是:
linux + Ruby 1.9 + rails 2.3.8 + mongodb + mongo_mapper linux + Ruby 1.9 + rails 2.3.8 + mongodb + mongo_mapper
I followed http://railscasts.com/episodes/194-mongodb-and-mongomapper , that everything is OK first. 我跟着http://railscasts.com/episodes/194-mongodb-and-mongomapper ,一切都好。 I can insert English strings successfully, but when I insert some Chinese strings, it inserted, but can't be displayed.
我可以成功插入英文字符串,但是当我插入一些中文字符串时,它会插入,但无法显示。
The web page shows an exception: 该网页显示一个例外:
invalid byte sequence in US-ASCII
I use mongo
command to see the data in mongodb, and it's correct. 我使用
mongo
命令查看mongodb中的数据,这是正确的。 But I don't know why rails can't display them. 但我不知道为什么rails无法显示它们。
thanks in advance 提前致谢
Please add the following lines in your environment.rb . 请在您的environment.rb中添加以下行。
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
The problem will be solved. 问题将得到解决。
You may correct this error writing this 您可以更正此错误来写这个
export LC_ALL="en_US.UTF-8"
to your ~/.profile
, ~/.bash_profile
or similar. 你的
~/.profile
, ~/.bash_profile
或类似的。 It works like a charm. 它就像一个魅力。
I had this error with a Rack application. 我在Rack应用程序中遇到此错误。
Adding 添加
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
to config.ru
resolved it for me. to
config.ru
为我解决了它。
cd $HOME
vi .bashrc
add locale conf below: 在下面添加语言环境:
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
export LC_CTYPE=en_US.UTF-8
save and run: source ~/.bashrc 保存并运行:source~ / .bashrc
Everything is okay~ 一切都还好〜
check your locale
settings of your OS. 检查操作系统的
locale
设置。 You need to set up en_US
.UTF-8` locale environment variable otherwise you may face this problem. 您需要设置
en_US
.UTF-8`语言环境变量,否则您可能会遇到此问题。
If you operating system is Ubuntu
then you can check your locale
by typing the following command. 如果您的操作系统是
Ubuntu
那么您可以通过键入以下命令来检查您的locale
。
sudo locale
and then you can re-configure your locale by typing the following command 然后,您可以通过键入以下命令重新配置您的区域设置
sudo locale-gen en_US.UTF-8
It may help you. 它可能会帮助你。 Thanks.
谢谢。
For me the problem was that I was using an older version of ruby. 对我来说问题是我使用的是旧版本的ruby。
rvm use 2.0
did the trick. rvm use 2.0
做了伎俩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.