0

I've built an experimental website using Rails ver. 4.2.5.1, on a CentOS 6.6 Linux box. I followed the "Getting Started With Rails" guide, and the Weblog build tutorial, and then made changes to use SSL, and run in production mode.

To precompile the assets (javascript and images), I ran "rake assets:clobber" to remove previous cached results, and then "rake assets:precompile". This creates the "fingerprinted" filenames (long hex string (MD5 hash?) catenated to original filenames), and drops compiled assets into ../Weblog/public/assets directory.

The "assets:precompile" process is documented in the Rails "Asset Pipeline" documentation, and the Sprockets documentation. The documentation I followed is at:

http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
https://github.com/rails/sprockets-rails

The "production.rb" file as generated by default using the methods in the "Getting Started With Rails" guide, does not seem to work. I start the WEBrick rails server using a two line script file, which is:

    export SSL=true
    rails server -b 0.0.0.0  -p 3000  -e production

I wanted to use the assets:precompile feature, and to make this work, I have made changes to the "config/application.rb" file, as per the sprockets-rails documentation, and included the line:

require 'sprockets/railtie'

I also had to made modifications to file "config/environments/production.rb" file. The default version of the file had:

     # Compress JavaScripts and CSS.
     config.assets.js_compressor = :uglifier
     # config.assets.css_compressor = :sass

Note that the :uglifier line was active, but the :sass line was commented out.

The default production.rb file also had this line:

     # Do not fallback to assets pipeline if a precompiled asset is missed
     config.assets.compile = false

What I had to do to make the WEBrick server actually operate with precompile assets, in production mode, from the Linux box, was to invoke the css_compessor, and to comment out the "config.assets.compile = false" line, not toggle it. (If the assets are already locally precompiled, then I don't need or want to compile them live, I am assuming)

The changes to the config/production.rb file to make the production mode webserver operate on the CentOS box, are thus:

     # Compress JavaScripts and CSS.
     # config.assets.js_compressor = :uglifier
     config.assets.css_compressor = :sass

     # Do not fallback to assets pipeline if a precompiled asset is missed
     # config.assets.compile = false

And that seems to do it. Without these changes, images would not render in the browser (Firefox 44 on Windows/ Safari on iOS/ Firefox on Android 5.1.1), and the "Destroy" link (to invoke delete method and delete an article from the database of articles) would not bring up the confirmation dialog box, or do the delete action. With the above changes to production.rb it all works, so when I run the server, the image files display, and the javascript to actually run the "Destroy/delete" process is getting served up to the browser, and the tutorial Weblog website works as expected. But it won't work if "config.assets.js_compressor = :uglifier" is turned on (by removing the # comment), and it won't work if the "config.assets.compile = false" is not commented out.

As near as I can tell, the precompile is working, and the precompiled assets, with the fingerprinted info (the long MD5 hex string) added to the filenames of the image files, are being served correctly, as is the javascript. I understand most deployments are to Heroku and AWS, so my use of my own hardware for production mode is a bit non-standard for Rails with WEBrick, but I am curious as to why. Am I missing some needed step?

Is there a different (better?) way to do the assets precompile assuming production mode is run on a local Linux box? And is there any obvious reason why ":uglifier" is not working? Thx.

Rusfuture
  • 157
  • 2
  • 10
  • Update: This question was similar, and had half the answer... – Rusfuture Apr 15 '16 at 06:53
  • Update: This question was similar, and had half the answer... http://stackoverflow.com/questions/18700219/rails-4-assets-not-loading-in-production Put config.assets.precompile = ['*.js','*.css', '*.css.erb'], in production.rb. Also, the cmd to precompile is " RAILS_ENV=production bundle exec rake assets:precompile". But if I uncomment "config.assets.js.compressor = :uglifier", the jquery_ujs javascript for Destroy does not work. Problem looks like it is javascript compressor. – Rusfuture Apr 15 '16 at 07:07

0 Answers0