Rails Paperclip how to delete attachment?
First off, when you create a check_box in a form_for (which it looks like you are), then the form should by default send :image_delete as "1" if checked and "0" if unchecked. The method declaration looks like this:
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
Which shows that you can assign other values if you want to, but that is of course optional.
Secondly, the call to manually delete an attachment without deleting the model instance to which it is attached to is:
@page.image.destroy #Will remove the attachment and save the model
@page.image.clear #Will queue the attachment to be deleted
And to accomplish your way of deleting the images through a checkbox, perhaps add something like this to your Page model:
class Page < ActiveRecord::Base
has_attached_file :image
before_save :destroy_image?
def image_delete
@image_delete ||= "0"
end
def image_delete=(value)
@image_delete = value
end
private
def destroy_image?
self.image.clear if @image_delete == "1"
end
end
This way, when you create your form and add the :image_delete checkbox, it will load the default value "0" from the User instance. And if that field is checked then the controller will update the image_delete to "1" and when the User is saved, it will check if the image is to be deleted.
How can I remove Paperclip attachment using link_to?
Create a patch route to remove attachment with user_id and matching action in the controller
link_to 'Delete Avatar', {action: :action_name, id: user.id}, method: :put
def action_name
# use either/or depending on your usecase
@user.avatar.destroy #Will remove the attachment and save the model
@user.avatar.clear #Will queue the attachment to be deleted
end
Paperclip attachments not getting deleted upon destroy
You need to set the attachment 'file' attribute to nil before destroying it, in order to delete the uploaded file from the disk.
So your code should be like this
Destroy action in Articles controller:
def destroy
@article = Article.find(params[:id])
begin
# first delete the attachments from disk
@article.attachments.each do |a|
a.file = nil
a.save
end
@article.destroy
rescue
flash[:danger] = "Unable to delete article"
else
flash[:success] = "Article deleted"
end
redirect_to admin_articles_url
end
Rails Paperclip delete local attachment if upload successful
Since you know the path of the file you can just do something like this after it's uploaded to s3 -
def remove_file
File.delete(@filename) if File.exist?(@filename)
end
Ruby on Rails: Deleting multiple attachments in paperclip on updates
Just add:
accepts_nested_attributes_for :project_images, allow_destroy: true
in your project.rb
and pass the parameters as it is, as it seems you are doing it the right way. The rest rails will handle to delete or create a new record. You don't need to loop on the params[:photos]
.
Hope this helps.
Rails and paperclip, delete the record but don't delete the attachment
you may want to take a look at how Attachment#assign
(called when you do object.attachment = new_attachment
) is implemented in paperclip.
Basically, it makes a bit of setup, then calls Attachment#clear
, then it saves the new file.
Attachment#clear puts the old file in a deletion queue that is processed when you call save again, what you want is simply to avoid the call to clear, which you could do by either writing a new assign method which skips that line or by monkey patching #clear
so that it becomes a no-op. In theory you could just monkey patch it on the instances where you want this to happen, but it seems to me you may want to do it for the whole project.
Or you can clear the instance variable holding the processing queue. That variable does not have an accessor, but it should be trivial to do an instance_variable_get
Paperclip - delete a file from Amazon S3?
This are the methods from Paperclip that can be used to remove the attachments:
# Clears out the attachment. Has the same effect as previously assigning
# nil to the attachment. Does NOT save. If you wish to clear AND save,
# use #destroy.
def clear(*styles_to_clear)
if styles_to_clear.any?
queue_some_for_delete(*styles_to_clear)
else
queue_all_for_delete
@queued_for_write = {}
@errors = {}
end
end
# Destroys the attachment. Has the same effect as previously assigning
# nil to the attachment *and saving*. This is permanent. If you wish to
# wipe out the existing attachment but not save, use #clear.
def destroy
clear
save
end
So you see, destroy only removes the attachment if no error occurs. I have tried it with my own setup against S3 so I know that destroy works.
Could the problem in your case possible be that you have any validations that cancels the save? I.e validates_attachment_presence or something similar?
I think one way to find out would be to try @user.logo.destroy and then check the content of @user.errors to see if it reports any error messages.
Related Topics
Nokogiri Gem Installation Error
How to Use "Rvm --Default" on Macosx
How to Make an Http Get With Modified Headers
Ruby: "Gem Install Bundler" Not Installing Bundler
Is There a "Phpmyadmin" for Ruby on Rails
Why Aren't Do/End and {} Always Equivalent
Block Comments in HTML.Erb Templates in Rails
Converting an Array of Objects to Activerecord::Relation
Why Does Adding "Sleep 1" in an After Hook Cause This Rspec/Capybara Test to Pass
Rails 3.1 Limit User Created Objects
Matching Balanced Parenthesis in Ruby Using Recursive Regular Expressions Like Perl
Google Plus API Shutdown Today, Which Alternative Can Be Used to Authentication
Has Anyone Tried Installing Ruby & Rubygems from Source on Ubuntu (Preferably Ubuntu 9)
Error: Error Installing Ffi: Error: Failed to Build Gem Native Extension
What Other Languages Have Features And/Or Libraries Similar to Perl's Format