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

Ruby on rails Rails:运行rake db:seed时US-ASCII中的字节序列无效(参数错误),ruby-on-rails,rake,seeding,argument-error,Ruby On Rails,Rake,Seeding,Argument Error,当我在我的Rails应用程序中运行rake db:seed时,我得到以下错误: US-ASCII中的字节序列无效(参数错误) 我刚刚添加了science\u-majors并向下添加到我的种子文件,现在当我运行rake db:seed时,会出现以下错误: 无效字节序列错误 为什么会这样?我该如何修复它 种子的一部分。rb @college = College.find_or_create_by_name!('University of Pittsburgh') if @college.update

当我在我的
Rails
应用程序中运行
rake db:seed
时,我得到以下错误:

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

我刚刚添加了
science\u-majors
并向下添加到我的种子文件,现在当我运行
rake db: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
@college=college.find\u或\u create\u by\u name!(“匹兹堡大学”)
if@college.update\u属性(
url:“匹兹堡大学”,
public:“public”,
年份:“4年”,
类别:“国立大学”,
日历:'学期',
地点:“宾夕法尼亚州匹兹堡”,
背景:“大城市(250-500k)”,
保留金:“90”,
专业:商业/市场营销:15%;社会科学:14%;健康专业:11%;英语:10%;工程学:9%;心理学:8%;生物学:7%;历史:5%,
奖学金链接:'http://www.oafa.pitt.edu/universityschlrs.aspx',
映射:“
”, 街景:“
”, 住房:, 食物:, 天气:'', 宿舍:塔楼(fr.)、萨瑟兰(fr.)、洛瑟罗普(fr.)、诺登堡(fr.)、荷兰(fr.)、福布斯(fr.)、荣誉(fr.)、麦考密克(McCormick)、布雷肯里奇(Brackenridge)、阿莫斯(Amos)、布鲁斯(Bruce)、黑豹(Panther)、宾夕法尼亚(Pennsylvania)、罗斯金(Ruskin)、花束花园(Bouquet Gardens)、奥克伍德公寓(Oakwood公寓)、中心广场公寓(Central Plaza公寓)、福布斯克雷格公寓(honors)、)、)、)、), 理科大学专业:精算数学|应用数学|生物信息学|生物科学|化学|计算机科学|生态学与进化|环境地质学|环境研究|地质学|科学史与哲学|语言学|数学|数学经济学|数学哲学|微生物学|分子生物学|自然科学领域|神经科学|物理学|物理学和天文学|科学计算|统计学|, 社会科学专业:“经济学*”经济学-统计学*”历史*”政治学*”政治哲学*”心理学*”社会学*”城市研究“, 人文专业:非洲研究*非洲研究-英语|人类学|中文|经典|英国文学|英语写作|电影研究|法语|德语|艺术与建筑史|人文领域|意大利语|意大利语|文科|音乐|哲学|宗教研究|俄语|斯拉夫语研究|西班牙语|艺术工作室|剧院艺术*', 商科|大学专业:“会计|金融|综合管理|全球管理|营销”, 工程学专业:“生物工程*、化学工程*、土木工程*、计算机工程*、电气工程*、工程物理*、工业工程*、材料科学与工程*、机械工程*”, 专业|大学专业:司法|应用发展心理学|建筑研究|运动训练|临床营养学和营养学|传播:Rhet&Comm |传播科学|牙科卫生|急救医学|健康信息管理|卫生服务|健康和体力活动|信息科学|法律研究|M媒体与专业传播|护理|药学|公共服务|康复科学|社会工作| | ) 其他的 #处理验证错误 结束 @学院=学院。按名称查找或创建!(‘哈佛’) if@college.update\u属性( 网址:'哈佛', 公营:"私营",, 年份:“4年”, 类别:“国立大学”, 日历:'学期', 地点:马萨诸塞州剑桥市, 环境:“小城市(50-250k)”, 学位:“学士、硕士、博士”, 后备军官训练团:“空军、陆军、海军”, 考虑:“学业平均成绩|校友关系|申请论文|性格/个人素质|课外活动|第一代大学生|地理居住地|面试|种族/民族地位|建议|中学成绩严谨|标准化考试分数|人才/能力|志愿工作|工作经验”, 学生(教职员工:"7比1") 其他的 #处理验证错误 结束
我忽略了所有其他的大学。到目前为止,我有200所学校,但皮特是唯一一所拥有全面数据的学校。其他人只有基本数据,比如哈佛大学。为了简洁起见,我还遗漏了皮特的一些数据,但是如果你想让我包括其余的数据,请告诉我


编辑:我试着在
科学专业
中去掉
*
,然后运行
rake db:seed
,但这没有改变任何事情。

您收到一个编码错误,因为您的文件系统没有配置为对添加的日期进行编码(因为它可能包含了以前的数据种子中不存在的新字符——可能在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编码绑定

编辑:

或者,如果不想更改系统编码,可以通过在文件中指定编码标准来设置整个编码项目:

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


在文件的顶部

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

有趣的是,一位朋友建议我从种子文件的顶部删除rails生成的附加注释,以便只保留
#encoding:utf-8


你知道什么-它起作用了。

在我的例子中,在种子文件中有一个无效字符,将单引号从“到”改为“起作用了”。

你也可以尝试
export RUBYOPT=“-KU-E utf-8:utf-8”

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

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

或者。

为什么我现在需要这样做,但之前我不需要这样做?我只是尝试了一下,顺便说一句,它成功了,谢谢!但我仍然想知道为什么以前一切都正常,现在我需要添加它才能正常工作。也许你添加了一些规范
docker run -it --rm -e LANG=C.UTF-8 ruby ...