Hi guys - am trying to retrieve a variant of an...
# avo-2
a
Hi guys - am trying to retrieve a variant of an ActiveStorage image to display it instead. Have just hacked together some code to see if I can get it to work but the below is returning an error: field :profile_pic, as: :file, format_using: -> { value.attached? ? value.variant(:thumb) : value }
m
Thread automatically created by codyyc. in #740893011994738751
a
Error below

https://cdn.discordapp.com/attachments/1151377099274326016/1151377325053722644/Screenshot_2023-09-13_at_4.42.12_PM.png

l
Hi @ancient-kitchen-45632
value.variant(:thumb)
and
value
are always returning what expected?
a
Value is returning a ActiveStorage::Attached::One
And variant(:thumb) is returning ActiveStorage::VariantWithRecord
Thanks for the quick reply btw!
l
I think the issue is that the field field expect an
ActiveStorage::Attached::One
and you're returning
ActiveStorage::VariantWithRecord
a
I can see they are different haha but am unsure what Avo is wanting me to give it to render
l
can you access the
value.variant(:thumb).blob
?
a
If I run that from console it returns an ActiveStorage::Blob but still doesnt render in Avo
l
Ok
i'm thinking on a solution but not sure if this works
try
Copy code
ruby
format_using: -> {
  if value.attached? && view == :show
    value.blob = value.variant(:thumb).blob
  end

  value
}
a
Thanks for that. But it seems to be rendering the original image
l
You're cheking it on the show page?
a
Yup!
Do you know if it's possible to use the image_tag helper to just generate an image instead?
render an image*
l
I was trying to replicate it but get stucked on this error

https://cdn.discordapp.com/attachments/1151377099274326016/1151383251680755723/image.png

😅
You can monkey patch the view where we render the image and change it
but then if for some reason we update that partial you need to apply the changes on your overriden view in order to get them
a
Yeah that doesn't sound ideal haha
I'm surprised no one else has come across this issue?
l
Can you try something?
a
Sure!
l
use the same logic but on the computed field instead format using
a
Sweet
One sec
I got some form of infinite loop
l
can i see the whole field definition code?
a
field :profile_pic, as: :file do |model, resource, view| if value.attached? && view == :show value.blob = value.variant(:thumb).blob end value end
l
yeah 😄
just a sec
Copy code
ruby
field :profile_pic, as: :file do |model, resource, view|
  profile_pic = model.profile_pic

  if profile_pic.attached? && view == :show
    profile_pic.blob = profile_pic.variant(:thumb).blob
  end

  profile_pic
end
Untested, adjust if some syntax error appear
a
Have changed that and it is still loading the original image.
Seem as though it might not be possible to override the blob?
l
I was able to get the variant working on my side
i'll get back with some feedback
a
Weird
Sounds good thank you!
Actually I've just checked in console and it seems that the thumb variant still has the same dimensions as the original file which shouldn't be happening. So it could in fact be an issue with the transform
l
Check that out but i think there are some styles on that view that even if variant is applied it would stretch the img
can you create a github issue please?
i'll pick it up when have the time and find a way to fix and support variants "officialy"
a
Yup I can do - happy to help
I think the issue is actually related to the image not being transformed so will get to the bottom of that to rule that out
But really appreciate your help and also love what you guys have done!
l
Thank you! We appreciate your feedback!
Hello @ancient-kitchen-45632 , @lemon-wall-20836 suggested the usage of the
format_using: -> { value.variant(resize_to_limit: [100, 100]).processed.image }
on the issue I created arround this problem. I tested it and it works, the styles are forcing the image to expand with low res anyway but we'll fix it on future releases.
Hey @ancient-kitchen-45632, great news, on the last version, just released, styles should allow you to use
format_using: -> { value.variant(resize_to_limit: [100, 100]).processed.image }
and the image will respect the resizing