Skip to content

Instantly share code, notes, and snippets.

@rondy
Created April 14, 2012 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rondy/2384845 to your computer and use it in GitHub Desktop.
Save rondy/2384845 to your computer and use it in GitHub Desktop.
Rails Assert pipeline changes for controller-specific assets
By default, Rails 3.2 loads everything in app/javascripts and everything in app/stylesheets on
every page, despite controller-specific file naming. If you want to load controller-specific
files only on views from their respective controllers, you need to change the manifests and the
layout. The basic idea is to NOT require the entire trees, but only specific subfolders, in the
manifests, and then load the controller-specific files separately in the layout.
Any file you DO want loaded on every page should be placed in app/assets/javascripts/general or
app/assets/stylesheets/general.
For this to work in production, you also need to ensure that the individual files are precompiled by modifying your production.rb file, listing all of the controller-specific files.
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require_self
*= require_tree ./general
*/
<!DOCTYPE html>
<html>
<head>
<title>ApTest</title>
<%= stylesheet_link_tag "application", params[:controller], :media => "all" %>
<%= javascript_include_tag "application", params[:controller] %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery_ujs
//= require_tree ./general
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# All controller-specific files should be listed here
config.assets.precompile += %w( animals.css animals.js birds.css birds.js )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment