Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a Paperclip Attachment in Activeadmin

I'm using paperclip to add image attachments to several models and Activeadmin to provide a simple admin interface.

I have this code in my activeadmin model file which allows for image uploads:

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
end
end

which works fine. All of the images I'm attaching like this are optional and so I'd like to give the user the option to remove a previously added image but can't work out how to do this in Activeadmin. All of the example I've seen are for situations where the attachments are managed through a separate has_many association rather than being part of the main model.

Does anyone know of a way to do this?

like image 359
TalkingQuickly Avatar asked Feb 11 '12 15:02

TalkingQuickly


2 Answers

In your active admin view

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

attr_writer :remove_standalone_background

def remove_standalone_background
  @remove_standalone_background || false
end

OR (depreciated in rails 3.2)

attr_accessor_with_default : standalone_background,false

before_save :before_save_callback

And

def before_save_callback
  if self.remove_standalone_background
    self.remove_standalone_background=nil
  end
end
like image 87
thekindofme Avatar answered Sep 20 '22 13:09

thekindofme


You could implement this by creating a custom method. This can be done

member_action :custom_action, :method => :get do
//code
end

Also you should add a custom column with a link such as

index do
  column "Custom" do |item|
    link_to "Custom action", "/admin/items/custom_action"
  end
end
like image 26
Dan Gurgui Avatar answered Sep 19 '22 13:09

Dan Gurgui