[英]Save Paperclip image with Sidekiq
我正在嘗試保存回形針上傳的圖像並拋出Sidekiq工人。 用戶將圖像選擇為image []並將其傳遞給控制器(作為params [:image])。
視圖:
<%= file_field_tag "image[]", type: :file, multiple: true %>
當我將其傳遞給另一個var(文件)時,它可以在控制器中工作,但是當我將其傳遞給sidekiq_worker時,它將變成字符串的哈希
控制器:
file = params[:image]
SidekiqWorker.perform_async("import_images", file)
Sidekiq工人
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(!@product.nil?)
@product.update(:image=> picture)
end
end
end
謝謝您的幫助 :)
所以,我所做的只是為了實現它...
用戶上傳文件后,它將文件保存在文件夾中,控制器中的變量獲取每個圖像的名稱。
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
結束
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.