2011-03-17 64 views
9

我用回形針將照片保存在我的Rails應用程序:刪除圖像用回形針

用戶模式:

class User < ActiveRecord::Base 
    has_one :profile 
end 

剖面模型:

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

我試着刪除頭像與:

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

但它不起作用。可能嗎?

回答

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

不能保存反對這樣current_user.profile.save

+0

這樣一來,該文件是從文件系統中刪除,但avatar_file_name場沒有被設置爲在配置表爲零。我嘗試手動將字段設置爲零:current_user.profile.avatar_file_name = nil; current_user.profile.save;它不起作用 – 2011-03-17 19:57:05

+0

你應該先保存你的對象。答案已更新 – fl00r 2011-03-17 20:05:25

+0

@MarcoAntelmi你在哪裏把這個答案,以使其工作? – jmcastel 2015-12-18 17:35:21