railties 3.1.1 → 3.1.2.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +2382 -0
- data/guides/output/2_2_release_notes.html +565 -0
- data/guides/output/2_3_release_notes.html +713 -0
- data/guides/output/3_0_release_notes.html +652 -0
- data/guides/output/3_1_release_notes.html +670 -0
- data/guides/output/action_controller_overview.html +925 -0
- data/guides/output/action_mailer_basics.html +658 -0
- data/guides/output/action_view_overview.html +1471 -0
- data/guides/output/active_model_basics.html +349 -0
- data/guides/output/active_record_basics.html +364 -0
- data/guides/output/active_record_querying.html +1272 -0
- data/guides/output/active_record_validations_callbacks.html +1292 -0
- data/guides/output/active_resource_basics.html +252 -0
- data/guides/output/active_support_core_extensions.html +3374 -0
- data/guides/output/ajax_on_rails.html +412 -0
- data/guides/output/api_documentation_guidelines.html +317 -0
- data/guides/output/asset_pipeline.html +691 -0
- data/guides/output/association_basics.html +1742 -0
- data/guides/output/caching_with_rails.html +533 -0
- data/guides/output/command_line.html +662 -0
- data/guides/output/configuring.html +811 -0
- data/guides/output/contribute.html +216 -0
- data/guides/output/contributing_to_ruby_on_rails.html +465 -0
- data/guides/output/credits.html +210 -0
- data/guides/output/debugging_rails_applications.html +791 -0
- data/guides/output/engines.html +673 -0
- data/guides/output/form_helpers.html +850 -0
- data/guides/output/generators.html +725 -0
- data/guides/output/getting_started.html +1980 -0
- data/guides/output/i18n.html +1054 -0
- data/guides/output/images/belongs_to.png +0 -0
- data/guides/output/images/book_icon.gif +0 -0
- data/guides/output/images/bullet.gif +0 -0
- data/guides/output/images/challenge.png +0 -0
- data/guides/output/images/chapters_icon.gif +0 -0
- data/guides/output/images/check_bullet.gif +0 -0
- data/guides/output/images/credits_pic_blank.gif +0 -0
- data/guides/output/images/csrf.png +0 -0
- data/guides/output/images/customized_error_messages.png +0 -0
- data/guides/output/images/edge_badge.png +0 -0
- data/guides/output/images/error_messages.png +0 -0
- data/guides/output/images/feature_tile.gif +0 -0
- data/guides/output/images/footer_tile.gif +0 -0
- data/guides/output/images/fxn.png +0 -0
- data/guides/output/images/grey_bullet.gif +0 -0
- data/guides/output/images/habtm.png +0 -0
- data/guides/output/images/has_many.png +0 -0
- data/guides/output/images/has_many_through.png +0 -0
- data/guides/output/images/has_one.png +0 -0
- data/guides/output/images/has_one_through.png +0 -0
- data/guides/output/images/header_backdrop.png +0 -0
- data/guides/output/images/header_tile.gif +0 -0
- data/guides/output/images/i18n/demo_html_safe.png +0 -0
- data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translated_en.png +0 -0
- data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translation_missing.png +0 -0
- data/guides/output/images/i18n/demo_untranslated.png +0 -0
- data/guides/output/images/icons/README +5 -0
- data/guides/output/images/icons/callouts/1.png +0 -0
- data/guides/output/images/icons/callouts/10.png +0 -0
- data/guides/output/images/icons/callouts/11.png +0 -0
- data/guides/output/images/icons/callouts/12.png +0 -0
- data/guides/output/images/icons/callouts/13.png +0 -0
- data/guides/output/images/icons/callouts/14.png +0 -0
- data/guides/output/images/icons/callouts/15.png +0 -0
- data/guides/output/images/icons/callouts/2.png +0 -0
- data/guides/output/images/icons/callouts/3.png +0 -0
- data/guides/output/images/icons/callouts/4.png +0 -0
- data/guides/output/images/icons/callouts/5.png +0 -0
- data/guides/output/images/icons/callouts/6.png +0 -0
- data/guides/output/images/icons/callouts/7.png +0 -0
- data/guides/output/images/icons/callouts/8.png +0 -0
- data/guides/output/images/icons/callouts/9.png +0 -0
- data/guides/output/images/icons/caution.png +0 -0
- data/guides/output/images/icons/example.png +0 -0
- data/guides/output/images/icons/home.png +0 -0
- data/guides/output/images/icons/important.png +0 -0
- data/guides/output/images/icons/next.png +0 -0
- data/guides/output/images/icons/note.png +0 -0
- data/guides/output/images/icons/prev.png +0 -0
- data/guides/output/images/icons/tip.png +0 -0
- data/guides/output/images/icons/up.png +0 -0
- data/guides/output/images/icons/warning.png +0 -0
- data/guides/output/images/jaimeiniesta.jpg +0 -0
- data/guides/output/images/nav_arrow.gif +0 -0
- data/guides/output/images/polymorphic.png +0 -0
- data/guides/output/images/posts_index.png +0 -0
- data/guides/output/images/radar.png +0 -0
- data/guides/output/images/rails_guides_logo.gif +0 -0
- data/guides/output/images/rails_logo_remix.gif +0 -0
- data/guides/output/images/rails_welcome.png +0 -0
- data/guides/output/images/session_fixation.png +0 -0
- data/guides/output/images/tab_grey.gif +0 -0
- data/guides/output/images/tab_info.gif +0 -0
- data/guides/output/images/tab_note.gif +0 -0
- data/guides/output/images/tab_red.gif +0 -0
- data/guides/output/images/tab_yellow.gif +0 -0
- data/guides/output/images/tab_yellow.png +0 -0
- data/guides/output/images/validation_error_messages.png +0 -0
- data/guides/output/images/vijaydev.jpg +0 -0
- data/guides/output/index.html +300 -0
- data/guides/output/initialization.html +1087 -0
- data/guides/output/javascripts/guides.js +7 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
- data/guides/output/javascripts/syntaxhighlighter/shCore.js +17 -0
- data/guides/output/layout.html +312 -0
- data/guides/output/layouts_and_rendering.html +1257 -0
- data/guides/output/migrations.html +751 -0
- data/guides/output/nested_model_forms.html +350 -0
- data/guides/output/performance_testing.html +858 -0
- data/guides/output/plugins.html +590 -0
- data/guides/output/rails_application_templates.html +368 -0
- data/guides/output/rails_on_rack.html +408 -0
- data/guides/output/routing.html +1246 -0
- data/guides/output/ruby_on_rails_guides_guidelines.html +218 -0
- data/guides/output/security.html +968 -0
- data/guides/output/stylesheets/fixes.css +16 -0
- data/guides/output/stylesheets/main.css +445 -0
- data/guides/output/stylesheets/print.css +52 -0
- data/guides/output/stylesheets/reset.css +43 -0
- data/guides/output/stylesheets/style.css +13 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCore.css +226 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
- data/guides/output/testing.html +1182 -0
- data/guides/source/active_record_validations_callbacks.textile +3 -3
- data/guides/source/active_support_core_extensions.textile +2 -2
- data/guides/source/asset_pipeline.textile +7 -5
- data/guides/source/association_basics.textile +2 -4
- data/guides/source/command_line.textile +1 -1
- data/guides/source/configuring.textile +4 -2
- data/guides/source/contributing_to_ruby_on_rails.textile +6 -6
- data/guides/source/initialization.textile +2 -2
- data/guides/source/performance_testing.textile +1 -1
- data/guides/source/routing.textile +1 -1
- data/guides/source/security.textile +2 -2
- data/lib/rails/commands/server.rb +1 -1
- data/lib/rails/engine.rb +1 -1
- data/lib/rails/generators/app_base.rb +2 -1
- data/lib/rails/generators/base.rb +3 -3
- data/lib/rails/generators/rails/app/templates/gitignore +15 -5
- data/lib/rails/rack.rb +0 -1
- data/lib/rails/tasks/documentation.rake +7 -7
- data/lib/rails/tasks/misc.rake +0 -2
- data/lib/rails/test_unit/testing.rake +2 -0
- data/lib/rails/version.rb +2 -2
- metadata +495 -326
- data/CHANGELOG +0 -2371
- data/lib/rails/rack/content_length.rb +0 -38
@@ -0,0 +1,1257 @@
|
|
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’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>
|
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’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’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’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’ve heard that Rails promotes “convention over configuration”. Default rendering is an excellent example of this. By default, controllers in Rails automatically render views with names that correspond to valid routes. For example, if you have this code in your <tt>BooksController</tt> class:</p>
|
130
|
+
<div class="code_container">
|
131
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
132
|
+
class BooksController < 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
|
+
<h1>Books are coming soon!</h1>
|
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 “Books are coming soon!” 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 < ApplicationController
|
153
|
+
def index
|
154
|
+
@books = Book.all
|
155
|
+
end
|
156
|
+
end
|
157
|
+
</pre>
|
158
|
+
</div>
|
159
|
+
<p>Note that we don’t have explicit render at the end of the index action in accordance with “convention over configuration” principle. The rule is that if you do not explicitly render something at the end of a controller action, Rails will automatically look for the <tt>action_name.html.erb</tt> template in the controller’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
|
+
<h1>Listing Books</h1>
|
164
|
+
|
165
|
+
<table>
|
166
|
+
<tr>
|
167
|
+
<th>Title</th>
|
168
|
+
<th>Summary</th>
|
169
|
+
<th></th>
|
170
|
+
<th></th>
|
171
|
+
<th></th>
|
172
|
+
</tr>
|
173
|
+
|
174
|
+
<% @books.each do |book| %>
|
175
|
+
<tr>
|
176
|
+
<td><%= book.title %></td>
|
177
|
+
<td><%= book.content %></td>
|
178
|
+
<td><%= link_to 'Show', book %></td>
|
179
|
+
<td><%= link_to 'Edit', edit_book_path(book) %></td>
|
180
|
+
<td><%= link_to 'Remove', book, :confirm => 'Are you sure?', :method => :delete %></td>
|
181
|
+
</tr>
|
182
|
+
<% end %>
|
183
|
+
</table>
|
184
|
+
|
185
|
+
<br />
|
186
|
+
|
187
|
+
<%= link_to 'New book', new_book_path %>
|
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’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’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 => 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’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’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 "edit"
|
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 => "edit"
|
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’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’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 => '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’s entirely outside of your application (perhaps you’re sharing views between two Rails applications):</p>
|
277
|
+
<div class="code_container">
|
278
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
279
|
+
render "/u/apps/warehouse_app/current/app/views/products/show"
|
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 =>
|
286
|
+
"/u/apps/warehouse_app/current/app/views/products/show"
|
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’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’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 => :edit
|
299
|
+
render 'edit'
|
300
|
+
render 'edit.html.erb'
|
301
|
+
render :action => 'edit'
|
302
|
+
render :action => 'edit.html.erb'
|
303
|
+
render 'books/edit'
|
304
|
+
render 'books/edit.html.erb'
|
305
|
+
render :template => 'books/edit'
|
306
|
+
render :template => '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 => '/path/to/rails/app/views/books/edit'
|
310
|
+
render :file => '/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’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 =>
|
319
|
+
"<% products.each do |p| %><p><%= p.name %></p><% end %>"
|
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 =>
|
327
|
+
"xml.p {'Horrid coding practice!'}", :type => :builder
|
328
|
+
</pre>
|
329
|
+
</div>
|
330
|
+
<h5 id="rendering-text">2.2.7 Rendering Text</h5>
|
331
|
+
<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>
|
332
|
+
<div class="code_container">
|
333
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
334
|
+
render :text => "OK"
|
335
|
+
</pre>
|
336
|
+
</div>
|
337
|
+
<div class="info"><p>Rendering pure text is most useful when you’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 => @product
|
344
|
+
</pre>
|
345
|
+
</div>
|
346
|
+
<div class="info"><p>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.</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 => @product
|
352
|
+
</pre>
|
353
|
+
</div>
|
354
|
+
<div class="info"><p>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.</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 => "alert('Hello Rails');"
|
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 => filename, :content_type => '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’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 => '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 => 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 => 500
|
397
|
+
render :status => :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 => photo, :location => 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 < ApplicationController
|
415
|
+
layout "inventory"
|
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 < ActionController::Base
|
425
|
+
layout "main"
|
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 < 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? ? "special" : "products"
|
445
|
+
end
|
446
|
+
|
447
|
+
end
|
448
|
+
</pre>
|
449
|
+
</div>
|
450
|
+
<p>Now, if the current user is a special user, they’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 < 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 < ApplicationController
|
464
|
+
layout "product", :except => [: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 < ActionController::Base
|
477
|
+
layout "main"
|
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 < 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 < PostsController
|
496
|
+
layout "special"
|
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 < 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 => "old"
|
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 “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>
|
530
|
+
<p>For example, here’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 => "special_show"
|
537
|
+
end
|
538
|
+
render :action => "regular_show"
|
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 – 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’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 => "special_show" and return
|
549
|
+
end
|
550
|
+
render :action => "regular_show"
|
551
|
+
end
|
552
|
+
</pre>
|
553
|
+
</div>
|
554
|
+
<p>Make sure to use <tt>and return</tt> and not <tt>&& return</tt>, since <tt>&& 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 => "special_show"
|
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’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’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’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 => 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’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 => "index"
|
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’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 => :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 "index", :alert => '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 => 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><head></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’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
|
+
<%= auto_discovery_link_tag(:rss, {:action => "feed"},
|
703
|
+
{:title => "RSS Feed"}) %>
|
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 “alternate”.</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, “<span class="caps">ATOM</span>” or “<span class="caps">RSS</span>”.</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
|
+
<%= javascript_include_tag "main" %>
|
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
|
+
<%= javascript_include_tag "main", "columns" %>
|
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
|
+
<%= javascript_include_tag "main", "/photos/columns" %>
|
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
|
+
<%= javascript_include_tag "http://example.com/main.js" %>
|
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
|
+
<%= javascript_include_tag :defaults %>
|
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
|
+
<%= javascript_include_tag :all %>
|
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
|
+
<%= javascript_include_tag :all, :recursive => true %>
|
761
|
+
</pre>
|
762
|
+
</div>
|
763
|
+
<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 => 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
|
+
<%= javascript_include_tag "main", "columns", :cache => true %>
|
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
|
+
<%= javascript_include_tag "main", "columns",
|
773
|
+
:cache => 'cache/main/display' %>
|
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><link></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
|
+
<%= stylesheet_link_tag "main" %>
|
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
|
+
<%= stylesheet_link_tag "main", "columns" %>
|
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
|
+
<%= stylesheet_link_tag "main", "/photos/columns" %>
|
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
|
+
<%= stylesheet_link_tag "http://example.com/main.css" %>
|
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
|
+
<%= stylesheet_link_tag "main_print", :media => "print" %>
|
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
|
+
<%= stylesheet_link_tag :all %>
|
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
|
+
<%= stylesheet_link_tag :all, :recursive => true %>
|
818
|
+
</pre>
|
819
|
+
</div>
|
820
|
+
<p>If you’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
|
+
<%= stylesheet_link_tag "main", "columns", :cache => true %>
|
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
|
+
<%= stylesheet_link_tag "main", "columns",
|
830
|
+
:cache => 'cache/main/display' %>
|
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><img /></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
|
+
<%= image_tag "header.png" %>
|
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
|
+
<%= image_tag "icons/delete.gif" %>
|
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
|
+
<%= image_tag "icons/delete.gif", {:height => 45} %>
|
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
|
+
<%= image_tag "home.gif", :onmouseover => "menu/home_highlight.gif" %>
|
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
|
+
<%= image_tag "home.gif" %>
|
864
|
+
<%= image_tag "home.gif", :alt => "Home" %>
|
865
|
+
</pre>
|
866
|
+
</div>
|
867
|
+
<p>You can also specify a special size tag, in the format “{width}x{height}”:</p>
|
868
|
+
<div class="code_container">
|
869
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
870
|
+
<%= image_tag "home.gif", :size => "50x20" %>
|
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
|
+
<%= image_tag "home.gif", :alt => "Go Home",
|
877
|
+
:id => "HomeImage",
|
878
|
+
:class => 'nav_bar' %>
|
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><video></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
|
+
<%= video_tag "movie.ogg" %>
|
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
|
+
<video src="/videos/movie.ogg" />
|
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><video></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
|
+
<%= video_tag ["trailer.ogg", "movie.ogg"] %>
|
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
|
+
<video><source src="trailer.ogg" /><source src="movie.ogg" /></video>
|
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><audio></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
|
+
<%= audio_tag "music.mp3" %>
|
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
|
+
<%= audio_tag "music/first_song.mp3" %>
|
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
|
+
<html>
|
940
|
+
<head>
|
941
|
+
</head>
|
942
|
+
<body>
|
943
|
+
<%= yield %>
|
944
|
+
</body>
|
945
|
+
</html>
|
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
|
+
<html>
|
952
|
+
<head>
|
953
|
+
<%= yield :head %>
|
954
|
+
</head>
|
955
|
+
<body>
|
956
|
+
<%= yield %>
|
957
|
+
</body>
|
958
|
+
</html>
|
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
|
+
<% content_for :head do %>
|
967
|
+
<title>A simple page</title>
|
968
|
+
<% end %>
|
969
|
+
|
970
|
+
<p>Hello, Rails!</p>
|
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
|
+
<html>
|
977
|
+
<head>
|
978
|
+
<title>A simple page</title>
|
979
|
+
</head>
|
980
|
+
<body>
|
981
|
+
<p>Hello, Rails!</p>
|
982
|
+
</body>
|
983
|
+
</html>
|
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’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 – usually just called “partials” – are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.</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
|
+
<%= render "menu" %>
|
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’re pulling in a partial from another folder:</p>
|
997
|
+
<div class="code_container">
|
998
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
999
|
+
<%= render "shared/menu" %>
|
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’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
|
+
<%= render "shared/ad_banner" %>
|
1008
|
+
|
1009
|
+
<h1>Products</h1>
|
1010
|
+
|
1011
|
+
<p>Here are a few of our fine products:</p>
|
1012
|
+
...
|
1013
|
+
|
1014
|
+
<%= render "shared/footer" %>
|
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’t need to see the details of these sections when you’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
|
+
<%= render :partial => "link_area", :layout => "graybar" %>
|
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
|
+
<h1>New zone</h1>
|
1036
|
+
<%= error_messages_for :zone %>
|
1037
|
+
<%= render :partial => "form", :locals => { :zone => @zone } %>
|
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
|
+
<h1>Editing zone</h1>
|
1046
|
+
<%= error_messages_for :zone %>
|
1047
|
+
<%= render :partial => "form", :locals => { :zone => @zone } %>
|
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
|
+
<%= form_for(zone) do |f| %>
|
1056
|
+
<p>
|
1057
|
+
<b>Zone name</b><br />
|
1058
|
+
<%= f.text_field :name %>
|
1059
|
+
</p>
|
1060
|
+
<p>
|
1061
|
+
<%= f.submit %>
|
1062
|
+
</p>
|
1063
|
+
<% end %>
|
1064
|
+
</pre>
|
1065
|
+
</div>
|
1066
|
+
<p>Although the same partial will be rendered into both views, Action View’s submit helper will return “Create Zone” for the new action and “Update Zone” for the edit action.</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
|
+
<%= render :partial => "customer", :object => @new_customer %>
|
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
|
+
<%= render @customer %>
|
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
|
+
<h1>Products</h1>
|
1090
|
+
<%= render :partial => "product", :collection => @products %>
|
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
|
+
<p>Product Name: <%= product.name %></p>
|
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
|
+
<h1>Products</h1>
|
1106
|
+
<%= render @products %>
|
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
|
+
<h1>Products</h1>
|
1114
|
+
<%= render(@products) || 'There are no products available.' %>
|
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
|
+
<h1>Contacts</h1>
|
1123
|
+
<%= render [customer1, employee1, customer2, employee2] %>
|
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
|
+
<p>Customer: <%= customer.name %></p>
|
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
|
+
<p>Employee: <%= employee.name %></p>
|
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
|
+
<%= render :partial => "product", :collection => @products, :as => :item %>
|
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
|
+
<%= render :partial => 'products', :collection => @products,
|
1155
|
+
:as => :item, :locals => {:title => "Products Page"} %>
|
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’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
|
+
<%= render @products, :spacer_template => "product_ruler" %>
|
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’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
|
+
<html>
|
1177
|
+
<head>
|
1178
|
+
<title><%= @page_title or 'Page Title' %></title>
|
1179
|
+
<%= stylesheet_link_tag 'layout' %>
|
1180
|
+
<style type="text/css"><%= yield :stylesheets %></style>
|
1181
|
+
</head>
|
1182
|
+
<body>
|
1183
|
+
<div id="top_menu">Top menu items here</div>
|
1184
|
+
<div id="menu">Menu items here</div>
|
1185
|
+
<div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
|
1186
|
+
</body>
|
1187
|
+
</html>
|
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
|
+
<% content_for :stylesheets do %>
|
1197
|
+
#top_menu {display: none}
|
1198
|
+
#right_menu {float: right; background-color: yellow; color: black}
|
1199
|
+
<% end %>
|
1200
|
+
<% content_for :content do %>
|
1201
|
+
<div id="right_menu">Right menu items here</div>
|
1202
|
+
<%= content_for?(:news_content) ? yield(:news_content) : yield %>
|
1203
|
+
<% end %>
|
1204
|
+
<%= render :template => 'layouts/application' %>
|
1205
|
+
</pre>
|
1206
|
+
</div>
|
1207
|
+
<p>That’s it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the “content” div.</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>
|