Freewind
Freewind

Reputation: 198358

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

I followed http://railscasts.com/episodes/194-mongodb-and-mongomapper, that everything is OK first. 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. But I don't know why rails can't display them.

thanks in advance

Upvotes: 14

Views: 7483

Answers (6)

rounak
rounak

Reputation: 9397

For me the problem was that I was using an older version of ruby. rvm use 2.0 did the trick.

Upvotes: 0

fantaxy025025
fantaxy025025

Reputation: 809

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

Everything is okay~

Upvotes: 1

Carlos Agarie
Carlos Agarie

Reputation: 4002

You may correct this error writing this

export LC_ALL="en_US.UTF-8"

to your ~/.profile, ~/.bash_profile or similar. It works like a charm.

Upvotes: 18

Shaharia Azam
Shaharia Azam

Reputation: 1976

check your locale settings of your OS. You need to set up en_US.UTF-8` locale environment variable otherwise you may face this problem.

If you operating system is Ubuntu then you can check your locale by typing the following command.

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.

Upvotes: 0

Ramanavel
Ramanavel

Reputation: 408

Please add the following lines in your environment.rb .

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

The problem will be solved.

Upvotes: 19

Mullins
Mullins

Reputation: 2364

I had this error with a Rack application.

Adding

   Encoding.default_external = Encoding::UTF_8

   Encoding.default_internal = Encoding::UTF_8

to config.ru resolved it for me.

Upvotes: 5

Related Questions