Rails 4 compiled assets only works after server reboot - ruby-on-rails

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.

Related

Compiling Assets in Ruby does it need to be done in deployment? Why not before?

So this is just a question I've been wondering about lately, I seem to spend a significant amount of time when deploying waiting for assets to compile.
Why can we not compile the assets on our development machines and submit it in the git repo?
I am currently using ruby 2.5.x and rails 5.2.4.x using the assets pipeline with uglifier.
Is there a way to do this in development and thus disable all asset compilation during deployment?
many thanks,
Simon
The solution was pretty simple
1) disable asset compiling during deployment
I'm using elastic beanstalk so just had to set this in the environment variables
2) Either
Add the /public/assets folder to git
Or for elastic beanstalk create .ebignore file and copy over the .gitignore but remove the /public/assets entry
3) before deploying run
Linux -- RAILS_ENV=development bundle exec rake assets:precompile
Windows -- set RAILS_ENV=development && bundle exec rake assets:precompile
4) deploy code to server as normal
eb deploy
1st: Why can we not compile the assets on our development machines and submit it in the git repo?
The reason why we need to precompile are:
Compressed the assets resources then cached some static content like images, css and so on.
It's help us to generates two files (.css and .js) and compressed all our's css file event it's file from vendors:
<script src="/assets/application-908e25f4bf641868d8683022a5b62f54.js"></script>
<link href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" media="screen"
rel="stylesheet" />
It's very helpful but It's take time and `And the resource it not live reload when you modify some code. You have to re-compile to apply the code.
-> So, That's why you should not compile the assets in DEVELOPMENT evironment.
2nd: Is there a way to do this in development and thus disable all asset compilation during deployment?
You also use precompile on DEVELOPMENT by run this command:
RAILS_ENV=development bundle exec rake assets:precompile
You can precompile assets in development environment by default using
config/development.rb
config.assets.debug = false
Thanks. Hope it's help
In my case, I precompile before deployment, commit, and push it to Github, as you mentioned.
Then, I deploy to production with capistrano gem: https://github.com/capistrano/capistrano
This is the command to precompile:
rails assets:precompile

Does Phusion Passenger automatically precompile assets after restarting?

Via git, I downloaded the latest version of my web-app to our server. I did a touch tmp/restart.txt but this didn't precompile my assets. I did a rake assets:clobber assets:precompile and this refreshed my assets.
Is Passenger supposed to refresh my asset pipeline automatically? If it is, what are some things I ought to look into to troubleshoot this?
Running rails 4.0.0 (unsure of how to check my passenger version)
No, Passenger does not compile assets for you. See http://guides.rubyonrails.org/asset_pipeline.html#in-production - By default Rails in production does "assumes assets have been precompiled and will be served as static assets by your web server".
Do you have a deployment script such as Capistrano ? Most rails deployment scripts should trigger precompilation.

How do I get the Rails server to refresh precompiled assets?

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}"

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

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