gpt4 book ai didi

ruby-on-rails - 在 Rails 中使用 Active Storage 时缺少主机问题

转载 作者:行者123 更新时间:2023-12-03 08:54:28 25 4
gpt4 key购买 nike

我不希望我们的服务器受到超出需要的影响(我们为此目的使用 CDN),我想使用资源的直接链接(没有主动存储重定向)

可以通过以下方式检索直接链接

object.active_storage_file.service_url

但这会导致错误:

Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

env/development.rb中我已经添加了:

Rails.application.routes.default_url_options[:host] = "localhost:3000"

但这并不能解决问题。知道为什么以及如何做到这一点吗?

最佳答案

在你的 Controller 中你可以写:

before_action do
ActiveStorage::Current.host = request.base_url
end

这样您就可以使用主机提供服务 URL:

active_storage_file.blob.service_url if active_storage_file.attached?

关于ruby-on-rails - 在 Rails 中使用 Active Storage 时缺少主机问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56700829/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com