Rails App Not Serving Assets in Production Environment

When testing locally your production environment, you have to compile the assets locally. Simply run the command below: RAILS_ENV=production bundle exec rake assets:precompile It will generate all the assets under public/assets. Next, you have to tell Rails to serve the assets itself. Server software (eg. Nginx or Apache) do it for you on environments like … Read more

Rails 3.1 remote requests submitting twice

Adding config.serve_static_assets = false to development.rb will prevent loading files from /public/assets. Actually I need to precompile locally because my test mode is using only static assets from /public/assets – tests are catching possible production asset problems. How? Just set config.assets.compile = false and config.serve_static_assets = true in test.rb configuration.

Route helpers in asset pipeline

UPDATE: Now there is a gem that does this for you: js-routes The problem is that Sprockets is evaluating the ERB outside of the context of your Rails app, and most of the stuff you’re expecting isn’t there. You can add things to your Sprockets context like so: Rails.application.assets.context_class.class_eval do include Rails.application.routes.url_helpers end That’s all … Read more

Rails 3.1 asset pipeline and manually ordered Javascript requires

You have two possible structure : the first one and the second one. With both following examples, you expose a package at /assets/externals.js. You can javascript_include_tag this package, but you can also require it in your application.js file. The first one vendor/ ├── assets │ ├── javascripts │ │ ├── externals.js │ │ ├── modernizr-1.7.js … Read more

Using a Rails helper method within a javascript asset

You can include any helper/module/class in an erb template with: <% environment.context_class.instance_eval { include MyHelper } %> See: https://github.com/rails/sprockets/blob/master/lib/sprockets/environment.rb and https://github.com/rails/sprockets/blob/master/lib/sprockets/context.rb To use the url helpers you have to include your specific applications’ helpers. They are available at Rails.application.routes.url_helpers so: <% environment.context_class.instance_eval { include Rails.application.routes.url_helpers } %> EDIT: Fixed links to moved sprockets repo but … Read more