Renaming uploaded files with Carrierwave

后端 未结 6 881
我寻月下人不归
我寻月下人不归 2021-01-31 08:46

I\'m using Carrierwave to upload files, and I have it working.

My issue is attempting to change the name of the uploaded file.

In the generated uploader.rb there

6条回答
  •  爱一瞬间的悲伤
    2021-01-31 09:26

    To just make the record.id prefix the filename you can do the following:

    class MyUploader < CarrierWave::Uploader::Base
    
      storage :file
    
      def store_dir
        model.class.to_s.underscore.pluralize
      end
    
      def filename
        model.id ? "#{model.id}-#{original_filename}" : original_filename
      end
    
      def url
        "/#{store_dir}/#{model.id}-#{model.file_before_type_cast}"
      end
    end
    

提交回复
热议问题