Rails: Assets cannot be found in production mode, although being served properly by caddy

╄→гoц情女王★ 提交于 2019-12-11 15:18:21

问题


I have been trying to fix this problem for almost days now.

I am trying to run my ruby on rails site in production mode, but (some of) the assets cannot be served. The images are located in a custom subfolder ( app/assets/audio/testfolder/demo.png ) which is added to Rails.application.config.assets.paths and precompiled properly but I am always getting the error

ActionView::Template::Error (The asset "demo.png" is not present in the asset pipeline.)

or to be more specific:

I, [2017-09-25T00:38:32.859541 #41037]  INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Started GET "/" for 127.0.0.1 at 2017-09-25 00:38:32 +0200
I, [2017-09-25T00:38:32.860377 #41037]  INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Processing by TestControllerController#index as HTML
I, [2017-09-25T00:38:32.861240 #41037]  INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1]   Rendering test_controller/index.html.erb within layouts/application
I, [2017-09-25T00:38:32.861558 #41037]  INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1]   Rendered test_controller/index.html.erb within layouts/application (0.1ms)
I, [2017-09-25T00:38:32.863790 #41037]  INFO -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] Completed 500 Internal Server Error in 3ms
F, [2017-09-25T00:38:32.864505 #41037] FATAL -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1]
F, [2017-09-25T00:38:32.864789 #41037] FATAL -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1] ActionView::Template::Error (The asset "demo.png" is not present in the asset pipeline.):
F, [2017-09-25T00:38:32.865058 #41037] FATAL -- : [dd7b2824-614f-4e55-ba32-83548f79b2e1]     10:
[dd7b2824-614f-4e55-ba32-83548f79b2e1]     11:   <body>
[dd7b2824-614f-4e55-ba32-83548f79b2e1]     12:     <%= yield %>
[dd7b2824-614f-4e55-ba32-83548f79b2e1]     13:     <%= image_tag(yield(:img)) %>
[dd7b2824-614f-4e55-ba32-83548f79b2e1]     14:     <audio src="<%= audio_path(yield(:audio))%>" type="audio/mpeg" controls preload="none"></audio>
[dd7b2824-614f-4e55-ba32-83548f79b2e1]     15:   </body>
[dd7b2824-614f-4e55-ba32-83548f79b2e1]     16: </html>

The image can be accessed by localhost:81/assets/demo-57cec01fc3b0b102085dc4de14576f8fd86f8298aed121ce17245df0546276d4.png directly, so the webserver (caddy) does not seem to be the problem.

I hosted a simple demo project on github ( https://github.com/Kafkalasch/RailsAssetsErrorDemo ) to reproduce the problem easily.

Just clone the project, precompile the assets rails assets:precompile , start the caddy server with the given Caddyfile, caddy -conf Caddyfile and start puma with config/puma.production.rb puma -C RubyDemo/config/puma.production.rb

I am using: ruby 2.4.1 puma 3.10.0 rails 5.1.4 caddy 0.10.6

I could reproduce this problem on Debian and Mac OS X, so I don't assume that the OS is relevant.

I want to use my webserver to serve static files, so I am looking for an other solution than simply setting config.assets.compile = true

Does anyone have an idea or any hint how I could solve my problem?

I am grateful for any response.


回答1:


I tested your app with puma webserver in production and development.

I solved the error by including the folder for the mp3 and png files in your index.html.erb file.

<% provide(:img, "testfolder/demo.png") %>
<% provide(:audio, "testfolder/demo.mp3") %>

because your images and assets are precompiled inside public/assets/testfolder instead of public/assets

The problem is solved



来源:https://stackoverflow.com/questions/46395764/rails-assets-cannot-be-found-in-production-mode-although-being-served-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!