Get path to ActiveStorage file on disk

前端 未结 4 581
[愿得一人]
[愿得一人] 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:42

    Just use:

    ActiveStorage::Blob.service.send(:path_for, user.avatar.key)
    

    You can do something like this on your model:

    class User < ApplicationRecord
      has_one_attached :avatar
    
      def avatar_on_disk
        ActiveStorage::Blob.service.send(:path_for, avatar.key)
      end
    end
    

提交回复
热议问题