Home > Software design >  ActiveStorage::Current.host= is deprecated, how can i use ActiveStorage::Current.url_options
ActiveStorage::Current.host= is deprecated, how can i use ActiveStorage::Current.url_options

Time:04-14

I render urls for my active record attachments in erb files with url method.

#controller    
class RecordMetadataController < ApplicationController
        before_action do
        ActiveStorage::Current.host = request.base_url
      end
    .
    .
    .
    end


#view
    <iframe src="<%= file.url expires_in: 30 ,disposition: :inline %>" width="600" height="750" style="border: none;"></iframe>

Rails gives DEPRECATION WARNING in my console so i tried to update my code but i cant make it work.

***DEPRECATION WARNING: ActiveStorage::Current.host= is deprecated, instead use ActiveStorage::Current.url_options***

updatded code

#controller
...
ActiveStorage::Current.url_options = request.base_url
...

new error

in web console i am trying to get full url for file

>> file.url
ArgumentError: Cannot generate URL for K01_D01_G12.pdf using Disk service, please set ActiveStorage::Current.url_options.

can anybody help?

CodePudding user response:

ActiveStorage::Current.url_options is actually a Hash that has separate keys for protocol, host, and port. Thus you would need:

before_action do
  ActiveStorage::Current.url_options = { protocol: request.protocol, host: request.host, port: request.port }
end

Alternatively, ActiveStorage provides a concern (ActiveStorage::SetCurrent) that does just that. So, you should be able to do the following:

class RecordMetadataController < ApplicationController
  include ActiveStorage::SetCurrent
  ...
end
  • Related