Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4; asset helper not pointing to fingerprinted image asset (failure to load in production @ Heroku)

This problem is driving me crazy... I think I've tried every conceivable combination of Sass file, ERB file, asset helper, image helper, etc. etc. Someone please give me new ideas!

Context:

Rails apps require use of asset helpers so that when the assets are precompiled, the source will be a fingerprinted asset file. I.e., if you just called img src="X.jpg", the site in production would look for X.jpg, but the file in public/assets has actually been fingerprinted as X-as;diofua;wemfiwaejfoiawefo.jpg. The only way to get to that fingerprinted file is to use an asset helper, e.g., image_url ('X.jpg').

Right now in my live site, I'm using an asset helper, but for whatever reason, it's not pointing at the fingerprinted asset. Note that the assets are found in development (but again, that's because there's no fingerprint added in development).

Code

Image titled "classic-map.png", located in app/assets/images/galleria

Image is called from a css.erb file required in the application.css file. In the css.erb file, I have the following code:

background-image: url(<%= asset_path 'galleria/classic-map.png' %>);

For reference, http://guides.rubyonrails.org/asset_pipeline.html Note that I'm choosing to write this as a css.erb file, hence the use of asset_path vs. asset-path. Also, I initially thought that the issue might have been in interpolation, but in the page source, the url is definitely working, it's just that it's pointing at url(galleria/classic-map.png) instead of url(galleria/classic-map-apsoidufalskjf;kasj.png)

A million kudos to whoever can help!

like image 647
james Avatar asked Feb 20 '14 04:02

james


People also ask

How do I Precompile assets in Heroku?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

How to enable asset pipeline in Rails?

The asset pipeline is implemented by the sprockets-rails gem, and is enabled by default. You can disable it while creating a new application by passing the --skip-asset-pipeline option.

What is assets precompile?

rails assets:precompile is the task that does the compilation (concatenation, minification, and preprocessing). When the task is run, Rails first looks at the files in the config.assets.precompile array. By default, this array includes application.js and application.css .


1 Answers

For what it's worth, this happened to be AGAIN, and this time I could not use the hack because I desperately needed the fingerprint. So somehow, magically, I ran a rake assets:clobber and heroku run rake assets:clobber to clean all assets, and then a straight up git push to force Heroku to do the precompilation for me. That did it, and everything works.

Now, when this happens, I clobber assets locally & in production and push, forcing Heroku to precompile remotely. Similar to @user2880239's answer. I have stopped precompiling locally and checking into git.

like image 142
james Avatar answered Oct 12 '22 02:10

james