我一直在Cloudinary文檔中尋找一段時間,但無法弄清楚如何使用Rails和html表單上傳多個文件。如何使用cloudinary和carrierwave上傳多個文件
我添加Cloudinary支持我Carrierwave ImageUploader
include Cloudinary::CarrierWave
,我將我的API密鑰和祕密config/cloudinary.yml
,但更重要的是,我需要改變嗎?就目前而言,我保持着與載波一樣的一切。所以,我的形式保存文件域
<%=file_field_tag "images[]", type: :file, multiple: true %>
,然後爲每個圖像的新picture
實例被創建,並且每一個畫面實例持有:image
屬性
if params[:images]
params[:images].each do |image|
@post.pictures.create(image: image)
end
end
我一直在我ImageUploader
或多或少地默認。
class ImageUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
當我上傳一些圖片,網址鏈接到一個網頁cloudinary,但沒有實際已經上傳到cloudinary。我可以在picture
控制器的create
動作中添加一些內容以實際上傳圖像嗎?像
def create
@picture = Picture.new(picture_params)
@picture.save
Cloudinary::Uploader.upload(picture_params)
end
上傳圖片的代碼行很好看。你應該有這樣的東西:'Cloudinary :: Uploader.upload(your_image,auth)' –
對不起,我更新了我的問題,但是這行似乎沒有上傳 – sjbuysse
您需要定義授權參數, '.upload'作爲第二個參數需要。你應該這樣做,並使用'.upload',你實際上'包括Cloudinary :: CarrierWave' –