【问题标题】:Rails: Invalid byte sequence in US-ASCII (Argument Error) when I run rake db:seedRails:当我运行 rake db:seed 时,US-ASCII 中的字节序列无效(参数错误)
【发布时间】:2013-06-06 13:37:08
【问题描述】:

当我在我的 Rails 应用程序中运行 rake db:seed 时,我收到了这个错误:

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

我刚刚将science_majors 添加到我的种子文件中,现在当我运行rake db:seed 时,它给了我这个错误:

无效字节序列错误

为什么会这样,我该如何解决?

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

我忽略了所有其他大学。到目前为止,我有 200 所学校,但 Pitt 是迄今为止唯一拥有全面数据的学校。其余的只有基本数据,比如哈佛。为简洁起见,我还为 Pitt 省略了一些数据,但如果您希望我包含其余数据,请告诉我。

编辑:我尝试在science_majors 中取出*,然后运行rake db:seed,但这并没有改变任何东西。

【问题讨论】:

    标签: ruby-on-rails rake seeding argument-error


    【解决方案1】:

    您收到一个编码错误,因为您的文件系统未配置为对您添加的日期进行编码(因为它可能包含新字符 - 可能在您的 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 编码进行捆绑。

    编辑:

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

    【讨论】:

    • 很高兴它有帮助。如果您采用这种方法,请确保您已部署项目的所有环境都将其语言环境设置为 UTF-8。否则,您需要将#encoding utf-8 添加到文件的顶部,以确保它被正确编码,正如另一张海报敏锐地指出的那样。
    • 我实际上只是尝试删除#encoding utf-8 并按照您所说的进行操作,但没有成功。 ~/collegeanswers &gt;&gt; export LANG=en_US.UTF-8 ~/collegeanswers &gt;&gt; export LC_ALL="en_US.UTF-8" ~/collegeanswers &gt;&gt; bundle Using rake (10.0.4) . . Using i18n (0.6.1) Your bundle is complete! Use bundle show [gemname]` 查看捆绑 gem 的安装位置。 ~/collegeanswers >> rake db:seed rake aborted!`你知道为什么吗?
    • 您的rake 错误是什么?您现有的 gem 可能尚未配置为使用您的文件系统编码。试试bbundle exec gem uninstall i18n,然后是bundle install
    • 我的 rake 错误是 invalid byte sequence in US-ASCII (ArgumentError) 。我刚试了你说的,还是一样的错误。
    • 您是否重新启动了您的网络服务器?更新了我的答案,以提供更多选项来完成您想要实现的目标。
    【解决方案2】:

    添加

    #encoding: utf-8
    

    在文件的顶部

    【讨论】:

    • 为什么我现在需要这样做,而以前我不必这样做?
    • 我刚试了一下,顺便说一句,谢谢!但我仍然想知道为什么以前一切正常,现在我需要添加它才能使其正常工作。
    • 也许您在文件中添加了一些特殊的 utf-8 符号.. :)
    • 我刚刚阅读了另一个人的答案的更新,它实际上更全面,所以我会将其标记为已接受的答案,但我仍然非常感谢你的帮助!
    【解决方案3】:

    你也可以试试export RUBYOPT="-KU -E utf-8:utf-8" 正如GH thread 中提到的那样

    【讨论】:

    • 谢谢!这是对shotgun-0.9.1/bin/shotgun:94:in `[]': invalid byte sequence in US-ASCII (ArgumentError) 的唯一帮助
    【解决方案4】:

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

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

    watch this issue

    【讨论】:

    • 或如本答案中的链接所示,如果创建您自己的 Docker 映像,请将“ENV LANG=C.UTF-8”添加到您的 Dockerfile
    【解决方案5】:

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

    有趣的是,一位朋友建议我从种子文件的顶部删除额外的、由 rails 生成的 cmets,这样就只剩下 #encoding: utf-8

    你会知道什么 - 它有效。

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多