Encoding issues in javascript files using rails resource pipeline

I am using rails 3.1 and the asset pipeline (ruby 1.9.2).

I get the following error when trying to serve javascript js.erb file with utf-8 encoded strings

 invalid byte sequence in US-ASCII 

I set Encoding.default_external = "UTF-8" in the environment.rb file. How can I make the resource pipeline work with a different encoding?

EDIT

The error appears only when I generate the utf-8 character outside the file (in this case, by querying from the database). The error goes away if I add

 <% "日" %> 

to the beginning of the file. I guess some kind of coding is going on here, but how can I avoid this without this hacker solution?

+11
ruby ruby-on-rails asset-pipeline
Oct 13 2018-11-11T00:
source share
1 answer

When a file is uploaded, Ruby tries to "guess" its encoding. If UTF-8 or any other characters other than ASCII are not found, it uses US-ASCII as the encoding for the file and throws an error if it unexpectedly encounters a character other than ASCII, which, for example, is loaded at run time.

The best solution to this problem is to force Ruby to use a specific encoding by adding # encoding: utf-8 as the first line of the .rb file or <%# encoding: utf-8 %> if it is a .erb file.

+20
Oct 27 2018-11-21T00:
source share



All Articles