Search code examples
ruby-on-railssql-serverutf-8character-encodingruby-1.9.2

Error: "incompatible character encodings: ASCII-8BIT and UTF-8" & "invalid byte sequence in UTF-8" when create a record RoR into SQL Server 2008 R2


I have build a web application with ruby on rails. With:

Ruby: ruby 1.9.2p290
Rails: rails (3.0.7)
Database: SQL Server 2008 R2

I've tested it in my local with input Chinese Traditional character into the input/textbox and create the data. it's working fine.
The data: 長壽
Result in Table SQL Server that using varchar(255) SQL_Latin1_General_CP1_CI_AS is: 長壽

With this, I also can view the text in UI.

But when I gave the application, ruby gem, and the preferences to my friends, they cannot input the Chinese Traditional character. it said, "incompatible character encodings: ASCII-8BIT and UTF-8" when click save button.
Even I restore my database into them, the web application cannot show the value properly it said "invalid byte sequence in UTF-8". Although, They also using the same settings as mine, like SQL Server, version of ruby, and path.
The difference is only I'm using English version of Windows 10 and my friends using Chinese Language of Windows 10.

I have try:
1. config.encoding = "utf-8" is already in application.rb file.
2. put # encoding: utf-8 on top of model and controller
3. Above Rails.application.initialize! line in environment.rb file, add following two lines:
--Encoding.default_external = Encoding::UTF_8
--Encoding.default_internal = Encoding::UTF_8
4. I also tried input encoding: utf8 in database.yml, the data can be saved, but it become question mark ?? in table database and when I want to view it, it also said "invalid byte sequence in UTF-8".


Any more information, i can give it to you if needed. :)


Solution

  • After research for a long time, I found that the windows encoding is the problem.
    I released that I can't input symbol such as "✓". It return the same error. And then I tried to trace what input in SQL profiler will transfer to database table, it same as "✓". But it should be encoded of "✓" (I had check the value also in my local).
    After research and googling for a long time, I found this post https://superuser.com/questions/336197/unicode-characters-suddenly-start-displaying-as-boxes-in-some-applications?newreg=8b561b87000d4e79b20cbea9c3d2eb07

    I tried it and restart the PC. and my application can save "✓" or Chinese character again. It saved become encoded string to database.
    (I'm still looking for the reason, how come the windows unicode is the root of this problem)