railties 3.0.11 → 3.0.12.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. data/lib/rails/version.rb +2 -2
  2. metadata +305 -461
  3. data/guides/output/2_2_release_notes.html +0 -565
  4. data/guides/output/2_3_release_notes.html +0 -713
  5. data/guides/output/3_0_release_notes.html +0 -652
  6. data/guides/output/3_1_release_notes.html +0 -670
  7. data/guides/output/action_controller_overview.html +0 -925
  8. data/guides/output/action_mailer_basics.html +0 -658
  9. data/guides/output/action_view_overview.html +0 -1471
  10. data/guides/output/active_model_basics.html +0 -349
  11. data/guides/output/active_record_basics.html +0 -364
  12. data/guides/output/active_record_querying.html +0 -1272
  13. data/guides/output/active_record_validations_callbacks.html +0 -1292
  14. data/guides/output/active_resource_basics.html +0 -252
  15. data/guides/output/active_support_core_extensions.html +0 -3374
  16. data/guides/output/ajax_on_rails.html +0 -412
  17. data/guides/output/api_documentation_guidelines.html +0 -317
  18. data/guides/output/asset_pipeline.html +0 -691
  19. data/guides/output/association_basics.html +0 -1742
  20. data/guides/output/caching_with_rails.html +0 -533
  21. data/guides/output/command_line.html +0 -662
  22. data/guides/output/configuring.html +0 -811
  23. data/guides/output/contribute.html +0 -216
  24. data/guides/output/contributing_to_ruby_on_rails.html +0 -465
  25. data/guides/output/credits.html +0 -210
  26. data/guides/output/debugging_rails_applications.html +0 -791
  27. data/guides/output/engines.html +0 -673
  28. data/guides/output/form_helpers.html +0 -850
  29. data/guides/output/generators.html +0 -725
  30. data/guides/output/getting_started.html +0 -1980
  31. data/guides/output/i18n.html +0 -1054
  32. data/guides/output/images/belongs_to.png +0 -0
  33. data/guides/output/images/book_icon.gif +0 -0
  34. data/guides/output/images/bullet.gif +0 -0
  35. data/guides/output/images/challenge.png +0 -0
  36. data/guides/output/images/chapters_icon.gif +0 -0
  37. data/guides/output/images/check_bullet.gif +0 -0
  38. data/guides/output/images/credits_pic_blank.gif +0 -0
  39. data/guides/output/images/csrf.png +0 -0
  40. data/guides/output/images/customized_error_messages.png +0 -0
  41. data/guides/output/images/edge_badge.png +0 -0
  42. data/guides/output/images/error_messages.png +0 -0
  43. data/guides/output/images/feature_tile.gif +0 -0
  44. data/guides/output/images/footer_tile.gif +0 -0
  45. data/guides/output/images/fxn.png +0 -0
  46. data/guides/output/images/grey_bullet.gif +0 -0
  47. data/guides/output/images/habtm.png +0 -0
  48. data/guides/output/images/has_many.png +0 -0
  49. data/guides/output/images/has_many_through.png +0 -0
  50. data/guides/output/images/has_one.png +0 -0
  51. data/guides/output/images/has_one_through.png +0 -0
  52. data/guides/output/images/header_backdrop.png +0 -0
  53. data/guides/output/images/header_tile.gif +0 -0
  54. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  55. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  56. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  57. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  58. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  59. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  60. data/guides/output/images/icons/README +0 -5
  61. data/guides/output/images/icons/callouts/1.png +0 -0
  62. data/guides/output/images/icons/callouts/10.png +0 -0
  63. data/guides/output/images/icons/callouts/11.png +0 -0
  64. data/guides/output/images/icons/callouts/12.png +0 -0
  65. data/guides/output/images/icons/callouts/13.png +0 -0
  66. data/guides/output/images/icons/callouts/14.png +0 -0
  67. data/guides/output/images/icons/callouts/15.png +0 -0
  68. data/guides/output/images/icons/callouts/2.png +0 -0
  69. data/guides/output/images/icons/callouts/3.png +0 -0
  70. data/guides/output/images/icons/callouts/4.png +0 -0
  71. data/guides/output/images/icons/callouts/5.png +0 -0
  72. data/guides/output/images/icons/callouts/6.png +0 -0
  73. data/guides/output/images/icons/callouts/7.png +0 -0
  74. data/guides/output/images/icons/callouts/8.png +0 -0
  75. data/guides/output/images/icons/callouts/9.png +0 -0
  76. data/guides/output/images/icons/caution.png +0 -0
  77. data/guides/output/images/icons/example.png +0 -0
  78. data/guides/output/images/icons/home.png +0 -0
  79. data/guides/output/images/icons/important.png +0 -0
  80. data/guides/output/images/icons/next.png +0 -0
  81. data/guides/output/images/icons/note.png +0 -0
  82. data/guides/output/images/icons/prev.png +0 -0
  83. data/guides/output/images/icons/tip.png +0 -0
  84. data/guides/output/images/icons/up.png +0 -0
  85. data/guides/output/images/icons/warning.png +0 -0
  86. data/guides/output/images/jaimeiniesta.jpg +0 -0
  87. data/guides/output/images/nav_arrow.gif +0 -0
  88. data/guides/output/images/polymorphic.png +0 -0
  89. data/guides/output/images/posts_index.png +0 -0
  90. data/guides/output/images/radar.png +0 -0
  91. data/guides/output/images/rails_guides_logo.gif +0 -0
  92. data/guides/output/images/rails_logo_remix.gif +0 -0
  93. data/guides/output/images/rails_welcome.png +0 -0
  94. data/guides/output/images/session_fixation.png +0 -0
  95. data/guides/output/images/tab_grey.gif +0 -0
  96. data/guides/output/images/tab_info.gif +0 -0
  97. data/guides/output/images/tab_note.gif +0 -0
  98. data/guides/output/images/tab_red.gif +0 -0
  99. data/guides/output/images/tab_yellow.gif +0 -0
  100. data/guides/output/images/tab_yellow.png +0 -0
  101. data/guides/output/images/validation_error_messages.png +0 -0
  102. data/guides/output/images/vijaydev.jpg +0 -0
  103. data/guides/output/index.html +0 -300
  104. data/guides/output/initialization.html +0 -1087
  105. data/guides/output/javascripts/guides.js +0 -7
  106. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  107. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  108. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  109. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  110. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  111. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  112. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  113. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  114. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  115. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  116. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  117. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  118. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  119. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  120. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  121. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  122. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  123. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  124. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  125. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  126. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  127. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  128. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  129. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  130. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  131. data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
  132. data/guides/output/layout.html +0 -312
  133. data/guides/output/layouts_and_rendering.html +0 -1257
  134. data/guides/output/migrations.html +0 -751
  135. data/guides/output/nested_model_forms.html +0 -350
  136. data/guides/output/performance_testing.html +0 -858
  137. data/guides/output/plugins.html +0 -590
  138. data/guides/output/rails_application_templates.html +0 -368
  139. data/guides/output/rails_on_rack.html +0 -408
  140. data/guides/output/routing.html +0 -1246
  141. data/guides/output/ruby_on_rails_guides_guidelines.html +0 -218
  142. data/guides/output/security.html +0 -968
  143. data/guides/output/stylesheets/fixes.css +0 -16
  144. data/guides/output/stylesheets/main.css +0 -445
  145. data/guides/output/stylesheets/print.css +0 -52
  146. data/guides/output/stylesheets/reset.css +0 -43
  147. data/guides/output/stylesheets/style.css +0 -13
  148. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
  149. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  150. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  151. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  152. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  153. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  154. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  155. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  156. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  157. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  158. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  159. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  160. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  161. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  162. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  163. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  164. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  165. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  166. data/guides/output/testing.html +0 -1182
@@ -1,691 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
-
4
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
- <head>
6
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
-
8
- <title>Ruby on Rails Guides: Asset Pipeline</title>
9
-
10
- <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
- <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
-
13
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
-
16
- <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
- </head>
18
- <body class="guide">
19
- <div id="topNav">
20
- <div class="wrapper">
21
- <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
22
- <a href="http://rubyonrails.org/">Overview</a> |
23
- <a href="http://rubyonrails.org/download">Download</a> |
24
- <a href="http://rubyonrails.org/deploy">Deploy</a> |
25
- <a href="https://github.com/rails/rails">Code</a> |
26
- <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
27
- <a href="http://rubyonrails.org/documentation">Documentation</a> |
28
- <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
29
- <a href="http://rubyonrails.org/community">Community</a> |
30
- <a href="http://weblog.rubyonrails.org/">Blog</a>
31
- </div>
32
- </div>
33
- <div id="header">
34
- <div class="wrapper clearfix">
35
- <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
36
- <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
37
- <ul class="nav">
38
- <li><a href="index.html">Home</a></li>
39
- <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
40
- <div id="guides" class="clearfix" style="display: none;">
41
- <hr />
42
- <dl class="L">
43
- <dt>Start Here</dt>
44
- <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
45
- <dt>Models</dt>
46
- <dd><a href="migrations.html">Rails Database Migrations</a></dd>
47
- <dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
48
- <dd><a href="association_basics.html">Active Record Associations</a></dd>
49
- <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
50
- <dt>Views</dt>
51
- <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
52
- <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
53
- <dt>Controllers</dt>
54
- <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
55
- <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
56
- </dl>
57
- <dl class="R">
58
- <dt>Digging Deeper</dt>
59
- <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
60
- <dd><a href="i18n.html">Rails Internationalization API</a></dd>
61
- <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
62
- <dd><a href="testing.html">Testing Rails Applications</a></dd>
63
- <dd><a href="security.html">Securing Rails Applications</a></dd>
64
- <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
65
- <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
66
- <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
67
- <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
68
- <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
69
- <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
70
-
71
- <dt>Extending Rails</dt>
72
- <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
73
- <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
74
- <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
75
-
76
- <dt>Contributing to Ruby on Rails</dt>
77
- <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
78
- <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
79
- <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
80
-
81
- <dt>Release Notes</dt>
82
- <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
83
- <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
84
- <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
85
- <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
86
- </dl>
87
- </div>
88
- </li>
89
- <li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
90
- <li><a href="credits.html">Credits</a></li>
91
- </ul>
92
- </div>
93
- </div>
94
- <hr class="hide" />
95
-
96
- <div id="feature">
97
- <div class="wrapper">
98
- <h2>Asset Pipeline</h2>
99
- <p>This guide covers the asset pipeline introduced in Rails 3.1.
100
- By referring to this guide you will be able to:</p>
101
- <ul>
102
- <li>Understand what the asset pipeline is and what it does</li>
103
- <li>Properly organize your application assets</li>
104
- <li>Understand the benefits of the asset pipeline</li>
105
- <li>Adding a pre-processor to the pipeline</li>
106
- <li>Package assets with a gem</li>
107
- </ul>
108
-
109
- <div id="subCol">
110
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
111
- <ol class="chapters">
112
- <li><a href="#what-is-the-asset-pipeline">What is the Asset Pipeline?</a><ul><li><a href="#main-features">Main Features</a></li> <li><a href="#what-is-fingerprinting-and-why-should-i-care">What is Fingerprinting and Why Should I Care?</a></li></ul></li><li><a href="#how-to-use-the-asset-pipeline">How to Use the Asset Pipeline</a><ul><li><a href="#asset-organization">Asset Organization</a></li> <li><a href="#coding-links-to-assets">Coding Links to Assets</a></li> <li><a href="#manifest-files-and-directives">Manifest Files and Directives</a></li> <li><a href="#preprocessing">Preprocessing</a></li></ul></li><li><a href="#in-development">In Development</a><ul><li><a href="#turning-debugging-off">Turning Debugging off</a></li></ul></li><li><a href="#in-production">In Production</a><ul><li><a href="#precompiling-assets">Precompiling Assets</a></li> <li><a href="#live-compilation">Live Compilation</a></li></ul></li><li><a href="#customizing-the-pipeline">Customizing the Pipeline</a><ul><li><a href="#css-compression"><span class="caps">CSS</span> Compression</a></li> <li><a href="#javascript-compression">JavaScript Compression</a></li> <li><a href="#using-your-own-compressor">Using Your Own Compressor</a></li> <li><a href="#changing-the-_assets_-path">Changing the <em>assets</em> Path</a></li> <li><a href="#x-sendfile-headers">X-Sendfile Headers</a></li></ul></li><li><a href="#how-caching-works">How Caching Works</a></li><li><a href="#adding-assets-to-your-gems">Adding Assets to Your Gems</a></li><li><a href="#making-your-library-or-gem-a-pre-processor">Making Your Library or Gem a Pre-Processor</a></li><li><a href="#upgrading-from-old-versions-of-rails">Upgrading from Old Versions of Rails</a></li></ol></div>
113
- </div>
114
- </div>
115
-
116
- <div id="container">
117
- <div class="wrapper">
118
- <div id="mainCol">
119
- <h3 id="what-is-the-asset-pipeline">1 What is the Asset Pipeline?</h3>
120
- <p>The asset pipeline provides a framework to concatenate and minify or compress JavaScript and <span class="caps">CSS</span> assets. It also adds the ability to write these assets in other languages such as CoffeeScript, Sass and <span class="caps">ERB</span>.</p>
121
- <p>Prior to Rails 3.1 these features were added through third-party Ruby libraries such as Jammit and Sprockets. Rails 3.1 is integrated with Sprockets through Action Pack which depends on the <tt>sprockets</tt> gem, by default.</p>
122
- <p>By having this as a core feature of Rails, all developers can benefit from the power of having their assets pre-processed, compressed and minified by one central library, Sprockets. This is part of Rails&#8217; &#8220;fast by default&#8221; strategy as outlined by <span class="caps">DHH</span> in his keynote at RailsConf 2011.</p>
123
- <p>In Rails 3.1, the asset pipeline is enabled by default. It can be disabled in <tt>config/application.rb</tt> by putting this line inside the application class definition:</p>
124
- <div class="code_container">
125
- <pre class="brush: ruby; gutter: false; toolbar: false">
126
- config.assets.enabled = false
127
- </pre>
128
- </div>
129
- <p>You can also disable it while creating a new application by passing the <tt>&#8212;skip-sprockets</tt> option.</p>
130
- <div class="code_container">
131
- <pre class="brush: plain; gutter: false; toolbar: false">
132
- rails new appname --skip-sprockets
133
- </pre>
134
- </div>
135
- <p>It is recommended that you use the defaults for all new apps.</p>
136
- <h4 id="main-features">1.1 Main Features</h4>
137
- <p>The first feature of the pipeline is to concatenate assets. This is important in a production environment, as it reduces the number of requests that a browser must make to render a web page.</p>
138
- <p>While Rails already has a feature to concatenate these types of assets by placing <tt>:cache => true</tt> at the end of tags such as <tt>javascript_include_tag</tt> and <tt>stylesheet_link_tag</tt>, it has a series of limitations. For example, it cannot generate the caches in advance, and it is not able to transparently include assets provided by third-party libraries.</p>
139
- <p>The default behavior in Rails 3.1 and onward is to concatenate all files into one master file each for JS and <span class="caps">CSS</span>. However, you can separate files or groups of files if required (see below). In production, an MD5 fingerprint is inserted into each filename so that the file is cached by the web browser but can be invalidated if the fingerprint is altered.</p>
140
- <p>The second feature is to minify or compress assets. For <span class="caps">CSS</span>, this usually involves removing whitespace and comments. For JavaScript, more complex processes can be applied. You can choose from a set of built in options or specify your own.</p>
141
- <p>The third feature is the ability to code these assets using another language, or language extension. These include Sass for <span class="caps">CSS</span>, CoffeeScript for JavaScript, and <span class="caps">ERB</span> for both.</p>
142
- <h4 id="what-is-fingerprinting-and-why-should-i-care">1.2 What is Fingerprinting and Why Should I Care?</h4>
143
- <p>Fingerprinting is a technique whereby the filenames of content that is static or infrequently updated are altered to be unique to the content contained in the file.</p>
144
- <p>When a filename is unique and based on its content, <span class="caps">HTTP</span> headers can be set to encourage caches everywhere (at ISPs, in browsers) to keep their own copy of the content. When the content is updated, the fingerprint will change and the remote clients will request the new file. This is generally known as <em>cache busting</em>.</p>
145
- <p>The most effective technique is to insert a hash of the content into the name, usually at the end. For example a <span class="caps">CSS</span> file <tt>global.css</tt> is hashed and the filename is updated to incorporate the digest, for example becoming:</p>
146
- <div class="code_container">
147
- <pre class="brush: plain; gutter: false; toolbar: false">
148
- global-908e25f4bf641868d8683022a5b62f54.css
149
- </pre>
150
- </div>
151
- <p>This is the strategy adopted by the Rails asset pipeline.</p>
152
- <p>Rails&#8217; old strategy was to append a query string to every asset linked with a built-in helper. In the source the generated code looked like this:</p>
153
- <div class="code_container">
154
- <pre class="brush: plain; gutter: false; toolbar: false">
155
- /stylesheets/global.css?1309495796
156
- </pre>
157
- </div>
158
- <p>This has several disadvantages:</p>
159
- <ol>
160
- <li>
161
- <strong>Not all caches will cache content with a query string</strong>.<br>
162
- <a href="http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/">Steve Souders recommends</a>, &#8220;&#8230;avoiding a querystring for cacheable resources&#8221;. He found that in this case 5-20% of requests will not be cached. Query strings in particular do not work at all with some CDNs for cache invalidation.
163
- </li>
164
- <li>
165
- <strong>The file name can change between nodes in multi-server environments.</strong><br>
166
- The query string in Rails is based on the modification time of the files. When assets are deployed to a cluster, there is no guarantee that the timestamps will be the same, resulting in different values being used depending on which server handles the request.
167
- </li>
168
- </ol>
169
- <p>The other problem is that when static assets are deployed with each new release of code, the mtime of <em>all</em> these files changes, forcing all remote clients to fetch them again, even when the content of those assets has not changed.</p>
170
- <p>Fingerprinting fixes these problems by avoiding query strings, and by ensuring filenames are consistent based on their content.</p>
171
- <p>Fingerprinting is enabled by default for production and disabled for all the others environments. You can enable or disable it in your configuration through the <tt>config.assets.digest</tt> option.</p>
172
- <p>More reading:</p>
173
- <ul>
174
- <li><a href="http://code.google.com/speed/page-speed/docs/caching.html">Optimize caching</a></li>
175
- <li><a href="http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/">Revving Filenames: don’t use querystring</a></li>
176
- </ul>
177
- <h3 id="how-to-use-the-asset-pipeline">2 How to Use the Asset Pipeline</h3>
178
- <p>In previous versions of Rails, all assets were located in subdirectories of <tt>public</tt> such as <tt>images</tt>, <tt>javascripts</tt> and <tt>stylesheets</tt>. With the asset pipeline, the preferred location for these assets is now the <tt>app/assets</tt> directory. Files in this directory are served by the Sprockets middleware included in the sprockets gem.</p>
179
- <p>This is not to say that assets can (or should) no longer be placed in <tt>public</tt>; they still can be and will be served as static files by the application or web server. You would only use <tt>app/assets</tt> if you wish your files to undergo some pre-processing before they are served.</p>
180
- <p>In production, the default is to precompile these files to <tt>public/assets</tt> so that they can be more efficiently delivered by the web server.</p>
181
- <p>When a scaffold or controller is generated for the application, Rails also generates a JavaScript file (or CoffeeScript file if the <tt>coffee-rails</tt> gem is in the <tt>Gemfile</tt>) and a Cascading Style Sheet file (or <span class="caps">SCSS</span> file if <tt>sass-rails</tt> is in the <tt>Gemfile</tt>) for that controller.</p>
182
- <p>For example, if a <tt>ProjectsController</tt> is generated, there will be a new file at <tt>app/assets/javascripts/projects.js.coffee</tt> and another at <tt>app/assets/stylesheets/projects.css.scss</tt>. You should put any JavaScript or <span class="caps">CSS</span> unique to a controller inside their respective asset files, as these files can then be loaded just for these controllers with lines such as <tt><%= javascript_include_tag params[:controller] %></tt> or <tt><%= stylesheet_link_tag params[:controller] %></tt>.</p>
183
- <div class="note"><p>You will need a <a href="https://github.com/sstephenson/execjs#readme">ExecJS</a> 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 <a href="https://github.com/sstephenson/execjs#readme">ExecJS</a> documentation to know all supported JavaScript runtimes.</p></div>
184
- <h4 id="asset-organization">2.1 Asset Organization</h4>
185
- <p>Assets can be placed inside an application in one of three locations: <tt>app/assets</tt>, <tt>lib/assets</tt> or <tt>vendor/assets</tt>.</p>
186
- <p><tt>app/assets</tt> is for assets that are owned by the application, such as custom images, JavaScript files or stylesheets.</p>
187
- <p><tt>lib/assets</tt> is for your own libraries&#8217; code that doesn&#8217;t really fit into the scope of the application or those libraries which are shared across applications.</p>
188
- <p><tt>vendor/assets</tt> is for assets that are owned by outside entities, such as code for JavaScript plugins.</p>
189
- <p>All subdirectories that exist within these three locations are added to the search path for Sprockets (visible by calling <tt>Rails.application.config.assets.paths</tt> in a console). When an asset is requested, these paths are traversed to see if they contain an asset matching the name specified. Once an asset has been found, it&#8217;s processed by Sprockets and served.</p>
190
- <p>You can add additional (fully qualified) paths to the pipeline in <tt>config/application.rb</tt>. For example:</p>
191
- <div class="code_container">
192
- <pre class="brush: ruby; gutter: false; toolbar: false">
193
- config.assets.paths &lt;&lt; Rails.root.join(&quot;app&quot;, &quot;assets&quot;, &quot;flash&quot;)
194
- </pre>
195
- </div>
196
- <h4 id="coding-links-to-assets">2.2 Coding Links to Assets</h4>
197
- <p>Sprockets does not add any new methods to access your assets &#8211; you still use the familiar <tt>javascript_include_tag</tt> and <tt>stylesheet_link_tag</tt>.</p>
198
- <div class="code_container">
199
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
200
- &lt;%= stylesheet_link_tag &quot;application&quot; %&gt;
201
- &lt;%= javascript_include_tag &quot;application&quot; %&gt;
202
- </pre>
203
- </div>
204
- <p>In regular views you can access images in the <tt>assets/images</tt> directory like this:</p>
205
- <div class="code_container">
206
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
207
- &lt;%= image_tag &quot;rails.png&quot; %&gt;
208
- </pre>
209
- </div>
210
- <p>Provided that the pipeline is enabled within your application (and not disabled in the current environment context), this file is served by Sprockets. If a file exists at <tt>public/assets/rails.png</tt> it is served by the webserver.</p>
211
- <p>Alternatively, a request for a file with an MD5 hash such as <tt>public/assets/rails-af27b6a414e6da00003503148be9b409.png</tt> is treated the same way. How these hashes are generated is covered in the <a href="#in-production">In Production</a> section later on in this guide.</p>
212
- <p>Sprockets will also look through the paths specified in <tt>config.assets.paths</tt> which includes the standard application paths and any path added by Rails engines.</p>
213
- <p>Images can also be organized into subdirectories if required, and they can be accessed by specifying the directory&#8217;s name in the tag:</p>
214
- <div class="code_container">
215
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
216
- &lt;%= image_tag &quot;icons/rails.png&quot; %&gt;
217
- </pre>
218
- </div>
219
- <h5 id="css-and-erb">2.2.1 <span class="caps">CSS</span> and <span class="caps">ERB</span></h5>
220
- <p>If you add an <tt>erb</tt> extension to a <span class="caps">CSS</span> asset, making it something such as <tt>application.css.erb</tt>, then helpers like <tt>asset_path</tt> are available in your <span class="caps">CSS</span> rules:</p>
221
- <div class="code_container">
222
- <pre class="brush: plain; gutter: false; toolbar: false">
223
- .class { background-image: url(&lt;%= asset_path 'image.png' %&gt;) }
224
- </pre>
225
- </div>
226
- <p>This writes the path to the particular asset being referenced. In this example, it would make sense to have an image in one of the asset load paths, such as <tt>app/assets/images/image.png</tt>, which would be referenced here. If this image is already available in <tt>public/assets</tt> as a fingerprinted file, then that path is referenced.</p>
227
- <p>If you want to use a <a href="http://en.wikipedia.org/wiki/Data_URI_scheme">data <span class="caps">URI</span></a> &#8212; a method of embedding the image data directly into the <span class="caps">CSS</span> file &#8212; you can use the <tt>asset_data_uri</tt> helper.</p>
228
- <div class="code_container">
229
- <pre class="brush: plain; gutter: false; toolbar: false">
230
- #logo { background: url(&lt;%= asset_data_uri 'logo.png' %&gt;) }
231
- </pre>
232
- </div>
233
- <p>This inserts a correctly-formatted data <span class="caps">URI</span> into the <span class="caps">CSS</span> source.</p>
234
- <p>Note that the closing tag cannot be of the style <tt>-%></tt>.</p>
235
- <h5 id="css-and-sass">2.2.2 <span class="caps">CSS</span> and Sass</h5>
236
- <p>When using the asset pipeline, paths to assets must be re-written and <tt>sass-rails</tt> provides <tt>-url</tt> and <tt>-path</tt> helpers (hyphenated in Sass, underscored in Ruby) for the following asset classes: image, font, video, audio, JavaScript and stylesheet.</p>
237
- <ul>
238
- <li><tt>image-url("rails.png")</tt> becomes <tt>url(/assets/rails.png)</tt></li>
239
- <li><tt>image-path("rails.png")</tt> becomes <tt>"/assets/rails.png"</tt>.</li>
240
- </ul>
241
- <p>The more generic form can also be used but the asset path and class must both be specified:</p>
242
- <ul>
243
- <li><tt>asset-url("rails.png", image)</tt> becomes <tt>url(/assets/rails.png)</tt></li>
244
- <li><tt>asset-path("rails.png", image)</tt> becomes <tt>"/assets/rails.png"</tt></li>
245
- </ul>
246
- <h5 id="javascript-coffeescript-and-erb">2.2.3 JavaScript/CoffeeScript and <span class="caps">ERB</span></h5>
247
- <p>If you add an <tt>erb</tt> extension to a JavaScript asset, making it something such as <tt>application.js.erb</tt>, then you can use the <tt>asset_path</tt> helper in your JavaScript code:</p>
248
- <div class="code_container">
249
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
250
- $('#logo').attr({
251
- src: &quot;&lt;%= asset_path('logo.png') %&gt;&quot;
252
- });
253
- </pre>
254
- </div>
255
- <p>This writes the path to the particular asset being referenced.</p>
256
- <p>Similarly, you can use the <tt>asset_path</tt> helper in CoffeeScript files with <tt>erb</tt> extension (eg. <tt>application.js.coffee.erb</tt>):</p>
257
- <div class="code_container">
258
- <pre class="brush: plain; gutter: false; toolbar: false">
259
- $('#logo').attr src: &quot;&lt;%= asset_path('logo.png') %&gt;&quot;
260
- </pre>
261
- </div>
262
- <h4 id="manifest-files-and-directives">2.3 Manifest Files and Directives</h4>
263
- <p>Sprockets uses manifest files to determine which assets to include and serve. These manifest files contain <em>directives</em> &#8212; instructions that tell Sprockets which files to require in order to build a single <span class="caps">CSS</span> or JavaScript file. With these directives, Sprockets loads the files specified, processes them if necessary, concatenates them into one single file and then compresses them (if <tt>Rails.application.config.assets.compress</tt> is true). By serving one file rather than many, the load time of pages are greatly reduced as there are fewer requests to make.</p>
264
- <p>For example, in the default Rails application there&#8217;s a <tt>app/assets/javascripts/application.js</tt> file which contains the following lines:</p>
265
- <div class="code_container">
266
- <pre class="brush: plain; gutter: false; toolbar: false">
267
- // ...
268
- //= require jquery
269
- //= require jquery_ujs
270
- //= require_tree .
271
- </pre>
272
- </div>
273
- <p>In JavaScript files, the directives begin with <tt>//=</tt>. In this case, the file is using the <tt>require</tt> and the <tt>require_tree</tt> directives. The <tt>require</tt> directive is used to tell Sprockets the files that you wish to require. Here, you are requiring the files <tt>jquery.js</tt> and <tt>jquery_ujs.js</tt> that are available somewhere in the search path for Sprockets. You need not supply the extensions explicitly. Sprockets assumes you are requiring a <tt>.js</tt> file when done from within a <tt>.js</tt> file.</p>
274
- <div class="note"><p>In Rails 3.1 the <tt>jquery-rails</tt> gem provides the <tt>jquery.js</tt> and <tt>jquery_ujs.js</tt> files via the asset pipeline. You won&#8217;t see them in the application tree.</p></div>
275
- <p>The <tt>require_tree</tt> directive tells Sprockets to recursively include <em>all</em> JavaScript files in this directory into the output. Only a path relative to the manifest file can be specified. There is also a <tt>require_directory</tt> directive which includes all JavaScript files only in the directory specified (no nesting).</p>
276
- <p>Directives are processed top to bottom, but the order in which files are included by <tt>require_tree</tt> is unspecified. You should not rely on any particular order among those. If you need to ensure some particular JavaScript ends up above some other, require it before in the manifest. Note that the family of <tt>require</tt> directives prevents files from being included twice in the output.</p>
277
- <p>There&#8217;s also a default <tt>app/assets/stylesheets/application.css</tt> file which contains these lines:</p>
278
- <div class="code_container">
279
- <pre class="brush: plain; gutter: false; toolbar: false">
280
- /* ...
281
- *= require_self
282
- *= require_tree .
283
- */
284
- </pre>
285
- </div>
286
- <p>The directives that work in the JavaScript files also work in stylesheets, obviously including stylesheets rather than JavaScript files. The <tt>require_tree</tt> directive here works the same way as the JavaScript one, requiring all stylesheets from the current directory.</p>
287
- <p>In this example <tt>require_self</tt> is used. This puts the <span class="caps">CSS</span> contained within the file (if any) at the precise location of the <tt>require_self</tt> call. If <tt>require_self</tt> is called more than once, only the last call is respected.</p>
288
- <div class="note"><p>If you want to use multiple Sass files, use the <a href="http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#import">Sass <tt>@import</tt> rule</a> instead of the Sprockets directives. Using Sprockets directives all Sass files exist within their own scope, making variables or mixins only available within the document they were defined in.</p></div>
289
- <p>You can have as many manifest files as you need. For example the <tt>admin.css</tt> and <tt>admin.js</tt> manifest could contain the JS and <span class="caps">CSS</span> files that are used for the admin section of an application.</p>
290
- <p>The same remarks about ordering made above apply. In particular, you can specify individual files and they are compiled in the order specified:</p>
291
- <div class="code_container">
292
- <pre class="brush: plain; gutter: false; toolbar: false">
293
- /* ...
294
- *= require reset
295
- *= require layout
296
- *= require chrome
297
- */
298
- </pre>
299
- </div>
300
- <h4 id="preprocessing">2.4 Preprocessing</h4>
301
- <p>The file extensions used on an asset determine what preprocessing is applied. When a controller or a scaffold is generated with the default Rails gemset, a CoffeeScript file and a <span class="caps">SCSS</span> file are generated in place of a regular JavaScript and <span class="caps">CSS</span> file. The example used before was a controller called &#8220;projects&#8221;, which generated an <tt>app/assets/javascripts/projects.js.coffee</tt> and an <tt>app/assets/stylesheets/projects.css.scss</tt> file.</p>
302
- <p>When these files are requested, they are processed by the processors provided by the <tt>coffee-script</tt> and <tt>sass-rails</tt> gems and then sent back to the browser as JavaScript and <span class="caps">CSS</span> respectively.</p>
303
- <p>Additional layers of preprocessing can be requested by adding other extensions, where each extension is processed in a right-to-left manner. These should be used in the order the processing should be applied. For example, a stylesheet called <tt>app/assets/stylesheets/projects.css.scss.erb</tt> is first processed as <span class="caps">ERB</span>, then <span class="caps">SCSS</span> and finally served as <span class="caps">CSS</span>. The same applies to a JavaScript file &#8212; <tt>app/assets/javascripts/projects.js.coffee.erb</tt> is processed as <span class="caps">ERB</span>, CoffeeScript, and served as JavaScript.</p>
304
- <p>Keep in mind that the order of these preprocessors is important. For example, if you called your JavaScript file <tt>app/assets/javascripts/projects.js.erb.coffee</tt> then it would be processed with the CoffeeScript interpreter first, which wouldn&#8217;t understand <span class="caps">ERB</span> and therefore you would run into problems.</p>
305
- <h3 id="in-development">3 In Development</h3>
306
- <p>In development mode assets are served as separate files in the order they are specified in the manifest file.</p>
307
- <p>This manifest <tt>app/assets/javascripts/application.js</tt>:</p>
308
- <div class="code_container">
309
- <pre class="brush: plain; gutter: false; toolbar: false">
310
- //= require core
311
- //= require projects
312
- //= require tickets
313
- </pre>
314
- </div>
315
- <p>would generate this <span class="caps">HTML</span>:</p>
316
- <div class="code_container">
317
- <pre class="brush: xml; gutter: false; toolbar: false">
318
- &lt;script src=&quot;/assets/core.js?body=1&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
319
- &lt;script src=&quot;/assets/projects.js?body=1&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
320
- &lt;script src=&quot;/assets/tickets.js?body=1&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
321
- </pre>
322
- </div>
323
- <p>The <tt>body</tt> param is required by Sprockets.</p>
324
- <h4 id="turning-debugging-off">3.1 Turning Debugging off</h4>
325
- <p>You can turn off debug mode by updating <tt>config/environments/development.rb</tt> to include:</p>
326
- <div class="code_container">
327
- <pre class="brush: ruby; gutter: false; toolbar: false">
328
- config.assets.debug = false
329
- </pre>
330
- </div>
331
- <p>When debug mode is off Sprockets concatenates and runs the necessary preprocessors on all files. With debug mode turned off the manifest above would generate instead:</p>
332
- <div class="code_container">
333
- <pre class="brush: xml; gutter: false; toolbar: false">
334
- &lt;script src=&quot;/assets/application.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
335
- </pre>
336
- </div>
337
- <p>Assets are compiled and cached on the first request after the server is started. Sprockets sets a <tt>must-revalidate</tt> Cache-Control <span class="caps">HTTP</span> header to reduce request overhead on subsequent requests &#8212; on these the browser gets a 304 (Not Modified) response.</p>
338
- <p>If any of the files in the manifest have changed between requests, the server responds with a new compiled file.</p>
339
- <p>Debug mode can also be enabled in the Rails helper methods:</p>
340
- <div class="code_container">
341
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
342
- &lt;%= stylesheet_link_tag &quot;application&quot;, :debug =&gt; true %&gt;
343
- &lt;%= javascript_include_tag &quot;application&quot;, :debug =&gt; true %&gt;
344
- </pre>
345
- </div>
346
- <p>The <tt>:debug</tt> option is redundant if debug mode is on.</p>
347
- <p>You could potentially also enable compression in development mode as a sanity check, and disable it on-demand as required for debugging.</p>
348
- <h3 id="in-production">4 In Production</h3>
349
- <p>In the production environment Rails uses the fingerprinting scheme outlined above. By default it is assumed that assets have been precompiled and will be served as static assets by your web server.</p>
350
- <p>During the precompilation phase an MD5 is generated from the contents of the compiled files, and inserted into the filenames as they are written to disc. These fingerprinted names are used by the Rails helpers in place of the manifest name.</p>
351
- <p>For example this:</p>
352
- <div class="code_container">
353
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
354
- &lt;%= javascript_include_tag &quot;application&quot; %&gt;
355
- &lt;%= stylesheet_link_tag &quot;application&quot; %&gt;
356
- </pre>
357
- </div>
358
- <p>generates something like this:</p>
359
- <div class="code_container">
360
- <pre class="brush: xml; gutter: false; toolbar: false">
361
- &lt;script src=&quot;/assets/application-908e25f4bf641868d8683022a5b62f54.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
362
- &lt;link href=&quot;/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css&quot; media=&quot;screen&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
363
- </pre>
364
- </div>
365
- <p>The fingerprinting behavior is controlled by the setting of <tt>config.assets.digest</tt> setting in Rails (which is <tt>true</tt> for production, <tt>false</tt> for everything else).</p>
366
- <div class="note"><p>Under normal circumstances the default option should not be changed. If there are no digests in the filenames, and far-future headers are set, remote clients will never know to refetch the files when their content changes.</p></div>
367
- <h4 id="precompiling-assets">4.1 Precompiling Assets</h4>
368
- <p>Rails comes bundled with a rake task to compile the asset manifests and other files in the pipeline to the disk.</p>
369
- <p>Compiled assets are written to the location specified in <tt>config.assets.prefix</tt>. The default setting will use the <tt>public/assets</tt> directory.</p>
370
- <p>You must use this task either during deployment or locally if you do not have write access to your production filesystem.</p>
371
- <p>The rake task is:</p>
372
- <div class="code_container">
373
- <pre class="brush: plain; gutter: false; toolbar: false">
374
- bundle exec rake assets:precompile
375
- </pre>
376
- </div>
377
- <p>For faster asset precompiles, you can partially load your application by setting
378
- <tt>config.assets.initialize_on_precompile</tt> to false in <tt>config/application.rb</tt>, though in that case templates
379
- cannot see application objects or methods. <strong>Heroku requires this to be false.</strong></p>
380
- <div class="warning"><p>If you set <tt>config.assets.initialize_on_precompile</tt> to false, be sure to
381
- test <tt>rake assets:precompile</tt> locally before deploying. It may expose bugs where
382
- your assets reference application objects or methods, since those are still
383
- in scope in development mode regardless of the value of this flag.</p></div>
384
- <p>Capistrano (v2.8.0 and above) has a recipe to handle this in deployment. Add the following line to <tt>Capfile</tt>:</p>
385
- <div class="code_container">
386
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
387
- load 'deploy/assets'
388
- </pre>
389
- </div>
390
- <p>This links the folder specified in <tt>config.assets.prefix</tt> to <tt>shared/assets</tt>. If you already use this shared folder you&#8217;ll need to write your own deployment task.</p>
391
- <p>It is important that this folder is shared between deployments so that remotely cached pages that reference the old compiled assets still work for the life of the cached page.</p>
392
- <div class="note"><p>If you are precompiling your assets locally, you can use <tt>bundle install --without assets</tt> on the server to avoid installing the assets gems (the gems in the assets group in the Gemfile).</p></div>
393
- <p>The default matcher for compiling files includes <tt>application.js</tt>, <tt>application.css</tt> and all files that do not end in <tt>js</tt> or <tt>css</tt>:</p>
394
- <div class="code_container">
395
- <pre class="brush: ruby; gutter: false; toolbar: false">
396
- [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]
397
- </pre>
398
- </div>
399
- <p>If you have other manifests or individual stylesheets and JavaScript files to include, you can add them to the <tt>precompile</tt> array:</p>
400
- <div class="code_container">
401
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
402
- config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
403
- </pre>
404
- </div>
405
- <p>The rake task also generates a <tt>manifest.yml</tt> that contains a list with all your assets and their respective fingerprints. This is used by the Rails helper methods and avoids handing the mapping requests back to Sprockets. A typical manifest file looks like:</p>
406
- <div class="code_container">
407
- <pre class="brush: plain; gutter: false; toolbar: false">
408
- ---
409
- rails.png: rails-bd9ad5a560b5a3a7be0808c5cd76a798.png
410
- jquery-ui.min.js: jquery-ui-7e33882a28fc84ad0e0e47e46cbf901c.min.js
411
- jquery.min.js: jquery-8a50feed8d29566738ad005e19fe1c2d.min.js
412
- application.js: application-3fdab497b8fb70d20cfc5495239dfc29.js
413
- application.css: application-8af74128f904600e41a6e39241464e03.css
414
- </pre>
415
- </div>
416
- <p>The default location for the manifest is the root of the location specified in <tt>config.assets.prefix</tt> (&#8216;/assets&#8217; by default).</p>
417
- <p>This can be changed with the <tt>config.assets.manifest</tt> option. A fully specified path is required:</p>
418
- <div class="code_container">
419
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
420
- config.assets.manifest = '/path/to/some/other/location'
421
- </pre>
422
- </div>
423
- <div class="note"><p>If there are missing precompiled files in production you will get an <tt>Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError</tt> exception indicating the name of the missing file(s).</p></div>
424
- <h5 id="server-configuration">4.1.1 Server Configuration</h5>
425
- <p>Precompiled assets exist on the filesystem and are served directly by your webserver. They do not have far-future headers by default, so to get the benefit of fingerprinting you&#8217;ll have to update your server configuration to add them.</p>
426
- <p>For Apache:</p>
427
- <div class="code_container">
428
- <pre class="brush: plain; gutter: false; toolbar: false">
429
- &lt;LocationMatch &quot;^/assets/.*$&quot;&gt;
430
- # Some browsers still send conditional-GET requests if there's a
431
- # Last-Modified header or an ETag header even if they haven't
432
- # reached the expiry date sent in the Expires header.
433
- Header unset Last-Modified
434
- Header unset ETag
435
- FileETag None
436
- # RFC says only cache for 1 year
437
- ExpiresActive On
438
- ExpiresDefault &quot;access plus 1 year&quot;
439
- &lt;/LocationMatch&gt;
440
- </pre>
441
- </div>
442
- <p>For nginx:</p>
443
- <div class="code_container">
444
- <pre class="brush: plain; gutter: false; toolbar: false">
445
- location ~ ^/assets/ {
446
- expires 1y;
447
- add_header Cache-Control public;
448
-
449
- # Some browsers still send conditional-GET requests if there's a
450
- # Last-Modified header or an ETag header even if they haven't
451
- # reached the expiry date sent in the Expires header.
452
- add_header Last-Modified &quot;&quot;;
453
- add_header ETag &quot;&quot;;
454
- break;
455
- }
456
- </pre>
457
- </div>
458
- <p>When files are precompiled, Sprockets also creates a <a href="http://en.wikipedia.org/wiki/Gzip">gzipped</a> (.gz) version of your assets. Web servers are typically configured to use a moderate compression ratio as a compromise, but since precompilation happens once Sprockets uses the maximum compression ratio, thus reducing the size of the data transfer to the minimum. On the other hand, web servers can be configured to serve compressed content directly from disk, rather than deflating non-compressed files themselves.</p>
459
- <p>Nginx is able to do this automatically enabling <tt>gzip_static</tt>:</p>
460
- <div class="code_container">
461
- <pre class="brush: plain; gutter: false; toolbar: false">
462
- location ~ ^/(assets)/ {
463
- root /path/to/public;
464
- gzip_static on; # to serve pre-gzipped version
465
- expires max;
466
- add_header Cache-Control public;
467
- }
468
- </pre>
469
- </div>
470
- <p>This directive is available if the core module that provides this feature was compiled with the web server. Ubuntu packages, even <tt>nginx-light</tt> have the module compiled. Otherwise, you may need to perform a manual compilation:</p>
471
- <div class="code_container">
472
- <pre class="brush: plain; gutter: false; toolbar: false">
473
- ./configure --with-http_gzip_static_module
474
- </pre>
475
- </div>
476
- <p>If you&#8217;re compiling nginx with Phusion Passenger you&#8217;ll need to pass that option when prompted.</p>
477
- <p>Unfortunately, a robust configuration for Apache is possible but tricky, please Google around.</p>
478
- <h4 id="live-compilation">4.2 Live Compilation</h4>
479
- <p>In some circumstances you may wish to use live compilation. In this mode all requests for assets in the pipeline are handled by Sprockets directly.</p>
480
- <p>To enable this option set:</p>
481
- <div class="code_container">
482
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
483
- config.assets.compile = true
484
- </pre>
485
- </div>
486
- <p>On the first request the assets are compiled and cached as outlined in development above, and the manifest names used in the helpers are altered to include the MD5 hash.</p>
487
- <p>Sprockets also sets the <tt>Cache-Control</tt> <span class="caps">HTTP</span> header to <tt>max-age=31536000</tt>. This signals all caches between your server and the client browser that this content (the file served) can be cached for 1 year. The effect of this is to reduce the number of requests for this asset from your server; the asset has a good chance of being in the local browser cache or some intermediate cache.</p>
488
- <p>This mode uses more memory, performs poorer than the default and is not recommended.</p>
489
- <p>When deploying a production application to a system without any pre-existing JavaScript runtimes, you may want to add one to your Gemfile:</p>
490
- <div class="code_container">
491
- <pre class="brush: plain; gutter: false; toolbar: false">
492
- group :production do
493
- gem 'therubyracer'
494
- end
495
- </pre>
496
- </div>
497
- <h3 id="customizing-the-pipeline">5 Customizing the Pipeline</h3>
498
- <h4 id="css-compression">5.1 <span class="caps">CSS</span> Compression</h4>
499
- <p>There is currently one option for compressing <span class="caps">CSS</span>, <span class="caps">YUI</span>. The <a href="http://developer.yahoo.com/yui/compressor/css.html"><span class="caps">YUI</span> <span class="caps">CSS</span> compressor</a> provides minification.</p>
500
- <p>The following line enables <span class="caps">YUI</span> compression, and requires the <tt>yui-compressor</tt> gem.</p>
501
- <div class="code_container">
502
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
503
- config.assets.css_compressor = :yui
504
- </pre>
505
- </div>
506
- <p>The <tt>config.assets.compress</tt> must be set to <tt>true</tt> to enable <span class="caps">CSS</span> compression.</p>
507
- <h4 id="javascript-compression">5.2 JavaScript Compression</h4>
508
- <p>Possible options for JavaScript compression are <tt>:closure</tt>, <tt>:uglifier</tt> and <tt>:yui</tt>. These require the use of the <tt>closure-compiler</tt>, <tt>uglifier</tt> or <tt>yui-compressor</tt> gems respectively.</p>
509
- <p>The default Gemfile includes <a href="https://github.com/lautis/uglifier">uglifier</a>. This gem wraps <a href="https://github.com/mishoo/UglifyJS">UglifierJS</a> (written for NodeJS) in Ruby. It compresses your code by removing white space and other magical things like changing your <tt>if</tt> and <tt>else</tt> statements to ternary operators where possible.</p>
510
- <p>The following line invokes <tt>uglifier</tt> for JavaScript compression.</p>
511
- <div class="code_container">
512
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
513
- config.assets.js_compressor = :uglifier
514
- </pre>
515
- </div>
516
- <p>The <tt>config.assets.compress</tt> must be set to <tt>true</tt> to enable JavaScript compression</p>
517
- <div class="note"><p>You will need a <a href="https://github.com/sstephenson/execjs#readme">ExecJS</a> supported runtime in order to use <tt>uglifier</tt>. If you are using Mac OS X or Windows you have installed a JavaScript runtime in your operating system. Check <a href="https://github.com/sstephenson/execjs#readme">ExecJS</a> documentation to know all supported JavaScript runtimes.</p></div>
518
- <h4 id="using-your-own-compressor">5.3 Using Your Own Compressor</h4>
519
- <p>The compressor config settings for <span class="caps">CSS</span> and JavaScript also take any Object. This object must have a <tt>compress</tt> method that takes a string as the sole argument and it must return a string.</p>
520
- <div class="code_container">
521
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
522
- class Transformer
523
- def compress(string)
524
- do_something_returning_a_string(string)
525
- end
526
- end
527
- </pre>
528
- </div>
529
- <p>To enable this, pass a <tt>new</tt> Object to the config option in <tt>application.rb</tt>:</p>
530
- <div class="code_container">
531
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
532
- config.assets.css_compressor = Transformer.new
533
- </pre>
534
- </div>
535
- <h4 id="changing-the-_assets_-path">5.4 Changing the <em>assets</em> Path</h4>
536
- <p>The public path that Sprockets uses by default is <tt>/assets</tt>.</p>
537
- <p>This can be changed to something else:</p>
538
- <div class="code_container">
539
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
540
- config.assets.prefix = &quot;/some_other_path&quot;
541
- </pre>
542
- </div>
543
- <p>This is a handy option if you have any existing project (pre Rails 3.1) that already uses this path or you wish to use this path for a new resource.</p>
544
- <h4 id="x-sendfile-headers">5.5 X-Sendfile Headers</h4>
545
- <p>The X-Sendfile header is a directive to the server to ignore the response from the application, and instead serve the file specified in the headers. This option is off by default, but can be enabled if your server supports it. When enabled, this passes responsibility for serving the file to the web server, which is faster.</p>
546
- <p>Apache and nginx support this option which is enabled in <tt>config/environments/production.rb</tt>.</p>
547
- <div class="code_container">
548
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
549
- # config.action_dispatch.x_sendfile_header = &quot;X-Sendfile&quot; # for apache
550
- # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
551
- </pre>
552
- </div>
553
- <div class="warning"><p>If you are upgrading an existing application and intend to use this option, take care to paste this configuration option only into <tt>production.rb</tt> (and not <tt>application.rb</tt>) and any other environment you define with production behavior.</p></div>
554
- <h3 id="how-caching-works">6 How Caching Works</h3>
555
- <p>Sprockets uses the default Rails cache store to cache assets in development and production.</p>
556
- <p><span class="caps">TODO</span>: Add more about changing the default store.</p>
557
- <h3 id="adding-assets-to-your-gems">7 Adding Assets to Your Gems</h3>
558
- <p>Assets can also come from external sources in the form of gems.</p>
559
- <p>A good example of this is the <tt>jquery-rails</tt> gem which comes with Rails as the standard JavaScript library gem. This gem contains an engine class which inherits from <tt>Rails::Engine</tt>. By doing this, Rails is informed that the directory for this gem may contain assets and the <tt>app/assets</tt>, <tt>lib/assets</tt> and <tt>vendor/assets</tt> directories of this engine are added to the search path of Sprockets.</p>
560
- <h3 id="making-your-library-or-gem-a-pre-processor">8 Making Your Library or Gem a Pre-Processor</h3>
561
- <p><span class="caps">TODO</span>: Registering gems on <a href="https://github.com/rtomayko/tilt">Tilt</a> enabling Sprockets to find them.</p>
562
- <h3 id="upgrading-from-old-versions-of-rails">9 Upgrading from Old Versions of Rails</h3>
563
- <p>There are two issues when upgrading. The first is moving the files to the new locations. See the section above for guidance on the correct locations for different file types.</p>
564
- <p>The second is updating the various environment files with the correct default options. The following changes reflect the defaults in version 3.1.0.</p>
565
- <p>In <tt>application.rb</tt>:</p>
566
- <div class="code_container">
567
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
568
- # Enable the asset pipeline
569
- config.assets.enabled = true
570
-
571
- # Version of your assets, change this if you want to expire all your assets
572
- config.assets.version = '1.0'
573
-
574
- # Change the path that assets are served from
575
- # config.assets.prefix = &quot;/assets&quot;
576
- </pre>
577
- </div>
578
- <p>In <tt>development.rb</tt>:</p>
579
- <div class="code_container">
580
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
581
- # Do not compress assets
582
- config.assets.compress = false
583
-
584
- # Expands the lines which load the assets
585
- config.assets.debug = true
586
- </pre>
587
- </div>
588
- <p>And in <tt>production.rb</tt>:</p>
589
- <div class="code_container">
590
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
591
- # Compress JavaScripts and CSS
592
- config.assets.compress = true
593
-
594
- # Choose the compressors to use
595
- # config.assets.js_compressor = :uglifier
596
- # config.assets.css_compressor = :yui
597
-
598
- # Don't fallback to assets pipeline if a precompiled asset is missed
599
- config.assets.compile = false
600
-
601
- # Generate digests for assets URLs.
602
- config.assets.digest = true
603
-
604
- # Defaults to Rails.root.join(&quot;public/assets&quot;)
605
- # config.assets.manifest = YOUR_PATH
606
-
607
- # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
608
- # config.assets.precompile += %w( search.js )
609
- </pre>
610
- </div>
611
- <p>There are no changes to <tt>test.rb</tt>. The defaults in the test environment are: <tt>config.assets.compile</tt> is true and <tt>config.assets.compress</tt>, <tt>config.assets.debug</tt> and <tt>config.assets.digest</tt> are false.</p>
612
- <p>The following should also be added to <tt>Gemfile</tt>:</p>
613
- <div class="code_container">
614
- <pre class="brush: plain; gutter: false; toolbar: false">
615
- # Gems used only for assets and not required
616
- # in production environments by default.
617
- group :assets do
618
- gem 'sass-rails', &quot;~&gt; 3.1.0&quot;
619
- gem 'coffee-rails', &quot;~&gt; 3.1.0&quot;
620
- gem 'uglifier'
621
- end
622
- </pre>
623
- </div>
624
- <p>If you use the <tt>assets</tt> group with Bundler, please make sure that your <tt>config/application.rb</tt> has the following Bundler require statement.</p>
625
- <div class="code_container">
626
- <pre class="brush: ruby; gutter: false; toolbar: false">
627
- if defined?(Bundler)
628
- # If you precompile assets before deploying to production, use this line
629
- Bundler.require *Rails.groups(:assets =&gt; %w(development test))
630
- # If you want your assets lazily compiled in production, use this line
631
- # Bundler.require(:default, :assets, Rails.env)
632
- end
633
- </pre>
634
- </div>
635
- <p>Instead of the old Rails 3.0 one</p>
636
- <div class="code_container">
637
- <pre class="brush: ruby; gutter: false; toolbar: false">
638
- # If you have a Gemfile, require the gems listed there, including any gems
639
- # you've limited to :test, :development, or :production.
640
- Bundler.require(:default, Rails.env) if defined?(Bundler)
641
- </pre>
642
- </div>
643
-
644
- <h3>Feedback</h3>
645
- <p>
646
- You're encouraged to help improve the quality of this guide.
647
- </p>
648
- <p>
649
- If you see any typos or factual errors you are confident to
650
- patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
651
- and push the change yourself. That branch of Rails has public write access.
652
- Commits are still reviewed, but that happens after you've submitted your
653
- contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
654
- cross-merged with master periodically.
655
- </p>
656
- <p>
657
- You may also find incomplete content, or stuff that is not up to date.
658
- Please do add any missing documentation for master. Check the
659
- <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
660
- for style and conventions.
661
- </p>
662
- <p>
663
- If for whatever reason you spot something to fix but cannot patch it yourself, please
664
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
665
- </p>
666
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
667
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
668
- </p>
669
- </div>
670
- </div>
671
- </div>
672
-
673
- <hr class="hide" />
674
- <div id="footer">
675
- <div class="wrapper">
676
- <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
677
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
678
- </div>
679
- </div>
680
-
681
- <script type="text/javascript" src="javascripts/guides.js"></script>
682
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
683
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
684
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
685
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
686
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
687
- <script type="text/javascript">
688
- SyntaxHighlighter.all()
689
- </script>
690
- </body>
691
- </html>