简体   繁体   English

US-ASCII中的无效字节序列(Ruby 1.9 + rails 2.3.8 + mongodb + mongo_mapper)

[英]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.

相关问题 RVM,Ruby 1.9.2,Rails 2.3.8,Passenger和“US-ASCII中的无效字节序列” - RVM, Ruby 1.9.2, Rails 2.3.8, Passenger and “invalid byte sequence in US-ASCII” Rails + Ruby 1.9“ US-ASCII中无效的字节序” - Rails + Ruby 1.9 “invalid byte squence in US-ASCII” 使用Rails和Ruby 1.9的无效多字节字符(US-ASCII) - invalid multibyte char (US-ASCII) with Rails and Ruby 1.9 ArgumentError(US-ASCII中无效的字节序列):在ruby升级1.9.3中 - ArgumentError (invalid byte sequence in US-ASCII): in ruby upgrade 1.9.3 将Ruby升级到1.9.2后,Rails查看错误“US-ASCII中的无效字节序列”错误 - Rails view error “invalid byte sequence in US-ASCII” error after upgrading Ruby to 1.9.2 在US-ASCII问题中使用ruby 1.9.3的Rails 3.2无效字节序列 - Rails 3.2 with ruby 1.9.3 invalid byte sequence in US-ASCII issue 使用MongoDB和mongo_mapper对Rails 2.3.8进行身份验证 - Authentication for Rails 2.3.8 using MongoDB and mongo_mapper US-ASCII whith数组中的无效字节序列 - invalid byte sequence in US-ASCII whith array MembersController中的ArgumentError#登录US-ASCII中的无效字节序列 - ArgumentError in MembersController#login invalid byte sequence in US-ASCII US-ASCII(ArgumentError)中的字节序列无效,没有任何帮助 - invalid byte sequence in US-ASCII (ArgumentError), nothing helps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM