jonepatr
jonepatr

Reputation: 7809

Bah! I keep getting this encoding error in rails with ruby 1.9.2

I use rails 3.1.1 with ruby-1.9.2-p290 on a debian system through rvm. I use swedish letters in my views and in the mysql database. I keep getting

incompatible character encodings: UTF-8 and ASCII-8BIT

when I visit a page that get's stuff from the db.

in my database.yml i have this:

staging:
  adapter: mysql
  database:something
  encoding: utf8
  username: something
  password: something
  host: localhost    

production:
  adapter: mysql
  database:something
  encoding: utf8
  username: something
  password: something
  host: localhost

my config.ru:

Encoding.default_external = "UTF-8"
require ::File.expand_path('../config/environment',  __FILE__)
run Someappname::Application

my environment.rb:

# Load the rails application
require File.expand_path('../application', __FILE__)
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
Someappname::Application.initialize!

my config/initializers/encoding.rb:

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

I have tried with all combinations of "Encoding.default_xxxxxx = Encoding::UTF_8" and without them to

Every controller, helper and mailer starts with:

# encoding: UTF-8

In mysql :

mysql> show variables like 'char%';

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | latin1                     |
| character_set_connection | latin1                     |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | latin1                     |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

And the output from:

mysql> show variables like 'colla%';

+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | latin1_swedish_ci |
| collation_database   | utf8_general_ci   |
| collation_server     | latin1_swedish_ci |

Any idea of what I can do? I'm strating to get really tired of this problem!

Upvotes: 2

Views: 542

Answers (1)

jonepatr
jonepatr

Reputation: 7809

Okey, seems that I managed to solve the problem! The problem was that I was using mysql and mysql2 at the same time. So to solve it, I had to delete mysql and grab the mysql2 "0.3.7" or else devise was throwing strange errors at me. And then I had to change the adapter in database.yml to adapter: mysql2

Hope this helps someone having the same problem!

Upvotes: 4

Related Questions