Rails: CSS changes in developement not visible - ruby-on-rails

I'm using Rails 5.0
Since I've done this command-line : ENV=production rake assets:precompile
when i change CSS I can see them in local instantly.
I have to kill the server, do this command-line again (ENV=production rake assets:precompile) in order to see the change I made.
Thaks for your help.

Rails requires you to pre-compile assets (css,js,etc) in production after you change anything in assets. However in development assets are compiled live by default.
If you want to compile assets live in production (Not Recommended):
then change config.assets.compile=false in config/environments/production.rb to:
config.assets.compile = true
That is Rails should recompile the assets when it detects that a new version of the source assets is there.
On production, you usually want to set it to false and handle asset compilation during deployment. For this, you have to run
RAILS_ENV=production bin/rails assets:precompile
Usually, if you deploy using Capistrano, it takes care of that.
Note: In rails 4 or earlier it was RAILS_ENV=production bin/rake assets:precompile but in Rails 5.x rake is merged into rails so you must use RAILS_ENV=production bin/rails assets:precompile.

In config/environments/development.rb,
I had config.assets.debug set to false instead of true.

Related

Rails: How do I force a new asset digest on ALL assets?

Running a rails 4.2 app with sprockets and using asset digests. When running rake assets:precompile it creates all my assets with digests. I need however to bump all asset digests to help me debug some caching stuff in production. I tried changing the assets version in:
# config/initializers/assets.rb
Rails.application.config.assets.version = '6.4'
However running rake assets:precompile again after this is done, does not create new files with new digests. Shouldn't it? Or am I missing out on something?
I found the only way to force expiration of assets and get them recompiled was to add the following in my config/environments/production.rb
config.assets.version = '1.1' #This currently doesnt work as intended so use
config.assets.prefix = '/production'
Then bundle exec rake assets:precompile RAILS_ENV=production
Rails 4 and Sprockets 3 don't quite get along as per the thread here thus the versioning not working as intended: https://github.com/rails/sprockets-rails/issues/240

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.

Rails page not change when javascript file changed

When I change a javascript file in Rails,and refresh the page,but not change.
why? I even restart the server.but still not work.
this situation caused just when I execute the command:
RAILS_ENV=production bundle exec rake assets:precompile
Add this to /config/environment/development.rb and this will work in DEV mode, for production mode you should pre-compile assets and turn off this setting.
So only for DEV mode
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true

Assets in Rails 4

I've updated my Rails 3.2 app to 4.0 version and have stumbled upon one problem.
When I run the site with config.assets.compile = false option it doesn't load any assets. I've tried rake assets:precompile and it compiles assets at public/assets, but site doesn't load any of those. I've got application.js and application.css included in my layout, but when I check the source of the page, it's said that they are not found.
When I run the site with config.assets.compile = true everything works perfectly, but I've read that turning this off will result in greater performance, so I need to get it work.
Thanks in advance.
UPD: I've found that if I'm using this command RAILS_ENV=production bundle exec rake assets:precompile everything works. The question is why the rake assets:precompile command doesn't working?
UPD2: For those, who have the same issue, here's the explanation.
Regular rake assets:precompile compiles assets for application that runs in development mode. Specifying RAILS_ENV=production part in the beginning makes the command to compile assets for production mode, the bundle exec part is not required. So the final command looks like this: RAILS_ENV=production rake assets:precompile. Maybe it was obvious, but it took me a while to understand. Thanks.

Confusion about rake assets:clean / cleanup on the asset pipeline in rails

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

Resources