Rails 3 Carrierwave 如何删除模型属性文件?

发布于 2024-12-28 17:11:01 字数 610 浏览 2 评论 0原文

我关注了 Railcast #253 http://railscasts.com/episodes/253-rierwave-file -上传并且效果很好。但后来我用 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

我不想删除货件,只想删除文档。

I followed the Railcast #253 http://railscasts.com/episodes/253-carrierwave-file-uploads and works great. But then I implemented it with ActiveAdmin and therefore Formtastic (ActiveAdmin uses Formtastic for the forms).

So I'm able to upload files and download files.

Problem is, it seems that Carrierwave expects a model instead of an attribute of the model when linking to remove links.

I have model Shipment, which has hbl_pdf attribute (a PDF document). This is my delete link...

row("HBL") { link_to 'remove', shipment.hbl_pdf, :confirm => "Are you sure?", :method => :delete }

I get error... undefined method model_name for PdfUploader:Class

I don't want to delete the shipment, only the document.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余厌 2025-01-04 17:11:01

为什么不对货件对象调用 remove_hbl_pdf! 呢?阅读以下内容的“删除上传的文件”部分:https://github.com/jnicklas/rierwave#readme< /a>

link_to 'remove', remove_shipment_pdf_path(shipment), :confirm => "Are you sure?", :method => :delete

并在您的控制器中

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!

Why aren't you calling remove_hbl_pdf! on the shipment object instead? Read the "Removing uploaded files" section of this: https://github.com/jnicklas/carrierwave#readme

link_to 'remove', remove_shipment_pdf_path(shipment), :confirm => "Are you sure?", :method => :delete

and in your 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

The syntax for the remove command is based on the name of your attribute. So if your model attribute name is .hbl_pdf then it is remove_hbl_pdf! and if it was .image then it would be remove_image!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文