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

Web Dev

Check in with your database

It pays to take a step back and look at how your database is set up every so often. You will often find quick wins and hidden tech debt.

Web Dev Productivity

Keeping on top of website updates

Learn what website updates are, what they entail, why they are important, and how we can help