rails paperclip .destroy & .clear removes the file

2019-08-10 15:54发布

问题:

I have this problem where I want to delete the file from s3 and have the system returns me the default url. However upon trying the code below and successfully deleting the files in s3, the url I get still points to s3.

I did the following code

user = User.first
user.profile_image.destroy
# i also tried
# user.profile_image.clear

render json: {profile_image: user.profile_image.url}
# the url that paperclip gives me is still points to the s3 server instead of my default picture link

回答1:

After destroying the image, you need to save the object:

user = User.first
user.profile_image.destroy

# saving changes to instance
user.save

Credit to this SO answer.