【发布时间】:2015-12-05 01:19:15
【问题描述】:
我一直在寻找 Cloudinary 文档,但不知道如何使用 Rails 和 html 表单将多个文件上传到它。
我向我的 Carrierwave ImageUploader 添加了 Cloudinary 支持
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
【问题讨论】:
-
你上传图片的那一行代码会很好看。你应该有这样的东西:
Cloudinary::Uploader.upload(your_image, auth) -
对不起,我更新了我的问题,但这条线似乎也没有上传它
-
您需要将
.upload所需的授权参数定义为第二个参数。你应该这样做,并在你实际使用include Cloudinary::CarrierWave的地方使用.upload -
授权,是指api key和secret吗?因为这是在
config/cloudinary.yml中定义的,所以应该没问题。另外,我应该将.upload添加到我的ImageUploader中吗? -
是的,是的——这听起来很合理。随意发布上传器的一些相关部分,而不是文件字段,这对我来说很好。