2017-11-30 2 views
0

사이드 클립 작업자에게 Paperclip 업로드 이미지를 저장하려고합니다. 사용자는 이미지를 이미지 []로 선택하여 컨트롤러 (params [: image])로 전달합니다.사이드 키크로 종이 클립 이미지 저장

보기 :

<%= file_field_tag "image[]", type: :file, multiple: true %> 

내가 다른 VAR (파일)에 전달하면, 컨트롤러에서 작동하지만이 sidekiq_worker에 전달할 때, 그것은 문자열의 해시로 전환

컨트롤러 :

file = params[:image] 
SidekiqWorker.perform_async("import_images", file) 

SidekiqWorker

def perform(type, file) 
    case type 
    when "import_images" 
     file.each do |picture| 
      puts picture.class 
      puts picture.original_filename 
     end 
     Product.import_images(file) 
     puts "SIDEKIQ_WORKER: IMPORTING IMAGES" 
    end 
end 

이미지 해시의 해시를 전달하려면 어떻게해야합니까? 또는 내가하고 싶은 것을 어떻게 성취 할 수 있습니까?

그런 다음 이미지는 모델로 처리되지만 해시는 이미 문자열로 바뀌어 작동하지 않습니다.

def self.import_images(file) 
file.each do |picture| 
    @product = Product.where(code: File.basename(picture.original_filename, ".*")) 
    if([email protected]?) 
     @product.update(:image=> picture) 
    end 
    end 
end 

는 사용자가 파일을 업로드 한 후

, 그것은 폴더와에 저장합니다 ... 난 그냥 일어날 수 있도록 한 일을 당신의 도움 :) 그래서

+0

죄송합니다, 왜 사용 sidekiq의 파일을 삭제? 당신은 이것에 대해 많은 가이드가 있고 당신은 아마존 S3 스토리지에 업로드 할 수 있습니다. .com/questions/19712816/carrierwave-rails-4-and-multiple-uploads –

+0

나는 Heroku를 사용하고 때로는 응용 프로그램 시간이 초과되었습니다. 업로드 된 이미지의 수는 줄어 듭니다. 두 번째 계획 프로세스에 있어야합니다. 반송파로 처리 할 수 ​​있습니까? 고마워요. –

+0

나는 그것에 대해 정말로 모른다. 죄송합니다. –

답변

0

, 주셔서 감사합니다 컨트롤러의 변수는 각 이미지의 이름을 가져옵니다. 그 후

when "Import images" 
    file = Array.new 
    params[:image].each do |picture| 
    File.open(Rails.root.join('public/system/products', 'uploaded', picture.original_filename), 'wb') do |f| 
     f.write(picture.read) 
    end 
    file.push picture.original_filename 
    end 
    SidekiqWorker.perform_async("import_images", file,0,0) 
    redirect_to products_url, notice: "#{t 'controllers.products.images'}" 

, 그것은 내 sidekiq_worker에 전달 나는 이미지를 검색하고 코드가 이미지의 이름에 해당 제품에 대한 검색 내 모델에 전달합니다. 처리 후, 업로드 된 이미지 :

def self.import_images(file) 
file.each do |image| 
    uploaded = open("public/system/products/uploaded/"+image) 
    prd = Product.where(code: File.basename(uploaded, '.*')) 
    if !prd.nil? 
    prd.update(image: uploaded) 
    end 
    File.delete("public/system/products/uploaded/"+image) 
end