Removing a paper clip application in Activeadmin

I use paperclip to add image attachments to multiple models and Activeadmin to provide a simple admin interface.

I have this code in my activeadmin model file that allows me to upload images:

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 great. All images that I attach like this are optional, so I would like to give the user the option to delete the previously added image, but cannot decide how to do it in Activeadmin. The whole example that I saw relates to situations where attachments are managed using a separate has_many association, and are not part of the main model.

Does anyone know how to do this?

+6
source share
5 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 can define a status flag, for example below.

 attr_writer :remove_standalone_background def remove_standalone_background @remove_standalone_background || false end 

OR (depreciates on 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 
+2
source

You can implement this by creating your own method. It can be done

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

You should also add a custom column with a link, e.g.

 index do column "Custom" do |item| link_to "Custom action", "/admin/items/custom_action" end end 
+1
source

Another option is to have a status flag for the attachment or image. Before saving the edited object, you disconnect the image.

+1
source

Thanks for the help. This is the final working code ...

admin /product.rb

 f.input :image, required: false, hint: (("Current image:<br/>").html_safe + f.template.image_tag(f.object.image.url(:thumb))).html_safe f.input :remove_image, as: :boolean, required: false, label: "Remove Image" 

models /product.rb

 attr_writer :remove_image def remove_image @remove_image || false end before_validation { self.image.clear if self.remove_image == '1' } 
+1
source

Although accepts_nested_attributes_for(:foo, allow_destroy: true) only works with ActiveRecord associations such as belongs_to , we can characterize its design to work similarly with removing attachments in paperclip.

(To understand how nested attributes work in Rails, see http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html).

Add a way to write <attachment_name>_attributes= , as shown below, into your model that already has has_attached_file :

 has_attached_file :standalone_background def standalone_background_attributes=(attributes) # Marks the attachment for destruction on next save, # if the attributes hash contains a _destroy flag # and a new file was not uploaded at the same time: if has_destroy_flag?(attributes) && !standalone_background.dirty? standalone_background.clear end end 

The <attachment_name>_attributes= calls Paperclip::Attachment#clear to mark the attachment to be destroyed when the next model is saved.

Then open the existing app/admin/your_model_here.rb (use the correct file path for your application) and configure strong parameters so that the nested _destroy flag in <attachment_name>_attributes :

 ActiveAdmin.register YourModelHere do permit_params :name, :subdomain, :standalone_background, standalone_background_attributes: [:_destroy] 

In the same file, add the nested _destroy block to the ActiveAdmin form block. This flag must be nested in <attachment_name>_attributes using semantic_fields_for (or one of the other nested attribute methods provided by formtastic).

 form :html => { :enctype => "multipart/form-data"} do |f| f.inputs "Details" do ... end f.inputs "General Customisation" do ... if f.object.standalone_background.present? f.semantic_fields_for :standalone_background_attributes do |fields| fields.input :_destroy, as: :boolean, label: 'Delete?' end end end end 

Your form should now show the delete flag when there is an application. Checking this box and submitting the correct form should remove the attachment.

Source: https://github.com/activeadmin/activeadmin/wiki/Deleting-Paperclip-Attachments-with-ActiveAdmin

0
source

Source: https://habr.com/ru/post/908214/


All Articles