我一直在Cloudinary文档中寻找一段时间,但无法弄清楚如何使用Rails和html表单上传多个文件 .
我为我的Carrierwave添加了Cloudinary支持 ImageUploader
include Cloudinary::CarrierWave
我把我的api-key和秘密添加到 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
2 回答
根据
image
的值,您可以这样做:这是在Paperclip中完成的 .