cloudinary和carrierwave之间的public_id不匹配 [英] public_id doesn't match between cloudinary and carrierwave

查看:82
本文介绍了cloudinary和carrierwave之间的public_id不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CarrierWave和Cloudinary将多张图片上传到我的博客文章中。我从浏览器上传它们。
这是通过使用 post 表单中的文件字段而发生的。

I'm using CarrierWave and Cloudinary to upload multiple pictures to my blogposts. I upload them from my browser. This happens with the use of a file field in the post form.

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

提交表单时,会显示图片为每个图像创建一个实例,并将该图像上传到Cloudinary。

When the form is being submitted, a picture instance is created for each one of the images and the image is being uploaded to Cloudinary.

def create
    @post = Post.new(post_params)
    if @post.save
        if params[:images]
            params[:images].each do |image|
                @post.pictures.create(image: image)
                Cloudinary::Uploader.upload(image)
            end
        end
    end
end

我使用的ImageUploader几乎是默认的(除了包括cloudinary插件)

The ImageUploader I use is almost default (exept for including the cloudinary plugin)

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。但是 public_id的却以某种方式无法匹配。有谁知道为什么不呢?当我调用 Cloudinary :: Uploader.upload(image)时是否创建了新的 public_id

Now, the image is being saved to my server, and the image is being uploaded to cloudinary. But somehow the public_id's never match. Does anyone understand why not? Is there a new public_id created when I call Cloudinary::Uploader.upload(image)?

推荐答案

在检查了Tal Lev-Ami(非常感谢!)提到我的项目之后,我发现ImageUploader出现了问题是行

After checking the project that Tal Lev-Ami (thanks a lot for this!) refered me to, I figured out that the problem with my ImageUploader was the line

storage :file

注释掉这一行后,一切运行正常(而且我不必手动上传图像)。如果有人知道为什么此行导致了问题,请成为我的客人在此处发布以供将来参考。

After commenting out this line, everything worked perfectly (and I don't have to manually upload my images). If anyone understands why this line was causing the problem, be my guest to post it here for future reference.

这篇关于cloudinary和carrierwave之间的public_id不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆