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

Updating payment method email addresses in Stripe

You can't update the email address associated with a payment method in Stripe via their dashboard, you need to use the Stripe CLI

Ruby

Irreversible Rails Migrations

What are irreversible migrations and how might we use them to our advantage?