Rails - Asset is not present in asset pipeline when using image_tag
Not sure if you want to set config.assets.compile = true in production, this will slow down your server
config.assets.compile=true in Rails production, why not?
Instead of explicitly setting config.assets.compile = false. Comment it out that worked for me, this is probably a bug. After commenting this out images were correctly rendered using the precompiled asset pipeline.
# config.assets.compile = false
This is the answer when none of the above work.
Are you referencing a .jpeg
file? .jpeg
files get compiled to .jpg
files and so if you reference .jpeg
you will get this error.
The solution is to rename the file to .jpg
and reference the image_tag
with the corresponding .jpg
extension.
The asset "aussen" is not present in the asset pipeline.
Technically true because you have not aussen
but you have aussen.jpg
so it will be <%= image_tag("aussen.jpg") %>
Look, while you use <%= image_tag("aussen") %>
then it will be genarate HTML like this
<%= image_tag("aussen") %>
#=> <img alt="Aussen" src="/assets/aussen" />
While you use <%= image_tag("aussen.jpg") %>
then it will be genarate HTML like this
<%= image_tag("aussen.jpg") %>
#=> <img alt="Aussen" src="/assets/aussen.jpg" />
When it's going into production mode then it will be shown some encrypted key on the page source like this
aussen-d2fb0029a12281121a1752c599e715a8e2b3db17f1e8e18248a79a7b1ca63b91.jpg
image_tag
AssetTagHelper
see this for reference.
Update production.rb
file config.assets.compile
false
to true
# config/environments/production.rb
...
config.assets.compile = true
...
I had this same issue when working on a Rails 6 application in Ubuntu 20.04.
The issue for me was that I had not precompiled the assets in production.
Here's how I fixed it:
First, I ran the command below to precompile the assets and make them available in the public
directory of my application:
rails assets:precompile RAILS_ENV=production
Note: In development this can be accomplished using webpacker
with the command: /bin/webpack-dev-server
Next, set up Nginx or Apache web server to serve the static files that are available in the public
directory of my application. For me I set up Nginx with the configuration below using Let's Encrypt for SSL:
upstream railsserver {
server 127.0.0.1:3000;
}
server {
# Replace 'localhost' with your FQDN if you want to use
# your app from remote or if you need to add a certificate
server_name my-website.com www.my-website.com;
root /home/deploy/my-website/public;
# Define where Nginx should write its logs
access_log /var/log/nginx/my-website/access.log;
error_log /var/log/nginx/my-website/error.log;
location / {
# First attempt to serve request as file, then
# the rails application directory
try_files $uri @railsserver;
}
location ~ ^/(assets/|robots.txt|humans.txt|favicon.ico) {
expires max;
}
location @railsserver {
proxy_set_header Host $http_host;
proxy_set_header CLIENT_IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_read_timeout 300;
proxy_pass http://railsserver;
gzip on;
gzip_types text/plain text/xml text/css image/svg+xml application/javas$
gzip_proxied any;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/my-website.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/my-website.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
That's all.
I hope this helps