rails 4.1.4 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rails might be problematic. Click here for more details.

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