rails 4.2.0.beta3 → 4.2.0.beta4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -7
- data/guides/output/2_2_release_notes.html +724 -0
- data/guides/output/2_3_release_notes.html +870 -0
- data/guides/output/3_0_release_notes.html +773 -0
- data/guides/output/3_1_release_notes.html +740 -0
- data/guides/output/3_2_release_notes.html +797 -0
- data/guides/output/4_0_release_notes.html +523 -0
- data/guides/output/4_1_release_notes.html +806 -0
- data/guides/output/4_2_release_notes.html +728 -0
- data/guides/output/Gemfile +6 -0
- data/guides/output/_license.html +226 -0
- data/guides/output/_welcome.html +233 -0
- data/guides/output/action_controller_overview.html +1335 -0
- data/guides/output/action_mailer_basics.html +928 -0
- data/guides/output/action_view_overview.html +1509 -0
- data/guides/output/active_job_basics.html +546 -0
- data/guides/output/active_model_basics.html +438 -0
- data/guides/output/active_record_basics.html +594 -0
- data/guides/output/active_record_callbacks.html +592 -0
- data/guides/output/active_record_migrations.html +1123 -0
- data/guides/output/active_record_postgresql.html +675 -0
- data/guides/output/active_record_querying.html +1796 -0
- data/guides/output/active_record_validations.html +1301 -0
- data/guides/output/active_support_core_extensions.html +3450 -0
- data/guides/output/active_support_instrumentation.html +1121 -0
- data/guides/output/api_documentation_guidelines.html +498 -0
- data/guides/output/asset_pipeline.html +1167 -0
- data/guides/output/association_basics.html +2086 -0
- data/guides/output/caching_with_rails.html +553 -0
- data/guides/output/command_line.html +791 -0
- data/guides/output/configuring.html +1055 -0
- data/guides/output/contributing_to_ruby_on_rails.html +657 -0
- data/guides/output/credits.html +284 -0
- data/guides/output/debugging_rails_applications.html +1014 -0
- data/guides/output/development_dependencies_install.html +478 -0
- data/guides/output/engines.html +1438 -0
- data/guides/output/form_helpers.html +1074 -0
- data/guides/output/generators.html +838 -0
- data/guides/output/getting_started.html +2092 -0
- data/guides/output/i18n.html +1198 -0
- data/guides/output/images/akshaysurve.jpg +0 -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/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/edge_badge.png +0 -0
- data/guides/output/images/favicon.ico +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/getting_started/article_with_comments.png +0 -0
- data/guides/output/images/getting_started/challenge.png +0 -0
- data/guides/output/images/getting_started/confirm_dialog.png +0 -0
- data/guides/output/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
- data/guides/output/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/output/images/getting_started/form_with_errors.png +0 -0
- data/guides/output/images/getting_started/index_action_with_edit_link.png +0 -0
- data/guides/output/images/getting_started/new_article.png +0 -0
- data/guides/output/images/getting_started/new_post.png +0 -0
- data/guides/output/images/getting_started/post_with_comments.png +0 -0
- data/guides/output/images/getting_started/rails_welcome.png +0 -0
- data/guides/output/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/output/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/output/images/getting_started/show_action_for_articles.png +0 -0
- data/guides/output/images/getting_started/show_action_for_posts.png +0 -0
- data/guides/output/images/getting_started/template_is_missing_articles_new.png +0 -0
- data/guides/output/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/output/images/getting_started/undefined_method_post_path.png +0 -0
- data/guides/output/images/getting_started/unknown_action_create_for_articles.png +0 -0
- data/guides/output/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/output/images/getting_started/unknown_action_new_for_articles.png +0 -0
- data/guides/output/images/getting_started/unknown_action_new_for_posts.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/nav_arrow.gif +0 -0
- data/guides/output/images/oscardelben.jpg +0 -0
- data/guides/output/images/polymorphic.png +0 -0
- data/guides/output/images/radar.png +0 -0
- data/guides/output/images/rails4_features.png +0 -0
- data/guides/output/images/rails_guides_kindle_cover.jpg +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/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/vijaydev.jpg +0 -0
- data/guides/output/index.html +354 -0
- data/guides/output/initialization.html +876 -0
- data/guides/output/javascripts/guides.js +59 -0
- data/guides/output/javascripts/jquery.min.js +4 -0
- data/guides/output/javascripts/responsive-tables.js +43 -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 +448 -0
- data/guides/output/layouts_and_rendering.html +1541 -0
- data/guides/output/maintenance_policy.html +257 -0
- data/guides/output/migrations.html +1360 -0
- data/guides/output/nested_model_forms.html +412 -0
- data/guides/output/plugins.html +644 -0
- data/guides/output/rails_application_templates.html +450 -0
- data/guides/output/rails_on_rack.html +547 -0
- data/guides/output/routing.html +1631 -0
- data/guides/output/ruby_on_rails_guides_guidelines.html +329 -0
- data/guides/output/security.html +935 -0
- data/guides/output/stylesheets/fixes.css +16 -0
- data/guides/output/stylesheets/kindle.css +11 -0
- data/guides/output/stylesheets/main.css +713 -0
- data/guides/output/stylesheets/print.css +52 -0
- data/guides/output/stylesheets/reset.css +43 -0
- data/guides/output/stylesheets/responsive-tables.css +50 -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 +1350 -0
- data/guides/output/upgrading_ruby_on_rails.html +1135 -0
- data/guides/output/working_with_javascript_in_rails.html +587 -0
- data/guides/source/4_0_release_notes.md +2 -2
- data/guides/source/4_2_release_notes.md +9 -2
- data/guides/source/action_controller_overview.md +3 -1
- data/guides/source/action_mailer_basics.md +1 -2
- data/guides/source/active_job_basics.md +25 -2
- data/guides/source/active_model_basics.md +350 -19
- data/guides/source/active_record_basics.md +2 -2
- data/guides/source/active_record_migrations.md +1 -1
- data/guides/source/active_record_validations.md +12 -4
- data/guides/source/active_support_core_extensions.md +7 -9
- data/guides/source/active_support_instrumentation.md +2 -0
- data/guides/source/association_basics.md +7 -7
- data/guides/source/configuring.md +24 -0
- data/guides/source/contributing_to_ruby_on_rails.md +1 -1
- data/guides/source/credits.html.erb +1 -1
- data/guides/source/documents.yaml +5 -0
- data/guides/source/engines.md +2 -2
- data/guides/source/form_helpers.md +6 -0
- data/guides/source/i18n.md +6 -9
- data/guides/source/layouts_and_rendering.md +1 -1
- data/guides/source/plugins.md +2 -2
- data/guides/source/rails_on_rack.md +4 -0
- data/guides/source/testing.md +1 -0
- data/guides/source/upgrading_ruby_on_rails.md +5 -5
- metadata +227 -27
@@ -0,0 +1,1541 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
|
3
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
4
|
+
<head>
|
5
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
6
|
+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
7
|
+
|
8
|
+
<title>Layouts and Rendering in Rails — Ruby on Rails Guides</title>
|
9
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
|
10
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
|
11
|
+
|
12
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
|
13
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
|
14
|
+
|
15
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
|
16
|
+
|
17
|
+
<link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
|
18
|
+
</head>
|
19
|
+
<body class="guide">
|
20
|
+
<div id="topNav">
|
21
|
+
<div class="wrapper">
|
22
|
+
<strong class="more-info-label">More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
|
23
|
+
<span class="red-button more-info-button">
|
24
|
+
More Ruby on Rails
|
25
|
+
</span>
|
26
|
+
<ul class="more-info-links s-hidden">
|
27
|
+
<li class="more-info"><a href="http://rubyonrails.org/">Overview</a></li>
|
28
|
+
<li class="more-info"><a href="http://rubyonrails.org/download">Download</a></li>
|
29
|
+
<li class="more-info"><a href="http://rubyonrails.org/deploy">Deploy</a></li>
|
30
|
+
<li class="more-info"><a href="https://github.com/rails/rails">Code</a></li>
|
31
|
+
<li class="more-info"><a href="http://rubyonrails.org/screencasts">Screencasts</a></li>
|
32
|
+
<li class="more-info"><a href="http://rubyonrails.org/documentation">Documentation</a></li>
|
33
|
+
<li class="more-info"><a href="http://rubyonrails.org/community">Community</a></li>
|
34
|
+
<li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
|
35
|
+
</ul>
|
36
|
+
</div>
|
37
|
+
</div>
|
38
|
+
<div id="header">
|
39
|
+
<div class="wrapper clearfix">
|
40
|
+
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
|
41
|
+
<ul class="nav">
|
42
|
+
<li><a class="nav-item" href="index.html">Home</a></li>
|
43
|
+
<li class="guides-index guides-index-large">
|
44
|
+
<a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guides Index</a>
|
45
|
+
<div id="guides" class="clearfix" style="display: none;">
|
46
|
+
<hr />
|
47
|
+
<dl class="L">
|
48
|
+
<dt>Start Here</dt>
|
49
|
+
<dd><a href="getting_started.html">Getting Started with Rails</a></dd>
|
50
|
+
<dt>Models</dt>
|
51
|
+
<dd><a href="active_record_basics.html">Active Record Basics</a></dd>
|
52
|
+
<dd><a href="active_record_migrations.html">Active Record Migrations</a></dd>
|
53
|
+
<dd><a href="active_record_validations.html">Active Record Validations</a></dd>
|
54
|
+
<dd><a href="active_record_callbacks.html">Active Record Callbacks</a></dd>
|
55
|
+
<dd><a href="association_basics.html">Active Record Associations</a></dd>
|
56
|
+
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
|
57
|
+
<dt>Views</dt>
|
58
|
+
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
|
59
|
+
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
|
60
|
+
<dt>Controllers</dt>
|
61
|
+
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
|
62
|
+
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
|
63
|
+
</dl>
|
64
|
+
<dl class="R">
|
65
|
+
<dt>Digging Deeper</dt>
|
66
|
+
<dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
|
67
|
+
<dd><a href="i18n.html">Rails Internationalization API</a></dd>
|
68
|
+
<dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
|
69
|
+
<dd><a href="active_job_basics.html">Active Job Basics</a></dd>
|
70
|
+
<dd><a href="security.html">Securing Rails Applications</a></dd>
|
71
|
+
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
|
72
|
+
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
|
73
|
+
<dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
|
74
|
+
<dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
|
75
|
+
<dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd>
|
76
|
+
<dt>Extending Rails</dt>
|
77
|
+
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
|
78
|
+
<dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
|
79
|
+
<dt>Contributing to Ruby on Rails</dt>
|
80
|
+
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
|
81
|
+
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
|
82
|
+
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
|
83
|
+
<dt>Maintenance Policy</dt>
|
84
|
+
<dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
|
85
|
+
<dt>Release Notes</dt>
|
86
|
+
<dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
|
87
|
+
<dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</a></dd>
|
88
|
+
<dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</a></dd>
|
89
|
+
<dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</a></dd>
|
90
|
+
<dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
|
91
|
+
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
|
92
|
+
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
|
93
|
+
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
|
94
|
+
</dl>
|
95
|
+
</div>
|
96
|
+
</li>
|
97
|
+
<li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribute</a></li>
|
98
|
+
<li><a class="nav-item" href="credits.html">Credits</a></li>
|
99
|
+
<li class="guides-index guides-index-small">
|
100
|
+
<select class="guides-index-item nav-item">
|
101
|
+
<option value="index.html">Guides Index</option>
|
102
|
+
<optgroup label="Start Here">
|
103
|
+
<option value="getting_started.html">Getting Started with Rails</option>
|
104
|
+
</optgroup>
|
105
|
+
<optgroup label="Models">
|
106
|
+
<option value="active_record_basics.html">Active Record Basics</option>
|
107
|
+
<option value="active_record_migrations.html">Active Record Migrations</option>
|
108
|
+
<option value="active_record_validations.html">Active Record Validations</option>
|
109
|
+
<option value="active_record_callbacks.html">Active Record Callbacks</option>
|
110
|
+
<option value="association_basics.html">Active Record Associations</option>
|
111
|
+
<option value="active_record_querying.html">Active Record Query Interface</option>
|
112
|
+
</optgroup>
|
113
|
+
<optgroup label="Views">
|
114
|
+
<option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option>
|
115
|
+
<option value="form_helpers.html">Action View Form Helpers</option>
|
116
|
+
</optgroup>
|
117
|
+
<optgroup label="Controllers">
|
118
|
+
<option value="action_controller_overview.html">Action Controller Overview</option>
|
119
|
+
<option value="routing.html">Rails Routing from the Outside In</option>
|
120
|
+
</optgroup>
|
121
|
+
<optgroup label="Digging Deeper">
|
122
|
+
<option value="active_support_core_extensions.html">Active Support Core Extensions</option>
|
123
|
+
<option value="i18n.html">Rails Internationalization API</option>
|
124
|
+
<option value="action_mailer_basics.html">Action Mailer Basics</option>
|
125
|
+
<option value="active_job_basics.html">Active Job Basics</option>
|
126
|
+
<option value="security.html">Securing Rails Applications</option>
|
127
|
+
<option value="debugging_rails_applications.html">Debugging Rails Applications</option>
|
128
|
+
<option value="configuring.html">Configuring Rails Applications</option>
|
129
|
+
<option value="command_line.html">Rails Command Line Tools and Rake Tasks</option>
|
130
|
+
<option value="asset_pipeline.html">Asset Pipeline</option>
|
131
|
+
<option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option>
|
132
|
+
</optgroup>
|
133
|
+
<optgroup label="Extending Rails">
|
134
|
+
<option value="rails_on_rack.html">Rails on Rack</option>
|
135
|
+
<option value="generators.html">Creating and Customizing Rails Generators</option>
|
136
|
+
</optgroup>
|
137
|
+
<optgroup label="Contributing to Ruby on Rails">
|
138
|
+
<option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
|
139
|
+
<option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
|
140
|
+
<option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</option>
|
141
|
+
</optgroup>
|
142
|
+
<optgroup label="Maintenance Policy">
|
143
|
+
<option value="maintenance_policy.html">Maintenance Policy</option>
|
144
|
+
</optgroup>
|
145
|
+
<optgroup label="Release Notes">
|
146
|
+
<option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
|
147
|
+
<option value="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</option>
|
148
|
+
<option value="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</option>
|
149
|
+
<option value="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</option>
|
150
|
+
<option value="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</option>
|
151
|
+
<option value="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</option>
|
152
|
+
<option value="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</option>
|
153
|
+
<option value="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</option>
|
154
|
+
</optgroup>
|
155
|
+
</select>
|
156
|
+
</li>
|
157
|
+
</ul>
|
158
|
+
</div>
|
159
|
+
</div>
|
160
|
+
<hr class="hide" />
|
161
|
+
|
162
|
+
<div id="feature">
|
163
|
+
<div class="wrapper">
|
164
|
+
<h2>Layouts and Rendering in Rails</h2><p>This guide covers the basic layout features of Action Controller and Action View.</p><p>After reading this guide, you will know:</p>
|
165
|
+
<ul>
|
166
|
+
<li>How to use the various rendering methods built into Rails.</li>
|
167
|
+
<li>How to create layouts with multiple content sections.</li>
|
168
|
+
<li>How to use partials to DRY up your views.</li>
|
169
|
+
<li>How to use nested layouts (sub-templates).</li>
|
170
|
+
</ul>
|
171
|
+
|
172
|
+
|
173
|
+
<div id="subCol">
|
174
|
+
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
175
|
+
<ol class="chapters">
|
176
|
+
<li><a href="#overview:-how-the-pieces-fit-together">Overview: How the Pieces Fit Together</a></li>
|
177
|
+
<li>
|
178
|
+
<a href="#creating-responses">Creating Responses</a>
|
179
|
+
|
180
|
+
<ul>
|
181
|
+
<li><a href="#rendering-by-default:-convention-over-configuration-in-action">Rendering by Default: Convention Over Configuration in Action</a></li>
|
182
|
+
<li><a href="#using-render">Using <code>render</code></a></li>
|
183
|
+
<li><a href="#using-redirect_to">Using <code>redirect_to</code></a></li>
|
184
|
+
<li><a href="#using-head-to-build-header-only-responses">Using <code>head</code> To Build Header-Only Responses</a></li>
|
185
|
+
</ul>
|
186
|
+
</li>
|
187
|
+
<li>
|
188
|
+
<a href="#structuring-layouts">Structuring Layouts</a>
|
189
|
+
|
190
|
+
<ul>
|
191
|
+
<li><a href="#asset-tag-helpers">Asset Tag Helpers</a></li>
|
192
|
+
<li><a href="#understanding-yield">Understanding <code>yield</code></a></li>
|
193
|
+
<li><a href="#using-the-content_for-method">Using the <code>content_for</code> Method</a></li>
|
194
|
+
<li><a href="#using-partials">Using Partials</a></li>
|
195
|
+
<li><a href="#using-nested-layouts">Using Nested Layouts</a></li>
|
196
|
+
</ul>
|
197
|
+
</li>
|
198
|
+
</ol>
|
199
|
+
|
200
|
+
</div>
|
201
|
+
|
202
|
+
</div>
|
203
|
+
</div>
|
204
|
+
|
205
|
+
<div id="container">
|
206
|
+
<div class="wrapper">
|
207
|
+
<div id="mainCol">
|
208
|
+
<h3 id="overview:-how-the-pieces-fit-together">1 Overview: How the Pieces Fit Together</h3><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><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><h3 id="creating-responses">2 Creating Responses</h3><p>From the controller's point of view, there are three ways to create an HTTP response:</p>
|
209
|
+
<ul>
|
210
|
+
<li>Call <code>render</code> to create a full response to send back to the browser</li>
|
211
|
+
<li>Call <code>redirect_to</code> to send an HTTP redirect status code to the browser</li>
|
212
|
+
<li>Call <code>head</code> to create a response consisting solely of HTTP headers to send back to the browser</li>
|
213
|
+
</ul>
|
214
|
+
<h4 id="rendering-by-default:-convention-over-configuration-in-action">2.1 Rendering by Default: Convention Over Configuration in Action</h4><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 <code>BooksController</code> class:</p><div class="code_container">
|
215
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
216
|
+
class BooksController < ApplicationController
|
217
|
+
end
|
218
|
+
|
219
|
+
</pre>
|
220
|
+
</div>
|
221
|
+
<p>And the following in your routes file:</p><div class="code_container">
|
222
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
223
|
+
resources :books
|
224
|
+
|
225
|
+
</pre>
|
226
|
+
</div>
|
227
|
+
<p>And you have a view file <code>app/views/books/index.html.erb</code>:</p><div class="code_container">
|
228
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
229
|
+
<h1>Books are coming soon!</h1>
|
230
|
+
|
231
|
+
</pre>
|
232
|
+
</div>
|
233
|
+
<p>Rails will automatically render <code>app/views/books/index.html.erb</code> when you navigate to <code>/books</code> and you will see "Books are coming soon!" on your screen.</p><p>However a coming soon screen is only minimally useful, so you will soon create your <code>Book</code> model and add the index action to <code>BooksController</code>:</p><div class="code_container">
|
234
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
235
|
+
class BooksController < ApplicationController
|
236
|
+
def index
|
237
|
+
@books = Book.all
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
</pre>
|
242
|
+
</div>
|
243
|
+
<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 <code>action_name.html.erb</code> template in the controller's view path and render it. So in this case, Rails will render the <code>app/views/books/index.html.erb</code> file.</p><p>If we want to display the properties of all the books in our view, we can do so with an ERB template like this:</p><div class="code_container">
|
244
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
245
|
+
<h1>Listing Books</h1>
|
246
|
+
|
247
|
+
<table>
|
248
|
+
<tr>
|
249
|
+
<th>Title</th>
|
250
|
+
<th>Summary</th>
|
251
|
+
<th></th>
|
252
|
+
<th></th>
|
253
|
+
<th></th>
|
254
|
+
</tr>
|
255
|
+
|
256
|
+
<% @books.each do |book| %>
|
257
|
+
<tr>
|
258
|
+
<td><%= book.title %></td>
|
259
|
+
<td><%= book.content %></td>
|
260
|
+
<td><%= link_to "Show", book %></td>
|
261
|
+
<td><%= link_to "Edit", edit_book_path(book) %></td>
|
262
|
+
<td><%= link_to "Remove", book, method: :delete, data: { confirm: "Are you sure?" } %></td>
|
263
|
+
</tr>
|
264
|
+
<% end %>
|
265
|
+
</table>
|
266
|
+
|
267
|
+
<br>
|
268
|
+
|
269
|
+
<%= link_to "New book", new_book_path %>
|
270
|
+
|
271
|
+
</pre>
|
272
|
+
</div>
|
273
|
+
<div class="note"><p>The actual rendering is done by subclasses of <code>ActionView::TemplateHandlers</code>. 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 <code>.erb</code> for ERB (HTML with embedded Ruby), and <code>.builder</code> for Builder (XML generator).</p></div><h4 id="using-render">2.2 Using <code>render</code>
|
274
|
+
</h4><p>In most cases, the <code>ActionController::Base#render</code> method does the heavy lifting of rendering your application's content for use by a browser. There are a variety of ways to customize the behavior of <code>render</code>. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.</p><div class="info"><p>If you want to see the exact results of a call to <code>render</code> without needing to inspect it in a browser, you can call <code>render_to_string</code>. This method takes exactly the same options as <code>render</code>, but it returns a string instead of sending a response back to the browser.</p></div><h5 id="rendering-nothing">2.2.1 Rendering Nothing</h5><p>Perhaps the simplest thing you can do with <code>render</code> is to render nothing at all:</p><div class="code_container">
|
275
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
276
|
+
render nothing: true
|
277
|
+
|
278
|
+
</pre>
|
279
|
+
</div>
|
280
|
+
<p>If you look at the response for this using cURL, you will see the following:</p><div class="code_container">
|
281
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
282
|
+
$ curl -i 127.0.0.1:3000/books
|
283
|
+
HTTP/1.1 200 OK
|
284
|
+
Connection: close
|
285
|
+
Date: Sun, 24 Jan 2010 09:25:18 GMT
|
286
|
+
Transfer-Encoding: chunked
|
287
|
+
Content-Type: */*; charset=utf-8
|
288
|
+
X-Runtime: 0.014297
|
289
|
+
Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
|
290
|
+
Cache-Control: no-cache
|
291
|
+
|
292
|
+
$
|
293
|
+
|
294
|
+
</pre>
|
295
|
+
</div>
|
296
|
+
<p>We see there is an empty response (no data after the <code>Cache-Control</code> line), but the request was successful because Rails has set the response to 200 OK. You can set the <code>:status</code> option on render to change this response. Rendering nothing can be useful for Ajax requests where all you want to send back to the browser is an acknowledgment that the request was completed.</p><div class="info"><p>You should probably be using the <code>head</code> method, discussed later in this guide, instead of <code>render :nothing</code>. This provides additional flexibility and makes it explicit that you're only generating HTTP headers.</p></div><h5 id="rendering-an-action's-view">2.2.2 Rendering an Action's View</h5><p>If you want to render the view that corresponds to a different template within the same controller, you can use <code>render</code> with the name of the view:</p><div class="code_container">
|
297
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
298
|
+
def update
|
299
|
+
@book = Book.find(params[:id])
|
300
|
+
if @book.update(book_params)
|
301
|
+
redirect_to(@book)
|
302
|
+
else
|
303
|
+
render "edit"
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
</pre>
|
308
|
+
</div>
|
309
|
+
<p>If the call to <code>update</code> fails, calling the <code>update</code> action in this controller will render the <code>edit.html.erb</code> template belonging to the same controller.</p><p>If you prefer, you can use a symbol instead of a string to specify the action to render:</p><div class="code_container">
|
310
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
311
|
+
def update
|
312
|
+
@book = Book.find(params[:id])
|
313
|
+
if @book.update(book_params)
|
314
|
+
redirect_to(@book)
|
315
|
+
else
|
316
|
+
render :edit
|
317
|
+
end
|
318
|
+
end
|
319
|
+
|
320
|
+
</pre>
|
321
|
+
</div>
|
322
|
+
<h5 id="rendering-an-action's-template-from-another-controller">2.2.3 Rendering an Action's Template from Another Controller</h5><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 <code>render</code>, which accepts the full path (relative to <code>app/views</code>) of the template to render. For example, if you're running code in an <code>AdminProductsController</code> that lives in <code>app/controllers/admin</code>, you can render the results of an action to a template in <code>app/views/products</code> this way:</p><div class="code_container">
|
323
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
324
|
+
render "products/show"
|
325
|
+
|
326
|
+
</pre>
|
327
|
+
</div>
|
328
|
+
<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 <code>:template</code> option (which was required on Rails 2.2 and earlier):</p><div class="code_container">
|
329
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
330
|
+
render template: "products/show"
|
331
|
+
|
332
|
+
</pre>
|
333
|
+
</div>
|
334
|
+
<h5 id="rendering-an-arbitrary-file">2.2.4 Rendering an Arbitrary File</h5><p>The <code>render</code> method can also use a view that's entirely outside of your application (perhaps you're sharing views between two Rails applications):</p><div class="code_container">
|
335
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
336
|
+
render "/u/apps/warehouse_app/current/app/views/products/show"
|
337
|
+
|
338
|
+
</pre>
|
339
|
+
</div>
|
340
|
+
<p>Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the <code>:file</code> option (which was required on Rails 2.2 and earlier):</p><div class="code_container">
|
341
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
342
|
+
render file: "/u/apps/warehouse_app/current/app/views/products/show"
|
343
|
+
|
344
|
+
</pre>
|
345
|
+
</div>
|
346
|
+
<p>The <code>:file</code> option takes an absolute file-system path. Of course, you need to have rights to the view that you're using to render the content.</p><div 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 <code>layout: true</code> option.</p></div><div class="info"><p>If you're running Rails on Microsoft Windows, you should use the <code>:file</code> option to render a file, because Windows filenames do not have the same format as Unix filenames.</p></div><h5 id="wrapping-it-up">2.2.5 Wrapping it up</h5><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><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 <code>edit.html.erb</code> template in the <code>views/books</code> directory:</p><div class="code_container">
|
347
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
348
|
+
render :edit
|
349
|
+
render action: :edit
|
350
|
+
render "edit"
|
351
|
+
render "edit.html.erb"
|
352
|
+
render action: "edit"
|
353
|
+
render action: "edit.html.erb"
|
354
|
+
render "books/edit"
|
355
|
+
render "books/edit.html.erb"
|
356
|
+
render template: "books/edit"
|
357
|
+
render template: "books/edit.html.erb"
|
358
|
+
render "/path/to/rails/app/views/books/edit"
|
359
|
+
render "/path/to/rails/app/views/books/edit.html.erb"
|
360
|
+
render file: "/path/to/rails/app/views/books/edit"
|
361
|
+
render file: "/path/to/rails/app/views/books/edit.html.erb"
|
362
|
+
|
363
|
+
</pre>
|
364
|
+
</div>
|
365
|
+
<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><h5 id="using-render-with-:inline">2.2.6 Using <code>render</code> with <code>:inline</code>
|
366
|
+
</h5><p>The <code>render</code> method can do without a view completely, if you're willing to use the <code>:inline</code> option to supply ERB as part of the method call. This is perfectly valid:</p><div class="code_container">
|
367
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
368
|
+
render inline: "<% products.each do |p| %><p><%= p.name %></p><% end %>"
|
369
|
+
|
370
|
+
</pre>
|
371
|
+
</div>
|
372
|
+
<div class="warning"><p>There is seldom any good reason to use this option. Mixing ERB into your controllers defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. Use a separate erb view instead.</p></div><p>By default, inline rendering uses ERB. You can force it to use Builder instead with the <code>:type</code> option:</p><div class="code_container">
|
373
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
374
|
+
render inline: "xml.p {'Horrid coding practice!'}", type: :builder
|
375
|
+
|
376
|
+
</pre>
|
377
|
+
</div>
|
378
|
+
<h5 id="rendering-text">2.2.7 Rendering Text</h5><p>You can send plain text - with no markup at all - back to the browser by using
|
379
|
+
the <code>:plain</code> option to <code>render</code>:</p><div class="code_container">
|
380
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
381
|
+
render plain: "OK"
|
382
|
+
|
383
|
+
</pre>
|
384
|
+
</div>
|
385
|
+
<div class="info"><p>Rendering pure text is most useful when you're responding to Ajax or web
|
386
|
+
service requests that are expecting something other than proper HTML.</p></div><div class="note"><p>By default, if you use the <code>:plain</code> option, the text is rendered without
|
387
|
+
using the current layout. If you want Rails to put the text into the current
|
388
|
+
layout, you need to add the <code>layout: true</code> option.</p></div><h5 id="rendering-html">2.2.8 Rendering HTML</h5><p>You can send a HTML string back to the browser by using the <code>:html</code> option to
|
389
|
+
<code>render</code>:</p><div class="code_container">
|
390
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
391
|
+
render html: "<strong>Not Found</strong>".html_safe
|
392
|
+
|
393
|
+
</pre>
|
394
|
+
</div>
|
395
|
+
<div class="info"><p>This is useful when you're rendering a small snippet of HTML code.
|
396
|
+
However, you might want to consider moving it to a template file if the markup
|
397
|
+
is complex.</p></div><div class="note"><p>This option will escape HTML entities if the string is not HTML safe.</p></div><h5 id="rendering-json">2.2.9 Rendering JSON</h5><p>JSON is a JavaScript data format used by many Ajax libraries. Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser:</p><div class="code_container">
|
398
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
399
|
+
render json: @product
|
400
|
+
|
401
|
+
</pre>
|
402
|
+
</div>
|
403
|
+
<div class="info"><p>You don't need to call <code>to_json</code> on the object that you want to render. If you use the <code>:json</code> option, <code>render</code> will automatically call <code>to_json</code> for you.</p></div><h5 id="rendering-xml">2.2.10 Rendering XML</h5><p>Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:</p><div class="code_container">
|
404
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
405
|
+
render xml: @product
|
406
|
+
|
407
|
+
</pre>
|
408
|
+
</div>
|
409
|
+
<div class="info"><p>You don't need to call <code>to_xml</code> on the object that you want to render. If you use the <code>:xml</code> option, <code>render</code> will automatically call <code>to_xml</code> for you.</p></div><h5 id="rendering-vanilla-javascript">2.2.11 Rendering Vanilla JavaScript</h5><p>Rails can render vanilla JavaScript:</p><div class="code_container">
|
410
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
411
|
+
render js: "alert('Hello Rails');"
|
412
|
+
|
413
|
+
</pre>
|
414
|
+
</div>
|
415
|
+
<p>This will send the supplied string to the browser with a MIME type of <code>text/javascript</code>.</p><h5 id="rendering-raw-body">2.2.12 Rendering raw body</h5><p>You can send a raw content back to the browser, without setting any content
|
416
|
+
type, by using the <code>:body</code> option to <code>render</code>:</p><div class="code_container">
|
417
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
418
|
+
render body: "raw"
|
419
|
+
|
420
|
+
</pre>
|
421
|
+
</div>
|
422
|
+
<div class="info"><p>This option should be used only if you don't care about the content type of
|
423
|
+
the response. Using <code>:plain</code> or <code>:html</code> might be more appropriate in most of the
|
424
|
+
time.</p></div><div class="note"><p>Unless overridden, your response returned from this render option will be
|
425
|
+
<code>text/html</code>, as that is the default content type of Action Dispatch response.</p></div><h5 id="options-for-render">2.2.13 Options for <code>render</code>
|
426
|
+
</h5><p>Calls to the <code>render</code> method generally accept four options:</p>
|
427
|
+
<ul>
|
428
|
+
<li><code>:content_type</code></li>
|
429
|
+
<li><code>:layout</code></li>
|
430
|
+
<li><code>:location</code></li>
|
431
|
+
<li><code>:status</code></li>
|
432
|
+
</ul>
|
433
|
+
<h6 id="the-:content_type-option">2.2.13.1 The <code>:content_type</code> Option</h6><p>By default, Rails will serve the results of a rendering operation with the MIME content-type of <code>text/html</code> (or <code>application/json</code> if you use the <code>:json</code> option, or <code>application/xml</code> for the <code>:xml</code> option.). There are times when you might like to change this, and you can do so by setting the <code>:content_type</code> option:</p><div class="code_container">
|
434
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
435
|
+
render file: filename, content_type: "application/rss"
|
436
|
+
|
437
|
+
</pre>
|
438
|
+
</div>
|
439
|
+
<h6 id="the-:layout-option">2.2.13.2 The <code>:layout</code> Option</h6><p>With most of the options to <code>render</code>, 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><p>You can use the <code>:layout</code> option to tell Rails to use a specific file as the layout for the current action:</p><div class="code_container">
|
440
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
441
|
+
render layout: "special_layout"
|
442
|
+
|
443
|
+
</pre>
|
444
|
+
</div>
|
445
|
+
<p>You can also tell Rails to render with no layout at all:</p><div class="code_container">
|
446
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
447
|
+
render layout: false
|
448
|
+
|
449
|
+
</pre>
|
450
|
+
</div>
|
451
|
+
<h6 id="the-:location-option">2.2.13.3 The <code>:location</code> Option</h6><p>You can use the <code>:location</code> option to set the HTTP <code>Location</code> header:</p><div class="code_container">
|
452
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
453
|
+
render xml: photo, location: photo_url(photo)
|
454
|
+
|
455
|
+
</pre>
|
456
|
+
</div>
|
457
|
+
<h6 id="the-:status-option">2.2.13.4 The <code>:status</code> Option</h6><p>Rails will automatically generate a response with the correct HTTP status code (in most cases, this is <code>200 OK</code>). You can use the <code>:status</code> option to change this:</p><div class="code_container">
|
458
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
459
|
+
render status: 500
|
460
|
+
render status: :forbidden
|
461
|
+
|
462
|
+
</pre>
|
463
|
+
</div>
|
464
|
+
<p>Rails understands both numeric status codes and the corresponding symbols shown below.</p>
|
465
|
+
<table>
|
466
|
+
<thead>
|
467
|
+
<tr>
|
468
|
+
<th>Response Class</th>
|
469
|
+
<th>HTTP Status Code</th>
|
470
|
+
<th>Symbol</th>
|
471
|
+
</tr>
|
472
|
+
</thead>
|
473
|
+
<tbody>
|
474
|
+
<tr>
|
475
|
+
<td><strong>Informational</strong></td>
|
476
|
+
<td>100</td>
|
477
|
+
<td>:continue</td>
|
478
|
+
</tr>
|
479
|
+
<tr>
|
480
|
+
<td></td>
|
481
|
+
<td>101</td>
|
482
|
+
<td>:switching_protocols</td>
|
483
|
+
</tr>
|
484
|
+
<tr>
|
485
|
+
<td></td>
|
486
|
+
<td>102</td>
|
487
|
+
<td>:processing</td>
|
488
|
+
</tr>
|
489
|
+
<tr>
|
490
|
+
<td><strong>Success</strong></td>
|
491
|
+
<td>200</td>
|
492
|
+
<td>:ok</td>
|
493
|
+
</tr>
|
494
|
+
<tr>
|
495
|
+
<td></td>
|
496
|
+
<td>201</td>
|
497
|
+
<td>:created</td>
|
498
|
+
</tr>
|
499
|
+
<tr>
|
500
|
+
<td></td>
|
501
|
+
<td>202</td>
|
502
|
+
<td>:accepted</td>
|
503
|
+
</tr>
|
504
|
+
<tr>
|
505
|
+
<td></td>
|
506
|
+
<td>203</td>
|
507
|
+
<td>:non_authoritative_information</td>
|
508
|
+
</tr>
|
509
|
+
<tr>
|
510
|
+
<td></td>
|
511
|
+
<td>204</td>
|
512
|
+
<td>:no_content</td>
|
513
|
+
</tr>
|
514
|
+
<tr>
|
515
|
+
<td></td>
|
516
|
+
<td>205</td>
|
517
|
+
<td>:reset_content</td>
|
518
|
+
</tr>
|
519
|
+
<tr>
|
520
|
+
<td></td>
|
521
|
+
<td>206</td>
|
522
|
+
<td>:partial_content</td>
|
523
|
+
</tr>
|
524
|
+
<tr>
|
525
|
+
<td></td>
|
526
|
+
<td>207</td>
|
527
|
+
<td>:multi_status</td>
|
528
|
+
</tr>
|
529
|
+
<tr>
|
530
|
+
<td></td>
|
531
|
+
<td>208</td>
|
532
|
+
<td>:already_reported</td>
|
533
|
+
</tr>
|
534
|
+
<tr>
|
535
|
+
<td></td>
|
536
|
+
<td>226</td>
|
537
|
+
<td>:im_used</td>
|
538
|
+
</tr>
|
539
|
+
<tr>
|
540
|
+
<td><strong>Redirection</strong></td>
|
541
|
+
<td>300</td>
|
542
|
+
<td>:multiple_choices</td>
|
543
|
+
</tr>
|
544
|
+
<tr>
|
545
|
+
<td></td>
|
546
|
+
<td>301</td>
|
547
|
+
<td>:moved_permanently</td>
|
548
|
+
</tr>
|
549
|
+
<tr>
|
550
|
+
<td></td>
|
551
|
+
<td>302</td>
|
552
|
+
<td>:found</td>
|
553
|
+
</tr>
|
554
|
+
<tr>
|
555
|
+
<td></td>
|
556
|
+
<td>303</td>
|
557
|
+
<td>:see_other</td>
|
558
|
+
</tr>
|
559
|
+
<tr>
|
560
|
+
<td></td>
|
561
|
+
<td>304</td>
|
562
|
+
<td>:not_modified</td>
|
563
|
+
</tr>
|
564
|
+
<tr>
|
565
|
+
<td></td>
|
566
|
+
<td>305</td>
|
567
|
+
<td>:use_proxy</td>
|
568
|
+
</tr>
|
569
|
+
<tr>
|
570
|
+
<td></td>
|
571
|
+
<td>306</td>
|
572
|
+
<td>:reserved</td>
|
573
|
+
</tr>
|
574
|
+
<tr>
|
575
|
+
<td></td>
|
576
|
+
<td>307</td>
|
577
|
+
<td>:temporary_redirect</td>
|
578
|
+
</tr>
|
579
|
+
<tr>
|
580
|
+
<td></td>
|
581
|
+
<td>308</td>
|
582
|
+
<td>:permanent_redirect</td>
|
583
|
+
</tr>
|
584
|
+
<tr>
|
585
|
+
<td><strong>Client Error</strong></td>
|
586
|
+
<td>400</td>
|
587
|
+
<td>:bad_request</td>
|
588
|
+
</tr>
|
589
|
+
<tr>
|
590
|
+
<td></td>
|
591
|
+
<td>401</td>
|
592
|
+
<td>:unauthorized</td>
|
593
|
+
</tr>
|
594
|
+
<tr>
|
595
|
+
<td></td>
|
596
|
+
<td>402</td>
|
597
|
+
<td>:payment_required</td>
|
598
|
+
</tr>
|
599
|
+
<tr>
|
600
|
+
<td></td>
|
601
|
+
<td>403</td>
|
602
|
+
<td>:forbidden</td>
|
603
|
+
</tr>
|
604
|
+
<tr>
|
605
|
+
<td></td>
|
606
|
+
<td>404</td>
|
607
|
+
<td>:not_found</td>
|
608
|
+
</tr>
|
609
|
+
<tr>
|
610
|
+
<td></td>
|
611
|
+
<td>405</td>
|
612
|
+
<td>:method_not_allowed</td>
|
613
|
+
</tr>
|
614
|
+
<tr>
|
615
|
+
<td></td>
|
616
|
+
<td>406</td>
|
617
|
+
<td>:not_acceptable</td>
|
618
|
+
</tr>
|
619
|
+
<tr>
|
620
|
+
<td></td>
|
621
|
+
<td>407</td>
|
622
|
+
<td>:proxy_authentication_required</td>
|
623
|
+
</tr>
|
624
|
+
<tr>
|
625
|
+
<td></td>
|
626
|
+
<td>408</td>
|
627
|
+
<td>:request_timeout</td>
|
628
|
+
</tr>
|
629
|
+
<tr>
|
630
|
+
<td></td>
|
631
|
+
<td>409</td>
|
632
|
+
<td>:conflict</td>
|
633
|
+
</tr>
|
634
|
+
<tr>
|
635
|
+
<td></td>
|
636
|
+
<td>410</td>
|
637
|
+
<td>:gone</td>
|
638
|
+
</tr>
|
639
|
+
<tr>
|
640
|
+
<td></td>
|
641
|
+
<td>411</td>
|
642
|
+
<td>:length_required</td>
|
643
|
+
</tr>
|
644
|
+
<tr>
|
645
|
+
<td></td>
|
646
|
+
<td>412</td>
|
647
|
+
<td>:precondition_failed</td>
|
648
|
+
</tr>
|
649
|
+
<tr>
|
650
|
+
<td></td>
|
651
|
+
<td>413</td>
|
652
|
+
<td>:request_entity_too_large</td>
|
653
|
+
</tr>
|
654
|
+
<tr>
|
655
|
+
<td></td>
|
656
|
+
<td>414</td>
|
657
|
+
<td>:request_uri_too_long</td>
|
658
|
+
</tr>
|
659
|
+
<tr>
|
660
|
+
<td></td>
|
661
|
+
<td>415</td>
|
662
|
+
<td>:unsupported_media_type</td>
|
663
|
+
</tr>
|
664
|
+
<tr>
|
665
|
+
<td></td>
|
666
|
+
<td>416</td>
|
667
|
+
<td>:requested_range_not_satisfiable</td>
|
668
|
+
</tr>
|
669
|
+
<tr>
|
670
|
+
<td></td>
|
671
|
+
<td>417</td>
|
672
|
+
<td>:expectation_failed</td>
|
673
|
+
</tr>
|
674
|
+
<tr>
|
675
|
+
<td></td>
|
676
|
+
<td>422</td>
|
677
|
+
<td>:unprocessable_entity</td>
|
678
|
+
</tr>
|
679
|
+
<tr>
|
680
|
+
<td></td>
|
681
|
+
<td>423</td>
|
682
|
+
<td>:locked</td>
|
683
|
+
</tr>
|
684
|
+
<tr>
|
685
|
+
<td></td>
|
686
|
+
<td>424</td>
|
687
|
+
<td>:failed_dependency</td>
|
688
|
+
</tr>
|
689
|
+
<tr>
|
690
|
+
<td></td>
|
691
|
+
<td>426</td>
|
692
|
+
<td>:upgrade_required</td>
|
693
|
+
</tr>
|
694
|
+
<tr>
|
695
|
+
<td></td>
|
696
|
+
<td>428</td>
|
697
|
+
<td>:precondition_required</td>
|
698
|
+
</tr>
|
699
|
+
<tr>
|
700
|
+
<td></td>
|
701
|
+
<td>429</td>
|
702
|
+
<td>:too_many_requests</td>
|
703
|
+
</tr>
|
704
|
+
<tr>
|
705
|
+
<td></td>
|
706
|
+
<td>431</td>
|
707
|
+
<td>:request_header_fields_too_large</td>
|
708
|
+
</tr>
|
709
|
+
<tr>
|
710
|
+
<td><strong>Server Error</strong></td>
|
711
|
+
<td>500</td>
|
712
|
+
<td>:internal_server_error</td>
|
713
|
+
</tr>
|
714
|
+
<tr>
|
715
|
+
<td></td>
|
716
|
+
<td>501</td>
|
717
|
+
<td>:not_implemented</td>
|
718
|
+
</tr>
|
719
|
+
<tr>
|
720
|
+
<td></td>
|
721
|
+
<td>502</td>
|
722
|
+
<td>:bad_gateway</td>
|
723
|
+
</tr>
|
724
|
+
<tr>
|
725
|
+
<td></td>
|
726
|
+
<td>503</td>
|
727
|
+
<td>:service_unavailable</td>
|
728
|
+
</tr>
|
729
|
+
<tr>
|
730
|
+
<td></td>
|
731
|
+
<td>504</td>
|
732
|
+
<td>:gateway_timeout</td>
|
733
|
+
</tr>
|
734
|
+
<tr>
|
735
|
+
<td></td>
|
736
|
+
<td>505</td>
|
737
|
+
<td>:http_version_not_supported</td>
|
738
|
+
</tr>
|
739
|
+
<tr>
|
740
|
+
<td></td>
|
741
|
+
<td>506</td>
|
742
|
+
<td>:variant_also_negotiates</td>
|
743
|
+
</tr>
|
744
|
+
<tr>
|
745
|
+
<td></td>
|
746
|
+
<td>507</td>
|
747
|
+
<td>:insufficient_storage</td>
|
748
|
+
</tr>
|
749
|
+
<tr>
|
750
|
+
<td></td>
|
751
|
+
<td>508</td>
|
752
|
+
<td>:loop_detected</td>
|
753
|
+
</tr>
|
754
|
+
<tr>
|
755
|
+
<td></td>
|
756
|
+
<td>510</td>
|
757
|
+
<td>:not_extended</td>
|
758
|
+
</tr>
|
759
|
+
<tr>
|
760
|
+
<td></td>
|
761
|
+
<td>511</td>
|
762
|
+
<td>:network_authentication_required</td>
|
763
|
+
</tr>
|
764
|
+
</tbody>
|
765
|
+
</table>
|
766
|
+
<h5 id="finding-layouts">2.2.14 Finding Layouts</h5><p>To find the current layout, Rails first looks for a file in <code>app/views/layouts</code> with the same base name as the controller. For example, rendering actions from the <code>PhotosController</code> class will use <code>app/views/layouts/photos.html.erb</code> (or <code>app/views/layouts/photos.builder</code>). If there is no such controller-specific layout, Rails will use <code>app/views/layouts/application.html.erb</code> or <code>app/views/layouts/application.builder</code>. If there is no <code>.erb</code> layout, Rails will use a <code>.builder</code> layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.</p><h6 id="specifying-layouts-for-controllers">2.2.14.1 Specifying Layouts for Controllers</h6><p>You can override the default layout conventions in your controllers by using the <code>layout</code> declaration. For example:</p><div class="code_container">
|
767
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
768
|
+
class ProductsController < ApplicationController
|
769
|
+
layout "inventory"
|
770
|
+
#...
|
771
|
+
end
|
772
|
+
|
773
|
+
</pre>
|
774
|
+
</div>
|
775
|
+
<p>With this declaration, all of the views rendered by the <code>ProductsController</code> will use <code>app/views/layouts/inventory.html.erb</code> as their layout.</p><p>To assign a specific layout for the entire application, use a <code>layout</code> declaration in your <code>ApplicationController</code> class:</p><div class="code_container">
|
776
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
777
|
+
class ApplicationController < ActionController::Base
|
778
|
+
layout "main"
|
779
|
+
#...
|
780
|
+
end
|
781
|
+
|
782
|
+
</pre>
|
783
|
+
</div>
|
784
|
+
<p>With this declaration, all of the views in the entire application will use <code>app/views/layouts/main.html.erb</code> for their layout.</p><h6 id="choosing-layouts-at-runtime">2.2.14.2 Choosing Layouts at Runtime</h6><p>You can use a symbol to defer the choice of layout until a request is processed:</p><div class="code_container">
|
785
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
786
|
+
class ProductsController < ApplicationController
|
787
|
+
layout :products_layout
|
788
|
+
|
789
|
+
def show
|
790
|
+
@product = Product.find(params[:id])
|
791
|
+
end
|
792
|
+
|
793
|
+
private
|
794
|
+
def products_layout
|
795
|
+
@current_user.special? ? "special" : "products"
|
796
|
+
end
|
797
|
+
|
798
|
+
end
|
799
|
+
|
800
|
+
</pre>
|
801
|
+
</div>
|
802
|
+
<p>Now, if the current user is a special user, they'll get a special layout when viewing a product.</p><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 <code>controller</code> instance, so the layout can be determined based on the current request:</p><div class="code_container">
|
803
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
804
|
+
class ProductsController < ApplicationController
|
805
|
+
layout Proc.new { |controller| controller.request.xhr? ? "popup" : "application" }
|
806
|
+
end
|
807
|
+
|
808
|
+
</pre>
|
809
|
+
</div>
|
810
|
+
<h6 id="conditional-layouts">2.2.14.3 Conditional Layouts</h6><p>Layouts specified at the controller level support the <code>:only</code> and <code>:except</code> options. These options take either a method name, or an array of method names, corresponding to method names within the controller:</p><div class="code_container">
|
811
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
812
|
+
class ProductsController < ApplicationController
|
813
|
+
layout "product", except: [:index, :rss]
|
814
|
+
end
|
815
|
+
|
816
|
+
</pre>
|
817
|
+
</div>
|
818
|
+
<p>With this declaration, the <code>product</code> layout would be used for everything but the <code>rss</code> and <code>index</code> methods.</p><h6 id="layout-inheritance">2.2.14.4 Layout Inheritance</h6><p>Layout declarations cascade downward in the hierarchy, and more specific layout declarations always override more general ones. For example:</p>
|
819
|
+
<ul>
|
820
|
+
<li>
|
821
|
+
<p><code>application_controller.rb</code></p>
|
822
|
+
<div class="code_container">
|
823
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
824
|
+
class ApplicationController < ActionController::Base
|
825
|
+
layout "main"
|
826
|
+
end
|
827
|
+
|
828
|
+
</pre>
|
829
|
+
</div>
|
830
|
+
</li>
|
831
|
+
<li>
|
832
|
+
<p><code>articles_controller.rb</code></p>
|
833
|
+
<div class="code_container">
|
834
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
835
|
+
class ArticlesController < ApplicationController
|
836
|
+
end
|
837
|
+
|
838
|
+
</pre>
|
839
|
+
</div>
|
840
|
+
</li>
|
841
|
+
<li>
|
842
|
+
<p><code>special_articles_controller.rb</code></p>
|
843
|
+
<div class="code_container">
|
844
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
845
|
+
class SpecialArticlesController < ArticlesController
|
846
|
+
layout "special"
|
847
|
+
end
|
848
|
+
|
849
|
+
</pre>
|
850
|
+
</div>
|
851
|
+
</li>
|
852
|
+
<li>
|
853
|
+
<p><code>old_articles_controller.rb</code></p>
|
854
|
+
<div class="code_container">
|
855
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
856
|
+
class OldArticlesController < SpecialArticlesController
|
857
|
+
layout false
|
858
|
+
|
859
|
+
def show
|
860
|
+
@article = Article.find(params[:id])
|
861
|
+
end
|
862
|
+
|
863
|
+
def index
|
864
|
+
@old_articles = Article.older
|
865
|
+
render layout: "old"
|
866
|
+
end
|
867
|
+
# ...
|
868
|
+
end
|
869
|
+
|
870
|
+
</pre>
|
871
|
+
</div>
|
872
|
+
</li>
|
873
|
+
</ul>
|
874
|
+
<p>In this application:</p>
|
875
|
+
<ul>
|
876
|
+
<li>In general, views will be rendered in the <code>main</code> layout</li>
|
877
|
+
<li>
|
878
|
+
<code>ArticlesController#index</code> will use the <code>main</code> layout</li>
|
879
|
+
<li>
|
880
|
+
<code>SpecialArticlesController#index</code> will use the <code>special</code> layout</li>
|
881
|
+
<li>
|
882
|
+
<code>OldArticlesController#show</code> will use no layout at all</li>
|
883
|
+
<li>
|
884
|
+
<code>OldArticlesController#index</code> will use the <code>old</code> layout</li>
|
885
|
+
</ul>
|
886
|
+
<h5 id="avoiding-double-render-errors">2.2.15 Avoiding Double Render Errors</h5><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 <code>render</code> works.</p><p>For example, here's some code that will trigger this error:</p><div class="code_container">
|
887
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
888
|
+
def show
|
889
|
+
@book = Book.find(params[:id])
|
890
|
+
if @book.special?
|
891
|
+
render action: "special_show"
|
892
|
+
end
|
893
|
+
render action: "regular_show"
|
894
|
+
end
|
895
|
+
|
896
|
+
</pre>
|
897
|
+
</div>
|
898
|
+
<p>If <code>@book.special?</code> evaluates to <code>true</code>, Rails will start the rendering process to dump the <code>@book</code> variable into the <code>special_show</code> view. But this will <em>not</em> stop the rest of the code in the <code>show</code> action from running, and when Rails hits the end of the action, it will start to render the <code>regular_show</code> view - and throw an error. The solution is simple: make sure that you have only one call to <code>render</code> or <code>redirect</code> in a single code path. One thing that can help is <code>and return</code>. Here's a patched version of the method:</p><div class="code_container">
|
899
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
900
|
+
def show
|
901
|
+
@book = Book.find(params[:id])
|
902
|
+
if @book.special?
|
903
|
+
render action: "special_show" and return
|
904
|
+
end
|
905
|
+
render action: "regular_show"
|
906
|
+
end
|
907
|
+
|
908
|
+
</pre>
|
909
|
+
</div>
|
910
|
+
<p>Make sure to use <code>and return</code> instead of <code>&& return</code> because <code>&& return</code> will not work due to the operator precedence in the Ruby Language.</p><p>Note that the implicit render done by ActionController detects if <code>render</code> has been called, so the following will work without errors:</p><div class="code_container">
|
911
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
912
|
+
def show
|
913
|
+
@book = Book.find(params[:id])
|
914
|
+
if @book.special?
|
915
|
+
render action: "special_show"
|
916
|
+
end
|
917
|
+
end
|
918
|
+
|
919
|
+
</pre>
|
920
|
+
</div>
|
921
|
+
<p>This will render a book with <code>special?</code> set with the <code>special_show</code> template, while other books will render with the default <code>show</code> template.</p><h4 id="using-redirect_to">2.3 Using <code>redirect_to</code>
|
922
|
+
</h4><p>Another way to handle returning responses to an HTTP request is with <code>redirect_to</code>. As you've seen, <code>render</code> tells Rails which view (or other asset) to use in constructing a response. The <code>redirect_to</code> method does something completely different: it tells the browser to send a new request for a different URL. For example, you could redirect from wherever you are in your code to the index of photos in your application with this call:</p><div class="code_container">
|
923
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
924
|
+
redirect_to photos_url
|
925
|
+
|
926
|
+
</pre>
|
927
|
+
</div>
|
928
|
+
<p>You can use <code>redirect_to</code> with any arguments that you could use with <code>link_to</code> or <code>url_for</code>. There's also a special redirect that sends the user back to the page they just came from:</p><div class="code_container">
|
929
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
930
|
+
redirect_to :back
|
931
|
+
|
932
|
+
</pre>
|
933
|
+
</div>
|
934
|
+
<h5 id="getting-a-different-redirect-status-code">2.3.1 Getting a Different Redirect Status Code</h5><p>Rails uses HTTP status code 302, a temporary redirect, when you call <code>redirect_to</code>. If you'd like to use a different status code, perhaps 301, a permanent redirect, you can use the <code>:status</code> option:</p><div class="code_container">
|
935
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
936
|
+
redirect_to photos_path, status: 301
|
937
|
+
|
938
|
+
</pre>
|
939
|
+
</div>
|
940
|
+
<p>Just like the <code>:status</code> option for <code>render</code>, <code>:status</code> for <code>redirect_to</code> accepts both numeric and symbolic header designations.</p><h5 id="the-difference-between-render-and-redirect_to">2.3.2 The Difference Between <code>render</code> and <code>redirect_to</code>
|
941
|
+
</h5><p>Sometimes inexperienced developers think of <code>redirect_to</code> as a sort of <code>goto</code> 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 HTTP 302 status code.</p><p>Consider these actions to see the difference:</p><div class="code_container">
|
942
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
943
|
+
def index
|
944
|
+
@books = Book.all
|
945
|
+
end
|
946
|
+
|
947
|
+
def show
|
948
|
+
@book = Book.find_by(id: params[:id])
|
949
|
+
if @book.nil?
|
950
|
+
render action: "index"
|
951
|
+
end
|
952
|
+
end
|
953
|
+
|
954
|
+
</pre>
|
955
|
+
</div>
|
956
|
+
<p>With the code in this form, there will likely be a problem if the <code>@book</code> variable is <code>nil</code>. Remember, a <code>render :action</code> doesn't run any code in the target action, so nothing will set up the <code>@books</code> variable that the <code>index</code> view will probably require. One way to fix this is to redirect instead of rendering:</p><div class="code_container">
|
957
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
958
|
+
def index
|
959
|
+
@books = Book.all
|
960
|
+
end
|
961
|
+
|
962
|
+
def show
|
963
|
+
@book = Book.find_by(id: params[:id])
|
964
|
+
if @book.nil?
|
965
|
+
redirect_to action: :index
|
966
|
+
end
|
967
|
+
end
|
968
|
+
|
969
|
+
</pre>
|
970
|
+
</div>
|
971
|
+
<p>With this code, the browser will make a fresh request for the index page, the code in the <code>index</code> method will run, and all will be well.</p><p>The only downside to this code is that it requires a round trip to the browser: the browser requested the show action with <code>/books/1</code> 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 <code>/books/</code>, the browser complies and sends a new request back to the controller asking now for the <code>index</code> 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><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><div class="code_container">
|
972
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
973
|
+
def index
|
974
|
+
@books = Book.all
|
975
|
+
end
|
976
|
+
|
977
|
+
def show
|
978
|
+
@book = Book.find_by(id: params[:id])
|
979
|
+
if @book.nil?
|
980
|
+
@books = Book.all
|
981
|
+
flash.now[:alert] = "Your book was not found"
|
982
|
+
render "index"
|
983
|
+
end
|
984
|
+
end
|
985
|
+
|
986
|
+
</pre>
|
987
|
+
</div>
|
988
|
+
<p>This would detect that there are no books with the specified ID, populate the <code>@books</code> instance variable with all the books in the model, and then directly render the <code>index.html.erb</code> template, returning it to the browser with a flash alert message to tell the user what happened.</p><h4 id="using-head-to-build-header-only-responses">2.4 Using <code>head</code> To Build Header-Only Responses</h4><p>The <code>head</code> method can be used to send responses with only headers to the browser. It provides a more obvious alternative to calling <code>render :nothing</code>. The <code>head</code> method accepts a number or symbol (see <a href="#the-status-option">reference table</a>) representing a HTTP status code. The options argument is interpreted as a hash of header names and values. For example, you can return only an error header:</p><div class="code_container">
|
989
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
990
|
+
head :bad_request
|
991
|
+
|
992
|
+
</pre>
|
993
|
+
</div>
|
994
|
+
<p>This would produce the following header:</p><div class="code_container">
|
995
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
996
|
+
HTTP/1.1 400 Bad Request
|
997
|
+
Connection: close
|
998
|
+
Date: Sun, 24 Jan 2010 12:15:53 GMT
|
999
|
+
Transfer-Encoding: chunked
|
1000
|
+
Content-Type: text/html; charset=utf-8
|
1001
|
+
X-Runtime: 0.013483
|
1002
|
+
Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
|
1003
|
+
Cache-Control: no-cache
|
1004
|
+
|
1005
|
+
</pre>
|
1006
|
+
</div>
|
1007
|
+
<p>Or you can use other HTTP headers to convey other information:</p><div class="code_container">
|
1008
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1009
|
+
head :created, location: photo_path(@photo)
|
1010
|
+
|
1011
|
+
</pre>
|
1012
|
+
</div>
|
1013
|
+
<p>Which would produce:</p><div class="code_container">
|
1014
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1015
|
+
HTTP/1.1 201 Created
|
1016
|
+
Connection: close
|
1017
|
+
Date: Sun, 24 Jan 2010 12:16:44 GMT
|
1018
|
+
Transfer-Encoding: chunked
|
1019
|
+
Location: /photos/1
|
1020
|
+
Content-Type: text/html; charset=utf-8
|
1021
|
+
X-Runtime: 0.083496
|
1022
|
+
Set-Cookie: _blog_session=...snip...; path=/; HttpOnly
|
1023
|
+
Cache-Control: no-cache
|
1024
|
+
|
1025
|
+
</pre>
|
1026
|
+
</div>
|
1027
|
+
<h3 id="structuring-layouts">3 Structuring Layouts</h3><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>
|
1028
|
+
<ul>
|
1029
|
+
<li>Asset tags</li>
|
1030
|
+
<li>
|
1031
|
+
<code>yield</code> and <code>content_for</code>
|
1032
|
+
</li>
|
1033
|
+
<li>Partials</li>
|
1034
|
+
</ul>
|
1035
|
+
<h4 id="asset-tag-helpers">3.1 Asset Tag Helpers</h4><p>Asset tag helpers provide methods for generating HTML that link views to feeds, JavaScript, stylesheets, images, videos and audios. There are six asset tag helpers available in Rails:</p>
|
1036
|
+
<ul>
|
1037
|
+
<li><code>auto_discovery_link_tag</code></li>
|
1038
|
+
<li><code>javascript_include_tag</code></li>
|
1039
|
+
<li><code>stylesheet_link_tag</code></li>
|
1040
|
+
<li><code>image_tag</code></li>
|
1041
|
+
<li><code>video_tag</code></li>
|
1042
|
+
<li><code>audio_tag</code></li>
|
1043
|
+
</ul>
|
1044
|
+
<p>You can use these tags in layouts or other views, although the <code>auto_discovery_link_tag</code>, <code>javascript_include_tag</code>, and <code>stylesheet_link_tag</code>, are most commonly used in the <code><head></code> section of a layout.</p><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><h5 id="linking-to-feeds-with-the-auto_discovery_link_tag">3.1.1 Linking to Feeds with the <code>auto_discovery_link_tag</code>
|
1045
|
+
</h5><p>The <code>auto_discovery_link_tag</code> helper builds HTML that most browsers and feed readers can use to detect the presence of RSS or Atom feeds. It takes the type of the link (<code>:rss</code> or <code>:atom</code>), a hash of options that are passed through to url_for, and a hash of options for the tag:</p><div class="code_container">
|
1046
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1047
|
+
<%= auto_discovery_link_tag(:rss, {action: "feed"},
|
1048
|
+
{title: "RSS Feed"}) %>
|
1049
|
+
|
1050
|
+
</pre>
|
1051
|
+
</div>
|
1052
|
+
<p>There are three tag options available for the <code>auto_discovery_link_tag</code>:</p>
|
1053
|
+
<ul>
|
1054
|
+
<li>
|
1055
|
+
<code>:rel</code> specifies the <code>rel</code> value in the link. The default value is "alternate".</li>
|
1056
|
+
<li>
|
1057
|
+
<code>:type</code> specifies an explicit MIME type. Rails will generate an appropriate MIME type automatically.</li>
|
1058
|
+
<li>
|
1059
|
+
<code>:title</code> specifies the title of the link. The default value is the uppercase <code>:type</code> value, for example, "ATOM" or "RSS".</li>
|
1060
|
+
</ul>
|
1061
|
+
<h5 id="linking-to-javascript-files-with-the-javascript_include_tag">3.1.2 Linking to JavaScript Files with the <code>javascript_include_tag</code>
|
1062
|
+
</h5><p>The <code>javascript_include_tag</code> helper returns an HTML <code>script</code> tag for each source provided.</p><p>If you are using Rails with the <a href="asset_pipeline.html">Asset Pipeline</a> enabled, this helper will generate a link to <code>/assets/javascripts/</code> rather than <code>public/javascripts</code> which was used in earlier versions of Rails. This link is then served by the asset pipeline.</p><p>A JavaScript file within a Rails application or Rails engine goes in one of three locations: <code>app/assets</code>, <code>lib/assets</code> or <code>vendor/assets</code>. These locations are explained in detail in the <a href="asset_pipeline.html#asset-organization">Asset Organization section in the Asset Pipeline Guide</a></p><p>You can specify a full path relative to the document root, or a URL, if you prefer. For example, to link to a JavaScript file that is inside a directory called <code>javascripts</code> inside of one of <code>app/assets</code>, <code>lib/assets</code> or <code>vendor/assets</code>, you would do this:</p><div class="code_container">
|
1063
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1064
|
+
<%= javascript_include_tag "main" %>
|
1065
|
+
|
1066
|
+
</pre>
|
1067
|
+
</div>
|
1068
|
+
<p>Rails will then output a <code>script</code> tag such as this:</p><div class="code_container">
|
1069
|
+
<pre class="brush: xml; gutter: false; toolbar: false">
|
1070
|
+
<script src='/assets/main.js'></script>
|
1071
|
+
|
1072
|
+
</pre>
|
1073
|
+
</div>
|
1074
|
+
<p>The request to this asset is then served by the Sprockets gem.</p><p>To include multiple files such as <code>app/assets/javascripts/main.js</code> and <code>app/assets/javascripts/columns.js</code> at the same time:</p><div class="code_container">
|
1075
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1076
|
+
<%= javascript_include_tag "main", "columns" %>
|
1077
|
+
|
1078
|
+
</pre>
|
1079
|
+
</div>
|
1080
|
+
<p>To include <code>app/assets/javascripts/main.js</code> and <code>app/assets/javascripts/photos/columns.js</code>:</p><div class="code_container">
|
1081
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1082
|
+
<%= javascript_include_tag "main", "/photos/columns" %>
|
1083
|
+
|
1084
|
+
</pre>
|
1085
|
+
</div>
|
1086
|
+
<p>To include <code>http://example.com/main.js</code>:</p><div class="code_container">
|
1087
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1088
|
+
<%= javascript_include_tag "http://example.com/main.js" %>
|
1089
|
+
|
1090
|
+
</pre>
|
1091
|
+
</div>
|
1092
|
+
<h5 id="linking-to-css-files-with-the-stylesheet_link_tag">3.1.3 Linking to CSS Files with the <code>stylesheet_link_tag</code>
|
1093
|
+
</h5><p>The <code>stylesheet_link_tag</code> helper returns an HTML <code><link></code> tag for each source provided.</p><p>If you are using Rails with the "Asset Pipeline" enabled, this helper will generate a link to <code>/assets/stylesheets/</code>. This link is then processed by the Sprockets gem. A stylesheet file can be stored in one of three locations: <code>app/assets</code>, <code>lib/assets</code> or <code>vendor/assets</code>.</p><p>You can specify a full path relative to the document root, or a URL. For example, to link to a stylesheet file that is inside a directory called <code>stylesheets</code> inside of one of <code>app/assets</code>, <code>lib/assets</code> or <code>vendor/assets</code>, you would do this:</p><div class="code_container">
|
1094
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1095
|
+
<%= stylesheet_link_tag "main" %>
|
1096
|
+
|
1097
|
+
</pre>
|
1098
|
+
</div>
|
1099
|
+
<p>To include <code>app/assets/stylesheets/main.css</code> and <code>app/assets/stylesheets/columns.css</code>:</p><div class="code_container">
|
1100
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1101
|
+
<%= stylesheet_link_tag "main", "columns" %>
|
1102
|
+
|
1103
|
+
</pre>
|
1104
|
+
</div>
|
1105
|
+
<p>To include <code>app/assets/stylesheets/main.css</code> and <code>app/assets/stylesheets/photos/columns.css</code>:</p><div class="code_container">
|
1106
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1107
|
+
<%= stylesheet_link_tag "main", "photos/columns" %>
|
1108
|
+
|
1109
|
+
</pre>
|
1110
|
+
</div>
|
1111
|
+
<p>To include <code>http://example.com/main.css</code>:</p><div class="code_container">
|
1112
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1113
|
+
<%= stylesheet_link_tag "http://example.com/main.css" %>
|
1114
|
+
|
1115
|
+
</pre>
|
1116
|
+
</div>
|
1117
|
+
<p>By default, the <code>stylesheet_link_tag</code> creates links with <code>media="screen" rel="stylesheet"</code>. You can override any of these defaults by specifying an appropriate option (<code>:media</code>, <code>:rel</code>):</p><div class="code_container">
|
1118
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1119
|
+
<%= stylesheet_link_tag "main_print", media: "print" %>
|
1120
|
+
|
1121
|
+
</pre>
|
1122
|
+
</div>
|
1123
|
+
<h5 id="linking-to-images-with-the-image_tag">3.1.4 Linking to Images with the <code>image_tag</code>
|
1124
|
+
</h5><p>The <code>image_tag</code> helper builds an HTML <code><img /></code> tag to the specified file. By default, files are loaded from <code>public/images</code>.</p><div class="warning"><p>Note that you must specify the extension of the image.</p></div><div class="code_container">
|
1125
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1126
|
+
<%= image_tag "header.png" %>
|
1127
|
+
|
1128
|
+
</pre>
|
1129
|
+
</div>
|
1130
|
+
<p>You can supply a path to the image if you like:</p><div class="code_container">
|
1131
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1132
|
+
<%= image_tag "icons/delete.gif" %>
|
1133
|
+
|
1134
|
+
</pre>
|
1135
|
+
</div>
|
1136
|
+
<p>You can supply a hash of additional HTML options:</p><div class="code_container">
|
1137
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1138
|
+
<%= image_tag "icons/delete.gif", {height: 45} %>
|
1139
|
+
|
1140
|
+
</pre>
|
1141
|
+
</div>
|
1142
|
+
<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><div class="code_container">
|
1143
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1144
|
+
<%= image_tag "home.gif" %>
|
1145
|
+
<%= image_tag "home.gif", alt: "Home" %>
|
1146
|
+
|
1147
|
+
</pre>
|
1148
|
+
</div>
|
1149
|
+
<p>You can also specify a special size tag, in the format "{width}x{height}":</p><div class="code_container">
|
1150
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1151
|
+
<%= image_tag "home.gif", size: "50x20" %>
|
1152
|
+
|
1153
|
+
</pre>
|
1154
|
+
</div>
|
1155
|
+
<p>In addition to the above special tags, you can supply a final hash of standard HTML options, such as <code>:class</code>, <code>:id</code> or <code>:name</code>:</p><div class="code_container">
|
1156
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1157
|
+
<%= image_tag "home.gif", alt: "Go Home",
|
1158
|
+
id: "HomeImage",
|
1159
|
+
class: "nav_bar" %>
|
1160
|
+
|
1161
|
+
</pre>
|
1162
|
+
</div>
|
1163
|
+
<h5 id="linking-to-videos-with-the-video_tag">3.1.5 Linking to Videos with the <code>video_tag</code>
|
1164
|
+
</h5><p>The <code>video_tag</code> helper builds an HTML 5 <code><video></code> tag to the specified file. By default, files are loaded from <code>public/videos</code>.</p><div class="code_container">
|
1165
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1166
|
+
<%= video_tag "movie.ogg" %>
|
1167
|
+
|
1168
|
+
</pre>
|
1169
|
+
</div>
|
1170
|
+
<p>Produces</p><div class="code_container">
|
1171
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1172
|
+
<video src="/videos/movie.ogg" />
|
1173
|
+
|
1174
|
+
</pre>
|
1175
|
+
</div>
|
1176
|
+
<p>Like an <code>image_tag</code> you can supply a path, either absolute, or relative to the <code>public/videos</code> directory. Additionally you can specify the <code>size: "#{width}x#{height}"</code> option just like an <code>image_tag</code>. Video tags can also have any of the HTML options specified at the end (<code>id</code>, <code>class</code> et al).</p><p>The video tag also supports all of the <code><video></code> HTML options through the HTML options hash, including:</p>
|
1177
|
+
<ul>
|
1178
|
+
<li>
|
1179
|
+
<code>poster: "image_name.png"</code>, provides an image to put in place of the video before it starts playing.</li>
|
1180
|
+
<li>
|
1181
|
+
<code>autoplay: true</code>, starts playing the video on page load.</li>
|
1182
|
+
<li>
|
1183
|
+
<code>loop: true</code>, loops the video once it gets to the end.</li>
|
1184
|
+
<li>
|
1185
|
+
<code>controls: true</code>, provides browser supplied controls for the user to interact with the video.</li>
|
1186
|
+
<li>
|
1187
|
+
<code>autobuffer: true</code>, the video will pre load the file for the user on page load.</li>
|
1188
|
+
</ul>
|
1189
|
+
<p>You can also specify multiple videos to play by passing an array of videos to the <code>video_tag</code>:</p><div class="code_container">
|
1190
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1191
|
+
<%= video_tag ["trailer.ogg", "movie.ogg"] %>
|
1192
|
+
|
1193
|
+
</pre>
|
1194
|
+
</div>
|
1195
|
+
<p>This will produce:</p><div class="code_container">
|
1196
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1197
|
+
<video><source src="/videos/trailer.ogg" /><source src="/videos/trailer.flv" /></video>
|
1198
|
+
|
1199
|
+
</pre>
|
1200
|
+
</div>
|
1201
|
+
<h5 id="linking-to-audio-files-with-the-audio_tag">3.1.6 Linking to Audio Files with the <code>audio_tag</code>
|
1202
|
+
</h5><p>The <code>audio_tag</code> helper builds an HTML 5 <code><audio></code> tag to the specified file. By default, files are loaded from <code>public/audios</code>.</p><div class="code_container">
|
1203
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1204
|
+
<%= audio_tag "music.mp3" %>
|
1205
|
+
|
1206
|
+
</pre>
|
1207
|
+
</div>
|
1208
|
+
<p>You can supply a path to the audio file if you like:</p><div class="code_container">
|
1209
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1210
|
+
<%= audio_tag "music/first_song.mp3" %>
|
1211
|
+
|
1212
|
+
</pre>
|
1213
|
+
</div>
|
1214
|
+
<p>You can also supply a hash of additional options, such as <code>:id</code>, <code>:class</code> etc.</p><p>Like the <code>video_tag</code>, the <code>audio_tag</code> has special options:</p>
|
1215
|
+
<ul>
|
1216
|
+
<li>
|
1217
|
+
<code>autoplay: true</code>, starts playing the audio on page load</li>
|
1218
|
+
<li>
|
1219
|
+
<code>controls: true</code>, provides browser supplied controls for the user to interact with the audio.</li>
|
1220
|
+
<li>
|
1221
|
+
<code>autobuffer: true</code>, the audio will pre load the file for the user on page load.</li>
|
1222
|
+
</ul>
|
1223
|
+
<h4 id="understanding-yield">3.2 Understanding <code>yield</code>
|
1224
|
+
</h4><p>Within the context of a layout, <code>yield</code> identifies a section where content from the view should be inserted. The simplest way to use this is to have a single <code>yield</code>, into which the entire contents of the view currently being rendered is inserted:</p><div class="code_container">
|
1225
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1226
|
+
<html>
|
1227
|
+
<head>
|
1228
|
+
</head>
|
1229
|
+
<body>
|
1230
|
+
<%= yield %>
|
1231
|
+
</body>
|
1232
|
+
</html>
|
1233
|
+
|
1234
|
+
</pre>
|
1235
|
+
</div>
|
1236
|
+
<p>You can also create a layout with multiple yielding regions:</p><div class="code_container">
|
1237
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1238
|
+
<html>
|
1239
|
+
<head>
|
1240
|
+
<%= yield :head %>
|
1241
|
+
</head>
|
1242
|
+
<body>
|
1243
|
+
<%= yield %>
|
1244
|
+
</body>
|
1245
|
+
</html>
|
1246
|
+
|
1247
|
+
</pre>
|
1248
|
+
</div>
|
1249
|
+
<p>The main body of the view will always render into the unnamed <code>yield</code>. To render content into a named <code>yield</code>, you use the <code>content_for</code> method.</p><h4 id="using-the-content_for-method">3.3 Using the <code>content_for</code> Method</h4><p>The <code>content_for</code> method allows you to insert content into a named <code>yield</code> block in your layout. For example, this view would work with the layout that you just saw:</p><div class="code_container">
|
1250
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1251
|
+
<% content_for :head do %>
|
1252
|
+
<title>A simple page</title>
|
1253
|
+
<% end %>
|
1254
|
+
|
1255
|
+
<p>Hello, Rails!</p>
|
1256
|
+
|
1257
|
+
</pre>
|
1258
|
+
</div>
|
1259
|
+
<p>The result of rendering this page into the supplied layout would be this HTML:</p><div class="code_container">
|
1260
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1261
|
+
<html>
|
1262
|
+
<head>
|
1263
|
+
<title>A simple page</title>
|
1264
|
+
</head>
|
1265
|
+
<body>
|
1266
|
+
<p>Hello, Rails!</p>
|
1267
|
+
</body>
|
1268
|
+
</html>
|
1269
|
+
|
1270
|
+
</pre>
|
1271
|
+
</div>
|
1272
|
+
<p>The <code>content_for</code> 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><h4 id="using-partials">3.4 Using Partials</h4><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><h5 id="naming-partials">3.4.1 Naming Partials</h5><p>To render a partial as part of a view, you use the <code>render</code> method within the view:</p><div class="code_container">
|
1273
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1274
|
+
<%= render "menu" %>
|
1275
|
+
|
1276
|
+
</pre>
|
1277
|
+
</div>
|
1278
|
+
<p>This will render a file named <code>_menu.html.erb</code> at that point within the view being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you're pulling in a partial from another folder:</p><div class="code_container">
|
1279
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1280
|
+
<%= render "shared/menu" %>
|
1281
|
+
|
1282
|
+
</pre>
|
1283
|
+
</div>
|
1284
|
+
<p>That code will pull in the partial from <code>app/views/shared/_menu.html.erb</code>.</p><h5 id="using-partials-to-simplify-views">3.4.2 Using Partials to Simplify Views</h5><p>One way to use partials is to treat them as the equivalent of subroutines: as a way to move details out of a view so that you can grasp what's going on more easily. For example, you might have a view that looked like this:</p><div class="code_container">
|
1285
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1286
|
+
<%= render "shared/ad_banner" %>
|
1287
|
+
|
1288
|
+
<h1>Products</h1>
|
1289
|
+
|
1290
|
+
<p>Here are a few of our fine products:</p>
|
1291
|
+
...
|
1292
|
+
|
1293
|
+
<%= render "shared/footer" %>
|
1294
|
+
|
1295
|
+
</pre>
|
1296
|
+
</div>
|
1297
|
+
<p>Here, the <code>_ad_banner.html.erb</code> and <code>_footer.html.erb</code> partials could contain content that is shared among many pages in your application. You don't need to see the details of these sections when you're concentrating on a particular page.</p><div class="info"><p>For content that is shared among all pages in your application, you can use partials directly from layouts.</p></div><h5 id="partial-layouts">3.4.3 Partial Layouts</h5><p>A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:</p><div class="code_container">
|
1298
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1299
|
+
<%= render partial: "link_area", layout: "graybar" %>
|
1300
|
+
|
1301
|
+
</pre>
|
1302
|
+
</div>
|
1303
|
+
<p>This would look for a partial named <code>_link_area.html.erb</code> and render it using the layout <code>_graybar.html.erb</code>. 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 <code>layouts</code> folder).</p><p>Also note that explicitly specifying <code>:partial</code> is required when passing additional options such as <code>:layout</code>.</p><h5 id="passing-local-variables">3.4.4 Passing Local Variables</h5><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>
|
1304
|
+
<ul>
|
1305
|
+
<li>
|
1306
|
+
<p><code>new.html.erb</code></p>
|
1307
|
+
<div class="code_container">
|
1308
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1309
|
+
<h1>New zone</h1>
|
1310
|
+
<%= render partial: "form", locals: {zone: @zone} %>
|
1311
|
+
|
1312
|
+
</pre>
|
1313
|
+
</div>
|
1314
|
+
</li>
|
1315
|
+
<li>
|
1316
|
+
<p><code>edit.html.erb</code></p>
|
1317
|
+
<div class="code_container">
|
1318
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1319
|
+
<h1>Editing zone</h1>
|
1320
|
+
<%= render partial: "form", locals: {zone: @zone} %>
|
1321
|
+
|
1322
|
+
</pre>
|
1323
|
+
</div>
|
1324
|
+
</li>
|
1325
|
+
<li>
|
1326
|
+
<p><code>_form.html.erb</code></p>
|
1327
|
+
<div class="code_container">
|
1328
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1329
|
+
<%= form_for(zone) do |f| %>
|
1330
|
+
<p>
|
1331
|
+
<b>Zone name</b><br>
|
1332
|
+
<%= f.text_field :name %>
|
1333
|
+
</p>
|
1334
|
+
<p>
|
1335
|
+
<%= f.submit %>
|
1336
|
+
</p>
|
1337
|
+
<% end %>
|
1338
|
+
|
1339
|
+
</pre>
|
1340
|
+
</div>
|
1341
|
+
</li>
|
1342
|
+
</ul>
|
1343
|
+
<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><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 <code>:object</code> option:</p><div class="code_container">
|
1344
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1345
|
+
<%= render partial: "customer", object: @new_customer %>
|
1346
|
+
|
1347
|
+
</pre>
|
1348
|
+
</div>
|
1349
|
+
<p>Within the <code>customer</code> partial, the <code>customer</code> variable will refer to <code>@new_customer</code> from the parent view.</p><p>If you have an instance of a model to render into a partial, you can use a shorthand syntax:</p><div class="code_container">
|
1350
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1351
|
+
<%= render @customer %>
|
1352
|
+
|
1353
|
+
</pre>
|
1354
|
+
</div>
|
1355
|
+
<p>Assuming that the <code>@customer</code> instance variable contains an instance of the <code>Customer</code> model, this will use <code>_customer.html.erb</code> to render it and will pass the local variable <code>customer</code> into the partial which will refer to the <code>@customer</code> instance variable in the parent view.</p><h5 id="rendering-collections">3.4.5 Rendering Collections</h5><p>Partials are very useful in rendering collections. When you pass a collection to a partial via the <code>:collection</code> option, the partial will be inserted once for each member in the collection:</p>
|
1356
|
+
<ul>
|
1357
|
+
<li>
|
1358
|
+
<p><code>index.html.erb</code></p>
|
1359
|
+
<div class="code_container">
|
1360
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1361
|
+
<h1>Products</h1>
|
1362
|
+
<%= render partial: "product", collection: @products %>
|
1363
|
+
|
1364
|
+
</pre>
|
1365
|
+
</div>
|
1366
|
+
</li>
|
1367
|
+
<li>
|
1368
|
+
<p><code>_product.html.erb</code></p>
|
1369
|
+
<div class="code_container">
|
1370
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1371
|
+
<p>Product Name: <%= product.name %></p>
|
1372
|
+
|
1373
|
+
</pre>
|
1374
|
+
</div>
|
1375
|
+
</li>
|
1376
|
+
</ul>
|
1377
|
+
<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 <code>_product</code>, and within the <code>_product</code> partial, you can refer to <code>product</code> to get the instance that is being rendered.</p><p>There is also a shorthand for this. Assuming <code>@products</code> is a collection of <code>product</code> instances, you can simply write this in the <code>index.html.erb</code> to produce the same result:</p><div class="code_container">
|
1378
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1379
|
+
<h1>Products</h1>
|
1380
|
+
<%= render @products %>
|
1381
|
+
|
1382
|
+
</pre>
|
1383
|
+
</div>
|
1384
|
+
<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>
|
1385
|
+
<ul>
|
1386
|
+
<li>
|
1387
|
+
<p><code>index.html.erb</code></p>
|
1388
|
+
<div class="code_container">
|
1389
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1390
|
+
<h1>Contacts</h1>
|
1391
|
+
<%= render [customer1, employee1, customer2, employee2] %>
|
1392
|
+
|
1393
|
+
</pre>
|
1394
|
+
</div>
|
1395
|
+
</li>
|
1396
|
+
<li>
|
1397
|
+
<p><code>customers/_customer.html.erb</code></p>
|
1398
|
+
<div class="code_container">
|
1399
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1400
|
+
<p>Customer: <%= customer.name %></p>
|
1401
|
+
|
1402
|
+
</pre>
|
1403
|
+
</div>
|
1404
|
+
</li>
|
1405
|
+
<li>
|
1406
|
+
<p><code>employees/_employee.html.erb</code></p>
|
1407
|
+
<div class="code_container">
|
1408
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1409
|
+
<p>Employee: <%= employee.name %></p>
|
1410
|
+
|
1411
|
+
</pre>
|
1412
|
+
</div>
|
1413
|
+
</li>
|
1414
|
+
</ul>
|
1415
|
+
<p>In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.</p><p>In the event that the collection is empty, <code>render</code> will return nil, so it should be fairly simple to provide alternative content.</p><div class="code_container">
|
1416
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1417
|
+
<h1>Products</h1>
|
1418
|
+
<%= render(@products) || "There are no products available." %>
|
1419
|
+
|
1420
|
+
</pre>
|
1421
|
+
</div>
|
1422
|
+
<h5 id="local-variables">3.4.6 Local Variables</h5><p>To use a custom local variable name within the partial, specify the <code>:as</code> option in the call to the partial:</p><div class="code_container">
|
1423
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1424
|
+
<%= render partial: "product", collection: @products, as: :item %>
|
1425
|
+
|
1426
|
+
</pre>
|
1427
|
+
</div>
|
1428
|
+
<p>With this change, you can access an instance of the <code>@products</code> collection as the <code>item</code> local variable within the partial.</p><p>You can also pass in arbitrary local variables to any partial you are rendering with the <code>locals: {}</code> option:</p><div class="code_container">
|
1429
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1430
|
+
<%= render partial: "product", collection: @products,
|
1431
|
+
as: :item, locals: {title: "Products Page"} %>
|
1432
|
+
|
1433
|
+
</pre>
|
1434
|
+
</div>
|
1435
|
+
<p>In this case, the partial will have access to a local variable <code>title</code> with the value "Products Page".</p><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 <code>_counter</code>. For example, if you're rendering <code>@products</code>, within the partial you can refer to <code>product_counter</code> to tell you how many times the partial has been rendered. This does not work in conjunction with the <code>as: :value</code> option.</p></div><p>You can also specify a second partial to be rendered between instances of the main partial by using the <code>:spacer_template</code> option:</p><h5 id="spacer-templates">3.4.7 Spacer Templates</h5><div class="code_container">
|
1436
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1437
|
+
<%= render partial: @products, spacer_template: "product_ruler" %>
|
1438
|
+
|
1439
|
+
</pre>
|
1440
|
+
</div>
|
1441
|
+
<p>Rails will render the <code>_product_ruler</code> partial (with no data passed in to it) between each pair of <code>_product</code> partials.</p><h5 id="collection-partial-layouts">3.4.8 Collection Partial Layouts</h5><p>When rendering collections it is also possible to use the <code>:layout</code> option:</p><div class="code_container">
|
1442
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
1443
|
+
<%= render partial: "product", collection: @products, layout: "special_layout" %>
|
1444
|
+
|
1445
|
+
</pre>
|
1446
|
+
</div>
|
1447
|
+
<p>The layout will be rendered together with the partial for each item in the collection. The current object and object_counter variables will be available in the layout as well, the same way they do within the partial.</p><h4 id="using-nested-layouts">3.5 Using Nested Layouts</h4><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><p>Suppose you have the following <code>ApplicationController</code> layout:</p>
|
1448
|
+
<ul>
|
1449
|
+
<li>
|
1450
|
+
<p><code>app/views/layouts/application.html.erb</code></p>
|
1451
|
+
<div class="code_container">
|
1452
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1453
|
+
<html>
|
1454
|
+
<head>
|
1455
|
+
<title><%= @page_title or "Page Title" %></title>
|
1456
|
+
<%= stylesheet_link_tag "layout" %>
|
1457
|
+
<style><%= yield :stylesheets %></style>
|
1458
|
+
</head>
|
1459
|
+
<body>
|
1460
|
+
<div id="top_menu">Top menu items here</div>
|
1461
|
+
<div id="menu">Menu items here</div>
|
1462
|
+
<div id="content"><%= content_for?(:content) ? yield(:content) : yield %></div>
|
1463
|
+
</body>
|
1464
|
+
</html>
|
1465
|
+
|
1466
|
+
</pre>
|
1467
|
+
</div>
|
1468
|
+
</li>
|
1469
|
+
</ul>
|
1470
|
+
<p>On pages generated by <code>NewsController</code>, you want to hide the top menu and add a right menu:</p>
|
1471
|
+
<ul>
|
1472
|
+
<li>
|
1473
|
+
<p><code>app/views/layouts/news.html.erb</code></p>
|
1474
|
+
<div class="code_container">
|
1475
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1476
|
+
<% content_for :stylesheets do %>
|
1477
|
+
#top_menu {display: none}
|
1478
|
+
#right_menu {float: right; background-color: yellow; color: black}
|
1479
|
+
<% end %>
|
1480
|
+
<% content_for :content do %>
|
1481
|
+
<div id="right_menu">Right menu items here</div>
|
1482
|
+
<%= content_for?(:news_content) ? yield(:news_content) : yield %>
|
1483
|
+
<% end %>
|
1484
|
+
<%= render template: "layouts/application" %>
|
1485
|
+
|
1486
|
+
</pre>
|
1487
|
+
</div>
|
1488
|
+
</li>
|
1489
|
+
</ul>
|
1490
|
+
<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><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 <code>ActionView::render</code> method via <code>render template: 'layouts/news'</code> to base a new layout on the News layout. If you are sure you will not subtemplate the <code>News</code> layout, you can replace the <code>content_for?(:news_content) ? yield(:news_content) : yield</code> with simply <code>yield</code>.</p>
|
1491
|
+
|
1492
|
+
<h3>Feedback</h3>
|
1493
|
+
<p>
|
1494
|
+
You're encouraged to help improve the quality of this guide.
|
1495
|
+
</p>
|
1496
|
+
<p>
|
1497
|
+
Please contribute if you see any typos or factual errors.
|
1498
|
+
To get started, you can read our <a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section.
|
1499
|
+
</p>
|
1500
|
+
<p>
|
1501
|
+
You may also find incomplete content, or stuff that is not up to date.
|
1502
|
+
Please do add any missing documentation for master. Make sure to check
|
1503
|
+
<a href="http://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
|
1504
|
+
if the issues are already fixed or not on the master branch.
|
1505
|
+
Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
|
1506
|
+
for style and conventions.
|
1507
|
+
</p>
|
1508
|
+
<p>
|
1509
|
+
If for whatever reason you spot something to fix but cannot patch it yourself, please
|
1510
|
+
<a href="https://github.com/rails/rails/issues">open an issue</a>.
|
1511
|
+
</p>
|
1512
|
+
<p>And last but not least, any kind of discussion regarding Ruby on Rails
|
1513
|
+
documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
|
1514
|
+
</p>
|
1515
|
+
</div>
|
1516
|
+
</div>
|
1517
|
+
</div>
|
1518
|
+
|
1519
|
+
<hr class="hide" />
|
1520
|
+
<div id="footer">
|
1521
|
+
<div class="wrapper">
|
1522
|
+
<p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p>
|
1523
|
+
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
|
1524
|
+
|
1525
|
+
</div>
|
1526
|
+
</div>
|
1527
|
+
|
1528
|
+
<script type="text/javascript" src="javascripts/jquery.min.js"></script>
|
1529
|
+
<script type="text/javascript" src="javascripts/responsive-tables.js"></script>
|
1530
|
+
<script type="text/javascript" src="javascripts/guides.js"></script>
|
1531
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
|
1532
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
|
1533
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
|
1534
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
1535
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
1536
|
+
<script type="text/javascript">
|
1537
|
+
SyntaxHighlighter.all();
|
1538
|
+
$(guidesIndex.bind);
|
1539
|
+
</script>
|
1540
|
+
</body>
|
1541
|
+
</html>
|