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

给你一囗甜甜゛ 提交于 2019-12-07 18:14:12

问题


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 >):

回答1:


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.




回答2:


variant = picture_of_car
            .variant(resize: '300x300')
            .processed 

variant.service.send(:path_for, variant.key) # Absolute path to variant file


来源:https://stackoverflow.com/questions/53542627/how-can-i-get-url-of-image-variant-in-model-outside-of-controller-view-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!