#課題
ActiveStorageで保存した画像のURLを簡単に取れると思ったら案外手古摺ったのでメモ。
image_tagを使ったerb
<%= image_tag recipe.image.variant(resize: '200x200').processed %>
実際のHTML
<img src=\"http://localhost:3000/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--d3fb1fe3b017a6060dd908f2463551e10cfbacb0/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9MY21WemFYcGxTU0lNTWpBd2VESXdNQVk2QmtWVSIsImV4cCI6bnVsbCwicHVyIjoidmFyaWF0aW9uIn19--306915654d885f72af9e428f63f831384a058376/01.png\" />
image_tag を使わずにURLだけ欲しい。
#結論
rails_representation_url を使うと取れた。
コンソールで検証
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"
出涸らしになった情報かもしれませんが念の為。
#参考情報
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