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

Ruby

Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails

Writing Marketing

We've deleted an article's worth of unhelpful words

We've improved several pages across our site by removing words that add no value, and often detract from the article.