invalid byte sequence utf-8 error in rails 2.3 and ruby 1.9
-
29-04-2021 - |
Pregunta
I've migrated my app from Rails 2.3.8 / Ruby 1.8.7 to 2.3.14 / Ruby 1.9.2 - it hasn't been easy, but basically works. An issue I keep having is that in the occasional Haml template I'll get the following error:
ActionView::TemplateError (invalid byte sequence in UTF-8) on line #....
This is an encoding issue, and I've seen some suggestions on how to deal with it (using Iconv.conv
), but the things I tried didn't seem to work. How do I resolve this on the application level (not having to patch every template)? Thanks.
EDIT: Some stack trace
ActionView::TemplateError (invalid byte sequence in UTF-8) on line #28 of app/views/users/_user.haml:
27: - if user.valid?
28: - image_onclick = remote_function( :url => { :action => :toggle_image, :id => user.id } )
vendor/rails/activesupport/lib/active_support/core_ext/object/blank.rb:68:in `=~'
vendor/rails/activesupport/lib/active_support/core_ext/object/blank.rb:68:in `!~'
vendor/rails/activesupport/lib/active_support/core_ext/object/blank.rb:68:in `blank?'
vendor/rails/activerecord/lib/active_record/validations.rb:478:in `block (2 levels) in validates_each'
vendor/rails/activerecord/lib/active_record/validations.rb:476:in `each'
vendor/rails/activerecord/lib/active_record/validations.rb:476:in `block in validates_each'
... etc
The database tables' CHARSET
is encoded in UTF-8
. I didn't assign any specific encoding to my templates, although I set
Encoding.default_internal = 'utf-8'
Encoding.default_external = 'utf-8'
in environment.rb
Solución
This seems to be an issue with the MySQL adapter I've been using - the old ruby-mysql
gem. I've upgraded to mysql2
and that seemed to have solved the issue.