How do I get the Rails server to refresh precompiled assets? - ruby-on-rails

I'm testing my Rails 4 app in the production environment on my localhost:3000 using the built in Webrick server. When I run RAILS_ENV=production bundle exec rake assets:precompile the assets are rebuilt in public and the manifest is rebuilt, but the pages are still being served with the previous asset names.
Restarting the rails server makes the new assets appear. Is there a less extreme way to achieve this and how will this behave when I port this to my production server running Phusion Passenger. I really don't want to restart Apache to get my assets in gear.

If you did not change the contents of assets, the precompiled version will be as same as the previous one. If you change it even a bit, the fingerprint will change and app will request for the new one only as you have set config.assets.digest = true.
Anyway another work around would be:
Just run:
rake assets:clean
and then,
rake assets:precompile
This makes everything in the asset pipeline to be rebuilt and serve freshly.
Rails automatically clear the cache for every individual file when its contents are edited.
If any of the above did not work, please try as below:
config.serve_static_assets = true in config/environments/production.rb
config.serve_static_assets configures Rails itself to serve static assets. Defaults to true, but in the production environment is turned off as the server software (e.g. Nginx or Apache) used to run the application should serve static assets instead. Unlike the default setting set this to true when running (absolutely not recommended!) or testing your app in production mode using WEBrick. Otherwise you won't be able use page caching and requests for files that exist regularly under the public directory will anyway hit your Rails app.
Ref: http://guides.rubyonrails.org/configuring.html#rails-general-configuration
Hope it helps :)

We faced the same problem where the old assets were being served even after trying rake assets:clean or assets:clobber and eventually server reboot would resolve the issue. The culprit in our case was unicorn. While deploying our rails app using mina and mina-unicorn, we ran rake assets:clobber, then compiled assets and then restarted unicorn in the end. By doing this the unicorn master never gets stopped and continues to show old assets. So, we changed our mina deploy script and instead of restarting unicorn, we stopped unicorn and started it back. This resolved the issue. So, the key steps are
Deploy application
run rake assets:clobber
run rake assets:precompile
stop unicorn
start unicorn
This kills the concept of zero downtime but this is a better solution than restarting the server.
I understand that you use passenger but this information can be helpful for others

You can recompile your rails assets by running the following command:
bin/rails assets:precompile RAILS_ENV=development

en:
activemodel: # or activerecord:
errors:
models:
person:
# Override the format for all Person attributes:
format: "Invalid %{attribute} (%{message})"
attributes:
age:
# Override the format for the age attribute:
format: "%{message}"
blank: "Please fill in your %{attribute}"

Related

Rails - Can I run rake assets:precompile on production server, while the production app is still running?

Sorry if this question sounds basic. But I haven't been able to find an answer anywhere on the web...
I'm currently running my Rails app on a Ubuntu server. Until now I've always shut down the production app before I pull the changes, run rake assets:clean assets:precompile, and only boot up the Rails app again once the process is finished.
I'm not sure whether the shutting down of app is necessary(i.e. if I don't do it, my app will behave erratically). It induces about 5 minutes of down time.
If that's a must, then maybe I should try to do local precompilation/more advanced deployment procedure, in order to reduce downtime? (Tried local compilation according to http://guides.rubyonrails.org/asset_pipeline.html#local-precompilation, but then after deleting original public/assets and pulling locally precompiled public/assets from the repo, the production server was having rack timeout all the time and won't render anything.)
YES you run rake assets:precompile Rails looks through your assets folder and copies over everything that is not Javascript or CSS into public/assets. It then creates application.js by reading app\assets\javascripts\application.js, and application.css by reading app\assets\stylesheets\application.css, loading up all the "require" files it finds in there.
So yes..you can do it ..but if you ran rake assets:clean..and then precompile...then public/assets will be updated with new compiled assets.
Dont forget to restart the server :)

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 4 compiled assets only works after server reboot

I deploy my Rails app using Capistrano 3 and rails 4, and ubuntu VPS, but the compiled assets cannot be found after deployment, it always return route error, I had to reboot my server, then it will work.
restart Nginx and Unicorn doesn't help.
Any idea?
Run
rake assets:precompile
before deployment
I do the following when deploying latest changes:
I pull my latest version from Github.
I run
bundle exec rake assets:precompile
sudo service nginx restart
Now when accessing your website the code will be loaded into the RAM with your assets being served correctly.
You need to disable static assets serving in your config/environments/production.rb
config.serve_static_assets = false
Hope I could help you.

Ruby on Rails: Ran rake assets:precompile and now both local and heroku deployment don't include bootstrap

I was having issues deploying my project to a heroku server (Precompile fail). So I found this response, https://stackoverflow.com/a/13713753/2989437, and followed up on the advice. I added one line to my application.rb file:
application.rb
module FirstEdc
class Application < Rails::Application
config.assets.initialize_on_precompile = false # I added this line
...
end
end
I then ran the precompile command, committed the changes, and managed to deploy successfully to heroku. However, now my bootstrap/css appears to have stopped functioning both on the heroku deployment, and my local deployment.
I learned that I was supposed to add another line to my deployment.rb file:
deployment.rb
FirstEdc::Application.configure do
...
# Allows for local precompilling --added by Ian
config.assets.prefix = '/dev-assets'
end
So I added this, recompiled and redeployed, but to no avail.
Finally, I ran a rake assets:clean in an attempt to at least get my local deployment back to normal, but it did not work.
Any advice would be greatly appreciated. I'm reading more into the asset pipeline now, but I feel like this could be a cache problem or something. I'll update as I figure out what's going on.
edit. Just to clarify, I've tried removing both additions, running a rake assets:clean and rake assets:clean:all, but neither fix my local deployment.
I don't use config.assets.prefix in any of my apps and they work just fine with Heroku, so not sure what that's doing.
Try removing that line, then running rake assets:clean. Now your local server should be using the files as you change them. When you want to push, run rake assets:precompile first, then push.
If you want to make changes locally after that, run rake assets:clean again to get rid of the precompiled files on your local machine.
If Heroku detects any files in public/assets it will not attempt to precompile your assets again. This is by design.
So, you need to make a decision to either always precompile your assets with rake assets:precompile, or remove any files in public/assets before pushing to Heroku.
(The recommended way is to allow Heroku to precompile them during push)

How to get rails admin working on production server?

Rails_admin works on my development server with port :3000,
but not working on production server, responds 404 error!
How to get it working on production server ?
Thanks
Have you tried running the following?:
$ rake rails_admin:copy_assets
Spotted this little bit in the GitHub ReadMe:
When running RailsAdmin in production the images, stylesheets,
and javascript assets may return 404
not found error's depending on your
servers static assets configuration.
To prevent this issue you can copy
assets directly into your application
by running:
$ rake rails_admin:copy_assets
I had to do a RAILS_ENV=production bundle exec rake db:reset, then it worked. Don't know whether it was a user session issue (no privileges?) or something else.
Be careful though not to execute the command above if you have "real" data in your database!

Resources