Rails page not change when javascript file changed - ruby-on-rails

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

Related

Rails: CSS changes in developement not visible

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.

Rails 5 - config.assets.compile should be true - why?

I am developing Rails 5 application and use assets pipeline.
It work well in development mode, but if I try to run it in production mode, it can't load images & styles correctly.
I checked and found that it is because
config.assets.compile = false
in config/environments/production.rb
Unless I set it true, it doesn't work at all.
I know live compilation isn't good for production, what is solution?
There are two options related to serving assets within a Rails server:
Asset compilation
config.assets.compile = true
refers to asset compilation. That is, whether Rails should recompile the assets when it detects that a new version of the source assets is there. In development, you want to have it set to true, so that your styles get compiled when you edit the css files. With the next request, Rails will automatically recompile the assets. 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.
Asset serving
The second option related to assets is
config.public_file_server.enabled
This describes whether it is Rails that should serve the compiled files from the public/assets directory. In development, you want that, so it's true by default. In production, you usually don't want to fire up your web server to serve the logo image or a css file, so you probably compile the assets and then host them separately (for example, on a CDN like cloudfront). If you still want them to be served in production, you can launch Rails with:
RAILS_SERVE_STATIC_FILES=true RAILS_ENV=production bin/rails server
Precompile your assets first.
Run RAILS_ENV=production rake assets:precompile to generate your stylesheets and js files in your public directory.

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.

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.

Can I still add changes to CSS after precompile?

Once I've done bundle exec rake assets:precompile RAILS_ENV=production
It seems that it created something like application-e24jrjf834jg93bwuk13uy5gfd1y24f.css
Then when I access to the page, that is called from my app.
In development mode, I could add changes to just to css file and it applied.
In production mode, can I still add little changes to css?
If possible, how?
If you have set config.assets.initialize_on_precompile to true in your production.rb file then all you need to do is restart your server. Otherwise just delete the precompiled assets by running bundle exec rake assets:clean and precompile assets again bundle exec rake assets:precompile RAILS_ENV=production.

Resources