invalid byte sequence in US-ASCII (Argument Error) when I run rake db:seed in Rails

佐手、 提交于 2019-11-29 19:13:48

You're receiving an encoding error because your filesystem isn't configured to encode the date you've added (since presumably it includes new characters – possibly in your HTML entity encoded map URL – that didn't exist in your prior data seed).

The following will should resolve this error by setting the UTF-8 locale on your machine:

# from your command line
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
bundle

The benefit of setting a system locale is that all gems (going forward) will be bundled using UTF-8 encoding.

EDIT:

Alternatively, if you don't want to change your system encoding, you can set your encoding project-wide by specifying an encoding standard in your Gemfile:

if RUBY_VERSION =~ /1.9/ # assuming you're running Ruby ~1.9
  Encoding.default_external = Encoding::UTF_8
  Encoding.default_internal = Encoding::UTF_8
end

Add

#encoding: utf-8

at the top of the file

You can also try export RUBYOPT="-KU -E utf-8:utf-8" as mentioned in this GH thread

If you are having this problem in a ruby docker container you might either set LANG=C.UTF-8:

docker run -it --rm -e LANG=C.UTF-8 ruby ...

or watch this issue.

Like others suggested, I added #encoding: utf-8 to the top of my (in this case, seed.rb) file, but still couldn't get the rake task to work.

Interestingly enough, a friend recommended that I remove the additional, rails-generated comments from the top of the seed file so that only #encoding: utf-8 remained.

What would you know - it worked.

There is a invalid character in your seed file, in my case changing single quotes from ` to ' worked out.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!