带有ActiveStorage的Rails6应用程序有一个应用程序助手,可以返回用户的个人资料图像。
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返回如下内容:
http://example.org/rails/active_storage/blobs/<big_long_chunk>/new-chimney-crown.jpg
当在视图中运行时,它返回以下内容:
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时,它并不理解当前的主机。
def perform(chat_message)
ActionCable.server.broadcast "stream:#{chat_message.stream.id}", {
chat_message: ChatMessagesController.render(chat_message)
}
发布于 2020-05-07 01:58:26
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,否则它将无法工作。
https://stackoverflow.com/questions/61553110
复制相似问题