Rails:当我运行rake db:seed时,出现US-ASCII中的无效字节序(参数错误)

94
当我在我的Rails应用程序中运行rake db:seed时,我遇到了以下错误:
无效的字节序列US-ASCII(参数错误)
我刚刚将science_majors添加到我的种子文件中,然后当我运行rake db:seed时,它给了我这个错误:
无效的字节序列错误
为什么会出现这种情况,我该如何修复呢?
seeds.rb的一部分
@college = College.find_or_create_by_name!('University of Pittsburgh')
if @college.update_attributes(
  url: 'university-of-pittsburgh',
  public: 'Public',
  years: '4-year',
  category: 'National University',
  calendar: 'Semester',
  location: 'Pittsburgh, PA',
  setting: 'Large City (250-500k)',
  retention: '90',
  majors: 'business/marketing: 15%|social sciences: 14%|health professions: 11%|english: 10%|engineering: 9%|psychology: 8%|biology: 7%|history: 5%',
  scholarships_link: 'http://www.oafa.pitt.edu/universityschlrs.aspx',
  map: '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=university+of+pittsburgh&amp;aq=&amp;sll=40.444261,-79.970448&amp;sspn=0.142165,0.251656&amp;ie=UTF8&amp;hq=university+of+pittsburgh&amp;t=m&amp;ll=40.443486,-79.958245&amp;spn=0.010798,0.021038&amp;output=embed"></iframe><br /><small><a href="https://maps.google.com/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=university+of+pittsburgh&amp;aq=&amp;sll=40.431368,-79.9805&amp;sspn=0.142165,0.251656&amp;ie=UTF8&amp;hq=university+of+pittsburgh&amp;t=m&amp;ll=40.443486,-79.958245&amp;spn=0.010798,0.021038" style="color:#0000FF;text-align:left">View Larger Map</a></small>',
  street_view: '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=University+of+Pittsburgh,+4200+Fifth+Ave,+Pittsburgh,+PA&amp;aq=0&amp;oq=uni&amp;sll=40.444261,-79.970448&amp;sspn=0.142165,0.251656&amp;ie=UTF8&amp;hq=University+of+Pittsburgh,+4200+Fifth+Ave,+Pittsburgh,+PA&amp;ll=40.443486,-79.958245&amp;spn=0.010798,0.021038&amp;t=m&amp;layer=c&amp;cbll=40.441775,-79.956671&amp;panoid=BgWQKMiSI1fpwP7JUF16Vg&amp;cbp=12,45.25,,0,-3.34&amp;output=svembed"></iframe><br /><small><a href="https://maps.google.com/maps?f=q&amp;source=embed&amp;hl=en&amp;geocode=&amp;q=University+of+Pittsburgh,+4200+Fifth+Ave,+Pittsburgh,+PA&amp;aq=0&amp;oq=uni&amp;sll=40.431368,-79.9805&amp;sspn=0.142165,0.251656&amp;ie=UTF8&amp;hq=University+of+Pittsburgh,+4200+Fifth+Ave,+Pittsburgh,+PA&amp;ll=40.443486,-79.958245&amp;spn=0.010798,0.021038&amp;t=m&amp;layer=c&amp;cbll=40.441775,-79.956671&amp;panoid=BgWQKMiSI1fpwP7JUF16Vg&amp;cbp=12,45.25,,0,-3.34" style="color:#0000FF;text-align:left">View Larger Map</a></small>',
  housing: '<iframe src="http://www.tour.pitt.edu/tour-categories/residence-halls" scrolling="no" height="820" width="900"></iframe>',
  food: '<div style="overflow: hidden; max-width: 1000px;"><iframe scrolling="no" src="http://www.pc.pitt.edu/dining/menus.php" style="margin-left: 0; height: 2000px; margin-top: -370px; margin-bottom: -375px; width: 750px;"></iframe></div>',
  weather: '<div style="overflow: hidden; max-width: 500px;"><iframe scrolling="no" src="http://www.weather.com/weather/wxclimatology/monthly/graph/USPA1290" style="margin-left: -40px; height: 1570px; margin-top: -490px; width: 500px;"></iframe></div>',
  dorms: 'Towers (fr.)|Sutherland (fr.)|Lothrop (fr.)|Nordenberg (fr.)|Holland (fr.)|Forbes (fr.; honors)|McCormick|Brackenridge|Amos|Bruce|Panther|Pennsylvania|Ruskin|Bouquet Gardens|Oakwood Apartments|Centre-Plaza Apartments|Forbes-Craig Apartments (honors)',
  science_majors: 'Actuarial Mathematics|Applied Mathematics|Bioinformatics|Biological Sciences|Chemistry*|Computer Science|Ecology and Evolution|Environmental Geology|Environmental Studies|Geology|History and Philosophy of Science|Linguistics*|Mathematics*|Mathematics-Economics|Mathematics-Philosophy|Microbiology|Molecular Biology|Natural Sciences Area|Neuroscience*|Physics*|Physics and Astronomy|Scientific Computing|Statistics',
  social_science_majors: 'Economics*|Economics–Statistics|History*|Political Science*|Politics-Philosophy|Psychology|Sociology*|Urban Studies',
  humanities_majors: 'Africana Studies*|Africana Studies–English|Anthropology|Chinese*|Classics*|English Literature*|English Writing|Film Studies*|French*|German|History of Art & Architecture|Humanities Area|Italian*|Italian Studies|Japanese*|Liberal Studies|Music*|Philosophy*|Religious Studies*|Russian|Slavic Studies|Spanish|Studio Arts*|Theatre Arts*',
  business_majors: 'Accounting|Finance|General Management|Global Management|Marketing',
  engineering_majors: 'Bioengineering*|Chemical Engineering*|Civil Engineering*|Computer Engineering|Electrical Engineering*|Engineering Physics|Industrial Engineering*|Materials Science and Engineering*|Mechanical Engineering*',
  professional_majors: 'Administration of Justice*|Applied Developmental Psychology|Architectural Studies|Athletic Training|Clinical Dietetics and Nutrition|Communication: Rhet & Comm|Communication Science|Dental Hygiene|Emergency Medicine|Health Information Management|Health Services|Health and Physical Activity|Information Science|Legal Studies*|Media and Professional Communications|Nursing|Pharmacy|Public Service*|Rehabilitation Science|Social Work*'
)
else
  # handle validation errors
end

@college= College.find_or_create_by_name!('Harvard')
if @college.update_attributes(
  url: 'harvard',
  public: 'Private',
  years: '4-year',
  category: 'National University',
  calendar: 'Semester',
  location: 'Cambridge, MA',
  setting: 'Small City (50-250k)',
  degrees: 'Bachelors, Masters, Doctoral',
  rotc: 'Air Force, Army, Navy',
  considered: 'Academic GPA|Alumni Relation|Application Essay|Character/Personal Qualities|Extracurricular Activities|First Generation College Student|Geographical Residence|Interview|Racial/Ethnic Status|Recommendations|Rigor of secondary school record|Standardized Test Scores|Talent/Ability|Volunteer Work|Work Experience',
  student_faculty: '7 to 1')
else
  # handle validation errors
end

我忽略了所有其他学院,目前我已经有200所学校的数据,但是只有匹兹堡大学具有全面的数据。其他学校只有基本的数据,例如哈佛大学。出于简洁起见,我也省略了一些匹兹堡大学的数据,但如果您希望我包含其余部分,请告诉我。

编辑:我尝试删除从science_majors开始的所有*,然后运行rake db:seed,但这没有改变任何东西。

6个回答

204

由于您添加的日期中可能包含先前数据种子中不存在的新字符(可能是在HTML实体编码映射URL中),所以您会收到编码错误。您的文件系统未配置为对其进行编码,因此出现了这个错误。

以下操作将通过在您的计算机上设置UTF-8语言环境来解决此错误:

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

设置系统语言环境的好处是,以后所有的gem都将使用UTF-8编码进行捆绑。

编辑:

或者,如果您不想更改系统编码,可以通过在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

1
很高兴能够帮到你。如果你采用这种方法,请确保所有部署项目的环境都将其区域设置为UTF-8。否则,你需要在文件顶部添加#encoding utf-8以确保它被正确编码,正如其他帖子中所指出的那样。 - zeantsoi
我实际上尝试删除#encoding utf-8并按照您说的做,但它没有起作用。〜/ collegeanswers >> export LANG = en_US.UTF-8 〜/ collegeanswers >> export LC_ALL =“en_US.UTF-8” 〜/ collegeanswers >> bundle 使用耙(10.0.4) 。 。 使用i18n(0.6.1) 您的bundle已完成!使用bundle show [gemname]查看捆绑的gem安装在哪里。 〜/ collegeanswers >> rake db:seed 耙中止了!你知道为什么吗? - Adam Zerner
我的Rake错误是invalid byte sequence in US-ASCII (ArgumentError)。我刚刚尝试了你说的方法,但还是出现了同样的错误。 - Adam Zerner
我不知道那是什么意思。我一直在使用Control+C在rails s之间来回切换。 - Adam Zerner
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/31547/discussion-between-adam-zerner-and-zeantsoi - Adam Zerner
显示剩余3条评论

66

添加

#encoding: utf-8

在文件顶部


为什么现在我需要这样做,但以前我不需要? - Adam Zerner
我刚试了一下,顺便说一句,它起作用了,谢谢!但我仍然想知道为什么以前一切都正常,现在我需要添加才能使其工作。 - Adam Zerner
1
也许您在文件中添加了一些特殊的UTF-8符号.. :) - BvuRVKyUVlViVIc7
我刚刚仔细阅读了另一个人的回答更新,发现它更加全面,所以我会将其标记为被接受的答案,但是我仍然非常感谢你的帮助! - Adam Zerner

41

您还可以尝试如下命令:export RUBYOPT="-KU -E utf-8:utf-8",该命令在这个GH thread中提到。


谢谢!只有这个能够帮助解决问题:shotgun-0.9.1/bin/shotgun:94:in `[]': invalid byte sequence in US-ASCII (ArgumentError) - Slava V

22

如果您在Ruby Docker容器中遇到此问题,则可以设置LANG=C.UTF-8

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

或者查看此问题


如果您要创建自己的Docker镜像,请在Dockerfile中添加“ENV LANG=C.UTF-8”。请参考此答案中的链接。 - Alex

7

像其他人建议的那样,我在文件顶部(在这种情况下是seed.rb)添加了#encoding: utf-8,但仍然无法使rake任务正常工作。

有趣的是,一位朋友建议我从种子文件的顶部删除额外的、rails生成的注释,以便只剩下#encoding: utf-8

你知道吗——它成功了。


1

您的种子文件中存在无效字符,在我的情况下,将单引号从`更改为'即可解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接