All you have to do is call update_attributes or the equivalent, and the clip will automatically delete the old attachment. For example, let's say you have this form (from paperclip docs ):
<%= form_for @user, :url => users_path, :html => { :multipart => true } do |form| %> <%= form.file_field :avatar %> ... <% end %>
In your controller, you might have something like the following:
def update @user = User.find(params[:id]) @user = User.update_attr(params) end
Or, if you want to update the attachment, you can do this:
def update @user = User.find(params[:id]) avatar_data = params.slice('avatar') @user = User.update_attr(avatar_data) end
In any case, the old attachment will be deleted from the repository, and the new file will take its place.
source share