Rails assets precompile on development issue - ruby-on-rails

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.

Related

Rails 4 - cannot change CSS without rake assets:precompile which breaks Javascript?

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.

Do you always have to run rake assets:precompile locally?

I read the docs but can't seem to understand if you have to run rake assets:precompile locally each time you change scss file or any other assets? Isn't there an automatic way to do it? One of the things I have noticed is that I forget to run it sometimes and my heroku changes do not appear. There must be a way to set it up automatically in rails?
If I change
config.assets.compile = false
to true, will do that it? Is a disadvantage of doing that?
You don't have to precompile your assets for Heroku to serve them. Heroku will precompile your assets automatically if you have not already precompiled assets locally. Read this heroku doc regarding the asset pipeline in Rails 3 (even if you are already using Rails 4). Then read this doc regarding the asset pipeline in Rails 4 on heroku.
Pay particular attention to this part:
If a public/assets/manifest.yml is detected in your app, Heroku will
assume you are handling asset compilation yourself and will not
attempt to compile your assets. Rails 4 uses a file called
public/assets/manifest-.json instead. On both versions you
can generate this file by running $ rake assets:precompile locally and
checking the resultant files into Git.
rake assets:precompile must be run for production environment. Do not need to run the command for the development environment. The command is used to collect all files into one and so be lighter to serve in production. Under development the styles they are wanted in the assets folder. After running the command, the styles are placed in the public folder.
If you forget to run rake assets:precompile Heroku should do it automatically. One reason it may not be is if you have checked your public folder into git as then during slug compilation Heroku will assume you precompiled your assets and will not do it for you.
Setting config.assets.compile = true can slow down your application by a lot, which is why it is only used in development.

Heroku can't find files in my Ruby on Rails app - even though they are right there?

When I try to access my site, then check my Heroku logs, I see this error:
ActionView::Template::Error (couldn't find file 'reset'
2012-06-13T02:31:43+00:00 app[web.1]: (in
/app/app/assets/stylesheets/application.css:4)):
(application.css contains the line *= require reset)
Then I thought to run "heroku run bundle exec rake assets:precompile:all" but this gives a similar error:
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
couldn't find file 'main/first.js.coffee'
(in /tmp/build_3428u21sggsoc/app/assets/javascripts/application.js:1)
Tasks: TOP => assets:precompile:primary
(That file is the first one required from my application.js, which has first line "//= require main/first.js.coffee")
In summary: my application runs fine locally, but when I deploy to Heroku, the files can no longer be found. Any ideas why?
Edit: here is the project tree. (There is one more directory before the app one, and that is the main project directory that also contains config, db, log, etc)
Another edit: there is no problem with .gitignore, or .slugignore.
At first, I would suggest you to run your application in production mode on your local computer. There are some errors (in assets but I also found some in routing) which can have impact only for production environment so you can test and fix them locally instead of having to do it from the production server.
About the asset precompilation on Heroku, the solution given by akjoe should result in compiled assets tracked in git repo : with this option, you should disable the asset precompilation which happen on Heroku and let Rails serve you assets (set config.serve_static_assets = true in your production.rb file) but this is not the best way to deal with the asset pipeline as you lost one of his major benefice which is freeing your rails application of request for asset.
To make it working properly, you should setup something like heroku explain : Using Rack::Cache with Memcached for Static Asset Caching in Rails 3.1+
I would also suggest you to try the assets precompilation locally in production environment RAILS_ENV=production bundle exec rake assets:precompile. To see if you got any error.
Finally you may want to check this different links to find useful information :
Rails 3.1+ Asset Pipeline on Heroku Cedar
Railscasts : #279 Understanding the Asset Pipeline
Rails Guide
I've had almost exactly the same problem and similar errors with stylesheets edits not taking effect... I found that I would edit css (or as in your case references to css files) which seemed to be ignored by Heroku. Turns out Heroku was ONLY referencing the stylesheets in the public/assets directory. I cleared this directory and was able to get it working.
I later found that you need to precompile your assets directory BEFORE you checkin to git. You would do this as follows:
Precompile assets directory: rake assets:precompile
Add the project files to the current Git repository: git add .
Checkin the file changes to the current Git repository: git commit
-am "description goes here"
Push the files to Heroku: git push heroku master (substitute
'master' for the branch you wish to push to Heroku).
Hope that helps!

Uncompile Development Asset Pipeline

I was compiling my asset pipeline for my production environment and it did for all my environments. How can I uncompile my asset pipeline for my development environment?
I have checked my config/development environment and cannot find a fix.
Thanks in advance for any help...
To remove precompiled assets use:
rake assets:clean
What this basically does is remove the public/assets directory. You may need to include the RAILS_ENV variable if you need to run it for a certain environment.
Try using
rake assets:clobber
worked for me in rails 4
When you run the compile task locally (on your development machine) the assets are compiled in the Rails production environment, but are written to the public folder.
This means that even when you run in development mode it'll use the compiled assets instead of sending requests to the pipeline. This is normal behavor - requests only go to the pipeline if the file does not exists in public/assets.
The compile task should generally only be used when deploying, and on the remote (production) machine.
If you have compiled locally, you can delete all the files in the public/assets folder and development will behave as before. If you checked these files into source control you'll need to remove them.
Once removed things should work fine.
s
One final tip: if this is an upgraded app check your config settings against those in the last section of the Rails asset pipeline guide.
For Rails 5:
$ RAILS_ENV=development bin/rake assets:clobber

Rails, precompiled assets; code changes not reflected in running app

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

Resources