Cloudinary :: CarrierWave如何在Cloudinary中选择上传文件夹

时间:2018-08-17 15:51:05

标签: ruby-on-rails carrierwave cloudinary

我尝试使用模块Cloudinary::CarrierWave,但是我所有的图像都存储在云的根文件夹中。

我希望所有文件都转到特定的远程文件夹。

我在课堂上尝试:

class PhotoUploader < CarrierWave::Uploader::Base
    include Cloudinary::CarrierWave
    def storage_identifier
        'specific_folder'
    end
end

我知道Cloudinary::Upload.upload有一些选项,但是由于我在ruby on rails上使用简单形式,所以我无法使用此方法。

有人有解决的办法吗?

2 个答案:

答案 0 :(得分:0)

在服务器端上传中使用Carrierwave时,可以使用如下代码:

<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
    <product description="Cardigan Sweater" product_image="cardigan.jpg">
        <catalog_item gender="Mens">
            <item_number>QWZ5671</item_number>
            <price>39.95</price>
            <size description="Medium">
                <color_swatch image="red_cardigan.jpg">Red</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
            </size>
            <size description="Large">
                <color_swatch image="red_cardigan.jpg">Red</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
            </size>
        </catalog_item>
        <catalog_item gender="Womens">
            <item_number>RRX9856</item_number>
            <price>42.50</price>
            <size description="Small">
                <color_swatch image="red_cardigan.jpg">Red</color_swatch>
                <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
            </size>
            <size description="Medium">
                <color_swatch image="red_cardigan.jpg">Red</color_swatch>
                <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
                <color_swatch image="black_cardigan.jpg">Black</color_swatch>
            </size>
            <size description="Large">
                <color_swatch image="navy_cardigan.jpg">Navy</color_swatch>
                <color_swatch image="black_cardigan.jpg">Black</color_swatch>
            </size>
            <size description="ExtraLarge">
                <color_swatch image="burgundy_cardigan.jpg">Burgundy</color_swatch>
                <color_swatch image="black_cardigan.jpg">Black</color_swatch>
            </size>
        </catalog_item>
    </product>
</catalog>

如果使用的是客户端上传,则可以在构建上传标签时设置文件夹参数:

classPictureUploader < CarrierWave::Uploader::Base include Cloudinary::CarrierWave ... def public_id return "my_folder/" + model.short_name end end

答案 1 :(得分:0)

感谢您的回答,我将在您的帮助下解决该问题,并提供一些其他提示。

我做了类似的事情来分离文件夹,但有时不存在model.id(我应该添加一个if)

Template

此代码对我不起作用:

def public_id
  "artpieces/#{model.class}/#{model.id}"
end

我可以直接写:

<%= f.cl_image_upload(:image, :folder => "my_folder") %>

因为我的输入已经是模型中这样的PhotoUploader声明:

<%= f.input :photo, input_html: {accept: ('image') } %>