How to make carrierwave delete the file when destroying a record?

后端 未结 3 1850
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 02:16

I\'m using the carrierwave gem to upload files.

I have built a system for users to flag images as inappropriate and for admins to remove the images. From what I can

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-15 03:02

    Not sure what CarrierWave offers for this, but you could use FileUtils in the Ruby standard library with an ActiveRecord callback.

    For instance,

    require 'FileUtils'
    before_destroy :remove_hard_image
    
    def remove_hard_image
      FileUtils.rm(path_to_image)
    end
    

    Sidenote: This code is from memory.

提交回复
热议问题