【问题标题】:rails paperclip .destroy & .clear removes the file from s3, but .url is still directing to s3 instead of default_urlrails paperclip .destroy & .clear 会从 s3 中删除文件,但 .url 仍然指向 s3 而不是 default_url
【发布时间】:2015-07-29 17:51:03
【问题描述】:

我有这个问题,我想从 s3 中删除文件并让系统返回默认 url。但是,在尝试下面的代码并成功删除 s3 中的文件后,我得到的 url 仍然指向 s3。

我做了以下代码

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

【问题讨论】:

  • 如果您在此user.profile_image.destroy 之后检查user.profile_image 会发生什么
  • 回形针附件还在,profile_image_file_nameprofile_image_file_size 等其他内容仍然存在,而不是零
  • 您不认为user.profile_image.destroy 工作不正确吗?因为它没有删除 profile_image。?
  • 我只是尝试了其他方法。在user.profile_image.destory 我也必须做user.update(profile_image_file_name: nil, profile_image_content_type: nil, profile_image_file_size: nil, profile_image_updated_at: nil) 然后当我打电话给user.profile_image.url 时,它会给我我指定的适当的default_url
  • 销毁后需要保存对象(user.save)。见this answer

标签: ruby ruby-on-rails-4 paperclip


【解决方案1】:

销毁图片后,需要保存对象:

user = User.first
user.profile_image.destroy

# saving changes to instance
user.save

归功于this SO answer

【讨论】:

    猜你喜欢
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多