Rails Paperclip how to delete attachment?

后端 未结 6 1088
小鲜肉
小鲜肉 2020-11-30 18:10

I am using Paperclip (w/ Amazon s3) on Rails 3. I want to delete an existing attachment without replacing it using an update action.

I\'ve only foun

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 18:32

    Modified version of Paul's solution, to support Rails 5 custom attributes. I just wish there were a way to include the module at the top of the file, before has_attached_file definitions.

    module Mixins
      module PaperclipRemover
    
        extend ActiveSupport::Concern
    
        included do
          attachment_definitions.keys.each do |name|
    
            attribute :"remove_#{name}", :boolean
    
            before_validation do
              self.send("#{name}=", nil) if send("remove_#{name}?")
            end
    
          end
        end
    
      end
    
    end
    

提交回复
热议问题