Seeding the sqlite3 database with Fixtures did work with config1 but with config2 utf-8 support fails.
config1: Ubuntu 11.04, ruby 1.9.2p136, Rails 3.0.3, "development" environment
config2: OS X Lion, ruby 1.9.2p290, Rails 3.0.3, "development" environment
When having a yml file like
001:
id: 1
name: "\xC3\xBC"
I use it to populate the database with
Fixtures.create_fixtures(...)
in seed.rb
Afterwards the database shout have an entry with id 1 and name "ü". That would be correct.
That worked with config1. With config2 I alway have an entry with name "ü". utf-8 encoding seems to fail.
I already turned on UTF-8 everywhere in the rails project:
encoding: utf-8
in database.yml
# encoding: utf-8
in seeds.rb
Encoding.default_external = "UTF-8"
Encoding.default_internal = "UTF-8"
config.encoding = "utf-8"
in config/environments/development.rb
Is it possible that these two systems have different endianness?
From the SQLite3 release notes:
Support for UTF-8 and UTF-16
The new API for SQLite 3.0 contains routines that accept text as both
UTF-8 and UTF-16 in the native byte order of the host machine. Each
database file manages text as either UTF-8, UTF-16BE (big-endian), or
UTF-16LE (little-endian).
You can check here:
http://en.wikipedia.org/wiki/Endianness#Endianness_and_operating_systems_on_architectures
Related
Since the default encoding is now utf-8 for Ruby 2.0, the rest-open-uri gem throws the following error on heroku:
/app/vendor/bundle/ruby/2.0.0/gems/bundler-1.5.2/lib/bundler/runtime.rb:76:in `require': /app/vendor/bundle/ruby/2.0.0/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb:413: invalid multibyte escape: /[\r\n\t !#-[]-~\x80-\xff]+|(\[\x00-\x7f])/ (SyntaxError)
I need to change the following line in rest-open-uri.rb. How can I access this file on heroku?
lib/ruby/gems/2.0.0/gems/rest-open-uri-1.0.0/lib/rest-open-uri.rb
# encoding: US-ASCII
rest-open-uri looks like a dead project. Do you want to get in the business of patching up 8 year old code to get your application to work?
It looks like rf-rest-open-uri is a more up-to-date fork of that project, have you tried it? I don't see any encoding instruction in that gem's rest-open-uri.rb source file.
I just upgraded my Ruby version from 1.8.7 to 1.9.2 (using RVM). So Ruby is: ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin12.2.0]. The app is on Rails 3.2.8. Since upgrading, I get an error regarding multi-byte characters.
I've found multiple very similar issues on stackoverflow, but none of the solutions in them worked for me:
Ruby 1.9 - invalid multibyte char (US-ASCII)
and
invalid multibyte char (US-ASCII) with Rails and Ruby 1.9
and
Rails 3.1, Ruby 1.9.2-p180 and UTF-8 issues
When I start my app, and try to browse to it, I multiple errors, such as:
SyntaxError
(/Users/antun/ror/parktwor3svn/app/helpers/application_helper.rb:35:
invalid multibyte char (UTF-8)
/Users/antun/ror/parktwor3svn/app/helpers/application_helper.rb:35:
syntax error, unexpected tIDENTIFIER, expecting ']' ... States",
"Afghanistan", "?land Islands", "Albania", "Algeri...
The offending code (in app/helpers/application_helper.rb) looks like this. For brevity, I've boiled it down to just the first few countries - the "Å" in "Åland Islands" is one of the characters that triggers it. When I look up that character in my editor, I get: 00c5. (There's a bunch more unicode characters in other country names).
def countries
return ["", "United States", "Afghanistan", "Åland Islands", "Albania"]
end
So far, I have tried adding the following to app/helpers/application_helper.rb as the very first line to fix it:
# encoding: UTF-8
and
# encoding: utf-8
and
# -*- coding: utf-8 -*-
None of these worked.
My config/application.rb has:
config.encoding = "utf-8"
My text editor is VIM, and when I do :set encoding on that file, it returns: "encoding=utf-8".
Use a tool such as od (assuming you're on Linux) to look at the actual contents of the file. If you have single byte C5, your file isn't in UTF-8. It's probably stored in ISO-8859-1 or similar, and Ruby is quite correctly complaining.
Running in to some parsing problems with ruby 1.9.2-p290 and rails 3.1.3.
My YAML file looks like this:
api_key: 12345
The other YAML files parse fine, like the database.yml and locale files. It's just this one.
Any ideas as to why?
there must be an error somwhere else, cause the line that you are showing is parsable by psych and syck engines:
YAML::ENGINE.yamler = 'psych'
YAML.load("api_key: 12345") # => {"api_key"=>12345}
YAML::ENGINE.yamler = 'syck'
YAML.load("api_key: 12345") # => {"api_key"=>12345}
Can I somehow configure Rails to avoid the need to add # coding: utf-8 to all files? Or must I add it to each file manually?
UPD
To improve my life I have found this gem:
magic_encoding
It won't fix the problem, but it will add magick line to each file. Related topic: Why are all strings ASCII-8BIT after I upgraded to Rails 3?
In a rails application you can specify the default chracter encoding in your application config.
Add the following code inside the Application class in in config/application.rb
Looks something like:
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
My Rails app is working from a mysql database, and when I switched from Rails 2.8, Ruby 1.8 and the mysql gem over to Rails 3.0.7, Ruby 1.9.2 and the mysql2 gem, all of a sudden my pages are rendering with smartquotes (curly quotes) and em-dashes, etc., all looking like gibberish.
I'm assuming this has something to do with UTF-8, but I don't know how to pinpoint it.
Here's what I do know:
(1) config/database.yml has the following:
development:
adapter: mysql2
encoding: utf8
(2) config/application.rb has the following:
config.encoding = "utf-8"
Don't know where to go from there.
Any suggestions?
You can narrow it down to the db vs the view by doing two things:
rails console # in your terminal in the project directory, then test that your ActiveRecord objects have the correct data.
ViewSource in your browser to see if the generated source is putting in the correct characters.
Later:
This is probably a duplicate of: mysql2 gem, Rails 3.0.3 and "incompatible character encodings" errors https://github.com/brianmario/mysql2/issues/124
And maybe even the monkeypatch here: http://www.rorra.com.ar/2010/07/30/rails-3-mysql-and-utf-8/