如何获取模型中图像变体的 url(在控制器/视图之外)?主动存储 [英] How can I get url of image variant in model (outside of controller/view)? Active Storage

查看:21
本文介绍了如何获取模型中图像变体的 url(在控制器/视图之外)?主动存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用此代码(Active Storage)获取模型中的 url

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").已处理

例如这段代码

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

NoMethodError (undefined method `signed_id' for #< ActiveStorage::Variant:0x0000000004ea6498 >):

推荐答案

解决方案:

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

此处提供了答案.

对于一个变体,您需要使用 rails_representation_url(variant) - 这将构建一个类似于 rails_blob_url 构建的 URL,但专门针对该变体.

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.

这篇关于如何获取模型中图像变体的 url(在控制器/视图之外)?主动存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆