aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/asset_pipeline.md
diff options
context:
space:
mode:
authorDamian Galarza <galarza.d@gmail.com>2012-12-16 17:43:59 -0500
committerDamian Galarza <galarza.d@gmail.com>2012-12-17 17:01:56 -0500
commit86c5cea9f414d34fd92adb064fde5ecc7b40c727 (patch)
tree0f32bd94460290f12b0c2f592293325a48f262e6 /guides/source/asset_pipeline.md
parent0c181b4b70a703436e608c38fc02cf31db7c915c (diff)
downloadrails-86c5cea9f414d34fd92adb064fde5ecc7b40c727.tar.gz
rails-86c5cea9f414d34fd92adb064fde5ecc7b40c727.tar.bz2
rails-86c5cea9f414d34fd92adb064fde5ecc7b40c727.zip
Explain controller specific assets more thoroughly
The current section on controller specific assets does not really explain how the default application.css and application.js files generated by rails will work with controller specific assets and is a bit ambiguous. We should remind users that they will be included into their application by default but that they have the option to include them only where needed if they want and how this works with precompiling assets. [ci_skip]
Diffstat (limited to 'guides/source/asset_pipeline.md')
-rw-r--r--guides/source/asset_pipeline.md49
1 files changed, 32 insertions, 17 deletions
diff --git a/guides/source/asset_pipeline.md b/guides/source/asset_pipeline.md
index 743a04ed42..0bb6d7ca71 100644
--- a/guides/source/asset_pipeline.md
+++ b/guides/source/asset_pipeline.md
@@ -96,29 +96,24 @@ Assets can still be placed in the `public` hierarchy. Any assets under `public`
In production, Rails precompiles these files to `public/assets` by default. The precompiled copies are then served as static assets by the web server. The files in `app/assets` are never served directly in production.
+### Controller Specific Assets
+
When you generate a scaffold or a controller, Rails also generates a JavaScript file (or CoffeeScript file if the `coffee-rails` gem is in the `Gemfile`) and a Cascading Style Sheet file (or SCSS file if `sass-rails` is in the `Gemfile`) for that controller.
-For example, if you generate a `ProjectsController`, Rails will also add 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 controllers with lines such as `<%= javascript_include_tag params[:controller] %>` or `<%= stylesheet_link_tag params[:controller] %>`. Note that you have to set `config.assets.precompile` in `config/environments/production.rb` if you want to precomepile them and use in production mode. You can append them one by one or do something like this:
+For example, if you generate a `ProjectsController`, Rails will also add a new file at `app/assets/javascripts/projects.js.coffee` and another at `app/assets/stylesheets/projects.css.scss`. By default these files will be ready to use by your application immediately using the `require_tree` directive. See [Manifest Files and Directives](#manifest-files-and-directives) for more details on require_tree.
- # config/environments/production.rb
- config.assets.precompile << Proc.new { |path|
- if path =~ /\.(css|js)\z/
- full_path = Rails.application.assets.resolve(path).to_path
- app_assets_path = Rails.root.join('app', 'assets').to_path
- if full_path.starts_with? app_assets_path
- puts "including asset: " + full_path
- true
- else
- puts "excluding asset: " + full_path
- false
- end
- else
- false
- end
- }
+You can also opt to include controller specific stylesheets and javascripts only in the controllers they belong to using the following: `<%= javascript_include_tag params[:controller] %>` or `<%= stylesheet_link_tag params[:controller] %>`. Ensure that you are not using the `require_tree` directive though, as this will result in your assets being included more than once.
+
+WARNING: When using asset precompiliation (the production default) you will need to ensure that your controller assets will be precompiled when loading them on a per page basis. By default .coffee and .scss files will not be precompiled on their own. This will result in false positives during development as these files will work just fine since assets will be compiled on the fly. When running in production however, you will see 500 errors since live compiliation is turned off by default. See [Precompiling Assets](#precompiling-assets) for more information on how precompiling works.
NOTE: You must have an [ExecJS](https://github.com/sstephenson/execjs#readme) supported runtime in order to use CoffeeScript. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check [ExecJS](https://github.com/sstephenson/execjs#readme) documentation to know all supported JavaScript runtimes.
+You can also disable the generation of asset files when generating a controller by adding the following to your `config/application.rb` configuration:
+
+ config.generators do |g|
+ g.assets false
+ end
+
### Asset Organization
Pipeline assets can be placed inside an application in one of three locations: `app/assets`, `lib/assets` or `vendor/assets`.
@@ -459,6 +454,26 @@ If you have other manifests or individual stylesheets and JavaScript files to in
config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
```
+Or you can opt to precompile all assets with something like this:
+
+ # config/environments/production.rb
+ config.assets.precompile << Proc.new { |path|
+ if path =~ /\.(css|js)\z/
+ full_path = Rails.application.assets.resolve(path).to_path
+ app_assets_path = Rails.root.join('app', 'assets').to_path
+ if full_path.starts_with? app_assets_path
+ puts "including asset: " + full_path
+ true
+ else
+ puts "excluding asset: " + full_path
+ false
+ end
+ else
+ false
+ end
+ }
+
+
NOTE. Always specify an expected compiled filename that ends with js or css, even if you want to add Sass or CoffeeScript files to the precompile array.
The rake task also generates a `manifest.yml` that contains a list with all your assets and their respective fingerprints. This is used by the Rails helper methods to avoid handing the mapping requests back to Sprockets. A typical manifest file looks like: