I just want to know have anybody solved the encoding problem in Rails' views by this time?
If you aren't in the know here's the deal.
Debian GNU/Linux 5.0.4; SQLite 126.96.36.199; Ruby 1.9.2; RoR 2.3.8; sqlite3-ruby 1.2.5.
$ sqlite3 -header -line db/development.sqlite3 'select * from friends;'
id = 1 name = Чендлер
id = 2 name = Фиби
id = 3 name = Росс
id = 4 name = Моника
id = 5 name = Рэйчел
id = 6 name = Джоуи
# app/controllers/friends_controller.rb # encoding: utf-8
class FriendsController < ApplicationController def show @friends = Friend.find(:all) end end
- @friends.each do |f| %span= f.name
#=> Чендлер Фиби Росс Моника Рэйчел Джоуи
The problem appears when we try to include UTF text in the template.
%h1 Друзья - @friends.each do |f| %span= f.name
#=> ActionView::TemplateError (incompatible character encodings: UTF-8 and ASCII-8BIT)
The source of the problem is that the data which was extracted from the SQLite3 DB are in the ASCII encoding but the title in my example is in UTF.
%h1= 'Друзья'.encoding #=> UTF-8 %span= @friends.name.encoding #=> ASCII-8BIT
The solution is to use the force_encoding method to level the differences between the encodings.
%h1 Друзья %span= @friends.name.force_encoding('utf-8')
#=> Друзья #=> Чендлер
However this is just a hack - it's tiresome to add force_encoding every time. So, here's a question: have there been suggested any robust solutions to change the situation with encoding incompatibilities since the problem appears first time?