Encoding issues in javascript files using rails asset pipeline - ruby-on-rails

I'm using rails 3.1 and the asset pipeline (ruby 1.9.2).
I get the following error when trying to serve a javascript js.erb file that has utf-8 encoded strings
invalid byte sequence in US-ASCII
I've set Encoding.default_external = "UTF-8" in my environment.rb file. How do i get the asset pipeline to serve with a different encoding?
EDIT
The error only shows up when I'm generating the utf-8 character outside of the file (in this case by querying from the DB). The error goes away if I add
<% "日" %>
to the top of the file. I'm guessing there's some kind of encoding guessing going on here, but how do I avoid it without that hacky solution?

When loading a file, Ruby tries to "guess" its encoding. If no UTF-8 or any other non-ASCII characters are found, it uses US-ASCII as encoding for the file and throws an error if it suddenly encounters a non-ASCII character, which e.g. is loaded at run-time.
The best solution for this problem is to force Ruby to use a certain encoding by adding
# encoding: utf-8 as the first line of a .rb file or <%# encoding: utf-8 %> if it's a .erb file.

Related

Ruby on Rails locales utf-8 issues

I allready have problems with utf-8 encoding in my ror app ...
some are fixed now. But some are still left.
I have now an utf-8 force in my layout
But still have problems with German special chars (ä, ö, ü). In my /config/locales/de.yml I have lots of them. In the File they look nice :) tested with rubymine and nano.
But when I start the app it crashes. The yml is encoded in utf-8 ..
I've also tried this:
f\xC3\xBCr --> should be für
always got this:
incompatible character encodings: UTF-8 and ASCII-8BIT
Does anyone have some hints for me?
It seems to me that the encoding of the app is set to UTF-8.
Are you sure that RubyMine saves your file with UTF-8?
You can add
# encoding: UTF-8 to the top of your files to assure it is set. (Not sure if this works in .yml)
Edit:
If you have pasted any text into the file it may still contain wrong encoding.
Move the de.yml out of the project.
Create a new file de.yml
de:
first_translation: Ich möchten ein bisschen Müsli
If this works, then you need to rewrite everything from the old file, no copying!

Incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string) on Heroku

I have a Rails application where I use regex-based rules to categorize transactions. In my seeds.rb, I create some categories and rules, then import transactions from a CSV file (also utf8-encoded) and allow them to be categorized. This process works fine on my development machine, but when I run it on Heroku, I get:
incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)
I am running the Cedar Stack, Rails 2.3.15. I have put
# encoding: utf-8
at the top of all my source files and I've set the encoding to utf-8 in my app config, so I'm not sure what else could be causing this problem. I'm wondering if has something to do with the Heroku configuration.
The issue could be caused by invisible characters that are ignored by your local operating system, ensuring proper encoding takes place whereas on Heroku, the characters mess up the magic number declaration at the top of the file and you end up with both ASCII-8BIT and UTF-8.
Since the file that is having issues contains the regex, it's probably your model class instead of seeds.rb.
There are many ways to view invisible characters in your file. In vi, just set the option :set list

UTF-8 issue in Ruby on Rails with × character

<a class="close" href="#">×</a>
I get an error regarding the use of ×.
It's used in error messages on twitter's bootstrap framework, I get an invalid byte sequence in UTF-8 error when I try to use it. Is there any work-around? Apart from using a normal x or X.
I have:
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
In my application.rb
This seems almost too simple, but why aren't you using ×?
You need to set the encoding at the top of the file where that character is used. You can do this with:
# coding: utf-8
class MyClass
end
I haven't tried it in an erb file, but I don't see why that would be any different. I think you can use the word "encoding" too instead of just "coding" if that feels better. All that is required is at minimum "coding".
What editor are you using?
I suspect that you are saving the source file using an encoding other than UTF-8 (such as Latin-1 or ANSI on Windows), which is then causing ruby to fail to interpret the file correctly.
I've tried adding the times symbol to one of my views (using HAML) and it worked correctly. I'm using VIM as my editor and saving in UTF-8 without any BOM.
#encoding: utf-8
class ClassiClass
end
everything works fine!

Encoding problems in rails on ruby 1.9.1

I am using rails 2.3.3 and ruby 1.9.1.
I am trying to render a view that includes a partial. In the partial i output a field of a model that is encoded in UTF8.
This fails with
ActionView::TemplateError (incompatible character encodings: ASCII-8BIT and UTF-8) on line #248 of app/views/movie/show.html.erb:
245: <!-- Coloumn right | start -->
246: <div class="col_right">
247:
248: <%= render :partial => 'movie_stats' %>
249:
250: <!-- uploaders -->
251: <div class="box_white">
On the other hand, i can output the field with utf8 content just fine if i directly use that field in a view (when it is not in a partial).
How can i fix this?
I already tried setting the default encoding but that did not seem to work.
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/
There appears to be an issue with ERB's encoding in Ruby 1.9. More details are in this Lighthouse ticket. A patch with a workaround has been included, perhaps it works for you?
The problem is erb code in ruby 1.9 distribution. When it compiles the template code it forces a 'ASCII-8bit' encoding, the problem is when the template code has multibyte characters the template code is returned in a 'ASCII-8bit' string and when this string is concat with a 'UTF8' string with multibyte character the exception is raised because the strings between this encodings are only compatible when both only have seven-bit characters.
There seems to be an incompatibility between Ruby 1.9x and the mysql gem with regard to how strings are passed back and forth (specifically the encoding of the strings).
To fix, run
gem install mysql2
on the server and update the database configuration file to use this gem instead of the previous one.

ruby on rails x charset

i'm having problem to deal with charset in ruby on rails app, specificially in my templates. Code that comes from my database, works fine, but codes like ç ~ that are located in my views are not working. I added the following codes to my code
I added a function like that, but that still not working i have ç ~ codes in my application.rhtml that are not working.
before_filter :configure_charsets
# Configuring charset to UTF-8 def configure_charsets
headers["Content-Type"] = "text/html; charset=UTF-8"
end
I added as well meta http-equiv html to utf-8 and a .htaccess parameter AddDefaultCharset UTF-8
That's still not working, any other tip?
Put this piece of code in your config (environment.rb)
Rails::Initializer.run do |config|
config.action_controller.default_charset = "iso-8859-1"
end
This will do it.
Also, remove the default charset line if any in layouts/application.html
Is the text editor you're using to put the special characters into the file (either source or views) treating those characters as UTF-8? For example, if you're using TextMate, you can deliberately save a file as UTF-8. If for some reason you used a different encoding earlier (a default, perhaps), those UTF-8 characters might be getting transcoded at the code editing stage, so even if the rendering process is using UTF-8 throughout, it'll still not work.
Further, if you're using something from a shell, like vi, or whatever, is your terminal set up to accept UTF-8 as default? If you had it set to ISO-8859-1 or whatever, you'd get the same issue.
Is your application.rhtml file written in the correct character set? Make sure it's UTF-8, and not ISO-8859-1.
So if the contents of your file are UTF-8, and the output is being interpreted as UTF-8, something in between is changing the data. Can give give us the the hex interpretation of the input bytes (anything non-ASCII will be at least two bytes in UTF-8) for one of your special characters, and the hex interpretation of the output byte or bytes? Perhaps we can figure out what the change is, and work back from there.

Resources