Carrierwave filename keeps changing on update_attributes

后端 未结 1 1605
春和景丽
春和景丽 2021-01-05 13:26

I have model Company and company has mounted carrierwave uploader Logo.

class Company < ActiveRecord::Base
  mount_uploader :logo, LogoUploader

相关标签:
1条回答
  • 2021-01-05 13:47

    I experienced the same and figured it out that an uploader class' filename method should not set a new filename unless original_filename presents. CarrierWave has a relevant wiki page about filename which doesn't directly address this issue, but is enough to get a clue.

    For example,

    This code changes the filename field every time the model is updated.

    class SampleUploader < CarrierWave::Uploader::Base
      def filename
        "#{Time.now.strftime('%Y%m%d%H%M%S')}.jpg"
      end
    end
    

    However this extra if statement prevents the former behaviour.

    class SampleUploader < CarrierWave::Uploader::Base
      def filename
        "#{Time.now.strftime('%Y%m%d%H%M%S')}.jpg" if original_filename.present?
      end
    end
    
    0 讨论(0)
提交回复
热议问题