Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronzalo/980e3336bf05d21eee0e12252d6a0934 to your computer and use it in GitHub Desktop.
Save ronzalo/980e3336bf05d21eee0e12252d6a0934 to your computer and use it in GitHub Desktop.
Rails Initializer to add destroyable attachments with Paperclip
# encoding: UTF-8
class ActiveRecord::Base
# Class method to add destroyable paperclip attachments.
#
# Example:
# has_attached_file :image
# has_destroyable_file :image
# attr_accessible :image_delete
#
# Adds `image_delete`, `image_delete=` methods. Before_save if `image_delete` is
# set to "1", the `image` attachment gets deleted.
#
# Example html in form:
# <%= f.check_box :image_delete %>
# <%= f.label :image_delete, 'Delete image' %>
def self.has_destroyable_file(*attachments)
attachments.each do |attachment|
attr_accessor :"#{attachment}_delete"
before_save do
self.send(attachment).clear if self.send(:"#{attachment}_delete") == "1"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment