删除图像用回形针

问题描述:

我用回形针将照片保存在我的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 

但它不起作用。可能吗?

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