首页 文章

Rails:虽然由球童正确提供,但在 生产环境 模式中无法找到资产

提问于
浏览
0

我几乎几天都试图解决这个问题 .

我试图在 生产环境 模式下在rails网站上运行我的ruby,但是(某些)资产无法提供 . 图像位于自定义子文件夹(app / assets / audio / testfolder / demo.png)中,该子文件夹已添加到 Rails.application.config.assets.paths 并正确预编译但我总是收到错误

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

或者更具体:

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>

图像可以直接访问localhost:81 / assets / demo-57cec01fc3b0b102085dc4de14576f8fd86f8298aed121ce17245df0546276d4.png,因此webserver(caddy)似乎不是问题所在 .

我在github(https://github.com/Kafkalasch/RailsAssetsErrorDemo)上主持了一个简单的演示项目,以便轻松地重现问题 .

只需克隆项目,预编译资产 rails assets:precompile ,用给定的Caddyfile启动caddy服务器, caddy -conf Caddyfile 并用config / puma.production.rb启动puma puma -C RubyDemo/config/puma.production.rb

我使用的是:ruby 2.4.1 puma 3.10.0 rails 5.1.4球童0.10.6

我可以在Debian和Mac OS X上重现这个问题,所以我不认为操作系统是相关的 .

我想使用我的网络服务器来提供静态文件,所以我正在寻找另一种解决方案,而不仅仅是设置 config.assets.compile = true

有没有人有想法或任何提示我如何解决我的问题?

我很感激任何回应 .

1 回答

  • 0

    我在 生产环境 和开发中用puma webserver测试了你的应用程序 .

    我通过在 index.html.erb 文件中包含 mp3png 文件的文件夹解决了错误 .

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

    因为您的图像和资源是在 public/assets/testfolder 中预编译而不是 public/assets

    问题已经解决了

相关问题