user3291025
user3291025

Reputation: 1117

Rails 4.2: image path and fingerprint only added when config.assets.compile = true

In production, the correct paths to my images are not called with the image tag, and the md5 fingerprint is not added. The image names (e.g. "pretty_picture.jpg") are stored in the database. The precompilation files are all present in the public folder including the manifest file.

When called with image_tag:

image_tag @test_question.question.image

I get:

<img src="/images/pretty_picture.jpg">

If I set config.assets.compile = true in production.rb the image is rendered and I get:

<img src="/assets/images/pics/pretty/pretty_picture-e0df5012b6930cda4efaa866af22a63f.jpg" >

My hack solution is to use (in HAML)

%img{src: "/assets/"+Rails.application.assets.find_asset(@test_question.question.image).digest_path}

In production.rb I have

config.assets.digest = true
config.assets.enabled = true
config.serve_static_files = false
config.assets.compile = false 

Setting the config.assets.compile to true in production is not recommended. This seems like very strange behaviour on behalf of sprockets and the asset pipeline. Any idea what is wrong with the use of image_tag here?

Upvotes: 5

Views: 813

Answers (1)

Baldrick
Baldrick

Reputation: 24340

In production, you should precompile the assets before starting the server, with the following command (and automate it to do it every time you deploy):

rake assets:precompile RAILS_ENV="production"

and keep config.assets.compile = false in your production.rb. Check in the Asset Pipeline Guide.

Upvotes: 1

Related Questions