2014-09-10 67 views
2

我发现这个solution使用复选框作为标志删除图像。但是,我想使用link_to。基本上,在图片附件旁边,我想要一个“删除头像”链接。如果可能,我希望链接只删除图像,而不更新其他属性。如何使用link_to删除Paperclip附件?

User类

class User < ActiveRecord::Base 
    has_attached_file :avatar 
    attr_accessible :avatar 

在形式

link_to "Delete Avatar", {:action => :update}, :method => :put 

显然,这并不实际删除图像,只是还没有。什么是最好的方法来做到这一点?让链接设置一个隐藏的标志,然后更新?或者这只是一个坏主意(如果是的话)?

回答

7

创建一个补丁路线,以删除与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