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

Getting the most out of your agency

Here are some tips based on years of working with, for, and as an agency on how to get the most out of any work you do.

VS Code Web Dev

Select multiple lines in VS Code

How to select multiple lines to edit at once within VS Code