rails 4.2.0.beta3 → 4.2.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -7
  3. data/guides/output/2_2_release_notes.html +724 -0
  4. data/guides/output/2_3_release_notes.html +870 -0
  5. data/guides/output/3_0_release_notes.html +773 -0
  6. data/guides/output/3_1_release_notes.html +740 -0
  7. data/guides/output/3_2_release_notes.html +797 -0
  8. data/guides/output/4_0_release_notes.html +523 -0
  9. data/guides/output/4_1_release_notes.html +806 -0
  10. data/guides/output/4_2_release_notes.html +728 -0
  11. data/guides/output/Gemfile +6 -0
  12. data/guides/output/_license.html +226 -0
  13. data/guides/output/_welcome.html +233 -0
  14. data/guides/output/action_controller_overview.html +1335 -0
  15. data/guides/output/action_mailer_basics.html +928 -0
  16. data/guides/output/action_view_overview.html +1509 -0
  17. data/guides/output/active_job_basics.html +546 -0
  18. data/guides/output/active_model_basics.html +438 -0
  19. data/guides/output/active_record_basics.html +594 -0
  20. data/guides/output/active_record_callbacks.html +592 -0
  21. data/guides/output/active_record_migrations.html +1123 -0
  22. data/guides/output/active_record_postgresql.html +675 -0
  23. data/guides/output/active_record_querying.html +1796 -0
  24. data/guides/output/active_record_validations.html +1301 -0
  25. data/guides/output/active_support_core_extensions.html +3450 -0
  26. data/guides/output/active_support_instrumentation.html +1121 -0
  27. data/guides/output/api_documentation_guidelines.html +498 -0
  28. data/guides/output/asset_pipeline.html +1167 -0
  29. data/guides/output/association_basics.html +2086 -0
  30. data/guides/output/caching_with_rails.html +553 -0
  31. data/guides/output/command_line.html +791 -0
  32. data/guides/output/configuring.html +1055 -0
  33. data/guides/output/contributing_to_ruby_on_rails.html +657 -0
  34. data/guides/output/credits.html +284 -0
  35. data/guides/output/debugging_rails_applications.html +1014 -0
  36. data/guides/output/development_dependencies_install.html +478 -0
  37. data/guides/output/engines.html +1438 -0
  38. data/guides/output/form_helpers.html +1074 -0
  39. data/guides/output/generators.html +838 -0
  40. data/guides/output/getting_started.html +2092 -0
  41. data/guides/output/i18n.html +1198 -0
  42. data/guides/output/images/akshaysurve.jpg +0 -0
  43. data/guides/output/images/belongs_to.png +0 -0
  44. data/guides/output/images/book_icon.gif +0 -0
  45. data/guides/output/images/bullet.gif +0 -0
  46. data/guides/output/images/chapters_icon.gif +0 -0
  47. data/guides/output/images/check_bullet.gif +0 -0
  48. data/guides/output/images/credits_pic_blank.gif +0 -0
  49. data/guides/output/images/csrf.png +0 -0
  50. data/guides/output/images/edge_badge.png +0 -0
  51. data/guides/output/images/favicon.ico +0 -0
  52. data/guides/output/images/feature_tile.gif +0 -0
  53. data/guides/output/images/footer_tile.gif +0 -0
  54. data/guides/output/images/fxn.png +0 -0
  55. data/guides/output/images/getting_started/article_with_comments.png +0 -0
  56. data/guides/output/images/getting_started/challenge.png +0 -0
  57. data/guides/output/images/getting_started/confirm_dialog.png +0 -0
  58. data/guides/output/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
  59. data/guides/output/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  60. data/guides/output/images/getting_started/form_with_errors.png +0 -0
  61. data/guides/output/images/getting_started/index_action_with_edit_link.png +0 -0
  62. data/guides/output/images/getting_started/new_article.png +0 -0
  63. data/guides/output/images/getting_started/new_post.png +0 -0
  64. data/guides/output/images/getting_started/post_with_comments.png +0 -0
  65. data/guides/output/images/getting_started/rails_welcome.png +0 -0
  66. data/guides/output/images/getting_started/routing_error_no_controller.png +0 -0
  67. data/guides/output/images/getting_started/routing_error_no_route_matches.png +0 -0
  68. data/guides/output/images/getting_started/show_action_for_articles.png +0 -0
  69. data/guides/output/images/getting_started/show_action_for_posts.png +0 -0
  70. data/guides/output/images/getting_started/template_is_missing_articles_new.png +0 -0
  71. data/guides/output/images/getting_started/template_is_missing_posts_new.png +0 -0
  72. data/guides/output/images/getting_started/undefined_method_post_path.png +0 -0
  73. data/guides/output/images/getting_started/unknown_action_create_for_articles.png +0 -0
  74. data/guides/output/images/getting_started/unknown_action_create_for_posts.png +0 -0
  75. data/guides/output/images/getting_started/unknown_action_new_for_articles.png +0 -0
  76. data/guides/output/images/getting_started/unknown_action_new_for_posts.png +0 -0
  77. data/guides/output/images/grey_bullet.gif +0 -0
  78. data/guides/output/images/habtm.png +0 -0
  79. data/guides/output/images/has_many.png +0 -0
  80. data/guides/output/images/has_many_through.png +0 -0
  81. data/guides/output/images/has_one.png +0 -0
  82. data/guides/output/images/has_one_through.png +0 -0
  83. data/guides/output/images/header_backdrop.png +0 -0
  84. data/guides/output/images/header_tile.gif +0 -0
  85. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  86. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  87. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  88. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  89. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  90. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  91. data/guides/output/images/icons/README +5 -0
  92. data/guides/output/images/icons/callouts/1.png +0 -0
  93. data/guides/output/images/icons/callouts/10.png +0 -0
  94. data/guides/output/images/icons/callouts/11.png +0 -0
  95. data/guides/output/images/icons/callouts/12.png +0 -0
  96. data/guides/output/images/icons/callouts/13.png +0 -0
  97. data/guides/output/images/icons/callouts/14.png +0 -0
  98. data/guides/output/images/icons/callouts/15.png +0 -0
  99. data/guides/output/images/icons/callouts/2.png +0 -0
  100. data/guides/output/images/icons/callouts/3.png +0 -0
  101. data/guides/output/images/icons/callouts/4.png +0 -0
  102. data/guides/output/images/icons/callouts/5.png +0 -0
  103. data/guides/output/images/icons/callouts/6.png +0 -0
  104. data/guides/output/images/icons/callouts/7.png +0 -0
  105. data/guides/output/images/icons/callouts/8.png +0 -0
  106. data/guides/output/images/icons/callouts/9.png +0 -0
  107. data/guides/output/images/icons/caution.png +0 -0
  108. data/guides/output/images/icons/example.png +0 -0
  109. data/guides/output/images/icons/home.png +0 -0
  110. data/guides/output/images/icons/important.png +0 -0
  111. data/guides/output/images/icons/next.png +0 -0
  112. data/guides/output/images/icons/note.png +0 -0
  113. data/guides/output/images/icons/prev.png +0 -0
  114. data/guides/output/images/icons/tip.png +0 -0
  115. data/guides/output/images/icons/up.png +0 -0
  116. data/guides/output/images/icons/warning.png +0 -0
  117. data/guides/output/images/nav_arrow.gif +0 -0
  118. data/guides/output/images/oscardelben.jpg +0 -0
  119. data/guides/output/images/polymorphic.png +0 -0
  120. data/guides/output/images/radar.png +0 -0
  121. data/guides/output/images/rails4_features.png +0 -0
  122. data/guides/output/images/rails_guides_kindle_cover.jpg +0 -0
  123. data/guides/output/images/rails_guides_logo.gif +0 -0
  124. data/guides/output/images/rails_logo_remix.gif +0 -0
  125. data/guides/output/images/session_fixation.png +0 -0
  126. data/guides/output/images/tab_grey.gif +0 -0
  127. data/guides/output/images/tab_info.gif +0 -0
  128. data/guides/output/images/tab_note.gif +0 -0
  129. data/guides/output/images/tab_red.gif +0 -0
  130. data/guides/output/images/tab_yellow.gif +0 -0
  131. data/guides/output/images/tab_yellow.png +0 -0
  132. data/guides/output/images/vijaydev.jpg +0 -0
  133. data/guides/output/index.html +354 -0
  134. data/guides/output/initialization.html +876 -0
  135. data/guides/output/javascripts/guides.js +59 -0
  136. data/guides/output/javascripts/jquery.min.js +4 -0
  137. data/guides/output/javascripts/responsive-tables.js +43 -0
  138. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  139. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  140. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  141. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  142. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  143. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  144. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  145. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  146. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  147. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  148. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  149. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  150. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  151. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  152. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  153. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  154. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  155. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  156. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  157. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  158. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  159. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  160. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  161. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  162. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  163. data/guides/output/javascripts/syntaxhighlighter/shCore.js +17 -0
  164. data/guides/output/layout.html +448 -0
  165. data/guides/output/layouts_and_rendering.html +1541 -0
  166. data/guides/output/maintenance_policy.html +257 -0
  167. data/guides/output/migrations.html +1360 -0
  168. data/guides/output/nested_model_forms.html +412 -0
  169. data/guides/output/plugins.html +644 -0
  170. data/guides/output/rails_application_templates.html +450 -0
  171. data/guides/output/rails_on_rack.html +547 -0
  172. data/guides/output/routing.html +1631 -0
  173. data/guides/output/ruby_on_rails_guides_guidelines.html +329 -0
  174. data/guides/output/security.html +935 -0
  175. data/guides/output/stylesheets/fixes.css +16 -0
  176. data/guides/output/stylesheets/kindle.css +11 -0
  177. data/guides/output/stylesheets/main.css +713 -0
  178. data/guides/output/stylesheets/print.css +52 -0
  179. data/guides/output/stylesheets/reset.css +43 -0
  180. data/guides/output/stylesheets/responsive-tables.css +50 -0
  181. data/guides/output/stylesheets/style.css +13 -0
  182. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +226 -0
  183. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  184. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  185. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  186. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  187. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  188. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  189. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  190. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  191. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  192. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  193. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  194. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  195. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  196. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  197. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  198. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  199. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  200. data/guides/output/testing.html +1350 -0
  201. data/guides/output/upgrading_ruby_on_rails.html +1135 -0
  202. data/guides/output/working_with_javascript_in_rails.html +587 -0
  203. data/guides/source/4_0_release_notes.md +2 -2
  204. data/guides/source/4_2_release_notes.md +9 -2
  205. data/guides/source/action_controller_overview.md +3 -1
  206. data/guides/source/action_mailer_basics.md +1 -2
  207. data/guides/source/active_job_basics.md +25 -2
  208. data/guides/source/active_model_basics.md +350 -19
  209. data/guides/source/active_record_basics.md +2 -2
  210. data/guides/source/active_record_migrations.md +1 -1
  211. data/guides/source/active_record_validations.md +12 -4
  212. data/guides/source/active_support_core_extensions.md +7 -9
  213. data/guides/source/active_support_instrumentation.md +2 -0
  214. data/guides/source/association_basics.md +7 -7
  215. data/guides/source/configuring.md +24 -0
  216. data/guides/source/contributing_to_ruby_on_rails.md +1 -1
  217. data/guides/source/credits.html.erb +1 -1
  218. data/guides/source/documents.yaml +5 -0
  219. data/guides/source/engines.md +2 -2
  220. data/guides/source/form_helpers.md +6 -0
  221. data/guides/source/i18n.md +6 -9
  222. data/guides/source/layouts_and_rendering.md +1 -1
  223. data/guides/source/plugins.md +2 -2
  224. data/guides/source/rails_on_rack.md +4 -0
  225. data/guides/source/testing.md +1 -0
  226. data/guides/source/upgrading_ruby_on_rails.md +5 -5
  227. metadata +227 -27
@@ -0,0 +1,1167 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
6
+ <meta name="viewport" content="width=device-width, initial-scale=1"/>
7
+
8
+ <title>The Asset Pipeline — Ruby on Rails Guides</title>
9
+ <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
10
+ <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
11
+
12
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
13
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
14
+
15
+ <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
16
+
17
+ <link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
18
+ </head>
19
+ <body class="guide">
20
+ <div id="topNav">
21
+ <div class="wrapper">
22
+ <strong class="more-info-label">More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
23
+ <span class="red-button more-info-button">
24
+ More Ruby on Rails
25
+ </span>
26
+ <ul class="more-info-links s-hidden">
27
+ <li class="more-info"><a href="http://rubyonrails.org/">Overview</a></li>
28
+ <li class="more-info"><a href="http://rubyonrails.org/download">Download</a></li>
29
+ <li class="more-info"><a href="http://rubyonrails.org/deploy">Deploy</a></li>
30
+ <li class="more-info"><a href="https://github.com/rails/rails">Code</a></li>
31
+ <li class="more-info"><a href="http://rubyonrails.org/screencasts">Screencasts</a></li>
32
+ <li class="more-info"><a href="http://rubyonrails.org/documentation">Documentation</a></li>
33
+ <li class="more-info"><a href="http://rubyonrails.org/community">Community</a></li>
34
+ <li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
35
+ </ul>
36
+ </div>
37
+ </div>
38
+ <div id="header">
39
+ <div class="wrapper clearfix">
40
+ <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
41
+ <ul class="nav">
42
+ <li><a class="nav-item" href="index.html">Home</a></li>
43
+ <li class="guides-index guides-index-large">
44
+ <a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guides Index</a>
45
+ <div id="guides" class="clearfix" style="display: none;">
46
+ <hr />
47
+ <dl class="L">
48
+ <dt>Start Here</dt>
49
+ <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
50
+ <dt>Models</dt>
51
+ <dd><a href="active_record_basics.html">Active Record Basics</a></dd>
52
+ <dd><a href="active_record_migrations.html">Active Record Migrations</a></dd>
53
+ <dd><a href="active_record_validations.html">Active Record Validations</a></dd>
54
+ <dd><a href="active_record_callbacks.html">Active Record Callbacks</a></dd>
55
+ <dd><a href="association_basics.html">Active Record Associations</a></dd>
56
+ <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
57
+ <dt>Views</dt>
58
+ <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
59
+ <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
60
+ <dt>Controllers</dt>
61
+ <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
62
+ <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
63
+ </dl>
64
+ <dl class="R">
65
+ <dt>Digging Deeper</dt>
66
+ <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
67
+ <dd><a href="i18n.html">Rails Internationalization API</a></dd>
68
+ <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
69
+ <dd><a href="active_job_basics.html">Active Job Basics</a></dd>
70
+ <dd><a href="security.html">Securing Rails Applications</a></dd>
71
+ <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
72
+ <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
73
+ <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
74
+ <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
75
+ <dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd>
76
+ <dt>Extending Rails</dt>
77
+ <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
78
+ <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
79
+ <dt>Contributing to Ruby on Rails</dt>
80
+ <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
81
+ <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
82
+ <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
83
+ <dt>Maintenance Policy</dt>
84
+ <dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
85
+ <dt>Release Notes</dt>
86
+ <dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
87
+ <dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</a></dd>
88
+ <dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</a></dd>
89
+ <dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</a></dd>
90
+ <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
91
+ <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
92
+ <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
93
+ <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
94
+ </dl>
95
+ </div>
96
+ </li>
97
+ <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribute</a></li>
98
+ <li><a class="nav-item" href="credits.html">Credits</a></li>
99
+ <li class="guides-index guides-index-small">
100
+ <select class="guides-index-item nav-item">
101
+ <option value="index.html">Guides Index</option>
102
+ <optgroup label="Start Here">
103
+ <option value="getting_started.html">Getting Started with Rails</option>
104
+ </optgroup>
105
+ <optgroup label="Models">
106
+ <option value="active_record_basics.html">Active Record Basics</option>
107
+ <option value="active_record_migrations.html">Active Record Migrations</option>
108
+ <option value="active_record_validations.html">Active Record Validations</option>
109
+ <option value="active_record_callbacks.html">Active Record Callbacks</option>
110
+ <option value="association_basics.html">Active Record Associations</option>
111
+ <option value="active_record_querying.html">Active Record Query Interface</option>
112
+ </optgroup>
113
+ <optgroup label="Views">
114
+ <option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option>
115
+ <option value="form_helpers.html">Action View Form Helpers</option>
116
+ </optgroup>
117
+ <optgroup label="Controllers">
118
+ <option value="action_controller_overview.html">Action Controller Overview</option>
119
+ <option value="routing.html">Rails Routing from the Outside In</option>
120
+ </optgroup>
121
+ <optgroup label="Digging Deeper">
122
+ <option value="active_support_core_extensions.html">Active Support Core Extensions</option>
123
+ <option value="i18n.html">Rails Internationalization API</option>
124
+ <option value="action_mailer_basics.html">Action Mailer Basics</option>
125
+ <option value="active_job_basics.html">Active Job Basics</option>
126
+ <option value="security.html">Securing Rails Applications</option>
127
+ <option value="debugging_rails_applications.html">Debugging Rails Applications</option>
128
+ <option value="configuring.html">Configuring Rails Applications</option>
129
+ <option value="command_line.html">Rails Command Line Tools and Rake Tasks</option>
130
+ <option value="asset_pipeline.html">Asset Pipeline</option>
131
+ <option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option>
132
+ </optgroup>
133
+ <optgroup label="Extending Rails">
134
+ <option value="rails_on_rack.html">Rails on Rack</option>
135
+ <option value="generators.html">Creating and Customizing Rails Generators</option>
136
+ </optgroup>
137
+ <optgroup label="Contributing to Ruby on Rails">
138
+ <option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
139
+ <option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
140
+ <option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</option>
141
+ </optgroup>
142
+ <optgroup label="Maintenance Policy">
143
+ <option value="maintenance_policy.html">Maintenance Policy</option>
144
+ </optgroup>
145
+ <optgroup label="Release Notes">
146
+ <option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
147
+ <option value="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</option>
148
+ <option value="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</option>
149
+ <option value="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</option>
150
+ <option value="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</option>
151
+ <option value="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</option>
152
+ <option value="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</option>
153
+ <option value="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</option>
154
+ </optgroup>
155
+ </select>
156
+ </li>
157
+ </ul>
158
+ </div>
159
+ </div>
160
+ <hr class="hide" />
161
+
162
+ <div id="feature">
163
+ <div class="wrapper">
164
+ <h2>The Asset Pipeline</h2><p>This guide covers the asset pipeline.</p><p>After reading this guide, you will know:</p>
165
+ <ul>
166
+ <li>What the asset pipeline is and what it does.</li>
167
+ <li>How to properly organize your application assets.</li>
168
+ <li>The benefits of the asset pipeline.</li>
169
+ <li>How to add a pre-processor to the pipeline.</li>
170
+ <li>How to package assets with a gem.</li>
171
+ </ul>
172
+
173
+
174
+ <div id="subCol">
175
+ <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
176
+ <ol class="chapters">
177
+ <li>
178
+ <a href="#what-is-the-asset-pipeline-questionmark">What is the Asset Pipeline?</a>
179
+
180
+ <ul>
181
+ <li><a href="#main-features">Main Features</a></li>
182
+ <li><a href="#what-is-fingerprinting-and-why-should-i-care-questionmark">What is Fingerprinting and Why Should I Care?</a></li>
183
+ </ul>
184
+ </li>
185
+ <li>
186
+ <a href="#how-to-use-the-asset-pipeline">How to Use the Asset Pipeline</a>
187
+
188
+ <ul>
189
+ <li><a href="#controller-specific-assets">Controller Specific Assets</a></li>
190
+ <li><a href="#asset-organization">Asset Organization</a></li>
191
+ <li><a href="#coding-links-to-assets">Coding Links to Assets</a></li>
192
+ <li><a href="#manifest-files-and-directives">Manifest Files and Directives</a></li>
193
+ <li><a href="#preprocessing">Preprocessing</a></li>
194
+ </ul>
195
+ </li>
196
+ <li>
197
+ <a href="#in-development">In Development</a>
198
+
199
+ <ul>
200
+ <li><a href="#runtime-error-checking">Runtime Error Checking</a></li>
201
+ <li><a href="#turning-digests-off">Turning Digests Off</a></li>
202
+ <li><a href="#turning-debugging-off">Turning Debugging Off</a></li>
203
+ </ul>
204
+ </li>
205
+ <li>
206
+ <a href="#in-production">In Production</a>
207
+
208
+ <ul>
209
+ <li><a href="#precompiling-assets">Precompiling Assets</a></li>
210
+ <li><a href="#local-precompilation">Local Precompilation</a></li>
211
+ <li><a href="#live-compilation">Live Compilation</a></li>
212
+ <li><a href="#cdns">CDNs</a></li>
213
+ </ul>
214
+ </li>
215
+ <li>
216
+ <a href="#customizing-the-pipeline">Customizing the Pipeline</a>
217
+
218
+ <ul>
219
+ <li><a href="#css-compression">CSS Compression</a></li>
220
+ <li><a href="#javascript-compression">JavaScript Compression</a></li>
221
+ <li><a href="#using-your-own-compressor">Using Your Own Compressor</a></li>
222
+ <li><a href="#changing-the-assets-path">Changing the <em>assets</em> Path</a></li>
223
+ <li><a href="#x-sendfile-headers">X-Sendfile Headers</a></li>
224
+ </ul>
225
+ </li>
226
+ <li><a href="#assets-cache-store">Assets Cache Store</a></li>
227
+ <li><a href="#adding-assets-to-your-gems">Adding Assets to Your Gems</a></li>
228
+ <li><a href="#making-your-library-or-gem-a-pre-processor">Making Your Library or Gem a Pre-Processor</a></li>
229
+ <li><a href="#upgrading-from-old-versions-of-rails">Upgrading from Old Versions of Rails</a></li>
230
+ </ol>
231
+
232
+ </div>
233
+
234
+ </div>
235
+ </div>
236
+
237
+ <div id="container">
238
+ <div class="wrapper">
239
+ <div id="mainCol">
240
+ <h3 id="what-is-the-asset-pipeline-questionmark">1 What is the Asset Pipeline?</h3><p>The asset pipeline provides a framework to concatenate and minify or compress
241
+ JavaScript and CSS assets. It also adds the ability to write these assets in
242
+ other languages and pre-processors such as CoffeeScript, Sass and ERB.</p><p>The asset pipeline is technically no longer a core feature of Rails 4, it has
243
+ been extracted out of the framework into the
244
+ <a href="https://github.com/rails/sprockets-rails">sprockets-rails</a> gem.</p><p>The asset pipeline is enabled by default.</p><p>You can disable the asset pipeline while creating a new application by
245
+ passing the <code>--skip-sprockets</code> option.</p><div class="code_container">
246
+ <pre class="brush: plain; gutter: false; toolbar: false">
247
+ rails new appname --skip-sprockets
248
+
249
+ </pre>
250
+ </div>
251
+ <p>Rails 4 automatically adds the <code>sass-rails</code>, <code>coffee-rails</code> and <code>uglifier</code>
252
+ gems to your Gemfile, which are used by Sprockets for asset compression:</p><div class="code_container">
253
+ <pre class="brush: ruby; gutter: false; toolbar: false">
254
+ gem 'sass-rails'
255
+ gem 'uglifier'
256
+ gem 'coffee-rails'
257
+
258
+ </pre>
259
+ </div>
260
+ <p>Using the <code>--skip-sprockets</code> option will prevent Rails 4 from adding
261
+ <code>sass-rails</code> and <code>uglifier</code> to Gemfile, so if you later want to enable
262
+ the asset pipeline you will have to add those gems to your Gemfile. Also,
263
+ creating an application with the <code>--skip-sprockets</code> option will generate
264
+ a slightly different <code>config/application.rb</code> file, with a require statement
265
+ for the sprockets railtie that is commented-out. You will have to remove
266
+ the comment operator on that line to later enable the asset pipeline:</p><div class="code_container">
267
+ <pre class="brush: ruby; gutter: false; toolbar: false">
268
+ # require "sprockets/railtie"
269
+
270
+ </pre>
271
+ </div>
272
+ <p>To set asset compression methods, set the appropriate configuration options
273
+ in <code>production.rb</code> - <code>config.assets.css_compressor</code> for your CSS and
274
+ <code>config.assets.js_compressor</code> for your JavaScript:</p><div class="code_container">
275
+ <pre class="brush: ruby; gutter: false; toolbar: false">
276
+ config.assets.css_compressor = :yui
277
+ config.assets.js_compressor = :uglifier
278
+
279
+ </pre>
280
+ </div>
281
+ <div class="note"><p>The <code>sass-rails</code> gem is automatically used for CSS compression if included
282
+ in Gemfile and no <code>config.assets.css_compressor</code> option is set.</p></div><h4 id="main-features">1.1 Main Features</h4><p>The first feature of the pipeline is to concatenate assets, which can reduce the
283
+ number of requests that a browser makes to render a web page. Web browsers are
284
+ limited in the number of requests that they can make in parallel, so fewer
285
+ requests can mean faster loading for your application.</p><p>Sprockets concatenates all JavaScript files into one master <code>.js</code> file and all
286
+ CSS files into one master <code>.css</code> file. As you'll learn later in this guide, you
287
+ can customize this strategy to group files any way you like. In production,
288
+ Rails inserts an MD5 fingerprint into each filename so that the file is cached
289
+ by the web browser. You can invalidate the cache by altering this fingerprint,
290
+ which happens automatically whenever you change the file contents.</p><p>The second feature of the asset pipeline is asset minification or compression.
291
+ For CSS files, this is done by removing whitespace and comments. For JavaScript,
292
+ more complex processes can be applied. You can choose from a set of built in
293
+ options or specify your own.</p><p>The third feature of the asset pipeline is it allows coding assets via a
294
+ higher-level language, with precompilation down to the actual assets. Supported
295
+ languages include Sass for CSS, CoffeeScript for JavaScript, and ERB for both by
296
+ default.</p><h4 id="what-is-fingerprinting-and-why-should-i-care-questionmark">1.2 What is Fingerprinting and Why Should I Care?</h4><p>Fingerprinting is a technique that makes the name of a file dependent on the
297
+ contents of the file. When the file contents change, the filename is also
298
+ changed. For content that is static or infrequently changed, this provides an
299
+ easy way to tell whether two versions of a file are identical, even across
300
+ different servers or deployment dates.</p><p>When a filename is unique and based on its content, HTTP headers can be set to
301
+ encourage caches everywhere (whether at CDNs, at ISPs, in networking equipment,
302
+ or in web browsers) to keep their own copy of the content. When the content is
303
+ updated, the fingerprint will change. This will cause the remote clients to
304
+ request a new copy of the content. This is generally known as <em>cache busting</em>.</p><p>The technique sprockets uses for fingerprinting is to insert a hash of the
305
+ content into the name, usually at the end. For example a CSS file <code>global.css</code></p><div class="code_container">
306
+ <pre class="brush: plain; gutter: false; toolbar: false">
307
+ global-908e25f4bf641868d8683022a5b62f54.css
308
+
309
+ </pre>
310
+ </div>
311
+ <p>This is the strategy adopted by the Rails asset pipeline.</p><p>Rails' old strategy was to append a date-based query string to every asset linked
312
+ with a built-in helper. In the source the generated code looked like this:</p><div class="code_container">
313
+ <pre class="brush: plain; gutter: false; toolbar: false">
314
+ /stylesheets/global.css?1309495796
315
+
316
+ </pre>
317
+ </div>
318
+ <p>The query string strategy has several disadvantages:</p>
319
+ <ol>
320
+ <li>
321
+ <p><strong>Not all caches will reliably cache content where the filename only differs by
322
+ query parameters</strong></p>
323
+ <p><a href="http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/">Steve Souders recommends</a>,
324
+ "...avoiding a querystring for cacheable resources". He found that in this
325
+ case 5-20% of requests will not be cached. Query strings in particular do not
326
+ work at all with some CDNs for cache invalidation.</p>
327
+ </li>
328
+ <li>
329
+ <p><strong>The file name can change between nodes in multi-server environments.</strong></p>
330
+ <p>The default query string in Rails 2.x is based on the modification time of
331
+ the files. When assets are deployed to a cluster, there is no guarantee that the
332
+ timestamps will be the same, resulting in different values being used depending
333
+ on which server handles the request.</p>
334
+ </li>
335
+ <li>
336
+ <p><strong>Too much cache invalidation</strong></p>
337
+ <p>When static assets are deployed with each new release of code, the mtime
338
+ (time of last modification) of <em>all</em> these files changes, forcing all remote
339
+ clients to fetch them again, even when the content of those assets has not changed.</p>
340
+ </li>
341
+ </ol>
342
+ <p>Fingerprinting fixes these problems by avoiding query strings, and by ensuring
343
+ that filenames are consistent based on their content.</p><p>Fingerprinting is enabled by default for production and disabled for all other
344
+ environments. You can enable or disable it in your configuration through the
345
+ <code>config.assets.digest</code> option.</p><p>More reading:</p>
346
+ <ul>
347
+ <li><a href="http://code.google.com/speed/page-speed/docs/caching.html">Optimize caching</a></li>
348
+ <li><a href="http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/">Revving Filenames: don't use querystring</a></li>
349
+ </ul>
350
+ <h3 id="how-to-use-the-asset-pipeline">2 How to Use the Asset Pipeline</h3><p>In previous versions of Rails, all assets were located in subdirectories of
351
+ <code>public</code> such as <code>images</code>, <code>javascripts</code> and <code>stylesheets</code>. With the asset
352
+ pipeline, the preferred location for these assets is now the <code>app/assets</code>
353
+ directory. Files in this directory are served by the Sprockets middleware.</p><p>Assets can still be placed in the <code>public</code> hierarchy. Any assets under <code>public</code>
354
+ will be served as static files by the application or web server. You should use
355
+ <code>app/assets</code> for files that must undergo some pre-processing before they are
356
+ served.</p><p>In production, Rails precompiles these files to <code>public/assets</code> by default. The
357
+ precompiled copies are then served as static assets by the web server. The files
358
+ in <code>app/assets</code> are never served directly in production.</p><h4 id="controller-specific-assets">2.1 Controller Specific Assets</h4><p>When you generate a scaffold or a controller, Rails also generates a JavaScript
359
+ file (or CoffeeScript file if the <code>coffee-rails</code> gem is in the <code>Gemfile</code>) and a
360
+ Cascading Style Sheet file (or SCSS file if <code>sass-rails</code> is in the <code>Gemfile</code>)
361
+ for that controller. Additionally, when generating a scaffold, Rails generates
362
+ the file scaffolds.css (or scaffolds.css.scss if <code>sass-rails</code> is in the
363
+ <code>Gemfile</code>.)</p><p>For example, if you generate a <code>ProjectsController</code>, Rails will also add a new
364
+ file at <code>app/assets/javascripts/projects.js.coffee</code> and another at
365
+ <code>app/assets/stylesheets/projects.css.scss</code>. By default these files will be ready
366
+ to use by your application immediately using the <code>require_tree</code> directive. See
367
+ <a href="#manifest-files-and-directives">Manifest Files and Directives</a> for more details
368
+ on require_tree.</p><p>You can also opt to include controller specific stylesheets and JavaScript files
369
+ only in their respective controllers using the following:</p><p><code>&lt;%= javascript_include_tag params[:controller] %&gt;</code> or <code>&lt;%= stylesheet_link_tag
370
+ params[:controller] %&gt;</code></p><p>When doing this, ensure you are not using the <code>require_tree</code> directive, as that
371
+ will result in your assets being included more than once.</p><div class="warning"><p>When using asset precompilation, you will need to ensure that your
372
+ controller assets will be precompiled when loading them on a per page basis. By
373
+ default .coffee and .scss files will not be precompiled on their own. See
374
+ <a href="#precompiling-assets">Precompiling Assets</a> for more information on how
375
+ precompiling works.</p></div><div class="note"><p>You must have an ExecJS supported runtime in order to use CoffeeScript.
376
+ If you are using Mac OS X or Windows, you have a JavaScript runtime installed in
377
+ your operating system. Check
378
+ <a href="https://github.com/sstephenson/execjs#readme">ExecJS</a> documentation to know all
379
+ supported JavaScript runtimes.</p></div><p>You can also disable generation of controller specific asset files by adding the
380
+ following to your <code>config/application.rb</code> configuration:</p><div class="code_container">
381
+ <pre class="brush: ruby; gutter: false; toolbar: false">
382
+ config.generators do |g|
383
+ g.assets false
384
+ end
385
+
386
+ </pre>
387
+ </div>
388
+ <h4 id="asset-organization">2.2 Asset Organization</h4><p>Pipeline assets can be placed inside an application in one of three locations:
389
+ <code>app/assets</code>, <code>lib/assets</code> or <code>vendor/assets</code>.</p>
390
+ <ul>
391
+ <li><p><code>app/assets</code> is for assets that are owned by the application, such as custom
392
+ images, JavaScript files or stylesheets.</p></li>
393
+ <li><p><code>lib/assets</code> is for your own libraries' code that doesn't really fit into the
394
+ scope of the application or those libraries which are shared across applications.</p></li>
395
+ <li><p><code>vendor/assets</code> is for assets that are owned by outside entities, such as
396
+ code for JavaScript plugins and CSS frameworks.</p></li>
397
+ </ul>
398
+ <div class="warning"><p>If you are upgrading from Rails 3, please take into account that assets
399
+ under <code>lib/assets</code> or <code>vendor/assets</code> are available for inclusion via the
400
+ application manifests but no longer part of the precompile array. See
401
+ <a href="#precompiling-assets">Precompiling Assets</a> for guidance.</p></div><h5 id="search-paths">2.2.1 Search Paths</h5><p>When a file is referenced from a manifest or a helper, Sprockets searches the
402
+ three default asset locations for it.</p><p>The default locations are: the <code>images</code>, <code>javascripts</code> and <code>stylesheets</code>
403
+ directories under the <code>app/assets</code> folder, but these subdirectories
404
+ are not special - any path under <code>assets/*</code> will be searched.</p><p>For example, these files:</p><div class="code_container">
405
+ <pre class="brush: plain; gutter: false; toolbar: false">
406
+ app/assets/javascripts/home.js
407
+ lib/assets/javascripts/moovinator.js
408
+ vendor/assets/javascripts/slider.js
409
+ vendor/assets/somepackage/phonebox.js
410
+
411
+ </pre>
412
+ </div>
413
+ <p>would be referenced in a manifest like this:</p><div class="code_container">
414
+ <pre class="brush: plain; gutter: false; toolbar: false">
415
+ //= require home
416
+ //= require moovinator
417
+ //= require slider
418
+ //= require phonebox
419
+
420
+ </pre>
421
+ </div>
422
+ <p>Assets inside subdirectories can also be accessed.</p><div class="code_container">
423
+ <pre class="brush: plain; gutter: false; toolbar: false">
424
+ app/assets/javascripts/sub/something.js
425
+
426
+ </pre>
427
+ </div>
428
+ <p>is referenced as:</p><div class="code_container">
429
+ <pre class="brush: plain; gutter: false; toolbar: false">
430
+ //= require sub/something
431
+
432
+ </pre>
433
+ </div>
434
+ <p>You can view the search path by inspecting
435
+ <code>Rails.application.config.assets.paths</code> in the Rails console.</p><p>Besides the standard <code>assets/*</code> paths, additional (fully qualified) paths can be
436
+ added to the pipeline in <code>config/application.rb</code>. For example:</p><div class="code_container">
437
+ <pre class="brush: ruby; gutter: false; toolbar: false">
438
+ config.assets.paths &lt;&lt; Rails.root.join("lib", "videoplayer", "flash")
439
+
440
+ </pre>
441
+ </div>
442
+ <p>Paths are traversed in the order they occur in the search path. By default,
443
+ this means the files in <code>app/assets</code> take precedence, and will mask
444
+ corresponding paths in <code>lib</code> and <code>vendor</code>.</p><p>It is important to note that files you want to reference outside a manifest must
445
+ be added to the precompile array or they will not be available in the production
446
+ environment.</p><h5 id="using-index-files">2.2.2 Using Index Files</h5><p>Sprockets uses files named <code>index</code> (with the relevant extensions) for a special
447
+ purpose.</p><p>For example, if you have a jQuery library with many modules, which is stored in
448
+ <code>lib/assets/javascripts/library_name</code>, the file <code>lib/assets/javascripts/library_name/index.js</code> serves as
449
+ the manifest for all files in this library. This file could include a list of
450
+ all the required files in order, or a simple <code>require_tree</code> directive.</p><p>The library as a whole can be accessed in the application manifest like so:</p><div class="code_container">
451
+ <pre class="brush: plain; gutter: false; toolbar: false">
452
+ //= require library_name
453
+
454
+ </pre>
455
+ </div>
456
+ <p>This simplifies maintenance and keeps things clean by allowing related code to
457
+ be grouped before inclusion elsewhere.</p><h4 id="coding-links-to-assets">2.3 Coding Links to Assets</h4><p>Sprockets does not add any new methods to access your assets - you still use the
458
+ familiar <code>javascript_include_tag</code> and <code>stylesheet_link_tag</code>:</p><div class="code_container">
459
+ <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
460
+ &lt;%= stylesheet_link_tag "application", media: "all" %&gt;
461
+ &lt;%= javascript_include_tag "application" %&gt;
462
+
463
+ </pre>
464
+ </div>
465
+ <p>If using the turbolinks gem, which is included by default in Rails 4, then
466
+ include the 'data-turbolinks-track' option which causes turbolinks to check if
467
+ an asset has been updated and if so loads it into the page:</p><div class="code_container">
468
+ <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
469
+ &lt;%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" =&gt; true %&gt;
470
+ &lt;%= javascript_include_tag "application", "data-turbolinks-track" =&gt; true %&gt;
471
+
472
+ </pre>
473
+ </div>
474
+ <p>In regular views you can access images in the <code>public/assets/images</code> directory
475
+ like this:</p><div class="code_container">
476
+ <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
477
+ &lt;%= image_tag "rails.png" %&gt;
478
+
479
+ </pre>
480
+ </div>
481
+ <p>Provided that the pipeline is enabled within your application (and not disabled
482
+ in the current environment context), this file is served by Sprockets. If a file
483
+ exists at <code>public/assets/rails.png</code> it is served by the web server.</p><p>Alternatively, a request for a file with an MD5 hash such as
484
+ <code>public/assets/rails-af27b6a414e6da00003503148be9b409.png</code> is treated the same
485
+ way. How these hashes are generated is covered in the <a href="#in-production">In
486
+ Production</a> section later on in this guide.</p><p>Sprockets will also look through the paths specified in <code>config.assets.paths</code>,
487
+ which includes the standard application paths and any paths added by Rails
488
+ engines.</p><p>Images can also be organized into subdirectories if required, and then can be
489
+ accessed by specifying the directory's name in the tag:</p><div class="code_container">
490
+ <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
491
+ &lt;%= image_tag "icons/rails.png" %&gt;
492
+
493
+ </pre>
494
+ </div>
495
+ <div class="warning"><p>If you're precompiling your assets (see <a href="#in-production">In Production</a>
496
+ below), linking to an asset that does not exist will raise an exception in the
497
+ calling page. This includes linking to a blank string. As such, be careful using
498
+ <code>image_tag</code> and the other helpers with user-supplied data.</p></div><h5 id="css-and-erb">2.3.1 CSS and ERB</h5><p>The asset pipeline automatically evaluates ERB. This means if you add an
499
+ <code>erb</code> extension to a CSS asset (for example, <code>application.css.erb</code>), then
500
+ helpers like <code>asset_path</code> are available in your CSS rules:</p><div class="code_container">
501
+ <pre class="brush: plain; gutter: false; toolbar: false">
502
+ .class { background-image: url(&lt;%= asset_path 'image.png' %&gt;) }
503
+
504
+ </pre>
505
+ </div>
506
+ <p>This writes the path to the particular asset being referenced. In this example,
507
+ it would make sense to have an image in one of the asset load paths, such as
508
+ <code>app/assets/images/image.png</code>, which would be referenced here. If this image is
509
+ already available in <code>public/assets</code> as a fingerprinted file, then that path is
510
+ referenced.</p><p>If you want to use a <a href="http://en.wikipedia.org/wiki/Data_URI_scheme">data URI</a> -
511
+ a method of embedding the image data directly into the CSS file - you can use
512
+ the <code>asset_data_uri</code> helper.</p><div class="code_container">
513
+ <pre class="brush: plain; gutter: false; toolbar: false">
514
+ #logo { background: url(&lt;%= asset_data_uri 'logo.png' %&gt;) }
515
+
516
+ </pre>
517
+ </div>
518
+ <p>This inserts a correctly-formatted data URI into the CSS source.</p><p>Note that the closing tag cannot be of the style <code>-%&gt;</code>.</p><h5 id="css-and-sass">2.3.2 CSS and Sass</h5><p>When using the asset pipeline, paths to assets must be re-written and
519
+ <code>sass-rails</code> provides <code>-url</code> and <code>-path</code> helpers (hyphenated in Sass,
520
+ underscored in Ruby) for the following asset classes: image, font, video, audio,
521
+ JavaScript and stylesheet.</p>
522
+ <ul>
523
+ <li>
524
+ <code>image-url("rails.png")</code> becomes <code>url(/assets/rails.png)</code>
525
+ </li>
526
+ <li>
527
+ <code>image-path("rails.png")</code> becomes <code>"/assets/rails.png"</code>.</li>
528
+ </ul>
529
+ <p>The more generic form can also be used:</p>
530
+ <ul>
531
+ <li>
532
+ <code>asset-url("rails.png")</code> becomes <code>url(/assets/rails.png)</code>
533
+ </li>
534
+ <li>
535
+ <code>asset-path("rails.png")</code> becomes <code>"/assets/rails.png"</code>
536
+ </li>
537
+ </ul>
538
+ <h5 id="javascript/coffeescript-and-erb">2.3.3 JavaScript/CoffeeScript and ERB</h5><p>If you add an <code>erb</code> extension to a JavaScript asset, making it something such as
539
+ <code>application.js.erb</code>, you can then use the <code>asset_path</code> helper in your
540
+ JavaScript code:</p><div class="code_container">
541
+ <pre class="brush: plain; gutter: false; toolbar: false">
542
+ $('#logo').attr({ src: "&lt;%= asset_path('logo.png') %&gt;" });
543
+
544
+ </pre>
545
+ </div>
546
+ <p>This writes the path to the particular asset being referenced.</p><p>Similarly, you can use the <code>asset_path</code> helper in CoffeeScript files with <code>erb</code>
547
+ extension (e.g., <code>application.js.coffee.erb</code>):</p><div class="code_container">
548
+ <pre class="brush: plain; gutter: false; toolbar: false">
549
+ $('#logo').attr src: "&lt;%= asset_path('logo.png') %&gt;"
550
+
551
+ </pre>
552
+ </div>
553
+ <h4 id="manifest-files-and-directives">2.4 Manifest Files and Directives</h4><p>Sprockets uses manifest files to determine which assets to include and serve.
554
+ These manifest files contain <em>directives</em> - instructions that tell Sprockets
555
+ which files to require in order to build a single CSS or JavaScript file. With
556
+ these directives, Sprockets loads the files specified, processes them if
557
+ necessary, concatenates them into one single file and then compresses them (if
558
+ <code>Rails.application.config.assets.compress</code> is true). By serving one file rather
559
+ than many, the load time of pages can be greatly reduced because the browser
560
+ makes fewer requests. Compression also reduces file size, enabling the
561
+ browser to download them faster.</p><p>For example, a new Rails 4 application includes a default
562
+ <code>app/assets/javascripts/application.js</code> file containing the following lines:</p><div class="code_container">
563
+ <pre class="brush: plain; gutter: false; toolbar: false">
564
+ // ...
565
+ //= require jquery
566
+ //= require jquery_ujs
567
+ //= require_tree .
568
+
569
+ </pre>
570
+ </div>
571
+ <p>In JavaScript files, Sprockets directives begin with <code>//=</code>. In the above case,
572
+ the file is using the <code>require</code> and the <code>require_tree</code> directives. The <code>require</code>
573
+ directive is used to tell Sprockets the files you wish to require. Here, you are
574
+ requiring the files <code>jquery.js</code> and <code>jquery_ujs.js</code> that are available somewhere
575
+ in the search path for Sprockets. You need not supply the extensions explicitly.
576
+ Sprockets assumes you are requiring a <code>.js</code> file when done from within a <code>.js</code>
577
+ file.</p><p>The <code>require_tree</code> directive tells Sprockets to recursively include <em>all</em>
578
+ JavaScript files in the specified directory into the output. These paths must be
579
+ specified relative to the manifest file. You can also use the
580
+ <code>require_directory</code> directive which includes all JavaScript files only in the
581
+ directory specified, without recursion.</p><p>Directives are processed top to bottom, but the order in which files are
582
+ included by <code>require_tree</code> is unspecified. You should not rely on any particular
583
+ order among those. If you need to ensure some particular JavaScript ends up
584
+ above some other in the concatenated file, require the prerequisite file first
585
+ in the manifest. Note that the family of <code>require</code> directives prevents files
586
+ from being included twice in the output.</p><p>Rails also creates a default <code>app/assets/stylesheets/application.css</code> file
587
+ which contains these lines:</p><div class="code_container">
588
+ <pre class="brush: plain; gutter: false; toolbar: false">
589
+ /* ...
590
+ *= require_self
591
+ *= require_tree .
592
+ */
593
+
594
+ </pre>
595
+ </div>
596
+ <p>Rails 4 creates both <code>app/assets/javascripts/application.js</code> and
597
+ <code>app/assets/stylesheets/application.css</code> regardless of whether the
598
+ --skip-sprockets option is used when creating a new rails application. This is
599
+ so you can easily add asset pipelining later if you like.</p><p>The directives that work in JavaScript files also work in stylesheets
600
+ (though obviously including stylesheets rather than JavaScript files). The
601
+ <code>require_tree</code> directive in a CSS manifest works the same way as the JavaScript
602
+ one, requiring all stylesheets from the current directory.</p><p>In this example, <code>require_self</code> is used. This puts the CSS contained within the
603
+ file (if any) at the precise location of the <code>require_self</code> call.</p><div class="note"><p>If you want to use multiple Sass files, you should generally use the <a href="http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#import">Sass <code>@import</code> rule</a>
604
+ instead of these Sprockets directives. When using Sprockets directives, Sass files exist within
605
+ their own scope, making variables or mixins only available within the document they were defined in.</p></div><p>You can do file globbing as well using <code>@import "*"</code>, and <code>@import "**/*"</code> to add the whole tree which is equivalent to how <code>require_tree</code> works. Check the <a href="https://github.com/rails/sass-rails#features">sass-rails documentation</a> for more info and important caveats.</p><p>You can have as many manifest files as you need. For example, the <code>admin.css</code>
606
+ and <code>admin.js</code> manifest could contain the JS and CSS files that are used for the
607
+ admin section of an application.</p><p>The same remarks about ordering made above apply. In particular, you can specify
608
+ individual files and they are compiled in the order specified. For example, you
609
+ might concatenate three CSS files together this way:</p><div class="code_container">
610
+ <pre class="brush: plain; gutter: false; toolbar: false">
611
+ /* ...
612
+ *= require reset
613
+ *= require layout
614
+ *= require chrome
615
+ */
616
+
617
+ </pre>
618
+ </div>
619
+ <h4 id="preprocessing">2.5 Preprocessing</h4><p>The file extensions used on an asset determine what preprocessing is applied.
620
+ When a controller or a scaffold is generated with the default Rails gemset, a
621
+ CoffeeScript file and a SCSS file are generated in place of a regular JavaScript
622
+ and CSS file. The example used before was a controller called "projects", which
623
+ generated an <code>app/assets/javascripts/projects.js.coffee</code> and an
624
+ <code>app/assets/stylesheets/projects.css.scss</code> file.</p><p>In development mode, or if the asset pipeline is disabled, when these files are
625
+ requested they are processed by the processors provided by the <code>coffee-script</code>
626
+ and <code>sass</code> gems and then sent back to the browser as JavaScript and CSS
627
+ respectively. When asset pipelining is enabled, these files are preprocessed and
628
+ placed in the <code>public/assets</code> directory for serving by either the Rails app or
629
+ web server.</p><p>Additional layers of preprocessing can be requested by adding other extensions,
630
+ where each extension is processed in a right-to-left manner. These should be
631
+ used in the order the processing should be applied. For example, a stylesheet
632
+ called <code>app/assets/stylesheets/projects.css.scss.erb</code> is first processed as ERB,
633
+ then SCSS, and finally served as CSS. The same applies to a JavaScript file -
634
+ <code>app/assets/javascripts/projects.js.coffee.erb</code> is processed as ERB, then
635
+ CoffeeScript, and served as JavaScript.</p><p>Keep in mind the order of these preprocessors is important. For example, if
636
+ you called your JavaScript file <code>app/assets/javascripts/projects.js.erb.coffee</code>
637
+ then it would be processed with the CoffeeScript interpreter first, which
638
+ wouldn't understand ERB and therefore you would run into problems.</p><h3 id="in-development">3 In Development</h3><p>In development mode, assets are served as separate files in the order they are
639
+ specified in the manifest file.</p><p>This manifest <code>app/assets/javascripts/application.js</code>:</p><div class="code_container">
640
+ <pre class="brush: plain; gutter: false; toolbar: false">
641
+ //= require core
642
+ //= require projects
643
+ //= require tickets
644
+
645
+ </pre>
646
+ </div>
647
+ <p>would generate this HTML:</p><div class="code_container">
648
+ <pre class="brush: xml; gutter: false; toolbar: false">
649
+ &lt;script src="/assets/core.js?body=1"&gt;&lt;/script&gt;
650
+ &lt;script src="/assets/projects.js?body=1"&gt;&lt;/script&gt;
651
+ &lt;script src="/assets/tickets.js?body=1"&gt;&lt;/script&gt;
652
+
653
+ </pre>
654
+ </div>
655
+ <p>The <code>body</code> param is required by Sprockets.</p><h4 id="runtime-error-checking">3.1 Runtime Error Checking</h4><p>By default the asset pipeline will check for potential errors in development mode during
656
+ runtime. To disable this behavior you can set:</p><div class="code_container">
657
+ <pre class="brush: ruby; gutter: false; toolbar: false">
658
+ config.assets.raise_runtime_errors = false
659
+
660
+ </pre>
661
+ </div>
662
+ <p>When this option is true, the asset pipeline will check if all the assets loaded
663
+ in your application are included in the <code>config.assets.precompile</code> list.
664
+ If <code>config.assets.digest</code> is also true, the asset pipeline will require that
665
+ all requests for assets include digests.</p><h4 id="turning-digests-off">3.2 Turning Digests Off</h4><p>You can turn off digests by updating <code>config/environments/development.rb</code> to
666
+ include:</p><div class="code_container">
667
+ <pre class="brush: ruby; gutter: false; toolbar: false">
668
+ config.assets.digest = false
669
+
670
+ </pre>
671
+ </div>
672
+ <p>When this option is true, digests will be generated for asset URLs.</p><h4 id="turning-debugging-off">3.3 Turning Debugging Off</h4><p>You can turn off debug mode by updating <code>config/environments/development.rb</code> to
673
+ include:</p><div class="code_container">
674
+ <pre class="brush: ruby; gutter: false; toolbar: false">
675
+ config.assets.debug = false
676
+
677
+ </pre>
678
+ </div>
679
+ <p>When debug mode is off, Sprockets concatenates and runs the necessary
680
+ preprocessors on all files. With debug mode turned off the manifest above would
681
+ generate instead:</p><div class="code_container">
682
+ <pre class="brush: xml; gutter: false; toolbar: false">
683
+ &lt;script src="/assets/application.js"&gt;&lt;/script&gt;
684
+
685
+ </pre>
686
+ </div>
687
+ <p>Assets are compiled and cached on the first request after the server is started.
688
+ Sprockets sets a <code>must-revalidate</code> Cache-Control HTTP header to reduce request
689
+ overhead on subsequent requests - on these the browser gets a 304 (Not Modified)
690
+ response.</p><p>If any of the files in the manifest have changed between requests, the server
691
+ responds with a new compiled file.</p><p>Debug mode can also be enabled in Rails helper methods:</p><div class="code_container">
692
+ <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
693
+ &lt;%= stylesheet_link_tag "application", debug: true %&gt;
694
+ &lt;%= javascript_include_tag "application", debug: true %&gt;
695
+
696
+ </pre>
697
+ </div>
698
+ <p>The <code>:debug</code> option is redundant if debug mode is already on.</p><p>You can also enable compression in development mode as a sanity check, and
699
+ disable it on-demand as required for debugging.</p><h3 id="in-production">4 In Production</h3><p>In the production environment Sprockets uses the fingerprinting scheme outlined
700
+ above. By default Rails assumes assets have been precompiled and will be
701
+ served as static assets by your web server.</p><p>During the precompilation phase an MD5 is generated from the contents of the
702
+ compiled files, and inserted into the filenames as they are written to disc.
703
+ These fingerprinted names are used by the Rails helpers in place of the manifest
704
+ name.</p><p>For example this:</p><div class="code_container">
705
+ <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
706
+ &lt;%= javascript_include_tag "application" %&gt;
707
+ &lt;%= stylesheet_link_tag "application" %&gt;
708
+
709
+ </pre>
710
+ </div>
711
+ <p>generates something like this:</p><div class="code_container">
712
+ <pre class="brush: xml; gutter: false; toolbar: false">
713
+ &lt;script src="/assets/application-908e25f4bf641868d8683022a5b62f54.js"&gt;&lt;/script&gt;
714
+ &lt;link href="/assets/application-4dd5b109ee3439da54f5bdfd78a80473.css" media="screen"
715
+ rel="stylesheet" /&gt;
716
+
717
+ </pre>
718
+ </div>
719
+ <p>Note: with the Asset Pipeline the :cache and :concat options aren't used
720
+ anymore, delete these options from the <code>javascript_include_tag</code> and
721
+ <code>stylesheet_link_tag</code>.</p><p>The fingerprinting behavior is controlled by the <code>config.assets.digest</code>
722
+ initialization option (which defaults to <code>true</code> for production and <code>false</code> for
723
+ everything else).</p><div class="note"><p>Under normal circumstances the default <code>config.assets.digest</code> option
724
+ should not be changed. If there are no digests in the filenames, and far-future
725
+ headers are set, remote clients will never know to refetch the files when their
726
+ content changes.</p></div><h4 id="precompiling-assets">4.1 Precompiling Assets</h4><p>Rails comes bundled with a rake task to compile the asset manifests and other
727
+ files in the pipeline.</p><p>Compiled assets are written to the location specified in <code>config.assets.prefix</code>.
728
+ By default, this is the <code>/assets</code> directory.</p><p>You can call this task on the server during deployment to create compiled
729
+ versions of your assets directly on the server. See the next section for
730
+ information on compiling locally.</p><p>The rake task is:</p><div class="code_container">
731
+ <pre class="brush: plain; gutter: false; toolbar: false">
732
+ $ RAILS_ENV=production bin/rake assets:precompile
733
+
734
+ </pre>
735
+ </div>
736
+ <p>Capistrano (v2.15.1 and above) includes a recipe to handle this in deployment.
737
+ Add the following line to <code>Capfile</code>:</p><div class="code_container">
738
+ <pre class="brush: ruby; gutter: false; toolbar: false">
739
+ load 'deploy/assets'
740
+
741
+ </pre>
742
+ </div>
743
+ <p>This links the folder specified in <code>config.assets.prefix</code> to <code>shared/assets</code>.
744
+ If you already use this shared folder you'll need to write your own deployment
745
+ task.</p><p>It is important that this folder is shared between deployments so that remotely
746
+ cached pages referencing the old compiled assets still work for the life of
747
+ the cached page.</p><p>The default matcher for compiling files includes <code>application.js</code>,
748
+ <code>application.css</code> and all non-JS/CSS files (this will include all image assets
749
+ automatically) from <code>app/assets</code> folders including your gems:</p><div class="code_container">
750
+ <pre class="brush: ruby; gutter: false; toolbar: false">
751
+ [ Proc.new { |filename, path| path =~ /app\/assets/ &amp;&amp; !%w(.js .css).include?(File.extname(filename)) },
752
+ /application.(css|js)$/ ]
753
+
754
+ </pre>
755
+ </div>
756
+ <div class="note"><p>The matcher (and other members of the precompile array; see below) is
757
+ applied to final compiled file names. This means anything that compiles to
758
+ JS/CSS is excluded, as well as raw JS/CSS files; for example, <code>.coffee</code> and
759
+ <code>.scss</code> files are <strong>not</strong> automatically included as they compile to JS/CSS.</p></div><p>If you have other manifests or individual stylesheets and JavaScript files to
760
+ include, you can add them to the <code>precompile</code> array in <code>config/initializers/assets.rb</code>:</p><div class="code_container">
761
+ <pre class="brush: ruby; gutter: false; toolbar: false">
762
+ Rails.application.config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
763
+
764
+ </pre>
765
+ </div>
766
+ <p>Or, you can opt to precompile all assets with something like this:</p><div class="code_container">
767
+ <pre class="brush: ruby; gutter: false; toolbar: false">
768
+ # config/initializers/assets.rb
769
+ Rails.application.config.assets.precompile &lt;&lt; Proc.new do |path|
770
+ if path =~ /\.(css|js)\z/
771
+ full_path = Rails.application.assets.resolve(path).to_path
772
+ app_assets_path = Rails.root.join('app', 'assets').to_path
773
+ if full_path.starts_with? app_assets_path
774
+ puts "including asset: " + full_path
775
+ true
776
+ else
777
+ puts "excluding asset: " + full_path
778
+ false
779
+ end
780
+ else
781
+ false
782
+ end
783
+ end
784
+
785
+ </pre>
786
+ </div>
787
+ <div class="note"><p>Always specify an expected compiled filename that ends with .js or .css,
788
+ even if you want to add Sass or CoffeeScript files to the precompile array.</p></div><p>The rake task also generates a <code>manifest-md5hash.json</code> that contains a list with
789
+ all your assets and their respective fingerprints. This is used by the Rails
790
+ helper methods to avoid handing the mapping requests back to Sprockets. A
791
+ typical manifest file looks like:</p><div class="code_container">
792
+ <pre class="brush: ruby; gutter: false; toolbar: false">
793
+ {"files":{"application-723d1be6cc741a3aabb1cec24276d681.js":{"logical_path":"application.js","mtime":"2013-07-26T22:55:03-07:00","size":302506,
794
+ "digest":"723d1be6cc741a3aabb1cec24276d681"},"application-12b3c7dd74d2e9df37e7cbb1efa76a6d.css":{"logical_path":"application.css","mtime":"2013-07-26T22:54:54-07:00","size":1560,
795
+ "digest":"12b3c7dd74d2e9df37e7cbb1efa76a6d"},"application-1c5752789588ac18d7e1a50b1f0fd4c2.css":{"logical_path":"application.css","mtime":"2013-07-26T22:56:17-07:00","size":1591,
796
+ "digest":"1c5752789588ac18d7e1a50b1f0fd4c2"},"favicon-a9c641bf2b81f0476e876f7c5e375969.ico":{"logical_path":"favicon.ico","mtime":"2013-07-26T23:00:10-07:00","size":1406,
797
+ "digest":"a9c641bf2b81f0476e876f7c5e375969"},"my_image-231a680f23887d9dd70710ea5efd3c62.png":{"logical_path":"my_image.png","mtime":"2013-07-26T23:00:27-07:00","size":6646,
798
+ "digest":"231a680f23887d9dd70710ea5efd3c62"}},"assets":{"application.js":
799
+ "application-723d1be6cc741a3aabb1cec24276d681.js","application.css":
800
+ "application-1c5752789588ac18d7e1a50b1f0fd4c2.css",
801
+ "favicon.ico":"favicona9c641bf2b81f0476e876f7c5e375969.ico","my_image.png":
802
+ "my_image-231a680f23887d9dd70710ea5efd3c62.png"}}
803
+
804
+ </pre>
805
+ </div>
806
+ <p>The default location for the manifest is the root of the location specified in
807
+ <code>config.assets.prefix</code> ('/assets' by default).</p><div class="note"><p>If there are missing precompiled files in production you will get an
808
+ <code>Sprockets::Helpers::RailsHelper::AssetPaths::AssetNotPrecompiledError</code>
809
+ exception indicating the name of the missing file(s).</p></div><h5 id="far-future-expires-header">4.1.1 Far-future Expires Header</h5><p>Precompiled assets exist on the file system and are served directly by your web
810
+ server. They do not have far-future headers by default, so to get the benefit of
811
+ fingerprinting you'll have to update your server configuration to add those
812
+ headers.</p><p>For Apache:</p><div class="code_container">
813
+ <pre class="brush: plain; gutter: false; toolbar: false">
814
+ # The Expires* directives requires the Apache module
815
+ # `mod_expires` to be enabled.
816
+ &lt;Location /assets/&gt;
817
+ # Use of ETag is discouraged when Last-Modified is present
818
+ Header unset ETag
819
+ FileETag None
820
+ # RFC says only cache for 1 year
821
+ ExpiresActive On
822
+ ExpiresDefault "access plus 1 year"
823
+ &lt;/Location&gt;
824
+
825
+ </pre>
826
+ </div>
827
+ <p>For NGINX:</p><div class="code_container">
828
+ <pre class="brush: plain; gutter: false; toolbar: false">
829
+ location ~ ^/assets/ {
830
+ expires 1y;
831
+ add_header Cache-Control public;
832
+
833
+ add_header ETag "";
834
+ break;
835
+ }
836
+
837
+ </pre>
838
+ </div>
839
+ <h5 id="gzip-compression">4.1.2 GZip Compression</h5><p>When files are precompiled, Sprockets also creates a
840
+ <a href="http://en.wikipedia.org/wiki/Gzip">gzipped</a> (.gz) version of your assets. Web
841
+ servers are typically configured to use a moderate compression ratio as a
842
+ compromise, but since precompilation happens once, Sprockets uses the maximum
843
+ compression ratio, thus reducing the size of the data transfer to the minimum.
844
+ On the other hand, web servers can be configured to serve compressed content
845
+ directly from disk, rather than deflating non-compressed files themselves.</p><p>NGINX is able to do this automatically enabling <code>gzip_static</code>:</p><div class="code_container">
846
+ <pre class="brush: plain; gutter: false; toolbar: false">
847
+ location ~ ^/(assets)/ {
848
+ root /path/to/public;
849
+ gzip_static on; # to serve pre-gzipped version
850
+ expires max;
851
+ add_header Cache-Control public;
852
+ }
853
+
854
+ </pre>
855
+ </div>
856
+ <p>This directive is available if the core module that provides this feature was
857
+ compiled with the web server. Ubuntu/Debian packages, even <code>nginx-light</code>, have
858
+ the module compiled. Otherwise, you may need to perform a manual compilation:</p><div class="code_container">
859
+ <pre class="brush: plain; gutter: false; toolbar: false">
860
+ ./configure --with-http_gzip_static_module
861
+
862
+ </pre>
863
+ </div>
864
+ <p>If you're compiling NGINX with Phusion Passenger you'll need to pass that option
865
+ when prompted.</p><p>A robust configuration for Apache is possible but tricky; please Google around.
866
+ (Or help update this Guide if you have a good configuration example for Apache.)</p><h4 id="local-precompilation">4.2 Local Precompilation</h4><p>There are several reasons why you might want to precompile your assets locally.
867
+ Among them are:</p>
868
+ <ul>
869
+ <li>You may not have write access to your production file system.</li>
870
+ <li>You may be deploying to more than one server, and want to avoid
871
+ duplication of work.</li>
872
+ <li>You may be doing frequent deploys that do not include asset changes.</li>
873
+ </ul>
874
+ <p>Local compilation allows you to commit the compiled files into source control,
875
+ and deploy as normal.</p><p>There are three caveats:</p>
876
+ <ul>
877
+ <li>You must not run the Capistrano deployment task that precompiles assets.</li>
878
+ <li>You must ensure any necessary compressors or minifiers are
879
+ available on your development system.</li>
880
+ <li>You must change the following application configuration setting:</li>
881
+ </ul>
882
+ <p>In <code>config/environments/development.rb</code>, place the following line:</p><div class="code_container">
883
+ <pre class="brush: ruby; gutter: false; toolbar: false">
884
+ config.assets.prefix = "/dev-assets"
885
+
886
+ </pre>
887
+ </div>
888
+ <p>The <code>prefix</code> change makes Sprockets use a different URL for serving assets in
889
+ development mode, and pass all requests to Sprockets. The prefix is still set to
890
+ <code>/assets</code> in the production environment. Without this change, the application
891
+ would serve the precompiled assets from <code>/assets</code> in development, and you would
892
+ not see any local changes until you compile assets again.</p><p>In practice, this will allow you to precompile locally, have those files in your
893
+ working tree, and commit those files to source control when needed. Development
894
+ mode will work as expected.</p><h4 id="live-compilation">4.3 Live Compilation</h4><p>In some circumstances you may wish to use live compilation. In this mode all
895
+ requests for assets in the pipeline are handled by Sprockets directly.</p><p>To enable this option set:</p><div class="code_container">
896
+ <pre class="brush: ruby; gutter: false; toolbar: false">
897
+ config.assets.compile = true
898
+
899
+ </pre>
900
+ </div>
901
+ <p>On the first request the assets are compiled and cached as outlined in
902
+ development above, and the manifest names used in the helpers are altered to
903
+ include the MD5 hash.</p><p>Sprockets also sets the <code>Cache-Control</code> HTTP header to <code>max-age=31536000</code>. This
904
+ signals all caches between your server and the client browser that this content
905
+ (the file served) can be cached for 1 year. The effect of this is to reduce the
906
+ number of requests for this asset from your server; the asset has a good chance
907
+ of being in the local browser cache or some intermediate cache.</p><p>This mode uses more memory, performs more poorly than the default and is not
908
+ recommended.</p><p>If you are deploying a production application to a system without any
909
+ pre-existing JavaScript runtimes, you may want to add one to your Gemfile:</p><div class="code_container">
910
+ <pre class="brush: ruby; gutter: false; toolbar: false">
911
+ group :production do
912
+ gem 'therubyracer'
913
+ end
914
+
915
+ </pre>
916
+ </div>
917
+ <h4 id="cdns">4.4 CDNs</h4><p>If your assets are being served by a CDN, ensure they don't stick around in your
918
+ cache forever. This can cause problems. If you use
919
+ <code>config.action_controller.perform_caching = true</code>, Rack::Cache will use
920
+ <code>Rails.cache</code> to store assets. This can cause your cache to fill up quickly.</p><p>Every cache is different, so evaluate how your CDN handles caching and make sure
921
+ that it plays nicely with the pipeline. You may find quirks related to your
922
+ specific set up, you may not. The defaults NGINX uses, for example, should give
923
+ you no problems when used as an HTTP cache.</p><p>If you want to serve only some assets from your CDN, you can use custom
924
+ <code>:host</code> option of <code>asset_url</code> helper, which overwrites value set in
925
+ <code>config.action_controller.asset_host</code>.</p><div class="code_container">
926
+ <pre class="brush: ruby; gutter: false; toolbar: false">
927
+ asset_url 'image.png', :host =&gt; 'http://cdn.example.com'
928
+
929
+ </pre>
930
+ </div>
931
+ <h3 id="customizing-the-pipeline">5 Customizing the Pipeline</h3><h4 id="css-compression">5.1 CSS Compression</h4><p>One of the options for compressing CSS is YUI. The <a href="http://yui.github.io/yuicompressor/css.html">YUI CSS
932
+ compressor</a> provides
933
+ minification.</p><p>The following line enables YUI compression, and requires the <code>yui-compressor</code>
934
+ gem.</p><div class="code_container">
935
+ <pre class="brush: ruby; gutter: false; toolbar: false">
936
+ config.assets.css_compressor = :yui
937
+
938
+ </pre>
939
+ </div>
940
+ <p>The other option for compressing CSS if you have the sass-rails gem installed is</p><div class="code_container">
941
+ <pre class="brush: ruby; gutter: false; toolbar: false">
942
+ config.assets.css_compressor = :sass
943
+
944
+ </pre>
945
+ </div>
946
+ <h4 id="javascript-compression">5.2 JavaScript Compression</h4><p>Possible options for JavaScript compression are <code>:closure</code>, <code>:uglifier</code> and
947
+ <code>:yui</code>. These require the use of the <code>closure-compiler</code>, <code>uglifier</code> or
948
+ <code>yui-compressor</code> gems, respectively.</p><p>The default Gemfile includes <a href="https://github.com/lautis/uglifier">uglifier</a>.
949
+ This gem wraps <a href="https://github.com/mishoo/UglifyJS">UglifyJS</a> (written for
950
+ NodeJS) in Ruby. It compresses your code by removing white space and comments,
951
+ shortening local variable names, and performing other micro-optimizations such
952
+ as changing <code>if</code> and <code>else</code> statements to ternary operators where possible.</p><p>The following line invokes <code>uglifier</code> for JavaScript compression.</p><div class="code_container">
953
+ <pre class="brush: ruby; gutter: false; toolbar: false">
954
+ config.assets.js_compressor = :uglifier
955
+
956
+ </pre>
957
+ </div>
958
+ <div class="note"><p>You will need an <a href="https://github.com/sstephenson/execjs#readme">ExecJS</a>
959
+ supported runtime in order to use <code>uglifier</code>. If you are using Mac OS X or
960
+ Windows you have a JavaScript runtime installed in your operating system.</p></div><div class="note"><p>The <code>config.assets.compress</code> initialization option is no longer used in
961
+ Rails 4 to enable either CSS or JavaScript compression. Setting it will have no
962
+ effect on the application. Instead, setting <code>config.assets.css_compressor</code> and
963
+ <code>config.assets.js_compressor</code> will control compression of CSS and JavaScript
964
+ assets.</p></div><h4 id="using-your-own-compressor">5.3 Using Your Own Compressor</h4><p>The compressor config settings for CSS and JavaScript also take any object.
965
+ This object must have a <code>compress</code> method that takes a string as the sole
966
+ argument and it must return a string.</p><div class="code_container">
967
+ <pre class="brush: ruby; gutter: false; toolbar: false">
968
+ class Transformer
969
+ def compress(string)
970
+ do_something_returning_a_string(string)
971
+ end
972
+ end
973
+
974
+ </pre>
975
+ </div>
976
+ <p>To enable this, pass a new object to the config option in <code>application.rb</code>:</p><div class="code_container">
977
+ <pre class="brush: ruby; gutter: false; toolbar: false">
978
+ config.assets.css_compressor = Transformer.new
979
+
980
+ </pre>
981
+ </div>
982
+ <h4 id="changing-the-assets-path">5.4 Changing the <em>assets</em> Path</h4><p>The public path that Sprockets uses by default is <code>/assets</code>.</p><p>This can be changed to something else:</p><div class="code_container">
983
+ <pre class="brush: ruby; gutter: false; toolbar: false">
984
+ config.assets.prefix = "/some_other_path"
985
+
986
+ </pre>
987
+ </div>
988
+ <p>This is a handy option if you are updating an older project that didn't use the
989
+ asset pipeline and already uses this path or you wish to use this path for
990
+ a new resource.</p><h4 id="x-sendfile-headers">5.5 X-Sendfile Headers</h4><p>The X-Sendfile header is a directive to the web server to ignore the response
991
+ from the application, and instead serve a specified file from disk. This option
992
+ is off by default, but can be enabled if your server supports it. When enabled,
993
+ this passes responsibility for serving the file to the web server, which is
994
+ faster. Have a look at <a href="http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_file">send_file</a>
995
+ on how to use this feature.</p><p>Apache and NGINX support this option, which can be enabled in
996
+ <code>config/environments/production.rb</code>:</p><div class="code_container">
997
+ <pre class="brush: ruby; gutter: false; toolbar: false">
998
+ # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache
999
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX
1000
+
1001
+ </pre>
1002
+ </div>
1003
+ <div class="warning"><p>If you are upgrading an existing application and intend to use this
1004
+ option, take care to paste this configuration option only into <code>production.rb</code>
1005
+ and any other environments you define with production behavior (not
1006
+ <code>application.rb</code>).</p></div><div class="info"><p>For further details have a look at the docs of your production web server:
1007
+ - <a href="https://tn123.org/mod_xsendfile/">Apache</a>
1008
+ - <a href="http://wiki.nginx.org/XSendfile">NGINX</a></p></div><h3 id="assets-cache-store">6 Assets Cache Store</h3><p>The default Rails cache store will be used by Sprockets to cache assets in
1009
+ development and production. This can be changed by setting
1010
+ <code>config.assets.cache_store</code>:</p><div class="code_container">
1011
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1012
+ config.assets.cache_store = :memory_store
1013
+
1014
+ </pre>
1015
+ </div>
1016
+ <p>The options accepted by the assets cache store are the same as the application's
1017
+ cache store.</p><div class="code_container">
1018
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1019
+ config.assets.cache_store = :memory_store, { size: 32.megabytes }
1020
+
1021
+ </pre>
1022
+ </div>
1023
+ <p>To disable the assets cache store:</p><div class="code_container">
1024
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1025
+ config.assets.configure do |env|
1026
+ env.cache = ActiveSupport::Cache.lookup_store(:null_store)
1027
+ end
1028
+
1029
+ </pre>
1030
+ </div>
1031
+ <h3 id="adding-assets-to-your-gems">7 Adding Assets to Your Gems</h3><p>Assets can also come from external sources in the form of gems.</p><p>A good example of this is the <code>jquery-rails</code> gem which comes with Rails as the
1032
+ standard JavaScript library gem. This gem contains an engine class which
1033
+ inherits from <code>Rails::Engine</code>. By doing this, Rails is informed that the
1034
+ directory for this gem may contain assets and the <code>app/assets</code>, <code>lib/assets</code> and
1035
+ <code>vendor/assets</code> directories of this engine are added to the search path of
1036
+ Sprockets.</p><h3 id="making-your-library-or-gem-a-pre-processor">8 Making Your Library or Gem a Pre-Processor</h3><p>As Sprockets uses <a href="https://github.com/rtomayko/tilt">Tilt</a> as a generic
1037
+ interface to different templating engines, your gem should just implement the
1038
+ Tilt template protocol. Normally, you would subclass <code>Tilt::Template</code> and
1039
+ reimplement the <code>prepare</code> method, which initializes your template, and the
1040
+ <code>evaluate</code> method, which returns the processed source. The original source is
1041
+ stored in <code>data</code>. Have a look at
1042
+ <a href="https://github.com/rtomayko/tilt/blob/master/lib/tilt/template.rb"><code>Tilt::Template</code></a>
1043
+ sources to learn more.</p><div class="code_container">
1044
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1045
+ module BangBang
1046
+ class Template &lt; ::Tilt::Template
1047
+ def prepare
1048
+ # Do any initialization here
1049
+ end
1050
+
1051
+ # Adds a "!" to original template.
1052
+ def evaluate(scope, locals, &amp;block)
1053
+ "#{data}!"
1054
+ end
1055
+ end
1056
+ end
1057
+
1058
+ </pre>
1059
+ </div>
1060
+ <p>Now that you have a <code>Template</code> class, it's time to associate it with an
1061
+ extension for template files:</p><div class="code_container">
1062
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1063
+ Sprockets.register_engine '.bang', BangBang::Template
1064
+
1065
+ </pre>
1066
+ </div>
1067
+ <h3 id="upgrading-from-old-versions-of-rails">9 Upgrading from Old Versions of Rails</h3><p>There are a few issues when upgrading from Rails 3.0 or Rails 2.x. The first is
1068
+ moving the files from <code>public/</code> to the new locations. See <a href="#asset-organization">Asset
1069
+ Organization</a> above for guidance on the correct locations
1070
+ for different file types.</p><p>Next will be avoiding duplicate JavaScript files. Since jQuery is the default
1071
+ JavaScript library from Rails 3.1 onwards, you don't need to copy <code>jquery.js</code>
1072
+ into <code>app/assets</code> and it will be included automatically.</p><p>The third is updating the various environment files with the correct default
1073
+ options.</p><p>In <code>application.rb</code>:</p><div class="code_container">
1074
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1075
+ # Version of your assets, change this if you want to expire all your assets
1076
+ config.assets.version = '1.0'
1077
+
1078
+ # Change the path that assets are served from config.assets.prefix = "/assets"
1079
+
1080
+ </pre>
1081
+ </div>
1082
+ <p>In <code>development.rb</code>:</p><div class="code_container">
1083
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1084
+ # Expands the lines which load the assets
1085
+ config.assets.debug = true
1086
+
1087
+ </pre>
1088
+ </div>
1089
+ <p>And in <code>production.rb</code>:</p><div class="code_container">
1090
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1091
+ # Choose the compressors to use (if any) config.assets.js_compressor =
1092
+ # :uglifier config.assets.css_compressor = :yui
1093
+
1094
+ # Don't fallback to assets pipeline if a precompiled asset is missed
1095
+ config.assets.compile = false
1096
+
1097
+ # Generate digests for assets URLs. This is planned for deprecation.
1098
+ config.assets.digest = true
1099
+
1100
+ # Precompile additional assets (application.js, application.css, and all
1101
+ # non-JS/CSS are already added) config.assets.precompile += %w( search.js )
1102
+
1103
+ </pre>
1104
+ </div>
1105
+ <p>Rails 4 no longer sets default config values for Sprockets in <code>test.rb</code>, so
1106
+ <code>test.rb</code> now requires Sprockets configuration. The old defaults in the test
1107
+ environment are: <code>config.assets.compile = true</code>, <code>config.assets.compress =
1108
+ false</code>, <code>config.assets.debug = false</code> and <code>config.assets.digest = false</code>.</p><p>The following should also be added to <code>Gemfile</code>:</p><div class="code_container">
1109
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1110
+ gem 'sass-rails', "~&gt; 3.2.3"
1111
+ gem 'coffee-rails', "~&gt; 3.2.1"
1112
+ gem 'uglifier'
1113
+
1114
+ </pre>
1115
+ </div>
1116
+
1117
+
1118
+ <h3>Feedback</h3>
1119
+ <p>
1120
+ You're encouraged to help improve the quality of this guide.
1121
+ </p>
1122
+ <p>
1123
+ Please contribute if you see any typos or factual errors.
1124
+ To get started, you can read our <a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section.
1125
+ </p>
1126
+ <p>
1127
+ You may also find incomplete content, or stuff that is not up to date.
1128
+ Please do add any missing documentation for master. Make sure to check
1129
+ <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
1130
+ if the issues are already fixed or not on the master branch.
1131
+ Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
1132
+ for style and conventions.
1133
+ </p>
1134
+ <p>
1135
+ If for whatever reason you spot something to fix but cannot patch it yourself, please
1136
+ <a href="https://github.com/rails/rails/issues">open an issue</a>.
1137
+ </p>
1138
+ <p>And last but not least, any kind of discussion regarding Ruby on Rails
1139
+ documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
1140
+ </p>
1141
+ </div>
1142
+ </div>
1143
+ </div>
1144
+
1145
+ <hr class="hide" />
1146
+ <div id="footer">
1147
+ <div class="wrapper">
1148
+ <p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p>
1149
+ <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
1150
+
1151
+ </div>
1152
+ </div>
1153
+
1154
+ <script type="text/javascript" src="javascripts/jquery.min.js"></script>
1155
+ <script type="text/javascript" src="javascripts/responsive-tables.js"></script>
1156
+ <script type="text/javascript" src="javascripts/guides.js"></script>
1157
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
1158
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
1159
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
1160
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
1161
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
1162
+ <script type="text/javascript">
1163
+ SyntaxHighlighter.all();
1164
+ $(guidesIndex.bind);
1165
+ </script>
1166
+ </body>
1167
+ </html>