Froze rails gems
[depot.git] / vendor / rails / railties / doc / guides / html / layouts_and_rendering.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5 <title>Layouts and Rendering in Rails</title>
6 <!--[if lt IE 8]>
7 <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
8 <![endif]-->
9 <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
10 <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
11 <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
12 <style type="text/css">
13 div#container {
14 max-width: 900px;
15 padding-bottom: 3em;
16 }
17
18 div#content {
19 margin-left: 200px;
20 }
21
22 div#container.notoc {
23 max-width: 600px;
24 }
25
26 .notoc div#content {
27 margin-left: 0;
28 }
29
30 pre {
31 line-height: 1.4em;
32 }
33
34 #content p tt {
35 background: #eeeeee;
36 border: solid 1px #cccccc;
37 padding: 3px;
38 }
39
40 dt {
41 font-weight: bold;
42 }
43
44 #content dt tt {
45 font-size: 10pt;
46 }
47
48 dd {
49 margin-left: 3em;
50 }
51
52 #content dt tt, #content pre tt {
53 background: none;
54 padding: 0;
55 border: 0;
56 }
57
58 #content .olist ol {
59 margin-left: 2em;
60 }
61
62 #header {
63 position: relative;
64 max-width: 840px;
65 margin-left: auto;
66 margin-right: auto;
67 }
68
69 #header.notoc {
70 max-width: 580px;
71 }
72
73 #logo {
74 position: absolute;
75 left: 10px;
76 top: 10px;
77 width: 110px;
78 height: 140px;
79 }
80
81 div#header h1#site_title {
82 background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
83 position: absolute;
84 width: 392px;
85 height: 55px;
86 left: 145px;
87 top: 20px;
88 margin: 0;
89 padding: 0;
90 }
91
92 #site_title span {
93 display: none;
94 }
95
96 #site_title_tagline {
97 display: none;
98 }
99
100 ul#navMain {
101 position: absolute;
102 margin: 0;
103 padding: 0;
104 top: 97px;
105 left: 145px;
106 }
107
108 .left-floaty, .right-floaty {
109 padding: 15px;
110 }
111
112 .admonitionblock,
113 .tableblock {
114 margin-left: 1em;
115 margin-right: 1em;
116 margin-top: 0.25em;
117 margin-bottom: 1em;
118 }
119
120 .admonitionblock .icon {
121 padding-right: 8px;
122 }
123
124 .admonitionblock .content {
125 border: solid 1px #ffda78;
126 background: #fffebd;
127 padding: 10px;
128 padding-top: 8px;
129 padding-bottom: 8px;
130 }
131
132 .admonitionblock .title {
133 font-size: 140%;
134 margin-bottom: 0.5em;
135 }
136
137 .tableblock table {
138 border: solid 1px #aaaaff;
139 background: #f0f0ff;
140 }
141
142 .tableblock th {
143 background: #e0e0e0;
144 }
145
146 .tableblock th,
147 .tableblock td {
148 padding: 3px;
149 padding-left: 5px;
150 padding-right: 5px;
151 }
152
153 .sidebarblock {
154 margin-top: 0.25em;
155 margin: 1em;
156 border: solid 1px #ccccbb;
157 padding: 8px;
158 background: #ffffe0;
159 }
160
161 .sidebarblock .sidebar-title {
162 font-size: 140%;
163 font-weight: 600;
164 margin-bottom: 0.3em;
165 }
166
167 .sidebarblock .sidebar-content > .para:last-child > p {
168 margin-bottom: 0;
169 }
170
171 .sidebarblock .sidebar-title a {
172 text-decoration: none;
173 }
174
175 .sidebarblock .sidebar-title a:hover {
176 text-decoration: underline;
177 }
178
179 </style>
180 </head>
181 <body>
182 <div id="header" >
183 <div id="logo">
184 <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
185 </div>
186
187 <h1 id="site_title"><span>Ruby on Rails</span></h1>
188 <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
189
190 <ul id="navMain">
191 <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
192 <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
193 </ul>
194 </div>
195
196 <div id="container">
197
198 <div id="sidebar">
199 <h2>Chapters</h2>
200 <ol>
201 <li>
202 <a href="#_overview_how_the_pieces_fit_together">Overview: How the Pieces Fit Together</a>
203 </li>
204 <li>
205 <a href="#_creating_responses">Creating Responses</a>
206 <ul>
207
208 <li><a href="#_rendering_by_default_convention_over_configuration_in_action">Rendering by Default: Convention Over Configuration in Action</a></li>
209
210 <li><a href="#_using_tt_render_tt">Using <tt>render</tt></a></li>
211
212 <li><a href="#_using_tt_redirect_to_tt">Using <tt>redirect_to</tt></a></li>
213
214 <li><a href="#_using_tt_head_tt_to_build_header_only_responses">Using <tt>head</tt> To Build Header-Only Responses</a></li>
215
216 </ul>
217 </li>
218 <li>
219 <a href="#_structuring_layouts">Structuring Layouts</a>
220 <ul>
221
222 <li><a href="#_asset_tags">Asset Tags</a></li>
223
224 <li><a href="#_understanding_tt_yield_tt">Understanding <tt>yield</tt></a></li>
225
226 <li><a href="#_using_tt_content_for_tt">Using <tt>content_for</tt></a></li>
227
228 <li><a href="#_using_partials">Using Partials</a></li>
229
230 </ul>
231 </li>
232 <li>
233 <a href="#_changelog">Changelog</a>
234 </li>
235 </ol>
236 </div>
237
238 <div id="content">
239 <h1>Layouts and Rendering in Rails</h1>
240 <div id="preamble">
241 <div class="sectionbody">
242 <div class="para"><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></div>
243 <div class="ilist"><ul>
244 <li>
245 <p>
246 Use the various rendering methods built in to Rails
247 </p>
248 </li>
249 <li>
250 <p>
251 Create layouts with multiple content sections
252 </p>
253 </li>
254 <li>
255 <p>
256 Use partials to DRY up your views
257 </p>
258 </li>
259 </ul></div>
260 </div>
261 </div>
262 <h2 id="_overview_how_the_pieces_fit_together">1. Overview: How the Pieces Fit Together</h2>
263 <div class="sectionbody">
264 <div class="para"><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'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.</p></div>
265 <div class="para"><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'll see all of those paths later in this guide.</p></div>
266 </div>
267 <h2 id="_creating_responses">2. Creating Responses</h2>
268 <div class="sectionbody">
269 <div class="para"><p>From the controller's point of view, there are three ways to create an HTTP response:</p></div>
270 <div class="ilist"><ul>
271 <li>
272 <p>
273 Call <tt>render</tt> to create a full response to send back to the browser
274 </p>
275 </li>
276 <li>
277 <p>
278 Call <tt>redirect_to</tt> to send an HTTP redirect status code to the browser
279 </p>
280 </li>
281 <li>
282 <p>
283 Call <tt>head</tt> to create a response consisting solely of HTTP headers to send back to the browser
284 </p>
285 </li>
286 </ul></div>
287 <div class="para"><p>I'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></div>
288 <h3 id="_rendering_by_default_convention_over_configuration_in_action">2.1. Rendering by Default: Convention Over Configuration in Action</h3>
289 <div class="para"><p>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 actions. For example, if you have this code in your <tt>BooksController</tt> class:</p></div>
290 <div class="listingblock">
291 <div class="content"><!-- Generator: GNU source-highlight 2.9
292 by Lorenzo Bettini
293 http://www.lorenzobettini.it
294 http://www.gnu.org/software/src-highlite -->
295 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
296 <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
297 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
298 </tt></pre></div></div>
299 <div class="para"><p>Rails will automatically render <tt>app/views/books/show.html.erb</tt> after running the method. In fact, if you have the default catch-all route in place (<tt>map.connect <em>:controller/:action/:id</em></tt>), Rails will even render views that don't have any code at all in the controller. For example, if you have the default route in place and a request comes in for <tt>/books/sale_list</tt>, Rails will render <tt>app/views/books/sale_list.html.erb</tt> in response.</p></div>
300 <div class="admonitionblock">
301 <table><tr>
302 <td class="icon">
303 <img src="./images/icons/note.png" alt="Note" />
304 </td>
305 <td class="content">The actual rendering is done by subclasses of <tt>ActionView::TemplateHandlers</tt>. 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. In Rails 2, the standard extensions are <tt>.erb</tt> for ERB (HTML with embedded Ruby), <tt>.rjs</tt> for RJS (javascript with embedded ruby) and <tt>.builder</tt> for Builder (XML generator). You'll also find <tt>.rhtml</tt> used for ERB templates and .rxml for Builder templates, but those extensions are now formally deprecated and will be removed from a future version of Rails.</td>
306 </tr></table>
307 </div>
308 <h3 id="_using_tt_render_tt">2.2. Using <tt>render</tt></h3>
309 <div class="para"><p>In most cases, the <tt>ActionController::Base#render</tt> 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 <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, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.</p></div>
310 <div class="admonitionblock">
311 <table><tr>
312 <td class="icon">
313 <img src="./images/icons/tip.png" alt="Tip" />
314 </td>
315 <td class="content">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.</td>
316 </tr></table>
317 </div>
318 <h4 id="_rendering_nothing">2.2.1. Rendering Nothing</h4>
319 <div class="para"><p>Perhaps the simplest thing you can do with <tt>render</tt> is to render nothing at all:</p></div>
320 <div class="listingblock">
321 <div class="content"><!-- Generator: GNU source-highlight 2.9
322 by Lorenzo Bettini
323 http://www.lorenzobettini.it
324 http://www.gnu.org/software/src-highlite -->
325 <pre><tt>render <span style="color: #990000">:</span>nothing <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
326 </tt></pre></div></div>
327 <div class="para"><p>This will send an empty response to the browser (though it will include any status headers you set with the :status option, discussed below).</p></div>
328 <div class="admonitionblock">
329 <table><tr>
330 <td class="icon">
331 <img src="./images/icons/tip.png" alt="Tip" />
332 </td>
333 <td class="content">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're only generating HTTP headers.</td>
334 </tr></table>
335 </div>
336 <h4 id="_using_tt_render_tt_with_tt_action_tt">2.2.2. Using <tt>render</tt> with <tt>:action</tt></h4>
337 <div class="para"><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 <tt>:action</tt> option:</p></div>
338 <div class="listingblock">
339 <div class="content"><!-- Generator: GNU source-highlight 2.9
340 by Lorenzo Bettini
341 http://www.lorenzobettini.it
342 http://www.gnu.org/software/src-highlite -->
343 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
344 <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
345 <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span>update_attributes<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>book<span style="color: #990000">])</span>
346 redirect_to<span style="color: #990000">(</span><span style="color: #009900">@book</span><span style="color: #990000">)</span>
347 <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
348 render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"edit"</span>
349 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
350 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
351 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
352 </tt></pre></div></div>
353 <div class="para"><p>If the call to <tt>update_attributes_ fails, calling the +update</tt> action in this controller will render the <tt>edit.html.erb</tt> template belonging to the same controller.</p></div>
354 <div class="admonitionblock">
355 <table><tr>
356 <td class="icon">
357 <img src="./images/icons/warning.png" alt="Warning" />
358 </td>
359 <td class="content">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>.</td>
360 </tr></table>
361 </div>
362 <h4 id="_using_tt_render_tt_with_tt_template_tt">2.2.3. Using <tt>render</tt> with <tt>:template</tt></h4>
363 <div class="para"><p>What if you want to render a template from an entirely different controller from the one that contains the action code? You can do that with the <tt>:template</tt> option to <tt>render</tt>, which accepts the full path (relative to <tt>app/views</tt>) of the template to render. For example, if you'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></div>
364 <div class="listingblock">
365 <div class="content"><!-- Generator: GNU source-highlight 2.9
366 by Lorenzo Bettini
367 http://www.lorenzobettini.it
368 http://www.gnu.org/software/src-highlite -->
369 <pre><tt>render <span style="color: #990000">:</span>template <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'products/show'</span>
370 </tt></pre></div></div>
371 <h4 id="_using_tt_render_tt_with_tt_file_tt">2.2.4. Using <tt>render</tt> with <tt>:file</tt></h4>
372 <div class="para"><p>If you want to use a view that's entirely outside of your application (perhaps you're sharing views between two Rails applications), you can use the <tt>:file</tt> option to <tt>render</tt>:</p></div>
373 <div class="listingblock">
374 <div class="content"><!-- Generator: GNU source-highlight 2.9
375 by Lorenzo Bettini
376 http://www.lorenzobettini.it
377 http://www.gnu.org/software/src-highlite -->
378 <pre><tt>render <span style="color: #990000">:</span>file <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"/u/apps/warehouse_app/current/app/views/products/show"</span>
379 </tt></pre></div></div>
380 <div class="para"><p>The <tt>:file</tt> 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.</p></div>
381 <div class="admonitionblock">
382 <table><tr>
383 <td class="icon">
384 <img src="./images/icons/note.png" alt="Note" />
385 </td>
386 <td class="content">By default, if you use the <tt>:file</tt> option, 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 &#8658; true</tt> option</td>
387 </tr></table>
388 </div>
389 <h4 id="_using_tt_render_tt_with_tt_inline_tt">2.2.5. Using <tt>render</tt> with <tt>:inline</tt></h4>
390 <div class="para"><p>The <tt>render</tt> method can do without a view completely, if you're willing to use the <tt>:inline</tt> option to supply ERB as part of the method call. This is perfectly valid:</p></div>
391 <div class="listingblock">
392 <div class="content"><!-- Generator: GNU source-highlight 2.9
393 by Lorenzo Bettini
394 http://www.lorenzobettini.it
395 http://www.gnu.org/software/src-highlite -->
396 <pre><tt>render <span style="color: #990000">:</span>inline <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"&lt;% products.each do |p| %&gt;&lt;p&gt;&lt;%= p.name %&gt;&lt;p&gt;&lt;% end %&gt;"</span>
397 </tt></pre></div></div>
398 <div class="admonitionblock">
399 <table><tr>
400 <td class="icon">
401 <img src="./images/icons/warning.png" alt="Warning" />
402 </td>
403 <td class="content">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.</td>
404 </tr></table>
405 </div>
406 <div class="para"><p>By default, inline rendering uses ERb. You can force it to use Builder instead with the <tt>:type</tt> option:</p></div>
407 <div class="listingblock">
408 <div class="content"><!-- Generator: GNU source-highlight 2.9
409 by Lorenzo Bettini
410 http://www.lorenzobettini.it
411 http://www.gnu.org/software/src-highlite -->
412 <pre><tt>render <span style="color: #990000">:</span>inline <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"xml.p {'Horrid coding practice!'}"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>type <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>builder
413 </tt></pre></div></div>
414 <h4 id="_using_tt_render_tt_with_tt_update_tt">2.2.6. Using <tt>render</tt> with <tt>:update</tt></h4>
415 <div class="para"><p>You can also render javascript-based page updates inline using the <tt>:update</tt> option to <tt>render</tt>:</p></div>
416 <div class="listingblock">
417 <div class="content"><!-- Generator: GNU source-highlight 2.9
418 by Lorenzo Bettini
419 http://www.lorenzobettini.it
420 http://www.gnu.org/software/src-highlite -->
421 <pre><tt>render <span style="color: #990000">:</span>update <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>page<span style="color: #990000">|</span>
422 page<span style="color: #990000">.</span>replace_html <span style="color: #FF0000">'warning'</span><span style="color: #990000">,</span> <span style="color: #FF0000">"Invalid options supplied"</span>
423 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
424 </tt></pre></div></div>
425 <div class="admonitionblock">
426 <table><tr>
427 <td class="icon">
428 <img src="./images/icons/warning.png" alt="Warning" />
429 </td>
430 <td class="content">Placing javascript updates in your controller may seem to streamline small updates, but it defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. I recommend using a separate rjs template instead, no matter how small the update.</td>
431 </tr></table>
432 </div>
433 <h4 id="_rendering_text">2.2.7. Rendering Text</h4>
434 <div class="para"><p>You can send plain text - with no markup at all - back to the browser by using the <tt>:text</tt> option to <tt>render</tt>:</p></div>
435 <div class="listingblock">
436 <div class="content"><!-- Generator: GNU source-highlight 2.9
437 by Lorenzo Bettini
438 http://www.lorenzobettini.it
439 http://www.gnu.org/software/src-highlite -->
440 <pre><tt>render <span style="color: #990000">:</span>text <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"OK"</span>
441 </tt></pre></div></div>
442 <div class="admonitionblock">
443 <table><tr>
444 <td class="icon">
445 <img src="./images/icons/tip.png" alt="Tip" />
446 </td>
447 <td class="content">Rendering pure text is most useful when you're responding to AJAX or web service requests that are expecting something other than proper HTML.</td>
448 </tr></table>
449 </div>
450 <div class="admonitionblock">
451 <table><tr>
452 <td class="icon">
453 <img src="./images/icons/note.png" alt="Note" />
454 </td>
455 <td class="content">By default, if you use the <tt>:text</tt> option, the file 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 &#8658; true</tt> option</td>
456 </tr></table>
457 </div>
458 <h4 id="_rendering_json">2.2.8. Rendering JSON</h4>
459 <div class="para"><p>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:</p></div>
460 <div class="listingblock">
461 <div class="content"><!-- Generator: GNU source-highlight 2.9
462 by Lorenzo Bettini
463 http://www.lorenzobettini.it
464 http://www.gnu.org/software/src-highlite -->
465 <pre><tt>render <span style="color: #990000">:</span>json <span style="color: #990000">=&gt;</span> <span style="color: #009900">@product</span>
466 </tt></pre></div></div>
467 <div class="admonitionblock">
468 <table><tr>
469 <td class="icon">
470 <img src="./images/icons/tip.png" alt="Tip" />
471 </td>
472 <td class="content">You don'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.</td>
473 </tr></table>
474 </div>
475 <h4 id="_rendering_xml">2.2.9. Rendering XML</h4>
476 <div class="para"><p>Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:</p></div>
477 <div class="listingblock">
478 <div class="content"><!-- Generator: GNU source-highlight 2.9
479 by Lorenzo Bettini
480 http://www.lorenzobettini.it
481 http://www.gnu.org/software/src-highlite -->
482 <pre><tt>render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@product</span>
483 </tt></pre></div></div>
484 <div class="admonitionblock">
485 <table><tr>
486 <td class="icon">
487 <img src="./images/icons/tip.png" alt="Tip" />
488 </td>
489 <td class="content">You don'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.</td>
490 </tr></table>
491 </div>
492 <h4 id="_rendering_vanilla_javascript">2.2.10. Rendering Vanilla JavaScript</h4>
493 <div class="para"><p>Rails can render vanilla JavaScript (as an alternative to using <tt>update</tt> with n <tt>.rjs</tt> file):</p></div>
494 <div class="listingblock">
495 <div class="content"><!-- Generator: GNU source-highlight 2.9
496 by Lorenzo Bettini
497 http://www.lorenzobettini.it
498 http://www.gnu.org/software/src-highlite -->
499 <pre><tt>render <span style="color: #990000">:</span>js <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"alert('Hello Rails');"</span>
500 </tt></pre></div></div>
501 <div class="para"><p>This will send the supplied string to the browser with a MIME type of <tt>text/javascript</tt>.</p></div>
502 <h4 id="_options_for_tt_render_tt">2.2.11. Options for <tt>render</tt></h4>
503 <div class="para"><p>Calls to the <tt>render</tt> method generally accept four options:</p></div>
504 <div class="ilist"><ul>
505 <li>
506 <p>
507 <tt>:content_type</tt>
508 </p>
509 </li>
510 <li>
511 <p>
512 <tt>:layout</tt>
513 </p>
514 </li>
515 <li>
516 <p>
517 <tt>:status</tt>
518 </p>
519 </li>
520 <li>
521 <p>
522 <tt>:location</tt>
523 </p>
524 </li>
525 </ul></div>
526 <h5 id="_the_tt_content_type_tt_option">The <tt>:content_type</tt> Option</h5>
527 <div class="para"><p>By default, Rails will serve the results of a rendering operation with the MIME 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></div>
528 <div class="listingblock">
529 <div class="content"><!-- Generator: GNU source-highlight 2.9
530 by Lorenzo Bettini
531 http://www.lorenzobettini.it
532 http://www.gnu.org/software/src-highlite -->
533 <pre><tt>render <span style="color: #990000">:</span>file <span style="color: #990000">=&gt;</span> filename<span style="color: #990000">,</span> <span style="color: #990000">:</span>content_type <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'application/rss'</span>
534 </tt></pre></div></div>
535 <h5 id="_the_tt_layout_tt_option">The <tt>:layout</tt> Option</h5>
536 <div class="para"><p>With most of the options to <tt>render</tt>, 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.</p></div>
537 <div class="para"><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></div>
538 <div class="listingblock">
539 <div class="content"><!-- Generator: GNU source-highlight 2.9
540 by Lorenzo Bettini
541 http://www.lorenzobettini.it
542 http://www.gnu.org/software/src-highlite -->
543 <pre><tt>render <span style="color: #990000">:</span>layout <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'special_layout'</span>
544 </tt></pre></div></div>
545 <div class="para"><p>You can also tell Rails to render with no layout at all:</p></div>
546 <div class="listingblock">
547 <div class="content"><!-- Generator: GNU source-highlight 2.9
548 by Lorenzo Bettini
549 http://www.lorenzobettini.it
550 http://www.gnu.org/software/src-highlite -->
551 <pre><tt>render <span style="color: #990000">:</span>layout <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
552 </tt></pre></div></div>
553 <h5 id="_the_tt_status_tt_option">The <tt>:status</tt> Option</h5>
554 <div class="para"><p>Rails will automatically generate a response with the correct HTML status code (in most cases, this is <tt>200 OK</tt>). You can use the <tt>:status</tt> option to change this:</p></div>
555 <div class="listingblock">
556 <div class="content"><!-- Generator: GNU source-highlight 2.9
557 by Lorenzo Bettini
558 http://www.lorenzobettini.it
559 http://www.gnu.org/software/src-highlite -->
560 <pre><tt>render <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #993399">500</span>
561 render <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>forbidden
562 </tt></pre></div></div>
563 <div class="para"><p>Rails understands either numeric status codes or symbols for status codes. You can find its list of status codes in <tt>actionpack/lib/action_controller/status_codes.rb</tt>. You can also see there how it maps symbols to status codes in that file.</p></div>
564 <h5 id="_the_tt_location_tt_option">The <tt>:location</tt> Option</h5>
565 <div class="para"><p>You can use the <tt>:location</tt> option to set the HTTP <tt>Location</tt> header:</p></div>
566 <div class="listingblock">
567 <div class="content"><!-- Generator: GNU source-highlight 2.9
568 by Lorenzo Bettini
569 http://www.lorenzobettini.it
570 http://www.gnu.org/software/src-highlite -->
571 <pre><tt>render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> photo<span style="color: #990000">,</span> <span style="color: #990000">:</span>location <span style="color: #990000">=&gt;</span> photo_url<span style="color: #990000">(</span>photo<span style="color: #990000">)</span>
572 </tt></pre></div></div>
573 <h4 id="_finding_layouts">2.2.12. Finding Layouts</h4>
574 <div class="para"><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>. If there is no such controller-specific layout, Rails will use <tt>/app/views/layouts/application.html.erb</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></div>
575 <h5 id="_specifying_layouts_on_a_per_controller_basis">Specifying Layouts on a per-Controller Basis</h5>
576 <div class="para"><p>You can override the automatic layout conventions in your controllers by using the <tt>layout</tt> declaration in the controller. For example:</p></div>
577 <div class="listingblock">
578 <div class="content"><!-- Generator: GNU source-highlight 2.9
579 by Lorenzo Bettini
580 http://www.lorenzobettini.it
581 http://www.gnu.org/software/src-highlite -->
582 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ApplicationController
583 layout <span style="color: #FF0000">"inventory"</span>
584 <span style="font-style: italic"><span style="color: #9A1900">#...</span></span>
585 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
586 </tt></pre></div></div>
587 <div class="para"><p>With this declaration, all methods within <tt>ProductsController</tt> will use <tt>app/views/layouts/inventory.html.erb</tt> for their layout.</p></div>
588 <div class="para"><p>To assign a specific layout for the entire application, use a declaration in your <tt>ApplicationController</tt> class:</p></div>
589 <div class="listingblock">
590 <div class="content"><!-- Generator: GNU source-highlight 2.9
591 by Lorenzo Bettini
592 http://www.lorenzobettini.it
593 http://www.gnu.org/software/src-highlite -->
594 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
595 layout <span style="color: #FF0000">"main"</span>
596 <span style="font-style: italic"><span style="color: #9A1900">#...</span></span>
597 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
598 </tt></pre></div></div>
599 <div class="para"><p>With this declaration, all views in the entire application will use <tt>app/views/layouts/main.html.erb</tt> for their layout.</p></div>
600 <h5 id="_choosing_layouts_at_runtime">Choosing Layouts at Runtime</h5>
601 <div class="para"><p>You can use a symbol to defer the choice of layout until a request is processed:</p></div>
602 <div class="listingblock">
603 <div class="content"><!-- Generator: GNU source-highlight 2.9
604 by Lorenzo Bettini
605 http://www.lorenzobettini.it
606 http://www.gnu.org/software/src-highlite -->
607 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ApplicationController
608 layout <span style="color: #990000">:</span>products_layout
609
610 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
611 <span style="color: #009900">@product</span> <span style="color: #990000">=</span> Product<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
612 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
613
614 private
615 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> products_layout
616 <span style="color: #009900">@current_user</span><span style="color: #990000">.</span>special? <span style="color: #990000">?</span> <span style="color: #FF0000">"special"</span> <span style="color: #990000">:</span> <span style="color: #FF0000">"products"</span>
617 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
618
619 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
620 </tt></pre></div></div>
621 <div class="para"><p>Now, if the current user is a special user, they'll get a special layout when viewing a product. You can even use an inline method to determine the layout:</p></div>
622 <div class="listingblock">
623 <div class="content"><!-- Generator: GNU source-highlight 2.9
624 by Lorenzo Bettini
625 http://www.lorenzobettini.it
626 http://www.gnu.org/software/src-highlite -->
627 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ApplicationController
628 layout proc<span style="color: #FF0000">{</span> <span style="color: #990000">|</span>controller<span style="color: #990000">|</span> controller<span style="color: #990000">.</span>
629 <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
630 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
631 </tt></pre></div></div>
632 <h5 id="_conditional_layouts">Conditional Layouts</h5>
633 <div class="para"><p>Layouts specified at the controller level support <tt>:only</tt> and <tt>:except</tt> options that take either a method name or an array of method names:</p></div>
634 <div class="listingblock">
635 <div class="content">
636 <pre><tt>class ProductsController &lt; ApplicationController
637 layout "inventory", :only =&gt; :index
638 layout "product", :except =&gt; [:index, :rss]
639 #...
640 end</tt></pre>
641 </div></div>
642 <div class="para"><p>With those declarations, the <tt>inventory</tt> layout would be used only for the <tt>index</tt> method, the <tt>product</tt> layout would be used for everything else except the <tt>rss</tt> method, and the <tt>rss</tt> method will have its layout determined by the automatic layout rules.</p></div>
643 <h5 id="_layout_inheritance">Layout Inheritance</h5>
644 <div class="para"><p>Layouts are shared downwards in the hierarchy, and more specific layouts always override more general ones. For example:</p></div>
645 <div class="para"><p><tt>application.rb</tt>:</p></div>
646 <div class="listingblock">
647 <div class="content"><!-- Generator: GNU source-highlight 2.9
648 by Lorenzo Bettini
649 http://www.lorenzobettini.it
650 http://www.gnu.org/software/src-highlite -->
651 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
652 layout <span style="color: #FF0000">"main"</span>
653 <span style="font-style: italic"><span style="color: #9A1900">#...</span></span>
654 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
655 </tt></pre></div></div>
656 <div class="para"><p><tt>posts_controller.rb</tt>:</p></div>
657 <div class="listingblock">
658 <div class="content"><!-- Generator: GNU source-highlight 2.9
659 by Lorenzo Bettini
660 http://www.lorenzobettini.it
661 http://www.gnu.org/software/src-highlite -->
662 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000">&lt;</span> ApplicationController
663 <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
664 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
665 </tt></pre></div></div>
666 <div class="para"><p><tt>special_posts_controller.rb</tt>:</p></div>
667 <div class="listingblock">
668 <div class="content"><!-- Generator: GNU source-highlight 2.9
669 by Lorenzo Bettini
670 http://www.lorenzobettini.it
671 http://www.gnu.org/software/src-highlite -->
672 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> SpecialPostsController <span style="color: #990000">&lt;</span> PostsController
673 layout <span style="color: #FF0000">"special"</span>
674 <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
675 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
676 </tt></pre></div></div>
677 <div class="para"><p><tt>old_posts_controller.rb</tt>:</p></div>
678 <div class="listingblock">
679 <div class="content"><!-- Generator: GNU source-highlight 2.9
680 by Lorenzo Bettini
681 http://www.lorenzobettini.it
682 http://www.gnu.org/software/src-highlite -->
683 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> OldPostsController <span style="color: #990000">&lt;</span> SpecialPostsController
684 layout <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span>
685
686 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
687 <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
688 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
689
690 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
691 <span style="color: #009900">@old_posts</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>older
692 render <span style="color: #990000">:</span>layout <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"old"</span>
693 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
694 <span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
695 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
696 </tt></pre></div></div>
697 <div class="para"><p>In this application:</p></div>
698 <div class="ilist"><ul>
699 <li>
700 <p>
701 In general, views will be rendered in the <tt>main</tt> layout
702 </p>
703 </li>
704 <li>
705 <p>
706 <tt>PostsController#index</tt> will use the <tt>main</tt> layout
707 </p>
708 </li>
709 <li>
710 <p>
711 <tt>SpecialPostsController#index</tt> will use the <tt>special</tt> layout
712 </p>
713 </li>
714 <li>
715 <p>
716 <tt>OldPostsController#show</tt> will use no layout at all
717 </p>
718 </li>
719 <li>
720 <p>
721 <tt>OldPostsController#index</tt> will use the <tt>old</tt> layout
722 </p>
723 </li>
724 </ul></div>
725 <h4 id="_avoiding_double_render_errors">2.2.13. Avoiding Double Render Errors</h4>
726 <div class="para"><p>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 <tt>render</tt> works.</p></div>
727 <div class="para"><p>For example, here's some code that will trigger this error:</p></div>
728 <div class="listingblock">
729 <div class="content"><!-- Generator: GNU source-highlight 2.9
730 by Lorenzo Bettini
731 http://www.lorenzobettini.it
732 http://www.gnu.org/software/src-highlite -->
733 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
734 <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
735 <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span>special?
736 render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"special_show"</span>
737 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
738 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
739 </tt></pre></div></div>
740 <div class="para"><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>show</tt> view - and throw an error. The solution is simple: make sure that you only have 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's a patched version of the method:</p></div>
741 <div class="listingblock">
742 <div class="content"><!-- Generator: GNU source-highlight 2.9
743 by Lorenzo Bettini
744 http://www.lorenzobettini.it
745 http://www.gnu.org/software/src-highlite -->
746 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
747 <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
748 <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span>special?
749 render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"special_show"</span> <span style="font-weight: bold"><span style="color: #0000FF">and</span></span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span>
750 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
751 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
752 </tt></pre></div></div>
753 <h3 id="_using_tt_redirect_to_tt">2.3. Using <tt>redirect_to</tt></h3>
754 <div class="para"><p>Another way to handle returning responses to a HTTP request is with <tt>redirect_to</tt>. As you'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 URL. For example, you could redirect from wherever you are in your code to the index of photos in your application with this call:</p></div>
755 <div class="listingblock">
756 <div class="content"><!-- Generator: GNU source-highlight 2.9
757 by Lorenzo Bettini
758 http://www.lorenzobettini.it
759 http://www.gnu.org/software/src-highlite -->
760 <pre><tt>redirect_to photos_path
761 </tt></pre></div></div>
762 <div class="para"><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>. In addition, there's a special redirect that sends the user back to the page they just came from:</p></div>
763 <div class="listingblock">
764 <div class="content">
765 <pre><tt>redirect_to :back</tt></pre>
766 </div></div>
767 <h4 id="_getting_a_different_redirect_status_code">2.3.1. Getting a Different Redirect Status Code</h4>
768 <div class="para"><p>Rails uses HTTP status code 302 (permanent redirect) when you call <tt>redirect_to</tt>. If you'd like to use a different status code (perhaps 301, temporary redirect), you can do so by using the <tt>:status</tt> option:</p></div>
769 <div class="listingblock">
770 <div class="content">
771 <pre><tt>redirect_to photos_path, :status =&gt; 301</tt></pre>
772 </div></div>
773 <div class="para"><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></div>
774 <h4 id="_the_difference_between_tt_render_tt_and_tt_redirect_tt">2.3.2. The Difference Between <tt>render</tt> and <tt>redirect</tt></h4>
775 <div class="para"><p>Sometimes inexperienced developers conceive 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've told the browser what request it should make next, by sending back a HTTP 302 status code.</p></div>
776 <div class="para"><p>Consider these actions to see the difference:</p></div>
777 <div class="listingblock">
778 <div class="content"><!-- Generator: GNU source-highlight 2.9
779 by Lorenzo Bettini
780 http://www.lorenzobettini.it
781 http://www.gnu.org/software/src-highlite -->
782 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
783 <span style="color: #009900">@books</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
784 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
785
786 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
787 <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
788 <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
789 render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"index"</span> <span style="font-weight: bold"><span style="color: #0000FF">and</span></span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span>
790 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
791 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
792 </tt></pre></div></div>
793 <div class="para"><p>With the code in this form, there will be likely be a problem if the <tt>@book</tt> variable is <tt>nil</tt>. Remember, a <tt>render :action</tt> doesn't run any code in the target action, so nothing will set up the <tt>@books</tt> variable that the <tt>index</tt> view is presumably depending on. One way to fix this is to redirect instead of rendering:</p></div>
794 <div class="listingblock">
795 <div class="content"><!-- Generator: GNU source-highlight 2.9
796 by Lorenzo Bettini
797 http://www.lorenzobettini.it
798 http://www.gnu.org/software/src-highlite -->
799 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
800 <span style="color: #009900">@books</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
801 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
802
803 <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
804 <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
805 <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
806 redirect_to <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"index"</span> <span style="font-weight: bold"><span style="color: #0000FF">and</span></span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span>
807 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
808 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
809 </tt></pre></div></div>
810 <div class="para"><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></div>
811 <h3 id="_using_tt_head_tt_to_build_header_only_responses">2.4. Using <tt>head</tt> To Build Header-Only Responses</h3>
812 <div class="para"><p>The <tt>head</tt> method exists to let you send back responses to the browser that have only headers. It provides a more obvious alternative to calling <tt>render :nothing</tt>. The <tt>head</tt> method takes one response, which is interpreted as a hash of header names and values. For example, you can return only an error header:</p></div>
813 <div class="listingblock">
814 <div class="content"><!-- Generator: GNU source-highlight 2.9
815 by Lorenzo Bettini
816 http://www.lorenzobettini.it
817 http://www.gnu.org/software/src-highlite -->
818 <pre><tt>head <span style="color: #990000">:</span>bad_request
819 </tt></pre></div></div>
820 <div class="para"><p>Or you can use other HTTP headers to convey additional information:</p></div>
821 <div class="listingblock">
822 <div class="content"><!-- Generator: GNU source-highlight 2.9
823 by Lorenzo Bettini
824 http://www.lorenzobettini.it
825 http://www.gnu.org/software/src-highlite -->
826 <pre><tt>head <span style="color: #990000">:</span>created<span style="color: #990000">,</span> <span style="color: #990000">:</span>location <span style="color: #990000">=&gt;</span> photo_path<span style="color: #990000">(</span><span style="color: #009900">@photo</span><span style="color: #990000">)</span>
827 </tt></pre></div></div>
828 </div>
829 <h2 id="_structuring_layouts">3. Structuring Layouts</h2>
830 <div class="sectionbody">
831 <div class="para"><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></div>
832 <div class="ilist"><ul>
833 <li>
834 <p>
835 Asset tags
836 </p>
837 </li>
838 <li>
839 <p>
840 <tt>yield</tt> and <tt>content_for</tt>
841 </p>
842 </li>
843 <li>
844 <p>
845 Partials
846 </p>
847 </li>
848 </ul></div>
849 <div class="para"><p>I'll discuss each of these in turn.</p></div>
850 <h3 id="_asset_tags">3.1. Asset Tags</h3>
851 <div class="para"><p>Asset tags provide methods for generating HTML that links views to assets like images, javascript, stylesheets, and feeds. There are four types of include tag:</p></div>
852 <div class="ilist"><ul>
853 <li>
854 <p>
855 auto_discovery_link_tag
856 </p>
857 </li>
858 <li>
859 <p>
860 javascript_include_tag
861 </p>
862 </li>
863 <li>
864 <p>
865 stylesheet_link_tag
866 </p>
867 </li>
868 <li>
869 <p>
870 image_tag
871 </p>
872 </li>
873 </ul></div>
874 <div class="para"><p>You can use these tags in layouts or other views, although the tags other than <tt>image_tag</tt> are most commonly used in the <tt>&lt;head&gt;</tt> section of a layout.</p></div>
875 <div class="admonitionblock">
876 <table><tr>
877 <td class="icon">
878 <img src="./images/icons/warning.png" alt="Warning" />
879 </td>
880 <td class="content">The asset tags do <em>not</em> verify the existence of the assets at the specified locations; they simply assume that you know what you're doing and generate the link.</td>
881 </tr></table>
882 </div>
883 <h4 id="_linking_to_feeds_with_tt_auto_discovery_link_tag_tt">3.1.1. Linking to Feeds with <tt>auto_discovery_link_tag</tt></h4>
884 <div class="para"><p>The <tt>auto_discovery_link_tag helper builds HTML that most browsers and newsreaders can use to detect the presences of RSS or ATOM feeds. It takes the type of the link (</tt>:rss+ or <tt>:atom</tt>), a hash of options that are passed through to url_for, and a hash of options for the tag:</p></div>
885 <div class="listingblock">
886 <div class="content"><!-- Generator: GNU source-highlight 2.9
887 by Lorenzo Bettini
888 http://www.lorenzobettini.it
889 http://www.gnu.org/software/src-highlite -->
890 <pre><tt><span style="color: #FF0000">&lt;%= auto_discovery_link_tag(:rss, {:action =&gt;</span> <span style="color: #FF0000">"feed"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>title <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"RSS Feed"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span> <span style="color: #990000">%&gt;</span>
891 </tt></pre></div></div>
892 <div class="para"><p>There are three tag options available for <tt>auto_discovery_link_tag</tt>:</p></div>
893 <div class="ilist"><ul>
894 <li>
895 <p>
896 <tt>:rel</tt> specifies the <tt>rel</tt> value in the link (defaults to "alternate")
897 </p>
898 </li>
899 <li>
900 <p>
901 <tt>:type</tt> specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically
902 </p>
903 </li>
904 <li>
905 <p>
906 <tt>:title</tt> specifies the title of the link
907 </p>
908 </li>
909 </ul></div>
910 <h4 id="_linking_to_javascript_files_with_tt_javascript_include_tag_tt">3.1.2. Linking to Javascript Files with <tt>javascript_include_tag</tt></h4>
911 <div class="para"><p>The <tt>javascript_include_tag</tt> helper returns an HTML <tt>&lt;script&gt;</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 URL, if you prefer. For example, to include <tt>public/javascripts/main.js</tt>:</p></div>
912 <div class="listingblock">
913 <div class="content"><!-- Generator: GNU source-highlight 2.9
914 by Lorenzo Bettini
915 http://www.lorenzobettini.it
916 http://www.gnu.org/software/src-highlite -->
917 <pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main" %&gt;</span>
918 </tt></pre></div></div>
919 <div class="para"><p>To include <tt>public/javascripts/main.js</tt> and <tt>public/javascripts/columns.js</tt>:</p></div>
920 <div class="listingblock">
921 <div class="content"><!-- Generator: GNU source-highlight 2.9
922 by Lorenzo Bettini
923 http://www.lorenzobettini.it
924 http://www.gnu.org/software/src-highlite -->
925 <pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "columns" %&gt;</span>
926 </tt></pre></div></div>
927 <div class="para"><p>To include <tt>public/javascripts/main.js</tt> and <tt>public/photos/columns.js</tt>:</p></div>
928 <div class="listingblock">
929 <div class="content"><!-- Generator: GNU source-highlight 2.9
930 by Lorenzo Bettini
931 http://www.lorenzobettini.it
932 http://www.gnu.org/software/src-highlite -->
933 <pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "/photos/columns" %&gt;</span>
934 </tt></pre></div></div>
935 <div class="para"><p>To include <tt>http://example.com/main.js</tt>:</p></div>
936 <div class="listingblock">
937 <div class="content"><!-- Generator: GNU source-highlight 2.9
938 by Lorenzo Bettini
939 http://www.lorenzobettini.it
940 http://www.gnu.org/software/src-highlite -->
941 <pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "http://example.com/main.js" %&gt;</span>
942 </tt></pre></div></div>
943 <div class="para"><p>The <tt>defaults</tt> option loads the Prototype and Scriptaculous libraries:</p></div>
944 <div class="listingblock">
945 <div class="content"><!-- Generator: GNU source-highlight 2.9
946 by Lorenzo Bettini
947 http://www.lorenzobettini.it
948 http://www.gnu.org/software/src-highlite -->
949 <pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag :defaults %&gt;</span>
950 </tt></pre></div></div>
951 <div class="para"><p>The <tt>all</tt> option loads every javascript file in <tt>public/javascripts</tt>, starting with the Prototype and Scriptaculous libraries:</p></div>
952 <div class="listingblock">
953 <div class="content"><!-- Generator: GNU source-highlight 2.9
954 by Lorenzo Bettini
955 http://www.lorenzobettini.it
956 http://www.gnu.org/software/src-highlite -->
957 <pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag :all %&gt;</span>
958 </tt></pre></div></div>
959 <div class="para"><p>You can supply the <tt>:recursive</tt> option to load files in subfolders of <tt>public/javascripts</tt> as well:</p></div>
960 <div class="listingblock">
961 <div class="content"><!-- Generator: GNU source-highlight 2.9
962 by Lorenzo Bettini
963 http://www.lorenzobettini.it
964 http://www.gnu.org/software/src-highlite -->
965 <pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag :all, :recursive =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span>
966 </tt></pre></div></div>
967 <div class="para"><p>If you'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 &#8658; true</tt> in your <tt>javascript_include_tag</tt>:</p></div>
968 <div class="listingblock">
969 <div class="content"><!-- Generator: GNU source-highlight 2.9
970 by Lorenzo Bettini
971 http://www.lorenzobettini.it
972 http://www.gnu.org/software/src-highlite -->
973 <pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "columns", :cache =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span>
974 </tt></pre></div></div>
975 <div class="para"><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></div>
976 <div class="listingblock">
977 <div class="content"><!-- Generator: GNU source-highlight 2.9
978 by Lorenzo Bettini
979 http://www.lorenzobettini.it
980 http://www.gnu.org/software/src-highlite -->
981 <pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "columns", :cache =&gt;</span> <span style="color: #FF0000">'cache/main/display'</span> <span style="color: #990000">%&gt;</span>
982 </tt></pre></div></div>
983 <div class="para"><p></p></div>
984 <h4 id="_linking_to_css_files_with_tt_stylesheet_link_tag_tt">3.1.3. Linking to CSS Files with <tt>stylesheet_link_tag</tt></h4>
985 <div class="para"><p>The <tt>stylesheet_link_tag</tt> helper returns an HTML <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 URL, if you prefer. For example, to include <tt>public/stylesheets/main.cs</tt>:</p></div>
986 <div class="listingblock">
987 <div class="content"><!-- Generator: GNU source-highlight 2.9
988 by Lorenzo Bettini
989 http://www.lorenzobettini.it
990 http://www.gnu.org/software/src-highlite -->
991 <pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main" %&gt;</span>
992 </tt></pre></div></div>
993 <div class="para"><p>To include <tt>public/stylesheets/main.css</tt> and <tt>public/stylesheets/columns.css</tt>:</p></div>
994 <div class="listingblock">
995 <div class="content"><!-- Generator: GNU source-highlight 2.9
996 by Lorenzo Bettini
997 http://www.lorenzobettini.it
998 http://www.gnu.org/software/src-highlite -->
999 <pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "columns" %&gt;</span>
1000 </tt></pre></div></div>
1001 <div class="para"><p>To include <tt>public/stylesheets/main.css</tt> and <tt>public/photos/columns.css</tt>:</p></div>
1002 <div class="listingblock">
1003 <div class="content"><!-- Generator: GNU source-highlight 2.9
1004 by Lorenzo Bettini
1005 http://www.lorenzobettini.it
1006 http://www.gnu.org/software/src-highlite -->
1007 <pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "/photos/columns" %&gt;</span>
1008 </tt></pre></div></div>
1009 <div class="para"><p>To include <tt>http://example.com/main.cs</tt>:</p></div>
1010 <div class="listingblock">
1011 <div class="content"><!-- Generator: GNU source-highlight 2.9
1012 by Lorenzo Bettini
1013 http://www.lorenzobettini.it
1014 http://www.gnu.org/software/src-highlite -->
1015 <pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "http://example.com/main.cs" %&gt;</span>
1016 </tt></pre></div></div>
1017 <div class="para"><p>By default, <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 (:media, :rel, or :type):</p></div>
1018 <div class="listingblock">
1019 <div class="content"><!-- Generator: GNU source-highlight 2.9
1020 by Lorenzo Bettini
1021 http://www.lorenzobettini.it
1022 http://www.gnu.org/software/src-highlite -->
1023 <pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main_print", media =&gt;</span> <span style="color: #FF0000">"print"</span> <span style="color: #990000">%&gt;</span>
1024 </tt></pre></div></div>
1025 <div class="para"><p>The <tt>all</tt> option links every CSS file in <tt>public/stylesheets</tt>:</p></div>
1026 <div class="listingblock">
1027 <div class="content"><!-- Generator: GNU source-highlight 2.9
1028 by Lorenzo Bettini
1029 http://www.lorenzobettini.it
1030 http://www.gnu.org/software/src-highlite -->
1031 <pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag :all %&gt;</span>
1032 </tt></pre></div></div>
1033 <div class="para"><p>You can supply the <tt>:recursive</tt> option to link files in subfolders of <tt>public/stylesheets</tt> as well:</p></div>
1034 <div class="listingblock">
1035 <div class="content"><!-- Generator: GNU source-highlight 2.9
1036 by Lorenzo Bettini
1037 http://www.lorenzobettini.it
1038 http://www.gnu.org/software/src-highlite -->
1039 <pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag :all, :recursive =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span>
1040 </tt></pre></div></div>
1041 <div class="para"><p>If you're loading multiple CSS 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 &#8658; true</tt> in your <tt>stylesheet_link_tag</tt>:</p></div>
1042 <div class="listingblock">
1043 <div class="content"><!-- Generator: GNU source-highlight 2.9
1044 by Lorenzo Bettini
1045 http://www.lorenzobettini.it
1046 http://www.gnu.org/software/src-highlite -->
1047 <pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "columns", :cache =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span>
1048 </tt></pre></div></div>
1049 <div class="para"><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></div>
1050 <div class="listingblock">
1051 <div class="content"><!-- Generator: GNU source-highlight 2.9
1052 by Lorenzo Bettini
1053 http://www.lorenzobettini.it
1054 http://www.gnu.org/software/src-highlite -->
1055 <pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "columns", :cache =&gt;</span> <span style="color: #FF0000">'cache/main/display'</span> <span style="color: #990000">%&gt;</span>
1056 </tt></pre></div></div>
1057 <div class="para"><p></p></div>
1058 <h4 id="_linking_to_images_with_tt_image_tag_tt">3.1.4. Linking to Images with <tt>image_tag</tt></h4>
1059 <div class="para"><p>The <tt>image_tag</tt> helper builds an HTML <tt>&lt;image&gt;</tt> tag to the specified file. By default, files are loaded from <tt>public/images</tt>. If you don't specify an extension, .png is assumed by default:</p></div>
1060 <div class="listingblock">
1061 <div class="content"><!-- Generator: GNU source-highlight 2.9
1062 by Lorenzo Bettini
1063 http://www.lorenzobettini.it
1064 http://www.gnu.org/software/src-highlite -->
1065 <pre><tt><span style="color: #FF0000">&lt;%= image_tag "header" %&gt;</span>
1066 </tt></pre></div></div>
1067 <div class="para"><p>You can supply a path to the image if you like:</p></div>
1068 <div class="listingblock">
1069 <div class="content"><!-- Generator: GNU source-highlight 2.9
1070 by Lorenzo Bettini
1071 http://www.lorenzobettini.it
1072 http://www.gnu.org/software/src-highlite -->
1073 <pre><tt><span style="color: #FF0000">&lt;%= image_tag "icons/delete.gif" %&gt;</span>
1074 </tt></pre></div></div>
1075 <div class="para"><p>You can supply a hash of additional HTML options:</p></div>
1076 <div class="listingblock">
1077 <div class="content"><!-- Generator: GNU source-highlight 2.9
1078 by Lorenzo Bettini
1079 http://www.lorenzobettini.it
1080 http://www.gnu.org/software/src-highlite -->
1081 <pre><tt><span style="color: #FF0000">&lt;%= image_tag "icons/delete.gif", :height =&gt;</span> <span style="color: #993399">45</span> <span style="color: #990000">%&gt;</span>
1082 </tt></pre></div></div>
1083 <div class="para"><p>There are also three special options you can use with <tt>image_tag</tt>:</p></div>
1084 <div class="ilist"><ul>
1085 <li>
1086 <p>
1087 <tt>:alt</tt> specifies the alt text for the image (which defaults to the file name of the file, capitalized and with no extension)
1088 </p>
1089 </li>
1090 <li>
1091 <p>
1092 </p>
1093 </li>
1094 <li>
1095 <p>
1096 <tt>:mouseover</tt> sets an alternate image to be used when the onmouseover event is fired.
1097 </p>
1098 </li>
1099 </ul></div>
1100 <h3 id="_understanding_tt_yield_tt">3.2. Understanding <tt>yield</tt></h3>
1101 <div class="para"><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></div>
1102 <div class="listingblock">
1103 <div class="content"><!-- Generator: GNU source-highlight 2.9
1104 by Lorenzo Bettini
1105 http://www.lorenzobettini.it
1106 http://www.gnu.org/software/src-highlite -->
1107 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;html&gt;</span></span>
1108 <span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
1109 <span style="font-weight: bold"><span style="color: #0000FF">&lt;/head&gt;</span></span>
1110 <span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
1111 &lt;%= yield %&gt;
1112 <span style="font-weight: bold"><span style="color: #0000FF">&lt;hbody&gt;</span></span>
1113 <span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span>
1114 </tt></pre></div></div>
1115 <div class="para"><p>You can also create a layout with multiple yielding regions:</p></div>
1116 <div class="listingblock">
1117 <div class="content"><!-- Generator: GNU source-highlight 2.9
1118 by Lorenzo Bettini
1119 http://www.lorenzobettini.it
1120 http://www.gnu.org/software/src-highlite -->
1121 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;html&gt;</span></span>
1122 <span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
1123 &lt;%= yield :head %&gt;
1124 <span style="font-weight: bold"><span style="color: #0000FF">&lt;/head&gt;</span></span>
1125 <span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
1126 &lt;%= yield %&gt;
1127 <span style="font-weight: bold"><span style="color: #0000FF">&lt;hbody&gt;</span></span>
1128 <span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span>
1129 </tt></pre></div></div>
1130 <div class="para"><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></div>
1131 <h3 id="_using_tt_content_for_tt">3.3. Using <tt>content_for</tt></h3>
1132 <div class="para"><p>The <tt>content_for</tt> method allows you to insert content into a <tt>yield</tt> block in your layout. You only use <tt>content_for</tt> to insert content in named yields. For example, this view would work with the layout that you just saw:</p></div>
1133 <div class="listingblock">
1134 <div class="content"><!-- Generator: GNU source-highlight 2.9
1135 by Lorenzo Bettini
1136 http://www.lorenzobettini.it
1137 http://www.gnu.org/software/src-highlite -->
1138 <pre><tt>&lt;% content_for :head do %&gt;
1139 <span style="font-weight: bold"><span style="color: #0000FF">&lt;title&gt;</span></span>A simple page<span style="font-weight: bold"><span style="color: #0000FF">&lt;/title&gt;</span></span>
1140 &lt;% end %&gt;
1141
1142 <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Hello, Rails!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
1143 </tt></pre></div></div>
1144 <div class="para"><p>The result of rendering this page into the supplied layout would be this HTML:</p></div>
1145 <div class="listingblock">
1146 <div class="content"><!-- Generator: GNU source-highlight 2.9
1147 by Lorenzo Bettini
1148 http://www.lorenzobettini.it
1149 http://www.gnu.org/software/src-highlite -->
1150 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;html&gt;</span></span>
1151 <span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
1152 <span style="font-weight: bold"><span style="color: #0000FF">&lt;title&gt;</span></span>A simple page<span style="font-weight: bold"><span style="color: #0000FF">&lt;/title&gt;</span></span>
1153 <span style="font-weight: bold"><span style="color: #0000FF">&lt;/head&gt;</span></span>
1154 <span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
1155 <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Hello, Rails!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
1156 <span style="font-weight: bold"><span style="color: #0000FF">&lt;hbody&gt;</span></span>
1157 <span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span>
1158 </tt></pre></div></div>
1159 <div class="para"><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's also useful for inserting tags that load page-specific javascript or css files into the header of an otherwise-generic layout.</p></div>
1160 <h3 id="_using_partials">3.4. Using Partials</h3>
1161 <div class="para"><p>Partial templates - usually just called "partials" - are another device for breaking apart 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></div>
1162 <h4 id="_naming_partials">3.4.1. Naming Partials</h4>
1163 <div class="para"><p>To render a partial as part of a view, you use the <tt>render</tt> method within the view, and include the <tt>:partial</tt> option:</p></div>
1164 <div class="listingblock">
1165 <div class="content"><!-- Generator: GNU source-highlight 2.9
1166 by Lorenzo Bettini
1167 http://www.lorenzobettini.it
1168 http://www.gnu.org/software/src-highlite -->
1169 <pre><tt><span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #FF0000">"menu"</span> <span style="color: #990000">%&gt;</span>
1170 </tt></pre></div></div>
1171 <div class="para"><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're pulling in a partial from another folder:</p></div>
1172 <div class="listingblock">
1173 <div class="content"><!-- Generator: GNU source-highlight 2.9
1174 by Lorenzo Bettini
1175 http://www.lorenzobettini.it
1176 http://www.gnu.org/software/src-highlite -->
1177 <pre><tt><span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #FF0000">"shared/menu"</span> <span style="color: #990000">%&gt;</span>
1178 </tt></pre></div></div>
1179 <div class="para"><p>That code will pull in the partial from <tt>app/views/shared/_menu.html.erb</tt>.</p></div>
1180 <h4 id="_using_partials_to_simplify_views">3.4.2. Using Partials to Simplify Views</h4>
1181 <div class="para"><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's going on more easily. For example, you might have a view that looked like this:</p></div>
1182 <div class="listingblock">
1183 <div class="content"><!-- Generator: GNU source-highlight 2.9
1184 by Lorenzo Bettini
1185 http://www.lorenzobettini.it
1186 http://www.gnu.org/software/src-highlite -->
1187 <pre><tt>&lt;%= render :partial =&gt; "shared/ad_banner" %&gt;
1188
1189 <span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Products<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
1190
1191 <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Here are a few of our fine products:<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
1192 ...
1193
1194 &lt;%= render :partial =&gt; "shared/footer" %&gt;
1195 </tt></pre></div></div>
1196 <div class="para"><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't need to see the details of these sections when you're concentrating on a particular page.</p></div>
1197 <div class="admonitionblock">
1198 <table><tr>
1199 <td class="icon">
1200 <img src="./images/icons/tip.png" alt="Tip" />
1201 </td>
1202 <td class="content">For content that is shared among all pages in your application, you can use partials directly from layouts.</td>
1203 </tr></table>
1204 </div>
1205 <h4 id="_partial_layouts">3.4.3. Partial Layouts</h4>
1206 <div class="para"><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></div>
1207 <div class="listingblock">
1208 <div class="content"><!-- Generator: GNU source-highlight 2.9
1209 by Lorenzo Bettini
1210 http://www.lorenzobettini.it
1211 http://www.gnu.org/software/src-highlite -->
1212 <pre><tt>&lt;%= render :partial =&gt; "link_area", :layout =&gt; "graybar" %&gt;
1213 </tt></pre></div></div>
1214 <div class="para"><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></div>
1215 <h4 id="_passing_local_variables">3.4.4. Passing Local Variables</h4>
1216 <div class="para"><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></div>
1217 <div class="para"><p><tt>new.html.erb</tt>:</p></div>
1218 <div class="listingblock">
1219 <div class="content"><!-- Generator: GNU source-highlight 2.9
1220 by Lorenzo Bettini
1221 http://www.lorenzobettini.it
1222 http://www.gnu.org/software/src-highlite -->
1223 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>New zone<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
1224 &lt;%= error_messages_for :zone %&gt;
1225 &lt;%= render :partial =&gt; "form", :locals =&gt; { :button_label =&gt; "Create zone", :zone =&gt; @zone } %&gt;
1226 </tt></pre></div></div>
1227 <div class="para"><p><tt>edit.html.erb</tt>:</p></div>
1228 <div class="listingblock">
1229 <div class="content"><!-- Generator: GNU source-highlight 2.9
1230 by Lorenzo Bettini
1231 http://www.lorenzobettini.it
1232 http://www.gnu.org/software/src-highlite -->
1233 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Editing zone<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
1234 &lt;%= error_messages_for :zone %&gt;
1235 &lt;%= render :partial =&gt; "form", :locals =&gt; { :button_label =&gt; "Update zone", :zone =&gt; @zone } %&gt;
1236 </tt></pre></div></div>
1237 <div class="para"><p><tt>_form.html.erb:</tt></p></div>
1238 <div class="listingblock">
1239 <div class="content"><!-- Generator: GNU source-highlight 2.9
1240 by Lorenzo Bettini
1241 http://www.lorenzobettini.it
1242 http://www.gnu.org/software/src-highlite -->
1243 <pre><tt>&lt;% form_for(zone) do |f| %&gt;
1244 <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
1245 <span style="font-weight: bold"><span style="color: #0000FF">&lt;b&gt;</span></span>Zone name<span style="font-weight: bold"><span style="color: #0000FF">&lt;/b&gt;&lt;br</span></span> <span style="font-weight: bold"><span style="color: #0000FF">/&gt;</span></span>
1246 &lt;%= f.text_field :name %&gt;
1247 <span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
1248 <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
1249 &lt;%= f.submit button_label %&gt;
1250 <span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
1251 &lt;% end %&gt;
1252 </tt></pre></div></div>
1253 <div class="para"><p>Although the same partial will be rendered into both views, the label on the submit button is controlled by a local variable passed into the partial.</p></div>
1254 <div class="para"><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></div>
1255 <div class="listingblock">
1256 <div class="content"><!-- Generator: GNU source-highlight 2.9
1257 by Lorenzo Bettini
1258 http://www.lorenzobettini.it
1259 http://www.gnu.org/software/src-highlite -->
1260 <pre><tt>&lt;%= render :partial =&gt; "customer", :object =&gt; @new_customer %&gt;
1261 </tt></pre></div></div>
1262 <div class="para"><p>Within the <tt>customer</tt> partial, the <tt>@customer</tt> variable will refer to <tt>@new_customer</tt> from the parent view.</p></div>
1263 <div class="admonitionblock">
1264 <table><tr>
1265 <td class="icon">
1266 <img src="./images/icons/warning.png" alt="Warning" />
1267 </td>
1268 <td class="content">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 is deprecated in Rails 2.2 and will be removed in a future version.</td>
1269 </tr></table>
1270 </div>
1271 <div class="para"><p>If you have an instance of a model to render into a partial, you can use a shorthand syntax:</p></div>
1272 <div class="listingblock">
1273 <div class="content"><!-- Generator: GNU source-highlight 2.9
1274 by Lorenzo Bettini
1275 http://www.lorenzobettini.it
1276 http://www.gnu.org/software/src-highlite -->
1277 <pre><tt>&lt;%= render :partial =&gt; @customer %&gt;
1278 </tt></pre></div></div>
1279 <div class="para"><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.</p></div>
1280 <h4 id="_rendering_collections">3.4.5. Rendering Collections</h4>
1281 <div class="para"><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></div>
1282 <div class="para"><p><tt>index.html.erb</tt>:</p></div>
1283 <div class="listingblock">
1284 <div class="content"><!-- Generator: GNU source-highlight 2.9
1285 by Lorenzo Bettini
1286 http://www.lorenzobettini.it
1287 http://www.gnu.org/software/src-highlite -->
1288 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Products<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
1289 &lt;%= render :partial =&gt; "product", :collection =&gt; @products %&gt;
1290 </tt></pre></div></div>
1291 <div class="para"><p><tt>_product.html.erb</tt>:</p></div>
1292 <div class="listingblock">
1293 <div class="content"><!-- Generator: GNU source-highlight 2.9
1294 by Lorenzo Bettini
1295 http://www.lorenzobettini.it
1296 http://www.gnu.org/software/src-highlite -->
1297 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Product Name: &lt;%= product.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
1298 </tt></pre></div></div>
1299 <div class="para"><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, and within the +_product</tt> partial, you can refer to <tt>product</tt> to get the instance that is being rendered. To use a custom local variable name within the partial, specify the <tt>:as</tt> option in the call to the partial:</p></div>
1300 <div class="listingblock">
1301 <div class="content"><!-- Generator: GNU source-highlight 2.9
1302 by Lorenzo Bettini
1303 http://www.lorenzobettini.it
1304 http://www.gnu.org/software/src-highlite -->
1305 <pre><tt>&lt;%= render :partial =&gt; "product", :collection =&gt; @products, :as =&gt; :item %&gt;
1306 </tt></pre></div></div>
1307 <div class="para"><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></div>
1308 <div class="admonitionblock">
1309 <table><tr>
1310 <td class="icon">
1311 <img src="./images/icons/tip.png" alt="Tip" />
1312 </td>
1313 <td class="content">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'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.</td>
1314 </tr></table>
1315 </div>
1316 <div class="para"><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></div>
1317 <div class="listingblock">
1318 <div class="content"><!-- Generator: GNU source-highlight 2.9
1319 by Lorenzo Bettini
1320 http://www.lorenzobettini.it
1321 http://www.gnu.org/software/src-highlite -->
1322 <pre><tt>&lt;%= render :partial =&gt; "product", :collection =&gt; @products, :spacer_template =&gt; "product_ruler" %&gt;
1323 </tt></pre></div></div>
1324 <div class="para"><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></div>
1325 <div class="para"><p>There's also a shorthand syntax available for rendering collections. For example, if <tt>@products</tt> is a collection of products, you can render the collection this way:</p></div>
1326 <div class="para"><p><tt>index.html.erb</tt>:</p></div>
1327 <div class="listingblock">
1328 <div class="content"><!-- Generator: GNU source-highlight 2.9
1329 by Lorenzo Bettini
1330 http://www.lorenzobettini.it
1331 http://www.gnu.org/software/src-highlite -->
1332 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Products<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
1333 &lt;%= render :partial =&gt; @products %&gt;
1334 </tt></pre></div></div>
1335 <div class="para"><p><tt>_product.html.erb</tt>:</p></div>
1336 <div class="listingblock">
1337 <div class="content"><!-- Generator: GNU source-highlight 2.9
1338 by Lorenzo Bettini
1339 http://www.lorenzobettini.it
1340 http://www.gnu.org/software/src-highlite -->
1341 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Product Name: &lt;%= product.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
1342 </tt></pre></div></div>
1343 <div class="para"><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></div>
1344 <div class="para"><p><tt>index.html.erb</tt>:</p></div>
1345 <div class="listingblock">
1346 <div class="content"><!-- Generator: GNU source-highlight 2.9
1347 by Lorenzo Bettini
1348 http://www.lorenzobettini.it
1349 http://www.gnu.org/software/src-highlite -->
1350 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Contacts<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
1351 &lt;%= render :partial =&gt; [customer1, employee1, customer2, employee2] %&gt;
1352 </tt></pre></div></div>
1353 <div class="para"><p><tt>_customer.html.erb</tt>:</p></div>
1354 <div class="listingblock">
1355 <div class="content"><!-- Generator: GNU source-highlight 2.9
1356 by Lorenzo Bettini
1357 http://www.lorenzobettini.it
1358 http://www.gnu.org/software/src-highlite -->
1359 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Name: &lt;%= customer.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
1360 </tt></pre></div></div>
1361 <div class="para"><p><tt>_employee.html.erb</tt>:</p></div>
1362 <div class="listingblock">
1363 <div class="content"><!-- Generator: GNU source-highlight 2.9
1364 by Lorenzo Bettini
1365 http://www.lorenzobettini.it
1366 http://www.gnu.org/software/src-highlite -->
1367 <pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Name: &lt;%= employee.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
1368 </tt></pre></div></div>
1369 <div class="para"><p>In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.</p></div>
1370 </div>
1371 <h2 id="_changelog">4. Changelog</h2>
1372 <div class="sectionbody">
1373 <div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15">Lighthouse ticket</a></p></div>
1374 <div class="ilist"><ul>
1375 <li>
1376 <p>
1377 November 9, 2008: Added partial collection counter by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
1378 </p>
1379 </li>
1380 <li>
1381 <p>
1382 November 1, 2008: Added <tt>:js</tt> option for <tt>render</tt> by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
1383 </p>
1384 </li>
1385 <li>
1386 <p>
1387 October 16, 2008: Ready for publication by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
1388 </p>
1389 </li>
1390 <li>
1391 <p>
1392 October 4, 2008: Additional info on partials (<tt>:object</tt>, <tt>:as</tt>, and <tt>:spacer_template</tt>) by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
1393 </p>
1394 </li>
1395 <li>
1396 <p>
1397 September 28, 2008: First draft by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
1398 </p>
1399 </li>
1400 </ul></div>
1401 </div>
1402
1403 </div>
1404 </div>
1405 </body>
1406 </html>