Setting the url_options hash in the controller
When grabbing the url of an ActiveStorage record through any of these methods: ActiveStorage::Blob#url
,ActiveStorage::Variant#url
,ActiveStorage::Preview#url
you may find yourself running into this error:
Cannot generate URL for <FILENAME> using Disk service,
please set ActiveStorage::Current.url_options.
This can be resolved in the controller by utilizing a concern that ActiveStorage provides for us; ActiveStorage::SetCurrent
.
It would look like this in your controller:
class MyController < ApplicationController
include ActiveStorage::SetCurrent
# The rest of your controller...
end
And now whenever you go to call #url
on your ActiveStorage record, it will know where to generate the url from!
Including ActiveStorage::SetCurrent
sets the ActiveStorage::Current.url_options
hash to match the current request.