我关注了 Railcast #253 http://railscasts.com/episodes/253-carrierwave-file-uploads效果很好。但后来我用 ActiveAdmin 和 Formtastic 实现了它(ActiveAdmin 使用 Formtastic 来处理表单)。
所以我可以上传文件和下载文件。
问题是,当链接到删除链接时,Carrierwave 似乎需要一个模型而不是模型的属性。
我有模型 Shipment,它具有 hbl_pdf 属性(PDF 文档)。这是我的删除链接...
row("HBL") { link_to 'remove', shipment.hbl_pdf, :confirm => "Are you sure?", :method => :delete }
我收到错误... PdfUploader:Class 的未定义方法 model_name
我不想删除货件,只删除文档。
最佳答案
您为什么不在 shipment 对象上调用 remove_hbl_pdf!
?阅读“删除上传的文件”部分:https://github.com/jnicklas/carrierwave#readme
link_to 'remove', remove_shipment_pdf_path(shipment), :confirm => "Are you sure?", :method => :delete
在你的 Controller 中
def remove_shipment_pdf
shipment = Shipment.find_by_id(params[:shipment_id])
shipment.remove_hbl_pdf! if shipment
# respond with something or redirect
end
删除命令的语法基于您的属性名称。因此,如果您的模型属性名称是 .hbl_pdf
那么它就是 remove_hbl_pdf!
如果它是 .image
那么它将是 remove_image!
关于ruby-on-rails - Rails 3 Carrierwave 如何删除作为模型属性的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9006513/