How to update/rename a carrierwave uploaded file?

后端 未结 6 1327
我在风中等你
我在风中等你 2021-02-04 20:30

I cant figure out how to update/rename a file uploaded/managed with Carrierwave-mongoid in rails 3.2.6. I want to rename the file in the db as well as on the filesystem.

<
6条回答
  •  别那么骄傲
    2021-02-04 21:17

    I used this rake task for reprocessing uploaded images after modifying version settings (filename and image size) in my uploader file:

    # Usage: rake carrierwave:reprocess class=Model
    namespace :carrierwave do
      task :reprocess => :environment do
    
        CLASS = ENV['class'].capitalize
        MODEL = Kernel.const_get(CLASS)
        records = MODEL.all
    
        records.each do |record|
          record.photo.recreate_versions! if record.photo?
        end
    
      end
    end
    

    Notes:

    • Replace "photo" with whatever you named your uploader.
    • Rake tasks go in the lib/tasks folder.
    • This is using Active Record, not sure if Mongoid needs something different.

提交回复
热议问题