突然ArgumentError:播种时UTF-8中的无效字节序列

时间:2017-07-05 05:25:32

标签: ruby-on-rails ruby utf-8 seeding

在处理我的Rails应用程序时,我正在尝试某种方式来裁剪图像,我从Rails Casts #182 开始,回到我以前的所有内容,然后再到blog post,在完成全部设置后,我决定重新启动我的数据库并播种它,但是当我尝试播放我的数据库时,我得到了ArgumentError: invalid byte sequence in UTF-8

我从未在一些小型研究之前和之后遇到过这个错误我发现这是由于某些字符无法被ruby读取,但是,在略读我的代码之后我发现之前没有任何区别我开始裁剪图像,经过进一步的检查,我还没找到原因。

我还没有学习如何阅读和跟踪rails / rake bug这也没有帮助,这就是我得到的:

rails aborted!
ArgumentError: invalid byte sequence in UTF-8
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/object/blank.rb:118:in `==='
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/core_ext/object/blank.rb:118:in `blank?'
/usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/sanitized_file.rb:131:in `is_path?'
/usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/sanitized_file.rb:97:in `size'
/usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/sanitized_file.rb:140:in `empty?'
/usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/uploader/cache.rb:120:in `cache!'
/usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/mounter.rb:44:in `block in cache'
/usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/mounter.rb:42:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/mounter.rb:42:in `map'
/usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/mounter.rb:42:in `cache'
/usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/mount.rb:300:in `main_image='
/usr/local/rvm/gems/ruby-2.3.0/gems/carrierwave-1.1.0/lib/carrierwave/orm/activerecord.rb:70:in `main_image='
/usr/local/rvm/gems/ruby-2.3.0/gems/activemodel-5.0.1/lib/active_model/attribute_assignment.rb:46:in `public_send'
/usr/local/rvm/gems/ruby-2.3.0/gems/activemodel-5.0.1/lib/active_model/attribute_assignment.rb:46:in `_assign_attribute'
/usr/local/rvm/gems/ruby-2.3.0/gems/activemodel-5.0.1/lib/active_model/attribute_assignment.rb:40:in `block in _assign_attributes'
/usr/local/rvm/gems/ruby-2.3.0/gems/activemodel-5.0.1/lib/active_model/attribute_assignment.rb:39:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/activemodel-5.0.1/lib/active_model/attribute_assignment.rb:39:in `_assign_attributes'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.0.1/lib/active_record/attribute_assignment.rb:26:in `_assign_attributes'
/usr/local/rvm/gems/ruby-2.3.0/gems/activemodel-5.0.1/lib/active_model/attribute_assignment.rb:33:in `assign_attributes'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.0.1/lib/active_record/core.rb:319:in `initialize'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.0.1/lib/active_record/inheritance.rb:65:in `new'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.0.1/lib/active_record/inheritance.rb:65:in `new'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.0.1/lib/active_record/persistence.rb:50:in `create!'
/home/ubuntu/workspace/db/seeds.rb:60:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `block in load'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:287:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.1/lib/rails/engine.rb:549:in `load_seed'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.0.1/lib/active_record/tasks/database_tasks.rb:268:in `load_seed'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-5.0.1/lib/active_record/railties/databases.rake:196:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.1/lib/rails/commands/rake_proxy.rb:14:in `block in run_rake_task'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.1/lib/rails/commands/rake_proxy.rb:11:in `run_rake_task'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.1/lib/rails/commands/commands_tasks.rb:51:in `run_command!'
/usr/local/rvm/gems/ruby-2.3.0/gems/railties-5.0.1/lib/rails/commands.rb:18:in `<top (required)>'
/home/ubuntu/workspace/bin/rails:9:in `require'
/home/ubuntu/workspace/bin/rails:9:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/client/rails.rb:28:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/client/rails.rb:28:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/client/command.rb:7:in `call'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/client.rb:30:in `run'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/bin/spring:49:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `load'
/usr/local/rvm/gems/ruby-2.3.0/gems/spring-1.7.2/lib/spring/binstub.rb:11:in `<top (required)>'
/home/ubuntu/workspace/bin/spring:15:in `require'
/home/ubuntu/workspace/bin/spring:15:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

我的主要问题是这个错误究竟发生在哪里?到目前为止,我已经通过经验了解到它可能是seeds.rb或模型(在这种情况下是event.rb)但是我没有看到任何不同的东西,我之前都让它们都工作过,我是甚至能够上传图像并播种它们没有问题,直到我实现并从Rails Cast中删除代码并在博客文章中实现代码,任何进入解决方案的方向都会有很大的帮助。

这是该模型的种子,事先工作正常。

Event.create!(
    name: "  1", 
    spanish_description: "El mejor programa para este sábado con los peques ! #kids #gratis #petfriendly. En Kuchitril. De 6 a 10 pm. #localmarket #tendencias #buenavibra. #corranlavoz!",
    english_description: "El mejor programa para este sábado con los peques ! #kids #gratis #petfriendly. En Kuchitril. De 6 a 10 pm. #localmarket #tendencias #buenavibra. #corranlavoz!",
    place_name: "mamita's",
    place_type: "beach club",
    address: "Calle 28 Norte Mza 10, Lote 8, Centro", 
    county: 'meh',
    city: "Playa del Carmen",
    state: "Quintana Roo",
    postal_code: 77710,

    user_id: 1,
    main_image: File.open(File.join(Rails.root, 'test.jpg')),
    sub_images: [File.open(File.join(Rails.root, 'chinchi.jpg'))],


    eventdates_attributes:[{ 
      start_date: Time.now.in_time_zone("Eastern Time (US & Canada)"),
      finish_date: Time.now.in_time_zone("Eastern Time (US & Canada)"),
      start_hour: "08:00am",
      finish_hour: "11:00am",
    }],

    tickets_attributes:[{ 
      ticket_type: "Solo una vez",
      price: 100,
      currency: "mxn",
      quantity: 150,
      description: "Un boleto muy divertido!"
    }, { 
      ticket_type: "Otra vez",
      price: 150,
      currency: "mxn",
      quantity: 150,
      description: "Un boleto muy divertido!"
    }]
)

重要编辑:所以我决定回到之前的提交只是为了不再处理这个问题,并且由于某种原因错误不断出现,这让我相信存在问题使用我正在使用的平台,也就是Cloud9,我也读过这个不应该在Rails 5.0上发生的问题

3 个答案:

答案 0 :(得分:3)

我找到问题的原因,我使用mount_uploaders进行单一上传,使上传成为一个数组,从而产生错误。将其更改为mount_uploader是解决方案,您必须要注意这些细节,以及所吸取的经验教训。我建议关闭这个问题,除非它对新手来说有点用处。

答案 1 :(得分:1)

不确定,这只是请求网址中的无效字节序列,请尝试以下方法:

在运行gem 'rack-utf8_sanitizer'之后添加到您的gemfile gem 'handle_invalid_percent_encoding_requests'gem 'utf8-cleaner'bundle install

答案 2 :(得分:0)

mount_uploaders需要一个数组,因此应将您的seed.rb文件设置为接受数组。

它应该看起来像这样:Model.create(images: [image1.jpg])

不是这样的:Model.create(images: image1.jpg)