If I thought that I could easily get the URL of the image saved in ActiveStorage, I unexpectedly used it, so make a note.
image_Erb using tag
<%= image_tag recipe.image.variant(resize: '200x200').processed %>
Actual html
<img src=\"http://localhost:3000/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--d3fb1fe3b017a6060dd908f2463551e10cfbacb0/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9MY21WemFYcGxTU0lNTWpBd2VESXdNQVk2QmtWVSIsImV4cCI6bnVsbCwicHVyIjoidmFyaWF0aW9uIn19--306915654d885f72af9e428f63f831384a058376/01.png\" />
I just want the URL without using image_tag.
I got it by using rails_representation_url.
Validate on console
irb(main):001:0> Rails.application.routes.url_helpers.rails_representation_url(recipe.image.variant(resize: "200x200").processed, only_path: true)
=> "/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--d3fb1fe3b017a6060dd908f2463551e10cfbacb0/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9MY21WemFYcGxTU0lNTWpBd2VESXdNQVk2QmtWVSIsImV4cCI6bnVsbCwicHVyIjoidmFyaWF0aW9uIn19--306915654d885f72af9e428f63f831384a058376/01.png "
It may be information that has been released, but just in case.
How can I get url of image variant in model (outside of controller/view)? Active Storage https://stackoverflow.com/questions/53542627/how-can-i-get-url-of-image-variant-in-model-outside-of-controller-view-active
Recommended Posts