Rails ActiveStorage:在代理模式下如何获取 Variant url?

Rails ActiveStorage: how to get Variant url when in proxy mode?

在 /config/application.rb 我有 config.active_storage.resolve_model_to_route = :rails_storage_proxy.

然后我用 Rails.application.routes.url_helpers.rails_representation_url(img, only_path: true)

生成 url

但我仍然通过重定向获取图像,而不是像我在配置代理模式时所期望的那样直接从我的 rails 应用获取图像。

我应该使用不同的方式生成 url 吗?还是我代理模式配置错了?

要在代理模式下获取变体 URL,您需要使用方法 Rails.application.routes.url_helpers.rails_storage_proxy_url(img, only_path: true)

此外,当使用 api only 模式时,您需要通过将 config.middleware.use ActionDispatch::Flash 添加到 application.rb 来启用 Flash 中间件。这是必要的,因为 this issue