Ruby 1.9 throwing javascript encoding error - ruby-on-rails

So, I updated my Ruby to 1.9.2, followed this:
Make sure 'config.encoding = "utf-8"' is there in application.rb file.
Make sure you are using 'mysql2' gem
Putting '# encoding: utf-8' on top of rake file.
Above 'Starter::Application.initialize!' line in environment.rb file, add following two lines:
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
and now all my Ruby code is working right, but my assets/javascript/application.js is loaded only with this code:
throw Error("Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8")
how can i define assets encoding?
ps.: i didn't try to precompile yet, this is happening on development mode

Read the answer to this question. Sounds like it's the same issue you're looking at. It sounds like the solution is to open the .js file with a text editor and save it as UTF-8 encoding.

Related

How do I fix "Incompatible character encodings: UTF-8 and ASCII-8BIT"?

Incompatible character encodings: UTF-8 and ASCII-8BIT
How can I solve this error on Rails 3.2.3 and Ruby 1.9.3?
I tried to put these two lines in the environment.rb:
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
I am sure that application.rb also contains this line "config.encoding = "utf-8".
Does anyone know how to solve this?
Consider using so called magic comments on the first line of your .rb file with encoding:
# encoding: UTF-8
class Xyz
...
end
It is very important in files where you place text with accents and other non-ASCII characters. They are the primary cause of the error you mention.
Sometimes it may happen that you mistype a character and, instead of a letter, you insert a hidden symbol. Also check your file for these. Look at the line ends and in spaces.
If you have data to store on the hdd, you can try data.force_encoding('UTF-8').

Inconsistent display of UTF-8 across app: garbled in some places, not others

Using haml, formtastic, rails 3.1.3, ruby 1.9.2, sqlite3 in dev, postgres in production.
I have a form, which successfully encodes strings as utf-8. Strings such as Słów are being passed properly in params and written to the database fine in both development and production.
However, after save, the form field displays a garbled Słów. Placing the attribute on the same page via something like #work.field also displays the garblage.
If I call #work.field on another template file, it renders fine.
I've done the usual:
- Make sure 'config.encoding = "utf-8"' is there in application.rb file.
- Putting '# encoding: utf-8' on top of file containing utf-8 characters.
- Above '<App Name>::Application.initialize!' line in environment.rb file, add following two lines:
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
Oh. I added this in layouts/application.rb which works.
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />

In Ruby on Rails, are '#encoding: utf-8' and 'config.encoding = "utf-8"' different?

I can specify any ruby file to use specific encoding by add a comment line at its top:
#encoding: utf-8
But in Rails' config/application.rb, I found this:
config.encoding = "utf-8"
Are they different? If I have set config.encoding = "utf-8", still I need #encoding: utf-8?
The config.encoding = "utf-8" part in config/application.rb is related to how rails should interpret content.
#encoding: utf-8 in a ruby file tells ruby that this file contains non-ascii characters.
These two cases are different. The first one (in config/application.rb) tells rails something, and has nothing at all to do with how ruby itself should interpret source files.
You can set the environment variable RUBYOPT=-Ku if you're lazy and want ruby to automatically set the default file encoding of .rb files to utf-8, but I'd rather recommend that you put your non-ascii bits in a translation file and reference that with I18n.t.

Rails/Ruby 1.9: Is there a better way to put Unicode in source files than sticking # encoding at the top of every file

I just upgraded to Rails 3 and Ruby 1.9. All of my source files that used Unicode inside of them (such as emdashes) caused problems until I found out that you now need to include the following magic comment on top of each source file:
# encoding: utf-8
Is there a better way to do this? It'd be nice if it just automatically treated every source file as utf-8 like Rails 2.3/Ruby 1.8 did, and I don't see any apparent disadvantage from doing so.
In my application.rb I already have the following, but I can't tell that it does anything:
config.encoding = "utf-8"
i found only rake check_encoding_headers it add the magic comment to all files.
There is also a gem for adding encodings to all headers.

Ruby on Rails 3, incompatible character encodings: UTF-8 and ASCII-8BIT with i18n

I've got some troubles with the couple Rails 3.0.1, Ruby 1.9.2 and my website localization.
The problem is quite simple, i've got something like that in a view :
f.input :zip_code, :label => I18n.t('labels.zip_code')
and a es.yml file :
es:
labels:
zip_code: "Este código postal no es valido."
There are no troubles with the en.yml file (it's pure ASCII) but when the website is set with i18n.locale == 'es' I get this error :
incompatible character encodings: UTF-8 and ASCII-8BIT
I have been looking around for quite a while but didn't found a way to use my UTF-8 translation files.
Did some knows how to make it works ?
Thanks for your help.
Ok so problem solved after some hours of googling...
There was actually two bugs in my code. The first one was a file encoding error and the second was the problem with the MySQL Data base configuration.
First, to solve the error caused by MySQL I used this two articles :
http://www.dotkam.com/2008/09/14/configure-rails-and-mysql-to-support-utf-8/
http://www.rorra.com.ar/2010/07/30/rails-3-mysql-and-utf-8/
Second, to solve the file encoding problem I added these 2 lines in my config/environment.rb
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
Hopefully this will help someone :)
I solved most of the problems by combining many solutions:
Make sure application.rb has this line: config.encoding = "utf-8".
Make sure you are using 'mysql2' gem
Putting # encoding: utf-8 at the top of any file containing utf-8 characters.
Add the following two lines above the <App Name>::Application.initialize! line in environment.rb:
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
http://rorguide.blogspot.com/2011/06/incompatible-character-encodings-ascii.html
Are you sure your es.yml file was saved as UTF-8?
If you're on Windows, use http://notepad-plus-plus.org/ to make sure.
Using this unpack function helped me sort this out finally, try this if you get the can't convert error message:
myString.unpack('U*').pack('U*')
Make sure you have config.encoding = "utf-8" in your config/application.rb. Also, your example translation file doesn't match the key you're searching for (com_name and first_name) but I suppose that could just be a typo.

Resources