ActiveStorage support
phildionne opened this issue · 5 comments
Hi, what would be required to support ActiveStorage ?
I'm also curious about this - ActiveStorage performance can be pretty ordinary for public assets, it seems like putting Imgix in front of it would solve that problem while also giving you greater control over the transformations.
Trying to use with ActiveStorage, even with a path I get the following
"exception":{"name":"ActionView::Template::Error","message":"undefined method `[]=' for nil:NilClass"
imgix/rails/tag.rb:22
Hey everyone thanks for all the interest in this. We’ve received this question a couple of times in the past and with this issue being opened up, I decided it's finally time to give an established answer on the matter. The good news is that yes, imgix-rails does support ActiveStorage albeit it does require some configuring. The caveat to all this is that imgix cannot process images stored locally with ActiveStorage, as it can only be used with remote sources.
To set up imgix with ActiveStorage, first ensure that the remote source your ActiveStorage service is pointing to is the same as your imgix source — such as an s3 bucket.
# config/storage.yml
service: S3
access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
region: us-east-1
bucket: your_own_bucket
Modify your active_storage.service
setting depending on what environment you are using. For example, to use Amazon s3 in production, make the following change:
# config/environments/production.rb
config.active_storage.service = :amazon
As you would normally with imgix-rails, configure your application to point to your imgix source:
# config/application.rb
Rails.application.configure do
config.imgix = {
source: your_domain,
use_https: true,
include_library_param: true
}
end
Finally, the two can be used together by passing in the filename of the ActiveStorage blob into the imgix-rails helper function:
# show.html.erb
<%= ix_image_tag(@your_model.image.key) %>
Awesome! Thanks for the walkthrough @sherwinski !
Yeah, so I also fixed the bug I mentioned above, although I had to fix it in a weird way, partly it was that we were trying to pass named args with splat to a helper that decides if things should go to imgix or rails default image_tag
, managing the API differences. Two args I insisted should default to nil
instead of {}
I chose to not splat args in helper method and also to wrap the Rails.application.configure
in a conditional (local-dev we just won't get the benefits of Imgix) and ensure imgix always gets what it needs.
Thank you so much @sherwinski for responding on this.