rails 4.2.0.beta3 → 4.2.0.beta4

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