Encoding problems in rails on ruby 1.9.1
I just had this as well so I think its worth having the correct answer.
The 2.8.1 MySql gem is not utf-8 friendly, so it sometimes will return UTF strings and lie to Rails, telling it that they are ASCII when in fact they are UTF-8. This makes things explode.
So: you can either monkey patch or get a compatible MySql gem. See: http://gnuu.org/2009/11/06/ruby19-rails-mysql-utf8/
Set UTF-8 as default for Ruby 1.9.3
To change the source encoding (i.e. the encoding your actual written source code is in), you have to use the magic comment currently:
# encoding: utf-8
It is not enough to either set the internal encoding (the encoding of the internal string representation after conversion) or the external encoding (the assumed encoding of read files). You actually have to set the magic encoding comment on top of files to set the source encoding.In ChiliProject we have a rake task which sets the correct encoding header in all files automatically before a release.
As for encoding defaults:
- Ruby 1.8 and below didn't knew the concept of string encodings at all. Strings were more or less byte arrays.
- Ruby 1.9: default string encoding is
US_ASCII
everywhere. - Ruby 2.0 and above: default string encoding is
UTF-8
.
Rails + Ruby 1.9 invalid byte squence in US-ASCII
I found the solution:
The problem is:
Fetching data from any database (Mysql, Postgresql, Sqlite2 & 3), all configured to have UTF-8 as it's character set, returns the data with ASCII-8BIT in ruby 1.9.1 and rails 2.3.2.1.
(Taken from: https://rails.lighthouseapp.com/projects/8994/tickets/2476)
My attempt to use the patched mysql adapter likely failed because my database was not configured to natively use utf8, so the patched adapter failed to work properly.
The fix ended up being to use the patch file available here: http://gnuu.org/2009/11/06/ruby19-rails-mysql-utf8/
require 'mysql'
class Mysql::Result
def encode(value, encoding = "utf-8")
String === value ? value.force_encoding(encoding) : value
end
def each_utf8(&block)
each_orig do |row|
yield row.map {|col| encode(col) }
end
end
alias each_orig each
alias each each_utf8
def each_hash_utf8(&block)
each_hash_orig do |row|
row.each {|k, v| row[k] = encode(v) }
yield(row)
end
end
alias each_hash_orig each_hash
alias each_hash each_hash_utf8
end
(Placed in lib/mysql_utf8fix.rb and required in enviornment.rb using require 'lib/mysql_utf8fix.rb'
) Has anyone successfully deployed a Rails project with Ruby 1.9.1?
Matz recently spoke at RubyFoo in London about ruby 1.9.1 adoption. Quite simply, ruby 1.9.1 is not production ready and should not be used for deployment just yet.
Ruby 1.9.2 will be production ready, but until then you should only use ruby 1.9.1 for play and testing.
Although many people out there have had successful deployments using 1.9.1, I would recommend sticking with REE 1.8.7 until 1.9.2 is out. Rails 3.0 will favor 1.9.2, but also work quite happily with 1.8.7 (it will NOT work with 1.8.6).
Method fails due to Encoding::UndefinedConversionError: U+03B1 from UTF-8 to ISO-8859-1
I fixed this thanks to Domon's suggestion. The webpage that Mechanize was interacting with was in ISO-8859-1 format (see more about how to detect that here), whereas my system was trying to read the page in UTF-8 formatting. To fix this, I entered agent.page.encoding = 'utf-8'
into my scrape
method's script as illustrated in Niels Kristian's answer here. (See also denis.peplin's answer there for further clarification on where to write it.) This allowed a forced conversion of the webpage into the proper formatting (UTF-8) that my system required to read it.
How to avoid Encoding::CompatibilityError with ruby 1.9 and mechanize form submit?
Well, looks like a bug in the 0.9.3 version of mechanize (probably issue 25). Using the current git version of mechanize fixes the posting issues for me.
But a mechanize 1.0 version should include the fix as well.
Mechanize on Ruby 1.9.3 encoding issue
just install the previous mechanize version:
gem install mechanize -v 2.0.1
orgem 'mechanize', '2.0.1' and `bundle update`
I've been getting the same issue with 2.1btw, there's an ongoing discussion on mechanize github regarding it
Related Topics
Why Should @@Class_Variables Be Avoided in Ruby
Converting Ruby Hashes to Arrays
Heroku Repo Size and Slug Size Increase with Each Deployment. Why
Ruby Gem Development - How to Use Activerecord
Sql Like Operator in Ruby on Rails
How to Validate Overlapping Times in Rails with Postgresql
How to Unescape C-Style Escape Sequences from Ruby
Robust Way to Deploy a Rack Application (Sinatra)
Can Sunspot Search Inside Array
How to Avoid Circular Creation of Associated Models in Factory_Girl
Indent Multiline String in Erb
Pod Install in Xcode Bots Trigger
Bundler Using Wrong Ruby Version
Error Installing Gems: Cannot Load Such File - Zlib
Ruby: Start Reading at Arbitrary Point in Large File