【问题标题】:How can I remove Paperclip attachment using link_to?如何使用 link_to 删除回形针附件?
【发布时间】:2014-09-10 19:30:45
【问题描述】:

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

用户类

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

在表格中

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

显然,这实际上并没有删除图像。最好的方法是什么?链接是否设置了隐藏标志然后更新?或者这只是一个坏主意(如果是这样,为什么)?

【问题讨论】:

    标签: ruby-on-rails paperclip


    【解决方案1】:

    创建一个补丁路由以删除控制器中带有 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-20
      相关资源
      最近更新 更多