rails 3.2.22.5 → 4.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rails might be problematic. Click here for more details.

Files changed (281) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +77 -0
  3. data/guides/CHANGELOG.md +9 -0
  4. data/guides/Rakefile +77 -0
  5. data/guides/assets/images/belongs_to.png +0 -0
  6. data/guides/assets/images/book_icon.gif +0 -0
  7. data/guides/assets/images/bullet.gif +0 -0
  8. data/guides/assets/images/challenge.png +0 -0
  9. data/guides/assets/images/chapters_icon.gif +0 -0
  10. data/guides/assets/images/check_bullet.gif +0 -0
  11. data/guides/assets/images/credits_pic_blank.gif +0 -0
  12. data/guides/assets/images/csrf.png +0 -0
  13. data/guides/assets/images/edge_badge.png +0 -0
  14. data/guides/assets/images/favicon.ico +0 -0
  15. data/guides/assets/images/feature_tile.gif +0 -0
  16. data/guides/assets/images/footer_tile.gif +0 -0
  17. data/guides/assets/images/fxn.png +0 -0
  18. data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
  19. data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  20. data/guides/assets/images/getting_started/form_with_errors.png +0 -0
  21. data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
  22. data/guides/assets/images/getting_started/new_post.png +0 -0
  23. data/guides/assets/images/getting_started/post_with_comments.png +0 -0
  24. data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
  25. data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
  26. data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
  27. data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
  28. data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
  29. data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
  30. data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
  31. data/guides/assets/images/grey_bullet.gif +0 -0
  32. data/guides/assets/images/habtm.png +0 -0
  33. data/guides/assets/images/has_many.png +0 -0
  34. data/guides/assets/images/has_many_through.png +0 -0
  35. data/guides/assets/images/has_one.png +0 -0
  36. data/guides/assets/images/has_one_through.png +0 -0
  37. data/guides/assets/images/header_backdrop.png +0 -0
  38. data/guides/assets/images/header_tile.gif +0 -0
  39. data/guides/assets/images/i18n/demo_html_safe.png +0 -0
  40. data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
  41. data/guides/assets/images/i18n/demo_translated_en.png +0 -0
  42. data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
  43. data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
  44. data/guides/assets/images/i18n/demo_untranslated.png +0 -0
  45. data/guides/assets/images/icons/README +5 -0
  46. data/guides/assets/images/icons/callouts/1.png +0 -0
  47. data/guides/assets/images/icons/callouts/10.png +0 -0
  48. data/guides/assets/images/icons/callouts/11.png +0 -0
  49. data/guides/assets/images/icons/callouts/12.png +0 -0
  50. data/guides/assets/images/icons/callouts/13.png +0 -0
  51. data/guides/assets/images/icons/callouts/14.png +0 -0
  52. data/guides/assets/images/icons/callouts/15.png +0 -0
  53. data/guides/assets/images/icons/callouts/2.png +0 -0
  54. data/guides/assets/images/icons/callouts/3.png +0 -0
  55. data/guides/assets/images/icons/callouts/4.png +0 -0
  56. data/guides/assets/images/icons/callouts/5.png +0 -0
  57. data/guides/assets/images/icons/callouts/6.png +0 -0
  58. data/guides/assets/images/icons/callouts/7.png +0 -0
  59. data/guides/assets/images/icons/callouts/8.png +0 -0
  60. data/guides/assets/images/icons/callouts/9.png +0 -0
  61. data/guides/assets/images/icons/caution.png +0 -0
  62. data/guides/assets/images/icons/example.png +0 -0
  63. data/guides/assets/images/icons/home.png +0 -0
  64. data/guides/assets/images/icons/important.png +0 -0
  65. data/guides/assets/images/icons/next.png +0 -0
  66. data/guides/assets/images/icons/note.png +0 -0
  67. data/guides/assets/images/icons/prev.png +0 -0
  68. data/guides/assets/images/icons/tip.png +0 -0
  69. data/guides/assets/images/icons/up.png +0 -0
  70. data/guides/assets/images/icons/warning.png +0 -0
  71. data/guides/assets/images/jaimeiniesta.jpg +0 -0
  72. data/guides/assets/images/nav_arrow.gif +0 -0
  73. data/guides/assets/images/oscardelben.jpg +0 -0
  74. data/guides/assets/images/polymorphic.png +0 -0
  75. data/guides/assets/images/radar.png +0 -0
  76. data/guides/assets/images/rails4_features.png +0 -0
  77. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  78. data/guides/assets/images/rails_guides_logo.gif +0 -0
  79. data/guides/assets/images/rails_logo_remix.gif +0 -0
  80. data/guides/assets/images/rails_welcome.png +0 -0
  81. data/guides/assets/images/session_fixation.png +0 -0
  82. data/guides/assets/images/tab_grey.gif +0 -0
  83. data/guides/assets/images/tab_info.gif +0 -0
  84. data/guides/assets/images/tab_note.gif +0 -0
  85. data/guides/assets/images/tab_red.gif +0 -0
  86. data/guides/assets/images/tab_yellow.gif +0 -0
  87. data/guides/assets/images/tab_yellow.png +0 -0
  88. data/guides/assets/images/vijaydev.jpg +0 -0
  89. data/guides/assets/javascripts/guides.js +57 -0
  90. data/guides/assets/javascripts/jquery.min.js +4 -0
  91. data/guides/assets/javascripts/responsive-tables.js +43 -0
  92. data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  93. data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  94. data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  95. data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  96. data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  97. data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  98. data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  99. data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  100. data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  101. data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  102. data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  103. data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  104. data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  105. data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  106. data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  107. data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  108. data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  109. data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  110. data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  111. data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  112. data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  113. data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  114. data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  115. data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  116. data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  117. data/guides/assets/javascripts/syntaxhighlighter/shCore.js +17 -0
  118. data/guides/assets/stylesheets/fixes.css +16 -0
  119. data/guides/assets/stylesheets/kindle.css +11 -0
  120. data/guides/assets/stylesheets/main.css +709 -0
  121. data/guides/assets/stylesheets/print.css +52 -0
  122. data/guides/assets/stylesheets/reset.css +43 -0
  123. data/guides/assets/stylesheets/responsive-tables.css +50 -0
  124. data/guides/assets/stylesheets/style.css +13 -0
  125. data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +226 -0
  126. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  127. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  128. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  129. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  130. data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  131. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  132. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  133. data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  134. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  135. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  136. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  137. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  138. data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  139. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  140. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  141. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  142. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  143. data/guides/code/getting_started/Gemfile +38 -0
  144. data/guides/code/getting_started/Gemfile.lock +150 -0
  145. data/guides/code/getting_started/README.rdoc +28 -0
  146. data/guides/code/getting_started/Rakefile +6 -0
  147. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  148. data/guides/code/getting_started/app/assets/javascripts/application.js +16 -0
  149. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
  150. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
  151. data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +3 -0
  152. data/guides/code/getting_started/app/assets/stylesheets/application.css +13 -0
  153. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
  154. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
  155. data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +3 -0
  156. data/guides/code/getting_started/app/controllers/application_controller.rb +5 -0
  157. data/guides/code/getting_started/app/controllers/comments_controller.rb +17 -0
  158. data/guides/code/getting_started/app/controllers/posts_controller.rb +47 -0
  159. data/guides/code/getting_started/app/controllers/welcome_controller.rb +4 -0
  160. data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
  161. data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
  162. data/guides/code/getting_started/app/helpers/posts_helper.rb +2 -0
  163. data/guides/code/getting_started/app/helpers/welcome_helper.rb +2 -0
  164. data/guides/code/getting_started/app/models/comment.rb +3 -0
  165. data/guides/code/getting_started/app/models/post.rb +7 -0
  166. data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
  167. data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
  168. data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
  169. data/guides/code/getting_started/app/views/posts/_form.html.erb +27 -0
  170. data/guides/code/getting_started/app/views/posts/edit.html.erb +5 -0
  171. data/guides/code/getting_started/app/views/posts/index.html.erb +21 -0
  172. data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
  173. data/guides/code/getting_started/app/views/posts/show.html.erb +18 -0
  174. data/guides/code/getting_started/app/views/welcome/index.html.erb +3 -0
  175. data/guides/code/getting_started/bin/bundle +4 -0
  176. data/guides/code/getting_started/bin/rails +4 -0
  177. data/guides/code/getting_started/bin/rake +4 -0
  178. data/guides/code/getting_started/config.ru +4 -0
  179. data/guides/code/getting_started/config/application.rb +17 -0
  180. data/guides/code/getting_started/config/boot.rb +4 -0
  181. data/guides/code/getting_started/config/database.yml +25 -0
  182. data/guides/code/getting_started/config/environment.rb +5 -0
  183. data/guides/code/getting_started/config/environments/development.rb +30 -0
  184. data/guides/code/getting_started/config/environments/production.rb +80 -0
  185. data/guides/code/getting_started/config/environments/test.rb +36 -0
  186. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
  187. data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +4 -0
  188. data/guides/code/getting_started/config/initializers/inflections.rb +16 -0
  189. data/guides/code/getting_started/config/initializers/locale.rb +9 -0
  190. data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
  191. data/guides/code/getting_started/config/initializers/secret_token.rb +12 -0
  192. data/guides/code/getting_started/config/initializers/session_store.rb +3 -0
  193. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
  194. data/guides/code/getting_started/config/locales/en.yml +23 -0
  195. data/guides/code/getting_started/config/routes.rb +7 -0
  196. data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +10 -0
  197. data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +11 -0
  198. data/guides/code/getting_started/db/schema.rb +33 -0
  199. data/guides/code/getting_started/db/seeds.rb +7 -0
  200. data/guides/code/getting_started/public/404.html +27 -0
  201. data/guides/code/getting_started/public/422.html +26 -0
  202. data/guides/code/getting_started/public/500.html +26 -0
  203. data/guides/code/getting_started/public/favicon.ico +0 -0
  204. data/guides/code/getting_started/public/robots.txt +5 -0
  205. data/guides/code/getting_started/test/controllers/comments_controller_test.rb +7 -0
  206. data/guides/code/getting_started/test/controllers/posts_controller_test.rb +7 -0
  207. data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +9 -0
  208. data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
  209. data/guides/code/getting_started/test/fixtures/posts.yml +9 -0
  210. data/guides/code/getting_started/test/helpers/comments_helper_test.rb +4 -0
  211. data/guides/code/getting_started/test/helpers/posts_helper_test.rb +4 -0
  212. data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +4 -0
  213. data/guides/code/getting_started/test/models/comment_test.rb +7 -0
  214. data/guides/code/getting_started/test/models/post_test.rb +7 -0
  215. data/guides/code/getting_started/test/test_helper.rb +15 -0
  216. data/guides/rails_guides.rb +44 -0
  217. data/guides/rails_guides/generator.rb +248 -0
  218. data/guides/rails_guides/helpers.rb +51 -0
  219. data/guides/rails_guides/indexer.rb +68 -0
  220. data/guides/rails_guides/kindle.rb +119 -0
  221. data/guides/rails_guides/levenshtein.rb +31 -0
  222. data/guides/rails_guides/markdown.rb +163 -0
  223. data/guides/rails_guides/markdown/renderer.rb +82 -0
  224. data/guides/source/2_2_release_notes.md +435 -0
  225. data/guides/source/2_3_release_notes.md +621 -0
  226. data/guides/source/3_0_release_notes.md +614 -0
  227. data/guides/source/3_1_release_notes.md +556 -0
  228. data/guides/source/3_2_release_notes.md +565 -0
  229. data/guides/source/4_0_release_notes.md +228 -0
  230. data/guides/source/_license.html.erb +2 -0
  231. data/guides/source/_welcome.html.erb +19 -0
  232. data/guides/source/action_controller_overview.md +872 -0
  233. data/guides/source/action_mailer_basics.md +599 -0
  234. data/guides/source/action_view_overview.md +1565 -0
  235. data/guides/source/active_model_basics.md +200 -0
  236. data/guides/source/active_record_basics.md +370 -0
  237. data/guides/source/active_record_callbacks.md +358 -0
  238. data/guides/source/active_record_querying.md +1621 -0
  239. data/guides/source/active_record_validations.md +1128 -0
  240. data/guides/source/active_support_core_extensions.md +3791 -0
  241. data/guides/source/active_support_instrumentation.md +487 -0
  242. data/guides/source/api_documentation_guidelines.md +209 -0
  243. data/guides/source/asset_pipeline.md +832 -0
  244. data/guides/source/association_basics.md +2129 -0
  245. data/guides/source/caching_with_rails.md +350 -0
  246. data/guides/source/command_line.md +594 -0
  247. data/guides/source/configuring.md +736 -0
  248. data/guides/source/contributing_to_ruby_on_rails.md +455 -0
  249. data/guides/source/credits.html.erb +76 -0
  250. data/guides/source/debugging_rails_applications.md +675 -0
  251. data/guides/source/development_dependencies_install.md +195 -0
  252. data/guides/source/documents.yaml +179 -0
  253. data/guides/source/engines.md +961 -0
  254. data/guides/source/form_helpers.md +955 -0
  255. data/guides/source/generators.md +644 -0
  256. data/guides/source/getting_started.md +1775 -0
  257. data/guides/source/i18n.md +983 -0
  258. data/guides/source/index.html.erb +27 -0
  259. data/guides/source/initialization.md +562 -0
  260. data/guides/source/kindle/KINDLE.md +26 -0
  261. data/guides/source/kindle/copyright.html.erb +1 -0
  262. data/guides/source/kindle/layout.html.erb +27 -0
  263. data/guides/source/kindle/rails_guides.opf.erb +52 -0
  264. data/guides/source/kindle/toc.html.erb +24 -0
  265. data/guides/source/kindle/toc.ncx.erb +64 -0
  266. data/guides/source/kindle/welcome.html.erb +5 -0
  267. data/guides/source/layout.html.erb +148 -0
  268. data/guides/source/layouts_and_rendering.md +1132 -0
  269. data/guides/source/migrations.md +1059 -0
  270. data/guides/source/nested_model_forms.md +225 -0
  271. data/guides/source/plugins.md +435 -0
  272. data/guides/source/rails_application_templates.md +229 -0
  273. data/guides/source/rails_on_rack.md +342 -0
  274. data/guides/source/routing.md +1088 -0
  275. data/guides/source/ruby_on_rails_guides_guidelines.md +124 -0
  276. data/guides/source/security.md +973 -0
  277. data/guides/source/testing.md +981 -0
  278. data/guides/source/upgrading_ruby_on_rails.md +286 -0
  279. data/guides/source/working_with_javascript_in_rails.md +396 -0
  280. data/guides/w3c_validator.rb +95 -0
  281. metadata +315 -31
@@ -0,0 +1,1565 @@
1
+ Action View Overview
2
+ ====================
3
+
4
+ After reading this guide, you will know:
5
+
6
+ * What Action View is and how to use it with Rails.
7
+ * How best to use templates, partials, and layouts.
8
+ * What helpers are provided by Action View and how to make your own.
9
+ * How to use localized views.
10
+ * How to use Action View outside of Rails.
11
+
12
+ --------------------------------------------------------------------------------
13
+
14
+ What is Action View?
15
+ --------------------
16
+
17
+ 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.
18
+
19
+ 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.
20
+
21
+ NOTE: 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.
22
+
23
+ Using Action View with Rails
24
+ ----------------------------
25
+
26
+ For each controller there is an associated directory in the `app/views` 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.
27
+
28
+ Let's take a look at what Rails does by default when creating a new resource using the scaffold generator:
29
+
30
+ ```bash
31
+ $ rails generate scaffold post
32
+ [...]
33
+ invoke scaffold_controller
34
+ create app/controllers/posts_controller.rb
35
+ invoke erb
36
+ create app/views/posts
37
+ create app/views/posts/index.html.erb
38
+ create app/views/posts/edit.html.erb
39
+ create app/views/posts/show.html.erb
40
+ create app/views/posts/new.html.erb
41
+ create app/views/posts/_form.html.erb
42
+ [...]
43
+ ```
44
+
45
+ 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.
46
+ For example, the index controller action of the `posts_controller.rb` will use the `index.html.erb` view file in the `app/views/posts` directory.
47
+ 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.
48
+
49
+
50
+ Templates, Partials and Layouts
51
+ -------------------------------
52
+
53
+ As mentioned before, the final HTML output is a composition of three Rails elements: `Templates`, `Partials` and `Layouts`.
54
+ Below is a brief overview of each one of them.
55
+
56
+ ### Templates
57
+
58
+ Action View templates can be written in several ways. If the template file has a `.erb` extension then it uses a mixture of ERB (included in Ruby) and HTML. If the template file has a `.builder` extension then a fresh instance of `Builder::XmlMarkup` library is used.
59
+
60
+ 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 `.html.erb` as a file extension.
61
+
62
+ #### ERB
63
+
64
+ Within an ERB template, Ruby code can be included using both `<% %>` and `<%= %>` tags. The `<% %>` tags are used to execute Ruby code that does not return anything, such as conditions, loops or blocks, and the `<%= %>` tags are used when you want output.
65
+
66
+ Consider the following loop for names:
67
+
68
+ ```html+erb
69
+ <h1>Names of all the people</h1>
70
+ <% @people.each do |person| %>
71
+ Name: <%= person.name %><br/>
72
+ <% end %>
73
+ ```
74
+
75
+ The loop is set up in regular embedding tags (`<% %>`) and the name is written using the output embedding tags (`<%= %>`). Note that this is not just a usage suggestion, for regular output functions like `print` or `puts` won't work with ERB templates. So this would be wrong:
76
+
77
+ ```html+erb
78
+ <%# WRONG %>
79
+ Hi, Mr. <% puts "Frodo" %>
80
+ ```
81
+
82
+ To suppress leading and trailing whitespaces, you can use `<%-` `-%>` interchangeably with `<%` and `%>`.
83
+
84
+ #### Builder
85
+
86
+ Builder templates are a more programmatic alternative to ERB. They are especially useful for generating XML content. An XmlMarkup object named `xml` is automatically made available to templates with a `.builder` extension.
87
+
88
+ Here are some basic examples:
89
+
90
+ ```ruby
91
+ xml.em("emphasized")
92
+ xml.em { xml.b("emph & bold") }
93
+ xml.a("A Link", "href" => "http://rubyonrails.org")
94
+ xml.target("name" => "compile", "option" => "fast")
95
+ ```
96
+
97
+ which would produce:
98
+
99
+ ```html
100
+ <em>emphasized</em>
101
+ <em><b>emph &amp; bold</b></em>
102
+ <a href="http://rubyonrails.org">A link</a>
103
+ <target option="fast" name="compile" />
104
+ ```
105
+
106
+ Any method with a block will be treated as an XML markup tag with nested markup in the block. For example, the following:
107
+
108
+ ```ruby
109
+ xml.div {
110
+ xml.h1(@person.name)
111
+ xml.p(@person.bio)
112
+ }
113
+ ```
114
+
115
+ would produce something like:
116
+
117
+ ```html
118
+ <div>
119
+ <h1>David Heinemeier Hansson</h1>
120
+ <p>A product of Danish Design during the Winter of '79...</p>
121
+ </div>
122
+ ```
123
+
124
+ Below is a full-length RSS example actually used on Basecamp:
125
+
126
+ ```ruby
127
+ xml.rss("version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/") do
128
+ xml.channel do
129
+ xml.title(@feed_title)
130
+ xml.link(@url)
131
+ xml.description "Basecamp: Recent items"
132
+ xml.language "en-us"
133
+ xml.ttl "40"
134
+
135
+ for item in @recent_items
136
+ xml.item do
137
+ xml.title(item_title(item))
138
+ xml.description(item_description(item)) if item_description(item)
139
+ xml.pubDate(item_pubDate(item))
140
+ xml.guid(@person.firm.account.url + @recent_items.url(item))
141
+ xml.link(@person.firm.account.url + @recent_items.url(item))
142
+ xml.tag!("dc:creator", item.author_name) if item_has_creator?(item)
143
+ end
144
+ end
145
+ end
146
+ end
147
+ ```
148
+
149
+ #### Template Caching
150
+
151
+ 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.
152
+
153
+ ### Partials
154
+
155
+ 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.
156
+
157
+ #### Naming Partials
158
+
159
+ To render a partial as part of a view, you use the `render` method within the view:
160
+
161
+ ```erb
162
+ <%= render "menu" %>
163
+ ```
164
+
165
+ This will render a file named `_menu.html.erb` 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:
166
+
167
+ ```erb
168
+ <%= render "shared/menu" %>
169
+ ```
170
+
171
+ That code will pull in the partial from `app/views/shared/_menu.html.erb`.
172
+
173
+ #### Using Partials to simplify Views
174
+
175
+ One way to use partials is to treat them as the equivalent of subroutines: as a way to move details out of a view so that you can grasp what's going on more easily. For example, you might have a view that looked like this:
176
+
177
+ ```html+erb
178
+ <%= render "shared/ad_banner" %>
179
+
180
+ <h1>Products</h1>
181
+
182
+ <p>Here are a few of our fine products:</p>
183
+ <% @products.each do |product| %>
184
+ <%= render partial: "product", locals: {product: product} %>
185
+ <% end %>
186
+
187
+ <%= render "shared/footer" %>
188
+ ```
189
+
190
+ Here, the `_ad_banner.html.erb` and `_footer.html.erb` partials could contain content that is shared among many pages in your application. You don't need to see the details of these sections when you're concentrating on a particular page.
191
+
192
+ #### The `as` and `object` options
193
+
194
+ By default `ActionView::Partials::PartialRenderer` has its object in a local variable with the same name as the template. So, given:
195
+
196
+ ```erb
197
+ <%= render partial: "product" %>
198
+ ```
199
+
200
+ within product we'll get `@product` in the local variable `product`, as if we had written:
201
+
202
+ ```erb
203
+ <%= render partial: "product", locals: {product: @product} %>
204
+ ```
205
+
206
+ With the `as` option we can specify a different name for the local variable. For example, if we wanted it to be `item` instead of `product` we would do:
207
+
208
+ ```erb
209
+ <%= render partial: "product", as: "item" %>
210
+ ```
211
+
212
+ The `object` 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).
213
+
214
+ For example, instead of:
215
+
216
+ ```erb
217
+ <%= render partial: "product", locals: {product: @item} %>
218
+ ```
219
+
220
+ we would do:
221
+
222
+ ```erb
223
+ <%= render partial: "product", object: @item %>
224
+ ```
225
+
226
+ The `object` and `as` options can also be used together:
227
+
228
+ ```erb
229
+ <%= render partial: "product", object: @item, as: "item" %>
230
+ ```
231
+
232
+ #### Rendering Collections
233
+
234
+ 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.
235
+
236
+ So this example for rendering all the products:
237
+
238
+ ```erb
239
+ <% @products.each do |product| %>
240
+ <%= render partial: "product", locals: { product: product } %>
241
+ <% end %>
242
+ ```
243
+
244
+ can be rewritten in a single line:
245
+
246
+ ```erb
247
+ <%= render partial: "product", collection: @products %>
248
+ ```
249
+
250
+ 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 `_product`, and within it you can refer to `product` to get the instance that is being rendered.
251
+
252
+ You can use a shorthand syntax for rendering collections. Assuming `@products` is a collection of `Product` instances, you can simply write the following to produce the same result:
253
+
254
+ ```erb
255
+ <%= render @products %>
256
+ ```
257
+
258
+ Rails determines the name of the partial to use by looking at the model name in the collection, `Product` 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.
259
+
260
+ #### Spacer Templates
261
+
262
+ You can also specify a second partial to be rendered between instances of the main partial by using the `:spacer_template` option:
263
+
264
+ ```erb
265
+ <%= render @products, spacer_template: "product_ruler" %>
266
+ ```
267
+
268
+ Rails will render the `_product_ruler` partial (with no data passed to it) between each pair of `_product` partials.
269
+
270
+ ### Layouts
271
+
272
+ TODO...
273
+
274
+ Using Templates, Partials and Layouts "The Rails Way"
275
+ --------------------------------------------------------
276
+
277
+ TODO...
278
+
279
+ Partial Layouts
280
+ ---------------
281
+
282
+ 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.
283
+
284
+ Let's say we're displaying a post on a page, that should be wrapped in a `div` for display purposes. First, we'll create a new `Post`:
285
+
286
+ ```ruby
287
+ Post.create(body: 'Partial Layouts are cool!')
288
+ ```
289
+
290
+ In the `show` template, we'll render the `_post` partial wrapped in the `box` layout:
291
+
292
+ **posts/show.html.erb**
293
+
294
+ ```erb
295
+ <%= render partial: 'post', layout: 'box', locals: {post: @post} %>
296
+ ```
297
+
298
+ The `box` layout simply wraps the `_post` partial in a `div`:
299
+
300
+ **posts/_box.html.erb**
301
+
302
+ ```html+erb
303
+ <div class='box'>
304
+ <%= yield %>
305
+ </div>
306
+ ```
307
+
308
+ The `_post` partial wraps the post's `body` in a `div` with the `id` of the post using the `div_for` helper:
309
+
310
+ **posts/_post.html.erb**
311
+
312
+ ```html+erb
313
+ <%= div_for(post) do %>
314
+ <p><%= post.body %></p>
315
+ <% end %>
316
+ ```
317
+
318
+ this would output the following:
319
+
320
+ ```html
321
+ <div class='box'>
322
+ <div id='post_1'>
323
+ <p>Partial Layouts are cool!</p>
324
+ </div>
325
+ </div>
326
+ ```
327
+
328
+ Note that the partial layout has access to the local `post` variable that was passed into the `render` call. However, unlike application-wide layouts, partial layouts still have the underscore prefix.
329
+
330
+ You can also render a block of code within a partial layout instead of calling `yield`. For example, if we didn't have the `_post` partial, we could do this instead:
331
+
332
+ **posts/show.html.erb**
333
+
334
+ ```html+erb
335
+ <% render(layout: 'box', locals: {post: @post}) do %>
336
+ <%= div_for(post) do %>
337
+ <p><%= post.body %></p>
338
+ <% end %>
339
+ <% end %>
340
+ ```
341
+
342
+ Supposing we use the same `_box` partial from above, this would produce the same output as the previous example.
343
+
344
+ View Paths
345
+ ----------
346
+
347
+ TODO...
348
+
349
+ Overview of helpers provided by Action View
350
+ -------------------------------------------
351
+
352
+ WIP: Not all the helpers are listed here. For a full list see the [API documentation](http://api.rubyonrails.org/classes/ActionView/Helpers.html)
353
+
354
+ The following is only a brief overview summary of the helpers available in Action View. It's recommended that you review the [API Documentation](http://api.rubyonrails.org/classes/ActionView/Helpers.html), which covers all of the helpers in more detail, but this should serve as a good starting point.
355
+
356
+ ### RecordTagHelper
357
+
358
+ This module provides methods for generating container tags, such as `div`, 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.
359
+
360
+ #### content_tag_for
361
+
362
+ Renders a container tag that relates to your Active Record Object.
363
+
364
+ For example, given `@post` is the object of `Post` class, you can do:
365
+
366
+ ```html+erb
367
+ <%= content_tag_for(:tr, @post) do %>
368
+ <td><%= @post.title %></td>
369
+ <% end %>
370
+ ```
371
+
372
+ This will generate this HTML output:
373
+
374
+ ```html
375
+ <tr id="post_1234" class="post">
376
+ <td>Hello World!</td>
377
+ </tr>
378
+ ```
379
+
380
+ You can also supply HTML attributes as an additional option hash. For example:
381
+
382
+ ```html+erb
383
+ <%= content_tag_for(:tr, @post, class: "frontpage") do %>
384
+ <td><%= @post.title %></td>
385
+ <% end %>
386
+ ```
387
+
388
+ Will generate this HTML output:
389
+
390
+ ```html
391
+ <tr id="post_1234" class="post frontpage">
392
+ <td>Hello World!</td>
393
+ </tr>
394
+ ```
395
+
396
+ 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 `@posts` is an array of two `Post` objects:
397
+
398
+ ```html+erb
399
+ <%= content_tag_for(:tr, @posts) do |post| %>
400
+ <td><%= post.title %></td>
401
+ <% end %>
402
+ ```
403
+
404
+ Will generate this HTML output:
405
+
406
+ ```html
407
+ <tr id="post_1234" class="post">
408
+ <td>Hello World!</td>
409
+ </tr>
410
+ <tr id="post_1235" class="post">
411
+ <td>Ruby on Rails Rocks!</td>
412
+ </tr>
413
+ ```
414
+
415
+ #### div_for
416
+
417
+ This is actually a convenient method which calls `content_tag_for` internally with `:div` as the tag name. You can pass either an Active Record object or a collection of objects. For example:
418
+
419
+ ```html+erb
420
+ <%= div_for(@post, class: "frontpage") do %>
421
+ <td><%= @post.title %></td>
422
+ <% end %>
423
+ ```
424
+
425
+ Will generate this HTML output:
426
+
427
+ ```html
428
+ <div id="post_1234" class="post frontpage">
429
+ <td>Hello World!</td>
430
+ </div>
431
+ ```
432
+
433
+ ### AssetTagHelper
434
+
435
+ This module provides methods for generating HTML that links views to assets such as images, JavaScript files, stylesheets, and feeds.
436
+
437
+ 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 `config.action_controller.asset_host` in the application configuration, typically in `config/environments/production.rb`. For example, let's say your asset host is `assets.example.com`:
438
+
439
+ ```ruby
440
+ config.action_controller.asset_host = "assets.example.com"
441
+ image_tag("rails.png") # => <img src="http://assets.example.com/images/rails.png" alt="Rails" />
442
+ ```
443
+
444
+ #### register_javascript_expansion
445
+
446
+ 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 `vendor/assets/javascripts`.
447
+
448
+ ```ruby
449
+ ActionView::Helpers::AssetTagHelper.register_javascript_expansion monkey: ["head", "body", "tail"]
450
+
451
+ javascript_include_tag :monkey # =>
452
+ <script src="/assets/head.js"></script>
453
+ <script src="/assets/body.js"></script>
454
+ <script src="/assets/tail.js"></script>
455
+ ```
456
+
457
+ #### register_stylesheet_expansion
458
+
459
+ Register one or more stylesheet files to be included when symbol is passed to `stylesheet_link_tag`. This method is typically intended to be called from plugin initialization to register stylesheet files that the plugin installed in `vendor/assets/stylesheets`.
460
+
461
+ ```ruby
462
+ ActionView::Helpers::AssetTagHelper.register_stylesheet_expansion monkey: ["head", "body", "tail"]
463
+
464
+ stylesheet_link_tag :monkey # =>
465
+ <link href="/assets/head.css" media="screen" rel="stylesheet" />
466
+ <link href="/assets/body.css" media="screen" rel="stylesheet" />
467
+ <link href="/assets/tail.css" media="screen" rel="stylesheet" />
468
+ ```
469
+
470
+ #### auto_discovery_link_tag
471
+
472
+ Returns a link tag that browsers and news readers can use to auto-detect an RSS or Atom feed.
473
+
474
+ ```ruby
475
+ auto_discovery_link_tag(:rss, "http://www.example.com/feed.rss", {title: "RSS Feed"}) # =>
476
+ <link rel="alternate" type="application/rss+xml" title="RSS Feed" href="http://www.example.com/feed" />
477
+ ```
478
+
479
+ #### image_path
480
+
481
+ Computes the path to an image asset in the `app/assets/images` directory. Full paths from the document root will be passed through. Used internally by `image_tag` to build the image path.
482
+
483
+ ```ruby
484
+ image_path("edit.png") # => /assets/edit.png
485
+ ```
486
+
487
+ Fingerprint will be added to the filename if config.assets.digest is set to true.
488
+
489
+ ```ruby
490
+ image_path("edit.png") # => /assets/edit-2d1a2db63fc738690021fedb5a65b68e.png
491
+ ```
492
+
493
+ #### image_url
494
+
495
+ Computes the url to an image asset in the `app/asset/images` directory. This will call `image_path` internally and merge with your current host or your asset host.
496
+
497
+ ```ruby
498
+ image_url("edit.png") # => http://www.example.com/assets/edit.png
499
+ ```
500
+
501
+ #### image_tag
502
+
503
+ Returns an html image tag for the source. The source can be a full path or a file that exists in your `app/assets/images` directory.
504
+
505
+ ```ruby
506
+ image_tag("icon.png") # => <img src="/assets/icon.png" alt="Icon" />
507
+ ```
508
+
509
+ #### javascript_include_tag
510
+
511
+ Returns an html script tag for each of the sources provided. You can pass in the filename (`.js` extension is optional) of JavaScript files that exist in your `app/assets/javascripts` directory for inclusion into the current page or you can pass the full path relative to your document root.
512
+
513
+ ```ruby
514
+ javascript_include_tag "common" # => <script src="/assets/common.js"></script>
515
+ ```
516
+
517
+ If the application does not use the asset pipeline, to include the jQuery JavaScript library in your application, pass `:defaults` as the source. When using `:defaults`, if an `application.js` file exists in your `app/assets/javascripts` directory, it will be included as well.
518
+
519
+ ```ruby
520
+ javascript_include_tag :defaults
521
+ ```
522
+
523
+ You can also include all JavaScript files in the `app/assets/javascripts` directory using `:all` as the source.
524
+
525
+ ```ruby
526
+ javascript_include_tag :all
527
+ ```
528
+
529
+ 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 `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).
530
+
531
+ ```ruby
532
+ javascript_include_tag :all, cache: true # =>
533
+ <script src="/javascripts/all.js"></script>
534
+ ```
535
+
536
+ #### javascript_path
537
+
538
+ Computes the path to a JavaScript asset in the `app/assets/javascripts` directory. If the source filename has no extension, `.js` will be appended. Full paths from the document root will be passed through. Used internally by `javascript_include_tag` to build the script path.
539
+
540
+ ```ruby
541
+ javascript_path "common" # => /assets/common.js
542
+ ```
543
+
544
+ #### javascript_url
545
+
546
+ Computes the url to a JavaScript asset in the `app/assets/javascripts` directory. This will call `javascript_path` internally and merge with your current host or your asset host.
547
+
548
+ ```ruby
549
+ javascript_url "common" # => http://www.example.com/assets/common.js
550
+ ```
551
+
552
+ #### stylesheet_link_tag
553
+
554
+ Returns a stylesheet link tag for the sources specified as arguments. If you don't specify an extension, `.css` will be appended automatically.
555
+
556
+ ```ruby
557
+ stylesheet_link_tag "application" # => <link href="/assets/application.css" media="screen" rel="stylesheet" />
558
+ ```
559
+
560
+ You can also include all styles in the stylesheet directory using :all as the source:
561
+
562
+ ```ruby
563
+ stylesheet_link_tag :all
564
+ ```
565
+
566
+ 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).
567
+
568
+ ```ruby
569
+ stylesheet_link_tag :all, cache: true
570
+ # => <link href="/assets/all.css" media="screen" rel="stylesheet" />
571
+ ```
572
+
573
+ #### stylesheet_path
574
+
575
+ Computes the path to a stylesheet asset in the `app/assets/stylesheets` 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.
576
+
577
+ ```ruby
578
+ stylesheet_path "application" # => /assets/application.css
579
+ ```
580
+
581
+ #### stylesheet_url
582
+
583
+ Computes the url to a stylesheet asset in the `app/assets/stylesheets` directory. This will call `stylesheet_path` internally and merge with your current host or your asset host.
584
+
585
+ ```ruby
586
+ stylesheet_url "application" # => http://www.example.com/assets/application.css
587
+ ```
588
+
589
+ ### AtomFeedHelper
590
+
591
+ #### atom_feed
592
+
593
+ This helper makes building an Atom feed easy. Here's a full usage example:
594
+
595
+ **config/routes.rb**
596
+
597
+ ```ruby
598
+ resources :posts
599
+ ```
600
+
601
+ **app/controllers/posts_controller.rb**
602
+
603
+ ```ruby
604
+ def index
605
+ @posts = Post.all
606
+
607
+ respond_to do |format|
608
+ format.html
609
+ format.atom
610
+ end
611
+ end
612
+ ```
613
+
614
+ **app/views/posts/index.atom.builder**
615
+
616
+ ```ruby
617
+ atom_feed do |feed|
618
+ feed.title("Posts Index")
619
+ feed.updated((@posts.first.created_at))
620
+
621
+ @posts.each do |post|
622
+ feed.entry(post) do |entry|
623
+ entry.title(post.title)
624
+ entry.content(post.body, type: 'html')
625
+
626
+ entry.author do |author|
627
+ author.name(post.author_name)
628
+ end
629
+ end
630
+ end
631
+ end
632
+ ```
633
+
634
+ ### BenchmarkHelper
635
+
636
+ #### benchmark
637
+
638
+ 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.
639
+
640
+ ```html+erb
641
+ <% benchmark "Process data files" do %>
642
+ <%= expensive_files_operation %>
643
+ <% end %>
644
+ ```
645
+
646
+ 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.
647
+
648
+ ### CacheHelper
649
+
650
+ #### cache
651
+
652
+ 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 `ActionController::Caching::Fragments` for more information.
653
+
654
+ ```erb
655
+ <% cache do %>
656
+ <%= render "shared/footer" %>
657
+ <% end %>
658
+ ```
659
+
660
+ ### CaptureHelper
661
+
662
+ #### capture
663
+
664
+ The `capture` method allows you to extract part of a template into a variable. You can then use this variable anywhere in your templates or layout.
665
+
666
+ ```html+erb
667
+ <% @greeting = capture do %>
668
+ <p>Welcome! The date and time is <%= Time.now %></p>
669
+ <% end %>
670
+ ```
671
+
672
+ The captured variable can then be used anywhere else.
673
+
674
+ ```html+erb
675
+ <html>
676
+ <head>
677
+ <title>Welcome!</title>
678
+ </head>
679
+ <body>
680
+ <%= @greeting %>
681
+ </body>
682
+ </html>
683
+ ```
684
+
685
+ #### content_for
686
+
687
+ Calling `content_for` 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 `yield`.
688
+
689
+ 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 `content_for` to include this JavaScript on our special page without fattening up the rest of the site.
690
+
691
+ **app/views/layouts/application.html.erb**
692
+
693
+ ```html+erb
694
+ <html>
695
+ <head>
696
+ <title>Welcome!</title>
697
+ <%= yield :special_script %>
698
+ </head>
699
+ <body>
700
+ <p>Welcome! The date and time is <%= Time.now %></p>
701
+ </body>
702
+ </html>
703
+ ```
704
+
705
+ **app/views/posts/special.html.erb**
706
+
707
+ ```html+erb
708
+ <p>This is a special page.</p>
709
+
710
+ <% content_for :special_script do %>
711
+ <script>alert('Hello!')</script>
712
+ <% end %>
713
+ ```
714
+
715
+ ### DateHelper
716
+
717
+ #### date_select
718
+
719
+ Returns a set of select tags (one for year, month, and day) pre-selected for accessing a specified date-based attribute.
720
+
721
+ ```ruby
722
+ date_select("post", "published_on")
723
+ ```
724
+
725
+ #### datetime_select
726
+
727
+ Returns a set of select tags (one for year, month, day, hour, and minute) pre-selected for accessing a specified datetime-based attribute.
728
+
729
+ ```ruby
730
+ datetime_select("post", "published_on")
731
+ ```
732
+
733
+ #### distance_of_time_in_words
734
+
735
+ Reports the approximate distance in time between two Time or Date objects or integers as seconds. Set `include_seconds` to true if you want more detailed approximations.
736
+
737
+ ```ruby
738
+ distance_of_time_in_words(Time.now, Time.now + 15.seconds) # => less than a minute
739
+ distance_of_time_in_words(Time.now, Time.now + 15.seconds, include_seconds: true) # => less than 20 seconds
740
+ ```
741
+
742
+ #### select_date
743
+
744
+ Returns a set of html select-tags (one for year, month, and day) pre-selected with the `date` provided.
745
+
746
+ ```ruby
747
+ # Generates a date select that defaults to the date provided (six days after today)
748
+ select_date(Time.today + 6.days)
749
+
750
+ # Generates a date select that defaults to today (no specified date)
751
+ select_date()
752
+ ```
753
+
754
+ #### select_datetime
755
+
756
+ Returns a set of html select-tags (one for year, month, day, hour, and minute) pre-selected with the `datetime` provided.
757
+
758
+ ```ruby
759
+ # Generates a datetime select that defaults to the datetime provided (four days after today)
760
+ select_datetime(Time.now + 4.days)
761
+
762
+ # Generates a datetime select that defaults to today (no specified datetime)
763
+ select_datetime()
764
+ ```
765
+
766
+ #### select_day
767
+
768
+ Returns a select tag with options for each of the days 1 through 31 with the current day selected.
769
+
770
+ ```ruby
771
+ # Generates a select field for days that defaults to the day for the date provided
772
+ select_day(Time.today + 2.days)
773
+
774
+ # Generates a select field for days that defaults to the number given
775
+ select_day(5)
776
+ ```
777
+
778
+ #### select_hour
779
+
780
+ Returns a select tag with options for each of the hours 0 through 23 with the current hour selected.
781
+
782
+ ```ruby
783
+ # Generates a select field for minutes that defaults to the minutes for the time provided
784
+ select_minute(Time.now + 6.hours)
785
+ ```
786
+
787
+ #### select_minute
788
+
789
+ Returns a select tag with options for each of the minutes 0 through 59 with the current minute selected.
790
+
791
+ ```ruby
792
+ # Generates a select field for minutes that defaults to the minutes for the time provided.
793
+ select_minute(Time.now + 6.hours)
794
+ ```
795
+
796
+ #### select_month
797
+
798
+ Returns a select tag with options for each of the months January through December with the current month selected.
799
+
800
+ ```ruby
801
+ # Generates a select field for months that defaults to the current month
802
+ select_month(Date.today)
803
+ ```
804
+
805
+ #### select_second
806
+
807
+ Returns a select tag with options for each of the seconds 0 through 59 with the current second selected.
808
+
809
+ ```ruby
810
+ # Generates a select field for seconds that defaults to the seconds for the time provided
811
+ select_second(Time.now + 16.minutes)
812
+ ```
813
+
814
+ #### select_time
815
+
816
+ Returns a set of html select-tags (one for hour and minute).
817
+
818
+ ```ruby
819
+ # Generates a time select that defaults to the time provided
820
+ select_time(Time.now)
821
+ ```
822
+
823
+ #### select_year
824
+
825
+ 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 `:start_year` and `:end_year` keys in the `options`.
826
+
827
+ ```ruby
828
+ # Generates a select field for five years on either side of Date.today that defaults to the current year
829
+ select_year(Date.today)
830
+
831
+ # Generates a select field from 1900 to 2009 that defaults to the current year
832
+ select_year(Date.today, start_year: 1900, end_year: 2009)
833
+ ```
834
+
835
+ #### time_ago_in_words
836
+
837
+ Like `distance_of_time_in_words`, but where `to_time` is fixed to `Time.now`.
838
+
839
+ ```ruby
840
+ time_ago_in_words(3.minutes.from_now) # => 3 minutes
841
+ ```
842
+
843
+ #### time_select
844
+
845
+ 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.
846
+
847
+ ```ruby
848
+ # Creates a time select tag that, when POSTed, will be stored in the order variable in the submitted attribute
849
+ time_select("order", "submitted")
850
+ ```
851
+
852
+ ### DebugHelper
853
+
854
+ Returns a `pre` tag that has object dumped by YAML. This creates a very readable way to inspect an object.
855
+
856
+ ```ruby
857
+ my_hash = {'first' => 1, 'second' => 'two', 'third' => [1,2,3]}
858
+ debug(my_hash)
859
+ ```
860
+
861
+ ```html
862
+ <pre class='debug_dump'>---
863
+ first: 1
864
+ second: two
865
+ third:
866
+ - 1
867
+ - 2
868
+ - 3
869
+ </pre>
870
+ ```
871
+
872
+ ### FormHelper
873
+
874
+ 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.
875
+
876
+ 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.
877
+
878
+ 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:
879
+
880
+ ```html+erb
881
+ # Note: a @person variable will have been created in the controller (e.g. @person = Person.new)
882
+ <%= form_for @person, url: {action: "create"} do |f| %>
883
+ <%= f.text_field :first_name %>
884
+ <%= f.text_field :last_name %>
885
+ <%= submit_tag 'Create' %>
886
+ <% end %>
887
+ ```
888
+
889
+ The HTML generated for this would be:
890
+
891
+ ```html
892
+ <form action="/people/create" method="post">
893
+ <input id="person_first_name" name="person[first_name]" type="text" />
894
+ <input id="person_last_name" name="person[last_name]" type="text" />
895
+ <input name="commit" type="submit" value="Create" />
896
+ </form>
897
+ ```
898
+
899
+ The params object created when this form is submitted would look like:
900
+
901
+ ```ruby
902
+ {"action" => "create", "controller" => "people", "person" => {"first_name" => "William", "last_name" => "Smith"}}
903
+ ```
904
+
905
+ The params hash has a nested person value, which can therefore be accessed with params[:person] in the controller.
906
+
907
+ #### check_box
908
+
909
+ Returns a checkbox tag tailored for accessing a specified attribute.
910
+
911
+ ```ruby
912
+ # Let's say that @post.validated? is 1:
913
+ check_box("post", "validated")
914
+ # => <input type="checkbox" id="post_validated" name="post[validated]" value="1" />
915
+ # <input name="post[validated]" type="hidden" value="0" />
916
+ ```
917
+
918
+ #### fields_for
919
+
920
+ 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:
921
+
922
+ ```html+erb
923
+ <%= form_for @person, url: {action: "update"} do |person_form| %>
924
+ First name: <%= person_form.text_field :first_name %>
925
+ Last name : <%= person_form.text_field :last_name %>
926
+
927
+ <%= fields_for @person.permission do |permission_fields| %>
928
+ Admin? : <%= permission_fields.check_box :admin %>
929
+ <% end %>
930
+ <% end %>
931
+ ```
932
+
933
+ #### file_field
934
+
935
+ Returns a file upload input tag tailored for accessing a specified attribute.
936
+
937
+ ```ruby
938
+ file_field(:user, :avatar)
939
+ # => <input type="file" id="user_avatar" name="user[avatar]" />
940
+ ```
941
+
942
+ #### form_for
943
+
944
+ Creates a form and a scope around a specific model object that is used as a base for questioning about values for the fields.
945
+
946
+ ```html+erb
947
+ <%= form_for @post do |f| %>
948
+ <%= f.label :title, 'Title' %>:
949
+ <%= f.text_field :title %><br />
950
+ <%= f.label :body, 'Body' %>:
951
+ <%= f.text_area :body %><br />
952
+ <% end %>
953
+ ```
954
+
955
+ #### hidden_field
956
+
957
+ Returns a hidden input tag tailored for accessing a specified attribute.
958
+
959
+ ```ruby
960
+ hidden_field(:user, :token)
961
+ # => <input type="hidden" id="user_token" name="user[token]" value="#{@user.token}" />
962
+ ```
963
+
964
+ #### label
965
+
966
+ Returns a label tag tailored for labelling an input field for a specified attribute.
967
+
968
+ ```ruby
969
+ label(:post, :title)
970
+ # => <label for="post_title">Title</label>
971
+ ```
972
+
973
+ #### password_field
974
+
975
+ Returns an input tag of the "password" type tailored for accessing a specified attribute.
976
+
977
+ ```ruby
978
+ password_field(:login, :pass)
979
+ # => <input type="text" id="login_pass" name="login[pass]" value="#{@login.pass}" />
980
+ ```
981
+
982
+ #### radio_button
983
+
984
+ Returns a radio button tag for accessing a specified attribute.
985
+
986
+ ```ruby
987
+ # Let's say that @post.category returns "rails":
988
+ radio_button("post", "category", "rails")
989
+ radio_button("post", "category", "java")
990
+ # => <input type="radio" id="post_category_rails" name="post[category]" value="rails" checked="checked" />
991
+ # <input type="radio" id="post_category_java" name="post[category]" value="java" />
992
+ ```
993
+
994
+ #### text_area
995
+
996
+ Returns a textarea opening and closing tag set tailored for accessing a specified attribute.
997
+
998
+ ```ruby
999
+ text_area(:comment, :text, size: "20x30")
1000
+ # => <textarea cols="20" rows="30" id="comment_text" name="comment[text]">
1001
+ # #{@comment.text}
1002
+ # </textarea>
1003
+ ```
1004
+
1005
+ #### text_field
1006
+
1007
+ Returns an input tag of the "text" type tailored for accessing a specified attribute.
1008
+
1009
+ ```ruby
1010
+ text_field(:post, :title)
1011
+ # => <input type="text" id="post_title" name="post[title]" value="#{@post.title}" />
1012
+ ```
1013
+
1014
+ ### FormOptionsHelper
1015
+
1016
+ Provides a number of methods for turning different kinds of containers into a set of option tags.
1017
+
1018
+ #### collection_select
1019
+
1020
+ Returns `select` and `option` tags for the collection of existing return values of `method` for `object`'s class.
1021
+
1022
+ Example object structure for use with this method:
1023
+
1024
+ ```ruby
1025
+ class Post < ActiveRecord::Base
1026
+ belongs_to :author
1027
+ end
1028
+
1029
+ class Author < ActiveRecord::Base
1030
+ has_many :posts
1031
+ def name_with_initial
1032
+ "#{first_name.first}. #{last_name}"
1033
+ end
1034
+ end
1035
+ ```
1036
+
1037
+ Sample usage (selecting the associated Author for an instance of Post, `@post`):
1038
+
1039
+ ```ruby
1040
+ collection_select(:post, :author_id, Author.all, :id, :name_with_initial, {prompt: true})
1041
+ ```
1042
+
1043
+ If `@post.author_id` is 1, this would return:
1044
+
1045
+ ```html
1046
+ <select name="post[author_id]">
1047
+ <option value="">Please select</option>
1048
+ <option value="1" selected="selected">D. Heinemeier Hansson</option>
1049
+ <option value="2">D. Thomas</option>
1050
+ <option value="3">M. Clark</option>
1051
+ </select>
1052
+ ```
1053
+
1054
+ #### collection_radio_buttons
1055
+
1056
+ Returns `radio_button` tags for the collection of existing return values of `method` for `object`'s class.
1057
+
1058
+ Example object structure for use with this method:
1059
+
1060
+ ```ruby
1061
+ class Post < ActiveRecord::Base
1062
+ belongs_to :author
1063
+ end
1064
+
1065
+ class Author < ActiveRecord::Base
1066
+ has_many :posts
1067
+ def name_with_initial
1068
+ "#{first_name.first}. #{last_name}"
1069
+ end
1070
+ end
1071
+ ```
1072
+
1073
+ Sample usage (selecting the associated Author for an instance of Post, `@post`):
1074
+
1075
+ ```ruby
1076
+ collection_radio_buttons(:post, :author_id, Author.all, :id, :name_with_initial)
1077
+ ```
1078
+
1079
+ If `@post.author_id` is 1, this would return:
1080
+
1081
+ ```html
1082
+ <input id="post_author_id_1" name="post[author_id]" type="radio" value="1" checked="checked" />
1083
+ <label for="post_author_id_1">D. Heinemeier Hansson</label>
1084
+ <input id="post_author_id_2" name="post[author_id]" type="radio" value="2" />
1085
+ <label for="post_author_id_2">D. Thomas</label>
1086
+ <input id="post_author_id_3" name="post[author_id]" type="radio" value="3" />
1087
+ <label for="post_author_id_3">M. Clark</label>
1088
+ ```
1089
+
1090
+ #### collection_check_boxes
1091
+
1092
+ Returns `check_box` tags for the collection of existing return values of `method` for `object`'s class.
1093
+
1094
+ Example object structure for use with this method:
1095
+
1096
+ ```ruby
1097
+ class Post < ActiveRecord::Base
1098
+ has_and_belongs_to_many :author
1099
+ end
1100
+
1101
+ class Author < ActiveRecord::Base
1102
+ has_and_belongs_to_many :posts
1103
+ def name_with_initial
1104
+ "#{first_name.first}. #{last_name}"
1105
+ end
1106
+ end
1107
+ ```
1108
+
1109
+ Sample usage (selecting the associated Authors for an instance of Post, `@post`):
1110
+
1111
+ ```ruby
1112
+ collection_check_boxes(:post, :author_ids, Author.all, :id, :name_with_initial)
1113
+ ```
1114
+
1115
+ If `@post.author_ids` is [1], this would return:
1116
+
1117
+ ```html
1118
+ <input id="post_author_ids_1" name="post[author_ids][]" type="checkbox" value="1" checked="checked" />
1119
+ <label for="post_author_ids_1">D. Heinemeier Hansson</label>
1120
+ <input id="post_author_ids_2" name="post[author_ids][]" type="checkbox" value="2" />
1121
+ <label for="post_author_ids_2">D. Thomas</label>
1122
+ <input id="post_author_ids_3" name="post[author_ids][]" type="checkbox" value="3" />
1123
+ <label for="post_author_ids_3">M. Clark</label>
1124
+ <input name="post[author_ids][]" type="hidden" value="" />
1125
+ ```
1126
+
1127
+ #### country_options_for_select
1128
+
1129
+ Returns a string of option tags for pretty much any country in the world.
1130
+
1131
+ #### country_select
1132
+
1133
+ Return select and option tags for the given object and method, using country_options_for_select to generate the list of option tags.
1134
+
1135
+ #### option_groups_from_collection_for_select
1136
+
1137
+ Returns a string of `option` tags, like `options_from_collection_for_select`, but groups them by `optgroup` tags based on the object relationships of the arguments.
1138
+
1139
+ Example object structure for use with this method:
1140
+
1141
+ ```ruby
1142
+ class Continent < ActiveRecord::Base
1143
+ has_many :countries
1144
+ # attribs: id, name
1145
+ end
1146
+
1147
+ class Country < ActiveRecord::Base
1148
+ belongs_to :continent
1149
+ # attribs: id, name, continent_id
1150
+ end
1151
+ ```
1152
+
1153
+ Sample usage:
1154
+
1155
+ ```ruby
1156
+ option_groups_from_collection_for_select(@continents, :countries, :name, :id, :name, 3)
1157
+ ```
1158
+
1159
+ Possible output:
1160
+
1161
+ ```html
1162
+ <optgroup label="Africa">
1163
+ <option value="1">Egypt</option>
1164
+ <option value="4">Rwanda</option>
1165
+ ...
1166
+ </optgroup>
1167
+ <optgroup label="Asia">
1168
+ <option value="3" selected="selected">China</option>
1169
+ <option value="12">India</option>
1170
+ <option value="5">Japan</option>
1171
+ ...
1172
+ </optgroup>
1173
+ ```
1174
+
1175
+ Note: Only the `optgroup` and `option` tags are returned, so you still have to wrap the output in an appropriate `select` tag.
1176
+
1177
+ #### options_for_select
1178
+
1179
+ Accepts a container (hash, array, enumerable, your type) and returns a string of option tags.
1180
+
1181
+ ```ruby
1182
+ options_for_select([ "VISA", "MasterCard" ])
1183
+ # => <option>VISA</option> <option>MasterCard</option>
1184
+ ```
1185
+
1186
+ Note: Only the `option` tags are returned, you have to wrap this call in a regular HTML `select` tag.
1187
+
1188
+ #### options_from_collection_for_select
1189
+
1190
+ Returns a string of option tags that have been compiled by iterating over the `collection` and assigning the result of a call to the `value_method` as the option value and the `text_method` as the option text.
1191
+
1192
+ ```ruby
1193
+ # options_from_collection_for_select(collection, value_method, text_method, selected = nil)
1194
+ ```
1195
+
1196
+ For example, imagine a loop iterating over each person in @project.people to generate an input tag:
1197
+
1198
+ ```ruby
1199
+ options_from_collection_for_select(@project.people, "id", "name")
1200
+ # => <option value="#{person.id}">#{person.name}</option>
1201
+ ```
1202
+
1203
+ Note: Only the `option` tags are returned, you have to wrap this call in a regular HTML `select` tag.
1204
+
1205
+ #### select
1206
+
1207
+ Create a select tag and a series of contained option tags for the provided object and method.
1208
+
1209
+ Example:
1210
+
1211
+ ```ruby
1212
+ select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {include_blank: true})
1213
+ ```
1214
+
1215
+ If `@post.person_id` is 1, this would become:
1216
+
1217
+ ```html
1218
+ <select name="post[person_id]">
1219
+ <option value=""></option>
1220
+ <option value="1" selected="selected">David</option>
1221
+ <option value="2">Sam</option>
1222
+ <option value="3">Tobias</option>
1223
+ </select>
1224
+ ```
1225
+
1226
+ #### time_zone_options_for_select
1227
+
1228
+ Returns a string of option tags for pretty much any time zone in the world.
1229
+
1230
+ #### time_zone_select
1231
+
1232
+ Return select and option tags for the given object and method, using `time_zone_options_for_select` to generate the list of option tags.
1233
+
1234
+ ```ruby
1235
+ time_zone_select( "user", "time_zone")
1236
+ ```
1237
+
1238
+ ### FormTagHelper
1239
+
1240
+ Provides a number of methods for creating form tags that doesn't rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually.
1241
+
1242
+ #### check_box_tag
1243
+
1244
+ Creates a check box form input tag.
1245
+
1246
+ ```ruby
1247
+ check_box_tag 'accept'
1248
+ # => <input id="accept" name="accept" type="checkbox" value="1" />
1249
+ ```
1250
+
1251
+ #### field_set_tag
1252
+
1253
+ Creates a field set for grouping HTML form elements.
1254
+
1255
+ ```html+erb
1256
+ <%= field_set_tag do %>
1257
+ <p><%= text_field_tag 'name' %></p>
1258
+ <% end %>
1259
+ # => <fieldset><p><input id="name" name="name" type="text" /></p></fieldset>
1260
+ ```
1261
+
1262
+ #### file_field_tag
1263
+
1264
+ Creates a file upload field.
1265
+
1266
+ ```html+erb
1267
+ <%= form_tag {action: "post"}, {multipart: true} do %>
1268
+ <label for="file">File to Upload</label> <%= file_field_tag "file" %>
1269
+ <%= submit_tag %>
1270
+ <% end %>
1271
+ ```
1272
+
1273
+ Example output:
1274
+
1275
+ ```ruby
1276
+ file_field_tag 'attachment'
1277
+ # => <input id="attachment" name="attachment" type="file" />
1278
+ ```
1279
+
1280
+ #### form_tag
1281
+
1282
+ Starts a form tag that points the action to an url configured with `url_for_options` just like `ActionController::Base#url_for`.
1283
+
1284
+ ```html+erb
1285
+ <%= form_tag '/posts' do %>
1286
+ <div><%= submit_tag 'Save' %></div>
1287
+ <% end %>
1288
+ # => <form action="/posts" method="post"><div><input type="submit" name="submit" value="Save" /></div></form>
1289
+ ```
1290
+
1291
+ #### hidden_field_tag
1292
+
1293
+ 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.
1294
+
1295
+ ```ruby
1296
+ hidden_field_tag 'token', 'VUBJKB23UIVI1UU1VOBVI@'
1297
+ # => <input id="token" name="token" type="hidden" value="VUBJKB23UIVI1UU1VOBVI@" />
1298
+ ```
1299
+
1300
+ #### image_submit_tag
1301
+
1302
+ Displays an image which when clicked will submit the form.
1303
+
1304
+ ```ruby
1305
+ image_submit_tag("login.png")
1306
+ # => <input src="/images/login.png" type="image" />
1307
+ ```
1308
+
1309
+ #### label_tag
1310
+
1311
+ Creates a label field.
1312
+
1313
+ ```ruby
1314
+ label_tag 'name'
1315
+ # => <label for="name">Name</label>
1316
+ ```
1317
+
1318
+ #### password_field_tag
1319
+
1320
+ Creates a password field, a masked text field that will hide the users input behind a mask character.
1321
+
1322
+ ```ruby
1323
+ password_field_tag 'pass'
1324
+ # => <input id="pass" name="pass" type="password" />
1325
+ ```
1326
+
1327
+ #### radio_button_tag
1328
+
1329
+ Creates a radio button; use groups of radio buttons named the same to allow users to select from a group of options.
1330
+
1331
+ ```ruby
1332
+ radio_button_tag 'gender', 'male'
1333
+ # => <input id="gender_male" name="gender" type="radio" value="male" />
1334
+ ```
1335
+
1336
+ #### select_tag
1337
+
1338
+ Creates a dropdown selection box.
1339
+
1340
+ ```ruby
1341
+ select_tag "people", "<option>David</option>"
1342
+ # => <select id="people" name="people"><option>David</option></select>
1343
+ ```
1344
+
1345
+ #### submit_tag
1346
+
1347
+ Creates a submit button with the text provided as the caption.
1348
+
1349
+ ```ruby
1350
+ submit_tag "Publish this post"
1351
+ # => <input name="commit" type="submit" value="Publish this post" />
1352
+ ```
1353
+
1354
+ #### text_area_tag
1355
+
1356
+ Creates a text input area; use a textarea for longer text inputs such as blog posts or descriptions.
1357
+
1358
+ ```ruby
1359
+ text_area_tag 'post'
1360
+ # => <textarea id="post" name="post"></textarea>
1361
+ ```
1362
+
1363
+ #### text_field_tag
1364
+
1365
+ Creates a standard text field; use these text fields to input smaller chunks of text like a username or a search query.
1366
+
1367
+ ```ruby
1368
+ text_field_tag 'name'
1369
+ # => <input id="name" name="name" type="text" />
1370
+ ```
1371
+
1372
+ ### JavaScriptHelper
1373
+
1374
+ Provides functionality for working with JavaScript in your views.
1375
+
1376
+ #### button_to_function
1377
+
1378
+ Returns a button that'll trigger a JavaScript function using the onclick handler. Examples:
1379
+
1380
+ ```ruby
1381
+ button_to_function "Greeting", "alert('Hello world!')"
1382
+ button_to_function "Delete", "if (confirm('Really?')) do_delete()"
1383
+ button_to_function "Details" do |page|
1384
+ page[:details].visual_effect :toggle_slide
1385
+ end
1386
+ ```
1387
+
1388
+ #### define_javascript_functions
1389
+
1390
+ Includes the Action Pack JavaScript libraries inside a single `script` tag.
1391
+
1392
+ #### escape_javascript
1393
+
1394
+ Escape carrier returns and single and double quotes for JavaScript segments.
1395
+
1396
+ #### javascript_tag
1397
+
1398
+ Returns a JavaScript tag wrapping the provided code.
1399
+
1400
+ ```ruby
1401
+ javascript_tag "alert('All is good')"
1402
+ ```
1403
+
1404
+ ```html
1405
+ <script>
1406
+ //<![CDATA[
1407
+ alert('All is good')
1408
+ //]]>
1409
+ </script>
1410
+ ```
1411
+
1412
+ #### link_to_function
1413
+
1414
+ Returns a link that will trigger a JavaScript function using the onclick handler and return false after the fact.
1415
+
1416
+ ```ruby
1417
+ link_to_function "Greeting", "alert('Hello world!')"
1418
+ # => <a onclick="alert('Hello world!'); return false;" href="#">Greeting</a>
1419
+ ```
1420
+
1421
+ ### NumberHelper
1422
+
1423
+ Provides methods for converting numbers into formatted strings. Methods are provided for phone numbers, currency, percentage, precision, positional notation, and file size.
1424
+
1425
+ #### number_to_currency
1426
+
1427
+ Formats a number into a currency string (e.g., $13.65).
1428
+
1429
+ ```ruby
1430
+ number_to_currency(1234567890.50) # => $1,234,567,890.50
1431
+ ```
1432
+
1433
+ #### number_to_human_size
1434
+
1435
+ Formats the bytes in size into a more understandable representation; useful for reporting file sizes to users.
1436
+
1437
+ ```ruby
1438
+ number_to_human_size(1234) # => 1.2 KB
1439
+ number_to_human_size(1234567) # => 1.2 MB
1440
+ ```
1441
+
1442
+ #### number_to_percentage
1443
+
1444
+ Formats a number as a percentage string.
1445
+
1446
+ ```ruby
1447
+ number_to_percentage(100, :precision => 0) # => 100%
1448
+ ```
1449
+
1450
+ #### number_to_phone
1451
+
1452
+ Formats a number into a US phone number.
1453
+
1454
+ ```ruby
1455
+ number_to_phone(1235551234) # => 123-555-1234
1456
+ ```
1457
+
1458
+ #### number_with_delimiter
1459
+
1460
+ Formats a number with grouped thousands using a delimiter.
1461
+
1462
+ ```ruby
1463
+ number_with_delimiter(12345678) # => 12,345,678
1464
+ ```
1465
+
1466
+ #### number_with_precision
1467
+
1468
+ Formats a number with the specified level of `precision`, which defaults to 3.
1469
+
1470
+ ```ruby
1471
+ number_with_precision(111.2345) # => 111.235
1472
+ number_with_precision(111.2345, 2) # => 111.23
1473
+ ```
1474
+
1475
+ Localized Views
1476
+ ---------------
1477
+
1478
+ Action View has the ability render different templates depending on the current locale.
1479
+
1480
+ For example, suppose you have a Posts controller with a show action. By default, calling this action will render `app/views/posts/show.html.erb`. But if you set `I18n.locale = :de`, then `app/views/posts/show.de.html.erb` 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.
1481
+
1482
+ You can use the same technique to localize the rescue files in your public directory. For example, setting `I18n.locale = :de` and creating `public/500.de.html` and `public/404.de.html` would allow you to have localized rescue pages.
1483
+
1484
+ 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 `app/controllers/application.rb`:
1485
+
1486
+ ```ruby
1487
+ before_action :set_expert_locale
1488
+
1489
+ def set_expert_locale
1490
+ I18n.locale = :expert if current_user.expert?
1491
+ end
1492
+ ```
1493
+
1494
+ Then you could create special views like `app/views/posts/show.expert.html.erb` that would only be displayed to expert users.
1495
+
1496
+ You can read more about the Rails Internationalization (I18n) API [here](i18n.html).
1497
+
1498
+ Using Action View outside of Rails
1499
+ ----------------------------------
1500
+
1501
+ Action View is a Rails component, but it can also be used without Rails. We can demonstrate this by creating a small [Rack](http://rack.rubyforge.org/) application that includes Action View functionality. This may be useful, for example, if you'd like access to Action View's helpers in a Rack application.
1502
+
1503
+ Let's start by ensuring that you have the Action Pack and Rack gems installed:
1504
+
1505
+ ```bash
1506
+ $ gem install actionpack
1507
+ $ gem install rack
1508
+ ```
1509
+
1510
+ Now we'll create a simple "Hello World" application that uses the `titleize` method provided by Active Support.
1511
+
1512
+ **hello_world.rb:**
1513
+
1514
+ ```ruby
1515
+ require 'active_support/core_ext/string/inflections'
1516
+ require 'rack'
1517
+
1518
+ def hello_world(env)
1519
+ [200, {"Content-Type" => "text/html"}, "hello world".titleize]
1520
+ end
1521
+
1522
+ Rack::Handler::Mongrel.run method(:hello_world), Port: 4567
1523
+ ```
1524
+
1525
+ We can see this all come together by starting up the application and then visiting `http://localhost:4567/`
1526
+
1527
+ ```bash
1528
+ $ ruby hello_world.rb
1529
+ ```
1530
+
1531
+ TODO needs a screenshot? I have one - not sure where to put it.
1532
+
1533
+ Notice how 'hello world' has been converted into 'Hello World' by the `titleize` helper method.
1534
+
1535
+ Action View can also be used with [Sinatra](http://www.sinatrarb.com/) in the same way.
1536
+
1537
+ Let's start by ensuring that you have the Action Pack and Sinatra gems installed:
1538
+
1539
+ ```bash
1540
+ $ gem install actionpack
1541
+ $ gem install sinatra
1542
+ ```
1543
+
1544
+ Now we'll create the same "Hello World" application in Sinatra.
1545
+
1546
+ **hello_world.rb:**
1547
+
1548
+ ```ruby
1549
+ require 'action_view'
1550
+ require 'sinatra'
1551
+
1552
+ get '/' do
1553
+ erb 'hello world'.titleize
1554
+ end
1555
+ ```
1556
+
1557
+ Then, we can run the application:
1558
+
1559
+ ```bash
1560
+ $ ruby hello_world.rb
1561
+ ```
1562
+
1563
+ Once the application is running, you can see Sinatra and Action View working together by visiting `http://localhost:4567/`
1564
+
1565
+ TODO needs a screenshot? I have one - not sure where to put it.