问题
I am encountering an incompatible character encoding error in production. I tried to reproduce it locally but had no luck. This is the error message:
A ActionView::Template::Error occurred in controller_name#action_name:
incompatible character encodings: UTF-8 and ASCII-8BIT
activesupport (3.0.5) lib/active_support/core_ext/string/output_safety.rb:80:in `concat'
config.encoding
is already set to "utf-8" in application.rb. What's perplexing to me is that the issue persists only for some time around the Passenger restart and vanishes after some time. Further debugging into the exception backtrace gave me this:
activesupport (3.0.5) lib/active_support/core_ext/string/output_safety.rb:80:in `concat'
activesupport (3.0.5) lib/active_support/core_ext/string/output_safety.rb:80:in `concat'
actionpack (3.0.5) lib/action_view/template/handlers/erb.rb:14:in `<<'
app/views/web/controller_name/action_name.erb:98
<%currentUrl = request.url%>
Line 98: <a href="<%= raw currentUrl %>" id="xyz">
It is mentioned in "ActionView::Template::Error (incompatible character encodings: UTF-8 and ASCII-8BIT)" that ActionDispatch::Request
might cause the problem, but I am clueless as to how the issue fixes itself after a while!
回答1:
If you are getting this error with any text which is coming from database, then you need to use mysql2 in stead of mysql adapter in database.yml. And add mysql2 gem in your Gemfile too. This will solve your problem.
回答2:
Try setting file encodings to UTF-8 by adding # encoding: UTF-8
to the top of the file(s).
来源:https://stackoverflow.com/questions/20075740/dealing-with-incompatible-character-encodings-utf-8-and-ascii-8bit