4

I have a Rails (version 3.2.1) project and I recently switched from Ruby 1.8.7 to Ruby 1.9.3. I added encoding comments in all .rb and .erb files but I have a problem with plain .js JavaScript assets. They contain comments in Polish saved with UTF-8 encoding. In most files it works fine - Ruby detects the encoding correctly - but in one file I have only a single UTF-8 character ś in a comment line. And now the Rails server fails with Encoding::CompatibilityError when displaying index page (which doesn't even reference the JavaScript file).

I know it's a problem with Ruby's automatic encoding detection as it probably can't detect encoding from single character outside ASCII range, but it's very frustrating since it shouldn't matter anyway - the file is only linked to as an asset.

My question is: how to enforce UTF-8 encoding in all .js assets?

2
  • did you set the default encoding in your application.rb file like config.encoding = "utf-8"? Commented Mar 22, 2012 at 14:49
  • @phoet yes, I have the line config.encoding = "utf-8" Commented Mar 22, 2012 at 14:58

1 Answer 1

4

try renaming it to js.erb and placing <%# encoding: utf-8 %> at the top

found a hint here: Encoding issues in javascript files using rails asset pipeline

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that seems to fix the problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.