“\xC4” from ASCII-8BIT to UTF-8 when running rake

2019-08-13 05:50发布

When I try to run heroku run rake db:migrate in my application, I get the following error

"\xC4" from ASCII-8BIT to UTF-8

with a stacktrace of

/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:150:in `write'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:150:in `puts'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:150:in `display_error_message'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:142:in `rescue in standard_exception_handling'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:132:in `standard_exception_handling'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/app/.bundle/gems/ruby/1.9.1/gems/rake-0.9.2.2/bin/rake:33:in `'
/app/.bundle/gems/ruby/1.9.1/bin/rake:19:in `load'
/app/.bundle/gems/ruby/1.9.1/bin/rake:19:in `'

I've tried setting encoding manually in config/environment.rb via

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

But it doesn't seem to help. The problem might be, that I'm using slim as a templating engine with many unicode characters directly in the templates, but I'm not sure if there's a better way of doing that.

1条回答
啃猪蹄的小仙女
2楼-- · 2019-08-13 06:42

Simply add this to the top of your file

# encoding: UTF-8

this error is frequent with migration when you insert data. For exemple I've got this with a simple migration adding Québec

Province.create(:name => 'Québec (Canada)')
查看更多
登录 后发表回答