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

Rails 3.1 asset pipeline: how to load controller-specific scripts?

To load only the necessary name_of_the_js_file.js file: remove the //=require_tree from application.js keep your js file (that you want to load when a specific page is loaded) in the asset pipeline add a helper in application_helper.rb def javascript(*files) content_for(:head) { javascript_include_tag(*files) } end yield into your layout: <%= yield(:head) %> add this in your view … Read more

How can I disable logging of asset pipeline (sprockets) messages in Ruby on Rails 3.1?

Place the following code in config/initializers/quiet_assets.rb if Rails.env.development? Rails.application.assets.try(:logger=, Logger.new(‘/dev/null’)) Rails::Rack::Logger.class_eval do def call_with_quiet_assets(env) previous_level = Rails.logger.level Rails.logger.level = Logger::ERROR if env[‘PATH_INFO’] =~ %r{^/assets/} call_without_quiet_assets(env) ensure Rails.logger.level = previous_level end alias_method_chain :call, :quiet_assets end end Updated: It now works for Ruby on Rails 3.2 too (previous attempt fixes before_dispatch, and now we’re going for the … Read more

Using Rails 3.1, where do you put your “page specific” JavaScript code?

The Asset Pipeline docs suggest how to do controller-specific JS: For example, if a ProjectsController is generated, there will be a new file at app/assets/javascripts/projects.js.coffee and another at app/assets/stylesheets/projects.css.scss. You should put any JavaScript or CSS unique to a controller inside their respective asset files, as these files can then be loaded just for these … Read more