2013-06-10 8 views
71

Quando eseguo rake db:seed nel mio Rails app, sto ottenendo questo errore:valida sequenza di byte in US-ASCII (argomento Errore) quando corro rake db: seme in Rails

invalid byte sequence in US-ASCII (Argument Error)

Ho appena aggiunto science_majors e giù per il mio file del seme, e ora quando corro rake db:seed mi dà questo errore:

invalid byte sequence error

Perché è questo, e come posso risolvere il problema?

parte di 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 

ho lasciato fuori tutti gli altri collegi. Finora ho 200 scuole, ma Pitt è l'unico con dati completi finora. Il resto ha solo dati di base, come Harvard. Ho anche escluso alcuni dati per Pitt per brevità, ma fammi sapere se vuoi che io includa il resto.

Modifica: ho provato a estrarre lo * su science_majors e quindi su rake db:seed, ma non ha modificato nulla.

risposta

145

Stai ricevendo un errore di codifica perché il tuo filesystem non è configurato per codificare la data che hai aggiunto (poiché presumibilmente include nuovi caratteri - probabilmente nel tuo URL della mappa con codifica HTML entità - che non esisteva nel tuo seme di dati precedenti).

Di seguito vi dovrebbe risolvere questo errore impostando il locale UTF-8 sulla vostra macchina:

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

Il vantaggio di fissare un impostazioni internazionali del sistema è che tutte le gemme (andando avanti) saranno in bundle con UTF- 8 codifica.

EDIT:

In alternativa, se non si desidera cambiare la codifica del sistema, è possibile impostare la codifica progetto di ampio specificando uno standard di codifica nella vostra 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

Felice di averlo aiutato. Se segui questo approccio, assicurati che tutti gli ambienti in cui hai distribuito il tuo progetto abbiano le loro impostazioni locali impostate su UTF-8. Altrimenti, dovrai aggiungere '#encoding utf-8' all'inizio del tuo file per assicurarti che sia codificato correttamente, come sottolinea astutamente l'altro poster. – zeantsoi

+0

in realtà ho appena provato a rimuovere #encoding utf-8 e fare ciò che hai detto ma non ha funzionato. '~/collegeanswers >> export LANG = en_US.UTF-8 ~/collegeanswers >> export LC_ALL = "en_US.UTF-8" ~/collegeanswers >> fascio Uso rake (10.0.4) . . Utilizzo di i18n (0.6.1) Il pacchetto è completo! Usa 'bundle show [gemname]' per vedere dove è installato un gem in bundle. ~/collegeanswers >> rake db: seed rake abortito! ' sai perché? –

+0

Qual è il tuo errore 'rake'? Probabilmente le tue gem esistenti non sono ancora configurate per funzionare con la codifica del filesystem. Prova 'bbundle exec gem uninstall uninstall i18n', quindi' bundle install'. – zeantsoi

61

Aggiungere

#encoding: utf-8 

nella parte superiore del file

+0

Perché devo farlo adesso, ma prima non dovevo? –

+0

L'ho appena provato e ha funzionato molto bene, quindi grazie! Ma mi piacerebbe ancora sapere perché tutto funzionava prima, e ora ho bisogno di aggiungere che funzioni. –

+1

Forse hai aggiunto alcuni segni speciali utf-8 nel tuo file .. :) – Lichtamberg

7

Come altri hanno suggerito, ho Adde d #encoding: utf-8 all'inizio del mio file (in questo caso, seed.rb), ma ancora non riuscivo a far funzionare il rake.

È interessante notare che un amico mi ha raccomandato di rimuovere i commenti aggiuntivi generati dai binari dalla parte superiore del file seme in modo che fosse rimasto solo #encoding: utf-8.

Cosa vorresti sapere: ha funzionato.

1

C'è un carattere non valido nel file seme, nel mio caso modificare le virgolette singole da "a" risolto.

21

Si può anche provare export RUBYOPT="-KU -E utf-8:utf-8" di cui alla presente GH thread

+0

Grazie! Questa è l'unica cosa che mi ha aiutato con '' 'shotgun-0.9.1/bin/shotgun: 94: in' [] ': sequenza di byte non valida in US-ASCII (ArgumentError) '' ' –

+0

Risposta impressionante. Aggiunto questo e ha funzionato. – adev

10

Se si hanno questo problema in un contenitore finestra mobile rubino si potrebbe entrambi i set LANG=C.UTF-8:

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

o watch this issue.