Deleting a Uploaded Paperclip Attachment/file in Active Admin

Was looking for a ‘the way’ of doing this, found none. So improvised. This post is a copy of my answer on stackoverflow.

[models.rb]
1
2
3
4
5
6
7
8
9
10
form :html => { :enctype => "multipart/form-data"} do |f|
f.inputs "Details" do
  f.input :name
  f.input :subdomain
end
f.inputs "General Customisation" do
  f.input :standalone_background,  :hint => (("current image:<br/>").html_safe +   f.template.image_tag(f.object.standalone_background.url(:thumb))).html_safe, :as => :file
  f.input :remove_standalone_background, as: :boolean, required: false, label: "remove standalone background"
 end
end

In your model

You could define a status flag like bellow

[model.rb]
1
2
3
4
5
attr_writer :remove_standalone_background

def remove_standalone_background
  @remove_standalone_background || false
end

OR (depreciated in rails 3.2)

[model.rb]
1
2
3
attr_accessor_with_default : standalone_background,false

before_save :before_save_callback

And

[model.rb]
1
2
3
4
5
def before_save_callback
  if self.remove_standalone_background
    self.remove_standalone_background=nil
  end
end

That does it for me. Not ideal, but it gets the job done.

Comments