10

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?

New Alexandria
  • 6,951
  • 4
  • 57
  • 77
spike
  • 9,794
  • 9
  • 54
  • 85

1 Answers1

19

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.

Frane
  • 514
  • 3
  • 11
  • I'd tried various versions of that (it's .erb so i tried things like <% # encoding: utf-8 %>), this inspired me to try more, and <%# encoding: utf-8 %> works!. update your answer and i'll accept it. – spike Oct 27 '11 at 22:17
  • You're right. While writing the I forgot that you were dealing with a .erb file in which case <%# encoding: utf-8 %> is correct. I updated my answer to express that. – Frane Oct 28 '11 at 09:12
  • for some reason I never got notified of this comment / edit. Thanks! – spike Nov 16 '11 at 16:28
  • I'd really like a system-wide solution so I don't need to paste that text at the top of every ERB (doesn't seem very DRY...) – Jonathon Horsman Apr 04 '13 at 11:12
  • 2
    AFAIK there's no way to do this system wide. However with Ruby 2.0 the default encoding was changed from ASCII to UTF-8. So, with Ruby 2.0 this should be problem of the past. See: http://www.ruby-lang.org/en/news/2013/02/24/ruby-2-0-0-p0-is-released/ – Frane Apr 05 '13 at 16:13