Rails 5无法在/ app / assets / images目录外显示图像

时间:2023-01-16 19:32:40
<%= image_tag("/public/pool_2.jpg") %>

it found the image, but there is no picture

它找到了图像,但没有图片

I checked the assets.paths in rails console, and it does include "/public" directory in the assets.paths

我检查了rails控制台中的assets.paths,它在assets.paths中包含了“/ public”目录

rails c

Rails.application.assets.paths

Rails.application.assets.paths

...

...

vice/home_service/public", ...

2 个解决方案

#1


1  

No, I didn't move my carrierwave uploads to /public for some reason. I also changed in /config/application.rb

不,我没有因为某些原因将我的carrierwave上传到/ public。我也在/config/application.rb中进行了更改

config.public_file_server.enabled = true

and I doubled checked the value of config.public_file_server.enabled on rails console. But Rails 5 still doesn't like to display static images like /public/image.png.

我在rails控制台上检查了config.public_file_server.enabled的值。但是Rails 5仍然不喜欢显示像/public/image.png这样的静态图像。

So I just sub the "/app/assets/images" part out, then it works. Now the image is displayed. Here is my hack:

所以我只是将“/ app / assets / images”部分分开,然后就可以了。现在显示图像。这是我的黑客:

<% @image_src = @profile.avatar.url(:thumb).sub(/\/app\/assets\/images\//, "")  %>
<%= image_tag @image_src %>
<%#= image_tag @profile.avatar.url(:thumb) %>

#2


0  

I would recommend placing the image in /public/assets/images in order to mirror the structure of app/assets, for consistency's sake.

为了保持一致性,我建议将图像放在/ public / assets / images中以反映app / assets的结构。

In your view, you could then use:

在您的视图中,您可以使用:

<%= image_tag '/assets/images/pool_2.jpg' %>

Also, be sure that config.public_file_server.enabled is set to true in config/application.rb.

另外,请确保在config / application.rb中将config.public_file_server.enabled设置为true。

You should review the How to Use the Asset Pipeline section of the Asset Pipeline docs.

您应该查看Asset Pipeline文档的“如何使用资产管道”部分。

#1


1  

No, I didn't move my carrierwave uploads to /public for some reason. I also changed in /config/application.rb

不,我没有因为某些原因将我的carrierwave上传到/ public。我也在/config/application.rb中进行了更改

config.public_file_server.enabled = true

and I doubled checked the value of config.public_file_server.enabled on rails console. But Rails 5 still doesn't like to display static images like /public/image.png.

我在rails控制台上检查了config.public_file_server.enabled的值。但是Rails 5仍然不喜欢显示像/public/image.png这样的静态图像。

So I just sub the "/app/assets/images" part out, then it works. Now the image is displayed. Here is my hack:

所以我只是将“/ app / assets / images”部分分开,然后就可以了。现在显示图像。这是我的黑客:

<% @image_src = @profile.avatar.url(:thumb).sub(/\/app\/assets\/images\//, "")  %>
<%= image_tag @image_src %>
<%#= image_tag @profile.avatar.url(:thumb) %>

#2


0  

I would recommend placing the image in /public/assets/images in order to mirror the structure of app/assets, for consistency's sake.

为了保持一致性,我建议将图像放在/ public / assets / images中以反映app / assets的结构。

In your view, you could then use:

在您的视图中,您可以使用:

<%= image_tag '/assets/images/pool_2.jpg' %>

Also, be sure that config.public_file_server.enabled is set to true in config/application.rb.

另外,请确保在config / application.rb中将config.public_file_server.enabled设置为true。

You should review the How to Use the Asset Pipeline section of the Asset Pipeline docs.

您应该查看Asset Pipeline文档的“如何使用资产管道”部分。