简体   繁体   中英

Rails and Utf-8 encoding

I know this is the most common problem in the history of programming but I reallu believe I have tried it all now but I still get: incompatible character encodings: UTF-8 and ASCII-8BIT in my pages#index.html file.

I am running Ruby 1.9.2 and Rails 3.1.1.

This is what I have verified / tested / set:

  • In database.yml, set encoding: utf8

    development: adapter: mysql encoding: utf8 reconnect: false database: kopa_development pool: 5 username: root password: host: localhost

  • Checked with Notepad++ that the textfile(s) in term is encoded to utf-8

  • Checked with mysql query browser that the database is set to utf-8, so are the columns
  • I have tried with # coding: utf-8 in the controller related to the view I am trying to look at.
  • I have put SET NAMES utf8 in mysql
  • I have tried Encoding.default_internal = Encoding::UTF_8 in environment.rb as well as Encoding.default_external = Encoding::UTF_8
  • I have config.encoding = "utf-8" in application.rb
  • I have Googled
  • I have read http://www.joelonsoftware.com/articles/Unicode.html
  • I have tried both Notepad++ and RubyMine
  • Adding .force_encoding works but is very inconvenient to do

A funny thing is that if I set the index.html.erb to ANSI it will complain that application.html.erb does not have the correct encoding (rather than index.html.erb). I don't know what this means but it makes me think that there is something fishy with the editor.

I remember when using e text editor that there were two things I could set (with reference to encoding) the encoding and something else. That "else" I don't remember what it was though and I don't have e anymore.

Regarding the error-message, do I understand it correctly that the first mentioned means the database encoding and the second the Rails(?) encoding? I have seen the opposite error message as well (ASCII first and UTF-8 after).

Edit: I Googled some more this morning and found there is something wrong with the mysql-gem. The articles were a bit old and I tried the solutions there (such as trying mysql2 & ruby-mysql) with no better luck.

I am all out of ideas. How about you?

I couldn't fix this problem. I "solved" it by purchasing a Mac and continue to develop my Rails apps on it instead...

Encoding - me: 1-0

I was able to overcome that error but unfortunately I got the same error in yield statement (application.html.erb). After an half hour I gave up.

I saved some erb files from utf-8 with BOM to utf-8 without BOM. Mixing encoding is not good and I think it's the reason for your errors.

I also found files with different line ending: lf or crlf.

If you are developing in Windows, I recommend you the installation of virtual box to setup a linux virtual machine. You can access file system through samba, it works pretty good for me.

Also, configure your text editor for using utf-8 without BOM and lf end of line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM