【问题标题】:Delete an image with Paperclip使用回形针删除图像
【发布时间】:2011-03-17 19:04:57
【问题描述】:

我正在使用 Paperclip 将图片保存在我的 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

但它不起作用。有可能吗?

【问题讨论】:

    标签: ruby-on-rails file-upload paperclip


    【解决方案1】:
    profile = current_user.profile
    profile.avatar.destroy
    profile.save
    

    你不能这样保存对象current_user.profile.save

    【讨论】:

    • 这样,文件会从文件系统中删除,但配置文件表中的 avatar_file_name 字段未设置为 nil。我尝试手动将该字段设置为 nil:current_user.profile.avatar_file_name = nil; current_user.profile.save;它不起作用
    • @MarcoAntelmi 你把这个答案放在哪里才能让它发挥作用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2013-06-25
    • 1970-01-01
    • 2017-08-27
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多