ActionView::Template::Error (The asset is not present in the asset pipeline.) in Rails 6

Viewed 3635

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.

1 Answers

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

you need the full file name

Related