Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get url of image variant in model (outside of controller/view)? Active Storage

I can get url in model with this code (Active Storage)

Rails.application.routes.url_helpers.rails_blob_path(picture_of_car, only_path: true)

But I need get url of resized varian

picture_of_car.variant(resize: "300x300").processed

For example this code

Rails.application.routes.url_helpers.rails_blob_path(picture_of_car.variant(resize: "300x300").processed, only_path: true)

throw

NoMethodError (undefined method `signed_id' for #< ActiveStorage::Variant:0x0000000004ea6498 >):
like image 982
ViT-Vetal- Avatar asked Nov 29 '18 15:11

ViT-Vetal-


1 Answers

Solution:

Rails.application.routes.url_helpers.rails_representation_url(picture_of_car.variant(resize: "300x300").processed, only_path: true)

Answer provided here.

for a variant you need to use rails_representation_url(variant) - this will build a url similar to the one that rails_blob_url builds but specifically for that variant.

like image 62
ViT-Vetal- Avatar answered Sep 23 '22 12:09

ViT-Vetal-