Carrierwave filename keeps changing on update_attributes

断了今生、忘了曾经 提交于 2019-11-30 20:24:39

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!