2017-09-26 11 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">}} 

그리고 DROPZONE없이를 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

것 같습니다 당신이 파일의 배열되는 것을 변경된 매개 변수 DROPZONE 사용하기 때문에 키가 파일 인 해시로 이동합니다. 간단한 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

대단히 고맙습니다. –