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_major,现在当我运行rake db:seed时,它给了我一个错误:
invalid byte sequence error
无效的字节序列错误
Why is this, and how can I fix it?
这是为什么呢?我该怎么解决呢?
part of seeds.rb
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&source=s_q&hl=en&geocode=&q=university+of+pittsburgh&aq=&sll=40.444261,-79.970448&sspn=0.142165,0.251656&ie=UTF8&hq=university+of+pittsburgh&t=m&ll=40.443486,-79.958245&spn=0.010798,0.021038&output=embed"></iframe><br /><small><a href="https://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=university+of+pittsburgh&aq=&sll=40.431368,-79.9805&sspn=0.142165,0.251656&ie=UTF8&hq=university+of+pittsburgh&t=m&ll=40.443486,-79.958245&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&source=embed&hl=en&geocode=&q=University+of+Pittsburgh,+4200+Fifth+Ave,+Pittsburgh,+PA&aq=0&oq=uni&sll=40.444261,-79.970448&sspn=0.142165,0.251656&ie=UTF8&hq=University+of+Pittsburgh,+4200+Fifth+Ave,+Pittsburgh,+PA&ll=40.443486,-79.958245&spn=0.010798,0.021038&t=m&layer=c&cbll=40.441775,-79.956671&panoid=BgWQKMiSI1fpwP7JUF16Vg&cbp=12,45.25,,0,-3.34&output=svembed"></iframe><br /><small><a href="https://maps.google.com/maps?f=q&source=embed&hl=en&geocode=&q=University+of+Pittsburgh,+4200+Fifth+Ave,+Pittsburgh,+PA&aq=0&oq=uni&sll=40.431368,-79.9805&sspn=0.142165,0.251656&ie=UTF8&hq=University+of+Pittsburgh,+4200+Fifth+Ave,+Pittsburgh,+PA&ll=40.443486,-79.958245&spn=0.010798,0.021038&t=m&layer=c&cbll=40.441775,-79.956671&panoid=BgWQKMiSI1fpwP7JUF16Vg&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. 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.
我把其他所有的大学都开除了。到目前为止,我有200所学校,但皮特是目前唯一拥有全面数据的学校。剩下的只有基本的数据,比如哈佛。为了简洁起见,我还遗漏了一些皮特的数据,但请告诉我,如果你想让我把剩下的部分包括进去的话。
Edit: I tried taking out the *
's in science_majors
on down and then running rake db:seed
, but that didn't change anything.
编辑:我试着把科学专业的*'s去掉,然后运行rake db:seed,但这并没有改变什么。
6 个解决方案
#1
147
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
#2
61
Add
添加
#encoding: utf-8
at the top of the file
在文件的顶部
#3
22
You can also try export RUBYOPT="-KU -E utf-8:utf-8"
as mentioned in this GH thread
您也可以尝试导出RUBYOPT=“-KU -E utf-8:utf-8”,正如在这个GH线程中提到的
#4
10
If you are having this problem in a ruby docker container you might either set LANG=C.UTF-8
:
如果您在ruby docker容器中遇到这个问题,您可以设置LANG=C.UTF-8:
docker run -it --rm -e LANG=C.UTF-8 ruby ...
or watch this issue.
或者看这个问题。
#5
7
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添加到我的(本例中是setup .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生成的注释,从种子文件的顶部,这样只有#编码:utf-8保留。
What would you know - it worked.
你会知道什么——它起作用了。
#6
1
There is a invalid character in your seed file, in my case changing single quotes from ` to ' worked out.
在你的种子文件中有一个无效的字符,在我的例子中把单引号从'改为'算出来了。
#1
147
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
#2
61
Add
添加
#encoding: utf-8
at the top of the file
在文件的顶部
#3
22
You can also try export RUBYOPT="-KU -E utf-8:utf-8"
as mentioned in this GH thread
您也可以尝试导出RUBYOPT=“-KU -E utf-8:utf-8”,正如在这个GH线程中提到的
#4
10
If you are having this problem in a ruby docker container you might either set LANG=C.UTF-8
:
如果您在ruby docker容器中遇到这个问题,您可以设置LANG=C.UTF-8:
docker run -it --rm -e LANG=C.UTF-8 ruby ...
or watch this issue.
或者看这个问题。
#5
7
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添加到我的(本例中是setup .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生成的注释,从种子文件的顶部,这样只有#编码:utf-8保留。
What would you know - it worked.
你会知道什么——它起作用了。
#6
1
There is a invalid character in your seed file, in my case changing single quotes from ` to ' worked out.
在你的种子文件中有一个无效的字符,在我的例子中把单引号从'改为'算出来了。