Rails将Paperclip目录结构转换为Carrierwave [英] Rails convert Paperclip directory structure to Carrierwave

查看:111
本文介绍了Rails将Paperclip目录结构转换为Carrierwave的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Paperclip宝石: https://github.com/thoughtbot/paperclip

我现在正在使用Carrierwave: https://github.com/carrierwaveuploader/carrierwave

我的生产网站当前正在使用Paperclip.我将更新生产网站以使用Carrierwave.

在Paperclip中上传的文件夹结构与Carrierwave不同.

我也在使用Amazon S3存储上传内容.

我想知道是否可以将通过Paperclip上传的生产文件转换为Carrierwave.

例如,在生产Paperclip的情况下,我目前的简历如下:

bucket_name/model_name/resume/000/000/model_id/original/test.pdf

对于Carrierwave,它应该是:

bucket_name/uploads/model_name/resume/model_id/original/test.pdf

现在看来,我必须手动进行此转换.我想知道是否有更好的方法.

请告知.

解决方案

CarrierWave::Compatibility::Paperclip模块已经提供了此功能.只需在您的上传器中执行以下操作即可:

class MyUploader < CarrierWave::Uploader::Base
  include CarrierWave::Compatibility::Paperclip

  # The :id_partition symbol will trigger a proc in the Paperclip compatibility module that will build out the properly partition directory structure
  def store_dir
    "#{model.class.to_s.underscore}/#{mounted_as.to_s}/:id_partition"
  end

end

I was using the Paperclip gem: https://github.com/thoughtbot/paperclip

I'm now using Carrierwave: https://github.com/carrierwaveuploader/carrierwave

My production website is currently using Paperclip. I'm going to be updating the production website to use Carrierwave.

The folder structure for uploads in Paperclip differs from Carrierwave.

I'm also using Amazon S3 to store uploads.

I'm wondering if there's a way to convert my production files uploaded with Paperclip to Carrierwave.

For example, with Paperclip in production I currently have something like the following for resumes:

bucket_name/model_name/resume/000/000/model_id/original/test.pdf

With Carrierwave it should be:

bucket_name/uploads/model_name/resume/model_id/original/test.pdf

Right now it seems I have to make this conversion manually. I was wondering if there's a better approach.

Please advise.

解决方案

The CarrierWave::Compatibility::Paperclip module already provides this functionality. Just do the following in your uploader:

class MyUploader < CarrierWave::Uploader::Base
  include CarrierWave::Compatibility::Paperclip

  # The :id_partition symbol will trigger a proc in the Paperclip compatibility module that will build out the properly partition directory structure
  def store_dir
    "#{model.class.to_s.underscore}/#{mounted_as.to_s}/:id_partition"
  end

end

这篇关于Rails将Paperclip目录结构转换为Carrierwave的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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