rails 4.1.4 → 6.0.0

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