Hi.
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 3.6.23.1; 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
# app/views/friends/show.haml
- @friends.each do |f| %span= f.name
#=> Чендлер Фиби Росс Моника Рэйчел Джоуи
The problem appears when we try to include UTF text in the template.
# app/views/friends/show.haml
%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.
# app/views/friends/show.haml
%h1= 'Друзья'.encoding #=> UTF-8 %span= @friends[0].name.encoding #=> ASCII-8BIT
The solution is to use the force_encoding method to level the differences between the encodings.
# app/views/friends/show.haml
%h1 Друзья %span= @friends[0].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?
Thanks.