上传多个图片

时间:2017-09-26 18:39:38

标签: ruby-on-rails paperclip dropzone.js

我使用dropzone.js上传多张图片时遇到问题

我收到此错误:

Paperclip::AdapterRegistry::NoHandlerError (No handler found for "0"):

这是我在使用dropzone提交表单时查看参数的方法

Processing by CarsController#create as JSON
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ND6DrphXI6sYZ+IGZd8HllyGR/74PbmBsyRCHqsRZO2BpgVNLCqJpkokW57pQ5lVaPm9AVzredrHNg9Lc8y1eQ==", "car"=>{„brand”=>"Audi ", "model"=>"A6"}, "null"=>"", "commit"=>"Create Car", "images"=>{"0"=>#<ActionDispatch::Http::UploadedFile:0x007fc42bcc4308 @tempfile=#<Tempfile:/var/folders/f5/x2w5mbln30q9q70f1mqxy3hc0000gn/T/RackMultipart20170926-645-1hajljx.png>, @original_filename=„car.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"images[0]\"; filename=\”car.png\"\r\nContent-Type: image/png\r\n">}}

没有dropzone

Processing by CarsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Pwp4IlmBXuaKN8GZ5TGckKpxRlpteKtsGqgVPE/rK3yKkv7B7fz063h0eAFprQJTmxe8pcm+azduullplzbz7A==", "car"=>{„brand”=>"Audi ", "model"=>"A6"}, "images"=>[#<ActionDispatch::Http::UploadedFile:0x007fc428b41b50 @tempfile=#<Tempfile:/var/folders/f5/x2w5mbln30q9q70f1mqxy3hc0000gn/T/RackMultipart20170925-645-sxrzzm.png>, @original_filename=„car.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"images[]\"; filename=\”car.png\"\r\nContent-Type: image/png\r\n">], "commit"=>"Create Car"}

没有dropzone一切正常。

这是我的表单

<%= form_with(model: car, local: true, :html => {:multipart => true, :class => "dropzone", :id => 'myAwesomeDropzone'}) do |form| %>


<%= file_field_tag "images[]", multiple: true %>

<%= form.submit :id => „submit” %>


<% end %>

Dropzone设置

 Dropzone.options.myAwesomeDropzone = { 
      autoProcessQueue: false,
      uploadMultiple: true,
      parallelUploads: 100,
      paramName: "images",
      maxFiles: 100,

      init: function() {
        var myDropzone = this;

        this.element.querySelector(„#submit”).addEventListener("click", function(e) {
          e.preventDefault();
          e.stopPropagation();
          myDropzone.processQueue();
        });

      }

    }

汽车控制器

  def create
     @car = Car.new(car_params)
      respond_to do |format|
      if @car.save
         if params[:images]
          params[:images].each { |image|
            @car.pictures.create(image: image)
          }
        end

        format.html { redirect_to @car, notice: 'Car was successfully created.' }
        format.json { render :show, status: :created, location: @car }
      else
        format.html { render :new }
        format.json { render json: @car.errors, status: :unprocessable_entity }
      end
    end
  end

图片模型

class Picture < ApplicationRecord

belongs_to :car
  has_attached_file :image, :processors => [:watermark],
                    :styles => { 
                                 :thumb => '150x150>', 
                                 :original => { :geometry => '1920x1080#', :watermark_path => "#{Rails.root}/public/images/logo.png" } 
                               },
                    :url    => '/assets/attachment/:id/:style/:basename.:extension',
                    :path   => ':rails_root/public/assets/attachment/:id/:style/:basename.:extension',
                    :default_url => "/images/:style/mising.png"
validates_attachment :image,
  content_type: { content_type: ["image/jpeg", "image/gif", "image/png"] }
end

我不知道如何解决这个问题,我依靠你的帮助

1 个答案:

答案 0 :(得分:1)

似乎自从使用dropzone后,参数从一个文件数组变为一个哈希,其中键是文件。简单的转型可以解决您的问题:

params[:images].each do |_i, image|
  @car.pictures.create(image: image)
end

我刚刚更改了each循环来迭代哈希而不是数组。这也可行:

params[:images].keys.each do |image|
  @car.pictures.create(image: image)
end