I placed an image 'jumbotron.jpeg' in the app/assets/images folder, which I use in a view:
<div class="jumbotron" style="background: url(<%= image_path 'jumbotron' %>); no-repeat center center fixed;">
It works fine in development but when I push to production, I encounter this error:
ActionView::Template::Error (The asset "jumbotron" is not present in the asset pipeline.):
There is another topic referring to the same issue here: Rails - Asset is not present in asset pipeline when using image_tag
The solution I found there is to set the following to true in config/environments/production.rb
:
config.assets.compile = true
It does work but it makes loading the page extremely slow. This post also explains why setting config.assets.compile to true is a bad idea: https://stackoverflow.com/a/8827757/11293450
So what I tried to do instead (after setting back config.assets.compile = false
) is to precompile the assets locally (cf. https://guides.rubyonrails.org/asset_pipeline.html#local-precompilation).
I changed config/environments/production.rb
to add this line:
config.assets.prefix = "/dev-assets"
Then ran:
rake assets:precompile
Which created a dev-assets folder in the public/folder.
I pushed the files to version control before deploying on the server:
git push
from my local environment to Githubgit pull
on my production server (a VPS) and then:bundle install --deployment --without development test
bundle exec rake assets:precompile db:migrate RAILS_ENV=production
passenger-config restart-app $(pwd)
But I'm still getting the same error:
ActionView::Template::Error (The asset "jumbotron" is not present in the asset pipeline.):
Edit: The solution is described below, the full name of the file was required. As a side note, the original file was a .jpeg
and I had initially wrote <%= image_path 'jumbotron.jpeg' %>
which triggered the error. I noticed afterward that Rails had actually changed the file extension from .jpeg
to .jpg
.
As noted here:
From 3.0, JPEG are automatically converted to .jpg (both with actual precompilation and sandbox precompile errors). If you have something like image_tag('image.jpeg'), it breaks with the AssestNotPrecompiled error. Renaming the file to image.jpg will fix it.