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


87

rake db:seed在我的Rails应用中运行时,出现此错误:

US-ASCII中的无效字节序列(参数错误)

我只是添加science_majors到种子文件中,现在,当我运行rake db:seed它时,出现了以下错误:

无效的字节序列错误

为什么会这样,我该如何解决?

seed.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所学校,但是Pitt是迄今为止唯一拥有全面数据的学校。其余的只有基本数据,例如哈佛。为了简洁起见,我也为Pitt保留了一些数据,但请告知您是否要我包括其余的数据。

编辑:我尝试将*s取出science_majors,然后再运行rake db:seed,但是没有任何改变。

Answers:


181

您收到编码错误是因为您的文件系统未配置为对添加的日期进行编码(因为它可能包含新字符(可能在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 2013年

我实际上只是想删除#encoding utf-8并按照您说的做,但没有用。 ~/collegeanswers >> export LANG=en_US.UTF-8 ~/collegeanswers >> export LC_ALL="en_US.UTF-8" ~/collegeanswers >> bundle Using rake (10.0.4) . . Using i18n (0.6.1) Your bundle is complete! Use bundle show [gemname]`以查看捆绑的gem的安装位置。〜/ collegeanswers >> rake db:seed rake已中止!`你知道为什么吗?
亚当·泽纳2013年

rake怎么了 您现有的gems可能尚未配置为与文件系统编码一起使用。bbundle exec gem uninstall i18n然后尝试bundle install
zeantsoi 2013年

我的耙错误是invalid byte sequence in US-ASCII (ArgumentError) 。我只是尝试了您说的内容,但遇到了同样的错误。
亚当·泽纳2013年

您是否重新启动了网络服务器?更新了我的答案,以提供更多选项来实现您要实现的目标。
zeantsoi 2013年

64

#encoding: utf-8

在文件的顶部


为什么我现在需要这样做,而不必这样做呢?
亚当·泽纳2013年

我只是尝试了一下,而且效果很好,所以谢谢!但是我仍然想知道以前为什么一切正常,现在我需要添加它才能正常工作。
亚当·泽纳2013年

1
也许您在文件中添加了一些特殊的utf-8标志。.:)
BvuRVKyUVlViVIc7

我只是阅读了对方答案的更新,而且实际上更全面,因此我将其标记为可接受的答案,但是我仍然非常感谢您的帮助!
亚当·泽纳2013年

39

您也可以export RUBYOPT="-KU -E utf-8:utf-8" 按照此GH线程中所述进行尝试


谢谢!那是唯一有帮助的东西shotgun-0.9.1/bin/shotgun:94:in `[]': invalid byte sequence in US-ASCII (ArgumentError)
Slava V

18

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

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

观看此问题


或如本答案中的链接所示,如果创建您自己的Docker映像,则将'ENV LANG = C.UTF-8'添加到您的Dockerfile中
Alex

7

就像其他建议一样,我将文件添加#encoding: utf-8到了我的文件(在本例中为seed.rb)的顶部,但仍然无法使rake任务正常工作。

有趣的是,一位朋友建议我从种子文件的顶部删除其他由rails生成的注释,以便仅#encoding: utf-8保留这些注释。

您会知道什么-有效。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.