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').
Related
I got error :_ incompatible character encodings: UTF-8 and Windows-1250_
when i try to show something with chars from Poland ie. 'ąęźć'
in my application.rb i got:
config.encoding = "windows-1250"
In database.yml:
encoding: windows-1250
How can i show params in windows-1250 in rails admin panel?
I would suggest you go with utf-8 encoding (which is ruby's default these days).
Your input 'ąęźć' is a valid utf-8 string, so you would face no problem in decoding it as a utf-8 string.
If you still want to hack around, you can use:
'ąęźć'.mb_chars.tidy_bytes.to_s
which should also give you the desired output.
I know this have been asked several times, but to me is happening something strange:
I have an index view where rendering certain characters (letters with accent) causes Rails to raise the exception
incompatible character encodings: ASCII-8BIT and UTF-8
so i checked my strings encoding and this is actually ASCII-8BIT everywhere, even though i set the proper encoding to UTF-8 in my application.rb
config.encoding = "utf-8"
and in my enviroment.rb
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
and in my database it appear:
character_set_database = utf-8
as suggestend in some guides.
Strings are inserted with a textarea field and are not concatenated to any other already inserted string.
The strange things are:
this happens only in the index view, whereas this is not happening in the show (same resource)
this happens only for this model (which is an email, with subject and body, but this shouldn't affect anything)
In my development environment everything goes well setting str.force_encoding('utf-8'), whereas in my production environment this is not working. (dev i'm with Ruby 2.0.0, in production Ruby 2.1.0, both Rails4, and both MySql)
setting the file view with # encoding utf-8 also doesn't work
trying str.force_encoding('ascii-8bit').encode('utf-8') says Encoding::UndefinedConversionError "\xC3" from ASCII-8BIT to UTF-8 which is an à, while using body.force_encoding('ascii-8bit').encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => '?'), replaces all accented charaters with a ?, while str.force_encoding('iso-8859-1').encode('utf-8') obviously generates the wrong character (a ?).
So my questions are 2:
- why is rails setting the string encodint to ascii-8bit?
- how to solve this issue?
I've already checked these questions (the newest ones with rails4):
Rails View Encoding Issues
"\xC2" to UTF-8 in conversion from ASCII-8BIT to UTF-8
How to convert a string to UTF8 in Ruby
Encoding::UndefinedConversionError: "\xE4" from ASCII-8BIT to UTF-8
and other resources also, but nothing worked.
You probably have a string literal in your source code somewhere that you then concatenate another string too. For instance:
some_string = "this is a string"
or even
some_string = "" #empty string
Those strings, stored in some_string, will be marked ASCII_8BIT, and if you then later do something like:
some_string = some_string + unicode_string
Then you'll get the error. That is, those strings will be marked ASCII-8BIT unless you add, to the top of the file where the string literals are created:
#encoding: utf-8
That declaration determines the default encoding that string literals in source code will have.
I am just guessing, because this pattern is a common source of this problem. To know more for sure, it would take more information than is in your question -- it would take debugging the actual source code, to figure out exactly what string is tagged as ASCII-8BIT when you expect it to be tagged UTF-8 instead, and exactly where that String came from.
I'm trying to include a degree symbol into my Rails view. If I put the degree symbol (°) straight into .html.erb file, it is displayed by browser in a normal way.
But this symbol should be transferred to view via string. And here the problem begins.
If I put this:
<%= 176.chr %>
into view, or put
.... + 176.chr
into ruby source, I get
incompatible character encodings: UTF-8 and ASCII-8BIT
How to make Rails recognize all views as UTF-8 by default?
You can use special replacement for this symbol in HTML: °.
http://www.w3schools.com/charsets/ref_html_entities_4.asp
You have to put it in HTML, outside the <%= %>. Or use raw helper. Or mark it as html_safe. And by the way, did you try to supply encoding in your chr? Like 176.chr(__ENCODING__) (__ENCODING__ here isn't placeholder, it's Ruby thing) or 176.chr(Encoding::UTF_8). All these approaches should work.
This should already be specified inside your application.rb inside /config/.
The relevant section should look like this:
module App
class Application < Rails::Application
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
end
end
I think the issue here is that you are generating a ASCII-8BIT character that should be inserted into the UTF-8 body.
If you want to use a UTF-8 String in your Ruby code you have to put this magic string into the first line of your ruby file:
# encoding: UTF-8
Details on Encoding in Ruby 1.9 can be found here
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.
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.