Deleting Attachments with rails_admin

How you can go about deleting attachments using Rails Admin

If you want to be able to delete an attachment or a file upload associated with rails_admin you can by making some pretty tiny changes to your model.

The first thing you want to do is let your model know there is a parameter it wants to look for, this parameter should be named :delete_#{name_of_attachment}, for example;

attr_accessor :delete_attachment

Next up we need to set up something to catch this being set by rails_admin and handle it appropriately. You can do this with one line;

before_validation { attachment.clear if delete_attachment == '1' }

The attachment.clear is how paperclip deals with items to be removed, but you can change this to whatever you want.

Thats it! Unfortunately I couldn’t find this documented anywhere so it took me longer than I would care to admit.


Recent posts View all

Freelancing

Why we have ads

We run ads on the site, this article talks about how we try and make them not terrible and why we monetise in the first place

Accessibility

Writing accessible hashtags

Write accessible hashtags by camel casing them. Because ThisReadsBetter thanthisdoes.