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

后端 未结 3 432
刺人心
刺人心 2020-12-16 00:42

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

相关标签:
3条回答
  • 2020-12-16 00:48

    Following the documentation at https://api.rubyonrails.org/classes/ActiveStorage/Variant.html it should be: picture_of_car.variant(resize: [300, 300]).processed.service_url

    0 讨论(0)
  • 2020-12-16 00:56
    variant = picture_of_car
                .variant(resize: '300x300')
                .processed 
    
    variant.service.send(:path_for, variant.key) # Absolute path to variant file
    
    0 讨论(0)
  • 2020-12-16 01:04

    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.

    0 讨论(0)
提交回复
热议问题