首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果从redis作业内的渲染器中调用,image_path将返回错误的url

如果从redis作业内的渲染器中调用,image_path将返回错误的url
EN

Stack Overflow用户
提问于 2020-05-02 08:58:37
回答 1查看 148关注 0票数 1

带有ActiveStorage的Rails6应用程序有一个应用程序助手,可以返回用户的个人资料图像。

代码语言:javascript
运行
复制
 module ApplicationHelper
        def profile_picture_with_class user, css_class, shape, width = 100
            if (shape == :square)
                placeholder_pic = "blank_profile_square.jpg"
            else
                placeholder_pic = "blank_profile_round.png"
            end
            image_path = user.profile_image.present? ? user.profile_image : placeholder_pic
            image_tag(image_path, width: width, class: css_class)
        end 
    end

当从应用程序内部调用此applicationHelper时,它可以正常工作。但是,如果一个redis作业使用这个applicationHelper,返回的image_tag将会代替我的应用程序的实际主机(dev中的localhost:3000),这会创建一个非常断开的链接。

我猜,因为这是在一个redis任务中触发的,它不知道主机是什么,所以它在前面加上了其他东西。我如何才能理解如何使用正确的主机和活动存储类型(磁盘与blob)

谢谢。

创建的两个链接的比较:

示例:

Redis返回如下内容:

代码语言:javascript
运行
复制
http://example.org/rails/active_storage/blobs/<big_long_chunk>/new-chimney-crown.jpg

当在视图中运行时,它返回以下内容:

代码语言:javascript
运行
复制
http://localhost:3000/rails/active_storage/disk/<big_long_chunk>/new-chimney-crown.jpg?content_type=image%2Fjpeg&disposition=inline%3B+filename%3D%22new-chimney-crown.jpg%22%3B+filename%2A%3DUTF-8%27%27new-chimney-crown.jpg

编辑:我很确定这与此有关:https://evilmartians.com/chronicles/new-feature-in-rails-5-render-views-outside-of-actions

这是redis的工作,我相信当从ChatMessagesController.render内部调用image_path时,它并不理解当前的主机。

代码语言:javascript
运行
复制
  def perform(chat_message)
    ActionCable.server.broadcast "stream:#{chat_message.stream.id}", {
        chat_message: ChatMessagesController.render(chat_message)       
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-07 01:58:26

代码语言:javascript
运行
复制
  def perform(chat_message)
    # ChatMessagesController.renderer.defaults[:http_host] = ENV['RAILS_APPLICATION_URL'].presence || 'http://localhost:3000' 

    renderer = ChatMessagesController.renderer.new(
      http_host: ENV['RAILS_APPLICATION_URL'].presence || 'http://localhost:3000' ,
      https:      Rails.env.production?
    )

    ActionCable.server.broadcast "stream:#{chat_message.stream.id}", {
        chat_message: renderer.render(chat_message)         
    }

  end

因此,无论出于什么原因,在运行时都会将默认设置重置为example.com。如果我创建了一个渲染器并专门设置了它的主机(和ssl),它似乎工作得很好。还必须正确设置SSL,否则它将无法工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61553110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档