如何在GraphQL中使用image_url帮助器?

时间:2018-08-06 13:32:50

标签: ruby-on-rails ruby-on-rails-5 graphql

在“操作”视图中,帮助程序image_url返回以下内容:

http://localhost:3010/assets/no-image-hash.jpg

对于config / initializers目录中的GraphQL,我放置了以下文件:

module GraphQL
  module Define
    class DefinedObjectProxy
      include ActionView::Helpers::AssetUrlHelper
      include ImagesHelper
    end
  end
end

但这没有帮助。

在GraphQL类型中,该字段返回以下内容:

"image": "/images/no-image.jpg",

应该是这样:

http://localhost:3010/assets/no-image-hash.jpg

为什么不起作用,我在做什么错了?

2 个答案:

答案 0 :(得分:0)

我相信您需要使用image_url()Have a glimpse at the docs

  

计算图像资产的完整URL。这将在内部使用image_path,因此它们的大多数行为都是相同的。由于image_url基于asset_url方法,因此可以设置:host选项。如果设置了:host选项,它将覆盖全局config.action_controller.asset_host设置。

     

image_url“ edit.png”,主机:“ http://stage.example.com”#=> http://stage.example.com/assets/edit.png

答案 1 :(得分:0)

在标准请求上下文之外使用image_url时,您将需要使用ActionController::Base.helpers.image_path("example.png")访问它以获取图像哈希。

此处有其他讨论/其他解决方案:https://github.com/rails/rails/issues/10051

相关问题