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