Rails asset pipeline: images not looking up resource with build id - ruby-on-rails

I'm getting a 404 on all of the images included with jquery-ui-rails on Rails 4.0.1 after going production. It works fine in development environment. The site is looking for /assets/jquery-ui/ui-icons_222222_256x240.png, but only public/assets/jquery-ui/ui-icons_222222_256x240-890385424135de1513f00cbecfb7f990.png exists in the filesystem. How come production build IDs are not being appended?
I've also had this problem with some fonts. For the time being I've worked around it by just manually copying and pasting to the sought path.

First thing to try is precompiling assets specifically for the production environment:
RAILS_ENV=production rake assets:precompile
If that doesn't do anything, set the following in production.rb and precompile again
config.assets.precompile += ['*.js', '*.css']
config.assets.compile = true

Related

Ruby on Rails assets not being precompiled on Heroku

I have a project that works in the local development environment but breaks when it is uploaded to Heroku. When visiting my project on Heroku, I notice that I get 404 responses from the server saying that it could not find my css and js files. I have done some searching and found out that Heroku is not precompiling my assets. The project will work fine until Heroku puts my project to sleep. Upon waking the project in Heroku, the css and js are broken.
The project is using Rails 4.2.4, I have made sure to to include config.serve_static_assets = true in my config/application.rb and gem 'rails_12factor', group: :production in my Gemfile.
The css and js only breaks when Heroku puts the project to sleep due to inactivity. Does anyone know how to have Heroku automatically precompile assets when it is awaken from sleep?
I had similar issues before, my best bet was to precompile in local and then push to heroku. Configure your production.rb as follows:
config.serve_static_files = false
config.assets.compile = false
then in your console precompile as follows:
rake assets:precompile RAILS_ENV=production
This will precompile everything in public/assets commit your changes and push to heroku.
Also reset your assets cache for avoid any inconsistence:
rake assets:precompile RAILS_ENV=production
The above will force all your public/assets directory to rebuild when you run precompile command.
If your issue is with assets recompilation my answer should solve it, if you still have issues then you are doing something wrong or the issue does not have anything to do with assets precompilation.
We set the configuration values of above to false because now you are sending the precompiled files to the repo, so we do not serve static files nor fallback assets pipeline if something is missing, we are going everything in local.
Gemfile
gem 'rails_12factor', group: :production
application.rb
By default Rails 4 will not serve your assets. To enable this functionality you need to go into config/application.rb and add this line:
config.serve_static_assets = true
production.rb
config.serve_static_files = true
config.assets.compile = true
Command Line
bundle install
bundle exec rake assets:precompile RAILS_ENV=production
Make sure the images are in the /public folder.
Reference here

asset precompile not generating any json files

I have an app which I want to deploy to Heroku. It works in development, but not when deploying to Heroku.
I am considering this to be an asset precompile issue.
But when I run bundle exec rake assets:precompile, it doesn't generate anything.
What could be the reason for this?
Seems like you overrided config.assets.precompile option and removed default values from it. Check your config/application.rb and config/initializers/assets.rb files.

Rails 3.2 precompile compiles image but ActionView does not pull it

I am deploying a RoR 3.2 app to a shared server.
When I run rake assets:precompile RAILS_ENV=production I can see at ~/public_html/my_app/assets/ that all my images are precompiled.
But when I go to mywebsite.com ActionView throws an error. The production log gives me:
ActionView::Template::Error (200/adrap21.png isn't precompiled).
Funny thing is that at ~/public_html/my_app/assets/200 I can see a precompiled image: adrap21-a5f042dd2b89a3d87eba25969495d678.png
I have run several times one of the following:
1) rake assets:precompile RAILS_ENV=production
2) RAILS_ENV=production rake assets:precompile
at my_app/config/environments/production.rb I have the following:
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
I have changed config.assets.compile from false to true and does not help.
I have read related answers but other people solutions are not working for me.
This one was suposed to be foolproof. Another one, more specific about not compiling images, this other prevents using config.assets.compile = true.
I have even tried the advice of using config.assets.precompile += %w[*.png *.jpg *.jpeg *.gif] , but it is not yet working. Might there be something wrong with images call? Is it a RoR 3.2 bug?
FOUND WHAT THE PROBLEM WAS.
Phusion Passenger needs to be restarted for the compiled assets to work. That is why, using Capistrano, you need to restart the server for all the compiled to work together.
Thanks again, Paritosh. I know too how slow is learning curve at beginning when you learn to program by yourself.

Heroku does not precompile assets when deploying

It very clearly says in its documentation that it would do this if I don't precompile them locally.
And truthfully, I have no interest in precompiling these locally.
What I've had in production.rb, I've duplicated in application.rb
In my production.rb :
config.serve_static_assets = false
config.assets.compile = false
config.assets.precompile << 'application.js'
config.assets.precompile << 'application.css'
config.assets.precompile << 'screen.css'
Then I deploy, and that returns :
-----> Compiled slug size: 52.4MB
-----> Launching... done, v28
http://myapp.herokuapp.com deployed to Heroku
So it "compiled" something, right? Except no go, I go to the site and the .css and .js files are blank.
In order to precompile this locally, I am required to comment out in bootstraps_and_overrides.css the line :
#import "screen.css.scss";
Then it precompiles locally, and my local machine will not load the css correctly, but remotely it will actually work correctly.
So my method of deployment now is comment out that line of code,
bundle exec rake assets:precompile
git add .
git commit -m "Adding public/assets"
git push heroku development:master
Then ( unfortunately! ) :
bundle exec rake assets:clean
And then uncomment that line of code in my .css.
Some things to check
You're on Cedar, right? Heroku only supports this behavior on Cedar.
You're on Rails 3, right? Heroku doesn't support precompiling assets on Rails 4.
You have asset pipeline turned on, right? in config/application.rb you need the line config.assets.enabled = true
You have to not have a public/assets folder. On deployment Heroku decides whether or not to precompile based on whether this folder is present (even if it's empty).
If the pipeline is on and you don't have an assets folder, then pre-compilation must be failing.
Try changing to this. I hope this will help you.
In config/environments/production.rb
config.assets.compile = true
config.assets.digest = true
You might be on the wrong Heroku stack. Make sure you specify stack Cedar when you create apps that use the asset pipeline.
heroku create -stack cedar
The reason it would not deploy was because of Google fonts. Moving the file to your application.css such as :
*= require_self
*= require_tree .
*/
#import url(http://fonts.googleapis.com/css?family=Special+Elite);
Will allow the app to deploy, and for the fonts to work.

How to avoid precompiled assets being served in development mode?

I prefer not to concatenate JavaScript files in development mode, but serve them as individual files. So I configured:
development.rb:
config.assets.compress = false
config.assets.debug = true
config.assets.compile = true
In my /app/assets/javascript directory I have:
reviews.js
reviews/
foo.js
bar.js
reviews.js:
//= require jquery
//= require jquery_ujs
//= require_tree ./reviews
I include the JavaScript using <%= javascript_include_tag "reviews" %> in my layout. The generated page correctly references the three scripts individually and reviews.js is essentially empty. So far so good.
Now when I precompile my assets for production using rake assets:precompile the three JavaScript files are concatenated into reviews.js. This is all fine for production but now, in development mode, the concatenated reviews.js is served in addition to the two individual files.
Of course, this leads to all kinds of nasty bugs when developing because now, the content of foo.js and bar.js is served twice, one of them in a potentially older version in reviews.js.
How can I make sure Rails doesn't use the precompiled assets in development mode?
In config/environments/development.rb set:
config.assets.prefix = "/assets_dev"
so that in development mode Rails will look there (but it will not find anything, as you will not compile assets in development (this is indeed what you are trying to do -- not compile assets)).
When precompiling for production, use
RAILS_ENV=production rake assets:precompile
so it compiles into the default assets folder, public/assets.
It sounds like you are precompiling locally. Because the files exist in the expected location they are being served by your dev server, and the requests are not going to Sprockets.
The only way to stop this is delete the compiled files.
Normally you do not need to compile locally. It is expected that in almost all cases the precompile task will be run during deployment of the app. There is a Capistrano recipe for this on the asset pipeline guide page.
If you do need to have those files locally committed to your repo you could use a branch to avoid the problem. Reserve your master branch for production code, and make a second branch for dev. Only compile and commit assets on master. When you switch to dev, they will be gone. Merge dev into master as required.
Edit: Make sure you force your browser to update (control + F5) or you may find the old assets used from the browser cache!
in config/environments/development.rb set:
config.serve_static_assets = false
and no files from /public will be served
I tried this and it worked. rake assets:precompile RAILS_ENV=production
I observed that the new version of assets pipeline does this when you run rake assets:precompile does rake assets:precompile:all

Resources