rails 3.2.22.5 → 4.0.0.beta1

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 (281) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +77 -0
  3. data/guides/CHANGELOG.md +9 -0
  4. data/guides/Rakefile +77 -0
  5. data/guides/assets/images/belongs_to.png +0 -0
  6. data/guides/assets/images/book_icon.gif +0 -0
  7. data/guides/assets/images/bullet.gif +0 -0
  8. data/guides/assets/images/challenge.png +0 -0
  9. data/guides/assets/images/chapters_icon.gif +0 -0
  10. data/guides/assets/images/check_bullet.gif +0 -0
  11. data/guides/assets/images/credits_pic_blank.gif +0 -0
  12. data/guides/assets/images/csrf.png +0 -0
  13. data/guides/assets/images/edge_badge.png +0 -0
  14. data/guides/assets/images/favicon.ico +0 -0
  15. data/guides/assets/images/feature_tile.gif +0 -0
  16. data/guides/assets/images/footer_tile.gif +0 -0
  17. data/guides/assets/images/fxn.png +0 -0
  18. data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
  19. data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  20. data/guides/assets/images/getting_started/form_with_errors.png +0 -0
  21. data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
  22. data/guides/assets/images/getting_started/new_post.png +0 -0
  23. data/guides/assets/images/getting_started/post_with_comments.png +0 -0
  24. data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
  25. data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
  26. data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
  27. data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
  28. data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
  29. data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
  30. data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
  31. data/guides/assets/images/grey_bullet.gif +0 -0
  32. data/guides/assets/images/habtm.png +0 -0
  33. data/guides/assets/images/has_many.png +0 -0
  34. data/guides/assets/images/has_many_through.png +0 -0
  35. data/guides/assets/images/has_one.png +0 -0
  36. data/guides/assets/images/has_one_through.png +0 -0
  37. data/guides/assets/images/header_backdrop.png +0 -0
  38. data/guides/assets/images/header_tile.gif +0 -0
  39. data/guides/assets/images/i18n/demo_html_safe.png +0 -0
  40. data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
  41. data/guides/assets/images/i18n/demo_translated_en.png +0 -0
  42. data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
  43. data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
  44. data/guides/assets/images/i18n/demo_untranslated.png +0 -0
  45. data/guides/assets/images/icons/README +5 -0
  46. data/guides/assets/images/icons/callouts/1.png +0 -0
  47. data/guides/assets/images/icons/callouts/10.png +0 -0
  48. data/guides/assets/images/icons/callouts/11.png +0 -0
  49. data/guides/assets/images/icons/callouts/12.png +0 -0
  50. data/guides/assets/images/icons/callouts/13.png +0 -0
  51. data/guides/assets/images/icons/callouts/14.png +0 -0
  52. data/guides/assets/images/icons/callouts/15.png +0 -0
  53. data/guides/assets/images/icons/callouts/2.png +0 -0
  54. data/guides/assets/images/icons/callouts/3.png +0 -0
  55. data/guides/assets/images/icons/callouts/4.png +0 -0
  56. data/guides/assets/images/icons/callouts/5.png +0 -0
  57. data/guides/assets/images/icons/callouts/6.png +0 -0
  58. data/guides/assets/images/icons/callouts/7.png +0 -0
  59. data/guides/assets/images/icons/callouts/8.png +0 -0
  60. data/guides/assets/images/icons/callouts/9.png +0 -0
  61. data/guides/assets/images/icons/caution.png +0 -0
  62. data/guides/assets/images/icons/example.png +0 -0
  63. data/guides/assets/images/icons/home.png +0 -0
  64. data/guides/assets/images/icons/important.png +0 -0
  65. data/guides/assets/images/icons/next.png +0 -0
  66. data/guides/assets/images/icons/note.png +0 -0
  67. data/guides/assets/images/icons/prev.png +0 -0
  68. data/guides/assets/images/icons/tip.png +0 -0
  69. data/guides/assets/images/icons/up.png +0 -0
  70. data/guides/assets/images/icons/warning.png +0 -0
  71. data/guides/assets/images/jaimeiniesta.jpg +0 -0
  72. data/guides/assets/images/nav_arrow.gif +0 -0
  73. data/guides/assets/images/oscardelben.jpg +0 -0
  74. data/guides/assets/images/polymorphic.png +0 -0
  75. data/guides/assets/images/radar.png +0 -0
  76. data/guides/assets/images/rails4_features.png +0 -0
  77. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  78. data/guides/assets/images/rails_guides_logo.gif +0 -0
  79. data/guides/assets/images/rails_logo_remix.gif +0 -0
  80. data/guides/assets/images/rails_welcome.png +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 +57 -0
  90. data/guides/assets/javascripts/jquery.min.js +4 -0
  91. data/guides/assets/javascripts/responsive-tables.js +43 -0
  92. data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  93. data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  94. data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  95. data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  96. data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  97. data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  98. data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  99. data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  100. data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  101. data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  102. data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  103. data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  104. data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  105. data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  106. data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  107. data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  108. data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  109. data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  110. data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  111. data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  112. data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  113. data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  114. data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  115. data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  116. data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  117. data/guides/assets/javascripts/syntaxhighlighter/shCore.js +17 -0
  118. data/guides/assets/stylesheets/fixes.css +16 -0
  119. data/guides/assets/stylesheets/kindle.css +11 -0
  120. data/guides/assets/stylesheets/main.css +709 -0
  121. data/guides/assets/stylesheets/print.css +52 -0
  122. data/guides/assets/stylesheets/reset.css +43 -0
  123. data/guides/assets/stylesheets/responsive-tables.css +50 -0
  124. data/guides/assets/stylesheets/style.css +13 -0
  125. data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +226 -0
  126. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  127. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  128. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  129. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  130. data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  131. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  132. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  133. data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  134. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  135. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  136. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  137. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  138. data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  139. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  140. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  141. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  142. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  143. data/guides/code/getting_started/Gemfile +38 -0
  144. data/guides/code/getting_started/Gemfile.lock +150 -0
  145. data/guides/code/getting_started/README.rdoc +28 -0
  146. data/guides/code/getting_started/Rakefile +6 -0
  147. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  148. data/guides/code/getting_started/app/assets/javascripts/application.js +16 -0
  149. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
  150. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
  151. data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +3 -0
  152. data/guides/code/getting_started/app/assets/stylesheets/application.css +13 -0
  153. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
  154. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
  155. data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +3 -0
  156. data/guides/code/getting_started/app/controllers/application_controller.rb +5 -0
  157. data/guides/code/getting_started/app/controllers/comments_controller.rb +17 -0
  158. data/guides/code/getting_started/app/controllers/posts_controller.rb +47 -0
  159. data/guides/code/getting_started/app/controllers/welcome_controller.rb +4 -0
  160. data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
  161. data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
  162. data/guides/code/getting_started/app/helpers/posts_helper.rb +2 -0
  163. data/guides/code/getting_started/app/helpers/welcome_helper.rb +2 -0
  164. data/guides/code/getting_started/app/models/comment.rb +3 -0
  165. data/guides/code/getting_started/app/models/post.rb +7 -0
  166. data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
  167. data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
  168. data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
  169. data/guides/code/getting_started/app/views/posts/_form.html.erb +27 -0
  170. data/guides/code/getting_started/app/views/posts/edit.html.erb +5 -0
  171. data/guides/code/getting_started/app/views/posts/index.html.erb +21 -0
  172. data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
  173. data/guides/code/getting_started/app/views/posts/show.html.erb +18 -0
  174. data/guides/code/getting_started/app/views/welcome/index.html.erb +3 -0
  175. data/guides/code/getting_started/bin/bundle +4 -0
  176. data/guides/code/getting_started/bin/rails +4 -0
  177. data/guides/code/getting_started/bin/rake +4 -0
  178. data/guides/code/getting_started/config.ru +4 -0
  179. data/guides/code/getting_started/config/application.rb +17 -0
  180. data/guides/code/getting_started/config/boot.rb +4 -0
  181. data/guides/code/getting_started/config/database.yml +25 -0
  182. data/guides/code/getting_started/config/environment.rb +5 -0
  183. data/guides/code/getting_started/config/environments/development.rb +30 -0
  184. data/guides/code/getting_started/config/environments/production.rb +80 -0
  185. data/guides/code/getting_started/config/environments/test.rb +36 -0
  186. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
  187. data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +4 -0
  188. data/guides/code/getting_started/config/initializers/inflections.rb +16 -0
  189. data/guides/code/getting_started/config/initializers/locale.rb +9 -0
  190. data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
  191. data/guides/code/getting_started/config/initializers/secret_token.rb +12 -0
  192. data/guides/code/getting_started/config/initializers/session_store.rb +3 -0
  193. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
  194. data/guides/code/getting_started/config/locales/en.yml +23 -0
  195. data/guides/code/getting_started/config/routes.rb +7 -0
  196. data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +10 -0
  197. data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +11 -0
  198. data/guides/code/getting_started/db/schema.rb +33 -0
  199. data/guides/code/getting_started/db/seeds.rb +7 -0
  200. data/guides/code/getting_started/public/404.html +27 -0
  201. data/guides/code/getting_started/public/422.html +26 -0
  202. data/guides/code/getting_started/public/500.html +26 -0
  203. data/guides/code/getting_started/public/favicon.ico +0 -0
  204. data/guides/code/getting_started/public/robots.txt +5 -0
  205. data/guides/code/getting_started/test/controllers/comments_controller_test.rb +7 -0
  206. data/guides/code/getting_started/test/controllers/posts_controller_test.rb +7 -0
  207. data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +9 -0
  208. data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
  209. data/guides/code/getting_started/test/fixtures/posts.yml +9 -0
  210. data/guides/code/getting_started/test/helpers/comments_helper_test.rb +4 -0
  211. data/guides/code/getting_started/test/helpers/posts_helper_test.rb +4 -0
  212. data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +4 -0
  213. data/guides/code/getting_started/test/models/comment_test.rb +7 -0
  214. data/guides/code/getting_started/test/models/post_test.rb +7 -0
  215. data/guides/code/getting_started/test/test_helper.rb +15 -0
  216. data/guides/rails_guides.rb +44 -0
  217. data/guides/rails_guides/generator.rb +248 -0
  218. data/guides/rails_guides/helpers.rb +51 -0
  219. data/guides/rails_guides/indexer.rb +68 -0
  220. data/guides/rails_guides/kindle.rb +119 -0
  221. data/guides/rails_guides/levenshtein.rb +31 -0
  222. data/guides/rails_guides/markdown.rb +163 -0
  223. data/guides/rails_guides/markdown/renderer.rb +82 -0
  224. data/guides/source/2_2_release_notes.md +435 -0
  225. data/guides/source/2_3_release_notes.md +621 -0
  226. data/guides/source/3_0_release_notes.md +614 -0
  227. data/guides/source/3_1_release_notes.md +556 -0
  228. data/guides/source/3_2_release_notes.md +565 -0
  229. data/guides/source/4_0_release_notes.md +228 -0
  230. data/guides/source/_license.html.erb +2 -0
  231. data/guides/source/_welcome.html.erb +19 -0
  232. data/guides/source/action_controller_overview.md +872 -0
  233. data/guides/source/action_mailer_basics.md +599 -0
  234. data/guides/source/action_view_overview.md +1565 -0
  235. data/guides/source/active_model_basics.md +200 -0
  236. data/guides/source/active_record_basics.md +370 -0
  237. data/guides/source/active_record_callbacks.md +358 -0
  238. data/guides/source/active_record_querying.md +1621 -0
  239. data/guides/source/active_record_validations.md +1128 -0
  240. data/guides/source/active_support_core_extensions.md +3791 -0
  241. data/guides/source/active_support_instrumentation.md +487 -0
  242. data/guides/source/api_documentation_guidelines.md +209 -0
  243. data/guides/source/asset_pipeline.md +832 -0
  244. data/guides/source/association_basics.md +2129 -0
  245. data/guides/source/caching_with_rails.md +350 -0
  246. data/guides/source/command_line.md +594 -0
  247. data/guides/source/configuring.md +736 -0
  248. data/guides/source/contributing_to_ruby_on_rails.md +455 -0
  249. data/guides/source/credits.html.erb +76 -0
  250. data/guides/source/debugging_rails_applications.md +675 -0
  251. data/guides/source/development_dependencies_install.md +195 -0
  252. data/guides/source/documents.yaml +179 -0
  253. data/guides/source/engines.md +961 -0
  254. data/guides/source/form_helpers.md +955 -0
  255. data/guides/source/generators.md +644 -0
  256. data/guides/source/getting_started.md +1775 -0
  257. data/guides/source/i18n.md +983 -0
  258. data/guides/source/index.html.erb +27 -0
  259. data/guides/source/initialization.md +562 -0
  260. data/guides/source/kindle/KINDLE.md +26 -0
  261. data/guides/source/kindle/copyright.html.erb +1 -0
  262. data/guides/source/kindle/layout.html.erb +27 -0
  263. data/guides/source/kindle/rails_guides.opf.erb +52 -0
  264. data/guides/source/kindle/toc.html.erb +24 -0
  265. data/guides/source/kindle/toc.ncx.erb +64 -0
  266. data/guides/source/kindle/welcome.html.erb +5 -0
  267. data/guides/source/layout.html.erb +148 -0
  268. data/guides/source/layouts_and_rendering.md +1132 -0
  269. data/guides/source/migrations.md +1059 -0
  270. data/guides/source/nested_model_forms.md +225 -0
  271. data/guides/source/plugins.md +435 -0
  272. data/guides/source/rails_application_templates.md +229 -0
  273. data/guides/source/rails_on_rack.md +342 -0
  274. data/guides/source/routing.md +1088 -0
  275. data/guides/source/ruby_on_rails_guides_guidelines.md +124 -0
  276. data/guides/source/security.md +973 -0
  277. data/guides/source/testing.md +981 -0
  278. data/guides/source/upgrading_ruby_on_rails.md +286 -0
  279. data/guides/source/working_with_javascript_in_rails.md +396 -0
  280. data/guides/w3c_validator.rb +95 -0
  281. metadata +315 -31
@@ -0,0 +1,26 @@
1
+ # Rails Guides on the Kindle
2
+
3
+
4
+ ## Synopsis
5
+
6
+ 1. Obtain `kindlegen` from the link below and put the binary in your path
7
+ 2. Run `KINDLE=1 rake generate_guides` to generate the guides and compile the `.mobi` file
8
+ 3. Copy `output/kindle/rails_guides.mobi` to your Kindle
9
+
10
+ ## Resources
11
+
12
+ * [Stack Overflow: Kindle Periodical Format](http://stackoverflow.com/questions/5379565/kindle-periodical-format)
13
+ * Example Periodical [.ncx](https://gist.github.com/mipearson/808c971ed087b839d462) and [.opf](https://gist.github.com/mipearson/d6349aa8488eca2ee6d0)
14
+ * [Kindle Publishing Guidelines](http://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf)
15
+ * [KindleGen & Kindle Previewer](http://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000234621)
16
+
17
+ ## TODO
18
+
19
+ ### Post release
20
+
21
+ * Integrate generated Kindle document into published HTML guides
22
+ * Tweak heading styles (most docs use h3/h4/h5, which end up being smaller than the text under it)
23
+ * Tweak table styles (smaller text? Many of the tables are unusable on a Kindle in portrait mode)
24
+ * Have the HTML/XML TOC 'drill down' into the TOCs of the individual guides
25
+ * `.epub` generation.
26
+
@@ -0,0 +1 @@
1
+ <%= render 'license' %>
@@ -0,0 +1,27 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
+
8
+ <title><%= yield(:page_title) || 'Ruby on Rails Guides' %></title>
9
+
10
+ <link rel="stylesheet" type="text/css" href="stylesheets/kindle.css" />
11
+
12
+ </head>
13
+ <body class="guide">
14
+
15
+ <% if content_for? :header_section %>
16
+ <%= yield :header_section %>
17
+ <div class="pagebreak">
18
+ <% end %>
19
+
20
+ <% if content_for? :index_section %>
21
+ <%= yield :index_section %>
22
+ <div class="pagebreak">
23
+ <% end %>
24
+
25
+ <%= yield.html_safe %>
26
+ </body>
27
+ </html>
@@ -0,0 +1,52 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+
3
+ <package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="RailsGuides">
4
+ <metadata>
5
+ <meta name="cover" content="cover" />
6
+ <dc-metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
7
+
8
+ <dc:title>Ruby on Rails Guides (<%= @version %>)</dc:title>
9
+
10
+ <dc:language>en-us</dc:language>
11
+ <dc:creator>Ruby on Rails</dc:creator>
12
+ <dc:publisher>Ruby on Rails</dc:publisher>
13
+ <dc:subject>Reference</dc:subject>
14
+ <dc:date><%= Time.now.strftime('%Y-%m-%d') %></dc:date>
15
+
16
+ <dc:description>These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together.</dc:description>
17
+ </dc-metadata>
18
+ <x-metadata>
19
+ <output content-type="application/x-mobipocket-subscription-magazine" encoding="utf-8"/>
20
+ </x-metadata>
21
+ </metadata>
22
+
23
+ <manifest>
24
+ <!-- HTML content files [mandatory] -->
25
+ <% documents_flat.each do |document| %>
26
+ <item id="<%= document['url'] %>" media-type="text/html" href="<%= document['url'] %>" />
27
+ <% end %>
28
+
29
+ <% %w{toc.html credits.html welcome.html copyright.html}.each do |url| %>
30
+ <item id="<%= url %>" media-type="text/html" href="<%= url %>" />
31
+ <% end %>
32
+
33
+ <item id="toc" media-type="application/x-dtbncx+xml" href="toc.ncx" />
34
+
35
+ <item id="cover" media-type="image/jpg" href="images/rails_guides_kindle_cover.jpg"/>
36
+ </manifest>
37
+
38
+ <spine toc="toc">
39
+ <itemref idref="toc.html" />
40
+ <itemref idref="welcome.html" />
41
+ <itemref idref="credits.html" />
42
+ <itemref idref="copyright.html" />
43
+ <% documents_flat.each do |document| %>
44
+ <itemref idref="<%= document['url'] %>" />
45
+ <% end %>
46
+ </spine>
47
+
48
+ <guide>
49
+ <reference type="toc" title="Table of Contents" href="toc.html"></reference>
50
+ </guide>
51
+
52
+ </package>
@@ -0,0 +1,24 @@
1
+ <% content_for :page_title do %>
2
+ Ruby on Rails Guides
3
+ <% end %>
4
+
5
+ <h1>Table of Contents</h1>
6
+ <div id="toc">
7
+ <ul><li><a href="welcome.html">Welcome</a></li></ul>
8
+ <% documents_by_section.each_with_index do |section, i| %>
9
+ <h3><%= "#{i + 1}." %> <%= section['name'] %></h3>
10
+ <ul>
11
+ <% section['documents'].each do |document| %>
12
+ <li>
13
+ <a href="<%= document['url'] %>"><%= document['name'] %></a>
14
+ <% if document['work_in_progress']%>(WIP)<% end %>
15
+ </li>
16
+ <% end %>
17
+ </ul>
18
+ <% end %>
19
+ <hr />
20
+ <ul>
21
+ <li><a href="credits.html">Credits</a></li>
22
+ <li><a href="copyright.html">Copyright &amp; License</a></li>
23
+ <ul>
24
+ </div>
@@ -0,0 +1,64 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
3
+ "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
4
+
5
+ <ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1" xml:lang="en-US">
6
+ <head>
7
+ <meta name="dtb:uid" content="RailsGuides"/>
8
+ <meta name="dtb:depth" content="2"/>
9
+ <meta name="dtb:totalPageCount" content="0"/>
10
+ <meta name="dtb:maxPageNumber" content="0"/>
11
+ </head>
12
+ <docTitle><text>Ruby on Rails Guides</text></docTitle>
13
+ <docAuthor><text>docrails</text></docAuthor>
14
+ <navMap>
15
+ <navPoint playOrder="0" class="periodical" id="periodical">
16
+ <navLabel>
17
+ <text>Table of Contents</text>
18
+ </navLabel>
19
+ <content src="toc.html"/>
20
+
21
+ <navPoint class="section" id="welcome" playOrder="1">
22
+ <navLabel>
23
+ <text>Introduction</text>
24
+ </navLabel>
25
+ <content src="welcome.html"/>
26
+
27
+ <navPoint class="article" id="welcome" playOrder="2">
28
+ <navLabel>
29
+ <text>Welcome</text>
30
+ </navLabel>
31
+ <content src="welcome.html"/>
32
+ </navPoint>
33
+ <navPoint class="article" id="credits" playOrder="3">
34
+ <navLabel><text>Credits</text></navLabel>
35
+ <content src="credits.html">
36
+ </navPoint>
37
+ <navPoint class="article" id="copyright" playOrder="4">
38
+ <navLabel><text>Copyright &amp; License</text></navLabel>
39
+ <content src="copyright.html">
40
+ </navPoint>
41
+ </navPoint>
42
+
43
+ <% play_order = 4 %>
44
+ <% documents_by_section.each_with_index do |section, section_no| %>
45
+ <navPoint class="section" id="chapter_<%= section_no + 1 %>" playOrder="<% play_order +=1 %>">
46
+ <navLabel>
47
+ <text><%= section['name'] %></text>
48
+ </navLabel>
49
+ <content src="<%=section['documents'].first['url'] %>"/>
50
+
51
+ <% section['documents'].each_with_index do |document, document_no| %>
52
+ <navPoint class="article" id="_<%=section_no+1%>.<%=document_no+1%>" playOrder="<%=play_order +=1 %>">
53
+ <navLabel>
54
+ <text><%= document['name'] %></text>
55
+ </navLabel>
56
+ <content src="<%=document['url'] %>"/>
57
+ </navPoint>
58
+ <% end %>
59
+ </navPoint>
60
+ <% end %>
61
+
62
+ </navPoint>
63
+ </navMap>
64
+ </ncx>
@@ -0,0 +1,5 @@
1
+ <%= render 'welcome' %>
2
+
3
+ <h3>Kindle Edition</h3>
4
+
5
+ The Kindle Edition of the Rails Guides should be considered a work in progress. Feedback is really welcome. Please see the "Feedback" section at the end of each guide for instructions.
@@ -0,0 +1,148 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
+ <meta name="viewport" content="width=device-width, initial-scale=1">
8
+
9
+ <title><%= yield(:page_title) || 'Ruby on Rails Guides' %></title>
10
+ <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
+ <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
+
13
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
+
16
+ <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
+
18
+ <link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
19
+ </head>
20
+ <body class="guide">
21
+ <% if @edge %>
22
+ <div>
23
+ <img src="images/edge_badge.png" alt="edge-badge" id="edge-badge" />
24
+ </div>
25
+ <% end %>
26
+ <div id="topNav">
27
+ <div class="wrapper">
28
+ <strong class="more-info-label">More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
29
+ <span class="red-button more-info-button">
30
+ More Ruby on Rails
31
+ </span>
32
+ <ul class="more-info-links s-hidden">
33
+ <li class="more-info"><a href="http://rubyonrails.org/">Overview</a></li>
34
+ <li class="more-info"><a href="http://rubyonrails.org/download">Download</a></li>
35
+ <li class="more-info"><a href="http://rubyonrails.org/deploy">Deploy</a></li>
36
+ <li class="more-info"><a href="https://github.com/rails/rails">Code</a></li>
37
+ <li class="more-info"><a href="http://rubyonrails.org/screencasts">Screencasts</a></li>
38
+ <li class="more-info"><a href="http://rubyonrails.org/documentation">Documentation</a></li>
39
+ <li class="more-info"><a href="http://rubyonrails.org/ecosystem">Ecosystem</a></li>
40
+ <li class="more-info"><a href="http://rubyonrails.org/community">Community</a></li>
41
+ <li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
42
+ </ul>
43
+ </div>
44
+ </div>
45
+ <div id="header">
46
+ <div class="wrapper clearfix">
47
+ <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
48
+ <ul class="nav">
49
+ <li><a class="nav-item" href="index.html">Home</a></li>
50
+ <li class="guides-index guides-index-large">
51
+ <a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu" class="guides-index-item nav-item">Guides Index</a>
52
+ <div id="guides" class="clearfix" style="display: none;">
53
+ <hr />
54
+ <% ['L', 'R'].each do |position| %>
55
+ <dl class="<%= position %>">
56
+ <% docs_for_menu(position).each do |section| %>
57
+ <dt><%= section['name'] %></dt>
58
+ <% finished_documents(section['documents']).each do |document| %>
59
+ <dd><a href="<%= document['url'] %>"><%= document['name'] %></a></dd>
60
+ <% end %>
61
+ <% end %>
62
+ </dl>
63
+ <% end %>
64
+ </div>
65
+ </li>
66
+ <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribute</a></li>
67
+ <li><a class="nav-item" href="credits.html">Credits</a></li>
68
+ <li class="guides-index guides-index-small">
69
+ <select class="guides-index-item nav-item">
70
+ <option value="index.html">Guides Index</option>
71
+ <% docs_for_menu.each do |section| %>
72
+ <optgroup label="<%= section['name'] %>">
73
+ <% finished_documents(section['documents']).each do |document| %>
74
+ <option value="<%= document['url'] %>"><%= document['name'] %></option>
75
+ <% end %>
76
+ </optgroup>
77
+ <% end %>
78
+ </select>
79
+ </li>
80
+ </ul>
81
+ </div>
82
+ </div>
83
+ </div>
84
+ <hr class="hide" />
85
+
86
+ <div id="feature">
87
+ <div class="wrapper">
88
+ <%= yield :header_section %>
89
+
90
+ <%= yield :index_section %>
91
+ </div>
92
+ </div>
93
+
94
+ <div id="container">
95
+ <div class="wrapper">
96
+ <div id="mainCol">
97
+ <%= yield.html_safe %>
98
+
99
+ <h3>Feedback</h3>
100
+ <p>
101
+ You're encouraged to help improve the quality of this guide.
102
+ </p>
103
+ <p>
104
+ If you see any typos or factual errors you are confident to
105
+ patch, please clone <%= link_to 'docrails', 'https://github.com/lifo/docrails' %>
106
+ and push the change yourself. That branch of Rails has public write access.
107
+ Commits are still reviewed, but that happens after you've submitted your
108
+ contribution. <%= link_to 'docrails', 'https://github.com/lifo/docrails' %> is
109
+ cross-merged with master periodically.
110
+ </p>
111
+ <p>
112
+ You may also find incomplete content, or stuff that is not up to date.
113
+ Please do add any missing documentation for master. Check the
114
+ <%= link_to 'Ruby on Rails Guides Guidelines', 'ruby_on_rails_guides_guidelines.html' %>
115
+ for style and conventions.
116
+ </p>
117
+ <p>
118
+ If for whatever reason you spot something to fix but cannot patch it yourself, please
119
+ <%= link_to 'open an issue', 'https://github.com/rails/rails/issues' %>.
120
+ </p>
121
+ <p>And last but not least, any kind of discussion regarding Ruby on Rails
122
+ documentation is very welcome in the <%= link_to 'rubyonrails-docs mailing list', 'http://groups.google.com/group/rubyonrails-docs' %>.
123
+ </p>
124
+ </div>
125
+ </div>
126
+ </div>
127
+
128
+ <hr class="hide" />
129
+ <div id="footer">
130
+ <div class="wrapper">
131
+ <%= render 'license' %>
132
+ </div>
133
+ </div>
134
+
135
+ <script type="text/javascript" src="javascripts/jquery.min.js"></script>
136
+ <script type="text/javascript" src="javascripts/responsive-tables.js"></script>
137
+ <script type="text/javascript" src="javascripts/guides.js"></script>
138
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
139
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
140
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
141
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
142
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
143
+ <script type="text/javascript">
144
+ SyntaxHighlighter.all()
145
+ $(guidesIndex.bind);
146
+ </script>
147
+ </body>
148
+ </html>
@@ -0,0 +1,1132 @@
1
+ Layouts and Rendering in Rails
2
+ ==============================
3
+
4
+ This guide covers the basic layout features of Action Controller and Action View. By referring to this guide, you will be able to:
5
+
6
+ After reading this guide, you will know:
7
+
8
+ * How to use the various rendering methods built into Rails.
9
+ * How to create layouts with multiple content sections.
10
+ * How to use partials to DRY up your views.
11
+ * How to use nested layouts (sub-templates).
12
+
13
+ --------------------------------------------------------------------------------
14
+
15
+ Overview: How the Pieces Fit Together
16
+ -------------------------------------
17
+
18
+ This guide focuses on the interaction between Controller and View in the Model-View-Controller triangle. As you know, the Controller is responsible for orchestrating the whole process of handling a request in Rails, though it normally hands off any heavy code to the Model. But then, when it's time to send a response back to the user, the Controller hands things off to the View. It's that handoff that is the subject of this guide.
19
+
20
+ In broad strokes, this involves deciding what should be sent as the response and calling an appropriate method to create that response. If the response is a full-blown view, Rails also does some extra work to wrap the view in a layout and possibly to pull in partial views. You'll see all of those paths later in this guide.
21
+
22
+ Creating Responses
23
+ ------------------
24
+
25
+ From the controller's point of view, there are three ways to create an HTTP response:
26
+
27
+ * Call `render` to create a full response to send back to the browser
28
+ * Call `redirect_to` to send an HTTP redirect status code to the browser
29
+ * Call `head` to create a response consisting solely of HTTP headers to send back to the browser
30
+
31
+ ### Rendering by Default: Convention Over Configuration in Action
32
+
33
+ You've heard that Rails promotes "convention over configuration". Default rendering is an excellent example of this. By default, controllers in Rails automatically render views with names that correspond to valid routes. For example, if you have this code in your `BooksController` class:
34
+
35
+ ```ruby
36
+ class BooksController < ApplicationController
37
+ end
38
+ ```
39
+
40
+ And the following in your routes file:
41
+
42
+ ```ruby
43
+ resources :books
44
+ ```
45
+
46
+ And you have a view file `app/views/books/index.html.erb`:
47
+
48
+ ```html+erb
49
+ <h1>Books are coming soon!</h1>
50
+ ```
51
+
52
+ Rails will automatically render `app/views/books/index.html.erb` when you navigate to `/books` and you will see "Books are coming soon!" on your screen.
53
+
54
+ However a coming soon screen is only minimally useful, so you will soon create your `Book` model and add the index action to `BooksController`:
55
+
56
+ ```ruby
57
+ class BooksController < ApplicationController
58
+ def index
59
+ @books = Book.all
60
+ end
61
+ end
62
+ ```
63
+
64
+ Note that we don't have explicit render at the end of the index action in accordance with "convention over configuration" principle. The rule is that if you do not explicitly render something at the end of a controller action, Rails will automatically look for the `action_name.html.erb` template in the controller's view path and render it. So in this case, Rails will render the `app/views/books/index.html.erb` file.
65
+
66
+ If we want to display the properties of all the books in our view, we can do so with an ERB template like this:
67
+
68
+ ```html+erb
69
+ <h1>Listing Books</h1>
70
+
71
+ <table>
72
+ <tr>
73
+ <th>Title</th>
74
+ <th>Summary</th>
75
+ <th></th>
76
+ <th></th>
77
+ <th></th>
78
+ </tr>
79
+
80
+ <% @books.each do |book| %>
81
+ <tr>
82
+ <td><%= book.title %></td>
83
+ <td><%= book.content %></td>
84
+ <td><%= link_to "Show", book %></td>
85
+ <td><%= link_to "Edit", edit_book_path(book) %></td>
86
+ <td><%= link_to "Remove", book, method: :delete, data: { confirm: "Are you sure?" } %></td>
87
+ </tr>
88
+ <% end %>
89
+ </table>
90
+
91
+ <br />
92
+
93
+ <%= link_to "New book", new_book_path %>
94
+ ```
95
+
96
+ NOTE: The actual rendering is done by subclasses of `ActionView::TemplateHandlers`. This guide does not dig into that process, but it's important to know that the file extension on your view controls the choice of template handler. Beginning with Rails 2, the standard extensions are `.erb` for ERB (HTML with embedded Ruby), and `.builder` for Builder (XML generator).
97
+
98
+ ### Using `render`
99
+
100
+ In most cases, the `ActionController::Base#render` method does the heavy lifting of rendering your application's content for use by a browser. There are a variety of ways to customize the behavior of `render`. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.
101
+
102
+ TIP: If you want to see the exact results of a call to `render` without needing to inspect it in a browser, you can call `render_to_string`. This method takes exactly the same options as `render`, but it returns a string instead of sending a response back to the browser.
103
+
104
+ #### Rendering Nothing
105
+
106
+ Perhaps the simplest thing you can do with `render` is to render nothing at all:
107
+
108
+ ```ruby
109
+ render nothing: true
110
+ ```
111
+
112
+ If you look at the response for this using cURL, you will see the following:
113
+
114
+ ```bash
115
+ $ curl -i 127.0.0.1:3000/books
116
+ HTTP/1.1 200 OK
117
+ Connection: close
118
+ Date: Sun, 24 Jan 2010 09:25:18 GMT
119
+ Transfer-Encoding: chunked
120
+ Content-Type: */*; charset=utf-8
121
+ X-Runtime: 0.014297
122
+ Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
123
+ Cache-Control: no-cache
124
+
125
+
126
+ $
127
+ ```
128
+
129
+ We see there is an empty response (no data after the `Cache-Control` line), but the request was successful because Rails has set the response to 200 OK. You can set the `:status` option on render to change this response. Rendering nothing can be useful for Ajax requests where all you want to send back to the browser is an acknowledgment that the request was completed.
130
+
131
+ TIP: You should probably be using the `head` method, discussed later in this guide, instead of `render :nothing`. This provides additional flexibility and makes it explicit that you're only generating HTTP headers.
132
+
133
+ #### Rendering an Action's View
134
+
135
+ If you want to render the view that corresponds to a different template within the same controller, you can use `render` with the name of the view:
136
+
137
+ ```ruby
138
+ def update
139
+ @book = Book.find(params[:id])
140
+ if @book.update(params[:book])
141
+ redirect_to(@book)
142
+ else
143
+ render "edit"
144
+ end
145
+ end
146
+ ```
147
+
148
+ If the call to `update` fails, calling the `update` action in this controller will render the `edit.html.erb` template belonging to the same controller.
149
+
150
+ If you prefer, you can use a symbol instead of a string to specify the action to render:
151
+
152
+ ```ruby
153
+ def update
154
+ @book = Book.find(params[:id])
155
+ if @book.update(params[:book])
156
+ redirect_to(@book)
157
+ else
158
+ render :edit
159
+ end
160
+ end
161
+ ```
162
+
163
+ #### Rendering an Action's Template from Another Controller
164
+
165
+ What if you want to render a template from an entirely different controller from the one that contains the action code? You can also do that with `render`, which accepts the full path (relative to `app/views`) of the template to render. For example, if you're running code in an `AdminProductsController` that lives in `app/controllers/admin`, you can render the results of an action to a template in `app/views/products` this way:
166
+
167
+ ```ruby
168
+ render "products/show"
169
+ ```
170
+
171
+ Rails knows that this view belongs to a different controller because of the embedded slash character in the string. If you want to be explicit, you can use the `:template` option (which was required on Rails 2.2 and earlier):
172
+
173
+ ```ruby
174
+ render template: "products/show"
175
+ ```
176
+
177
+ #### Rendering an Arbitrary File
178
+
179
+ The `render` method can also use a view that's entirely outside of your application (perhaps you're sharing views between two Rails applications):
180
+
181
+ ```ruby
182
+ render "/u/apps/warehouse_app/current/app/views/products/show"
183
+ ```
184
+
185
+ Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the `:file` option (which was required on Rails 2.2 and earlier):
186
+
187
+ ```ruby
188
+ render file: "/u/apps/warehouse_app/current/app/views/products/show"
189
+ ```
190
+
191
+ The `:file` option takes an absolute file-system path. Of course, you need to have rights to the view that you're using to render the content.
192
+
193
+ NOTE: By default, the file is rendered without using the current layout. If you want Rails to put the file into the current layout, you need to add the `layout: true` option.
194
+
195
+ TIP: If you're running Rails on Microsoft Windows, you should use the `:file` option to render a file, because Windows filenames do not have the same format as Unix filenames.
196
+
197
+ #### Wrapping it up
198
+
199
+ The above three ways of rendering (rendering another template within the controller, rendering a template within another controller and rendering an arbitrary file on the file system) are actually variants of the same action.
200
+
201
+ In fact, in the BooksController class, inside of the update action where we want to render the edit template if the book does not update successfully, all of the following render calls would all render the `edit.html.erb` template in the `views/books` directory:
202
+
203
+ ```ruby
204
+ render :edit
205
+ render action: :edit
206
+ render "edit"
207
+ render "edit.html.erb"
208
+ render action: "edit"
209
+ render action: "edit.html.erb"
210
+ render "books/edit"
211
+ render "books/edit.html.erb"
212
+ render template: "books/edit"
213
+ render template: "books/edit.html.erb"
214
+ render "/path/to/rails/app/views/books/edit"
215
+ render "/path/to/rails/app/views/books/edit.html.erb"
216
+ render file: "/path/to/rails/app/views/books/edit"
217
+ render file: "/path/to/rails/app/views/books/edit.html.erb"
218
+ ```
219
+
220
+ Which one you use is really a matter of style and convention, but the rule of thumb is to use the simplest one that makes sense for the code you are writing.
221
+
222
+ #### Using `render` with `:inline`
223
+
224
+ The `render` method can do without a view completely, if you're willing to use the `:inline` option to supply ERB as part of the method call. This is perfectly valid:
225
+
226
+ ```ruby
227
+ render inline: "<% products.each do |p| %><p><%= p.name %></p><% end %>"
228
+ ```
229
+
230
+ WARNING: There is seldom any good reason to use this option. Mixing ERB into your controllers defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. Use a separate erb view instead.
231
+
232
+ By default, inline rendering uses ERB. You can force it to use Builder instead with the `:type` option:
233
+
234
+ ```ruby
235
+ render inline: "xml.p {'Horrid coding practice!'}", type: :builder
236
+ ```
237
+
238
+ #### Rendering Text
239
+
240
+ You can send plain text - with no markup at all - back to the browser by using the `:text` option to `render`:
241
+
242
+ ```ruby
243
+ render text: "OK"
244
+ ```
245
+
246
+ TIP: Rendering pure text is most useful when you're responding to Ajax or web service requests that are expecting something other than proper HTML.
247
+
248
+ NOTE: By default, if you use the `:text` option, the text is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the `layout: true` option.
249
+
250
+ #### Rendering JSON
251
+
252
+ JSON is a JavaScript data format used by many Ajax libraries. Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser:
253
+
254
+ ```ruby
255
+ render json: @product
256
+ ```
257
+
258
+ TIP: You don't need to call `to_json` on the object that you want to render. If you use the `:json` option, `render` will automatically call `to_json` for you.
259
+
260
+ #### Rendering XML
261
+
262
+ Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:
263
+
264
+ ```ruby
265
+ render xml: @product
266
+ ```
267
+
268
+ TIP: You don't need to call `to_xml` on the object that you want to render. If you use the `:xml` option, `render` will automatically call `to_xml` for you.
269
+
270
+ #### Rendering Vanilla JavaScript
271
+
272
+ Rails can render vanilla JavaScript:
273
+
274
+ ```ruby
275
+ render js: "alert('Hello Rails');"
276
+ ```
277
+
278
+ This will send the supplied string to the browser with a MIME type of `text/javascript`.
279
+
280
+ #### Options for `render`
281
+
282
+ Calls to the `render` method generally accept four options:
283
+
284
+ * `:content_type`
285
+ * `:layout`
286
+ * `:status`
287
+ * `:location`
288
+
289
+ ##### The `:content_type` Option
290
+
291
+ By default, Rails will serve the results of a rendering operation with the MIME content-type of `text/html` (or `application/json` if you use the `:json` option, or `application/xml` for the `:xml` option.). There are times when you might like to change this, and you can do so by setting the `:content_type` option:
292
+
293
+ ```ruby
294
+ render file: filename, content_type: "application/rss"
295
+ ```
296
+
297
+ ##### The `:layout` Option
298
+
299
+ With most of the options to `render`, the rendered content is displayed as part of the current layout. You'll learn more about layouts and how to use them later in this guide.
300
+
301
+ You can use the `:layout` option to tell Rails to use a specific file as the layout for the current action:
302
+
303
+ ```ruby
304
+ render layout: "special_layout"
305
+ ```
306
+
307
+ You can also tell Rails to render with no layout at all:
308
+
309
+ ```ruby
310
+ render layout: false
311
+ ```
312
+
313
+ ##### The `:status` Option
314
+
315
+ Rails will automatically generate a response with the correct HTTP status code (in most cases, this is `200 OK`). You can use the `:status` option to change this:
316
+
317
+ ```ruby
318
+ render status: 500
319
+ render status: :forbidden
320
+ ```
321
+
322
+ Rails understands both numeric and symbolic status codes.
323
+
324
+ ##### The `:location` Option
325
+
326
+ You can use the `:location` option to set the HTTP `Location` header:
327
+
328
+ ```ruby
329
+ render xml: photo, location: photo_url(photo)
330
+ ```
331
+
332
+ #### Finding Layouts
333
+
334
+ To find the current layout, Rails first looks for a file in `app/views/layouts` with the same base name as the controller. For example, rendering actions from the `PhotosController` class will use `app/views/layouts/photos.html.erb` (or `app/views/layouts/photos.builder`). If there is no such controller-specific layout, Rails will use `app/views/layouts/application.html.erb` or `app/views/layouts/application.builder`. If there is no `.erb` layout, Rails will use a `.builder` layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.
335
+
336
+ ##### Specifying Layouts for Controllers
337
+
338
+ You can override the default layout conventions in your controllers by using the `layout` declaration. For example:
339
+
340
+ ```ruby
341
+ class ProductsController < ApplicationController
342
+ layout "inventory"
343
+ #...
344
+ end
345
+ ```
346
+
347
+ With this declaration, all of the views rendered by the products controller will use `app/views/layouts/inventory.html.erb` as their layout.
348
+
349
+ To assign a specific layout for the entire application, use a `layout` declaration in your `ApplicationController` class:
350
+
351
+ ```ruby
352
+ class ApplicationController < ActionController::Base
353
+ layout "main"
354
+ #...
355
+ end
356
+ ```
357
+
358
+ With this declaration, all of the views in the entire application will use `app/views/layouts/main.html.erb` for their layout.
359
+
360
+ ##### Choosing Layouts at Runtime
361
+
362
+ You can use a symbol to defer the choice of layout until a request is processed:
363
+
364
+ ```ruby
365
+ class ProductsController < ApplicationController
366
+ layout "products_layout"
367
+
368
+ def show
369
+ @product = Product.find(params[:id])
370
+ end
371
+
372
+ private
373
+ def products_layout
374
+ @current_user.special? ? "special" : "products"
375
+ end
376
+
377
+ end
378
+ ```
379
+
380
+ Now, if the current user is a special user, they'll get a special layout when viewing a product.
381
+
382
+ You can even use an inline method, such as a Proc, to determine the layout. For example, if you pass a Proc object, the block you give the Proc will be given the `controller` instance, so the layout can be determined based on the current request:
383
+
384
+ ```ruby
385
+ class ProductsController < ApplicationController
386
+ layout Proc.new { |controller| controller.request.xhr? ? "popup" : "application" }
387
+ end
388
+ ```
389
+
390
+ ##### Conditional Layouts
391
+
392
+ Layouts specified at the controller level support the `:only` and `:except` options. These options take either a method name, or an array of method names, corresponding to method names within the controller:
393
+
394
+ ```ruby
395
+ class ProductsController < ApplicationController
396
+ layout "product", except: [:index, :rss]
397
+ end
398
+ ```
399
+
400
+ With this declaration, the `product` layout would be used for everything but the `rss` and `index` methods.
401
+
402
+ ##### Layout Inheritance
403
+
404
+ Layout declarations cascade downward in the hierarchy, and more specific layout declarations always override more general ones. For example:
405
+
406
+ * `application_controller.rb`
407
+
408
+ ```ruby
409
+ class ApplicationController < ActionController::Base
410
+ layout "main"
411
+ end
412
+ ```
413
+
414
+ * `posts_controller.rb`
415
+
416
+ ```ruby
417
+ class PostsController < ApplicationController
418
+ end
419
+ ```
420
+
421
+ * `special_posts_controller.rb`
422
+
423
+ ```ruby
424
+ class SpecialPostsController < PostsController
425
+ layout "special"
426
+ end
427
+ ```
428
+
429
+ * `old_posts_controller.rb`
430
+
431
+ ```ruby
432
+ class OldPostsController < SpecialPostsController
433
+ layout false
434
+
435
+ def show
436
+ @post = Post.find(params[:id])
437
+ end
438
+
439
+ def index
440
+ @old_posts = Post.older
441
+ render layout: "old"
442
+ end
443
+ # ...
444
+ end
445
+ ```
446
+
447
+ In this application:
448
+
449
+ * In general, views will be rendered in the `main` layout
450
+ * `PostsController#index` will use the `main` layout
451
+ * `SpecialPostsController#index` will use the `special` layout
452
+ * `OldPostsController#show` will use no layout at all
453
+ * `OldPostsController#index` will use the `old` layout
454
+
455
+ #### Avoiding Double Render Errors
456
+
457
+ Sooner or later, most Rails developers will see the error message "Can only render or redirect once per action". While this is annoying, it's relatively easy to fix. Usually it happens because of a fundamental misunderstanding of the way that `render` works.
458
+
459
+ For example, here's some code that will trigger this error:
460
+
461
+ ```ruby
462
+ def show
463
+ @book = Book.find(params[:id])
464
+ if @book.special?
465
+ render action: "special_show"
466
+ end
467
+ render action: "regular_show"
468
+ end
469
+ ```
470
+
471
+ If `@book.special?` evaluates to `true`, Rails will start the rendering process to dump the `@book` variable into the `special_show` view. But this will _not_ stop the rest of the code in the `show` action from running, and when Rails hits the end of the action, it will start to render the `regular_show` view - and throw an error. The solution is simple: make sure that you have only one call to `render` or `redirect` in a single code path. One thing that can help is `and return`. Here's a patched version of the method:
472
+
473
+ ```ruby
474
+ def show
475
+ @book = Book.find(params[:id])
476
+ if @book.special?
477
+ render action: "special_show" and return
478
+ end
479
+ render action: "regular_show"
480
+ end
481
+ ```
482
+
483
+ Make sure to use `and return` instead of `&& return` because `&& return` will not work due to the operator precedence in the Ruby Language.
484
+
485
+ Note that the implicit render done by ActionController detects if `render` has been called, so the following will work without errors:
486
+
487
+ ```ruby
488
+ def show
489
+ @book = Book.find(params[:id])
490
+ if @book.special?
491
+ render action: "special_show"
492
+ end
493
+ end
494
+ ```
495
+
496
+ This will render a book with `special?` set with the `special_show` template, while other books will render with the default `show` template.
497
+
498
+ ### Using `redirect_to`
499
+
500
+ Another way to handle returning responses to an HTTP request is with `redirect_to`. As you've seen, `render` tells Rails which view (or other asset) to use in constructing a response. The `redirect_to` method does something completely different: it tells the browser to send a new request for a different URL. For example, you could redirect from wherever you are in your code to the index of photos in your application with this call:
501
+
502
+ ```ruby
503
+ redirect_to photos_url
504
+ ```
505
+
506
+ You can use `redirect_to` with any arguments that you could use with `link_to` or `url_for`. There's also a special redirect that sends the user back to the page they just came from:
507
+
508
+ ```ruby
509
+ redirect_to :back
510
+ ```
511
+
512
+ #### Getting a Different Redirect Status Code
513
+
514
+ Rails uses HTTP status code 302, a temporary redirect, when you call `redirect_to`. If you'd like to use a different status code, perhaps 301, a permanent redirect, you can use the `:status` option:
515
+
516
+ ```ruby
517
+ redirect_to photos_path, status: 301
518
+ ```
519
+
520
+ Just like the `:status` option for `render`, `:status` for `redirect_to` accepts both numeric and symbolic header designations.
521
+
522
+ #### The Difference Between `render` and `redirect_to`
523
+
524
+ Sometimes inexperienced developers think of `redirect_to` as a sort of `goto` command, moving execution from one place to another in your Rails code. This is _not_ correct. Your code stops running and waits for a new request for the browser. It just happens that you've told the browser what request it should make next, by sending back an HTTP 302 status code.
525
+
526
+ Consider these actions to see the difference:
527
+
528
+ ```ruby
529
+ def index
530
+ @books = Book.all
531
+ end
532
+
533
+ def show
534
+ @book = Book.find_by_id(params[:id])
535
+ if @book.nil?
536
+ render action: "index"
537
+ end
538
+ end
539
+ ```
540
+
541
+ With the code in this form, there will likely be a problem if the `@book` variable is `nil`. Remember, a `render :action` doesn't run any code in the target action, so nothing will set up the `@books` variable that the `index` view will probably require. One way to fix this is to redirect instead of rendering:
542
+
543
+ ```ruby
544
+ def index
545
+ @books = Book.all
546
+ end
547
+
548
+ def show
549
+ @book = Book.find_by_id(params[:id])
550
+ if @book.nil?
551
+ redirect_to action: :index
552
+ end
553
+ end
554
+ ```
555
+
556
+ With this code, the browser will make a fresh request for the index page, the code in the `index` method will run, and all will be well.
557
+
558
+ The only downside to this code is that it requires a round trip to the browser: the browser requested the show action with `/books/1` and the controller finds that there are no books, so the controller sends out a 302 redirect response to the browser telling it to go to `/books/`, the browser complies and sends a new request back to the controller asking now for the `index` action, the controller then gets all the books in the database and renders the index template, sending it back down to the browser which then shows it on your screen.
559
+
560
+ While in a small application, this added latency might not be a problem, it is something to think about if response time is a concern. We can demonstrate one way to handle this with a contrived example:
561
+
562
+ ```ruby
563
+ def index
564
+ @books = Book.all
565
+ end
566
+
567
+ def show
568
+ @book = Book.find_by_id(params[:id])
569
+ if @book.nil?
570
+ @books = Book.all
571
+ render "index", alert: "Your book was not found!"
572
+ end
573
+ end
574
+ ```
575
+
576
+ This would detect that there are no books with the specified ID, populate the `@books` instance variable with all the books in the model, and then directly render the `index.html.erb` template, returning it to the browser with a flash alert message to tell the user what happened.
577
+
578
+ ### Using `head` To Build Header-Only Responses
579
+
580
+ The `head` method can be used to send responses with only headers to the browser. It provides a more obvious alternative to calling `render :nothing`. The `head` method takes one parameter, which is interpreted as a hash of header names and values. For example, you can return only an error header:
581
+
582
+ ```ruby
583
+ head :bad_request
584
+ ```
585
+
586
+ This would produce the following header:
587
+
588
+ ```
589
+ HTTP/1.1 400 Bad Request
590
+ Connection: close
591
+ Date: Sun, 24 Jan 2010 12:15:53 GMT
592
+ Transfer-Encoding: chunked
593
+ Content-Type: text/html; charset=utf-8
594
+ X-Runtime: 0.013483
595
+ Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
596
+ Cache-Control: no-cache
597
+ ```
598
+
599
+ Or you can use other HTTP headers to convey other information:
600
+
601
+ ```ruby
602
+ head :created, location: photo_path(@photo)
603
+ ```
604
+
605
+ Which would produce:
606
+
607
+ ```
608
+ HTTP/1.1 201 Created
609
+ Connection: close
610
+ Date: Sun, 24 Jan 2010 12:16:44 GMT
611
+ Transfer-Encoding: chunked
612
+ Location: /photos/1
613
+ Content-Type: text/html; charset=utf-8
614
+ X-Runtime: 0.083496
615
+ Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
616
+ Cache-Control: no-cache
617
+ ```
618
+
619
+ Structuring Layouts
620
+ -------------------
621
+
622
+ When Rails renders a view as a response, it does so by combining the view with the current layout, using the rules for finding the current layout that were covered earlier in this guide. Within a layout, you have access to three tools for combining different bits of output to form the overall response:
623
+
624
+ * Asset tags
625
+ * `yield` and `content_for`
626
+ * Partials
627
+
628
+ ### Asset Tag Helpers
629
+
630
+ Asset tag helpers provide methods for generating HTML that link views to feeds, JavaScript, stylesheets, images, videos and audios. There are six asset tag helpers available in Rails:
631
+
632
+ * `auto_discovery_link_tag`
633
+ * `javascript_include_tag`
634
+ * `stylesheet_link_tag`
635
+ * `image_tag`
636
+ * `video_tag`
637
+ * `audio_tag`
638
+
639
+ You can use these tags in layouts or other views, although the `auto_discovery_link_tag`, `javascript_include_tag`, and `stylesheet_link_tag`, are most commonly used in the `<head>` section of a layout.
640
+
641
+ WARNING: The asset tag helpers do _not_ verify the existence of the assets at the specified locations; they simply assume that you know what you're doing and generate the link.
642
+
643
+ #### Linking to Feeds with the `auto_discovery_link_tag`
644
+
645
+ The `auto_discovery_link_tag` helper builds HTML that most browsers and newsreaders can use to detect the presence of RSS or Atom feeds. It takes the type of the link (`:rss` or `:atom`), a hash of options that are passed through to url_for, and a hash of options for the tag:
646
+
647
+ ```erb
648
+ <%= auto_discovery_link_tag(:rss, {action: "feed"},
649
+ {title: "RSS Feed"}) %>
650
+ ```
651
+
652
+ There are three tag options available for the `auto_discovery_link_tag`:
653
+
654
+ * `:rel` specifies the `rel` value in the link. The default value is "alternate".
655
+ * `:type` specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically.
656
+ * `:title` specifies the title of the link. The default value is the uppercased `:type` value, for example, "ATOM" or "RSS".
657
+
658
+ #### Linking to JavaScript Files with the `javascript_include_tag`
659
+
660
+ The `javascript_include_tag` helper returns an HTML `script` tag for each source provided.
661
+
662
+ If you are using Rails with the [Asset Pipeline](asset_pipeline.html) enabled, this helper will generate a link to `/assets/javascripts/` rather than `public/javascripts` which was used in earlier versions of Rails. This link is then served by the asset pipeline.
663
+
664
+ A JavaScript file within a Rails application or Rails engine goes in one of three locations: `app/assets`, `lib/assets` or `vendor/assets`. These locations are explained in detail in the [Asset Organization section in the Asset Pipeline Guide](asset_pipeline.html#asset-organization)
665
+
666
+ You can specify a full path relative to the document root, or a URL, if you prefer. For example, to link to a JavaScript file that is inside a directory called `javascripts` inside of one of `app/assets`, `lib/assets` or `vendor/assets`, you would do this:
667
+
668
+ ```erb
669
+ <%= javascript_include_tag "main" %>
670
+ ```
671
+
672
+ Rails will then output a `script` tag such as this:
673
+
674
+ ```html
675
+ <script src='/assets/main.js'></script>
676
+ ```
677
+
678
+ The request to this asset is then served by the Sprockets gem.
679
+
680
+ To include multiple files such as `app/assets/javascripts/main.js` and `app/assets/javascripts/columns.js` at the same time:
681
+
682
+ ```erb
683
+ <%= javascript_include_tag "main", "columns" %>
684
+ ```
685
+
686
+ To include `app/assets/javascripts/main.js` and `app/assets/javascripts/photos/columns.js`:
687
+
688
+ ```erb
689
+ <%= javascript_include_tag "main", "/photos/columns" %>
690
+ ```
691
+
692
+ To include `http://example.com/main.js`:
693
+
694
+ ```erb
695
+ <%= javascript_include_tag "http://example.com/main.js" %>
696
+ ```
697
+
698
+ #### Linking to CSS Files with the `stylesheet_link_tag`
699
+
700
+ The `stylesheet_link_tag` helper returns an HTML `<link>` tag for each source provided.
701
+
702
+ If you are using Rails with the "Asset Pipeline" enabled, this helper will generate a link to `/assets/stylesheets/`. This link is then processed by the Sprockets gem. A stylesheet file can be stored in one of three locations: `app/assets`, `lib/assets` or `vendor/assets`.
703
+
704
+ You can specify a full path relative to the document root, or a URL. For example, to link to a stylesheet file that is inside a directory called `stylesheets` inside of one of `app/assets`, `lib/assets` or `vendor/assets`, you would do this:
705
+
706
+ ```erb
707
+ <%= stylesheet_link_tag "main" %>
708
+ ```
709
+
710
+ To include `app/assets/stylesheets/main.css` and `app/assets/stylesheets/columns.css`:
711
+
712
+ ```erb
713
+ <%= stylesheet_link_tag "main", "columns" %>
714
+ ```
715
+
716
+ To include `app/assets/stylesheets/main.css` and `app/assets/stylesheets/photos/columns.css`:
717
+
718
+ ```erb
719
+ <%= stylesheet_link_tag "main", "photos/columns" %>
720
+ ```
721
+
722
+ To include `http://example.com/main.css`:
723
+
724
+ ```erb
725
+ <%= stylesheet_link_tag "http://example.com/main.css" %>
726
+ ```
727
+
728
+ By default, the `stylesheet_link_tag` creates links with `media="screen" rel="stylesheet"`. You can override any of these defaults by specifying an appropriate option (`:media`, `:rel`):
729
+
730
+ ```erb
731
+ <%= stylesheet_link_tag "main_print", media: "print" %>
732
+ ```
733
+
734
+ #### Linking to Images with the `image_tag`
735
+
736
+ The `image_tag` helper builds an HTML `<img />` tag to the specified file. By default, files are loaded from `public/images`.
737
+
738
+ WARNING: Note that you must specify the extension of the image.
739
+
740
+ ```erb
741
+ <%= image_tag "header.png" %>
742
+ ```
743
+
744
+ You can supply a path to the image if you like:
745
+
746
+ ```erb
747
+ <%= image_tag "icons/delete.gif" %>
748
+ ```
749
+
750
+ You can supply a hash of additional HTML options:
751
+
752
+ ```erb
753
+ <%= image_tag "icons/delete.gif", {height: 45} %>
754
+ ```
755
+
756
+ You can supply alternate text for the image which will be used if the user has images turned off in their browser. If you do not specify an alt text explicitly, it defaults to the file name of the file, capitalized and with no extension. For example, these two image tags would return the same code:
757
+
758
+ ```erb
759
+ <%= image_tag "home.gif" %>
760
+ <%= image_tag "home.gif", alt: "Home" %>
761
+ ```
762
+
763
+ You can also specify a special size tag, in the format "{width}x{height}":
764
+
765
+ ```erb
766
+ <%= image_tag "home.gif", size: "50x20" %>
767
+ ```
768
+
769
+ In addition to the above special tags, you can supply a final hash of standard HTML options, such as `:class`, `:id` or `:name`:
770
+
771
+ ```erb
772
+ <%= image_tag "home.gif", alt: "Go Home",
773
+ id: "HomeImage",
774
+ class: "nav_bar" %>
775
+ ```
776
+
777
+ #### Linking to Videos with the `video_tag`
778
+
779
+ The `video_tag` helper builds an HTML 5 `<video>` tag to the specified file. By default, files are loaded from `public/videos`.
780
+
781
+ ```erb
782
+ <%= video_tag "movie.ogg" %>
783
+ ```
784
+
785
+ Produces
786
+
787
+ ```erb
788
+ <video src="/videos/movie.ogg" />
789
+ ```
790
+
791
+ Like an `image_tag` you can supply a path, either absolute, or relative to the `public/videos` directory. Additionally you can specify the `size: "#{width}x#{height}"` option just like an `image_tag`. Video tags can also have any of the HTML options specified at the end (`id`, `class` et al).
792
+
793
+ The video tag also supports all of the `<video>` HTML options through the HTML options hash, including:
794
+
795
+ * `poster: "image_name.png"`, provides an image to put in place of the video before it starts playing.
796
+ * `autoplay: true`, starts playing the video on page load.
797
+ * `loop: true`, loops the video once it gets to the end.
798
+ * `controls: true`, provides browser supplied controls for the user to interact with the video.
799
+ * `autobuffer: true`, the video will pre load the file for the user on page load.
800
+
801
+ You can also specify multiple videos to play by passing an array of videos to the `video_tag`:
802
+
803
+ ```erb
804
+ <%= video_tag ["trailer.ogg", "movie.ogg"] %>
805
+ ```
806
+
807
+ This will produce:
808
+
809
+ ```erb
810
+ <video><source src="trailer.ogg" /><source src="movie.ogg" /></video>
811
+ ```
812
+
813
+ #### Linking to Audio Files with the `audio_tag`
814
+
815
+ The `audio_tag` helper builds an HTML 5 `<audio>` tag to the specified file. By default, files are loaded from `public/audios`.
816
+
817
+ ```erb
818
+ <%= audio_tag "music.mp3" %>
819
+ ```
820
+
821
+ You can supply a path to the audio file if you like:
822
+
823
+ ```erb
824
+ <%= audio_tag "music/first_song.mp3" %>
825
+ ```
826
+
827
+ You can also supply a hash of additional options, such as `:id`, `:class` etc.
828
+
829
+ Like the `video_tag`, the `audio_tag` has special options:
830
+
831
+ * `autoplay: true`, starts playing the audio on page load
832
+ * `controls: true`, provides browser supplied controls for the user to interact with the audio.
833
+ * `autobuffer: true`, the audio will pre load the file for the user on page load.
834
+
835
+ ### Understanding `yield`
836
+
837
+ Within the context of a layout, `yield` identifies a section where content from the view should be inserted. The simplest way to use this is to have a single `yield`, into which the entire contents of the view currently being rendered is inserted:
838
+
839
+ ```html+erb
840
+ <html>
841
+ <head>
842
+ </head>
843
+ <body>
844
+ <%= yield %>
845
+ </body>
846
+ </html>
847
+ ```
848
+
849
+ You can also create a layout with multiple yielding regions:
850
+
851
+ ```html+erb
852
+ <html>
853
+ <head>
854
+ <%= yield :head %>
855
+ </head>
856
+ <body>
857
+ <%= yield %>
858
+ </body>
859
+ </html>
860
+ ```
861
+
862
+ The main body of the view will always render into the unnamed `yield`. To render content into a named `yield`, you use the `content_for` method.
863
+
864
+ ### Using the `content_for` Method
865
+
866
+ The `content_for` method allows you to insert content into a named `yield` block in your layout. For example, this view would work with the layout that you just saw:
867
+
868
+ ```html+erb
869
+ <% content_for :head do %>
870
+ <title>A simple page</title>
871
+ <% end %>
872
+
873
+ <p>Hello, Rails!</p>
874
+ ```
875
+
876
+ The result of rendering this page into the supplied layout would be this HTML:
877
+
878
+ ```html+erb
879
+ <html>
880
+ <head>
881
+ <title>A simple page</title>
882
+ </head>
883
+ <body>
884
+ <p>Hello, Rails!</p>
885
+ </body>
886
+ </html>
887
+ ```
888
+
889
+ The `content_for` method is very helpful when your layout contains distinct regions such as sidebars and footers that should get their own blocks of content inserted. It's also useful for inserting tags that load page-specific JavaScript or css files into the header of an otherwise generic layout.
890
+
891
+ ### Using Partials
892
+
893
+ Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.
894
+
895
+ #### Naming Partials
896
+
897
+ To render a partial as part of a view, you use the `render` method within the view:
898
+
899
+ ```ruby
900
+ <%= render "menu" %>
901
+ ```
902
+
903
+ This will render a file named `_menu.html.erb` at that point within the view being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you're pulling in a partial from another folder:
904
+
905
+ ```ruby
906
+ <%= render "shared/menu" %>
907
+ ```
908
+
909
+ That code will pull in the partial from `app/views/shared/_menu.html.erb`.
910
+
911
+ #### Using Partials to Simplify Views
912
+
913
+ One way to use partials is to treat them as the equivalent of subroutines: as a way to move details out of a view so that you can grasp what's going on more easily. For example, you might have a view that looked like this:
914
+
915
+ ```erb
916
+ <%= render "shared/ad_banner" %>
917
+
918
+ <h1>Products</h1>
919
+
920
+ <p>Here are a few of our fine products:</p>
921
+ ...
922
+
923
+ <%= render "shared/footer" %>
924
+ ```
925
+
926
+ Here, the `_ad_banner.html.erb` and `_footer.html.erb` partials could contain content that is shared among many pages in your application. You don't need to see the details of these sections when you're concentrating on a particular page.
927
+
928
+ TIP: For content that is shared among all pages in your application, you can use partials directly from layouts.
929
+
930
+ #### Partial Layouts
931
+
932
+ A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:
933
+
934
+ ```erb
935
+ <%= render partial: "link_area", layout: "graybar" %>
936
+ ```
937
+
938
+ This would look for a partial named `_link_area.html.erb` and render it using the layout `_graybar.html.erb`. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master `layouts` folder).
939
+
940
+ Also note that explicitly specifying `:partial` is required when passing additional options such as `:layout`.
941
+
942
+ #### Passing Local Variables
943
+
944
+ You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content:
945
+
946
+ * `new.html.erb`
947
+
948
+ ```html+erb
949
+ <h1>New zone</h1>
950
+ <%= error_messages_for :zone %>
951
+ <%= render partial: "form", locals: {zone: @zone} %>
952
+ ```
953
+
954
+ * `edit.html.erb`
955
+
956
+ ```html+erb
957
+ <h1>Editing zone</h1>
958
+ <%= error_messages_for :zone %>
959
+ <%= render partial: "form", locals: {zone: @zone} %>
960
+ ```
961
+
962
+ * `_form.html.erb`
963
+
964
+ ```html+erb
965
+ <%= form_for(zone) do |f| %>
966
+ <p>
967
+ <b>Zone name</b><br />
968
+ <%= f.text_field :name %>
969
+ </p>
970
+ <p>
971
+ <%= f.submit %>
972
+ </p>
973
+ <% end %>
974
+ ```
975
+
976
+ Although the same partial will be rendered into both views, Action View's submit helper will return "Create Zone" for the new action and "Update Zone" for the edit action.
977
+
978
+ Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the `:object` option:
979
+
980
+ ```erb
981
+ <%= render partial: "customer", object: @new_customer %>
982
+ ```
983
+
984
+ Within the `customer` partial, the `customer` variable will refer to `@new_customer` from the parent view.
985
+
986
+ If you have an instance of a model to render into a partial, you can use a shorthand syntax:
987
+
988
+ ```erb
989
+ <%= render @customer %>
990
+ ```
991
+
992
+ Assuming that the `@customer` instance variable contains an instance of the `Customer` model, this will use `_customer.html.erb` to render it and will pass the local variable `customer` into the partial which will refer to the `@customer` instance variable in the parent view.
993
+
994
+ #### Rendering Collections
995
+
996
+ Partials are very useful in rendering collections. When you pass a collection to a partial via the `:collection` option, the partial will be inserted once for each member in the collection:
997
+
998
+ * `index.html.erb`
999
+
1000
+ ```html+erb
1001
+ <h1>Products</h1>
1002
+ <%= render partial: "product", collection: @products %>
1003
+ ```
1004
+
1005
+ * `_product.html.erb`
1006
+
1007
+ ```html+erb
1008
+ <p>Product Name: <%= product.name %></p>
1009
+ ```
1010
+
1011
+ When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is `_product`, and within the `_product` partial, you can refer to `product` to get the instance that is being rendered.
1012
+
1013
+ There is also a shorthand for this. Assuming `@products` is a collection of `product` instances, you can simply write this in the `index.html.erb` to produce the same result:
1014
+
1015
+ ```html+erb
1016
+ <h1>Products</h1>
1017
+ <%= render @products %>
1018
+ ```
1019
+
1020
+ Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection:
1021
+
1022
+ * `index.html.erb`
1023
+
1024
+ ```html+erb
1025
+ <h1>Contacts</h1>
1026
+ <%= render [customer1, employee1, customer2, employee2] %>
1027
+ ```
1028
+
1029
+ * `customers/_customer.html.erb`
1030
+
1031
+ ```html+erb
1032
+ <p>Customer: <%= customer.name %></p>
1033
+ ```
1034
+
1035
+ * `employees/_employee.html.erb`
1036
+
1037
+ ```html+erb
1038
+ <p>Employee: <%= employee.name %></p>
1039
+ ```
1040
+
1041
+ In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.
1042
+
1043
+ In the event that the collection is empty, `render` will return nil, so it should be fairly simple to provide alternative content.
1044
+
1045
+ ```html+erb
1046
+ <h1>Products</h1>
1047
+ <%= render(@products) || "There are no products available." %>
1048
+ ```
1049
+
1050
+ #### Local Variables
1051
+
1052
+ To use a custom local variable name within the partial, specify the `:as` option in the call to the partial:
1053
+
1054
+ ```erb
1055
+ <%= render partial: "product", collection: @products, as: :item %>
1056
+ ```
1057
+
1058
+ With this change, you can access an instance of the `@products` collection as the `item` local variable within the partial.
1059
+
1060
+ You can also pass in arbitrary local variables to any partial you are rendering with the `locals: {}` option:
1061
+
1062
+ ```erb
1063
+ <%= render partial: "products", collection: @products,
1064
+ as: :item, locals: {title: "Products Page"} %>
1065
+ ```
1066
+
1067
+ Would render a partial `_products.html.erb` once for each instance of `product` in the `@products` instance variable passing the instance to the partial as a local variable called `item` and to each partial, make the local variable `title` available with the value `Products Page`.
1068
+
1069
+ TIP: Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by `_counter`. For example, if you're rendering `@products`, within the partial you can refer to `product_counter` to tell you how many times the partial has been rendered. This does not work in conjunction with the `as: :value` option.
1070
+
1071
+ You can also specify a second partial to be rendered between instances of the main partial by using the `:spacer_template` option:
1072
+
1073
+ #### Spacer Templates
1074
+
1075
+ ```erb
1076
+ <%= render partial: @products, spacer_template: "product_ruler" %>
1077
+ ```
1078
+
1079
+ Rails will render the `_product_ruler` partial (with no data passed in to it) between each pair of `_product` partials.
1080
+
1081
+ #### Collection Partial Layouts
1082
+
1083
+ When rendering collections it is also possible to use the `:layout` option:
1084
+
1085
+ ```erb
1086
+ <%= render partial: "product", collection: @products, layout: "special_layout" %>
1087
+ ```
1088
+
1089
+ The layout will be rendered together with the partial for each item in the collection. The current object and object_counter variables will be available in the layout as well, the same way they do within the partial.
1090
+
1091
+ ### Using Nested Layouts
1092
+
1093
+ You may find that your application requires a layout that differs slightly from your regular application layout to support one particular controller. Rather than repeating the main layout and editing it, you can accomplish this by using nested layouts (sometimes called sub-templates). Here's an example:
1094
+
1095
+ Suppose you have the following `ApplicationController` layout:
1096
+
1097
+ * `app/views/layouts/application.html.erb`
1098
+
1099
+ ```html+erb
1100
+ <html>
1101
+ <head>
1102
+ <title><%= @page_title or "Page Title" %></title>
1103
+ <%= stylesheet_link_tag "layout" %>
1104
+ <style><%= yield :stylesheets %></style>
1105
+ </head>
1106
+ <body>
1107
+ <div id="top_menu">Top menu items here</div>
1108
+ <div id="menu">Menu items here</div>
1109
+ <div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
1110
+ </body>
1111
+ </html>
1112
+ ```
1113
+
1114
+ On pages generated by `NewsController`, you want to hide the top menu and add a right menu:
1115
+
1116
+ * `app/views/layouts/news.html.erb`
1117
+
1118
+ ```html+erb
1119
+ <% content_for :stylesheets do %>
1120
+ #top_menu {display: none}
1121
+ #right_menu {float: right; background-color: yellow; color: black}
1122
+ <% end %>
1123
+ <% content_for :content do %>
1124
+ <div id="right_menu">Right menu items here</div>
1125
+ <%= content_for?(:news_content) ? yield(:news_content) : yield %>
1126
+ <% end %>
1127
+ <%= render template: "layouts/application" %>
1128
+ ```
1129
+
1130
+ That's it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the "content" div.
1131
+
1132
+ There are several ways of getting similar results with different sub-templating schemes using this technique. Note that there is no limit in nesting levels. One can use the `ActionView::render` method via `render template: 'layouts/news'` to base a new layout on the News layout. If you are sure you will not subtemplate the `News` layout, you can replace the `content_for?(:news_content) ? yield(:news_content) : yield` with simply `yield`.