简体   繁体   English

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

[英]Rails: Invalid byte sequence in US-ASCII (Argument Error) when I run rake db:seed

When I run rake db:seed in my Rails app, I'm getting this error:当我在Rails应用程序中运行rake db:seed时,出现以下错误:

invalid byte sequence in US-ASCII (Argument Error) US-ASCII 中的无效字节序列(参数错误)

I just added science_majors and down to my seed file, and now when I run rake db:seed it gives me this error:我刚刚将science_majors添加到我的种子文件中,现在当我运行rake db:seed它给了我这个错误:

invalid byte sequence error无效字节序列错误

Why is this, and how can I fix it?这是为什么,我该如何解决?

part of seeds.rb种子.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

I left out all the other colleges.我遗漏了所有其他大学。 I have 200 schools so far, but Pitt is the only one with comprehensive data so far.到目前为止,我有 200 所学校,但到目前为止,皮特是唯一一所拥有全面数据的学校。 The rest just have basic data, like Harvard.其余的只有基本数据,比如哈佛。 I also left out some of the data for Pitt for the sake of brevity, but let me know if you'd like me to include the rest.为简洁起见,我还省略了 Pitt 的一些数据,但如果您希望我包括其余数据,请告诉我。

Edit: I tried taking out the * 's in science_majors on down and then running rake db:seed , but that didn't change anything.编辑:我尝试取出science_majors* ,然后运行rake db:seed ,但这并没有改变任何东西。

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).您收到编码错误,因为您的文件系统未配置为对您添加的日期进行编码(因为它可能包含新字符 - 可能在您的 HTML 实体编码地图 URL 中 - 您之前的数据种子中不存在) .

The following will should resolve this error by setting the UTF-8 locale on your machine:以下将通过在您的机器上设置 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

The benefit of setting a system locale is that all gems (going forward) will be bundled using UTF-8 encoding.设置系统区域设置的好处是所有 gems(未来)都将使用 UTF-8 编码进行捆绑。

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

您也可以尝试export RUBYOPT="-KU -E utf-8:utf-8"如本GH 线程中所述

If you are having this problem in a ruby docker container you might either set LANG=C.UTF-8 :如果您在 ruby LANG=C.UTF-8容器中遇到此问题,您可以设置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.就像其他人建议的那样,我将#encoding: utf-8添加到我的(在这种情况下,seed.rb)文件的顶部,但仍然无法让 rake 任务工作。

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.有趣的是,一位朋友建议我从种子文件的顶部删除额外的、由 rails 生成的注释,这样就只剩下#encoding: utf-8

What would you know - it worked.你会知道什么 - 它有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 rake任务因US-ASCII中的无效字节序列而失败 - rake tasks fail with invalid byte sequence in US-ASCII bundle install error“US-ASCII(ArgumentError)中的无效字节序列” - bundle install error “invalid byte sequence in US-ASCII (ArgumentError)” 将Ruby升级到1.9.2后,Rails查看错误“US-ASCII中的无效字节序列”错误 - Rails view error “invalid byte sequence in US-ASCII” error after upgrading Ruby to 1.9.2 US-ASCII whith数组中的无效字节序列 - invalid byte sequence in US-ASCII whith array MembersController中的ArgumentError#登录US-ASCII中的无效字节序列 - ArgumentError in MembersController#login invalid byte sequence in US-ASCII US-ASCII(ArgumentError)中的字节序列无效,没有任何帮助 - invalid byte sequence in US-ASCII (ArgumentError), nothing helps ArgumentError(US-ASCII中无效的字节序列):在ruby升级1.9.3中 - ArgumentError (invalid byte sequence in US-ASCII): in ruby upgrade 1.9.3 Heroku-US-ASCII中的无效字节序列(ArgumentError) - Heroku - invalid byte sequence in US-ASCII (ArgumentError) US-ASCII中的无效字节序列(Ruby 1.9 + rails 2.3.8 + mongodb + mongo_mapper) - invalid byte sequence in US-ASCII (Ruby 1.9 + rails 2.3.8 + mongodb + mongo_mapper) RVM,Ruby 1.9.2,Rails 2.3.8,Passenger和“US-ASCII中的无效字节序列” - RVM, Ruby 1.9.2, Rails 2.3.8, Passenger and “invalid byte sequence in US-ASCII”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM