Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 3.1.3: trying to run locally in production, asset pipeline not working

My app runs fine in development mode.

When I run in production mode using RAILS_ENV=production rails s none of my .css or .js files get loaded. (I did precompile assets, using RAILS_ENV=production bundle exec rake assets:precompile.)

The webrick log shows:

ActionController::RoutingError (No route matches [GET] "/assets/application-a73ce43be7bf75953cd0c60b2b4a7eb0.js"):

and

ActionController::RoutingError (No route matches [GET] "/assets/application-986dd79830088c416c1632c12999ec69.css"):

The files were compiled (according to the log\production.log file) and my public/assets directory does show those files with and without the fingerprint:

ls public/assets application-986dd79830088c416c1632c12999ec69.css application-986dd79830088c416c1632c12999ec69.css.gz application-a73ce43be7bf75953cd0c60b2b4a7eb0.js application-a73ce43be7bf75953cd0c60b2b4a7eb0.js.gz application.css application.css.gz application.js application.js.gz 
like image 551
jpw Avatar asked Apr 11 '12 01:04

jpw


People also ask

How does Rails asset pipeline work?

The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and ERB. Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets.

How do you Precompile rails assets?

To compile your assets locally, run the assets:precompile task locally on your app. Make sure to use the production environment so that the production version of your assets are generated. A public/assets directory will be created. Inside this directory you'll find a manifest.

What is Require_tree?

The require_tree directive tells Sprockets to recursively include all JavaScript files in the specified directory into the output. These paths must be specified relative to the manifest file.

What is assets Precompile?

rails assets:precompile is the task that does the compilation (concatenation, minification, and preprocessing). When the task is run, Rails first looks at the files in the config.assets.precompile array. By default, this array includes application.js and application.css .


1 Answers

Rails doesn't serve static assets in production mode by default. If you want to disable this and serve the static assets, update your config/environments/production.rb file with this:

config.serve_static_assets = true 

The reasoning behind Rails' default configuration is that it assumes you'll be running behind a standard web server in production mode (Apache, Nginx, etc.) which will then proxy the requests to your Rails app. Since the assets are precompiled, Apache/Nginx/etc. can directly serve them without needing to talk to your Rails processes.

like image 75
Dylan Markow Avatar answered Sep 22 '22 08:09

Dylan Markow