railties 3.0.11 → 3.0.12.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (166) hide show
  1. data/lib/rails/version.rb +2 -2
  2. metadata +305 -461
  3. data/guides/output/2_2_release_notes.html +0 -565
  4. data/guides/output/2_3_release_notes.html +0 -713
  5. data/guides/output/3_0_release_notes.html +0 -652
  6. data/guides/output/3_1_release_notes.html +0 -670
  7. data/guides/output/action_controller_overview.html +0 -925
  8. data/guides/output/action_mailer_basics.html +0 -658
  9. data/guides/output/action_view_overview.html +0 -1471
  10. data/guides/output/active_model_basics.html +0 -349
  11. data/guides/output/active_record_basics.html +0 -364
  12. data/guides/output/active_record_querying.html +0 -1272
  13. data/guides/output/active_record_validations_callbacks.html +0 -1292
  14. data/guides/output/active_resource_basics.html +0 -252
  15. data/guides/output/active_support_core_extensions.html +0 -3374
  16. data/guides/output/ajax_on_rails.html +0 -412
  17. data/guides/output/api_documentation_guidelines.html +0 -317
  18. data/guides/output/asset_pipeline.html +0 -691
  19. data/guides/output/association_basics.html +0 -1742
  20. data/guides/output/caching_with_rails.html +0 -533
  21. data/guides/output/command_line.html +0 -662
  22. data/guides/output/configuring.html +0 -811
  23. data/guides/output/contribute.html +0 -216
  24. data/guides/output/contributing_to_ruby_on_rails.html +0 -465
  25. data/guides/output/credits.html +0 -210
  26. data/guides/output/debugging_rails_applications.html +0 -791
  27. data/guides/output/engines.html +0 -673
  28. data/guides/output/form_helpers.html +0 -850
  29. data/guides/output/generators.html +0 -725
  30. data/guides/output/getting_started.html +0 -1980
  31. data/guides/output/i18n.html +0 -1054
  32. data/guides/output/images/belongs_to.png +0 -0
  33. data/guides/output/images/book_icon.gif +0 -0
  34. data/guides/output/images/bullet.gif +0 -0
  35. data/guides/output/images/challenge.png +0 -0
  36. data/guides/output/images/chapters_icon.gif +0 -0
  37. data/guides/output/images/check_bullet.gif +0 -0
  38. data/guides/output/images/credits_pic_blank.gif +0 -0
  39. data/guides/output/images/csrf.png +0 -0
  40. data/guides/output/images/customized_error_messages.png +0 -0
  41. data/guides/output/images/edge_badge.png +0 -0
  42. data/guides/output/images/error_messages.png +0 -0
  43. data/guides/output/images/feature_tile.gif +0 -0
  44. data/guides/output/images/footer_tile.gif +0 -0
  45. data/guides/output/images/fxn.png +0 -0
  46. data/guides/output/images/grey_bullet.gif +0 -0
  47. data/guides/output/images/habtm.png +0 -0
  48. data/guides/output/images/has_many.png +0 -0
  49. data/guides/output/images/has_many_through.png +0 -0
  50. data/guides/output/images/has_one.png +0 -0
  51. data/guides/output/images/has_one_through.png +0 -0
  52. data/guides/output/images/header_backdrop.png +0 -0
  53. data/guides/output/images/header_tile.gif +0 -0
  54. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  55. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  56. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  57. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  58. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  59. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  60. data/guides/output/images/icons/README +0 -5
  61. data/guides/output/images/icons/callouts/1.png +0 -0
  62. data/guides/output/images/icons/callouts/10.png +0 -0
  63. data/guides/output/images/icons/callouts/11.png +0 -0
  64. data/guides/output/images/icons/callouts/12.png +0 -0
  65. data/guides/output/images/icons/callouts/13.png +0 -0
  66. data/guides/output/images/icons/callouts/14.png +0 -0
  67. data/guides/output/images/icons/callouts/15.png +0 -0
  68. data/guides/output/images/icons/callouts/2.png +0 -0
  69. data/guides/output/images/icons/callouts/3.png +0 -0
  70. data/guides/output/images/icons/callouts/4.png +0 -0
  71. data/guides/output/images/icons/callouts/5.png +0 -0
  72. data/guides/output/images/icons/callouts/6.png +0 -0
  73. data/guides/output/images/icons/callouts/7.png +0 -0
  74. data/guides/output/images/icons/callouts/8.png +0 -0
  75. data/guides/output/images/icons/callouts/9.png +0 -0
  76. data/guides/output/images/icons/caution.png +0 -0
  77. data/guides/output/images/icons/example.png +0 -0
  78. data/guides/output/images/icons/home.png +0 -0
  79. data/guides/output/images/icons/important.png +0 -0
  80. data/guides/output/images/icons/next.png +0 -0
  81. data/guides/output/images/icons/note.png +0 -0
  82. data/guides/output/images/icons/prev.png +0 -0
  83. data/guides/output/images/icons/tip.png +0 -0
  84. data/guides/output/images/icons/up.png +0 -0
  85. data/guides/output/images/icons/warning.png +0 -0
  86. data/guides/output/images/jaimeiniesta.jpg +0 -0
  87. data/guides/output/images/nav_arrow.gif +0 -0
  88. data/guides/output/images/polymorphic.png +0 -0
  89. data/guides/output/images/posts_index.png +0 -0
  90. data/guides/output/images/radar.png +0 -0
  91. data/guides/output/images/rails_guides_logo.gif +0 -0
  92. data/guides/output/images/rails_logo_remix.gif +0 -0
  93. data/guides/output/images/rails_welcome.png +0 -0
  94. data/guides/output/images/session_fixation.png +0 -0
  95. data/guides/output/images/tab_grey.gif +0 -0
  96. data/guides/output/images/tab_info.gif +0 -0
  97. data/guides/output/images/tab_note.gif +0 -0
  98. data/guides/output/images/tab_red.gif +0 -0
  99. data/guides/output/images/tab_yellow.gif +0 -0
  100. data/guides/output/images/tab_yellow.png +0 -0
  101. data/guides/output/images/validation_error_messages.png +0 -0
  102. data/guides/output/images/vijaydev.jpg +0 -0
  103. data/guides/output/index.html +0 -300
  104. data/guides/output/initialization.html +0 -1087
  105. data/guides/output/javascripts/guides.js +0 -7
  106. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  107. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  108. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  109. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  110. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  111. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  112. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  113. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  114. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  115. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  116. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  117. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  118. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  119. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  120. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  121. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  122. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  123. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  124. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  125. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  126. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  127. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  128. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  129. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  130. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  131. data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
  132. data/guides/output/layout.html +0 -312
  133. data/guides/output/layouts_and_rendering.html +0 -1257
  134. data/guides/output/migrations.html +0 -751
  135. data/guides/output/nested_model_forms.html +0 -350
  136. data/guides/output/performance_testing.html +0 -858
  137. data/guides/output/plugins.html +0 -590
  138. data/guides/output/rails_application_templates.html +0 -368
  139. data/guides/output/rails_on_rack.html +0 -408
  140. data/guides/output/routing.html +0 -1246
  141. data/guides/output/ruby_on_rails_guides_guidelines.html +0 -218
  142. data/guides/output/security.html +0 -968
  143. data/guides/output/stylesheets/fixes.css +0 -16
  144. data/guides/output/stylesheets/main.css +0 -445
  145. data/guides/output/stylesheets/print.css +0 -52
  146. data/guides/output/stylesheets/reset.css +0 -43
  147. data/guides/output/stylesheets/style.css +0 -13
  148. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
  149. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  150. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  151. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  152. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  153. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  154. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  155. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  156. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  157. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  158. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  159. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  160. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  161. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  162. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  163. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  164. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  165. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  166. data/guides/output/testing.html +0 -1182
@@ -1,1257 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
-
4
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
- <head>
6
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
-
8
- <title>Ruby on Rails Guides: Layouts and Rendering in Rails</title>
9
-
10
- <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
- <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
-
13
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
-
16
- <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
- </head>
18
- <body class="guide">
19
- <div id="topNav">
20
- <div class="wrapper">
21
- <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
22
- <a href="http://rubyonrails.org/">Overview</a> |
23
- <a href="http://rubyonrails.org/download">Download</a> |
24
- <a href="http://rubyonrails.org/deploy">Deploy</a> |
25
- <a href="https://github.com/rails/rails">Code</a> |
26
- <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
27
- <a href="http://rubyonrails.org/documentation">Documentation</a> |
28
- <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
29
- <a href="http://rubyonrails.org/community">Community</a> |
30
- <a href="http://weblog.rubyonrails.org/">Blog</a>
31
- </div>
32
- </div>
33
- <div id="header">
34
- <div class="wrapper clearfix">
35
- <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
36
- <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
37
- <ul class="nav">
38
- <li><a href="index.html">Home</a></li>
39
- <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
40
- <div id="guides" class="clearfix" style="display: none;">
41
- <hr />
42
- <dl class="L">
43
- <dt>Start Here</dt>
44
- <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
45
- <dt>Models</dt>
46
- <dd><a href="migrations.html">Rails Database Migrations</a></dd>
47
- <dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
48
- <dd><a href="association_basics.html">Active Record Associations</a></dd>
49
- <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
50
- <dt>Views</dt>
51
- <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
52
- <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
53
- <dt>Controllers</dt>
54
- <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
55
- <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
56
- </dl>
57
- <dl class="R">
58
- <dt>Digging Deeper</dt>
59
- <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
60
- <dd><a href="i18n.html">Rails Internationalization API</a></dd>
61
- <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
62
- <dd><a href="testing.html">Testing Rails Applications</a></dd>
63
- <dd><a href="security.html">Securing Rails Applications</a></dd>
64
- <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
65
- <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
66
- <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
67
- <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
68
- <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
69
- <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
70
-
71
- <dt>Extending Rails</dt>
72
- <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
73
- <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
74
- <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
75
-
76
- <dt>Contributing to Ruby on Rails</dt>
77
- <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
78
- <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
79
- <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
80
-
81
- <dt>Release Notes</dt>
82
- <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
83
- <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
84
- <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
85
- <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
86
- </dl>
87
- </div>
88
- </li>
89
- <li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
90
- <li><a href="credits.html">Credits</a></li>
91
- </ul>
92
- </div>
93
- </div>
94
- <hr class="hide" />
95
-
96
- <div id="feature">
97
- <div class="wrapper">
98
- <h2>Layouts and Rendering in Rails</h2>
99
- <p>This guide covers the basic layout features of Action Controller and Action View. By referring to this guide, you will be able to:</p>
100
- <ul>
101
- <li>Use the various rendering methods built into Rails</li>
102
- <li>Create layouts with multiple content sections</li>
103
- <li>Use partials to <span class="caps">DRY</span> up your views</li>
104
- <li>Use nested layouts (sub-templates)</li>
105
- </ul>
106
-
107
- <div id="subCol">
108
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
109
- <ol class="chapters">
110
- <li><a href="#overview-how-the-pieces-fit-together">Overview: How the Pieces Fit Together</a></li><li><a href="#creating-responses">Creating Responses</a><ul><li><a href="#rendering-by-default-convention-over-configuration-in-action">Rendering by Default: Convention Over Configuration in Action</a></li> <li><a href="#using-render">Using <tt>render</tt></a></li> <li><a href="#using-redirect_to">Using <tt>redirect_to</tt></a></li> <li><a href="#using-head-to-build-header-only-responses">Using <tt>head</tt> To Build Header-Only Responses</a></li></ul></li><li><a href="#structuring-layouts">Structuring Layouts</a><ul><li><a href="#asset-tag-helpers">Asset Tag Helpers</a></li> <li><a href="#understanding-yield">Understanding <tt>yield</tt></a></li> <li><a href="#using-the-content_for-method">Using the <tt>content_for</tt> Method</a></li> <li><a href="#using-partials">Using Partials</a></li> <li><a href="#using-nested-layouts">Using Nested Layouts</a></li></ul></li></ol></div>
111
- </div>
112
- </div>
113
-
114
- <div id="container">
115
- <div class="wrapper">
116
- <div id="mainCol">
117
- <h3 id="overview-how-the-pieces-fit-together">1 Overview: How the Pieces Fit Together</h3>
118
- <p>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&#8217;s time to send a response back to the user, the Controller hands things off to the View. It&#8217;s that handoff that is the subject of this guide.</p>
119
- <p>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&#8217;ll see all of those paths later in this guide.</p>
120
- <h3 id="creating-responses">2 Creating Responses</h3>
121
- <p>From the controller&#8217;s point of view, there are three ways to create an <span class="caps">HTTP</span> response:</p>
122
- <ul>
123
- <li>Call <tt>render</tt> to create a full response to send back to the browser</li>
124
- <li>Call <tt>redirect_to</tt> to send an <span class="caps">HTTP</span> redirect status code to the browser</li>
125
- <li>Call <tt>head</tt> to create a response consisting solely of <span class="caps">HTTP</span> headers to send back to the browser</li>
126
- </ul>
127
- <p>I&#8217;ll cover each of these methods in turn. But first, a few words about the very easiest thing that the controller can do to create a response: nothing at all.</p>
128
- <h4 id="rendering-by-default-convention-over-configuration-in-action">2.1 Rendering by Default: Convention Over Configuration in Action</h4>
129
- <p>You&#8217;ve heard that Rails promotes &#8220;convention over configuration&#8221;. 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 <tt>BooksController</tt> class:</p>
130
- <div class="code_container">
131
- <pre class="brush: ruby; gutter: false; toolbar: false">
132
- class BooksController &lt; ApplicationController
133
- end
134
- </pre>
135
- </div>
136
- <p>And the following in your routes file:</p>
137
- <div class="code_container">
138
- <pre class="brush: ruby; gutter: false; toolbar: false">
139
- resources :books
140
- </pre>
141
- </div>
142
- <p>And you have a view file <tt>app/views/books/index.html.erb</tt>:</p>
143
- <div class="code_container">
144
- <pre class="brush: ruby; gutter: false; toolbar: false">
145
- &lt;h1&gt;Books are coming soon!&lt;/h1&gt;
146
- </pre>
147
- </div>
148
- <p>Rails will automatically render <tt>app/views/books/index.html.erb</tt> when you navigate to <tt>/books</tt> and you will see &#8220;Books are coming soon!&#8221; on your screen.</p>
149
- <p>However a coming soon screen is only minimally useful, so you will soon create your <tt>Book</tt> model and add the index action to <tt>BooksController</tt>:</p>
150
- <div class="code_container">
151
- <pre class="brush: ruby; gutter: false; toolbar: false">
152
- class BooksController &lt; ApplicationController
153
- def index
154
- @books = Book.all
155
- end
156
- end
157
- </pre>
158
- </div>
159
- <p>Note that we don&#8217;t have explicit render at the end of the index action in accordance with &#8220;convention over configuration&#8221; 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 <tt>action_name.html.erb</tt> template in the controller&#8217;s view path and render it. So in this case, Rails will render the <tt>app/views/books/index.html.erb</tt> file.</p>
160
- <p>If we want to display the properties of all the books in our view, we can do so with an <span class="caps">ERB</span> template like this:</p>
161
- <div class="code_container">
162
- <pre class="brush: ruby; gutter: false; toolbar: false">
163
- &lt;h1&gt;Listing Books&lt;/h1&gt;
164
-
165
- &lt;table&gt;
166
- &lt;tr&gt;
167
- &lt;th&gt;Title&lt;/th&gt;
168
- &lt;th&gt;Summary&lt;/th&gt;
169
- &lt;th&gt;&lt;/th&gt;
170
- &lt;th&gt;&lt;/th&gt;
171
- &lt;th&gt;&lt;/th&gt;
172
- &lt;/tr&gt;
173
-
174
- &lt;% @books.each do |book| %&gt;
175
- &lt;tr&gt;
176
- &lt;td&gt;&lt;%= book.title %&gt;&lt;/td&gt;
177
- &lt;td&gt;&lt;%= book.content %&gt;&lt;/td&gt;
178
- &lt;td&gt;&lt;%= link_to 'Show', book %&gt;&lt;/td&gt;
179
- &lt;td&gt;&lt;%= link_to 'Edit', edit_book_path(book) %&gt;&lt;/td&gt;
180
- &lt;td&gt;&lt;%= link_to 'Remove', book, :confirm =&gt; 'Are you sure?', :method =&gt; :delete %&gt;&lt;/td&gt;
181
- &lt;/tr&gt;
182
- &lt;% end %&gt;
183
- &lt;/table&gt;
184
-
185
- &lt;br /&gt;
186
-
187
- &lt;%= link_to 'New book', new_book_path %&gt;
188
- </pre>
189
- </div>
190
- <div class="note"><p>The actual rendering is done by subclasses of <tt>ActionView::TemplateHandlers</tt>. This guide does not dig into that process, but it&#8217;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 <tt>.erb</tt> for <span class="caps">ERB</span> (<span class="caps">HTML</span> with embedded Ruby), and <tt>.builder</tt> for Builder (<span class="caps">XML</span> generator).</p></div>
191
- <h4 id="using-render">2.2 Using <tt>render</tt></h4>
192
- <p>In most cases, the <tt>ActionController::Base#render</tt> method does the heavy lifting of rendering your application&#8217;s content for use by a browser. There are a variety of ways to customize the behaviour of <tt>render</tt>. 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, <span class="caps">JSON</span>, or <span class="caps">XML</span>. You can specify the content type or <span class="caps">HTTP</span> status of the rendered response as well.</p>
193
- <div class="info"><p>If you want to see the exact results of a call to <tt>render</tt> without needing to inspect it in a browser, you can call <tt>render_to_string</tt>. This method takes exactly the same options as <tt>render</tt>, but it returns a string instead of sending a response back to the browser.</p></div>
194
- <h5 id="rendering-nothing">2.2.1 Rendering Nothing</h5>
195
- <p>Perhaps the simplest thing you can do with <tt>render</tt> is to render nothing at all:</p>
196
- <div class="code_container">
197
- <pre class="brush: ruby; gutter: false; toolbar: false">
198
- render :nothing =&gt; true
199
- </pre>
200
- </div>
201
- <p>If you look at the response for this using cURL, you will see the following:</p>
202
- <div class="code_container">
203
- <pre class="brush: plain; gutter: false; toolbar: false">
204
- $ curl -i 127.0.0.1:3000/books
205
- HTTP/1.1 200 OK
206
- Connection: close
207
- Date: Sun, 24 Jan 2010 09:25:18 GMT
208
- Transfer-Encoding: chunked
209
- Content-Type: */*; charset=utf-8
210
- X-Runtime: 0.014297
211
- Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
212
- Cache-Control: no-cache
213
-
214
-
215
- $
216
- </pre>
217
- </div>
218
- <p>We see there is an empty response (no data after the <tt>Cache-Control</tt> line), but the request was successful because Rails has set the response to 200 OK. You can set the <tt>:status</tt> option on render to change this response. Rendering nothing can be useful for <span class="caps">AJAX</span> requests where all you want to send back to the browser is an acknowledgment that the request was completed.</p>
219
- <div class="info"><p>You should probably be using the <tt>head</tt> method, discussed later in this guide, instead of <tt>render :nothing</tt>. This provides additional flexibility and makes it explicit that you&#8217;re only generating <span class="caps">HTTP</span> headers.</p></div>
220
- <h5 id="rendering-an-action-s-view">2.2.2 Rendering an Action&#8217;s View</h5>
221
- <p>If you want to render the view that corresponds to a different action within the same template, you can use <tt>render</tt> with the name of the view:</p>
222
- <div class="code_container">
223
- <pre class="brush: ruby; gutter: false; toolbar: false">
224
- def update
225
- @book = Book.find(params[:id])
226
- if @book.update_attributes(params[:book])
227
- redirect_to(@book)
228
- else
229
- render &quot;edit&quot;
230
- end
231
- end
232
- </pre>
233
- </div>
234
- <p>If the call to <tt>update_attributes</tt> fails, calling the <tt>update</tt> action in this controller will render the <tt>edit.html.erb</tt> template belonging to the same controller.</p>
235
- <p>If you prefer, you can use a symbol instead of a string to specify the action to render:</p>
236
- <div class="code_container">
237
- <pre class="brush: ruby; gutter: false; toolbar: false">
238
- def update
239
- @book = Book.find(params[:id])
240
- if @book.update_attributes(params[:book])
241
- redirect_to(@book)
242
- else
243
- render :edit
244
- end
245
- end
246
- </pre>
247
- </div>
248
- <p>To be explicit, you can use <tt>render</tt> with the <tt>:action</tt> option (though this is no longer necessary in Rails 3.0):</p>
249
- <div class="code_container">
250
- <pre class="brush: ruby; gutter: false; toolbar: false">
251
- def update
252
- @book = Book.find(params[:id])
253
- if @book.update_attributes(params[:book])
254
- redirect_to(@book)
255
- else
256
- render :action =&gt; &quot;edit&quot;
257
- end
258
- end
259
- </pre>
260
- </div>
261
- <div class="warning"><p>Using <tt>render</tt> with <tt>:action</tt> is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does <em>not</em> run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling <tt>render</tt>.</p></div>
262
- <h5 id="rendering-an-action-s-template-from-another-controller">2.2.3 Rendering an Action&#8217;s Template from Another Controller</h5>
263
- <p>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 <tt>render</tt>, which accepts the full path (relative to <tt>app/views</tt>) of the template to render. For example, if you&#8217;re running code in an <tt>AdminProductsController</tt> that lives in <tt>app/controllers/admin</tt>, you can render the results of an action to a template in <tt>app/views/products</tt> this way:</p>
264
- <div class="code_container">
265
- <pre class="brush: ruby; gutter: false; toolbar: false">
266
- render 'products/show'
267
- </pre>
268
- </div>
269
- <p>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 <tt>:template</tt> option (which was required on Rails 2.2 and earlier):</p>
270
- <div class="code_container">
271
- <pre class="brush: ruby; gutter: false; toolbar: false">
272
- render :template =&gt; 'products/show'
273
- </pre>
274
- </div>
275
- <h5 id="rendering-an-arbitrary-file">2.2.4 Rendering an Arbitrary File</h5>
276
- <p>The <tt>render</tt> method can also use a view that&#8217;s entirely outside of your application (perhaps you&#8217;re sharing views between two Rails applications):</p>
277
- <div class="code_container">
278
- <pre class="brush: ruby; gutter: false; toolbar: false">
279
- render &quot;/u/apps/warehouse_app/current/app/views/products/show&quot;
280
- </pre>
281
- </div>
282
- <p>Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the <tt>:file</tt> option (which was required on Rails 2.2 and earlier):</p>
283
- <div class="code_container">
284
- <pre class="brush: ruby; gutter: false; toolbar: false">
285
- render :file =&gt;
286
- &quot;/u/apps/warehouse_app/current/app/views/products/show&quot;
287
- </pre>
288
- </div>
289
- <p>The <tt>:file</tt> option takes an absolute file-system path. Of course, you need to have rights to the view that you&#8217;re using to render the content.</p>
290
- <div class="note"><p>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 <tt>:layout => true</tt> option.</p></div>
291
- <div class="info"><p>If you&#8217;re running Rails on Microsoft Windows, you should use the <tt>:file</tt> option to render a file, because Windows filenames do not have the same format as Unix filenames.</p></div>
292
- <h5 id="wrapping-it-up">2.2.5 Wrapping it up</h5>
293
- <p>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.</p>
294
- <p>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 <tt>edit.html.erb</tt> template in the <tt>views/books</tt> directory:</p>
295
- <div class="code_container">
296
- <pre class="brush: ruby; gutter: false; toolbar: false">
297
- render :edit
298
- render :action =&gt; :edit
299
- render 'edit'
300
- render 'edit.html.erb'
301
- render :action =&gt; 'edit'
302
- render :action =&gt; 'edit.html.erb'
303
- render 'books/edit'
304
- render 'books/edit.html.erb'
305
- render :template =&gt; 'books/edit'
306
- render :template =&gt; 'books/edit.html.erb'
307
- render '/path/to/rails/app/views/books/edit'
308
- render '/path/to/rails/app/views/books/edit.html.erb'
309
- render :file =&gt; '/path/to/rails/app/views/books/edit'
310
- render :file =&gt; '/path/to/rails/app/views/books/edit.html.erb'
311
- </pre>
312
- </div>
313
- <p>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.</p>
314
- <h5 id="using-render-with-inline">2.2.6 Using <tt>render</tt> with <tt>:inline</tt></h5>
315
- <p>The <tt>render</tt> method can do without a view completely, if you&#8217;re willing to use the <tt>:inline</tt> option to supply <span class="caps">ERB</span> as part of the method call. This is perfectly valid:</p>
316
- <div class="code_container">
317
- <pre class="brush: ruby; gutter: false; toolbar: false">
318
- render :inline =&gt;
319
- &quot;&lt;% products.each do |p| %&gt;&lt;p&gt;&lt;%= p.name %&gt;&lt;/p&gt;&lt;% end %&gt;&quot;
320
- </pre>
321
- </div>
322
- <div class="warning"><p>There is seldom any good reason to use this option. Mixing <span class="caps">ERB</span> into your controllers defeats the <span class="caps">MVC</span> orientation of Rails and will make it harder for other developers to follow the logic of your project. Use a separate erb view instead.</p></div>
323
- <p>By default, inline rendering uses <span class="caps">ERB</span>. You can force it to use Builder instead with the <tt>:type</tt> option:</p>
324
- <div class="code_container">
325
- <pre class="brush: ruby; gutter: false; toolbar: false">
326
- render :inline =&gt;
327
- &quot;xml.p {'Horrid coding practice!'}&quot;, :type =&gt; :builder
328
- </pre>
329
- </div>
330
- <h5 id="rendering-text">2.2.7 Rendering Text</h5>
331
- <p>You can send plain text &#8211; with no markup at all &#8211; back to the browser by using the <tt>:text</tt> option to <tt>render</tt>:</p>
332
- <div class="code_container">
333
- <pre class="brush: ruby; gutter: false; toolbar: false">
334
- render :text =&gt; &quot;OK&quot;
335
- </pre>
336
- </div>
337
- <div class="info"><p>Rendering pure text is most useful when you&#8217;re responding to <span class="caps">AJAX</span> or web service requests that are expecting something other than proper <span class="caps">HTML</span>.</p></div>
338
- <div class="note"><p>By default, if you use the <tt>:text</tt> 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 <tt>:layout => true</tt> option.</p></div>
339
- <h5 id="rendering-json">2.2.8 Rendering <span class="caps">JSON</span></h5>
340
- <p><span class="caps">JSON</span> is a JavaScript data format used by many <span class="caps">AJAX</span> libraries. Rails has built-in support for converting objects to <span class="caps">JSON</span> and rendering that <span class="caps">JSON</span> back to the browser:</p>
341
- <div class="code_container">
342
- <pre class="brush: ruby; gutter: false; toolbar: false">
343
- render :json =&gt; @product
344
- </pre>
345
- </div>
346
- <div class="info"><p>You don&#8217;t need to call <tt>to_json</tt> on the object that you want to render. If you use the <tt>:json</tt> option, <tt>render</tt> will automatically call <tt>to_json</tt> for you.</p></div>
347
- <h5 id="rendering-xml">2.2.9 Rendering <span class="caps">XML</span></h5>
348
- <p>Rails also has built-in support for converting objects to <span class="caps">XML</span> and rendering that <span class="caps">XML</span> back to the caller:</p>
349
- <div class="code_container">
350
- <pre class="brush: ruby; gutter: false; toolbar: false">
351
- render :xml =&gt; @product
352
- </pre>
353
- </div>
354
- <div class="info"><p>You don&#8217;t need to call <tt>to_xml</tt> on the object that you want to render. If you use the <tt>:xml</tt> option, <tt>render</tt> will automatically call <tt>to_xml</tt> for you.</p></div>
355
- <h5 id="rendering-vanilla-javascript">2.2.10 Rendering Vanilla JavaScript</h5>
356
- <p>Rails can render vanilla JavaScript:</p>
357
- <div class="code_container">
358
- <pre class="brush: ruby; gutter: false; toolbar: false">
359
- render :js =&gt; &quot;alert('Hello Rails');&quot;
360
- </pre>
361
- </div>
362
- <p>This will send the supplied string to the browser with a <span class="caps">MIME</span> type of <tt>text/javascript</tt>.</p>
363
- <h5 id="options-for-render">2.2.11 Options for <tt>render</tt></h5>
364
- <p>Calls to the <tt>render</tt> method generally accept four options:</p>
365
- <ul>
366
- <li><tt>:content_type</tt></li>
367
- <li><tt>:layout</tt></li>
368
- <li><tt>:status</tt></li>
369
- <li><tt>:location</tt></li>
370
- </ul>
371
- <h6 id="the-content_type-option">2.2.11.1 The <tt>:content_type</tt> Option</h6>
372
- <p>By default, Rails will serve the results of a rendering operation with the <span class="caps">MIME</span> content-type of <tt>text/html</tt> (or <tt>application/json</tt> if you use the <tt>:json</tt> option, or <tt>application/xml</tt> for the <tt>:xml</tt> option.). There are times when you might like to change this, and you can do so by setting the <tt>:content_type</tt> option:</p>
373
- <div class="code_container">
374
- <pre class="brush: ruby; gutter: false; toolbar: false">
375
- render :file =&gt; filename, :content_type =&gt; 'application/rss'
376
- </pre>
377
- </div>
378
- <h6 id="the-layout-option">2.2.11.2 The <tt>:layout</tt> Option</h6>
379
- <p>With most of the options to <tt>render</tt>, the rendered content is displayed as part of the current layout. You&#8217;ll learn more about layouts and how to use them later in this guide.</p>
380
- <p>You can use the <tt>:layout</tt> option to tell Rails to use a specific file as the layout for the current action:</p>
381
- <div class="code_container">
382
- <pre class="brush: ruby; gutter: false; toolbar: false">
383
- render :layout =&gt; 'special_layout'
384
- </pre>
385
- </div>
386
- <p>You can also tell Rails to render with no layout at all:</p>
387
- <div class="code_container">
388
- <pre class="brush: ruby; gutter: false; toolbar: false">
389
- render :layout =&gt; false
390
- </pre>
391
- </div>
392
- <h6 id="the-status-option">2.2.11.3 The <tt>:status</tt> Option</h6>
393
- <p>Rails will automatically generate a response with the correct <span class="caps">HTTP</span> status code (in most cases, this is <tt>200 OK</tt>). You can use the <tt>:status</tt> option to change this:</p>
394
- <div class="code_container">
395
- <pre class="brush: ruby; gutter: false; toolbar: false">
396
- render :status =&gt; 500
397
- render :status =&gt; :forbidden
398
- </pre>
399
- </div>
400
- <p>Rails understands both numeric and symbolic status codes.</p>
401
- <h6 id="the-location-option">2.2.11.4 The <tt>:location</tt> Option</h6>
402
- <p>You can use the <tt>:location</tt> option to set the <span class="caps">HTTP</span> <tt>Location</tt> header:</p>
403
- <div class="code_container">
404
- <pre class="brush: ruby; gutter: false; toolbar: false">
405
- render :xml =&gt; photo, :location =&gt; photo_url(photo)
406
- </pre>
407
- </div>
408
- <h5 id="finding-layouts">2.2.12 Finding Layouts</h5>
409
- <p>To find the current layout, Rails first looks for a file in <tt>app/views/layouts</tt> with the same base name as the controller. For example, rendering actions from the <tt>PhotosController</tt> class will use <tt>app/views/layouts/photos.html.erb</tt> (or <tt>app/views/layouts/photos.builder</tt>). If there is no such controller-specific layout, Rails will use <tt>app/views/layouts/application.html.erb</tt> or <tt>app/views/layouts/application.builder</tt>. If there is no <tt>.erb</tt> layout, Rails will use a <tt>.builder</tt> layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.</p>
410
- <h6 id="specifying-layouts-for-controllers">2.2.12.1 Specifying Layouts for Controllers</h6>
411
- <p>You can override the default layout conventions in your controllers by using the <tt>layout</tt> declaration. For example:</p>
412
- <div class="code_container">
413
- <pre class="brush: ruby; gutter: false; toolbar: false">
414
- class ProductsController &lt; ApplicationController
415
- layout &quot;inventory&quot;
416
- #...
417
- end
418
- </pre>
419
- </div>
420
- <p>With this declaration, all of the methods within <tt>ProductsController</tt> will use <tt>app/views/layouts/inventory.html.erb</tt> for their layout.</p>
421
- <p>To assign a specific layout for the entire application, use a <tt>layout</tt> declaration in your <tt>ApplicationController</tt> class:</p>
422
- <div class="code_container">
423
- <pre class="brush: ruby; gutter: false; toolbar: false">
424
- class ApplicationController &lt; ActionController::Base
425
- layout &quot;main&quot;
426
- #...
427
- end
428
- </pre>
429
- </div>
430
- <p>With this declaration, all of the views in the entire application will use <tt>app/views/layouts/main.html.erb</tt> for their layout.</p>
431
- <h6 id="choosing-layouts-at-runtime">2.2.12.2 Choosing Layouts at Runtime</h6>
432
- <p>You can use a symbol to defer the choice of layout until a request is processed:</p>
433
- <div class="code_container">
434
- <pre class="brush: ruby; gutter: false; toolbar: false">
435
- class ProductsController &lt; ApplicationController
436
- layout :products_layout
437
-
438
- def show
439
- @product = Product.find(params[:id])
440
- end
441
-
442
- private
443
- def products_layout
444
- @current_user.special? ? &quot;special&quot; : &quot;products&quot;
445
- end
446
-
447
- end
448
- </pre>
449
- </div>
450
- <p>Now, if the current user is a special user, they&#8217;ll get a special layout when viewing a product.</p>
451
- <p>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 <tt>controller</tt> instance, so the layout can be determined based on the current request:</p>
452
- <div class="code_container">
453
- <pre class="brush: ruby; gutter: false; toolbar: false">
454
- class ProductsController &lt; ApplicationController
455
- layout Proc.new { |controller| controller.request.xhr? ? 'popup' : 'application' }
456
- end
457
- </pre>
458
- </div>
459
- <h6 id="conditional-layouts">2.2.12.3 Conditional Layouts</h6>
460
- <p>Layouts specified at the controller level support the <tt>:only</tt> and <tt>:except</tt> options. These options take either a method name, or an array of method names, corresponding to method names within the controller:</p>
461
- <div class="code_container">
462
- <pre class="brush: ruby; gutter: false; toolbar: false">
463
- class ProductsController &lt; ApplicationController
464
- layout &quot;product&quot;, :except =&gt; [:index, :rss]
465
- end
466
- </pre>
467
- </div>
468
- <p>With this declaration, the <tt>product</tt> layout would be used for everything but the <tt>rss</tt> and <tt>index</tt> methods.</p>
469
- <h6 id="layout-inheritance">2.2.12.4 Layout Inheritance</h6>
470
- <p>Layout declarations cascade downward in the hierarchy, and more specific layout declarations always override more general ones. For example:</p>
471
- <ul>
472
- <li><tt>application_controller.rb</tt></li>
473
- </ul>
474
- <div class="code_container">
475
- <pre class="brush: ruby; gutter: false; toolbar: false">
476
- class ApplicationController &lt; ActionController::Base
477
- layout &quot;main&quot;
478
- end
479
- </pre>
480
- </div>
481
- <ul>
482
- <li><tt>posts_controller.rb</tt></li>
483
- </ul>
484
- <div class="code_container">
485
- <pre class="brush: ruby; gutter: false; toolbar: false">
486
- class PostsController &lt; ApplicationController
487
- end
488
- </pre>
489
- </div>
490
- <ul>
491
- <li><tt>special_posts_controller.rb</tt></li>
492
- </ul>
493
- <div class="code_container">
494
- <pre class="brush: ruby; gutter: false; toolbar: false">
495
- class SpecialPostsController &lt; PostsController
496
- layout &quot;special&quot;
497
- end
498
- </pre>
499
- </div>
500
- <ul>
501
- <li><tt>old_posts_controller.rb</tt></li>
502
- </ul>
503
- <div class="code_container">
504
- <pre class="brush: ruby; gutter: false; toolbar: false">
505
- class OldPostsController &lt; SpecialPostsController
506
- layout nil
507
-
508
- def show
509
- @post = Post.find(params[:id])
510
- end
511
-
512
- def index
513
- @old_posts = Post.older
514
- render :layout =&gt; &quot;old&quot;
515
- end
516
- # ...
517
- end
518
- </pre>
519
- </div>
520
- <p>In this application:</p>
521
- <ul>
522
- <li>In general, views will be rendered in the <tt>main</tt> layout</li>
523
- <li><tt>PostsController#index</tt> will use the <tt>main</tt> layout</li>
524
- <li><tt>SpecialPostsController#index</tt> will use the <tt>special</tt> layout</li>
525
- <li><tt>OldPostsController#show</tt> will use no layout at all</li>
526
- <li><tt>OldPostsController#index</tt> will use the <tt>old</tt> layout</li>
527
- </ul>
528
- <h5 id="avoiding-double-render-errors">2.2.13 Avoiding Double Render Errors</h5>
529
- <p>Sooner or later, most Rails developers will see the error message &#8220;Can only render or redirect once per action&#8221;. While this is annoying, it&#8217;s relatively easy to fix. Usually it happens because of a fundamental misunderstanding of the way that <tt>render</tt> works.</p>
530
- <p>For example, here&#8217;s some code that will trigger this error:</p>
531
- <div class="code_container">
532
- <pre class="brush: ruby; gutter: false; toolbar: false">
533
- def show
534
- @book = Book.find(params[:id])
535
- if @book.special?
536
- render :action =&gt; &quot;special_show&quot;
537
- end
538
- render :action =&gt; &quot;regular_show&quot;
539
- end
540
- </pre>
541
- </div>
542
- <p>If <tt>@book.special?</tt> evaluates to <tt>true</tt>, Rails will start the rendering process to dump the <tt>@book</tt> variable into the <tt>special_show</tt> view. But this will <em>not</em> stop the rest of the code in the <tt>show</tt> action from running, and when Rails hits the end of the action, it will start to render the <tt>regular_show</tt> view &#8211; and throw an error. The solution is simple: make sure that you have only one call to <tt>render</tt> or <tt>redirect</tt> in a single code path. One thing that can help is <tt>and return</tt>. Here&#8217;s a patched version of the method:</p>
543
- <div class="code_container">
544
- <pre class="brush: ruby; gutter: false; toolbar: false">
545
- def show
546
- @book = Book.find(params[:id])
547
- if @book.special?
548
- render :action =&gt; &quot;special_show&quot; and return
549
- end
550
- render :action =&gt; &quot;regular_show&quot;
551
- end
552
- </pre>
553
- </div>
554
- <p>Make sure to use <tt>and return</tt> and not <tt>&amp;&amp; return</tt>, since <tt>&amp;&amp; return</tt> will not work due to the operator precedence in the Ruby Language.</p>
555
- <p>Note that the implicit render done by ActionController detects if <tt>render</tt> has been called, so the following will work without errors:</p>
556
- <div class="code_container">
557
- <pre class="brush: ruby; gutter: false; toolbar: false">
558
- def show
559
- @book = Book.find(params[:id])
560
- if @book.special?
561
- render :action =&gt; &quot;special_show&quot;
562
- end
563
- end
564
- </pre>
565
- </div>
566
- <p>This will render a book with <tt>special?</tt> set with the <tt>special_show</tt> template, while other books will render with the default <tt>show</tt> template.</p>
567
- <h4 id="using-redirect_to">2.3 Using <tt>redirect_to</tt></h4>
568
- <p>Another way to handle returning responses to an <span class="caps">HTTP</span> request is with <tt>redirect_to</tt>. As you&#8217;ve seen, <tt>render</tt> tells Rails which view (or other asset) to use in constructing a response. The <tt>redirect_to</tt> method does something completely different: it tells the browser to send a new request for a different <span class="caps">URL</span>. For example, you could redirect from wherever you are in your code to the index of photos in your application with this call:</p>
569
- <div class="code_container">
570
- <pre class="brush: ruby; gutter: false; toolbar: false">
571
- redirect_to photos_url
572
- </pre>
573
- </div>
574
- <p>You can use <tt>redirect_to</tt> with any arguments that you could use with <tt>link_to</tt> or <tt>url_for</tt>. There&#8217;s also a special redirect that sends the user back to the page they just came from:</p>
575
- <div class="code_container">
576
- <pre class="brush: ruby; gutter: false; toolbar: false">
577
- redirect_to :back
578
- </pre>
579
- </div>
580
- <h5 id="getting-a-different-redirect-status-code">2.3.1 Getting a Different Redirect Status Code</h5>
581
- <p>Rails uses <span class="caps">HTTP</span> status code 302, a temporary redirect, when you call <tt>redirect_to</tt>. If you&#8217;d like to use a different status code, perhaps 301, a permanent redirect, you can use the <tt>:status</tt> option:</p>
582
- <div class="code_container">
583
- <pre class="brush: ruby; gutter: false; toolbar: false">
584
- redirect_to photos_path, :status =&gt; 301
585
- </pre>
586
- </div>
587
- <p>Just like the <tt>:status</tt> option for <tt>render</tt>, <tt>:status</tt> for <tt>redirect_to</tt> accepts both numeric and symbolic header designations.</p>
588
- <h5 id="the-difference-between-render-and-redirect_to">2.3.2 The Difference Between <tt>render</tt> and <tt>redirect_to</tt></h5>
589
- <p>Sometimes inexperienced developers think of <tt>redirect_to</tt> as a sort of <tt>goto</tt> command, moving execution from one place to another in your Rails code. This is <em>not</em> correct. Your code stops running and waits for a new request for the browser. It just happens that you&#8217;ve told the browser what request it should make next, by sending back an <span class="caps">HTTP</span> 302 status code.</p>
590
- <p>Consider these actions to see the difference:</p>
591
- <div class="code_container">
592
- <pre class="brush: ruby; gutter: false; toolbar: false">
593
- def index
594
- @books = Book.all
595
- end
596
-
597
- def show
598
- @book = Book.find_by_id(params[:id])
599
- if @book.nil?
600
- render :action =&gt; &quot;index&quot;
601
- end
602
- end
603
- </pre>
604
- </div>
605
- <p>With the code in this form, there will likely be a problem if the <tt>@book</tt> variable is <tt>nil</tt>. Remember, a <tt>render :action</tt> doesn&#8217;t run any code in the target action, so nothing will set up the <tt>@books</tt> variable that the <tt>index</tt> view will probably require. One way to fix this is to redirect instead of rendering:</p>
606
- <div class="code_container">
607
- <pre class="brush: ruby; gutter: false; toolbar: false">
608
- def index
609
- @books = Book.all
610
- end
611
-
612
- def show
613
- @book = Book.find_by_id(params[:id])
614
- if @book.nil?
615
- redirect_to :action =&gt; :index
616
- end
617
- end
618
- </pre>
619
- </div>
620
- <p>With this code, the browser will make a fresh request for the index page, the code in the <tt>index</tt> method will run, and all will be well.</p>
621
- <p>The only downside to this code is that it requires a round trip to the browser: the browser requested the show action with <tt>/books/1</tt> 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 <tt>/books/</tt>, the browser complies and sends a new request back to the controller asking now for the <tt>index</tt> 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.</p>
622
- <p>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:</p>
623
- <div class="code_container">
624
- <pre class="brush: ruby; gutter: false; toolbar: false">
625
- def index
626
- @books = Book.all
627
- end
628
-
629
- def show
630
- @book = Book.find_by_id(params[:id])
631
- if @book.nil?
632
- @books = Book.all
633
- render &quot;index&quot;, :alert =&gt; 'Your book was not found!'
634
- end
635
- end
636
- </pre>
637
- </div>
638
- <p>This would detect that there are no books with the specified ID, populate the <tt>@books</tt> instance variable with all the books in the model, and then directly render the <tt>index.html.erb</tt> template, returning it to the browser with a flash alert message to tell the user what happened.</p>
639
- <h4 id="using-head-to-build-header-only-responses">2.4 Using <tt>head</tt> To Build Header-Only Responses</h4>
640
- <p>The <tt>head</tt> method can be used to send responses with only headers to the browser. It provides a more obvious alternative to calling <tt>render :nothing</tt>. The <tt>head</tt> method takes one parameter, which is interpreted as a hash of header names and values. For example, you can return only an error header:</p>
641
- <div class="code_container">
642
- <pre class="brush: ruby; gutter: false; toolbar: false">
643
- head :bad_request
644
- </pre>
645
- </div>
646
- <p>This would produce the following header:</p>
647
- <div class="code_container">
648
- <pre class="brush: plain; gutter: false; toolbar: false">
649
- HTTP/1.1 400 Bad Request
650
- Connection: close
651
- Date: Sun, 24 Jan 2010 12:15:53 GMT
652
- Transfer-Encoding: chunked
653
- Content-Type: text/html; charset=utf-8
654
- X-Runtime: 0.013483
655
- Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
656
- Cache-Control: no-cache
657
- </pre>
658
- </div>
659
- <p>Or you can use other <span class="caps">HTTP</span> headers to convey other information:</p>
660
- <div class="code_container">
661
- <pre class="brush: ruby; gutter: false; toolbar: false">
662
- head :created, :location =&gt; photo_path(@photo)
663
- </pre>
664
- </div>
665
- <p>Which would produce:</p>
666
- <div class="code_container">
667
- <pre class="brush: plain; gutter: false; toolbar: false">
668
- HTTP/1.1 201 Created
669
- Connection: close
670
- Date: Sun, 24 Jan 2010 12:16:44 GMT
671
- Transfer-Encoding: chunked
672
- Location: /photos/1
673
- Content-Type: text/html; charset=utf-8
674
- X-Runtime: 0.083496
675
- Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
676
- Cache-Control: no-cache
677
- </pre>
678
- </div>
679
- <h3 id="structuring-layouts">3 Structuring Layouts</h3>
680
- <p>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:</p>
681
- <ul>
682
- <li>Asset tags</li>
683
- <li><tt>yield</tt> and <tt>content_for</tt></li>
684
- <li>Partials</li>
685
- </ul>
686
- <h4 id="asset-tag-helpers">3.1 Asset Tag Helpers</h4>
687
- <p>Asset tag helpers provide methods for generating <span class="caps">HTML</span> that link views to feeds, JavaScript, stylesheets, images, videos and audios. There are six asset tag helpers available in Rails:</p>
688
- <ul>
689
- <li><tt>auto_discovery_link_tag</tt></li>
690
- <li><tt>javascript_include_tag</tt></li>
691
- <li><tt>stylesheet_link_tag</tt></li>
692
- <li><tt>image_tag</tt></li>
693
- <li><tt>video_tag</tt></li>
694
- <li><tt>audio_tag</tt></li>
695
- </ul>
696
- <p>You can use these tags in layouts or other views, although the <tt>auto_discovery_link_tag</tt>, <tt>javascript_include_tag</tt>, and <tt>stylesheet_link_tag</tt>, are most commonly used in the <tt>&lt;head&gt;</tt> section of a layout.</p>
697
- <div class="warning"><p>The asset tag helpers do <em>not</em> verify the existence of the assets at the specified locations; they simply assume that you know what you&#8217;re doing and generate the link.</p></div>
698
- <h5 id="linking-to-feeds-with-the-auto_discovery_link_tag">3.1.1 Linking to Feeds with the <tt>auto_discovery_link_tag</tt></h5>
699
- <p>The <tt>auto_discovery_link_tag</tt> helper builds <span class="caps">HTML</span> that most browsers and newsreaders can use to detect the presences of <span class="caps">RSS</span> or <span class="caps">ATOM</span> feeds. It takes the type of the link (<tt>:rss</tt> or <tt>:atom</tt>), a hash of options that are passed through to url_for, and a hash of options for the tag:</p>
700
- <div class="code_container">
701
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
702
- &lt;%= auto_discovery_link_tag(:rss, {:action =&gt; &quot;feed&quot;},
703
- {:title =&gt; &quot;RSS Feed&quot;}) %&gt;
704
- </pre>
705
- </div>
706
- <p>There are three tag options available for the <tt>auto_discovery_link_tag</tt>:</p>
707
- <ul>
708
- <li><tt>:rel</tt> specifies the <tt>rel</tt> value in the link. The default value is &#8220;alternate&#8221;.</li>
709
- <li><tt>:type</tt> specifies an explicit <span class="caps">MIME</span> type. Rails will generate an appropriate <span class="caps">MIME</span> type automatically.</li>
710
- <li><tt>:title</tt> specifies the title of the link. The default value is the upshifted <tt>:type</tt> value, for example, &#8220;<span class="caps">ATOM</span>&#8221; or &#8220;<span class="caps">RSS</span>&#8221;.</li>
711
- </ul>
712
- <h5 id="linking-to-javascript-files-with-the-javascript_include_tag">3.1.2 Linking to JavaScript Files with the <tt>javascript_include_tag</tt></h5>
713
- <p>The <tt>javascript_include_tag</tt> helper returns an <span class="caps">HTML</span> <tt>script</tt> tag for each source provided. Rails looks in <tt>public/javascripts</tt> for these files by default, but you can specify a full path relative to the document root, or a <span class="caps">URL</span>, if you prefer. For example, to include <tt>public/javascripts/main.js</tt>:</p>
714
- <div class="code_container">
715
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
716
- &lt;%= javascript_include_tag &quot;main&quot; %&gt;
717
- </pre>
718
- </div>
719
- <p>To include <tt>public/javascripts/main.js</tt> and <tt>public/javascripts/columns.js</tt>:</p>
720
- <div class="code_container">
721
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
722
- &lt;%= javascript_include_tag &quot;main&quot;, &quot;columns&quot; %&gt;
723
- </pre>
724
- </div>
725
- <p>To include <tt>public/javascripts/main.js</tt> and <tt>public/photos/columns.js</tt>:</p>
726
- <div class="code_container">
727
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
728
- &lt;%= javascript_include_tag &quot;main&quot;, &quot;/photos/columns&quot; %&gt;
729
- </pre>
730
- </div>
731
- <p>To include <tt>http://example.com/main.js</tt>:</p>
732
- <div class="code_container">
733
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
734
- &lt;%= javascript_include_tag &quot;http://example.com/main.js&quot; %&gt;
735
- </pre>
736
- </div>
737
- <p>If the application does not use the asset pipeline, the <tt>:defaults</tt> option loads jQuery by default:</p>
738
- <div class="code_container">
739
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
740
- &lt;%= javascript_include_tag :defaults %&gt;
741
- </pre>
742
- </div>
743
- <p>And you can in any case override the expansion in <tt>config/application.rb</tt>:</p>
744
- <div class="code_container">
745
- <pre class="brush: ruby; gutter: false; toolbar: false">
746
- config.action_view.javascript_expansions[:defaults] = %w(foo.js bar.js)
747
- </pre>
748
- </div>
749
- <p>When using <tt>:defaults</tt>, if an <tt>application.js</tt> file exists in <tt>public/javascripts</tt> it will be included as well at then end.</p>
750
- <p>Also, the <tt>:all</tt> option loads every JavaScript file in <tt>public/javascripts</tt>:</p>
751
- <div class="code_container">
752
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
753
- &lt;%= javascript_include_tag :all %&gt;
754
- </pre>
755
- </div>
756
- <p>Note that your defaults of choice will be included first, so they will be available to all subsequently included files.</p>
757
- <p>You can supply the <tt>:recursive</tt> option to load files in subfolders of <tt>public/javascripts</tt> as well:</p>
758
- <div class="code_container">
759
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
760
- &lt;%= javascript_include_tag :all, :recursive =&gt; true %&gt;
761
- </pre>
762
- </div>
763
- <p>If you&#8217;re loading multiple JavaScript files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify <tt>:cache => true</tt> in your <tt>javascript_include_tag</tt>:</p>
764
- <div class="code_container">
765
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
766
- &lt;%= javascript_include_tag &quot;main&quot;, &quot;columns&quot;, :cache =&gt; true %&gt;
767
- </pre>
768
- </div>
769
- <p>By default, the combined file will be delivered as <tt>javascripts/all.js</tt>. You can specify a location for the cached asset file instead:</p>
770
- <div class="code_container">
771
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
772
- &lt;%= javascript_include_tag &quot;main&quot;, &quot;columns&quot;,
773
- :cache =&gt; 'cache/main/display' %&gt;
774
- </pre>
775
- </div>
776
- <p>You can even use dynamic paths such as <tt>cache/#{current_site}/main/display</tt>.</p>
777
- <h5 id="linking-to-css-files-with-the-stylesheet_link_tag">3.1.3 Linking to <span class="caps">CSS</span> Files with the <tt>stylesheet_link_tag</tt></h5>
778
- <p>The <tt>stylesheet_link_tag</tt> helper returns an <span class="caps">HTML</span> <tt>&lt;link&gt;</tt> tag for each source provided. Rails looks in <tt>public/stylesheets</tt> for these files by default, but you can specify a full path relative to the document root, or a <span class="caps">URL</span>, if you prefer. For example, to include <tt>public/stylesheets/main.css</tt>:</p>
779
- <div class="code_container">
780
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
781
- &lt;%= stylesheet_link_tag &quot;main&quot; %&gt;
782
- </pre>
783
- </div>
784
- <p>To include <tt>public/stylesheets/main.css</tt> and <tt>public/stylesheets/columns.css</tt>:</p>
785
- <div class="code_container">
786
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
787
- &lt;%= stylesheet_link_tag &quot;main&quot;, &quot;columns&quot; %&gt;
788
- </pre>
789
- </div>
790
- <p>To include <tt>public/stylesheets/main.css</tt> and <tt>public/photos/columns.css</tt>:</p>
791
- <div class="code_container">
792
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
793
- &lt;%= stylesheet_link_tag &quot;main&quot;, &quot;/photos/columns&quot; %&gt;
794
- </pre>
795
- </div>
796
- <p>To include <tt>http://example.com/main.css</tt>:</p>
797
- <div class="code_container">
798
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
799
- &lt;%= stylesheet_link_tag &quot;http://example.com/main.css&quot; %&gt;
800
- </pre>
801
- </div>
802
- <p>By default, the <tt>stylesheet_link_tag</tt> creates links with <tt>media="screen" rel="stylesheet" type="text/css"</tt>. You can override any of these defaults by specifying an appropriate option (<tt>:media</tt>, <tt>:rel</tt>, or <tt>:type</tt>):</p>
803
- <div class="code_container">
804
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
805
- &lt;%= stylesheet_link_tag &quot;main_print&quot;, :media =&gt; &quot;print&quot; %&gt;
806
- </pre>
807
- </div>
808
- <p>The <tt>all</tt> option links every <span class="caps">CSS</span> file in <tt>public/stylesheets</tt>:</p>
809
- <div class="code_container">
810
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
811
- &lt;%= stylesheet_link_tag :all %&gt;
812
- </pre>
813
- </div>
814
- <p>You can supply the <tt>:recursive</tt> option to link files in subfolders of <tt>public/stylesheets</tt> as well:</p>
815
- <div class="code_container">
816
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
817
- &lt;%= stylesheet_link_tag :all, :recursive =&gt; true %&gt;
818
- </pre>
819
- </div>
820
- <p>If you&#8217;re loading multiple <span class="caps">CSS</span> files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify <tt>:cache => true</tt> in your <tt>stylesheet_link_tag</tt>:</p>
821
- <div class="code_container">
822
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
823
- &lt;%= stylesheet_link_tag &quot;main&quot;, &quot;columns&quot;, :cache =&gt; true %&gt;
824
- </pre>
825
- </div>
826
- <p>By default, the combined file will be delivered as <tt>stylesheets/all.css</tt>. You can specify a location for the cached asset file instead:</p>
827
- <div class="code_container">
828
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
829
- &lt;%= stylesheet_link_tag &quot;main&quot;, &quot;columns&quot;,
830
- :cache =&gt; 'cache/main/display' %&gt;
831
- </pre>
832
- </div>
833
- <p>You can even use dynamic paths such as <tt>cache/#{current_site}/main/display</tt>.</p>
834
- <h5 id="linking-to-images-with-the-image_tag">3.1.4 Linking to Images with the <tt>image_tag</tt></h5>
835
- <p>The <tt>image_tag</tt> helper builds an <span class="caps">HTML</span> <tt>&lt;img /&gt;</tt> tag to the specified file. By default, files are loaded from <tt>public/images</tt>.</p>
836
- <div class="warning"><p>Note that you must specify the extension of the image. Previous versions of Rails would allow you to just use the image name and would append <tt>.png</tt> if no extension was given but Rails 3.0 does not.</p></div>
837
- <div class="code_container">
838
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
839
- &lt;%= image_tag &quot;header.png&quot; %&gt;
840
- </pre>
841
- </div>
842
- <p>You can supply a path to the image if you like:</p>
843
- <div class="code_container">
844
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
845
- &lt;%= image_tag &quot;icons/delete.gif&quot; %&gt;
846
- </pre>
847
- </div>
848
- <p>You can supply a hash of additional <span class="caps">HTML</span> options:</p>
849
- <div class="code_container">
850
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
851
- &lt;%= image_tag &quot;icons/delete.gif&quot;, {:height =&gt; 45} %&gt;
852
- </pre>
853
- </div>
854
- <p>You can also supply an alternate image to show on mouseover:</p>
855
- <div class="code_container">
856
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
857
- &lt;%= image_tag &quot;home.gif&quot;, :onmouseover =&gt; &quot;menu/home_highlight.gif&quot; %&gt;
858
- </pre>
859
- </div>
860
- <p>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:</p>
861
- <div class="code_container">
862
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
863
- &lt;%= image_tag &quot;home.gif&quot; %&gt;
864
- &lt;%= image_tag &quot;home.gif&quot;, :alt =&gt; &quot;Home&quot; %&gt;
865
- </pre>
866
- </div>
867
- <p>You can also specify a special size tag, in the format &#8220;{width}x{height}&#8221;:</p>
868
- <div class="code_container">
869
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
870
- &lt;%= image_tag &quot;home.gif&quot;, :size =&gt; &quot;50x20&quot; %&gt;
871
- </pre>
872
- </div>
873
- <p>In addition to the above special tags, you can supply a final hash of standard <span class="caps">HTML</span> options, such as <tt>:class</tt>, <tt>:id</tt> or <tt>:name</tt>:</p>
874
- <div class="code_container">
875
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
876
- &lt;%= image_tag &quot;home.gif&quot;, :alt =&gt; &quot;Go Home&quot;,
877
- :id =&gt; &quot;HomeImage&quot;,
878
- :class =&gt; 'nav_bar' %&gt;
879
- </pre>
880
- </div>
881
- <h5 id="linking-to-videos-with-the-video_tag">3.1.5 Linking to Videos with the <tt>video_tag</tt></h5>
882
- <p>The <tt>video_tag</tt> helper builds an <span class="caps">HTML</span> 5 <tt>&lt;video&gt;</tt> tag to the specified file. By default, files are loaded from <tt>public/videos</tt>.</p>
883
- <div class="code_container">
884
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
885
- &lt;%= video_tag &quot;movie.ogg&quot; %&gt;
886
- </pre>
887
- </div>
888
- <p>Produces</p>
889
- <div class="code_container">
890
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
891
- &lt;video src=&quot;/videos/movie.ogg&quot; /&gt;
892
- </pre>
893
- </div>
894
- <p>Like an <tt>image_tag</tt> you can supply a path, either absolute, or relative to the <tt>public/videos</tt> directory. Additionally you can specify the <tt>:size => "#{width}x#{height}"</tt> option just like an <tt>image_tag</tt>. Video tags can also have any of the <span class="caps">HTML</span> options specified at the end (<tt>id</tt>, <tt>class</tt> et al).</p>
895
- <p>The video tag also supports all of the <tt>&lt;video&gt;</tt> <span class="caps">HTML</span> options through the <span class="caps">HTML</span> options hash, including:</p>
896
- <ul>
897
- <li><tt>:poster => 'image_name.png'</tt>, provides an image to put in place of the video before it starts playing.</li>
898
- <li><tt>:autoplay => true</tt>, starts playing the video on page load.</li>
899
- <li><tt>:loop => true</tt>, loops the video once it gets to the end.</li>
900
- <li><tt>:controls => true</tt>, provides browser supplied controls for the user to interact with the video.</li>
901
- <li><tt>:autobuffer => true</tt>, the video will pre load the file for the user on page load.</li>
902
- </ul>
903
- <p>You can also specify multiple videos to play by passing an array of videos to the <tt>video_tag</tt>:</p>
904
- <div class="code_container">
905
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
906
- &lt;%= video_tag [&quot;trailer.ogg&quot;, &quot;movie.ogg&quot;] %&gt;
907
- </pre>
908
- </div>
909
- <p>This will produce:</p>
910
- <div class="code_container">
911
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
912
- &lt;video&gt;&lt;source src=&quot;trailer.ogg&quot; /&gt;&lt;source src=&quot;movie.ogg&quot; /&gt;&lt;/video&gt;
913
- </pre>
914
- </div>
915
- <h5 id="linking-to-audio-files-with-the-audio_tag">3.1.6 Linking to Audio Files with the <tt>audio_tag</tt></h5>
916
- <p>The <tt>audio_tag</tt> helper builds an <span class="caps">HTML</span> 5 <tt>&lt;audio&gt;</tt> tag to the specified file. By default, files are loaded from <tt>public/audios</tt>.</p>
917
- <div class="code_container">
918
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
919
- &lt;%= audio_tag &quot;music.mp3&quot; %&gt;
920
- </pre>
921
- </div>
922
- <p>You can supply a path to the audio file if you like:</p>
923
- <div class="code_container">
924
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
925
- &lt;%= audio_tag &quot;music/first_song.mp3&quot; %&gt;
926
- </pre>
927
- </div>
928
- <p>You can also supply a hash of additional options, such as <tt>:id</tt>, <tt>:class</tt> etc.</p>
929
- <p>Like the <tt>video_tag</tt>, the <tt>audio_tag</tt> has special options:</p>
930
- <ul>
931
- <li><tt>:autoplay => true</tt>, starts playing the audio on page load</li>
932
- <li><tt>:controls => true</tt>, provides browser supplied controls for the user to interact with the audio.</li>
933
- <li><tt>:autobuffer => true</tt>, the audio will pre load the file for the user on page load.</li>
934
- </ul>
935
- <h4 id="understanding-yield">3.2 Understanding <tt>yield</tt></h4>
936
- <p>Within the context of a layout, <tt>yield</tt> identifies a section where content from the view should be inserted. The simplest way to use this is to have a single <tt>yield</tt>, into which the entire contents of the view currently being rendered is inserted:</p>
937
- <div class="code_container">
938
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
939
- &lt;html&gt;
940
- &lt;head&gt;
941
- &lt;/head&gt;
942
- &lt;body&gt;
943
- &lt;%= yield %&gt;
944
- &lt;/body&gt;
945
- &lt;/html&gt;
946
- </pre>
947
- </div>
948
- <p>You can also create a layout with multiple yielding regions:</p>
949
- <div class="code_container">
950
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
951
- &lt;html&gt;
952
- &lt;head&gt;
953
- &lt;%= yield :head %&gt;
954
- &lt;/head&gt;
955
- &lt;body&gt;
956
- &lt;%= yield %&gt;
957
- &lt;/body&gt;
958
- &lt;/html&gt;
959
- </pre>
960
- </div>
961
- <p>The main body of the view will always render into the unnamed <tt>yield</tt>. To render content into a named <tt>yield</tt>, you use the <tt>content_for</tt> method.</p>
962
- <h4 id="using-the-content_for-method">3.3 Using the <tt>content_for</tt> Method</h4>
963
- <p>The <tt>content_for</tt> method allows you to insert content into a named <tt>yield</tt> block in your layout. For example, this view would work with the layout that you just saw:</p>
964
- <div class="code_container">
965
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
966
- &lt;% content_for :head do %&gt;
967
- &lt;title&gt;A simple page&lt;/title&gt;
968
- &lt;% end %&gt;
969
-
970
- &lt;p&gt;Hello, Rails!&lt;/p&gt;
971
- </pre>
972
- </div>
973
- <p>The result of rendering this page into the supplied layout would be this <span class="caps">HTML</span>:</p>
974
- <div class="code_container">
975
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
976
- &lt;html&gt;
977
- &lt;head&gt;
978
- &lt;title&gt;A simple page&lt;/title&gt;
979
- &lt;/head&gt;
980
- &lt;body&gt;
981
- &lt;p&gt;Hello, Rails!&lt;/p&gt;
982
- &lt;/body&gt;
983
- &lt;/html&gt;
984
- </pre>
985
- </div>
986
- <p>The <tt>content_for</tt> 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&#8217;s also useful for inserting tags that load page-specific JavaScript or css files into the header of an otherwise generic layout.</p>
987
- <h4 id="using-partials">3.4 Using Partials</h4>
988
- <p>Partial templates &#8211; usually just called &#8220;partials&#8221; &#8211; 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.</p>
989
- <h5 id="naming-partials">3.4.1 Naming Partials</h5>
990
- <p>To render a partial as part of a view, you use the <tt>render</tt> method within the view:</p>
991
- <div class="code_container">
992
- <pre class="brush: ruby; gutter: false; toolbar: false">
993
- &lt;%= render &quot;menu&quot; %&gt;
994
- </pre>
995
- </div>
996
- <p>This will render a file named <tt>_menu.html.erb</tt> 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&#8217;re pulling in a partial from another folder:</p>
997
- <div class="code_container">
998
- <pre class="brush: ruby; gutter: false; toolbar: false">
999
- &lt;%= render &quot;shared/menu&quot; %&gt;
1000
- </pre>
1001
- </div>
1002
- <p>That code will pull in the partial from <tt>app/views/shared/_menu.html.erb</tt>.</p>
1003
- <h5 id="using-partials-to-simplify-views">3.4.2 Using Partials to Simplify Views</h5>
1004
- <p>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&#8217;s going on more easily. For example, you might have a view that looked like this:</p>
1005
- <div class="code_container">
1006
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1007
- &lt;%= render &quot;shared/ad_banner&quot; %&gt;
1008
-
1009
- &lt;h1&gt;Products&lt;/h1&gt;
1010
-
1011
- &lt;p&gt;Here are a few of our fine products:&lt;/p&gt;
1012
- ...
1013
-
1014
- &lt;%= render &quot;shared/footer&quot; %&gt;
1015
- </pre>
1016
- </div>
1017
- <p>Here, the <tt>_ad_banner.html.erb</tt> and <tt>_footer.html.erb</tt> partials could contain content that is shared among many pages in your application. You don&#8217;t need to see the details of these sections when you&#8217;re concentrating on a particular page.</p>
1018
- <div class="info"><p>For content that is shared among all pages in your application, you can use partials directly from layouts.</p></div>
1019
- <h5 id="partial-layouts">3.4.3 Partial Layouts</h5>
1020
- <p>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:</p>
1021
- <div class="code_container">
1022
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1023
- &lt;%= render :partial =&gt; &quot;link_area&quot;, :layout =&gt; &quot;graybar&quot; %&gt;
1024
- </pre>
1025
- </div>
1026
- <p>This would look for a partial named <tt>_link_area.html.erb</tt> and render it using the layout <tt>_graybar.html.erb</tt>. 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 <tt>layouts</tt> folder).</p>
1027
- <p>Also note that explicitly specifying <tt>:partial</tt> is required when passing additional options such as <tt>:layout</tt>.</p>
1028
- <h5 id="passing-local-variables">3.4.4 Passing Local Variables</h5>
1029
- <p>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:</p>
1030
- <ul>
1031
- <li><tt>new.html.erb</tt></li>
1032
- </ul>
1033
- <div class="code_container">
1034
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1035
- &lt;h1&gt;New zone&lt;/h1&gt;
1036
- &lt;%= error_messages_for :zone %&gt;
1037
- &lt;%= render :partial =&gt; &quot;form&quot;, :locals =&gt; { :zone =&gt; @zone } %&gt;
1038
- </pre>
1039
- </div>
1040
- <ul>
1041
- <li><tt>edit.html.erb</tt></li>
1042
- </ul>
1043
- <div class="code_container">
1044
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1045
- &lt;h1&gt;Editing zone&lt;/h1&gt;
1046
- &lt;%= error_messages_for :zone %&gt;
1047
- &lt;%= render :partial =&gt; &quot;form&quot;, :locals =&gt; { :zone =&gt; @zone } %&gt;
1048
- </pre>
1049
- </div>
1050
- <ul>
1051
- <li><tt>_form.html.erb</tt></li>
1052
- </ul>
1053
- <div class="code_container">
1054
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1055
- &lt;%= form_for(zone) do |f| %&gt;
1056
- &lt;p&gt;
1057
- &lt;b&gt;Zone name&lt;/b&gt;&lt;br /&gt;
1058
- &lt;%= f.text_field :name %&gt;
1059
- &lt;/p&gt;
1060
- &lt;p&gt;
1061
- &lt;%= f.submit %&gt;
1062
- &lt;/p&gt;
1063
- &lt;% end %&gt;
1064
- </pre>
1065
- </div>
1066
- <p>Although the same partial will be rendered into both views, Action View&#8217;s submit helper will return &#8220;Create Zone&#8221; for the new action and &#8220;Update Zone&#8221; for the edit action.</p>
1067
- <p>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 <tt>:object</tt> option:</p>
1068
- <div class="code_container">
1069
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1070
- &lt;%= render :partial =&gt; &quot;customer&quot;, :object =&gt; @new_customer %&gt;
1071
- </pre>
1072
- </div>
1073
- <p>Within the <tt>customer</tt> partial, the <tt>customer</tt> variable will refer to <tt>@new_customer</tt> from the parent view.</p>
1074
- <div class="warning"><p>In previous versions of Rails, the default local variable would look for an instance variable with the same name as the partial in the parent. This behavior was deprecated in 2.3 and has been removed in Rails 3.0.</p></div>
1075
- <p>If you have an instance of a model to render into a partial, you can use a shorthand syntax:</p>
1076
- <div class="code_container">
1077
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1078
- &lt;%= render @customer %&gt;
1079
- </pre>
1080
- </div>
1081
- <p>Assuming that the <tt>@customer</tt> instance variable contains an instance of the <tt>Customer</tt> model, this will use <tt>_customer.html.erb</tt> to render it and will pass the local variable <tt>customer</tt> into the partial which will refer to the <tt>@customer</tt> instance variable in the parent view.</p>
1082
- <h5 id="rendering-collections">3.4.5 Rendering Collections</h5>
1083
- <p>Partials are very useful in rendering collections. When you pass a collection to a partial via the <tt>:collection</tt> option, the partial will be inserted once for each member in the collection:</p>
1084
- <ul>
1085
- <li><tt>index.html.erb</tt></li>
1086
- </ul>
1087
- <div class="code_container">
1088
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1089
- &lt;h1&gt;Products&lt;/h1&gt;
1090
- &lt;%= render :partial =&gt; &quot;product&quot;, :collection =&gt; @products %&gt;
1091
- </pre>
1092
- </div>
1093
- <ul>
1094
- <li><tt>_product.html.erb</tt></li>
1095
- </ul>
1096
- <div class="code_container">
1097
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1098
- &lt;p&gt;Product Name: &lt;%= product.name %&gt;&lt;/p&gt;
1099
- </pre>
1100
- </div>
1101
- <p>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 <tt>_product</tt>, and within the <tt>_product</tt> partial, you can refer to <tt>product</tt> to get the instance that is being rendered.</p>
1102
- <p>In Rails 3.0, there is also a shorthand for this. Assuming <tt>@products</tt> is a collection of <tt>product</tt> instances, you can simply write this in the <tt>index.html.erb</tt> to produce the same result:</p>
1103
- <div class="code_container">
1104
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1105
- &lt;h1&gt;Products&lt;/h1&gt;
1106
- &lt;%= render @products %&gt;
1107
- </pre>
1108
- </div>
1109
- <p>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:</p>
1110
- <p>In the event that the collection is empty, <tt>render</tt> will return nil, so it should be fairly simple to provide alternative content.</p>
1111
- <div class="code_container">
1112
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1113
- &lt;h1&gt;Products&lt;/h1&gt;
1114
- &lt;%= render(@products) || 'There are no products available.' %&gt;
1115
- </pre>
1116
- </div>
1117
- <ul>
1118
- <li><tt>index.html.erb</tt></li>
1119
- </ul>
1120
- <div class="code_container">
1121
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1122
- &lt;h1&gt;Contacts&lt;/h1&gt;
1123
- &lt;%= render [customer1, employee1, customer2, employee2] %&gt;
1124
- </pre>
1125
- </div>
1126
- <ul>
1127
- <li><tt>customers/_customer.html.erb</tt></li>
1128
- </ul>
1129
- <div class="code_container">
1130
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1131
- &lt;p&gt;Customer: &lt;%= customer.name %&gt;&lt;/p&gt;
1132
- </pre>
1133
- </div>
1134
- <ul>
1135
- <li><tt>employees/_employee.html.erb</tt></li>
1136
- </ul>
1137
- <div class="code_container">
1138
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1139
- &lt;p&gt;Employee: &lt;%= employee.name %&gt;&lt;/p&gt;
1140
- </pre>
1141
- </div>
1142
- <p>In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.</p>
1143
- <h5 id="local-variables">3.4.6 Local Variables</h5>
1144
- <p>To use a custom local variable name within the partial, specify the <tt>:as</tt> option in the call to the partial:</p>
1145
- <div class="code_container">
1146
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1147
- &lt;%= render :partial =&gt; &quot;product&quot;, :collection =&gt; @products, :as =&gt; :item %&gt;
1148
- </pre>
1149
- </div>
1150
- <p>With this change, you can access an instance of the <tt>@products</tt> collection as the <tt>item</tt> local variable within the partial.</p>
1151
- <p>You can also pass in arbitrary local variables to any partial you are rendering with the <tt>:locals => {}</tt> option:</p>
1152
- <div class="code_container">
1153
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1154
- &lt;%= render :partial =&gt; 'products', :collection =&gt; @products,
1155
- :as =&gt; :item, :locals =&gt; {:title =&gt; &quot;Products Page&quot;} %&gt;
1156
- </pre>
1157
- </div>
1158
- <p>Would render a partial <tt>_products.html.erb</tt> once for each instance of <tt>product</tt> in the <tt>@products</tt> instance variable passing the instance to the partial as a local variable called <tt>item</tt> and to each partial, make the local variable <tt>title</tt> available with the value <tt>Products Page</tt>.</p>
1159
- <div class="info"><p>Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by <tt>_counter</tt>. For example, if you&#8217;re rendering <tt>@products</tt>, within the partial you can refer to <tt>product_counter</tt> to tell you how many times the partial has been rendered. This does not work in conjunction with the <tt>:as => :value</tt> option.</p></div>
1160
- <p>You can also specify a second partial to be rendered between instances of the main partial by using the <tt>:spacer_template</tt> option:</p>
1161
- <h5 id="spacer-templates">3.4.7 Spacer Templates</h5>
1162
- <div class="code_container">
1163
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1164
- &lt;%= render @products, :spacer_template =&gt; &quot;product_ruler&quot; %&gt;
1165
- </pre>
1166
- </div>
1167
- <p>Rails will render the <tt>_product_ruler</tt> partial (with no data passed in to it) between each pair of <tt>_product</tt> partials.</p>
1168
- <h4 id="using-nested-layouts">3.5 Using Nested Layouts</h4>
1169
- <p>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&#8217;s an example:</p>
1170
- <p>Suppose you have the following <tt>ApplicationController</tt> layout:</p>
1171
- <ul>
1172
- <li><tt>app/views/layouts/application.html.erb</tt></li>
1173
- </ul>
1174
- <div class="code_container">
1175
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1176
- &lt;html&gt;
1177
- &lt;head&gt;
1178
- &lt;title&gt;&lt;%= @page_title or 'Page Title' %&gt;&lt;/title&gt;
1179
- &lt;%= stylesheet_link_tag 'layout' %&gt;
1180
- &lt;style type=&quot;text/css&quot;&gt;&lt;%= yield :stylesheets %&gt;&lt;/style&gt;
1181
- &lt;/head&gt;
1182
- &lt;body&gt;
1183
- &lt;div id=&quot;top_menu&quot;&gt;Top menu items here&lt;/div&gt;
1184
- &lt;div id=&quot;menu&quot;&gt;Menu items here&lt;/div&gt;
1185
- &lt;div id=&quot;content&quot;&gt;&lt;%= content_for?(:content) ? yield(:content) : yield %&gt;&lt;/div&gt;
1186
- &lt;/body&gt;
1187
- &lt;/html&gt;
1188
- </pre>
1189
- </div>
1190
- <p>On pages generated by <tt>NewsController</tt>, you want to hide the top menu and add a right menu:</p>
1191
- <ul>
1192
- <li><tt>app/views/layouts/news.html.erb</tt></li>
1193
- </ul>
1194
- <div class="code_container">
1195
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1196
- &lt;% content_for :stylesheets do %&gt;
1197
- #top_menu {display: none}
1198
- #right_menu {float: right; background-color: yellow; color: black}
1199
- &lt;% end %&gt;
1200
- &lt;% content_for :content do %&gt;
1201
- &lt;div id=&quot;right_menu&quot;&gt;Right menu items here&lt;/div&gt;
1202
- &lt;%= content_for?(:news_content) ? yield(:news_content) : yield %&gt;
1203
- &lt;% end %&gt;
1204
- &lt;%= render :template =&gt; 'layouts/application' %&gt;
1205
- </pre>
1206
- </div>
1207
- <p>That&#8217;s it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the &#8220;content&#8221; div.</p>
1208
- <p>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 <tt>ActionView::render</tt> method via <tt>render :template => 'layouts/news'</tt> to base a new layout on the News layout. If you are sure you will not subtemplate the <tt>News</tt> layout, you can replace the <tt>content_for?(:news_content) ? yield(:news_content) : yield</tt> with simply <tt>yield</tt>.</p>
1209
-
1210
- <h3>Feedback</h3>
1211
- <p>
1212
- You're encouraged to help improve the quality of this guide.
1213
- </p>
1214
- <p>
1215
- If you see any typos or factual errors you are confident to
1216
- patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
1217
- and push the change yourself. That branch of Rails has public write access.
1218
- Commits are still reviewed, but that happens after you've submitted your
1219
- contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
1220
- cross-merged with master periodically.
1221
- </p>
1222
- <p>
1223
- You may also find incomplete content, or stuff that is not up to date.
1224
- Please do add any missing documentation for master. Check the
1225
- <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
1226
- for style and conventions.
1227
- </p>
1228
- <p>
1229
- If for whatever reason you spot something to fix but cannot patch it yourself, please
1230
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
1231
- </p>
1232
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
1233
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
1234
- </p>
1235
- </div>
1236
- </div>
1237
- </div>
1238
-
1239
- <hr class="hide" />
1240
- <div id="footer">
1241
- <div class="wrapper">
1242
- <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
1243
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
1244
- </div>
1245
- </div>
1246
-
1247
- <script type="text/javascript" src="javascripts/guides.js"></script>
1248
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
1249
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
1250
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
1251
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
1252
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
1253
- <script type="text/javascript">
1254
- SyntaxHighlighter.all()
1255
- </script>
1256
- </body>
1257
- </html>