carrierwaveuploader / carrierwave

Classier solution for file uploads for Rails, Sinatra and other Ruby web frameworks
https://github.com/carrierwaveuploader/carrierwave
8.78k stars 1.66k forks source link

Multiple files upload failed with Rails 4.2.5 and MySQL 5.7.9, MySQL2 0.4.2 #1794

Closed dexterouschen closed 8 years ago

dexterouschen commented 8 years ago

I've tried to follow https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads (which means I'm using master branch from Github) and produce the same effect but it seemed that uploaded files are not serialized correctly, thus the invalid JSON error. Both create and update won't work.

ActiveRecord::StatementInvalid (Mysql2::Error: Invalid JSON text: "Invalid value." at position 1 in value (or column) '---
- lebow_hall.jpg
- lebow_Lobby.jpg
'.: UPDATE `albums` SET `photos` = '---\n- lebow_hall.jpg\n- lebow_Lobby.jpg\n', `updated_at` = '2015-12-12 00:27:54' WHERE `albums`.`id` = 25):
app/controllers/albums_controller.rb:44:in `block in update'
app/controllers/albums_controller.rb:43:in `update'

Rendered /Users/wholeren/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (7.3ms)
Rendered /Users/wholeren/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.5ms)
Rendered /Users/wholeren/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.6ms)
Rendered /Users/wholeren/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (57.1ms)
Cannot render console with content type multipart/form-dataAllowed content types: [#<Mime::Type:0x007fe73217b468 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0x007fe7321783d0 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x007fe732152f18 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]

class Album < ActiveRecord::Base
   mount_uploaders :photos, PhotoUploader
   belongs_to :albumable, polymorphic: true
end

class AlbumsController < ApplicationController
  before_action :set_album, only: [:show, :edit, :update, :destroy]

...

private
  def set_album
    @album = Album.find(params[:id])
  end
  def album_params
    params.require(:album).permit(:name, :albumable_id, :albumable_type, { photos: [] })
  end
end

<%= form_for(@album) do |f| %>
...
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :photos %><br>
    <%= f.file_field :photos, multiple: true %>
  </div>
  <div class="field">
    <%= f.label :albumable_id %><br>
    <%= f.text_field :albumable_id %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

my gem setup

gem 'rails', '4.2.5']
gem 'activerecord-deprecated_finders', require: 'active_record/deprecated_finders'
gem 'active_model_serializers', github: 'rails-api/active_model_serializers'
gem "activeuuid"
gem 'mysql2', '>= 0.3.13', '< 0.5'
gem 'sass-rails', '~> 5.0'
gem 'compass-rails', github: 'Compass/compass-rails'
gem 'chosen-rails'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'jbuilder', '~> 2.0'
gem 'jquery-ui-rails'
gem 'bcrypt'
gem 'passenger'
gem 'kaminari'
gem 'ruby-pinyin', require: 'ruby-pinyin'
gem 'rest-client', github: 'rest-client/rest-client', require: 'rest-client'
gem 'devise'
gem 'rolify'
gem 'fuzzily'
gem 'dusen'
gem 'friendly_id'
gem 'carrierwave', github: 'carrierwaveuploader/carrierwave'
gem "mini_magick"
gem "jquery-fileupload-rails"

group :development, :test do
  gem 'byebug'
end

group :development do
  gem 'web-console', '~> 2.0'
  gem 'spring'
end
thomasfedb commented 8 years ago

@dexterouschen we require a minimal reproduction of the issue you are experiencing in order to be able to investigate the issue. Have you done any debugging at all? What have you tried? Also, please be careful when formatting issue reports, take time to make it easy for us to help you.

Feel free to request this issue be reopened when you've addressed the above.

dexterouschen commented 8 years ago

@thomasfedb I've tried to follow https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads and produce the same effect but it seemed that uploaded files are not serialized correctly, thus the invalid JSON error.

dexterouschen commented 8 years ago

@thomasfedb I wonder if I've described the issue clearly enough for it to be reopened.

thomasfedb commented 8 years ago

@dexterouschen is there a reason that you're running on the carrierwave master branch?

dexterouschen commented 8 years ago

@thomasfedb I want to try out the multi-file upload functionality so I won't have to implement an intermediate layer to support the implementation in which a model can have multiple files associated to it.

Note: You must specify using the master branch to enable this feature

Now I've already switched back to my previous solution of intermediate layer and everything works fine. But it'd be nicer to have native support of this.

thomasfedb commented 8 years ago

As we were unable to get a minimal reproduction I'm closing this issue.

pacuna commented 8 years ago

I'm having this same issue. The code:

class Report < ActiveRecord::Base
  mount_uploaders :articles_screenshots, ArticleScreenshotsUploader

  serialize :articles_screenshots  # this is a text field

end

I have a loop in which I'm setting the array of files:

   articles_screenshots << Pathname.new("path-of-file.png").open

And then when I assign and save the record:

    self.articles_screenshots = articles_screenshots
    save!

I get the error:

ActiveRecord::StatementInvalid: Mysql2::Error: Invalid JSON text: "Invalid value." at position 1 in value (or column) '---
- materiales-cocinas-encimeras.png
'.: UPDATE `reports` SET `articles_screenshots` = '---\n- materiales-cocinas-encimeras.png\n', `updated_at` = '2016-02-10 20:48:32' WHERE `reports`.`id` = 5
    from /vendored_gems/ruby/2.2.0/gems/mysql2-0.4.1/lib/mysql2/client.rb:85:in `_query'
    from /vendored_gems/ruby/2.2.0/gems/mysql2-0.4.1/lib/mysql2/client.rb:85:in `block in query'
    from /vendored_gems/ruby/2.2.0/gems/mysql2-0.4.1/lib/mysql2/client.rb:84:in `handle_interrupt'
    from /vendored_gems/ruby/2.2.0/gems/mysql2-0.4.1/lib/mysql2/client.rb:84:in `query'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:305:in `block in execute'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
    from /vendored_gems/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:305:in `execute'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/mysql2_adapter.rb:231:in `execute'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/mysql2_adapter.rb:252:in `exec_delete'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:114:in `update'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `update'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/relation.rb:88:in `_update_record'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/persistence.rb:515:in `_update_record'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/locking/optimistic.rb:79:in `_update_record'
... 19 levels...
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/transactions.rb:291:in `block in save!'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/transactions.rb:351:in `block in with_transaction_returning_status'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/transactions.rb:220:in `transaction'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
    from /vendored_gems/ruby/2.2.0/gems/activerecord-4.2.5/lib/active_record/transactions.rb:291:in `save!'
    from (irb):28
    from /vendored_gems/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/console.rb:110:in `start'
    from /vendored_gems/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/console.rb:9:in `start'
    from /vendored_gems/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /vendored_gems/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /vendored_gems/ruby/2.2.0/gems/railties-4.2.5/lib/rails/commands.rb:17:in `<top (required)>'
    from bin/rails:4:in `require'

I'm using Rails 4.2.5, MySQL 5.7.10 and the master branch for carrierwave.

Any ideas?

thomasfedb commented 8 years ago

@pacuna this issue is closed. Feel free to open a new issue if you've found a bug or need assistance.