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

1

0

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.

Laicize answered 24/9, 2017 at 23:21 Comment(3)
did you try to clear the cache? in Rails console run $ Rails.cache.clear, it solved this problem for me many times.Bradley
I did now. Did not change anything.Laicize
I configured your app. Development is fine and I can see the asset at localhost:3000/assets/demo.png the asset is the fingerprinted one ... then production has an error. but I can not see logs from running on my local machine with rails s -e production. You have done many configuration hear, not necessary and I can not figure out how to have production show the text of this error message. This must be connected with config you have done with the serverReconnoiter
R
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

Reconnoiter answered 25/9, 2017 at 1:49 Comment(3)
@Laicize that solves the problem, we need to dig deep and understand what is wrong with your config. There are other SO post about this issue so we will solve this. ThanksReconnoiter
Hello Fabrizio! I am not quite sure if I understand you well. What would you need? Apparently I just used wrong paths. What config files do you need?Laicize
@Laicize it is good like this, but I have been following a similar issue #46108901 where somehow the assets are precompiled in a subfolder called public/assets/assets and sprockets would not find them, anyway your problem is solved but follow that post in case we find a better understanding of the issueReconnoiter

© 2022 - 2024 — McMap. All rights reserved.