I am developing an application with Rails 3.0 and Backbone and I tried
asset precompilation (rake assets:precompile).
Since then any change I made in the code is not reflected in the executed application,
in development environment.
Thank in advance
You'll have to pre-compile assets every time you make a change.
rake assets:precompile RAILS_ENV=development
I proposed a possible cause and solution to a similar question here which relates to
config/application.rb containing files to be precompiled.
I am writing with respect to Rails 3.2.22
if you are facing this problem then here is solution:-
Causes
Since you are run rake assets:precompile the script has created a folder public/assets and generated all the assets file the browser might ask for. So, when you make new changes in the js/css asset files, the requests from browser are served from public/assets directory.
Two Solutions
rm -df public/assets
rake assets:clean
Related
If I make a change to application.css.scss, no changes are reflected unless I do rake assets:precompile. However, when I run that, none of the JavaScript in my application works. At that point, I have no way of getting the JavaScript to work unless I re-clone the application from GitHub (which wipes out my CSS changes of course). This is all on a local development environment. Any ideas as to why?
Running in development environment should not use precompiled assets unless you've previously precompiled them.
Clean them up with rake assets:clobber and it should pick up your assets as opposed to the precompiled ones.
I have a relatively simple Rails application which has a few coffeescript files that I need to change occasionally.
The public assets folder keeps growing with new files every time I am forced to recompile in order to push to heroku. When this happens I have new asset file that I have to add to git in order for them to be pushed to Heroku for my JS and CSS to appear (if I do not add these new files and push I am left with no CSS or JS).
Obviously I have got the wrong end of the stick here somehow, and am creating extra unnecessary files.
Can anyone shed light on this? I am including a screenshot of my public/assets directory so you get the picture:
Before you run rake assets:precompile, clean up your old assets:
with either rake assets:clean
which only removes old assets (keeps the most recent 3 copies) from public/assets. Useful when doing rolling deploys that may still be serving old assets while the new ones are being compiled.
or rake assets:clobber
which empties out public/assets and clears the Sprockets file system cache.
Source: https://github.com/rails/sprockets-rails/blob/master/README.md
Note that rake assets:clobber is for Rails 4 only.
I precompiled assets on my dev environment (by mistake!) now any changes done on js/css files are not reflecting on browsing site locally.
I removed assets folder from public directory but then no css/js was available.
How do I get rid of this?
As a temporary solution I just cloned project into new directory and it works.
The question is: why do you need asset precompilation in the development environment? It's not meant to work like this.
The asset pipeline allows working in development with the uncompressed, unminified versions of your JS files. It also reloads them each time you refresh your browser, so you can develop your application with ease.
In production, though, the asset pipeline precompiles the JS files / assets you have into one single, minified file. This allows for better performance on the client as the files are smaller and are fetched in one single request.
So precompiling assets in development makes no sense at all.
In case if I understood you correctly, generally, when you precompile by default assets directory is being created inside public directory. To get your assets back, you can precompile again.
There is also a cache in tmp directory that you might consider removing.
Later you could use a $ (bundle exec) rake assets:precompile in combination with $ (bundle exec) rake assets:clean instead of $ rm -r public/assets so that new assets would be in effect rake way.
A one line command to look at new changes after your commits in environment would be
$ RAILS_ENV=(environment) rake assets:clean assets:precompile
but generally in development assets are not meant to be served as in production mode, so running previous with RAILS_ENV=production and starting a local server in production mode would be considered as a way to check (but not to make sure) if your assets would be served upon deployment in real production.
I went into the exact same problem and resolved it following #dashi's advice: 'remove directory assets in public, then start server in development mode.' everything is back to normal.
Today I was working with an application I've had running on Heroku for a few months now and in an attempt to get something working I ran in my development environment:
rake assets:precompile
When I committed my changes and pushed to Heroku, I get 500 errors on my request:
ActionView::Template::Error (jquery.flexslider-min.js isn't precompiled):
I'm at a bit of a loss as to what to do, I've tried a few things:
Lazily compile in production (which I really don't want to do):
Bundler.require(:default, :assets, Rails.env)
Specifically list all of the files that need to be "precompiled" (also don't really want to do this, doesn't seem very efficient):
config.assets.precompile += ...
So far I've simply done a rollback to my last working version. I'm currently stuck unable to push new code. Will be setting up a staging environment (like I should've done long ago) but not sure what to do next or what might fix this issue? Why didn't this throw an error before?
UPDATE
rake assets:clean
Appears to have resolved the problem, although I don't understand why. Can someone share some insight into this?
If you running Rails 4.0 or above, rake assets:clean has been replaced with rake assets:clobber.
However, there are some current problems with clean and clobber with regards to permanently deleting assets. You can follow the issue here. https://github.com/heroku/heroku-buildpack-ruby/issues/123
I'm willing to bet a compiled version/filename inside of manifest.yml in the public/assets folder was out of date/wrong.
If you've made changes to the flexslider.js file, you'll need to recompile with rake assets:precompile and push the updated version to github. I believe you can set the version of the assets inside of the manifest.yml file.
Also, I believe you can run heroku run rake assets:clean or heroku run rake assets:precompile.
I don't think it would be a good idea to precompile assets inside of heroku because of versioning and name conflicts/not stored in github.
You can clean the assets in heroku and push the repo again, so you wouldn't need to precompile locally and push to github, unless there was indeed an issue in the local compilation.
I'd also take a few minutes to read http://guides.rubyonrails.org/asset_pipeline.html
Another possibility is your file name is having issues with sprockets. Why not use the development version of the flexslider.js, rename it to something slightly more convenient, and allow sprockets to do the minification.
Found the solution in the GitHub thread:
increment the config.assets.version variable in
${project-root}/config/application.rb
Assets were refreshed after I'd added config.assets.version = '1.1' at the end of my config file.
Could somebody explain to me what the command rake assets:clean really does? Unfortunately the Rails Guides dont mention it. There is also the command rake assets:cleanup. Whats the difference?
Furthermore could somebody tell me when do I have to run rake assets:precompile in production. Do I run it on the server console after I deployed all my application files to my production server? Or do I precompile on my local machine and then do a deploy of all files?
Thanks all
Note: This answer is rails 3 specific. For rails 4 and later, look at other answers here.
If you precompile on your local machine, then you can commit these generated assets into the repository and proceed with deployment. No need to compile them on production machine.
But it introduces a problem: now when you change source files (coffescript / scss), the app won't pick up the changes, because it will serve precompiled files instead. rake assets:clean deletes these precompiled files.
In my projects assets are precompiled as a part of deployment. Capistrano makes it very easy.
Also, I never heard of rake assets:cleanup.
Run rake assets:clobber to actually clean the assets.
http://www.dixis.com/?p=735
Sergio's answer was completely correct in Rails 3. rake assets:clean deleted all assets that had been previously precompiled into the public/assets directory.
In Rails 4, you run rake assets:clobber to do the same thing.
If you run rake assets:precompile with the following config (by default turned on in staging and production):
# config/environments/production.rb
config.assets.digest = true
You compiled assets get timestamped. This means you can compile your new assets while leaving the old assets in place. You usually want to do this in production so you website will still access the old files while your running precompile to create your new files (because you've added new css/javascript). You now want to get rid of the old files that are no longer in use. The clean it removes the old versions of the precompiled assets while leaving the new assets in place.
rake assets:clean removes compiled assets. It is run by cap deploy:assets:clean to remove compiled assets, generally from a remote server.
cap deploy:clean removes old releases, generally from a remote server. It is not rake assets:clean
rake != cap
rake assets:clean is now run by cap deploy:cleanup_assets. Add require 'capistrano/rails/assets' to your Capfile and you get this cap-task. My capistrano version is v3.2.1.
clean up those untracked files with git clean -f for files and git clean -f -d for directories