如何使用link_to删除Paperclip附件?

3
我找到了这个使用复选框作为标志来删除图片的 解决方案。然而,我想使用一个 link_to。基本上,我想在图像附件旁边放一个“删除头像”的链接。如果可能,我希望链接只删除图像,而不更新其他属性。
用户类
class User < ActiveRecord::Base
  has_attached_file :avatar
  attr_accessible :avatar

在表格中
link_to "Delete Avatar", {:action => :update}, :method => :put

显然,这并不真正删除图片。最好的方法是什么?设置一个隐藏标志并进行更新吗?或者这只是一个坏主意(如果是的话,请说明原因)?
1个回答

8
在控制器中创建一个补丁路由,以删除具有user_id和匹配操作的附件。
link_to 'Delete Avatar', {action: :action_name, id: user.id}, method: :put

def action_name
  # use either/or depending on your usecase
  @user.avatar.destroy #Will remove the attachment and save the model
  @user.avatar.clear #Will queue the attachment to be deleted
end

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接