Skip to main content

How do I get avatar.variant(resize: “200x200”) on Rails 5 to work using aws on production?

General • Asked by Luis Daniel Fonseca

I can't get user.avatar.variant(resize: "200x200") to work while using a cloud provider in production because it uses my website path instead of aws.

<%= image_tag user.avatar.variant(resize: "200x200") %> 

# Current Output
<img src="">

# Should be
<img src="">

I need to user.avatar.service_url.split("?").first to get the right path for the img but when I do this, I can't use .variant() because that path is a string.

# this works but can't resize    
<%= image_tag user.avatar.service_url.split("?").first %> 

Any idea how I overcome this.


ActiveStorage URLs always go through your Rails app. That's how they generate the resize 200x200 on the fly without precomputing it. That's the primary downside to using ActiveStorage right now.

In a future version of Rails, they will have an option to use direct urls to AWS, but you can't do that right now.

If you want this feature today, you'd be better off using Shrine for file uploads.

Hey Chris! Thanks a lot for getting back to me.

Do you think it would make sense to implement like a post-process user images feature?

Something like this:

While they release that option of using direct urls to AWS on Active Storage.

The thing is that I already implemented the basic features for active storage, and I already had some users uploading their profile picture.

Thanks again!

Yeah, that would probably work fine for now. I'm not sure when Rails 6.1 will come out but it might be a little while so you'll probably need a solution like this until then. This basically replicates what Shrine would do for you.

Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.