Incompatible character encodings: ASCII-8BIT and U

2019-05-19 15:13发布

问题:

I am trying to use javascript in my rails 3 app, version 1.9.3. When I include this line %= javascript_include_tag "application" %> I get the following error:

Encoding::CompatibilityError in Products#index 

incompatible character encodings: ASCII-8BIT and UTF-8

Extracted source (around line #9):

6:<%= javascript_include_tag 'jquery-1.7.2.min.js','jquery-ui-1.8.21.custom.min.js' %>
7:<%= stylesheet_link_tag 'jquery-ui-1.8.21.custom.css' %>
8:<%= stylesheet_link_tag  "pro" %>
9:<%= javascript_include_tag "application" %>
10:<%= csrf_meta_tags %>
11:   </head>

回答1:

Ruby 1.9 is really picky when it comes to string encoding.

You have to make sure you're using utf8 encoded files throughout your application and your database.

In this case, I'd look at "application.js" to see if it is encoded in utf8 and if it contains any 'misencoded' characters in utf8.

You can also try to set this in application.rb :

class Application < Rails::Application
  config.encoding = "utf-8"
  #Your conf ...


end

And this in environment.rb before the initliaze! call:

Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8

I had similar problems while upgrading a Rails 2 and this helped me.