Asset pipeline encoding problems (UTF-8 vs. ASCII-

2019-02-17 00:48发布

问题:

I'm trying to create a gem that wraps d3.js, Source can be found at https://github.com/iblue/d3-rails

So when I include this gem in my Gemfile

gem "d3-rails", :git => "git://github.com/iblue/d3-rails.git"

And when I include the javascript in my application.js:

//=require d3

Then my asset compilation fails and my compiled application.js just contains

throw Error("Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT")

I am using Rails 3.1.3 and ruby-1.9.3-p125. jQuery uses exactly the same gem structure and it works. What am I doing wrong?

EDIT:

I also saw this question: Ruby 1.9 throwing javascript encoding error. This does not apply here, my file is valid UTF-8:

ruby-1.9.3-p125 :001 > d = File.read("./d3.js")
 => [...]
ruby-1.9.3-p125 :002 > d.encoding
 => #<Encoding:UTF-8> 
ruby-1.9.3-p125 :003 > d.valid_encoding?
 => true 

EDIT 2:

I also tried to insert some voodoo in my config/environment.rb. It does not work:

# -*- encoding : utf-8 -*-
# Load the rails application
require File.expand_path('../application', __FILE__)

# --------- VOODOO BEGINS HERE -----------------
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
# --------- END VOODO --------------------------

# Initialize the rails application
Ratecode::Application.initialize!

回答1:

Is the error from a WEBrick server or something else? Does rake assets:precompile work? (don't forget to clear the assets after)

If the latter fails, double check the value of your $LANG environment variables is UTF-8 (with env). If the rake task works but the app is failing then it could be the server env vars.