获取磁盘上 ActiveStorage 文件的路径 [英] Get path to ActiveStorage file on disk

查看:38
本文介绍了获取磁盘上 ActiveStorage 文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取磁盘上使用 ActiveStorage 的文件的路径.该文件存储在本地.

I need to get the path to the file on disk which is using ActiveStorage. The file is stored locally.

当我使用回形针时,我在返回完整路径的附件上使用了 path 方法.

When I was using paperclip, I used the path method on the attachment which returned the full path.

示例:

user.avatar.path

在查看 Active Storage Docs 时,它看起来像 rails_blob_path 可以解决问题.在查看它返回的内容后,它没有提供文档的路径.因此,它返回此错误:

While looking at the Active Storage Docs, it looked like rails_blob_path would do the trick. After looking at what it returned though, it does not provide the path to the document. Thus, it returns this error:

没有那个文件或目录@rb_sysopen -

No such file or directory @ rb_sysopen -

背景

我需要文档的路径,因为我正在使用 combine_pdf gem 来组合多个 pdf成一个单一的pdf.

I need the path to the document because I am using the combine_pdf gem in order to combine multiple pdfs into a single pdf.

对于回形针实现,我遍历所选pdf附件的full_paths并将它们load到组合的pdf中:

For the paperclip implementation, I iterated through the full_paths of the selected pdf attachments and load them into the combined pdf:

attachment_paths.each {|att_path| report << CombinePDF.load(att_path)}

推荐答案

感谢@muistooshort 在评论中的帮助,查看了活动存储代码,这是有效的:

Thanks to the help of @muistooshort in the comments, after looking at the Active Storage Code, this works:

active_storage_disk_service = ActiveStorage::Service::DiskService.new(root: Rails.root.to_s + '/storage/')
active_storage_disk_service.send(:path_for, user.avatar.blob.key)
  # => returns full path to the document stored locally on disk

这个解决方案对我来说有点棘手.我很想听听其他解决方案.不过这对我有用.

This solution feels a bit hacky to me. I'd love to hear of other solutions. This does work for me though.

这篇关于获取磁盘上 ActiveStorage 文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆