2

I am migrating from deprecated paperclip gem to ActiveStorage and wondering how can I get hold of attachment URL in the model (for example if I want to add it to custom as_json method, some elasticshearh data etc.)

Couldn't find anything in documentation for that particular use case.

alexs333
  • 12,143
  • 11
  • 51
  • 89
  • I had the same problem, it seems there is no public URL for files uploaded without a private markup. With ActiveStorage all files are private by default. That is a problem for me with emails embedding images, images must be inline attached. see this thread https://stackoverflow.com/questions/50719769/activestorage-for-s3-private-files for a patch (I have not yet moved to AS for this reason. waiting for Rails 6 to see if we get something a bit more rounded) – Maxence Jul 03 '18 at 09:03
  • 1
    Maybe I'm not getting the point, but check out this https://stackoverflow.com/questions/50775686/how-to-get-url-of-active-storage-image – iGian Jul 03 '18 at 10:24
  • @iGian Is the URL generated permanent or for a limited time only ? Maybe my mistake but I thought we couldnt get permanent links to assets with AS. I need dive into AS more thoroughly maybe.. – Maxence Jul 04 '18 at 14:39
  • @Maxence, after your comment I did a test on local storage of my dummy app. I copied the image url, stopped the server and relaunched many times pasting the url into many browsers. The URL is still valid. So, I suppose it is permanent. Also for remote storage. Can you confirm by a similar test? – iGian Jul 04 '18 at 14:47
  • @iGian thanks a lot. I was afraid to move to AS from Paperclip due to such limitations. I will do a test on a dedicated bucket with a dummy app and see if I get the same result. – Maxence Jul 04 '18 at 14:50
  • Does this answer your question? [How to get url of Active Storage image](https://stackoverflow.com/questions/50775686/how-to-get-url-of-active-storage-image) – KNejad May 19 '21 at 16:18

1 Answers1

2
file = fetch_your_file

link = rails_blob_path(file, disposition: 'attachment') 

will return the attachment link

Rashid D R
  • 162
  • 6
  • 2
    Note that (to my understanding) this is NOT a link to the file itself, it's a link to a Rails endpoint that will look up the blob and redirect to a temporary signed url for it. This is great for private attachments... not so great for images which will be displayed on the page because rendering the page will trigger a separate Rails request (involving multiple db queries) per each image shown. – Topher Hunt Jan 19 '19 at 10:06