2013-06-10 44 views
71

Khi tôi chạy rake db:seed trong ứng dụng Rails của tôi, tôi nhận được lỗi này:không hợp lệ chuỗi byte trong US-ASCII (Argument Lỗi) khi tôi chạy rake db: hạt giống trong Rails

invalid byte sequence in US-ASCII (Argument Error)

Tôi chỉ cần thêm science_majors và xuống để tập hạt giống của tôi, và bây giờ khi tôi chạy rake db:seed nó mang lại cho tôi lỗi này:

invalid byte sequence error

tại sao điều này, và làm thế nào tôi có thể sửa chữa nó?

phần của 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 

tôi rời ra tất cả các trường cao đẳng khác. Tôi có 200 trường học cho đến nay, nhưng Pitt là người duy nhất có dữ liệu toàn diện cho đến nay. Phần còn lại chỉ có dữ liệu cơ bản, như Harvard. Tôi cũng để lại một số dữ liệu cho Pitt vì lợi ích ngắn gọn, nhưng hãy cho tôi biết nếu bạn muốn tôi bao gồm phần còn lại.

Chỉnh sửa: Tôi đã thử đưa ra số * trong số science_majors xuống dưới và sau đó chạy rake db:seed, nhưng điều đó không thay đổi bất cứ điều gì.

Trả lời

145

Bạn nhận được lỗi mã hóa vì hệ thống tệp của bạn không được định cấu hình để mã hóa ngày bạn đã thêm (vì có thể bao gồm các ký tự mới - có thể trong thực thể HTML được mã hóa URL bản đồ của bạn - không tồn tại trong hạt giống dữ liệu trước).

Dữ liệu sau sẽ phải giải quyết lỗi này bằng cách thiết lập locale UTF-8 trên máy tính của bạn:

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

Lợi ích của việc thiết lập một miền địa phương hệ thống là tất cả đá quý (đi về phía trước) sẽ được đóng gói bằng UTF- 8 mã hóa.

EDIT:

Ngoài ra, nếu bạn không muốn thay đổi mã hóa hệ thống của bạn, bạn có thể thiết lập mã hóa của bạn dự án rộng bằng cách xác định một tiêu chuẩn mã hóa trong Gemfile của bạn:

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

Rất vui khi được trợ giúp. Nếu bạn sử dụng phương pháp này, hãy đảm bảo rằng tất cả các môi trường mà bạn đã triển khai dự án của mình để đặt ngôn ngữ của chúng thành UTF-8. Nếu không, bạn sẽ cần phải thêm '#encoding utf-8' vào đầu tệp để đảm bảo rằng tệp được mã hóa đúng cách, như áp phích khác đã lưu ý một cách nghiêm túc. – zeantsoi

+0

tôi thực sự chỉ cố gắng để loại bỏ #encoding utf-8 và làm những gì bạn nói nhưng nó không hoạt động. '~/collegeanswers >> xuất khẩu LANG = en_US.UTF-8 ~/collegeanswers >> xuất khẩu LC_ALL = "en_US.UTF-8" ~/collegeanswers >> bó dùng cào (10.0.4) . . Sử dụng i18n (0.6.1) Gói của bạn đã hoàn tất! Sử dụng 'bundle show [gemname]' để xem vị trí gem đã được cài đặt. ~/collegeanswers >> rake db: hạt giống rake hủy bỏ! ' bạn có biết tại sao không? –

+0

Lỗi 'rake' của bạn là gì? Đá quý hiện tại của bạn có lẽ không được cấu hình để làm việc với mã hóa hệ thống tập tin của bạn được nêu ra. Hãy thử 'bbundle exec gem uninstall i18n', sau đó' bundle install'. – zeantsoi

61

Thêm

#encoding: utf-8 

ở phía trên cùng của tập tin

+0

Tại sao tôi cần làm điều đó ngay bây giờ, nhưng trước khi tôi không phải làm vậy? –

+0

Tôi chỉ thử nó và nó đã làm việc btw, vì vậy cảm ơn! Nhưng tôi vẫn muốn biết tại sao mọi thứ đã hoạt động trước đây, và bây giờ tôi cần phải thêm vào đó để nó hoạt động. –

+1

Có thể bạn đã thêm một số dấu hiệu utf-8 đặc biệt vào tệp của mình .. :) – Lichtamberg

7

Giống như những người khác đề nghị, tôi adde d #encoding: utf-8 lên đầu tệp (seed.rb) của tôi (nhưng trong trường hợp này là tệp seed.rb) nhưng vẫn không thể nhận nhiệm vụ cào làm việc.

Điều thú vị là đủ, một người bạn khuyên tôi nên xóa các nhận xét bổ sung, tạo đường ray từ đầu tệp hạt giống để chỉ còn lại #encoding: utf-8.

Bạn sẽ biết điều gì - nó hoạt động.

1

Có một ký tự không hợp lệ trong tệp hạt giống của bạn, trong trường hợp của tôi, việc thay đổi dấu nháy đơn từ `thành 'được tính toán.

21

Bạn cũng có thể thử export RUBYOPT="-KU -E utf-8:utf-8" như đã đề cập trong này GH thread

+0

Cảm ơn! Đó là điều duy nhất đã giúp với '' 'shotgun-0.9.1/bin/shotgun: 94: trong' [] ': chuỗi byte không hợp lệ trong US-ASCII (ArgumentError) '' ' –

+0

Câu trả lời thú vị. Thêm vào này và nó đã làm việc. – adev

10

Nếu bạn đang gặp vấn đề này trong một container Docker ruby ​​bạn có thể thiết lập một trong hai LANG=C.UTF-8:

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

hoặc watch this issue.