【问题标题】:ActiveStorage service_url && rails_blob_path cannot generate full url when not using S3ActiveStorage service_url && rails_blob_path 在不使用 S3 时无法生成完整的 url
【发布时间】:2018-12-09 04:23:52
【问题描述】:

我有一个基本的 ActiveStorage 设置,其中一个型号为has_many_attached :file_attachments。在其他地方的服务中,我试图生成一个在主应用程序(电子邮件、工作等)之外使用的链接。

在生产中使用 S3 我可以: item.file_attachments.first.service_url 我得到了一个指向 S3 存储桶+对象的适当链接。

我无法使用 rails 指南中规定的方法:Rails.application.routes.url_helpers.rails_blob_path(item.file_attachments.first)

它的错误是: ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true 我可以将host: 'http://....' 参数传递给它,它很高兴虽然它仍然没有生成完整的URL,只是路径。

开发中我正在使用磁盘支持的文件存储,我不能使用任何一种方法:

> Rails.application.routes.url_helpers.rails_blob_path(item.file_attachments.first)
ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

在此处设置主机也不会生成完整的 URL。

在生产中service_url 有效,但是在开发中我得到了错误:

> item.file_attachments.first.service_url
ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

指定主机也无济于事:

item.file_attachments.first.service_url(host:'http://localhost.com')
ArgumentError: unknown keyword: host

我也试过添加

config.action_mailer.default_url_options = { :host => "localhost:3000" }
config.action_storage.default_url_options = { :host => "localhost:3000" }
Rails.application.routes.default_url_options[:host] = 'localhost:3000'

没有成功。

我的问题是 - 我怎样才能以一种适用于开发和生产的方式获取完整的 URL? 或者我应该将主机设置在哪里?

【问题讨论】:

标签: rails-activestorage


【解决方案1】:

Active Storage 的磁盘服务希望在ActiveStorage::Current.host 中找到一个用于生成 URL 的主机。

当您手动调用ActiveStorage::Blob#service_url 时,请确保设置了ActiveStorage::Current.host。如果你从控制器调用它,你可以继承ActiveStorage::BaseController。如果这不是一个选项,请在 before_action 挂钩中设置 ActiveStorage::Current.host

class Items::FilesController < ApplicationController
  before_action do
    ActiveStorage::Current.host = request.base_url
  end
end

在控制器之外,使用ActiveStorage::Current.set 提供主机:

ActiveStorage::Current.set(host: "https://www.example.com") do
  item.file_attachments.first.service_url
end

【讨论】:

  • 谢谢先生!我得到了这个信息。我可以要求跟进 - 如果我不想直接链接到 S3,是否有类似的方法可以通过 /rails/active_storage(重定向)端点获取持久的 URL?
  • 谢谢,这救了我。我相信 Action Storage 和 Action Mailer 需要在文档中更新,因为据我所知,这是在电子邮件正文中生成图像以便电子邮件客户端显示它们的唯一方法。基本上,如果以常规方式执行此操作,则不会显示图像,因为电子邮件客户端/服务提供商需要实际的service_url
  • 对模型测试很有帮助!
  • 如何设置“only_path: true”而不是主机?另外,ActiveStorage::Current 是否也允许设置根目录?
  • "include ActiveStorage::SetCurrent" 在控制器中的作用与上面的示例相同。
【解决方案2】:

我遇到了类似的问题。由于容器设置,我不得不强制端口不同。在我的情况下,ActiveStorage 完成的重定向包含错误的端口。对我来说,调整 default_url_options 工作:

before_action :configure_active_storage_for_docker, if: -> { Rails.env.development? }

def configure_active_storage_for_docker
  Rails.application.routes.default_url_options[:port] = 4000
end

【讨论】:

    猜你喜欢
    • 2020-03-27
    • 2019-02-11
    • 2018-08-28
    • 1970-01-01
    • 2019-07-23
    • 2015-02-05
    • 2018-09-07
    • 1970-01-01
    • 2020-06-06
    相关资源
    最近更新 更多