Delete an image with Paperclip

核能气质少年 提交于 2019-11-28 03:19:27

问题


I'm using Paperclip to save pictures in my Rails application:

User model:

class User < ActiveRecord::Base
  has_one :profile
end

Profile model:

class Profile < ActiveRecord::Base
  has_attached_file :avatar, :styles => {:medium => "300x300>", :thumb => "100x100>"}
  belongs_to :user
end

I try to delete the avatar with:

current_user.profile.avatar = nil
current_user.profile.save

but it doesn't work. Is it possible?


回答1:


profile = current_user.profile
profile.avatar.destroy
profile.save

You can't save object this way current_user.profile.save



来源:https://stackoverflow.com/questions/5343937/delete-an-image-with-paperclip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!