Get path to ActiveStorage file on disk

前端 未结 4 712
[愿得一人]
[愿得一人] 2020-12-09 02:16

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

When I was using paperclip, I used the path<

4条回答
  •  余生分开走
    2020-12-09 02:36

    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.

提交回复
热议问题