如何在Ruby on Rails上渲染图像?

问题描述

我想在应用程序中调用方法后渲染图像。在我的rails控制台中验证要渲染的图像的file_path,我收到以下返回信息:

[23]撬(主)> y.profile_photo.file_path =>“ / rails / active_storage / blobs / eyJfcmfpbHMiOnsibWVzc2FnZSI6IkJBaHBFQT09IiwiZXhwIjpudWxsLCJwdXIiOijibG9iX2lkIn19--6b614514bb24ee97ca27d7d7a7d7e7e7e7e7e7e7e7e7e7e7e4e4e7e4e4e4e4e4e4e)

肥胖:y是一个名为驱动程序

用户

然后在 .erb 文件中,我尝试传递此图像以呈现为:

<div> <img src="http:localhost:3000/#{<%[email protected]_photo.file_path%>}"></div>

但是没有用。如何渲染该图像? (位于):

/ rails / active_storage / blobs / eyJfcmfpbHMiOnsibWVzc2FnZSI6IkJBaHBFQT09IiwiZXhwIjpudWxsLCJwdXIiOijibG9iX2lkIn19--6b614514bb24ee97ca27d7d7a7d7b4e7e7e7e7e7e7e7e7b0e

That's how it was rendered in the gmail

上面,这就是在gmail中呈现的方式

解决方法

在滑轨中,您可以使用image_tag渲染图像。

<%= image_tag @ride.driver.profile_photo.file_path %>

或者您可以使用html标签并在src属性中呈现图像。

 <img src="<%= @ride.driver.profile_photo.file_path %>">

您不必使用绝对路径来呈现图像src,因为浏览器会使用它。