rails 4.2.0.beta4 → 4.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. checksums.yaml +4 -4
  2. data/guides/Rakefile +3 -1
  3. data/guides/bug_report_templates/action_controller_gem.rb +2 -2
  4. data/guides/bug_report_templates/action_controller_master.rb +2 -2
  5. data/guides/source/3_0_release_notes.md +2 -2
  6. data/guides/source/4_1_release_notes.md +1 -1
  7. data/guides/source/4_2_release_notes.md +250 -225
  8. data/guides/source/_welcome.html.erb +1 -1
  9. data/guides/source/action_controller_overview.md +6 -6
  10. data/guides/source/action_mailer_basics.md +26 -11
  11. data/guides/source/action_view_overview.md +0 -1
  12. data/guides/source/active_job_basics.md +29 -23
  13. data/guides/source/active_record_postgresql.md +2 -7
  14. data/guides/source/active_record_validations.md +1 -0
  15. data/guides/source/active_support_core_extensions.md +1 -1
  16. data/guides/source/active_support_instrumentation.md +5 -5
  17. data/guides/source/asset_pipeline.md +2 -2
  18. data/guides/source/association_basics.md +0 -2
  19. data/guides/source/command_line.md +6 -15
  20. data/guides/source/configuring.md +4 -4
  21. data/guides/source/documents.yaml +0 -1
  22. data/guides/source/engines.md +26 -15
  23. data/guides/source/form_helpers.md +10 -2
  24. data/guides/source/getting_started.md +9 -9
  25. data/guides/source/rails_on_rack.md +2 -2
  26. data/guides/source/ruby_on_rails_guides_guidelines.md +1 -0
  27. data/guides/source/security.md +1 -1
  28. data/guides/source/testing.md +80 -13
  29. data/guides/source/upgrading_ruby_on_rails.md +23 -20
  30. metadata +29 -229
  31. data/guides/output/2_2_release_notes.html +0 -724
  32. data/guides/output/2_3_release_notes.html +0 -870
  33. data/guides/output/3_0_release_notes.html +0 -773
  34. data/guides/output/3_1_release_notes.html +0 -740
  35. data/guides/output/3_2_release_notes.html +0 -797
  36. data/guides/output/4_0_release_notes.html +0 -523
  37. data/guides/output/4_1_release_notes.html +0 -806
  38. data/guides/output/4_2_release_notes.html +0 -728
  39. data/guides/output/Gemfile +0 -6
  40. data/guides/output/_license.html +0 -226
  41. data/guides/output/_welcome.html +0 -233
  42. data/guides/output/action_controller_overview.html +0 -1335
  43. data/guides/output/action_mailer_basics.html +0 -928
  44. data/guides/output/action_view_overview.html +0 -1509
  45. data/guides/output/active_job_basics.html +0 -546
  46. data/guides/output/active_model_basics.html +0 -438
  47. data/guides/output/active_record_basics.html +0 -594
  48. data/guides/output/active_record_callbacks.html +0 -592
  49. data/guides/output/active_record_migrations.html +0 -1123
  50. data/guides/output/active_record_postgresql.html +0 -675
  51. data/guides/output/active_record_querying.html +0 -1796
  52. data/guides/output/active_record_validations.html +0 -1301
  53. data/guides/output/active_support_core_extensions.html +0 -3450
  54. data/guides/output/active_support_instrumentation.html +0 -1121
  55. data/guides/output/api_documentation_guidelines.html +0 -498
  56. data/guides/output/asset_pipeline.html +0 -1167
  57. data/guides/output/association_basics.html +0 -2086
  58. data/guides/output/caching_with_rails.html +0 -553
  59. data/guides/output/command_line.html +0 -791
  60. data/guides/output/configuring.html +0 -1055
  61. data/guides/output/contributing_to_ruby_on_rails.html +0 -657
  62. data/guides/output/credits.html +0 -284
  63. data/guides/output/debugging_rails_applications.html +0 -1014
  64. data/guides/output/development_dependencies_install.html +0 -478
  65. data/guides/output/engines.html +0 -1438
  66. data/guides/output/form_helpers.html +0 -1074
  67. data/guides/output/generators.html +0 -838
  68. data/guides/output/getting_started.html +0 -2092
  69. data/guides/output/i18n.html +0 -1198
  70. data/guides/output/images/akshaysurve.jpg +0 -0
  71. data/guides/output/images/belongs_to.png +0 -0
  72. data/guides/output/images/book_icon.gif +0 -0
  73. data/guides/output/images/bullet.gif +0 -0
  74. data/guides/output/images/chapters_icon.gif +0 -0
  75. data/guides/output/images/check_bullet.gif +0 -0
  76. data/guides/output/images/credits_pic_blank.gif +0 -0
  77. data/guides/output/images/csrf.png +0 -0
  78. data/guides/output/images/edge_badge.png +0 -0
  79. data/guides/output/images/favicon.ico +0 -0
  80. data/guides/output/images/feature_tile.gif +0 -0
  81. data/guides/output/images/footer_tile.gif +0 -0
  82. data/guides/output/images/fxn.png +0 -0
  83. data/guides/output/images/getting_started/article_with_comments.png +0 -0
  84. data/guides/output/images/getting_started/challenge.png +0 -0
  85. data/guides/output/images/getting_started/confirm_dialog.png +0 -0
  86. data/guides/output/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
  87. data/guides/output/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  88. data/guides/output/images/getting_started/form_with_errors.png +0 -0
  89. data/guides/output/images/getting_started/index_action_with_edit_link.png +0 -0
  90. data/guides/output/images/getting_started/new_article.png +0 -0
  91. data/guides/output/images/getting_started/new_post.png +0 -0
  92. data/guides/output/images/getting_started/post_with_comments.png +0 -0
  93. data/guides/output/images/getting_started/rails_welcome.png +0 -0
  94. data/guides/output/images/getting_started/routing_error_no_controller.png +0 -0
  95. data/guides/output/images/getting_started/routing_error_no_route_matches.png +0 -0
  96. data/guides/output/images/getting_started/show_action_for_articles.png +0 -0
  97. data/guides/output/images/getting_started/show_action_for_posts.png +0 -0
  98. data/guides/output/images/getting_started/template_is_missing_articles_new.png +0 -0
  99. data/guides/output/images/getting_started/template_is_missing_posts_new.png +0 -0
  100. data/guides/output/images/getting_started/undefined_method_post_path.png +0 -0
  101. data/guides/output/images/getting_started/unknown_action_create_for_articles.png +0 -0
  102. data/guides/output/images/getting_started/unknown_action_create_for_posts.png +0 -0
  103. data/guides/output/images/getting_started/unknown_action_new_for_articles.png +0 -0
  104. data/guides/output/images/getting_started/unknown_action_new_for_posts.png +0 -0
  105. data/guides/output/images/grey_bullet.gif +0 -0
  106. data/guides/output/images/habtm.png +0 -0
  107. data/guides/output/images/has_many.png +0 -0
  108. data/guides/output/images/has_many_through.png +0 -0
  109. data/guides/output/images/has_one.png +0 -0
  110. data/guides/output/images/has_one_through.png +0 -0
  111. data/guides/output/images/header_backdrop.png +0 -0
  112. data/guides/output/images/header_tile.gif +0 -0
  113. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  114. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  115. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  116. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  117. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  118. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  119. data/guides/output/images/icons/README +0 -5
  120. data/guides/output/images/icons/callouts/1.png +0 -0
  121. data/guides/output/images/icons/callouts/10.png +0 -0
  122. data/guides/output/images/icons/callouts/11.png +0 -0
  123. data/guides/output/images/icons/callouts/12.png +0 -0
  124. data/guides/output/images/icons/callouts/13.png +0 -0
  125. data/guides/output/images/icons/callouts/14.png +0 -0
  126. data/guides/output/images/icons/callouts/15.png +0 -0
  127. data/guides/output/images/icons/callouts/2.png +0 -0
  128. data/guides/output/images/icons/callouts/3.png +0 -0
  129. data/guides/output/images/icons/callouts/4.png +0 -0
  130. data/guides/output/images/icons/callouts/5.png +0 -0
  131. data/guides/output/images/icons/callouts/6.png +0 -0
  132. data/guides/output/images/icons/callouts/7.png +0 -0
  133. data/guides/output/images/icons/callouts/8.png +0 -0
  134. data/guides/output/images/icons/callouts/9.png +0 -0
  135. data/guides/output/images/icons/caution.png +0 -0
  136. data/guides/output/images/icons/example.png +0 -0
  137. data/guides/output/images/icons/home.png +0 -0
  138. data/guides/output/images/icons/important.png +0 -0
  139. data/guides/output/images/icons/next.png +0 -0
  140. data/guides/output/images/icons/note.png +0 -0
  141. data/guides/output/images/icons/prev.png +0 -0
  142. data/guides/output/images/icons/tip.png +0 -0
  143. data/guides/output/images/icons/up.png +0 -0
  144. data/guides/output/images/icons/warning.png +0 -0
  145. data/guides/output/images/nav_arrow.gif +0 -0
  146. data/guides/output/images/oscardelben.jpg +0 -0
  147. data/guides/output/images/polymorphic.png +0 -0
  148. data/guides/output/images/radar.png +0 -0
  149. data/guides/output/images/rails4_features.png +0 -0
  150. data/guides/output/images/rails_guides_kindle_cover.jpg +0 -0
  151. data/guides/output/images/rails_guides_logo.gif +0 -0
  152. data/guides/output/images/rails_logo_remix.gif +0 -0
  153. data/guides/output/images/session_fixation.png +0 -0
  154. data/guides/output/images/tab_grey.gif +0 -0
  155. data/guides/output/images/tab_info.gif +0 -0
  156. data/guides/output/images/tab_note.gif +0 -0
  157. data/guides/output/images/tab_red.gif +0 -0
  158. data/guides/output/images/tab_yellow.gif +0 -0
  159. data/guides/output/images/tab_yellow.png +0 -0
  160. data/guides/output/images/vijaydev.jpg +0 -0
  161. data/guides/output/index.html +0 -354
  162. data/guides/output/initialization.html +0 -876
  163. data/guides/output/javascripts/guides.js +0 -59
  164. data/guides/output/javascripts/jquery.min.js +0 -4
  165. data/guides/output/javascripts/responsive-tables.js +0 -43
  166. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  167. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  168. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  169. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  170. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  171. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  172. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  173. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  174. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  175. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  176. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  177. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  178. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  179. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  180. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  181. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  182. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  183. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  184. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  185. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  186. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  187. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  188. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  189. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  190. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  191. data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
  192. data/guides/output/layout.html +0 -448
  193. data/guides/output/layouts_and_rendering.html +0 -1541
  194. data/guides/output/maintenance_policy.html +0 -257
  195. data/guides/output/migrations.html +0 -1360
  196. data/guides/output/nested_model_forms.html +0 -412
  197. data/guides/output/plugins.html +0 -644
  198. data/guides/output/rails_application_templates.html +0 -450
  199. data/guides/output/rails_on_rack.html +0 -547
  200. data/guides/output/routing.html +0 -1631
  201. data/guides/output/ruby_on_rails_guides_guidelines.html +0 -329
  202. data/guides/output/security.html +0 -935
  203. data/guides/output/stylesheets/fixes.css +0 -16
  204. data/guides/output/stylesheets/kindle.css +0 -11
  205. data/guides/output/stylesheets/main.css +0 -713
  206. data/guides/output/stylesheets/print.css +0 -52
  207. data/guides/output/stylesheets/reset.css +0 -43
  208. data/guides/output/stylesheets/responsive-tables.css +0 -50
  209. data/guides/output/stylesheets/style.css +0 -13
  210. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
  211. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  212. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  213. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  214. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  215. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  216. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  217. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  218. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  219. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  220. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  221. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  222. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  223. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  224. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  225. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  226. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  227. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  228. data/guides/output/testing.html +0 -1350
  229. data/guides/output/upgrading_ruby_on_rails.html +0 -1135
  230. data/guides/output/working_with_javascript_in_rails.html +0 -587
@@ -1,553 +0,0 @@
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>Caching with Rails: An overview — 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>Caching with Rails: An overview</h2><p>This guide will teach you what you need to know about avoiding that expensive round-trip to your database and returning what you need to return to the web clients in the shortest time possible.</p><p>After reading this guide, you will know:</p>
165
- <ul>
166
- <li>Page and action caching (moved to separate gems as of Rails 4).</li>
167
- <li>Fragment caching.</li>
168
- <li>Alternative cache stores.</li>
169
- <li>Conditional GET support.</li>
170
- </ul>
171
-
172
-
173
- <div id="subCol">
174
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
175
- <ol class="chapters">
176
- <li>
177
- <a href="#basic-caching">Basic Caching</a>
178
-
179
- <ul>
180
- <li><a href="#page-caching">Page Caching</a></li>
181
- <li><a href="#action-caching">Action Caching</a></li>
182
- <li><a href="#fragment-caching">Fragment Caching</a></li>
183
- <li><a href="#low-level-caching">Low-Level Caching</a></li>
184
- <li><a href="#sql-caching">SQL Caching</a></li>
185
- </ul>
186
- </li>
187
- <li>
188
- <a href="#cache-stores">Cache Stores</a>
189
-
190
- <ul>
191
- <li><a href="#configuration">Configuration</a></li>
192
- <li><a href="#activesupport::cache::store">ActiveSupport::Cache::Store</a></li>
193
- <li><a href="#activesupport::cache::memorystore">ActiveSupport::Cache::MemoryStore</a></li>
194
- <li><a href="#activesupport::cache::filestore">ActiveSupport::Cache::FileStore</a></li>
195
- <li><a href="#activesupport::cache::memcachestore">ActiveSupport::Cache::MemCacheStore</a></li>
196
- <li><a href="#activesupport::cache::ehcachestore">ActiveSupport::Cache::EhcacheStore</a></li>
197
- <li><a href="#activesupport::cache::nullstore">ActiveSupport::Cache::NullStore</a></li>
198
- <li><a href="#custom-cache-stores">Custom Cache Stores</a></li>
199
- <li><a href="#cache-keys">Cache Keys</a></li>
200
- </ul>
201
- </li>
202
- <li><a href="#conditional-get-support">Conditional GET support</a></li>
203
- </ol>
204
-
205
- </div>
206
-
207
- </div>
208
- </div>
209
-
210
- <div id="container">
211
- <div class="wrapper">
212
- <div id="mainCol">
213
- <h3 id="basic-caching">1 Basic Caching</h3><p>This is an introduction to three types of caching techniques: page, action and
214
- fragment caching. Rails provides by default fragment caching. In order to use
215
- page and action caching, you will need to add <code>actionpack-page_caching</code> and
216
- <code>actionpack-action_caching</code> to your Gemfile.</p><p>To start playing with caching you'll want to ensure that <code>config.action_controller.perform_caching</code> is set to <code>true</code>, if you're running in development mode. This flag is normally set in the corresponding <code>config/environments/*.rb</code> and caching is disabled by default for development and test, and enabled for production.</p><div class="code_container">
217
- <pre class="brush: ruby; gutter: false; toolbar: false">
218
- config.action_controller.perform_caching = true
219
-
220
- </pre>
221
- </div>
222
- <h4 id="page-caching">1.1 Page Caching</h4><p>Page caching is a Rails mechanism which allows the request for a generated page to be fulfilled by the webserver (i.e. Apache or NGINX), without ever having to go through the Rails stack at all. Obviously, this is super-fast. Unfortunately, it can't be applied to every situation (such as pages that need authentication) and since the webserver is literally just serving a file from the filesystem, cache expiration is an issue that needs to be dealt with.</p><div class="info"><p>Page Caching has been removed from Rails 4. See the <a href="https://github.com/rails/actionpack-page_caching">actionpack-page_caching gem</a>. See <a href="http://signalvnoise.com/posts/3113-how-key-based-cache-expiration-works">DHH's key-based cache expiration overview</a> for the newly-preferred method.</p></div><h4 id="action-caching">1.2 Action Caching</h4><p>Page Caching cannot be used for actions that have before filters - for example, pages that require authentication. This is where Action Caching comes in. Action Caching works like Page Caching except the incoming web request hits the Rails stack so that before filters can be run on it before the cache is served. This allows authentication and other restrictions to be run while still serving the result of the output from a cached copy.</p><div class="info"><p>Action Caching has been removed from Rails 4. See the <a href="https://github.com/rails/actionpack-action_caching">actionpack-action_caching gem</a>. See <a href="http://signalvnoise.com/posts/3113-how-key-based-cache-expiration-works">DHH's key-based cache expiration overview</a> for the newly-preferred method.</p></div><h4 id="fragment-caching">1.3 Fragment Caching</h4><p>Life would be perfect if we could get away with caching the entire contents of a page or action and serving it out to the world. Unfortunately, dynamic web applications usually build pages with a variety of components not all of which have the same caching characteristics. In order to address such a dynamically created page where different parts of the page need to be cached and expired differently, Rails provides a mechanism called Fragment Caching.</p><p>Fragment Caching allows a fragment of view logic to be wrapped in a cache block and served out of the cache store when the next request comes in.</p><p>As an example, if you wanted to show all the orders placed on your website in real time and didn't want to cache that part of the page, but did want to cache the part of the page which lists all products available, you could use this piece of code:</p><div class="code_container">
223
- <pre class="brush: plain; gutter: false; toolbar: false">
224
- &lt;% Order.find_recent.each do |o| %&gt;
225
- &lt;%= o.buyer.name %&gt; bought &lt;%= o.product.name %&gt;
226
- &lt;% end %&gt;
227
-
228
- &lt;% cache do %&gt;
229
- All available products:
230
- &lt;% Product.all.each do |p| %&gt;
231
- &lt;%= link_to p.name, product_url(p) %&gt;
232
- &lt;% end %&gt;
233
- &lt;% end %&gt;
234
-
235
- </pre>
236
- </div>
237
- <p>The cache block in our example will bind to the action that called it and is written out to the same place as the Action Cache, which means that if you want to cache multiple fragments per action, you should provide an <code>action_suffix</code> to the cache call:</p><div class="code_container">
238
- <pre class="brush: plain; gutter: false; toolbar: false">
239
- &lt;% cache(action: 'recent', action_suffix: 'all_products') do %&gt;
240
- All available products:
241
-
242
- </pre>
243
- </div>
244
- <p>and you can expire it using the <code>expire_fragment</code> method, like so:</p><div class="code_container">
245
- <pre class="brush: ruby; gutter: false; toolbar: false">
246
- expire_fragment(controller: 'products', action: 'recent', action_suffix: 'all_products')
247
-
248
- </pre>
249
- </div>
250
- <p>If you don't want the cache block to bind to the action that called it, you can also use globally keyed fragments by calling the <code>cache</code> method with a key:</p><div class="code_container">
251
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
252
- &lt;% cache('all_available_products') do %&gt;
253
- All available products:
254
- &lt;% end %&gt;
255
-
256
- </pre>
257
- </div>
258
- <p>This fragment is then available to all actions in the <code>ProductsController</code> using the key and can be expired the same way:</p><div class="code_container">
259
- <pre class="brush: ruby; gutter: false; toolbar: false">
260
- expire_fragment('all_available_products')
261
-
262
- </pre>
263
- </div>
264
- <p>If you want to avoid expiring the fragment manually, whenever an action updates a product, you can define a helper method:</p><div class="code_container">
265
- <pre class="brush: ruby; gutter: false; toolbar: false">
266
- module ProductsHelper
267
- def cache_key_for_products
268
- count = Product.count
269
- max_updated_at = Product.maximum(:updated_at).try(:utc).try(:to_s, :number)
270
- "products/all-#{count}-#{max_updated_at}"
271
- end
272
- end
273
-
274
- </pre>
275
- </div>
276
- <p>This method generates a cache key that depends on all products and can be used in the view:</p><div class="code_container">
277
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
278
- &lt;% cache(cache_key_for_products) do %&gt;
279
- All available products:
280
- &lt;% end %&gt;
281
-
282
- </pre>
283
- </div>
284
- <p>If you want to cache a fragment under certain condition you can use <code>cache_if</code> or <code>cache_unless</code></p><div class="code_container">
285
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
286
- &lt;% cache_if (condition, cache_key_for_products) do %&gt;
287
- All available products:
288
- &lt;% end %&gt;
289
-
290
- </pre>
291
- </div>
292
- <p>You can also use an Active Record model as the cache key:</p><div class="code_container">
293
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
294
- &lt;% Product.all.each do |p| %&gt;
295
- &lt;% cache(p) do %&gt;
296
- &lt;%= link_to p.name, product_url(p) %&gt;
297
- &lt;% end %&gt;
298
- &lt;% end %&gt;
299
-
300
- </pre>
301
- </div>
302
- <p>Behind the scenes, a method called <code>cache_key</code> will be invoked on the model and it returns a string like <code>products/23-20130109142513</code>. The cache key includes the model name, the id and finally the updated_at timestamp. Thus it will automatically generate a new fragment when the product is updated because the key changes.</p><p>You can also combine the two schemes which is called "Russian Doll Caching":</p><div class="code_container">
303
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
304
- &lt;% cache(cache_key_for_products) do %&gt;
305
- All available products:
306
- &lt;% Product.all.each do |p| %&gt;
307
- &lt;% cache(p) do %&gt;
308
- &lt;%= link_to p.name, product_url(p) %&gt;
309
- &lt;% end %&gt;
310
- &lt;% end %&gt;
311
- &lt;% end %&gt;
312
-
313
- </pre>
314
- </div>
315
- <p>It's called "Russian Doll Caching" because it nests multiple fragments. The advantage is that if a single product is updated, all the other inner fragments can be reused when regenerating the outer fragment.</p><h4 id="low-level-caching">1.4 Low-Level Caching</h4><p>Sometimes you need to cache a particular value or query result, instead of caching view fragments. Rails caching mechanism works great for storing <strong>any</strong> kind of information.</p><p>The most efficient way to implement low-level caching is using the <code>Rails.cache.fetch</code> method. This method does both reading and writing to the cache. When passed only a single argument, the key is fetched and value from the cache is returned. If a block is passed, the result of the block will be cached to the given key and the result is returned.</p><p>Consider the following example. An application has a <code>Product</code> model with an instance method that looks up the product’s price on a competing website. The data returned by this method would be perfect for low-level caching:</p><div class="code_container">
316
- <pre class="brush: ruby; gutter: false; toolbar: false">
317
- class Product &lt; ActiveRecord::Base
318
- def competing_price
319
- Rails.cache.fetch("#{cache_key}/competing_price", expires_in: 12.hours) do
320
- Competitor::API.find_price(id)
321
- end
322
- end
323
- end
324
-
325
- </pre>
326
- </div>
327
- <div class="note"><p>Notice that in this example we used <code>cache_key</code> method, so the resulting cache-key will be something like <code>products/233-20140225082222765838000/competing_price</code>. <code>cache_key</code> generates a string based on the model’s <code>id</code> and <code>updated_at</code> attributes. This is a common convention and has the benefit of invalidating the cache whenever the product is updated. In general, when you use low-level caching for instance level information, you need to generate a cache key.</p></div><h4 id="sql-caching">1.5 SQL Caching</h4><p>Query caching is a Rails feature that caches the result set returned by each query so that if Rails encounters the same query again for that request, it will use the cached result set as opposed to running the query against the database again.</p><p>For example:</p><div class="code_container">
328
- <pre class="brush: ruby; gutter: false; toolbar: false">
329
- class ProductsController &lt; ApplicationController
330
-
331
- def index
332
- # Run a find query
333
- @products = Product.all
334
-
335
- ...
336
-
337
- # Run the same query again
338
- @products = Product.all
339
- end
340
-
341
- end
342
-
343
- </pre>
344
- </div>
345
- <h3 id="cache-stores">2 Cache Stores</h3><p>Rails provides different stores for the cached data created by <strong>action</strong> and <strong>fragment</strong> caches.</p><div class="info"><p>Page caches are always stored on disk.</p></div><h4 id="configuration">2.1 Configuration</h4><p>You can set up your application's default cache store by calling <code>config.cache_store=</code> in the Application definition inside your <code>config/application.rb</code> file or in an Application.configure block in an environment specific configuration file (i.e. <code>config/environments/*.rb</code>). The first argument will be the cache store to use and the rest of the argument will be passed as arguments to the cache store constructor.</p><div class="code_container">
346
- <pre class="brush: ruby; gutter: false; toolbar: false">
347
- config.cache_store = :memory_store
348
-
349
- </pre>
350
- </div>
351
- <div class="note"><p>Alternatively, you can call <code>ActionController::Base.cache_store</code> outside of a configuration block.</p></div><p>You can access the cache by calling <code>Rails.cache</code>.</p><h4 id="activesupport::cache::store">2.2 ActiveSupport::Cache::Store</h4><p>This class provides the foundation for interacting with the cache in Rails. This is an abstract class and you cannot use it on its own. Rather you must use a concrete implementation of the class tied to a storage engine. Rails ships with several implementations documented below.</p><p>The main methods to call are <code>read</code>, <code>write</code>, <code>delete</code>, <code>exist?</code>, and <code>fetch</code>. The fetch method takes a block and will either return an existing value from the cache, or evaluate the block and write the result to the cache if no value exists.</p><p>There are some common options used by all cache implementations. These can be passed to the constructor or the various methods to interact with entries.</p>
352
- <ul>
353
- <li><p><code>:namespace</code> - This option can be used to create a namespace within the cache store. It is especially useful if your application shares a cache with other applications.</p></li>
354
- <li><p><code>:compress</code> - This option can be used to indicate that compression should be used in the cache. This can be useful for transferring large cache entries over a slow network.</p></li>
355
- <li><p><code>:compress_threshold</code> - This options is used in conjunction with the <code>:compress</code> option to indicate a threshold under which cache entries should not be compressed. This defaults to 16 kilobytes.</p></li>
356
- <li><p><code>:expires_in</code> - This option sets an expiration time in seconds for the cache entry when it will be automatically removed from the cache.</p></li>
357
- <li><p><code>:race_condition_ttl</code> - This option is used in conjunction with the <code>:expires_in</code> option. It will prevent race conditions when cache entries expire by preventing multiple processes from simultaneously regenerating the same entry (also known as the dog pile effect). This option sets the number of seconds that an expired entry can be reused while a new value is being regenerated. It's a good practice to set this value if you use the <code>:expires_in</code> option.</p></li>
358
- </ul>
359
- <h4 id="activesupport::cache::memorystore">2.3 ActiveSupport::Cache::MemoryStore</h4><p>This cache store keeps entries in memory in the same Ruby process. The cache store has a bounded size specified by the <code>:size</code> options to the initializer (default is 32Mb). When the cache exceeds the allotted size, a cleanup will occur and the least recently used entries will be removed.</p><div class="code_container">
360
- <pre class="brush: ruby; gutter: false; toolbar: false">
361
- config.cache_store = :memory_store, { size: 64.megabytes }
362
-
363
- </pre>
364
- </div>
365
- <p>If you're running multiple Ruby on Rails server processes (which is the case if you're using mongrel_cluster or Phusion Passenger), then your Rails server process instances won't be able to share cache data with each other. This cache store is not appropriate for large application deployments, but can work well for small, low traffic sites with only a couple of server processes or for development and test environments.</p><h4 id="activesupport::cache::filestore">2.4 ActiveSupport::Cache::FileStore</h4><p>This cache store uses the file system to store entries. The path to the directory where the store files will be stored must be specified when initializing the cache.</p><div class="code_container">
366
- <pre class="brush: ruby; gutter: false; toolbar: false">
367
- config.cache_store = :file_store, "/path/to/cache/directory"
368
-
369
- </pre>
370
- </div>
371
- <p>With this cache store, multiple server processes on the same host can share a cache. Servers processes running on different hosts could share a cache by using a shared file system, but that set up would not be ideal and is not recommended. The cache store is appropriate for low to medium traffic sites that are served off one or two hosts.</p><p>Note that the cache will grow until the disk is full unless you periodically clear out old entries.</p><p>This is the default cache store implementation.</p><h4 id="activesupport::cache::memcachestore">2.5 ActiveSupport::Cache::MemCacheStore</h4><p>This cache store uses Danga's <code>memcached</code> server to provide a centralized cache for your application. Rails uses the bundled <code>dalli</code> gem by default. This is currently the most popular cache store for production websites. It can be used to provide a single, shared cache cluster with very high performance and redundancy.</p><p>When initializing the cache, you need to specify the addresses for all memcached servers in your cluster. If none is specified, it will assume memcached is running on the local host on the default port, but this is not an ideal set up for larger sites.</p><p>The <code>write</code> and <code>fetch</code> methods on this cache accept two additional options that take advantage of features specific to memcached. You can specify <code>:raw</code> to send a value directly to the server with no serialization. The value must be a string or number. You can use memcached direct operation like <code>increment</code> and <code>decrement</code> only on raw values. You can also specify <code>:unless_exist</code> if you don't want memcached to overwrite an existing entry.</p><div class="code_container">
372
- <pre class="brush: ruby; gutter: false; toolbar: false">
373
- config.cache_store = :mem_cache_store, "cache-1.example.com", "cache-2.example.com"
374
-
375
- </pre>
376
- </div>
377
- <h4 id="activesupport::cache::ehcachestore">2.6 ActiveSupport::Cache::EhcacheStore</h4><p>If you are using JRuby you can use Terracotta's Ehcache as the cache store for your application. Ehcache is an open source Java cache that also offers an enterprise version with increased scalability, management, and commercial support. You must first install the jruby-ehcache-rails3 gem (version 1.1.0 or later) to use this cache store.</p><div class="code_container">
378
- <pre class="brush: ruby; gutter: false; toolbar: false">
379
- config.cache_store = :ehcache_store
380
-
381
- </pre>
382
- </div>
383
- <p>When initializing the cache, you may use the <code>:ehcache_config</code> option to specify the Ehcache config file to use (where the default is "ehcache.xml" in your Rails config directory), and the :cache_name option to provide a custom name for your cache (the default is rails_cache).</p><p>In addition to the standard <code>:expires_in</code> option, the <code>write</code> method on this cache can also accept the additional <code>:unless_exist</code> option, which will cause the cache store to use Ehcache's <code>putIfAbsent</code> method instead of <code>put</code>, and therefore will not overwrite an existing entry. Additionally, the <code>write</code> method supports all of the properties exposed by the <a href="http://ehcache.org/apidocs/net/sf/ehcache/Element.html">Ehcache Element class</a> , including:</p>
384
- <table>
385
- <thead>
386
- <tr>
387
- <th>Property</th>
388
- <th>Argument Type</th>
389
- <th>Description</th>
390
- </tr>
391
- </thead>
392
- <tbody>
393
- <tr>
394
- <td>elementEvictionData</td>
395
- <td>ElementEvictionData</td>
396
- <td>Sets this element's eviction data instance.</td>
397
- </tr>
398
- <tr>
399
- <td>eternal</td>
400
- <td>boolean</td>
401
- <td>Sets whether the element is eternal.</td>
402
- </tr>
403
- <tr>
404
- <td>timeToIdle, tti</td>
405
- <td>int</td>
406
- <td>Sets time to idle</td>
407
- </tr>
408
- <tr>
409
- <td>timeToLive, ttl, expires_in</td>
410
- <td>int</td>
411
- <td>Sets time to Live</td>
412
- </tr>
413
- <tr>
414
- <td>version</td>
415
- <td>long</td>
416
- <td>Sets the version attribute of the ElementAttributes object.</td>
417
- </tr>
418
- </tbody>
419
- </table>
420
- <p>These options are passed to the <code>write</code> method as Hash options using either camelCase or underscore notation, as in the following examples:</p><div class="code_container">
421
- <pre class="brush: ruby; gutter: false; toolbar: false">
422
- Rails.cache.write('key', 'value', time_to_idle: 60.seconds, timeToLive: 600.seconds)
423
- caches_action :index, expires_in: 60.seconds, unless_exist: true
424
-
425
- </pre>
426
- </div>
427
- <p>For more information about Ehcache, see <a href="http://ehcache.org/">http://ehcache.org/</a> .
428
- For more information about Ehcache for JRuby and Rails, see <a href="http://ehcache.org/documentation/jruby.html">http://ehcache.org/documentation/jruby.html</a></p><h4 id="activesupport::cache::nullstore">2.7 ActiveSupport::Cache::NullStore</h4><p>This cache store implementation is meant to be used only in development or test environments and it never stores anything. This can be very useful in development when you have code that interacts directly with <code>Rails.cache</code>, but caching may interfere with being able to see the results of code changes. With this cache store, all <code>fetch</code> and <code>read</code> operations will result in a miss.</p><div class="code_container">
429
- <pre class="brush: ruby; gutter: false; toolbar: false">
430
- config.cache_store = :null_store
431
-
432
- </pre>
433
- </div>
434
- <h4 id="custom-cache-stores">2.8 Custom Cache Stores</h4><p>You can create your own custom cache store by simply extending <code>ActiveSupport::Cache::Store</code> and implementing the appropriate methods. In this way, you can swap in any number of caching technologies into your Rails application.</p><p>To use a custom cache store, simple set the cache store to a new instance of the class.</p><div class="code_container">
435
- <pre class="brush: ruby; gutter: false; toolbar: false">
436
- config.cache_store = MyCacheStore.new
437
-
438
- </pre>
439
- </div>
440
- <h4 id="cache-keys">2.9 Cache Keys</h4><p>The keys used in a cache can be any object that responds to either <code>:cache_key</code> or to <code>:to_param</code>. You can implement the <code>:cache_key</code> method on your classes if you need to generate custom keys. Active Record will generate keys based on the class name and record id.</p><p>You can use Hashes and Arrays of values as cache keys.</p><div class="code_container">
441
- <pre class="brush: ruby; gutter: false; toolbar: false">
442
- # This is a legal cache key
443
- Rails.cache.read(site: "mysite", owners: [owner_1, owner_2])
444
-
445
- </pre>
446
- </div>
447
- <p>The keys you use on <code>Rails.cache</code> will not be the same as those actually used with the storage engine. They may be modified with a namespace or altered to fit technology backend constraints. This means, for instance, that you can't save values with <code>Rails.cache</code> and then try to pull them out with the <code>memcache-client</code> gem. However, you also don't need to worry about exceeding the memcached size limit or violating syntax rules.</p><h3 id="conditional-get-support">3 Conditional GET support</h3><p>Conditional GETs are a feature of the HTTP specification that provide a way for web servers to tell browsers that the response to a GET request hasn't changed since the last request and can be safely pulled from the browser cache.</p><p>They work by using the <code>HTTP_IF_NONE_MATCH</code> and <code>HTTP_IF_MODIFIED_SINCE</code> headers to pass back and forth both a unique content identifier and the timestamp of when the content was last changed. If the browser makes a request where the content identifier (etag) or last modified since timestamp matches the server's version then the server only needs to send back an empty response with a not modified status.</p><p>It is the server's (i.e. our) responsibility to look for a last modified timestamp and the if-none-match header and determine whether or not to send back the full response. With conditional-get support in Rails this is a pretty easy task:</p><div class="code_container">
448
- <pre class="brush: ruby; gutter: false; toolbar: false">
449
- class ProductsController &lt; ApplicationController
450
-
451
- def show
452
- @product = Product.find(params[:id])
453
-
454
- # If the request is stale according to the given timestamp and etag value
455
- # (i.e. it needs to be processed again) then execute this block
456
- if stale?(last_modified: @product.updated_at.utc, etag: @product.cache_key)
457
- respond_to do |wants|
458
- # ... normal response processing
459
- end
460
- end
461
-
462
- # If the request is fresh (i.e. it's not modified) then you don't need to do
463
- # anything. The default render checks for this using the parameters
464
- # used in the previous call to stale? and will automatically send a
465
- # :not_modified. So that's it, you're done.
466
- end
467
- end
468
-
469
- </pre>
470
- </div>
471
- <p>Instead of an options hash, you can also simply pass in a model, Rails will use the <code>updated_at</code> and <code>cache_key</code> methods for setting <code>last_modified</code> and <code>etag</code>:</p><div class="code_container">
472
- <pre class="brush: ruby; gutter: false; toolbar: false">
473
- class ProductsController &lt; ApplicationController
474
- def show
475
- @product = Product.find(params[:id])
476
-
477
- if stale?(@product)
478
- respond_to do |wants|
479
- # ... normal response processing
480
- end
481
- end
482
- end
483
- end
484
-
485
- </pre>
486
- </div>
487
- <p>If you don't have any special response processing and are using the default rendering mechanism (i.e. you're not using respond_to or calling render yourself) then you've got an easy helper in fresh_when:</p><div class="code_container">
488
- <pre class="brush: ruby; gutter: false; toolbar: false">
489
- class ProductsController &lt; ApplicationController
490
-
491
- # This will automatically send back a :not_modified if the request is fresh,
492
- # and will render the default template (product.*) if it's stale.
493
-
494
- def show
495
- @product = Product.find(params[:id])
496
- fresh_when last_modified: @product.published_at.utc, etag: @product
497
- end
498
- end
499
-
500
- </pre>
501
- </div>
502
-
503
-
504
- <h3>Feedback</h3>
505
- <p>
506
- You're encouraged to help improve the quality of this guide.
507
- </p>
508
- <p>
509
- Please contribute if you see any typos or factual errors.
510
- 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.
511
- </p>
512
- <p>
513
- You may also find incomplete content, or stuff that is not up to date.
514
- Please do add any missing documentation for master. Make sure to check
515
- <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
516
- if the issues are already fixed or not on the master branch.
517
- Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
518
- for style and conventions.
519
- </p>
520
- <p>
521
- If for whatever reason you spot something to fix but cannot patch it yourself, please
522
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
523
- </p>
524
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
525
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
526
- </p>
527
- </div>
528
- </div>
529
- </div>
530
-
531
- <hr class="hide" />
532
- <div id="footer">
533
- <div class="wrapper">
534
- <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>
535
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
536
-
537
- </div>
538
- </div>
539
-
540
- <script type="text/javascript" src="javascripts/jquery.min.js"></script>
541
- <script type="text/javascript" src="javascripts/responsive-tables.js"></script>
542
- <script type="text/javascript" src="javascripts/guides.js"></script>
543
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
544
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
545
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
546
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
547
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
548
- <script type="text/javascript">
549
- SyntaxHighlighter.all();
550
- $(guidesIndex.bind);
551
- </script>
552
- </body>
553
- </html>