我使用url方法在erb文件中呈现活动记录附件的url。
#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在我的控制台中给警告,所以我试图更新我的代码,但我无法使它工作。
***DEPRECATION WARNING: ActiveStorage::Current.host= is deprecated, instead use ActiveStorage::Current.url_options***
更新代码
#controller
...
ActiveStorage::Current.url_options = request.base_url
...
新误差
在web控制台中,我正在尝试获取文件的完整url
>> file.url
ArgumentError: Cannot generate URL for K01_D01_G12.pdf using Disk service, please set ActiveStorage::Current.url_options.
有人能帮忙吗?
发布于 2022-04-13 22:52:35
ActiveStorage::Current.url_options
实际上是一个哈希,它为protocol
、host
和port
提供了单独的键。因此,你需要:
before_action do
ActiveStorage::Current.url_options = { protocol: request.protocol, host: request.host, port: request.port }
end
或者,ActiveStorage提供了一个关注点(ActiveStorage::SetCurrent
),它就是这样做的。因此,您应该能够执行以下操作:
class RecordMetadataController < ApplicationController
include ActiveStorage::SetCurrent
...
end
https://stackoverflow.com/questions/71860516
复制相似问题