4

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):

  1. I added config.encoding = "utf-8" in config/application.rb
  2. I added the encoding: utf8 in config/database.rb
  3. I added those 2 lines before the initialize!

config/environment.rb

Encoding::default_internal = Encoding::UTF_8
Encoding::default_external = Encoding::UTF_8`
  1. 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.

3 Answers 3

1

Try creating database with utf params:

CREATE DATABASE `mydb` CHARACTER SET utf8 COLLATE utf8_general_ci;
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I tried to recreate a new database using your command line, and I imported the db structure (with utf8) from my previous db. When I add data with special characters from my rails app, I still have the question marks in the DB so it didn't change anything (but the display is still good on the web app).
Did anything changed? display is now good on the web, but before it wasn't? only when you look inside DB you get question marks? Or nothing changed, im not sure i understood you right.
Sorry! I wanted to say that nothing changed. Before it was ok on the web, but with questions marks in the DB. And now, I have the same behavior. Weird, right?
1

The connection between RoR and MySQL needs to be in utf8. Do the RoR equivalent of SET NAMES utf8.

1 Comment

I did it in the application_controller.rb :)
0

The problem was linked to my terminal. I imported the language list through it and I think this was the source of my encoding problem.

I downloaded Sequel Pro to manage my database and all of my data are not corrupted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.