I have a webapp using Ruby 1.9 and Rails 4. In my local VM (ubuntu), everything's ok. My DB and tables are using utf8_unicode_ci, and data are well saved into the tables and well printed on webapp pages. My problem is on my production server (EB on AWS). I'm using MySQL and my database was in swedish format, so I converted it to UTF8 with the following command:
# for my database
ALTER DATABASE xxx CHARACTER SET utf8 COLLATE utf8_unicode_ci;
# for each tables in the DB (didn’t affect a join table that I have but doesn’t matter)
ALTER TABLE xxx CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
But all data containing special characters from my rails form are saved with question marks, like caract?res sp?ciaux. But when I display the text on a page, special characters are rendered correctly.
Furthermore, I imported a list of languages in a table, so languages are correctly stored in the DB (e.g français, tchèque), but when I retrieve those values from the web pages, I got: français, tchèque
I tried 4 solutions that I found on internet (probably not in this order):
- I added config.encoding = "utf-8" in config/application.rb
- I added the encoding: utf8 in config/database.rb
- I added those 2 lines before the initialize!
config/environment.rb
Encoding::default_internal = Encoding::UTF_8
Encoding::default_external = Encoding::UTF_8`
- I added a filter in the app controller
app/controllers/application_controller.rb
before_filter :configure_charsets
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
def configure_charsets
response.headers["Content-Type"] = "text/html; charset=utf-8"
suppress(ActiveRecord::StatementInvalid) do
ActiveRecord::Base.connection.execute 'SET NAMES UTF8'
end
end
Now I have all this configuration but my problem is still the same. It is like these modifications didn't affect anything.