railties 3.1.3 → 3.1.4.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (180) hide show
  1. data/CHANGELOG.md +17 -2
  2. data/guides/source/3_1_release_notes.textile +107 -0
  3. data/guides/source/active_record_querying.textile +1 -1
  4. data/guides/source/asset_pipeline.textile +3 -11
  5. data/guides/source/configuring.textile +3 -1
  6. data/guides/source/i18n.textile +1 -1
  7. data/guides/source/performance_testing.textile +1 -1
  8. data/guides/source/routing.textile +1 -1
  9. data/lib/rails/application.rb +21 -1
  10. data/lib/rails/application/configuration.rb +3 -1
  11. data/lib/rails/generators/app_base.rb +19 -5
  12. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +2 -0
  13. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +4 -0
  14. data/lib/rails/tasks/documentation.rake +1 -1
  15. data/lib/rails/version.rb +2 -2
  16. metadata +335 -491
  17. data/guides/output/2_2_release_notes.html +0 -565
  18. data/guides/output/2_3_release_notes.html +0 -713
  19. data/guides/output/3_0_release_notes.html +0 -652
  20. data/guides/output/3_1_release_notes.html +0 -670
  21. data/guides/output/action_controller_overview.html +0 -925
  22. data/guides/output/action_mailer_basics.html +0 -658
  23. data/guides/output/action_view_overview.html +0 -1471
  24. data/guides/output/active_model_basics.html +0 -349
  25. data/guides/output/active_record_basics.html +0 -364
  26. data/guides/output/active_record_querying.html +0 -1272
  27. data/guides/output/active_record_validations_callbacks.html +0 -1292
  28. data/guides/output/active_resource_basics.html +0 -252
  29. data/guides/output/active_support_core_extensions.html +0 -3374
  30. data/guides/output/ajax_on_rails.html +0 -412
  31. data/guides/output/api_documentation_guidelines.html +0 -317
  32. data/guides/output/asset_pipeline.html +0 -691
  33. data/guides/output/association_basics.html +0 -1742
  34. data/guides/output/caching_with_rails.html +0 -533
  35. data/guides/output/command_line.html +0 -662
  36. data/guides/output/configuring.html +0 -811
  37. data/guides/output/contribute.html +0 -216
  38. data/guides/output/contributing_to_ruby_on_rails.html +0 -465
  39. data/guides/output/credits.html +0 -210
  40. data/guides/output/debugging_rails_applications.html +0 -791
  41. data/guides/output/engines.html +0 -673
  42. data/guides/output/form_helpers.html +0 -850
  43. data/guides/output/generators.html +0 -725
  44. data/guides/output/getting_started.html +0 -1980
  45. data/guides/output/i18n.html +0 -1054
  46. data/guides/output/images/belongs_to.png +0 -0
  47. data/guides/output/images/book_icon.gif +0 -0
  48. data/guides/output/images/bullet.gif +0 -0
  49. data/guides/output/images/challenge.png +0 -0
  50. data/guides/output/images/chapters_icon.gif +0 -0
  51. data/guides/output/images/check_bullet.gif +0 -0
  52. data/guides/output/images/credits_pic_blank.gif +0 -0
  53. data/guides/output/images/csrf.png +0 -0
  54. data/guides/output/images/customized_error_messages.png +0 -0
  55. data/guides/output/images/edge_badge.png +0 -0
  56. data/guides/output/images/error_messages.png +0 -0
  57. data/guides/output/images/feature_tile.gif +0 -0
  58. data/guides/output/images/footer_tile.gif +0 -0
  59. data/guides/output/images/fxn.png +0 -0
  60. data/guides/output/images/grey_bullet.gif +0 -0
  61. data/guides/output/images/habtm.png +0 -0
  62. data/guides/output/images/has_many.png +0 -0
  63. data/guides/output/images/has_many_through.png +0 -0
  64. data/guides/output/images/has_one.png +0 -0
  65. data/guides/output/images/has_one_through.png +0 -0
  66. data/guides/output/images/header_backdrop.png +0 -0
  67. data/guides/output/images/header_tile.gif +0 -0
  68. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  69. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  70. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  71. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  72. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  73. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  74. data/guides/output/images/icons/README +0 -5
  75. data/guides/output/images/icons/callouts/1.png +0 -0
  76. data/guides/output/images/icons/callouts/10.png +0 -0
  77. data/guides/output/images/icons/callouts/11.png +0 -0
  78. data/guides/output/images/icons/callouts/12.png +0 -0
  79. data/guides/output/images/icons/callouts/13.png +0 -0
  80. data/guides/output/images/icons/callouts/14.png +0 -0
  81. data/guides/output/images/icons/callouts/15.png +0 -0
  82. data/guides/output/images/icons/callouts/2.png +0 -0
  83. data/guides/output/images/icons/callouts/3.png +0 -0
  84. data/guides/output/images/icons/callouts/4.png +0 -0
  85. data/guides/output/images/icons/callouts/5.png +0 -0
  86. data/guides/output/images/icons/callouts/6.png +0 -0
  87. data/guides/output/images/icons/callouts/7.png +0 -0
  88. data/guides/output/images/icons/callouts/8.png +0 -0
  89. data/guides/output/images/icons/callouts/9.png +0 -0
  90. data/guides/output/images/icons/caution.png +0 -0
  91. data/guides/output/images/icons/example.png +0 -0
  92. data/guides/output/images/icons/home.png +0 -0
  93. data/guides/output/images/icons/important.png +0 -0
  94. data/guides/output/images/icons/next.png +0 -0
  95. data/guides/output/images/icons/note.png +0 -0
  96. data/guides/output/images/icons/prev.png +0 -0
  97. data/guides/output/images/icons/tip.png +0 -0
  98. data/guides/output/images/icons/up.png +0 -0
  99. data/guides/output/images/icons/warning.png +0 -0
  100. data/guides/output/images/jaimeiniesta.jpg +0 -0
  101. data/guides/output/images/nav_arrow.gif +0 -0
  102. data/guides/output/images/polymorphic.png +0 -0
  103. data/guides/output/images/posts_index.png +0 -0
  104. data/guides/output/images/radar.png +0 -0
  105. data/guides/output/images/rails_guides_logo.gif +0 -0
  106. data/guides/output/images/rails_logo_remix.gif +0 -0
  107. data/guides/output/images/rails_welcome.png +0 -0
  108. data/guides/output/images/session_fixation.png +0 -0
  109. data/guides/output/images/tab_grey.gif +0 -0
  110. data/guides/output/images/tab_info.gif +0 -0
  111. data/guides/output/images/tab_note.gif +0 -0
  112. data/guides/output/images/tab_red.gif +0 -0
  113. data/guides/output/images/tab_yellow.gif +0 -0
  114. data/guides/output/images/tab_yellow.png +0 -0
  115. data/guides/output/images/validation_error_messages.png +0 -0
  116. data/guides/output/images/vijaydev.jpg +0 -0
  117. data/guides/output/index.html +0 -300
  118. data/guides/output/initialization.html +0 -1087
  119. data/guides/output/javascripts/guides.js +0 -7
  120. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  121. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  122. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  123. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  124. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  125. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  126. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  127. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  128. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  129. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  130. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  131. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  132. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  133. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  134. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  135. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  136. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  137. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  138. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  139. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  140. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  141. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  142. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  143. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  144. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  145. data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
  146. data/guides/output/layout.html +0 -312
  147. data/guides/output/layouts_and_rendering.html +0 -1257
  148. data/guides/output/migrations.html +0 -751
  149. data/guides/output/nested_model_forms.html +0 -350
  150. data/guides/output/performance_testing.html +0 -858
  151. data/guides/output/plugins.html +0 -590
  152. data/guides/output/rails_application_templates.html +0 -368
  153. data/guides/output/rails_on_rack.html +0 -408
  154. data/guides/output/routing.html +0 -1246
  155. data/guides/output/ruby_on_rails_guides_guidelines.html +0 -218
  156. data/guides/output/security.html +0 -968
  157. data/guides/output/stylesheets/fixes.css +0 -16
  158. data/guides/output/stylesheets/main.css +0 -445
  159. data/guides/output/stylesheets/print.css +0 -52
  160. data/guides/output/stylesheets/reset.css +0 -43
  161. data/guides/output/stylesheets/style.css +0 -13
  162. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
  163. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  164. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  165. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  166. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  167. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  168. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  169. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  170. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  171. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  172. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  173. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  174. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  175. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  176. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  177. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  178. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  179. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  180. data/guides/output/testing.html +0 -1182
@@ -1,1471 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
-
4
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
- <head>
6
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
7
-
8
- <title>Ruby on Rails Guides: Action View Overview</title>
9
-
10
- <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
11
- <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
12
-
13
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
14
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
15
-
16
- <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
17
- </head>
18
- <body class="guide">
19
- <div id="topNav">
20
- <div class="wrapper">
21
- <strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
22
- <a href="http://rubyonrails.org/">Overview</a> |
23
- <a href="http://rubyonrails.org/download">Download</a> |
24
- <a href="http://rubyonrails.org/deploy">Deploy</a> |
25
- <a href="https://github.com/rails/rails">Code</a> |
26
- <a href="http://rubyonrails.org/screencasts">Screencasts</a> |
27
- <a href="http://rubyonrails.org/documentation">Documentation</a> |
28
- <a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
29
- <a href="http://rubyonrails.org/community">Community</a> |
30
- <a href="http://weblog.rubyonrails.org/">Blog</a>
31
- </div>
32
- </div>
33
- <div id="header">
34
- <div class="wrapper clearfix">
35
- <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
36
- <p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
37
- <ul class="nav">
38
- <li><a href="index.html">Home</a></li>
39
- <li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
40
- <div id="guides" class="clearfix" style="display: none;">
41
- <hr />
42
- <dl class="L">
43
- <dt>Start Here</dt>
44
- <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
45
- <dt>Models</dt>
46
- <dd><a href="migrations.html">Rails Database Migrations</a></dd>
47
- <dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
48
- <dd><a href="association_basics.html">Active Record Associations</a></dd>
49
- <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
50
- <dt>Views</dt>
51
- <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
52
- <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
53
- <dt>Controllers</dt>
54
- <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
55
- <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
56
- </dl>
57
- <dl class="R">
58
- <dt>Digging Deeper</dt>
59
- <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
60
- <dd><a href="i18n.html">Rails Internationalization API</a></dd>
61
- <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
62
- <dd><a href="testing.html">Testing Rails Applications</a></dd>
63
- <dd><a href="security.html">Securing Rails Applications</a></dd>
64
- <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
65
- <dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
66
- <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
67
- <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
68
- <dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
69
- <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
70
-
71
- <dt>Extending Rails</dt>
72
- <dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
73
- <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
74
- <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
75
-
76
- <dt>Contributing to Ruby on Rails</dt>
77
- <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
78
- <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
79
- <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
80
-
81
- <dt>Release Notes</dt>
82
- <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
83
- <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
84
- <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
85
- <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
86
- </dl>
87
- </div>
88
- </li>
89
- <li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
90
- <li><a href="credits.html">Credits</a></li>
91
- </ul>
92
- </div>
93
- </div>
94
- <hr class="hide" />
95
-
96
- <div id="feature">
97
- <div class="wrapper">
98
- <h2>Action View Overview</h2>
99
- <p>In this guide you will learn:</p>
100
- <ul>
101
- <li>What Action View is, and how to use it with Rails</li>
102
- <li>How to use Action View outside of Rails</li>
103
- <li>How best to use templates, partials, and layouts</li>
104
- <li>What helpers are provided by Action View, and how to make your own</li>
105
- <li>How to use localized views</li>
106
- </ul>
107
-
108
- <div id="subCol">
109
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
110
- <ol class="chapters">
111
- <li><a href="#what-is-action-view">What is Action View?</a></li><li><a href="#using-action-view-with-rails">Using Action View with Rails</a></li><li><a href="#using-action-view-outside-of-rails">Using Action View outside of Rails</a></li><li><a href="#templates-partials-and-layouts">Templates, Partials and Layouts</a><ul><li><a href="#templates">Templates</a></li> <li><a href="#partials">Partials</a></li> <li><a href="#layouts">Layouts</a></li></ul></li><li><a href="#using-templates-partials-and-layouts-in-the-rails-way">Using Templates, Partials and Layouts in &#8220;The Rails Way&#8221;</a></li><li><a href="#partial-layouts">Partial Layouts</a></li><li><a href="#view-paths">View Paths</a></li><li><a href="#overview-of-all-the-helpers-provided-by-action-view">Overview of all the helpers provided by Action View</a><ul><li><a href="#activerecordhelper">ActiveRecordHelper</a></li> <li><a href="#recordtaghelper">RecordTagHelper</a></li> <li><a href="#assettaghelper">AssetTagHelper</a></li> <li><a href="#atomfeedhelper">AtomFeedHelper</a></li> <li><a href="#benchmarkhelper">BenchmarkHelper</a></li> <li><a href="#cachehelper">CacheHelper</a></li> <li><a href="#capturehelper">CaptureHelper</a></li> <li><a href="#datehelper">DateHelper</a></li> <li><a href="#debughelper">DebugHelper</a></li> <li><a href="#formhelper">FormHelper</a></li> <li><a href="#formoptionshelper">FormOptionsHelper</a></li> <li><a href="#formtaghelper">FormTagHelper</a></li> <li><a href="#javascripthelper">JavaScriptHelper</a></li> <li><a href="#numberhelper">NumberHelper</a></li></ul></li><li><a href="#localized-views">Localized Views</a></li></ol></div>
112
- </div>
113
- </div>
114
-
115
- <div id="container">
116
- <div class="wrapper">
117
- <div id="mainCol">
118
- <h3 id="what-is-action-view">1 What is Action View?</h3>
119
- <p>Action View and Action Controller are the two major components of Action Pack. In Rails, web requests are handled by Action Pack, which splits the work into a controller part (performing the logic) and a view part (rendering a template). Typically, Action Controller will be concerned with communicating with the database and performing <span class="caps">CRUD</span> actions where necessary. Action View is then responsible for compiling the response.</p>
120
- <p>Action View templates are written using embedded Ruby in tags mingled with <span class="caps">HTML</span>. To avoid cluttering the templates with boilerplate code, a number of helper classes provide common behavior for forms, dates, and strings. It&#8217;s also easy to add new helpers to your application as it evolves.</p>
121
- <p>Note: Some features of Action View are tied to Active Record, but that doesn&#8217;t mean that Action View depends on Active Record. Action View is an independent package that can be used with any sort of backend.</p>
122
- <h3 id="using-action-view-with-rails">2 Using Action View with Rails</h3>
123
- <p>For each controller there is an associated directory in the <tt>app/views</tt> directory which holds the template files that make up the views associated with that controller. These files are used to display the view that results from each controller action.</p>
124
- <p>Let&#8217;s take a look at what Rails does by default when creating a new resource using the scaffold generator:</p>
125
- <div class="code_container">
126
- <pre class="brush: plain; gutter: false; toolbar: false">
127
- $ rails generate scaffold post
128
- [...]
129
- invoke scaffold_controller
130
- create app/controllers/posts_controller.rb
131
- invoke erb
132
- create app/views/posts
133
- create app/views/posts/index.html.erb
134
- create app/views/posts/edit.html.erb
135
- create app/views/posts/show.html.erb
136
- create app/views/posts/new.html.erb
137
- create app/views/posts/_form.html.erb
138
- [...]
139
- </pre>
140
- </div>
141
- <p>There is a naming convention for views in Rails. Typically, the views share their name with the associated controller action, as you can see above.
142
- For example, the index controller action of the <tt>posts_controller.rb</tt> will use the <tt>index.html.erb</tt> view file in the <tt>app/views/posts</tt> directory.
143
- The complete <span class="caps">HTML</span> returned to the client is composed of a combination of this <span class="caps">ERB</span> file, a layout template that wraps it, and all the partials that the view may reference. Later on this guide you can find a more detailed documentation of each one of this three components.</p>
144
- <h3 id="using-action-view-outside-of-rails">3 Using Action View outside of Rails</h3>
145
- <p>Action View works well with Action Record, but it can also be used with other Ruby tools. We can demonstrate this by creating a small <a href="http://rack.rubyforge.org/">Rack</a> application that includes Action View functionality. This may be useful, for example, if you&#8217;d like access to Action View&#8217;s helpers in a Rack application.</p>
146
- <p>Let&#8217;s start by ensuring that you have the Action Pack and Rack gems installed:</p>
147
- <div class="code_container">
148
- <pre class="brush: plain; gutter: false; toolbar: false">
149
- $ gem install actionpack
150
- $ gem install rack
151
- </pre>
152
- </div>
153
- <p>Now we&#8217;ll create a simple &#8220;Hello World&#8221; application that uses the <tt>titleize</tt> method provided by Active Support.</p>
154
- <p><strong>hello_world.rb:</strong></p>
155
- <div class="code_container">
156
- <pre class="brush: ruby; gutter: false; toolbar: false">
157
- require 'rubygems'
158
- require 'active_support/core_ext/string/inflections'
159
- require 'rack'
160
-
161
- def hello_world(env)
162
- [200, {&quot;Content-Type&quot; =&gt; &quot;text/html&quot;}, &quot;hello world&quot;.titleize]
163
- end
164
-
165
- Rack::Handler::Mongrel.run method(:hello_world), :Port =&gt; 4567
166
- </pre>
167
- </div>
168
- <p>We can see this all come together by starting up the application and then visiting <tt>http://localhost:4567/</tt></p>
169
- <div class="code_container">
170
- <pre class="brush: plain; gutter: false; toolbar: false">
171
- $ ruby hello_world.rb
172
- </pre>
173
- </div>
174
- <p><span class="caps">TODO</span> needs a screenshot? I have one &#8211; not sure where to put it.</p>
175
- <p>Notice how &#8216;hello world&#8217; has been converted into &#8216;Hello World&#8217; by the <tt>titleize</tt> helper method.</p>
176
- <p>Action View can also be used with <a href="http://www.sinatrarb.com/">Sinatra</a> in the same way.</p>
177
- <p>Let&#8217;s start by ensuring that you have the Action Pack and Sinatra gems installed:</p>
178
- <div class="code_container">
179
- <pre class="brush: plain; gutter: false; toolbar: false">
180
- $ gem install actionpack
181
- $ gem install sinatra
182
- </pre>
183
- </div>
184
- <p>Now we&#8217;ll create the same &#8220;Hello World&#8221; application in Sinatra.</p>
185
- <p><strong>hello_world.rb:</strong></p>
186
- <div class="code_container">
187
- <pre class="brush: ruby; gutter: false; toolbar: false">
188
- require 'rubygems'
189
- require 'action_view'
190
- require 'sinatra'
191
-
192
- get '/' do
193
- erb 'hello world'.titleize
194
- end
195
- </pre>
196
- </div>
197
- <p>Then, we can run the application:</p>
198
- <div class="code_container">
199
- <pre class="brush: plain; gutter: false; toolbar: false">
200
- $ ruby hello_world.rb
201
- </pre>
202
- </div>
203
- <p>Once the application is running, you can see Sinatra and Action View working together by visiting <tt>http://localhost:4567/</tt></p>
204
- <p><span class="caps">TODO</span> needs a screenshot? I have one &#8211; not sure where to put it.</p>
205
- <h3 id="templates-partials-and-layouts">4 Templates, Partials and Layouts</h3>
206
- <p>As mentioned before, the final <span class="caps">HTML</span> output is a composition of three Rails elements: <tt>Templates</tt>, <tt>Partials</tt> and <tt>Layouts</tt>.
207
- Find below a brief overview of each one of them.</p>
208
- <h4 id="templates">4.1 Templates</h4>
209
- <p>Action View templates can be written in several ways. If the template file has a <tt>.erb</tt> extension then it uses a mixture of <span class="caps">ERB</span> (included in Ruby) and <span class="caps">HTML</span>. If the template file has a <tt>.builder</tt> extension then a fresh instance of <tt>Builder::XmlMarkup</tt> library is used.</p>
210
- <p>Rails supports multiple template systems and uses a file extension to distinguish amongst them. For example, an <span class="caps">HTML</span> file using the <span class="caps">ERB</span> template system will have <tt>.html.erb</tt> as a file extension.</p>
211
- <h5 id="erb">4.1.1 <span class="caps">ERB</span></h5>
212
- <p>Within an <span class="caps">ERB</span> template Ruby code can be included using both <tt><% %></tt> and <tt><%= %></tt> tags. The <tt><% %></tt> are used to execute Ruby code that does not return anything, such as conditions, loops or blocks, and the <tt><%= %></tt> tags are used when you want output.</p>
213
- <p>Consider the following loop for names:</p>
214
- <div class="code_container">
215
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
216
- &lt;b&gt;Names of all the people&lt;/b&gt;
217
- &lt;% @people.each do |person| %&gt;
218
- Name: &lt;%= person.name %&gt;&lt;br/&gt;
219
- &lt;% end %&gt;
220
- </pre>
221
- </div>
222
- <p>The loop is setup in regular embedding tags <tt><% %></tt> and the name is written using the output embedding tag <tt><%= %></tt>. Note that this is not just a usage suggestion, for Regular output functions like print or puts won&#8217;t work with <span class="caps">ERB</span> templates. So this would be wrong:</p>
223
- <div class="code_container">
224
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
225
- &lt;%# WRONG %&gt;
226
- Hi, Mr. &lt;% puts &quot;Frodo&quot; %&gt;
227
- </pre>
228
- </div>
229
- <p>To suppress leading and trailing whitespaces, you can use <tt><%-</tt> <tt>-%></tt> interchangeably with <tt><%</tt> and <tt>%></tt>.</p>
230
- <h5 id="builder">4.1.2 Builder</h5>
231
- <p>Builder templates are a more programmatic alternative to <span class="caps">ERB</span>. They are especially useful for generating <span class="caps">XML</span> content. An XmlMarkup object named <tt>xml</tt> is automatically made available to templates with a <tt>.builder</tt> extension.</p>
232
- <p>Here are some basic examples:</p>
233
- <div class="code_container">
234
- <pre class="brush: ruby; gutter: false; toolbar: false">
235
- xml.em(&quot;emphasized&quot;)
236
- xml.em { xml.b(&quot;emph &amp; bold&quot;) }
237
- xml.a(&quot;A Link&quot;, &quot;href&quot;=&gt;&quot;http://rubyonrails.org&quot;)
238
- xml.target(&quot;name&quot;=&gt;&quot;compile&quot;, &quot;option&quot;=&gt;&quot;fast&quot;)
239
- </pre>
240
- </div>
241
- <p>will produce</p>
242
- <div class="code_container">
243
- <pre class="brush: xml; gutter: false; toolbar: false">
244
- &lt;em&gt;emphasized&lt;/em&gt;
245
- &lt;em&gt;&lt;b&gt;emph &amp;amp; bold&lt;/b&gt;&lt;/em&gt;
246
- &lt;a href=&quot;http://rubyonrails.org&quot;&gt;A link&lt;/a&gt;
247
- &lt;target option=&quot;fast&quot; name=&quot;compile&quot; /&gt;
248
- </pre>
249
- </div>
250
- <p>Any method with a block will be treated as an <span class="caps">XML</span> markup tag with nested markup in the block. For example, the following:</p>
251
- <div class="code_container">
252
- <pre class="brush: ruby; gutter: false; toolbar: false">
253
- xml.div {
254
- xml.h1(@person.name)
255
- xml.p(@person.bio)
256
- }
257
- </pre>
258
- </div>
259
- <p>would produce something like:</p>
260
- <div class="code_container">
261
- <pre class="brush: xml; gutter: false; toolbar: false">
262
- &lt;div&gt;
263
- &lt;h1&gt;David Heinemeier Hansson&lt;/h1&gt;
264
- &lt;p&gt;A product of Danish Design during the Winter of '79...&lt;/p&gt;
265
- &lt;/div&gt;
266
- </pre>
267
- </div>
268
- <p>A full-length <span class="caps">RSS</span> example actually used on Basecamp:</p>
269
- <div class="code_container">
270
- <pre class="brush: ruby; gutter: false; toolbar: false">
271
- xml.rss(&quot;version&quot; =&gt; &quot;2.0&quot;, &quot;xmlns:dc&quot; =&gt; &quot;http://purl.org/dc/elements/1.1/&quot;) do
272
- xml.channel do
273
- xml.title(@feed_title)
274
- xml.link(@url)
275
- xml.description &quot;Basecamp: Recent items&quot;
276
- xml.language &quot;en-us&quot;
277
- xml.ttl &quot;40&quot;
278
-
279
- for item in @recent_items
280
- xml.item do
281
- xml.title(item_title(item))
282
- xml.description(item_description(item)) if item_description(item)
283
- xml.pubDate(item_pubDate(item))
284
- xml.guid(@person.firm.account.url + @recent_items.url(item))
285
- xml.link(@person.firm.account.url + @recent_items.url(item))
286
- xml.tag!(&quot;dc:creator&quot;, item.author_name) if item_has_creator?(item)
287
- end
288
- end
289
- end
290
- end
291
- </pre>
292
- </div>
293
- <h5 id="template-caching">4.1.3 Template Caching</h5>
294
- <p>By default, Rails will compile each template to a method in order to render it. When you alter a template, Rails will check the file&#8217;s modification time and recompile it in development mode.</p>
295
- <h4 id="partials">4.2 Partials</h4>
296
- <p>Partial templates – usually just called &#8220;partials&#8221; – 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>
297
- <h5 id="naming-partials">4.2.1 Naming Partials</h5>
298
- <p>To render a partial as part of a view, you use the <tt>render</tt> method within the view:</p>
299
- <div class="code_container">
300
- <pre class="brush: ruby; gutter: false; toolbar: false">
301
- &lt;%= render &quot;menu&quot; %&gt;
302
- </pre>
303
- </div>
304
- <p>This will render a file named <tt>_menu.html.erb</tt> at that point within the view is being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you&#8217;re pulling in a partial from another folder:</p>
305
- <div class="code_container">
306
- <pre class="brush: ruby; gutter: false; toolbar: false">
307
- &lt;%= render &quot;shared/menu&quot; %&gt;
308
- </pre>
309
- </div>
310
- <p>That code will pull in the partial from <tt>app/views/shared/_menu.html.erb</tt>.</p>
311
- <h5 id="using-partials-to-simplify-views">4.2.2 Using Partials to simplify Views</h5>
312
- <p>One way to use partials is to treat them as the equivalent of subroutines: as a way to move details out of a view so that you can grasp what&#8217;s going on more easily. For example, you might have a view that looked like this:</p>
313
- <div class="code_container">
314
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
315
- &lt;%= render &quot;shared/ad_banner&quot; %&gt;
316
-
317
- &lt;h1&gt;Products&lt;/h1&gt;
318
-
319
- &lt;p&gt;Here are a few of our fine products:&lt;/p&gt;
320
- &lt;% @products.each do |product| %&gt;
321
- &lt;%= render :partial =&gt; &quot;product&quot;, :locals =&gt; { :product =&gt; product } %&gt;
322
- &lt;% end %&gt;
323
-
324
- &lt;%= render &quot;shared/footer&quot; %&gt;
325
- </pre>
326
- </div>
327
- <p>Here, the <tt>_ad_banner.html.erb</tt> and <tt>_footer.html.erb</tt> partials could contain content that is shared among many pages in your application. You don&#8217;t need to see the details of these sections when you&#8217;re concentrating on a particular page.</p>
328
- <h5 id="the-as-and-object-options">4.2.3 The :as and :object options</h5>
329
- <p>By default <tt>ActionView::Partials::PartialRenderer</tt> has its object in a local variable with the same name as the template. So, given</p>
330
- <div class="code_container">
331
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
332
- &lt;%= render :partial =&gt; &quot;product&quot; %&gt;
333
- </pre>
334
- </div>
335
- <p>within product we&#8217;ll get <tt>@product</tt> in the local variable <tt>product</tt>, as if we had written:</p>
336
- <div class="code_container">
337
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
338
- &lt;%= render :partial =&gt; &quot;product&quot;, :locals =&gt; { :product =&gt; @product } %&gt;
339
- </pre>
340
- </div>
341
- <p>With the <tt>:as</tt> option we can specify a different name for said local variable. For example, if we wanted it to be <tt>item</tt> instead of product+ we&#8217;d do:</p>
342
- <div class="code_container">
343
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
344
- &lt;%= render :partial =&gt; &quot;product&quot;, :as =&gt; 'item' %&gt;
345
- </pre>
346
- </div>
347
- <p>The <tt>:object</tt> option can be used to directly specify which object is rendered into the partial; useful when the template&#8217;s object is elsewhere, in a different ivar or in a local variable for instance.</p>
348
- <p>For example, instead of:</p>
349
- <div class="code_container">
350
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
351
- &lt;%= render :partial =&gt; &quot;product&quot;, :locals =&gt; { :product =&gt; @item } %&gt;
352
- </pre>
353
- </div>
354
- <p>you&#8217;d do:</p>
355
- <div class="code_container">
356
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
357
- &lt;%= render :partial =&gt; &quot;product&quot;, :object =&gt; @item %&gt;
358
- </pre>
359
- </div>
360
- <p>The <tt>:object</tt> and <tt>:as</tt> options can be used together.</p>
361
- <h5 id="rendering-collections">4.2.4 Rendering Collections</h5>
362
- <p>The example of partial use describes a familiar pattern where a template needs to iterate over an array and render a sub template for each of the elements. This pattern has been implemented as a single method that accepts an array and renders a partial by the same name as the elements contained within.
363
- So the three-lined example for rendering all the products can be rewritten with a single line:</p>
364
- <div class="code_container">
365
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
366
- &lt;%= render :partial =&gt; &quot;product&quot;, :collection =&gt; @products %&gt;
367
- </pre>
368
- </div>
369
- <p>When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is <tt>_product</tt> , and within the <tt>_product</tt> partial, you can refer to <tt>product</tt> to get the instance that is being rendered.</p>
370
- <p>You can use a shorthand syntax for rendering collections. Assuming @products is a collection of <tt>Product</tt> instances, you can simply write the following to produce the same result:</p>
371
- <div class="code_container">
372
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
373
- &lt;%= render @products %&gt;
374
- </pre>
375
- </div>
376
- <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>
377
- <h5 id="spacer-templates">4.2.5 Spacer Templates</h5>
378
- <p>You can also specify a second partial to be rendered between instances of the main partial by using the <tt>:spacer_template</tt> option:</p>
379
- <div class="code_container">
380
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
381
- &lt;%= render @products, :spacer_template =&gt; &quot;product_ruler&quot; %&gt;
382
- </pre>
383
- </div>
384
- <p>Rails will render the <tt>_product_ruler</tt> partial (with no data passed in to it) between each pair of <tt>_product</tt> partials.</p>
385
- <h4 id="layouts">4.3 Layouts</h4>
386
- <p><span class="caps">TODO</span>&#8230;</p>
387
- <h3 id="using-templates-partials-and-layouts-in-the-rails-way">5 Using Templates, Partials and Layouts in &#8220;The Rails Way&#8221;</h3>
388
- <p><span class="caps">TODO</span>&#8230;</p>
389
- <h3 id="partial-layouts">6 Partial Layouts</h3>
390
- <p>Partials can have their own layouts applied to them. These layouts are different than the ones that are specified globally for the entire action, but they work in a similar fashion.</p>
391
- <p>Let&#8217;s say we&#8217;re displaying a post on a page where it should be wrapped in a <tt>div</tt> for display purposes. First, we&#8217;ll create a new <tt>Post</tt>:</p>
392
- <div class="code_container">
393
- <pre class="brush: ruby; gutter: false; toolbar: false">
394
- Post.create(:body =&gt; 'Partial Layouts are cool!')
395
- </pre>
396
- </div>
397
- <p>In the <tt>show</tt> template, we&#8217;ll render the <tt>post</tt> partial wrapped in the <tt>box</tt> layout:</p>
398
- <p><strong>posts/show.html.erb</strong></p>
399
- <div class="code_container">
400
- <pre class="brush: ruby; gutter: false; toolbar: false">
401
- &lt;%= render :partial =&gt; 'post', :layout =&gt; 'box', :locals =&gt; {:post =&gt; @post} %&gt;
402
- </pre>
403
- </div>
404
- <p>The <tt>box</tt> layout simply wraps the <tt>post</tt> partial in a <tt>div</tt>:</p>
405
- <p><strong>posts/_box.html.erb</strong></p>
406
- <div class="code_container">
407
- <pre class="brush: ruby; gutter: false; toolbar: false">
408
- &lt;div class='box'&gt;
409
- &lt;%= yield %&gt;
410
- &lt;/div&gt;
411
- </pre>
412
- </div>
413
- <p>The <tt>post</tt> partial wraps the post&#8217;s <tt>body</tt> in a <tt>div</tt> with the <tt>id</tt> of the post using the <tt>div_for</tt> helper:</p>
414
- <p><strong>posts/_post.html.erb</strong></p>
415
- <div class="code_container">
416
- <pre class="brush: ruby; gutter: false; toolbar: false">
417
- &lt;%= div_for(post) do %&gt;
418
- &lt;p&gt;&lt;%= post.body %&gt;&lt;/p&gt;
419
- &lt;% end %&gt;
420
- </pre>
421
- </div>
422
- <p>This example would output the following:</p>
423
- <div class="code_container">
424
- <pre class="brush: xml; gutter: false; toolbar: false">
425
- &lt;div class='box'&gt;
426
- &lt;div id='post_1'&gt;
427
- &lt;p&gt;Partial Layouts are cool!&lt;/p&gt;
428
- &lt;/div&gt;
429
- &lt;/div&gt;
430
- </pre>
431
- </div>
432
- <p>Note that the partial layout has access to the local <tt>post</tt> variable that was passed into the <tt>render</tt> call. However, unlike application-wide layouts, partial layouts still have the underscore prefix.</p>
433
- <p>You can also render a block of code within a partial layout instead of calling <tt>yield</tt>. For example, if we didn&#8217;t have the <tt>post</tt> partial, we could do this instead:</p>
434
- <p><strong>posts/show.html.erb</strong></p>
435
- <div class="code_container">
436
- <pre class="brush: ruby; gutter: false; toolbar: false">
437
- &lt;% render(:layout =&gt; 'box', :locals =&gt; {:post =&gt; @post}) do %&gt;
438
- &lt;%= div_for(post) do %&gt;
439
- &lt;p&gt;&lt;%= post.body %&gt;&lt;/p&gt;
440
- &lt;% end %&gt;
441
- &lt;% end %&gt;
442
- </pre>
443
- </div>
444
- <p>If we&#8217;re using the same <tt>box</tt> partial from above, his would produce the same output as the previous example.</p>
445
- <h3 id="view-paths">7 View Paths</h3>
446
- <p><span class="caps">TODO</span>&#8230;</p>
447
- <h3 id="overview-of-all-the-helpers-provided-by-action-view">8 Overview of all the helpers provided by Action View</h3>
448
- <p>The following is only a brief overview summary of the helpers available in Action View. It&#8217;s recommended that you review the <span class="caps">API</span> Documentation, which covers all of the helpers in more detail, but this should serve as a good starting point.</p>
449
- <h4 id="activerecordhelper">8.1 ActiveRecordHelper</h4>
450
- <p>The Active Record Helper makes it easier to create forms for records kept in instance variables. You may also want to review the <a href="form_helpers.html">Rails Form helpers guide</a>.</p>
451
- <h5 id="error_message_on">8.1.1 error_message_on</h5>
452
- <p>Returns a string containing the error message attached to the method on the object if one exists.</p>
453
- <div class="code_container">
454
- <pre class="brush: ruby; gutter: false; toolbar: false">
455
- error_message_on &quot;post&quot;, &quot;title&quot;
456
- </pre>
457
- </div>
458
- <h5 id="error_messages_for">8.1.2 error_messages_for</h5>
459
- <p>Returns a string with a <span class="caps">DIV</span> containing all of the error messages for the objects located as instance variables by the names given.</p>
460
- <div class="code_container">
461
- <pre class="brush: ruby; gutter: false; toolbar: false">
462
- error_messages_for &quot;post&quot;
463
- </pre>
464
- </div>
465
- <h5 id="form">8.1.3 form</h5>
466
- <p>Returns a form with inputs for all attributes of the specified Active Record object. For example, let&#8217;s say we have a <tt>@post</tt> with attributes named <tt>title</tt> of type <tt>String</tt> and <tt>body</tt> of type <tt>Text</tt>. Calling <tt>form</tt> would produce a form to creating a new post with inputs for those attributes.</p>
467
- <div class="code_container">
468
- <pre class="brush: ruby; gutter: false; toolbar: false">
469
- form(&quot;post&quot;)
470
- </pre>
471
- </div>
472
- <div class="code_container">
473
- <pre class="brush: xml; gutter: false; toolbar: false">
474
- &lt;form action='/posts/create' method='post'&gt;
475
- &lt;p&gt;
476
- &lt;label for=&quot;post_title&quot;&gt;Title&lt;/label&gt;&lt;br /&gt;
477
- &lt;input id=&quot;post_title&quot; name=&quot;post[title]&quot; size=&quot;30&quot; type=&quot;text&quot; value=&quot;Hello World&quot; /&gt;
478
- &lt;/p&gt;
479
- &lt;p&gt;
480
- &lt;label for=&quot;post_body&quot;&gt;Body&lt;/label&gt;&lt;br /&gt;
481
- &lt;textarea cols=&quot;40&quot; id=&quot;post_body&quot; name=&quot;post[body]&quot; rows=&quot;20&quot;&gt;&lt;/textarea&gt;
482
- &lt;/p&gt;
483
- &lt;input name=&quot;commit&quot; type=&quot;submit&quot; value=&quot;Create&quot; /&gt;
484
- &lt;/form&gt;
485
- </pre>
486
- </div>
487
- <p>Typically, <tt>form_for</tt> is used instead of <tt>form</tt> because it doesn&#8217;t automatically include all of the model&#8217;s attributes.</p>
488
- <h5 id="input">8.1.4 input</h5>
489
- <p>Returns a default input tag for the type of object returned by the method.</p>
490
- <p>For example, if <tt>@post</tt> has an attribute <tt>title</tt> mapped to a <tt>String</tt> column that holds &#8220;Hello World&#8221;:</p>
491
- <div class="code_container">
492
- <pre class="brush: ruby; gutter: false; toolbar: false">
493
- input(&quot;post&quot;, &quot;title&quot;) # =&gt;
494
- &lt;input id=&quot;post_title&quot; name=&quot;post[title]&quot; size=&quot;30&quot; type=&quot;text&quot; value=&quot;Hello World&quot; /&gt;
495
- </pre>
496
- </div>
497
- <h4 id="recordtaghelper">8.2 RecordTagHelper</h4>
498
- <p>This module provides methods for generating a container tag, such as a <tt><div></tt>, for your record. This is the recommended way of creating a container for render your Active Record object, as it adds an appropriate class and id attributes to that container. You can then refer to those containers easily by following the convention, instead of having to think about which class or id attribute you should use.</p>
499
- <h5 id="content_tag_for">8.2.1 content_tag_for</h5>
500
- <p>Renders a container tag that relates to your Active Record Object.</p>
501
- <p>For example, given <tt>@post</tt> is the object of <tt>Post</tt> class, you can do:</p>
502
- <div class="code_container">
503
- <pre class="brush: ruby; gutter: false; toolbar: false">
504
- &lt;%= content_tag_for(:tr, @post) do %&gt;
505
- &lt;td&gt;&lt;%= @post.title %&gt;&lt;/td&gt;
506
- &lt;% end %&gt;
507
- </pre>
508
- </div>
509
- <p>This will generate this <span class="caps">HTML</span> output:</p>
510
- <div class="code_container">
511
- <pre class="brush: xml; gutter: false; toolbar: false">
512
- &lt;tr id=&quot;post_1234&quot; class=&quot;post&quot;&gt;
513
- &lt;td&gt;Hello World!&lt;/td&gt;
514
- &lt;/tr&gt;
515
- </pre>
516
- </div>
517
- <p>You can also supply <span class="caps">HTML</span> attributes as an additional option hash. For example:</p>
518
- <div class="code_container">
519
- <pre class="brush: ruby; gutter: false; toolbar: false">
520
- &lt;%= content_tag_for(:tr, @post, :class =&gt; &quot;frontpage&quot;) do %&gt;
521
- &lt;td&gt;&lt;%= @post.title %&gt;&lt;/td&gt;
522
- &lt;% end %&gt;
523
- </pre>
524
- </div>
525
- <p>Will generate this <span class="caps">HTML</span> output:</p>
526
- <div class="code_container">
527
- <pre class="brush: xml; gutter: false; toolbar: false">
528
- &lt;tr id=&quot;post_1234&quot; class=&quot;post frontpage&quot;&gt;
529
- &lt;td&gt;Hello World!&lt;/td&gt;
530
- &lt;/tr&gt;
531
- </pre>
532
- </div>
533
- <p>You can pass a collection of Active Record objects. This method will loop through your objects and create a container for each of them. For example, given <tt>@posts</tt> is an array of two <tt>Post</tt> objects:</p>
534
- <div class="code_container">
535
- <pre class="brush: ruby; gutter: false; toolbar: false">
536
- &lt;%= content_tag_for(:tr, @posts) do |post| %&gt;
537
- &lt;td&gt;&lt;%= post.title %&gt;&lt;/td&gt;
538
- &lt;% end %&gt;
539
- </pre>
540
- </div>
541
- <p>Will generate this <span class="caps">HTML</span> output:</p>
542
- <div class="code_container">
543
- <pre class="brush: xml; gutter: false; toolbar: false">
544
- &lt;tr id=&quot;post_1234&quot; class=&quot;post&quot;&gt;
545
- &lt;td&gt;Hello World!&lt;/td&gt;
546
- &lt;/tr&gt;
547
- &lt;tr id=&quot;post_1235&quot; class=&quot;post&quot;&gt;
548
- &lt;td&gt;Ruby on Rails Rocks!&lt;/td&gt;
549
- &lt;/tr&gt;
550
- </pre>
551
- </div>
552
- <h5 id="div_for">8.2.2 div_for</h5>
553
- <p>This is actually a convenient method which calls <tt>content_tag_for</tt> internally with <tt>:div</tt> as the tag name. You can pass either an Active Record object or a collection of objects. For example:</p>
554
- <div class="code_container">
555
- <pre class="brush: ruby; gutter: false; toolbar: false">
556
- &lt;%= div_for(@post, :class =&gt; &quot;frontpage&quot;) do %&gt;
557
- &lt;td&gt;&lt;%= @post.title %&gt;&lt;/td&gt;
558
- &lt;% end %&gt;
559
- </pre>
560
- </div>
561
- <p>Will generate this <span class="caps">HTML</span> output:</p>
562
- <div class="code_container">
563
- <pre class="brush: xml; gutter: false; toolbar: false">
564
- &lt;div id=&quot;post_1234&quot; class=&quot;post frontpage&quot;&gt;
565
- &lt;td&gt;Hello World!&lt;/td&gt;
566
- &lt;/div&gt;
567
- </pre>
568
- </div>
569
- <h4 id="assettaghelper">8.3 AssetTagHelper</h4>
570
- <p>This module provides methods for generating <span class="caps">HTML</span> that links views to assets such as images, JavaScript files, stylesheets, and feeds.</p>
571
- <p>By default, Rails links to these assets on the current host in the public folder, but you can direct Rails to link to assets from a dedicated assets server by setting <tt>ActionController::Base.asset_host</tt> in the application configuration, typically in <tt>config/environments/production.rb</tt>. For example, let&#8217;s say your asset host is <tt>assets.example.com</tt>:</p>
572
- <div class="code_container">
573
- <pre class="brush: ruby; gutter: false; toolbar: false">
574
- ActionController::Base.asset_host = &quot;assets.example.com&quot;
575
- image_tag(&quot;rails.png&quot;) # =&gt; &lt;img src=&quot;http://assets.example.com/images/rails.png&quot; alt=&quot;Rails&quot; /&gt;
576
- </pre>
577
- </div>
578
- <h5 id="register_javascript_expansion">8.3.1 register_javascript_expansion</h5>
579
- <p>Register one or more JavaScript files to be included when symbol is passed to javascript_include_tag. This method is typically intended to be called from plugin initialization to register JavaScript files that the plugin installed in <tt>public/javascripts</tt>.</p>
580
- <div class="code_container">
581
- <pre class="brush: ruby; gutter: false; toolbar: false">
582
- ActionView::Helpers::AssetTagHelper.register_javascript_expansion :monkey =&gt; [&quot;head&quot;, &quot;body&quot;, &quot;tail&quot;]
583
-
584
- javascript_include_tag :monkey # =&gt;
585
- &lt;script type=&quot;text/javascript&quot; src=&quot;/javascripts/head.js&quot;&gt;&lt;/script&gt;
586
- &lt;script type=&quot;text/javascript&quot; src=&quot;/javascripts/body.js&quot;&gt;&lt;/script&gt;
587
- &lt;script type=&quot;text/javascript&quot; src=&quot;/javascripts/tail.js&quot;&gt;&lt;/script&gt;
588
- </pre>
589
- </div>
590
- <h5 id="register_stylesheet_expansion">8.3.2 register_stylesheet_expansion</h5>
591
- <p>Register one or more stylesheet files to be included when symbol is passed to <tt>stylesheet_link_tag</tt>. This method is typically intended to be called from plugin initialization to register stylesheet files that the plugin installed in <tt>public/stylesheets</tt>.</p>
592
- <div class="code_container">
593
- <pre class="brush: ruby; gutter: false; toolbar: false">
594
- ActionView::Helpers::AssetTagHelper.register_stylesheet_expansion :monkey =&gt; [&quot;head&quot;, &quot;body&quot;, &quot;tail&quot;]
595
-
596
- stylesheet_link_tag :monkey # =&gt;
597
- &lt;link href=&quot;/stylesheets/head.css&quot; media=&quot;screen&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
598
- &lt;link href=&quot;/stylesheets/body.css&quot; media=&quot;screen&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
599
- &lt;link href=&quot;/stylesheets/tail.css&quot; media=&quot;screen&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
600
- </pre>
601
- </div>
602
- <h5 id="auto_discovery_link_tag">8.3.3 auto_discovery_link_tag</h5>
603
- <p>Returns a link tag that browsers and news readers can use to auto-detect an <span class="caps">RSS</span> or <span class="caps">ATOM</span> feed.</p>
604
- <div class="code_container">
605
- <pre class="brush: ruby; gutter: false; toolbar: false">
606
- auto_discovery_link_tag(:rss, &quot;http://www.example.com/feed.rss&quot;, {:title =&gt; &quot;RSS Feed&quot;}) # =&gt;
607
- &lt;link rel=&quot;alternate&quot; type=&quot;application/rss+xml&quot; title=&quot;RSS Feed&quot; href=&quot;http://www.example.com/feed&quot; /&gt;
608
- </pre>
609
- </div>
610
- <h5 id="image_path">8.3.4 image_path</h5>
611
- <p>Computes the path to an image asset in the <tt>public/images</tt> directory. Full paths from the document root will be passed through. Used internally by <tt>image_tag</tt> to build the image path.</p>
612
- <div class="code_container">
613
- <pre class="brush: ruby; gutter: false; toolbar: false">
614
- image_path(&quot;edit.png&quot;) # =&gt; /images/edit.png
615
- </pre>
616
- </div>
617
- <h5 id="image_tag">8.3.5 image_tag</h5>
618
- <p>Returns an html image tag for the source. The source can be a full path or a file that exists in your <tt>public/images</tt> directory.</p>
619
- <div class="code_container">
620
- <pre class="brush: ruby; gutter: false; toolbar: false">
621
- image_tag(&quot;icon.png&quot;) # =&gt; &lt;img src=&quot;/images/icon.png&quot; alt=&quot;Icon&quot; /&gt;
622
- </pre>
623
- </div>
624
- <h5 id="javascript_include_tag">8.3.6 javascript_include_tag</h5>
625
- <p>Returns an html script tag for each of the sources provided. You can pass in the filename (<tt>.js</tt> extension is optional) of JavaScript files that exist in your <tt>public/javascripts</tt> directory for inclusion into the current page or you can pass the full path relative to your document root.</p>
626
- <div class="code_container">
627
- <pre class="brush: ruby; gutter: false; toolbar: false">
628
- javascript_include_tag &quot;common&quot; # =&gt;
629
- &lt;script type=&quot;text/javascript&quot; src=&quot;/javascripts/common.js&quot;&gt;&lt;/script&gt;
630
- </pre>
631
- </div>
632
- <p>If the application does not use the asset pipeline, to include the jQuery JavaScript library in your application, pass <tt>:defaults</tt> as the source. When using <tt>:defaults</tt>, if an <tt>application.js</tt> file exists in your <tt>public/javascripts</tt> directory, it will be included as well.</p>
633
- <div class="code_container">
634
- <pre class="brush: ruby; gutter: false; toolbar: false">
635
- javascript_include_tag :defaults
636
- </pre>
637
- </div>
638
- <p>You can also include all JavaScript files in the <tt>public/javascripts</tt> directory using <tt>:all</tt> as the source.</p>
639
- <div class="code_container">
640
- <pre class="brush: ruby; gutter: false; toolbar: false">
641
- javascript_include_tag :all
642
- </pre>
643
- </div>
644
- <p>You can also cache multiple JavaScript files into one file, which requires less <span class="caps">HTTP</span> connections to download and can better be compressed by gzip (leading to faster transfers). Caching will only happen if <tt>ActionController::Base.perform_caching</tt> is set to true (which is the case by default for the Rails production environment, but not for the development environment).</p>
645
- <div class="code_container">
646
- <pre class="brush: ruby; gutter: false; toolbar: false">
647
- javascript_include_tag :all, :cache =&gt; true # =&gt;
648
- &lt;script type=&quot;text/javascript&quot; src=&quot;/javascripts/all.js&quot;&gt;&lt;/script&gt;
649
- </pre>
650
- </div>
651
- <h5 id="javascript_path">8.3.7 javascript_path</h5>
652
- <p>Computes the path to a JavaScript asset in the <tt>public/javascripts</tt> directory. If the source filename has no extension, <tt>.js</tt> will be appended. Full paths from the document root will be passed through. Used internally by <tt>javascript_include_tag</tt> to build the script path.</p>
653
- <div class="code_container">
654
- <pre class="brush: ruby; gutter: false; toolbar: false">
655
- javascript_path &quot;common&quot; # =&gt; /javascripts/common.js
656
- </pre>
657
- </div>
658
- <h5 id="stylesheet_link_tag">8.3.8 stylesheet_link_tag</h5>
659
- <p>Returns a stylesheet link tag for the sources specified as arguments. If you don&#8217;t specify an extension, <tt>.css</tt> will be appended automatically.</p>
660
- <div class="code_container">
661
- <pre class="brush: ruby; gutter: false; toolbar: false">
662
- stylesheet_link_tag &quot;application&quot; # =&gt;
663
- &lt;link href=&quot;/stylesheets/application.css&quot; media=&quot;screen&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
664
- </pre>
665
- </div>
666
- <p>You can also include all styles in the stylesheet directory using :all as the source:</p>
667
- <div class="code_container">
668
- <pre class="brush: ruby; gutter: false; toolbar: false">
669
- stylesheet_link_tag :all
670
- </pre>
671
- </div>
672
- <p>You can also cache multiple stylesheets into one file, which requires less <span class="caps">HTTP</span> connections and can better be compressed by gzip (leading to faster transfers). Caching will only happen if ActionController::Base.perform_caching is set to true (which is the case by default for the Rails production environment, but not for the development environment).</p>
673
- <div class="code_container">
674
- <pre class="brush: ruby; gutter: false; toolbar: false">
675
- stylesheet_link_tag :all, :cache =&gt; true
676
- &lt;link href=&quot;/stylesheets/all.css&quot; media=&quot;screen&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
677
- </pre>
678
- </div>
679
- <h5 id="stylesheet_path">8.3.9 stylesheet_path</h5>
680
- <p>Computes the path to a stylesheet asset in the <tt>public/stylesheets</tt> directory. If the source filename has no extension, .css will be appended. Full paths from the document root will be passed through. Used internally by stylesheet_link_tag to build the stylesheet path.</p>
681
- <div class="code_container">
682
- <pre class="brush: ruby; gutter: false; toolbar: false">
683
- stylesheet_path &quot;application&quot; # =&gt; /stylesheets/application.css
684
- </pre>
685
- </div>
686
- <h4 id="atomfeedhelper">8.4 AtomFeedHelper</h4>
687
- <h5 id="atom_feed">8.4.1 atom_feed</h5>
688
- <p>This helper makes building an <span class="caps">ATOM</span> feed easy. Here&#8217;s a full usage example:</p>
689
- <p><strong>config/routes.rb</strong></p>
690
- <div class="code_container">
691
- <pre class="brush: ruby; gutter: false; toolbar: false">
692
- resources :posts
693
- </pre>
694
- </div>
695
- <p><strong>app/controllers/posts_controller.rb</strong></p>
696
- <div class="code_container">
697
- <pre class="brush: ruby; gutter: false; toolbar: false">
698
- def index
699
- @posts = Post.all
700
-
701
- respond_to do |format|
702
- format.html
703
- format.atom
704
- end
705
- end
706
- </pre>
707
- </div>
708
- <p><strong>app/views/posts/index.atom.builder</strong></p>
709
- <div class="code_container">
710
- <pre class="brush: ruby; gutter: false; toolbar: false">
711
- atom_feed do |feed|
712
- feed.title(&quot;Posts Index&quot;)
713
- feed.updated((@posts.first.created_at))
714
-
715
- @posts.each do |post|
716
- feed.entry(post) do |entry|
717
- entry.title(post.title)
718
- entry.content(post.body, :type =&gt; 'html')
719
-
720
- entry.author do |author|
721
- author.name(post.author_name)
722
- end
723
- end
724
- end
725
- end
726
- </pre>
727
- </div>
728
- <h4 id="benchmarkhelper">8.5 BenchmarkHelper</h4>
729
- <h5 id="benchmark">8.5.1 benchmark</h5>
730
- <p>Allows you to measure the execution time of a block in a template and records the result to the log. Wrap this block around expensive operations or possible bottlenecks to get a time reading for the operation.</p>
731
- <div class="code_container">
732
- <pre class="brush: ruby; gutter: false; toolbar: false">
733
- &lt;% benchmark &quot;Process data files&quot; do %&gt;
734
- &lt;%= expensive_files_operation %&gt;
735
- &lt;% end %&gt;
736
- </pre>
737
- </div>
738
- <p>This would add something like &#8220;Process data files (0.34523)&#8221; to the log, which you can then use to compare timings when optimizing your code.</p>
739
- <h4 id="cachehelper">8.6 CacheHelper</h4>
740
- <h5 id="cache">8.6.1 cache</h5>
741
- <p>A method for caching fragments of a view rather than an entire action or page. This technique is useful caching pieces like menus, lists of news topics, static <span class="caps">HTML</span> fragments, and so on. This method takes a block that contains the content you wish to cache. See <tt>ActionController::Caching::Fragments</tt> for more information.</p>
742
- <div class="code_container">
743
- <pre class="brush: ruby; gutter: false; toolbar: false">
744
- &lt;% cache do %&gt;
745
- &lt;%= render &quot;shared/footer&quot; %&gt;
746
- &lt;% end %&gt;
747
- </pre>
748
- </div>
749
- <h4 id="capturehelper">8.7 CaptureHelper</h4>
750
- <h5 id="capture">8.7.1 capture</h5>
751
- <p>The <tt>capture</tt> method allows you to extract part of a template into a variable. You can then use this variable anywhere in your templates or layout.</p>
752
- <div class="code_container">
753
- <pre class="brush: ruby; gutter: false; toolbar: false">
754
- &lt;% @greeting = capture do %&gt;
755
- &lt;p&gt;Welcome! The date and time is &lt;%= Time.now %&gt;&lt;/p&gt;
756
- &lt;% end %&gt;
757
- </pre>
758
- </div>
759
- <p>The captured variable can then be used anywhere else.</p>
760
- <div class="code_container">
761
- <pre class="brush: ruby; gutter: false; toolbar: false">
762
- &lt;html&gt;
763
- &lt;head&gt;
764
- &lt;title&gt;Welcome!&lt;/title&gt;
765
- &lt;/head&gt;
766
- &lt;body&gt;
767
- &lt;%= @greeting %&gt;
768
- &lt;/body&gt;
769
- &lt;/html&gt;
770
- </pre>
771
- </div>
772
- <h5 id="content_for">8.7.2 content_for</h5>
773
- <p>Calling <tt>content_for</tt> stores a block of markup in an identifier for later use. You can make subsequent calls to the stored content in other templates or the layout by passing the identifier as an argument to <tt>yield</tt>.</p>
774
- <p>For example, let&#8217;s say we have a standard application layout, but also a special page that requires certain Javascript that the rest of the site doesn&#8217;t need. We can use <tt>content_for</tt> to include this Javascript on our special page without fattening up the rest of the site.</p>
775
- <p><strong>app/views/layouts/application.html.erb</strong></p>
776
- <div class="code_container">
777
- <pre class="brush: ruby; gutter: false; toolbar: false">
778
- &lt;html&gt;
779
- &lt;head&gt;
780
- &lt;title&gt;Welcome!&lt;/title&gt;
781
- &lt;%= yield :special_script %&gt;
782
- &lt;/head&gt;
783
- &lt;body&gt;
784
- &lt;p&gt;Welcome! The date and time is &lt;%= Time.now %&gt;&lt;/p&gt;
785
- &lt;/body&gt;
786
- &lt;/html&gt;
787
- </pre>
788
- </div>
789
- <p><strong>app/views/posts/special.html.erb</strong></p>
790
- <div class="code_container">
791
- <pre class="brush: ruby; gutter: false; toolbar: false">
792
- &lt;p&gt;This is a special page.&lt;/p&gt;
793
-
794
- &lt;% content_for :special_script do %&gt;
795
- &lt;script type=&quot;text/javascript&quot;&gt;alert('Hello!')&lt;/script&gt;
796
- &lt;% end %&gt;
797
- </pre>
798
- </div>
799
- <h4 id="datehelper">8.8 DateHelper</h4>
800
- <h5 id="date_select">8.8.1 date_select</h5>
801
- <p>Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute.</p>
802
- <div class="code_container">
803
- <pre class="brush: ruby; gutter: false; toolbar: false">
804
- date_select(&quot;post&quot;, &quot;published_on&quot;)
805
- </pre>
806
- </div>
807
- <h5 id="datetime_select">8.8.2 datetime_select</h5>
808
- <p>Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based attribute.</p>
809
- <div class="code_container">
810
- <pre class="brush: ruby; gutter: false; toolbar: false">
811
- datetime_select(&quot;post&quot;, &quot;published_on&quot;)
812
- </pre>
813
- </div>
814
- <h5 id="distance_of_time_in_words">8.8.3 distance_of_time_in_words</h5>
815
- <p>Reports the approximate distance in time between two Time or Date objects or integers as seconds. Set <tt>include_seconds</tt> to true if you want more detailed approximations.</p>
816
- <div class="code_container">
817
- <pre class="brush: ruby; gutter: false; toolbar: false">
818
- distance_of_time_in_words(Time.now, Time.now + 15.seconds) # =&gt; less than a minute
819
- distance_of_time_in_words(Time.now, Time.now + 15.seconds, true) # =&gt; less than 20 seconds
820
- </pre>
821
- </div>
822
- <h5 id="select_date">8.8.4 select_date</h5>
823
- <p>Returns a set of html select-tags (one for year, month, and day) pre-selected with the <tt>date</tt> provided.</p>
824
- <div class="code_container">
825
- <pre class="brush: ruby; gutter: false; toolbar: false">
826
- # Generates a date select that defaults to the date provided (six days after today)
827
- select_date(Time.today + 6.days)
828
-
829
- # Generates a date select that defaults to today (no specified date)
830
- select_date()
831
- </pre>
832
- </div>
833
- <h5 id="select_datetime">8.8.5 select_datetime</h5>
834
- <p>Returns a set of html select-tags (one for year, month, day, hour, and minute) pre-selected with the <tt>datetime</tt> provided.</p>
835
- <div class="code_container">
836
- <pre class="brush: ruby; gutter: false; toolbar: false">
837
- # Generates a datetime select that defaults to the datetime provided (four days after today)
838
- select_datetime(Time.now + 4.days)
839
-
840
- # Generates a datetime select that defaults to today (no specified datetime)
841
- select_datetime()
842
- </pre>
843
- </div>
844
- <h5 id="select_day">8.8.6 select_day</h5>
845
- <p>Returns a select tag with options for each of the days 1 through 31 with the current day selected.</p>
846
- <div class="code_container">
847
- <pre class="brush: ruby; gutter: false; toolbar: false">
848
- # Generates a select field for days that defaults to the day for the date provided
849
- select_day(Time.today + 2.days)
850
-
851
- # Generates a select field for days that defaults to the number given
852
- select_day(5)
853
- </pre>
854
- </div>
855
- <h5 id="select_hour">8.8.7 select_hour</h5>
856
- <p>Returns a select tag with options for each of the hours 0 through 23 with the current hour selected.</p>
857
- <div class="code_container">
858
- <pre class="brush: ruby; gutter: false; toolbar: false">
859
- # Generates a select field for minutes that defaults to the minutes for the time provided
860
- select_minute(Time.now + 6.hours)
861
- </pre>
862
- </div>
863
- <h5 id="select_minute">8.8.8 select_minute</h5>
864
- <p>Returns a select tag with options for each of the minutes 0 through 59 with the current minute selected.</p>
865
- <div class="code_container">
866
- <pre class="brush: ruby; gutter: false; toolbar: false">
867
- # Generates a select field for minutes that defaults to the minutes for the time provided.
868
- select_minute(Time.now + 6.hours)
869
- </pre>
870
- </div>
871
- <h5 id="select_month">8.8.9 select_month</h5>
872
- <p>Returns a select tag with options for each of the months January through December with the current month selected.</p>
873
- <div class="code_container">
874
- <pre class="brush: ruby; gutter: false; toolbar: false">
875
- # Generates a select field for months that defaults to the current month
876
- select_month(Date.today)
877
- </pre>
878
- </div>
879
- <h5 id="select_second">8.8.10 select_second</h5>
880
- <p>Returns a select tag with options for each of the seconds 0 through 59 with the current second selected.</p>
881
- <div class="code_container">
882
- <pre class="brush: ruby; gutter: false; toolbar: false">
883
- # Generates a select field for seconds that defaults to the seconds for the time provided
884
- select_second(Time.now + 16.minutes)
885
- </pre>
886
- </div>
887
- <h5 id="select_time">8.8.11 select_time</h5>
888
- <p>Returns a set of html select-tags (one for hour and minute).</p>
889
- <div class="code_container">
890
- <pre class="brush: ruby; gutter: false; toolbar: false">
891
- # Generates a time select that defaults to the time provided
892
- select_time(Time.now)
893
- </pre>
894
- </div>
895
- <h5 id="select_year">8.8.12 select_year</h5>
896
- <p>Returns a select tag with options for each of the five years on each side of the current, which is selected. The five year radius can be changed using the <tt>:start_year</tt> and <tt>:end_year</tt> keys in the <tt>options</tt>.</p>
897
- <div class="code_container">
898
- <pre class="brush: ruby; gutter: false; toolbar: false">
899
- # Generates a select field for five years on either side of Date.today that defaults to the current year
900
- select_year(Date.today)
901
-
902
- # Generates a select field from 1900 to 2009 that defaults to the current year
903
- select_year(Date.today, :start_year =&gt; 1900, :end_year =&gt; 2009)
904
- </pre>
905
- </div>
906
- <h5 id="time_ago_in_words">8.8.13 time_ago_in_words</h5>
907
- <p>Like <tt>distance_of_time_in_words</tt>, but where <tt>to_time</tt> is fixed to <tt>Time.now</tt>.</p>
908
- <div class="code_container">
909
- <pre class="brush: ruby; gutter: false; toolbar: false">
910
- time_ago_in_words(3.minutes.from_now) # =&gt; 3 minutes
911
- </pre>
912
- </div>
913
- <h5 id="time_select">8.8.14 time_select</h5>
914
- <p>Returns a set of select tags (one for hour, minute and optionally second) pre-selected for accessing a specified time-based attribute. The selects are prepared for multi-parameter assignment to an Active Record object.</p>
915
- <div class="code_container">
916
- <pre class="brush: ruby; gutter: false; toolbar: false">
917
- # Creates a time select tag that, when POSTed, will be stored in the order variable in the submitted attribute
918
- time_select(&quot;order&quot;, &quot;submitted&quot;)
919
- </pre>
920
- </div>
921
- <h4 id="debughelper">8.9 DebugHelper</h4>
922
- <p>Returns a <tt>pre</tt> tag that has object dumped by <span class="caps">YAML</span>. This creates a very readable way to inspect an object.</p>
923
- <div class="code_container">
924
- <pre class="brush: ruby; gutter: false; toolbar: false">
925
- my_hash = {'first' =&gt; 1, 'second' =&gt; 'two', 'third' =&gt; [1,2,3]}
926
- debug(my_hash)
927
- </pre>
928
- </div>
929
- <div class="code_container">
930
- <pre class="brush: xml; gutter: false; toolbar: false">
931
- &lt;pre class='debug_dump'&gt;---
932
- first: 1
933
- second: two
934
- third:
935
- - 1
936
- - 2
937
- - 3
938
- &lt;/pre&gt;
939
- </pre>
940
- </div>
941
- <h4 id="formhelper">8.10 FormHelper</h4>
942
- <p>Form helpers are designed to make working with models much easier compared to using just standard <span class="caps">HTML</span> elements by providing a set of methods for creating forms based on your models. This helper generates the <span class="caps">HTML</span> for forms, providing a method for each sort of input (e.g., text, password, select, and so on). When the form is submitted (i.e., when the user hits the submit button or form.submit is called via JavaScript), the form inputs will be bundled into the params object and passed back to the controller.</p>
943
- <p>There are two types of form helpers: those that specifically work with model attributes and those that don&#8217;t. This helper deals with those that work with model attributes; to see an example of form helpers that don&#8217;t work with model attributes, check the ActionView::Helpers::FormTagHelper documentation.</p>
944
- <p>The core method of this helper, form_for, gives you the ability to create a form for a model instance; for example, let&#8217;s say that you have a model Person and want to create a new instance of it:</p>
945
- <div class="code_container">
946
- <pre class="brush: ruby; gutter: false; toolbar: false">
947
- # Note: a @person variable will have been created in the controller (e.g. @person = Person.new)
948
- &lt;%= form_for @person, :url =&gt; { :action =&gt; &quot;create&quot; } do |f| %&gt;
949
- &lt;%= f.text_field :first_name %&gt;
950
- &lt;%= f.text_field :last_name %&gt;
951
- &lt;%= submit_tag 'Create' %&gt;
952
- &lt;% end %&gt;
953
- </pre>
954
- </div>
955
- <p>The <span class="caps">HTML</span> generated for this would be:</p>
956
- <div class="code_container">
957
- <pre class="brush: xml; gutter: false; toolbar: false">
958
- &lt;form action=&quot;/persons/create&quot; method=&quot;post&quot;&gt;
959
- &lt;input id=&quot;person_first_name&quot; name=&quot;person[first_name]&quot; size=&quot;30&quot; type=&quot;text&quot; /&gt;
960
- &lt;input id=&quot;person_last_name&quot; name=&quot;person[last_name]&quot; size=&quot;30&quot; type=&quot;text&quot; /&gt;
961
- &lt;input name=&quot;commit&quot; type=&quot;submit&quot; value=&quot;Create&quot; /&gt;
962
- &lt;/form&gt;
963
- </pre>
964
- </div>
965
- <p>The params object created when this form is submitted would look like:</p>
966
- <div class="code_container">
967
- <pre class="brush: ruby; gutter: false; toolbar: false">
968
- {&quot;action&quot;=&gt;&quot;create&quot;, &quot;controller&quot;=&gt;&quot;persons&quot;, &quot;person&quot;=&gt;{&quot;first_name&quot;=&gt;&quot;William&quot;, &quot;last_name&quot;=&gt;&quot;Smith&quot;}}
969
- </pre>
970
- </div>
971
- <p>The params hash has a nested person value, which can therefore be accessed with params[:person] in the controller.</p>
972
- <h5 id="check_box">8.10.1 check_box</h5>
973
- <p>Returns a checkbox tag tailored for accessing a specified attribute.</p>
974
- <div class="code_container">
975
- <pre class="brush: ruby; gutter: false; toolbar: false">
976
- # Let's say that @post.validated? is 1:
977
- check_box(&quot;post&quot;, &quot;validated&quot;)
978
- # =&gt; &lt;input type=&quot;checkbox&quot; id=&quot;post_validated&quot; name=&quot;post[validated]&quot; value=&quot;1&quot; /&gt;
979
- # &lt;input name=&quot;post[validated]&quot; type=&quot;hidden&quot; value=&quot;0&quot; /&gt;
980
- </pre>
981
- </div>
982
- <h5 id="fields_for">8.10.2 fields_for</h5>
983
- <p>Creates a scope around a specific model object like form_for, but doesn&#8217;t create the form tags themselves. This makes fields_for suitable for specifying additional model objects in the same form:</p>
984
- <div class="code_container">
985
- <pre class="brush: ruby; gutter: false; toolbar: false">
986
- &lt;%= form_for @person, :url =&gt; { :action =&gt; &quot;update&quot; } do |person_form| %&gt;
987
- First name: &lt;%= person_form.text_field :first_name %&gt;
988
- Last name : &lt;%= person_form.text_field :last_name %&gt;
989
-
990
- &lt;%= fields_for @person.permission do |permission_fields| %&gt;
991
- Admin? : &lt;%= permission_fields.check_box :admin %&gt;
992
- &lt;% end %&gt;
993
- &lt;% end %&gt;
994
- </pre>
995
- </div>
996
- <h5 id="file_field">8.10.3 file_field</h5>
997
- <p>Returns a file upload input tag tailored for accessing a specified attribute.</p>
998
- <div class="code_container">
999
- <pre class="brush: ruby; gutter: false; toolbar: false">
1000
- file_field(:user, :avatar)
1001
- # =&gt; &lt;input type=&quot;file&quot; id=&quot;user_avatar&quot; name=&quot;user[avatar]&quot; /&gt;
1002
- </pre>
1003
- </div>
1004
- <h5 id="form_for">8.10.4 form_for</h5>
1005
- <p>Creates a form and a scope around a specific model object that is used as a base for questioning about values for the fields.</p>
1006
- <div class="code_container">
1007
- <pre class="brush: ruby; gutter: false; toolbar: false">
1008
- &lt;%= form_for @post do |f| %&gt;
1009
- &lt;%= f.label :title, 'Title' %&gt;:
1010
- &lt;%= f.text_field :title %&gt;&lt;br /&gt;
1011
- &lt;%= f.label :body, 'Body' %&gt;:
1012
- &lt;%= f.text_area :body %&gt;&lt;br /&gt;
1013
- &lt;% end %&gt;
1014
- </pre>
1015
- </div>
1016
- <h5 id="hidden_field">8.10.5 hidden_field</h5>
1017
- <p>Returns a hidden input tag tailored for accessing a specified attribute.</p>
1018
- <div class="code_container">
1019
- <pre class="brush: ruby; gutter: false; toolbar: false">
1020
- hidden_field(:user, :token)
1021
- # =&gt; &lt;input type=&quot;hidden&quot; id=&quot;user_token&quot; name=&quot;user[token]&quot; value=&quot;#{@user.token}&quot; /&gt;
1022
- </pre>
1023
- </div>
1024
- <h5 id="label">8.10.6 label</h5>
1025
- <p>Returns a label tag tailored for labelling an input field for a specified attribute.</p>
1026
- <div class="code_container">
1027
- <pre class="brush: ruby; gutter: false; toolbar: false">
1028
- label(:post, :title)
1029
- # =&gt; &lt;label for=&quot;post_title&quot;&gt;Title&lt;/label&gt;
1030
- </pre>
1031
- </div>
1032
- <h5 id="password_field">8.10.7 password_field</h5>
1033
- <p>Returns an input tag of the &#8220;password&#8221; type tailored for accessing a specified attribute.</p>
1034
- <div class="code_container">
1035
- <pre class="brush: ruby; gutter: false; toolbar: false">
1036
- password_field(:login, :pass)
1037
- # =&gt; &lt;input type=&quot;text&quot; id=&quot;login_pass&quot; name=&quot;login[pass]&quot; value=&quot;#{@login.pass}&quot; /&gt;
1038
- </pre>
1039
- </div>
1040
- <h5 id="radio_button">8.10.8 radio_button</h5>
1041
- <p>Returns a radio button tag for accessing a specified attribute.</p>
1042
- <div class="code_container">
1043
- <pre class="brush: ruby; gutter: false; toolbar: false">
1044
- # Let's say that @post.category returns &quot;rails&quot;:
1045
- radio_button(&quot;post&quot;, &quot;category&quot;, &quot;rails&quot;)
1046
- radio_button(&quot;post&quot;, &quot;category&quot;, &quot;java&quot;)
1047
- # =&gt; &lt;input type=&quot;radio&quot; id=&quot;post_category_rails&quot; name=&quot;post[category]&quot; value=&quot;rails&quot; checked=&quot;checked&quot; /&gt;
1048
- # &lt;input type=&quot;radio&quot; id=&quot;post_category_java&quot; name=&quot;post[category]&quot; value=&quot;java&quot; /&gt;
1049
- </pre>
1050
- </div>
1051
- <h5 id="text_area">8.10.9 text_area</h5>
1052
- <p>Returns a textarea opening and closing tag set tailored for accessing a specified attribute.</p>
1053
- <div class="code_container">
1054
- <pre class="brush: ruby; gutter: false; toolbar: false">
1055
- text_area(:comment, :text, :size =&gt; &quot;20x30&quot;)
1056
- # =&gt; &lt;textarea cols=&quot;20&quot; rows=&quot;30&quot; id=&quot;comment_text&quot; name=&quot;comment[text]&quot;&gt;
1057
- # #{@comment.text}
1058
- # &lt;/textarea&gt;
1059
- </pre>
1060
- </div>
1061
- <h5 id="text_field">8.10.10 text_field</h5>
1062
- <p>Returns an input tag of the &#8220;text&#8221; type tailored for accessing a specified attribute.</p>
1063
- <div class="code_container">
1064
- <pre class="brush: ruby; gutter: false; toolbar: false">
1065
- text_field(:post, :title)
1066
- # =&gt; &lt;input type=&quot;text&quot; id=&quot;post_title&quot; name=&quot;post[title]&quot; value=&quot;#{@post.title}&quot; /&gt;
1067
- </pre>
1068
- </div>
1069
- <h4 id="formoptionshelper">8.11 FormOptionsHelper</h4>
1070
- <p>Provides a number of methods for turning different kinds of containers into a set of option tags.</p>
1071
- <h5 id="collection_select">8.11.1 collection_select</h5>
1072
- <p>Returns <tt>select</tt> and <tt>option</tt> tags for the collection of existing return values of <tt>method</tt> for <tt>object</tt>&#8217;s class.</p>
1073
- <p>Example object structure for use with this method:</p>
1074
- <div class="code_container">
1075
- <pre class="brush: ruby; gutter: false; toolbar: false">
1076
- class Post &lt; ActiveRecord::Base
1077
- belongs_to :author
1078
- end
1079
-
1080
- class Author &lt; ActiveRecord::Base
1081
- has_many :posts
1082
- def name_with_initial
1083
- &quot;#{first_name.first}. #{last_name}&quot;
1084
- end
1085
- end
1086
- </pre>
1087
- </div>
1088
- <p>Sample usage (selecting the associated Author for an instance of Post, <tt>@post</tt>):</p>
1089
- <div class="code_container">
1090
- <pre class="brush: ruby; gutter: false; toolbar: false">
1091
- collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {:prompt =&gt; true})
1092
- </pre>
1093
- </div>
1094
- <p>If <tt>@post.author_id</tt> is 1, this would return:</p>
1095
- <div class="code_container">
1096
- <pre class="brush: xml; gutter: false; toolbar: false">
1097
- &lt;select name=&quot;post[author_id]&quot;&gt;
1098
- &lt;option value=&quot;&quot;&gt;Please select&lt;/option&gt;
1099
- &lt;option value=&quot;1&quot; selected=&quot;selected&quot;&gt;D. Heinemeier Hansson&lt;/option&gt;
1100
- &lt;option value=&quot;2&quot;&gt;D. Thomas&lt;/option&gt;
1101
- &lt;option value=&quot;3&quot;&gt;M. Clark&lt;/option&gt;
1102
- &lt;/select&gt;
1103
- </pre>
1104
- </div>
1105
- <h5 id="country_options_for_select">8.11.2 country_options_for_select</h5>
1106
- <p>Returns a string of option tags for pretty much any country in the world.</p>
1107
- <h5 id="country_select">8.11.3 country_select</h5>
1108
- <p>Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.</p>
1109
- <h5 id="option_groups_from_collection_for_select">8.11.4 option_groups_from_collection_for_select</h5>
1110
- <p>Returns a string of <tt>option</tt> tags, like <tt>options_from_collection_for_select</tt>, but groups them by <tt>optgroup</tt> tags based on the object relationships of the arguments.</p>
1111
- <p>Example object structure for use with this method:</p>
1112
- <div class="code_container">
1113
- <pre class="brush: ruby; gutter: false; toolbar: false">
1114
- class Continent &lt; ActiveRecord::Base
1115
- has_many :countries
1116
- # attribs: id, name
1117
- end
1118
-
1119
- class Country &lt; ActiveRecord::Base
1120
- belongs_to :continent
1121
- # attribs: id, name, continent_id
1122
- end
1123
- </pre>
1124
- </div>
1125
- <p>Sample usage:</p>
1126
- <div class="code_container">
1127
- <pre class="brush: ruby; gutter: false; toolbar: false">
1128
- option_groups_from_collection_for_select(@continents, :countries, :name, :id, :name, 3)
1129
- </pre>
1130
- </div>
1131
- <p>Possible output:</p>
1132
- <div class="code_container">
1133
- <pre class="brush: xml; gutter: false; toolbar: false">
1134
- &lt;optgroup label=&quot;Africa&quot;&gt;
1135
- &lt;option value=&quot;1&quot;&gt;Egypt&lt;/option&gt;
1136
- &lt;option value=&quot;4&quot;&gt;Rwanda&lt;/option&gt;
1137
- ...
1138
- &lt;/optgroup&gt;
1139
- &lt;optgroup label=&quot;Asia&quot;&gt;
1140
- &lt;option value=&quot;3&quot; selected=&quot;selected&quot;&gt;China&lt;/option&gt;
1141
- &lt;option value=&quot;12&quot;&gt;India&lt;/option&gt;
1142
- &lt;option value=&quot;5&quot;&gt;Japan&lt;/option&gt;
1143
- ...
1144
- &lt;/optgroup&gt;
1145
- </pre>
1146
- </div>
1147
- <p>Note: Only the <tt>optgroup</tt> and <tt>option</tt> tags are returned, so you still have to wrap the output in an appropriate <tt>select</tt> tag.</p>
1148
- <h5 id="options_for_select">8.11.5 options_for_select</h5>
1149
- <p>Accepts a container (hash, array, enumerable, your type) and returns a string of option tags.</p>
1150
- <div class="code_container">
1151
- <pre class="brush: ruby; gutter: false; toolbar: false">
1152
- options_for_select([ &quot;VISA&quot;, &quot;MasterCard&quot; ])
1153
- # =&gt; &lt;option&gt;VISA&lt;/option&gt; &lt;option&gt;MasterCard&lt;/option&gt;
1154
- </pre>
1155
- </div>
1156
- <p>Note: Only the <tt>option</tt> tags are returned, you have to wrap this call in a regular <span class="caps">HTML</span> <tt>select</tt> tag.</p>
1157
- <h5 id="options_from_collection_for_select">8.11.6 options_from_collection_for_select</h5>
1158
- <p>Returns a string of option tags that have been compiled by iterating over the <tt>collection</tt> and assigning the result of a call to the <tt>value_method</tt> as the option value and the <tt>text_method</tt> as the option text.</p>
1159
- <div class="code_container">
1160
- <pre class="brush: ruby; gutter: false; toolbar: false">
1161
- # options_from_collection_for_select(collection, value_method, text_method, selected = nil)
1162
- </pre>
1163
- </div>
1164
- <p>For example, imagine a loop iterating over each person in @project.people to generate an input tag:</p>
1165
- <div class="code_container">
1166
- <pre class="brush: ruby; gutter: false; toolbar: false">
1167
- options_from_collection_for_select(@project.people, &quot;id&quot;, &quot;name&quot;)
1168
- # =&gt; &lt;option value=&quot;#{person.id}&quot;&gt;#{person.name}&lt;/option&gt;
1169
- </pre>
1170
- </div>
1171
- <p>Note: Only the <tt>option</tt> tags are returned, you have to wrap this call in a regular <span class="caps">HTML</span> <tt>select</tt> tag.</p>
1172
- <h5 id="select">8.11.7 select</h5>
1173
- <p>Create a select tag and a series of contained option tags for the provided object and method.</p>
1174
- <p>Example:</p>
1175
- <div class="code_container">
1176
- <pre class="brush: ruby; gutter: false; toolbar: false">
1177
- select(&quot;post&quot;, &quot;person_id&quot;, Person.all.collect {|p| [ p.name, p.id ] }, { :include_blank =&gt; true })
1178
- </pre>
1179
- </div>
1180
- <p>If <tt>@post.person_id</tt> is 1, this would become:</p>
1181
- <div class="code_container">
1182
- <pre class="brush: xml; gutter: false; toolbar: false">
1183
- &lt;select name=&quot;post[person_id]&quot;&gt;
1184
- &lt;option value=&quot;&quot;&gt;&lt;/option&gt;
1185
- &lt;option value=&quot;1&quot; selected=&quot;selected&quot;&gt;David&lt;/option&gt;
1186
- &lt;option value=&quot;2&quot;&gt;Sam&lt;/option&gt;
1187
- &lt;option value=&quot;3&quot;&gt;Tobias&lt;/option&gt;
1188
- &lt;/select&gt;
1189
- </pre>
1190
- </div>
1191
- <h5 id="time_zone_options_for_select">8.11.8 time_zone_options_for_select</h5>
1192
- <p>Returns a string of option tags for pretty much any time zone in the world.</p>
1193
- <h5 id="time_zone_select">8.11.9 time_zone_select</h5>
1194
- <p>Return select and option tags for the given object and method, using <tt>time_zone_options_for_select</tt> to generate the list of option tags.</p>
1195
- <div class="code_container">
1196
- <pre class="brush: ruby; gutter: false; toolbar: false">
1197
- time_zone_select( &quot;user&quot;, &quot;time_zone&quot;)
1198
- </pre>
1199
- </div>
1200
- <h4 id="formtaghelper">8.12 FormTagHelper</h4>
1201
- <p>Provides a number of methods for creating form tags that doesn&#8217;t rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually.</p>
1202
- <h5 id="check_box_tag">8.12.1 check_box_tag</h5>
1203
- <p>Creates a check box form input tag.</p>
1204
- <div class="code_container">
1205
- <pre class="brush: ruby; gutter: false; toolbar: false">
1206
- check_box_tag 'accept'
1207
- # =&gt; &lt;input id=&quot;accept&quot; name=&quot;accept&quot; type=&quot;checkbox&quot; value=&quot;1&quot; /&gt;
1208
- </pre>
1209
- </div>
1210
- <h5 id="field_set_tag">8.12.2 field_set_tag</h5>
1211
- <p>Creates a field set for grouping <span class="caps">HTML</span> form elements.</p>
1212
- <div class="code_container">
1213
- <pre class="brush: ruby; gutter: false; toolbar: false">
1214
- &lt;%= field_set_tag do %&gt;
1215
- &lt;p&gt;&lt;%= text_field_tag 'name' %&gt;&lt;/p&gt;
1216
- &lt;% end %&gt;
1217
- # =&gt; &lt;fieldset&gt;&lt;p&gt;&lt;input id=&quot;name&quot; name=&quot;name&quot; type=&quot;text&quot; /&gt;&lt;/p&gt;&lt;/fieldset&gt;
1218
- </pre>
1219
- </div>
1220
- <h5 id="file_field_tag">8.12.3 file_field_tag</h5>
1221
- <p>Creates a file upload field.</p>
1222
- <p>Prior to Rails 3.1, if you are using file uploads, then you will need to set the multipart option for the form tag. Rails 3.1+ does this automatically.</p>
1223
- <div class="code_container">
1224
- <pre class="brush: ruby; gutter: false; toolbar: false">
1225
- &lt;%= form_tag { :action =&gt; &quot;post&quot; }, { :multipart =&gt; true } do %&gt;
1226
- &lt;label for=&quot;file&quot;&gt;File to Upload&lt;/label&gt; &lt;%= file_field_tag &quot;file&quot; %&gt;
1227
- &lt;%= submit_tag %&gt;
1228
- &lt;% end %&gt;
1229
- </pre>
1230
- </div>
1231
- <p>Example output:</p>
1232
- <div class="code_container">
1233
- <pre class="brush: ruby; gutter: false; toolbar: false">
1234
- file_field_tag 'attachment'
1235
- # =&gt; &lt;input id=&quot;attachment&quot; name=&quot;attachment&quot; type=&quot;file&quot; /&gt;
1236
- </pre>
1237
- </div>
1238
- <h5 id="form_tag">8.12.4 form_tag</h5>
1239
- <p>Starts a form tag that points the action to an url configured with <tt>url_for_options</tt> just like <tt>ActionController::Base#url_for</tt>.</p>
1240
- <div class="code_container">
1241
- <pre class="brush: ruby; gutter: false; toolbar: false">
1242
- &lt;%= form_tag '/posts' do %&gt;
1243
- &lt;div&gt;&lt;%= submit_tag 'Save' %&gt;&lt;/div&gt;
1244
- &lt;% end %&gt;
1245
- # =&gt; &lt;form action=&quot;/posts&quot; method=&quot;post&quot;&gt;&lt;div&gt;&lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Save&quot; /&gt;&lt;/div&gt;&lt;/form&gt;
1246
- </pre>
1247
- </div>
1248
- <h5 id="hidden_field_tag">8.12.5 hidden_field_tag</h5>
1249
- <p>Creates a hidden form input field used to transmit data that would be lost due to HTTP&#8217;s statelessness or data that should be hidden from the user.</p>
1250
- <div class="code_container">
1251
- <pre class="brush: ruby; gutter: false; toolbar: false">
1252
- hidden_field_tag 'token', 'VUBJKB23UIVI1UU1VOBVI@'
1253
- # =&gt; &lt;input id=&quot;token&quot; name=&quot;token&quot; type=&quot;hidden&quot; value=&quot;VUBJKB23UIVI1UU1VOBVI@&quot; /&gt;
1254
- </pre>
1255
- </div>
1256
- <h5 id="image_submit_tag">8.12.6 image_submit_tag</h5>
1257
- <p>Displays an image which when clicked will submit the form.</p>
1258
- <div class="code_container">
1259
- <pre class="brush: ruby; gutter: false; toolbar: false">
1260
- image_submit_tag(&quot;login.png&quot;)
1261
- # =&gt; &lt;input src=&quot;/images/login.png&quot; type=&quot;image&quot; /&gt;
1262
- </pre>
1263
- </div>
1264
- <h5 id="label_tag">8.12.7 label_tag</h5>
1265
- <p>Creates a label field.</p>
1266
- <div class="code_container">
1267
- <pre class="brush: ruby; gutter: false; toolbar: false">
1268
- label_tag 'name'
1269
- # =&gt; &lt;label for=&quot;name&quot;&gt;Name&lt;/label&gt;
1270
- </pre>
1271
- </div>
1272
- <h5 id="password_field_tag">8.12.8 password_field_tag</h5>
1273
- <p>Creates a password field, a masked text field that will hide the users input behind a mask character.</p>
1274
- <div class="code_container">
1275
- <pre class="brush: ruby; gutter: false; toolbar: false">
1276
- password_field_tag 'pass'
1277
- # =&gt; &lt;input id=&quot;pass&quot; name=&quot;pass&quot; type=&quot;password&quot; /&gt;
1278
- </pre>
1279
- </div>
1280
- <h5 id="radio_button_tag">8.12.9 radio_button_tag</h5>
1281
- <p>Creates a radio button; use groups of radio buttons named the same to allow users to select from a group of options.</p>
1282
- <div class="code_container">
1283
- <pre class="brush: ruby; gutter: false; toolbar: false">
1284
- radio_button_tag 'gender', 'male'
1285
- # =&gt; &lt;input id=&quot;gender_male&quot; name=&quot;gender&quot; type=&quot;radio&quot; value=&quot;male&quot; /&gt;
1286
- </pre>
1287
- </div>
1288
- <h5 id="select_tag">8.12.10 select_tag</h5>
1289
- <p>Creates a dropdown selection box.</p>
1290
- <div class="code_container">
1291
- <pre class="brush: ruby; gutter: false; toolbar: false">
1292
- select_tag &quot;people&quot;, &quot;&lt;option&gt;David&lt;/option&gt;&quot;
1293
- # =&gt; &lt;select id=&quot;people&quot; name=&quot;people&quot;&gt;&lt;option&gt;David&lt;/option&gt;&lt;/select&gt;
1294
- </pre>
1295
- </div>
1296
- <h5 id="submit_tag">8.12.11 submit_tag</h5>
1297
- <p>Creates a submit button with the text provided as the caption.</p>
1298
- <div class="code_container">
1299
- <pre class="brush: ruby; gutter: false; toolbar: false">
1300
- submit_tag &quot;Publish this post&quot;
1301
- # =&gt; &lt;input name=&quot;commit&quot; type=&quot;submit&quot; value=&quot;Publish this post&quot; /&gt;
1302
- </pre>
1303
- </div>
1304
- <h5 id="text_area_tag">8.12.12 text_area_tag</h5>
1305
- <p>Creates a text input area; use a textarea for longer text inputs such as blog posts or descriptions.</p>
1306
- <div class="code_container">
1307
- <pre class="brush: ruby; gutter: false; toolbar: false">
1308
- text_area_tag 'post'
1309
- # =&gt; &lt;textarea id=&quot;post&quot; name=&quot;post&quot;&gt;&lt;/textarea&gt;
1310
- </pre>
1311
- </div>
1312
- <h5 id="text_field_tag">8.12.13 text_field_tag</h5>
1313
- <p>Creates a standard text field; use these text fields to input smaller chunks of text like a username or a search query.</p>
1314
- <div class="code_container">
1315
- <pre class="brush: ruby; gutter: false; toolbar: false">
1316
- text_field_tag 'name'
1317
- # =&gt; &lt;input id=&quot;name&quot; name=&quot;name&quot; type=&quot;text&quot; /&gt;
1318
- </pre>
1319
- </div>
1320
- <h4 id="javascripthelper">8.13 JavaScriptHelper</h4>
1321
- <p>Provides functionality for working with JavaScript in your views.</p>
1322
- <h5 id="button_to_function">8.13.1 button_to_function</h5>
1323
- <p>Returns a button that&#8217;ll trigger a JavaScript function using the onclick handler. Examples:</p>
1324
- <div class="code_container">
1325
- <pre class="brush: ruby; gutter: false; toolbar: false">
1326
- button_to_function &quot;Greeting&quot;, &quot;alert('Hello world!')&quot;
1327
- button_to_function &quot;Delete&quot;, &quot;if (confirm('Really?')) do_delete()&quot;
1328
- button_to_function &quot;Details&quot; do |page|
1329
- page[:details].visual_effect :toggle_slide
1330
- end
1331
- </pre>
1332
- </div>
1333
- <h5 id="define_javascript_functions">8.13.2 define_javascript_functions</h5>
1334
- <p>Includes the Action Pack JavaScript libraries inside a single <tt>script</tt> tag.</p>
1335
- <h5 id="escape_javascript">8.13.3 escape_javascript</h5>
1336
- <p>Escape carrier returns and single and double quotes for JavaScript segments.</p>
1337
- <h5 id="javascript_tag">8.13.4 javascript_tag</h5>
1338
- <p>Returns a JavaScript tag wrapping the provided code.</p>
1339
- <div class="code_container">
1340
- <pre class="brush: ruby; gutter: false; toolbar: false">
1341
- javascript_tag &quot;alert('All is good')&quot;
1342
- </pre>
1343
- </div>
1344
- <div class="code_container">
1345
- <pre class="brush: xml; gutter: false; toolbar: false">
1346
- &lt;script type=&quot;text/javascript&quot;&gt;
1347
- //&lt;![CDATA[
1348
- alert('All is good')
1349
- //]]&gt;
1350
- &lt;/script&gt;
1351
- </pre>
1352
- </div>
1353
- <h5 id="link_to_function">8.13.5 link_to_function</h5>
1354
- <p>Returns a link that will trigger a JavaScript function using the onclick handler and return false after the fact.</p>
1355
- <div class="code_container">
1356
- <pre class="brush: ruby; gutter: false; toolbar: false">
1357
- link_to_function &quot;Greeting&quot;, &quot;alert('Hello world!')&quot;
1358
- # =&gt; &lt;a onclick=&quot;alert('Hello world!'); return false;&quot; href=&quot;#&quot;&gt;Greeting&lt;/a&gt;
1359
- </pre>
1360
- </div>
1361
- <h4 id="numberhelper">8.14 NumberHelper</h4>
1362
- <p>Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, and file size.</p>
1363
- <h5 id="number_to_currency">8.14.1 number_to_currency</h5>
1364
- <p>Formats a number into a currency string (e.g., $13.65).</p>
1365
- <div class="code_container">
1366
- <pre class="brush: ruby; gutter: false; toolbar: false">
1367
- number_to_currency(1234567890.50) # =&gt; $1,234,567,890.50
1368
- </pre>
1369
- </div>
1370
- <h5 id="number_to_human_size">8.14.2 number_to_human_size</h5>
1371
- <p>Formats the bytes in size into a more understandable representation; useful for reporting file sizes to users.</p>
1372
- <div class="code_container">
1373
- <pre class="brush: ruby; gutter: false; toolbar: false">
1374
- number_to_human_size(1234) # =&gt; 1.2 KB
1375
- number_to_human_size(1234567) # =&gt; 1.2 MB
1376
- </pre>
1377
- </div>
1378
- <h5 id="number_to_percentage">8.14.3 number_to_percentage</h5>
1379
- <p>Formats a number as a percentage string.</p>
1380
- <div class="code_container">
1381
- <pre class="brush: ruby; gutter: false; toolbar: false">
1382
- number_to_percentage(100, :precision =&gt; 0) # =&gt; 100%
1383
- </pre>
1384
- </div>
1385
- <h5 id="number_to_phone">8.14.4 number_to_phone</h5>
1386
- <p>Formats a number into a US phone number.</p>
1387
- <div class="code_container">
1388
- <pre class="brush: ruby; gutter: false; toolbar: false">
1389
- number_to_phone(1235551234) # =&gt; 123-555-1234
1390
- </pre>
1391
- </div>
1392
- <h5 id="number_with_delimiter">8.14.5 number_with_delimiter</h5>
1393
- <p>Formats a number with grouped thousands using a delimiter.</p>
1394
- <div class="code_container">
1395
- <pre class="brush: ruby; gutter: false; toolbar: false">
1396
- number_with_delimiter(12345678) # =&gt; 12,345,678
1397
- </pre>
1398
- </div>
1399
- <h5 id="number_with_precision">8.14.6 number_with_precision</h5>
1400
- <p>Formats a number with the specified level of <tt>precision</tt>, which defaults to 3.</p>
1401
- <div class="code_container">
1402
- <pre class="brush: ruby; gutter: false; toolbar: false">
1403
- number_with_precision(111.2345) # =&gt; 111.235
1404
- number_with_precision(111.2345, 2) # =&gt; 111.23
1405
- </pre>
1406
- </div>
1407
- <h3 id="localized-views">9 Localized Views</h3>
1408
- <p>Action View has the ability render different templates depending on the current locale.</p>
1409
- <p>For example, suppose you have a Posts controller with a show action. By default, calling this action will render <tt>app/views/posts/show.html.erb</tt>. But if you set <tt>I18n.locale = :de</tt>, then <tt>app/views/posts/show.de.html.erb</tt> will be rendered instead. If the localized template isn&#8217;t present, the undecorated version will be used. This means you&#8217;re not required to provide localized views for all cases, but they will be preferred and used if available.</p>
1410
- <p>You can use the same technique to localize the rescue files in your public directory. For example, setting <tt>I18n.locale = :de</tt> and creating <tt>public/500.de.html</tt> and <tt>public/404.de.html</tt> would allow you to have localized rescue pages.</p>
1411
- <p>Since Rails doesn&#8217;t restrict the symbols that you use to set I18n.locale, you can leverage this system to display different content depending on anything you like. For example, suppose you have some &#8220;expert&#8221; users that should see different pages from &#8220;normal&#8221; users. You could add the following to <tt>app/controllers/application.rb</tt>:</p>
1412
- <div class="code_container">
1413
- <pre class="brush: ruby; gutter: false; toolbar: false">
1414
- before_filter :set_expert_locale
1415
-
1416
- def set_expert_locale
1417
- I18n.locale = :expert if current_user.expert?
1418
- end
1419
- </pre>
1420
- </div>
1421
- <p>Then you could create special views like <tt>app/views/posts/show.expert.html.erb</tt> that would only be displayed to expert users.</p>
1422
- <p>You can read more about the Rails Internationalization (I18n) <span class="caps">API</span> <a href="i18n.html">here</a>.</p>
1423
-
1424
- <h3>Feedback</h3>
1425
- <p>
1426
- You're encouraged to help improve the quality of this guide.
1427
- </p>
1428
- <p>
1429
- If you see any typos or factual errors you are confident to
1430
- patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
1431
- and push the change yourself. That branch of Rails has public write access.
1432
- Commits are still reviewed, but that happens after you've submitted your
1433
- contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
1434
- cross-merged with master periodically.
1435
- </p>
1436
- <p>
1437
- You may also find incomplete content, or stuff that is not up to date.
1438
- Please do add any missing documentation for master. Check the
1439
- <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
1440
- for style and conventions.
1441
- </p>
1442
- <p>
1443
- If for whatever reason you spot something to fix but cannot patch it yourself, please
1444
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
1445
- </p>
1446
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
1447
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
1448
- </p>
1449
- </div>
1450
- </div>
1451
- </div>
1452
-
1453
- <hr class="hide" />
1454
- <div id="footer">
1455
- <div class="wrapper">
1456
- <p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
1457
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
1458
- </div>
1459
- </div>
1460
-
1461
- <script type="text/javascript" src="javascripts/guides.js"></script>
1462
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
1463
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
1464
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
1465
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
1466
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
1467
- <script type="text/javascript">
1468
- SyntaxHighlighter.all()
1469
- </script>
1470
- </body>
1471
- </html>