2017-09-26 185 views
1

我有問題,使用dropzone.js上傳多張圖片

我得到這個錯誤上傳多張圖片:

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

這是怎麼看的時候,我在提交表格參數懸浮窗

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">}} 

而如果沒有懸浮窗

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.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

看來,既然你使用懸浮窗被文件的數組改變參數到一個散列,其中的密鑰是文件。一個簡單的trasnformation可以解決你的問題:

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 
+0

非常感謝,作品像一個魅力:D –