2

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:

  1. git push from my local environment to Github
  2. git pull on my production server (a VPS) and then:
  3. bundle install --deployment --without development test
  4. bundle exec rake assets:precompile db:migrate RAILS_ENV=production
  5. 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.

NZisKool
  • 189
  • 3
  • 11
  • 1
    How are you deploying? if using capistrano then assets will be compiled on cap production deploy. No need to pull the code from your VPS, you are just overwriting all the changes you just pushed to git. It seems a very strange process you have – jamesc Jul 19 '20 at 07:50
  • I edited the question to make it clearer: I run git pull when I'm on the server, to get the code from Github. I don't use capistrano at the moment, the points 1 to 5 above are my actual way to deploy from development to production. – NZisKool Jul 19 '20 at 07:56
  • 1
    Can you post the output from `bundle exec rake assets:precompile` please; plus does your jpeg exist on your VPS? – jamesc Jul 19 '20 at 08:09
  • When I try to run that on the VPS, I get this error: `rake aborted! LoadError: Could not load the 'listen' gem. Add 'gem 'listen'' to the development group of your Gemfile [.....] Caused by: Bootsnap::LoadPathCache::FallbackScan:` (even though it's already there in the development group of the Gemfile). The jpeg are on the server (both in public/assets and /public/dev-assets). – NZisKool Jul 19 '20 at 08:57
  • 1
    ok. so do you get any errors with the RAILS_ENV=production argument? – jamesc Jul 19 '20 at 09:04
  • 'bundle exec rake assets:precompile RAILS_ENV=production' works fine. `Everything's up-to-date. Nothing to do`. – NZisKool Jul 19 '20 at 09:06
  • 1
    What happens if you change `<%= image_path 'jumbotron' %>` to `<%= image_path 'jumbotron.jpeg' %>` you need the full file name – jamesc Jul 19 '20 at 09:11
  • Thanks!! That solved it! Actually, I had the extension there at first and removed them as I read it could be the cause of this missing asset. But the other stuff changed in between may have helped. In any case, thank you James, really a big help! – NZisKool Jul 19 '20 at 09:24

1 Answers1

7

What happens if you change <%= image_path 'jumbotron' %> to <%= image_path 'jumbotron.jpeg' %>

you need the full file name

jamesc
  • 12,423
  • 15
  • 74
  • 113
  • 5
    Thanks, that solved it! Actually, the original images (before compiling them) have a `.jpeg` extension. But Rails transform them to `.jpg`, which may have been the original issue. – NZisKool Jul 19 '20 at 09:29
  • 1
    @NZisKool glad you got the problem solved. Can I humbly suggest that you do not git pull to deploy, you are heading for a massive headache. I have no idea what is in your `.gitignore` but there are a whole bunch of reasons why you shouldn't do this – jamesc Jul 19 '20 at 09:35
  • Thanks for the advice. I should use capistrano instead? – NZisKool Jul 19 '20 at 11:26
  • @nziskool there are many different rails deployment options nowadays however Capistrani is probably the most established of them. Takes a little getting your head round but once learned and scripts written is a one liner in your command line on local machine to deploy. There is a ton of help out there. capistrano-rails gem is your friend – jamesc Jul 19 '20 at 11:37
  • 4
    We ran into this as well specifically with .jpeg extensions. Renaming the .jpeg to .jpg fixed it, but what a weird issue! – Peter P. Apr 04 '21 at 07:49