Removing Paperclip attachments
Paperclip’s GitHub page includes the necessary steps to delete an attachment - the model’s attribute that refers to the attachment simply gets set to nil. Here is the full implementation of deleting a user’s profile picture in a Rails app. Start with tests (right?!):
spec/features/editing_users_spec.rb:
(This post covers creation of an attachment in a FactoryGirl factory.)
Red. The test first fails when it tries to find a “Remove profile photo” link. In my implementation, photo removal takes place in the Edit view, so that’s where the link goes (or, to be more precise, into the ‘form’ partial):
app/views/users/_form.html.erb:
(If the user has not been persisted to the database yet, as is the case when the partial is used by the New view to create a new user, the link_to
cannot be generated as there is no user id yet - see routes.rb below. This is why it is wrapped in the if..persisted?
clause.)
Red. In this iteration, the test fails because remove_user_photo_path
is undefined. That’s next:
config/routes.rb:
As can be seen from the route, the link_to
method used in the view will generate a url such as /user/1/remove_photo
, which is why having a user id (from a persisted user) is necessary.
Red. The test now fails because the remove_photo
action does not exist in the Users controller. We are finally ready to make use of Paperclip’s instructions on deleting attachments:
app/controllers/users_controller.rb:
The action pulls the relevant user record out of the database, sets its attachment-referring photo
attribute to nil
, and saves the user to the database. It then redirects the browser to the user’s profile page, where the profile image now displays Paperclip’s default graphic and not the photo defined in the factory.
Green :)