rails 4.2.0.beta4 → 4.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. checksums.yaml +4 -4
  2. data/guides/Rakefile +3 -1
  3. data/guides/bug_report_templates/action_controller_gem.rb +2 -2
  4. data/guides/bug_report_templates/action_controller_master.rb +2 -2
  5. data/guides/source/3_0_release_notes.md +2 -2
  6. data/guides/source/4_1_release_notes.md +1 -1
  7. data/guides/source/4_2_release_notes.md +250 -225
  8. data/guides/source/_welcome.html.erb +1 -1
  9. data/guides/source/action_controller_overview.md +6 -6
  10. data/guides/source/action_mailer_basics.md +26 -11
  11. data/guides/source/action_view_overview.md +0 -1
  12. data/guides/source/active_job_basics.md +29 -23
  13. data/guides/source/active_record_postgresql.md +2 -7
  14. data/guides/source/active_record_validations.md +1 -0
  15. data/guides/source/active_support_core_extensions.md +1 -1
  16. data/guides/source/active_support_instrumentation.md +5 -5
  17. data/guides/source/asset_pipeline.md +2 -2
  18. data/guides/source/association_basics.md +0 -2
  19. data/guides/source/command_line.md +6 -15
  20. data/guides/source/configuring.md +4 -4
  21. data/guides/source/documents.yaml +0 -1
  22. data/guides/source/engines.md +26 -15
  23. data/guides/source/form_helpers.md +10 -2
  24. data/guides/source/getting_started.md +9 -9
  25. data/guides/source/rails_on_rack.md +2 -2
  26. data/guides/source/ruby_on_rails_guides_guidelines.md +1 -0
  27. data/guides/source/security.md +1 -1
  28. data/guides/source/testing.md +80 -13
  29. data/guides/source/upgrading_ruby_on_rails.md +23 -20
  30. metadata +29 -229
  31. data/guides/output/2_2_release_notes.html +0 -724
  32. data/guides/output/2_3_release_notes.html +0 -870
  33. data/guides/output/3_0_release_notes.html +0 -773
  34. data/guides/output/3_1_release_notes.html +0 -740
  35. data/guides/output/3_2_release_notes.html +0 -797
  36. data/guides/output/4_0_release_notes.html +0 -523
  37. data/guides/output/4_1_release_notes.html +0 -806
  38. data/guides/output/4_2_release_notes.html +0 -728
  39. data/guides/output/Gemfile +0 -6
  40. data/guides/output/_license.html +0 -226
  41. data/guides/output/_welcome.html +0 -233
  42. data/guides/output/action_controller_overview.html +0 -1335
  43. data/guides/output/action_mailer_basics.html +0 -928
  44. data/guides/output/action_view_overview.html +0 -1509
  45. data/guides/output/active_job_basics.html +0 -546
  46. data/guides/output/active_model_basics.html +0 -438
  47. data/guides/output/active_record_basics.html +0 -594
  48. data/guides/output/active_record_callbacks.html +0 -592
  49. data/guides/output/active_record_migrations.html +0 -1123
  50. data/guides/output/active_record_postgresql.html +0 -675
  51. data/guides/output/active_record_querying.html +0 -1796
  52. data/guides/output/active_record_validations.html +0 -1301
  53. data/guides/output/active_support_core_extensions.html +0 -3450
  54. data/guides/output/active_support_instrumentation.html +0 -1121
  55. data/guides/output/api_documentation_guidelines.html +0 -498
  56. data/guides/output/asset_pipeline.html +0 -1167
  57. data/guides/output/association_basics.html +0 -2086
  58. data/guides/output/caching_with_rails.html +0 -553
  59. data/guides/output/command_line.html +0 -791
  60. data/guides/output/configuring.html +0 -1055
  61. data/guides/output/contributing_to_ruby_on_rails.html +0 -657
  62. data/guides/output/credits.html +0 -284
  63. data/guides/output/debugging_rails_applications.html +0 -1014
  64. data/guides/output/development_dependencies_install.html +0 -478
  65. data/guides/output/engines.html +0 -1438
  66. data/guides/output/form_helpers.html +0 -1074
  67. data/guides/output/generators.html +0 -838
  68. data/guides/output/getting_started.html +0 -2092
  69. data/guides/output/i18n.html +0 -1198
  70. data/guides/output/images/akshaysurve.jpg +0 -0
  71. data/guides/output/images/belongs_to.png +0 -0
  72. data/guides/output/images/book_icon.gif +0 -0
  73. data/guides/output/images/bullet.gif +0 -0
  74. data/guides/output/images/chapters_icon.gif +0 -0
  75. data/guides/output/images/check_bullet.gif +0 -0
  76. data/guides/output/images/credits_pic_blank.gif +0 -0
  77. data/guides/output/images/csrf.png +0 -0
  78. data/guides/output/images/edge_badge.png +0 -0
  79. data/guides/output/images/favicon.ico +0 -0
  80. data/guides/output/images/feature_tile.gif +0 -0
  81. data/guides/output/images/footer_tile.gif +0 -0
  82. data/guides/output/images/fxn.png +0 -0
  83. data/guides/output/images/getting_started/article_with_comments.png +0 -0
  84. data/guides/output/images/getting_started/challenge.png +0 -0
  85. data/guides/output/images/getting_started/confirm_dialog.png +0 -0
  86. data/guides/output/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
  87. data/guides/output/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  88. data/guides/output/images/getting_started/form_with_errors.png +0 -0
  89. data/guides/output/images/getting_started/index_action_with_edit_link.png +0 -0
  90. data/guides/output/images/getting_started/new_article.png +0 -0
  91. data/guides/output/images/getting_started/new_post.png +0 -0
  92. data/guides/output/images/getting_started/post_with_comments.png +0 -0
  93. data/guides/output/images/getting_started/rails_welcome.png +0 -0
  94. data/guides/output/images/getting_started/routing_error_no_controller.png +0 -0
  95. data/guides/output/images/getting_started/routing_error_no_route_matches.png +0 -0
  96. data/guides/output/images/getting_started/show_action_for_articles.png +0 -0
  97. data/guides/output/images/getting_started/show_action_for_posts.png +0 -0
  98. data/guides/output/images/getting_started/template_is_missing_articles_new.png +0 -0
  99. data/guides/output/images/getting_started/template_is_missing_posts_new.png +0 -0
  100. data/guides/output/images/getting_started/undefined_method_post_path.png +0 -0
  101. data/guides/output/images/getting_started/unknown_action_create_for_articles.png +0 -0
  102. data/guides/output/images/getting_started/unknown_action_create_for_posts.png +0 -0
  103. data/guides/output/images/getting_started/unknown_action_new_for_articles.png +0 -0
  104. data/guides/output/images/getting_started/unknown_action_new_for_posts.png +0 -0
  105. data/guides/output/images/grey_bullet.gif +0 -0
  106. data/guides/output/images/habtm.png +0 -0
  107. data/guides/output/images/has_many.png +0 -0
  108. data/guides/output/images/has_many_through.png +0 -0
  109. data/guides/output/images/has_one.png +0 -0
  110. data/guides/output/images/has_one_through.png +0 -0
  111. data/guides/output/images/header_backdrop.png +0 -0
  112. data/guides/output/images/header_tile.gif +0 -0
  113. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  114. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  115. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  116. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  117. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  118. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  119. data/guides/output/images/icons/README +0 -5
  120. data/guides/output/images/icons/callouts/1.png +0 -0
  121. data/guides/output/images/icons/callouts/10.png +0 -0
  122. data/guides/output/images/icons/callouts/11.png +0 -0
  123. data/guides/output/images/icons/callouts/12.png +0 -0
  124. data/guides/output/images/icons/callouts/13.png +0 -0
  125. data/guides/output/images/icons/callouts/14.png +0 -0
  126. data/guides/output/images/icons/callouts/15.png +0 -0
  127. data/guides/output/images/icons/callouts/2.png +0 -0
  128. data/guides/output/images/icons/callouts/3.png +0 -0
  129. data/guides/output/images/icons/callouts/4.png +0 -0
  130. data/guides/output/images/icons/callouts/5.png +0 -0
  131. data/guides/output/images/icons/callouts/6.png +0 -0
  132. data/guides/output/images/icons/callouts/7.png +0 -0
  133. data/guides/output/images/icons/callouts/8.png +0 -0
  134. data/guides/output/images/icons/callouts/9.png +0 -0
  135. data/guides/output/images/icons/caution.png +0 -0
  136. data/guides/output/images/icons/example.png +0 -0
  137. data/guides/output/images/icons/home.png +0 -0
  138. data/guides/output/images/icons/important.png +0 -0
  139. data/guides/output/images/icons/next.png +0 -0
  140. data/guides/output/images/icons/note.png +0 -0
  141. data/guides/output/images/icons/prev.png +0 -0
  142. data/guides/output/images/icons/tip.png +0 -0
  143. data/guides/output/images/icons/up.png +0 -0
  144. data/guides/output/images/icons/warning.png +0 -0
  145. data/guides/output/images/nav_arrow.gif +0 -0
  146. data/guides/output/images/oscardelben.jpg +0 -0
  147. data/guides/output/images/polymorphic.png +0 -0
  148. data/guides/output/images/radar.png +0 -0
  149. data/guides/output/images/rails4_features.png +0 -0
  150. data/guides/output/images/rails_guides_kindle_cover.jpg +0 -0
  151. data/guides/output/images/rails_guides_logo.gif +0 -0
  152. data/guides/output/images/rails_logo_remix.gif +0 -0
  153. data/guides/output/images/session_fixation.png +0 -0
  154. data/guides/output/images/tab_grey.gif +0 -0
  155. data/guides/output/images/tab_info.gif +0 -0
  156. data/guides/output/images/tab_note.gif +0 -0
  157. data/guides/output/images/tab_red.gif +0 -0
  158. data/guides/output/images/tab_yellow.gif +0 -0
  159. data/guides/output/images/tab_yellow.png +0 -0
  160. data/guides/output/images/vijaydev.jpg +0 -0
  161. data/guides/output/index.html +0 -354
  162. data/guides/output/initialization.html +0 -876
  163. data/guides/output/javascripts/guides.js +0 -59
  164. data/guides/output/javascripts/jquery.min.js +0 -4
  165. data/guides/output/javascripts/responsive-tables.js +0 -43
  166. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
  167. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
  168. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
  169. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
  170. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
  171. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
  172. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
  173. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
  174. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
  175. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
  176. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
  177. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
  178. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
  179. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
  180. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
  181. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
  182. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
  183. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
  184. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
  185. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
  186. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
  187. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
  188. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
  189. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
  190. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
  191. data/guides/output/javascripts/syntaxhighlighter/shCore.js +0 -17
  192. data/guides/output/layout.html +0 -448
  193. data/guides/output/layouts_and_rendering.html +0 -1541
  194. data/guides/output/maintenance_policy.html +0 -257
  195. data/guides/output/migrations.html +0 -1360
  196. data/guides/output/nested_model_forms.html +0 -412
  197. data/guides/output/plugins.html +0 -644
  198. data/guides/output/rails_application_templates.html +0 -450
  199. data/guides/output/rails_on_rack.html +0 -547
  200. data/guides/output/routing.html +0 -1631
  201. data/guides/output/ruby_on_rails_guides_guidelines.html +0 -329
  202. data/guides/output/security.html +0 -935
  203. data/guides/output/stylesheets/fixes.css +0 -16
  204. data/guides/output/stylesheets/kindle.css +0 -11
  205. data/guides/output/stylesheets/main.css +0 -713
  206. data/guides/output/stylesheets/print.css +0 -52
  207. data/guides/output/stylesheets/reset.css +0 -43
  208. data/guides/output/stylesheets/responsive-tables.css +0 -50
  209. data/guides/output/stylesheets/style.css +0 -13
  210. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +0 -226
  211. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
  212. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
  213. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
  214. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
  215. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
  216. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
  217. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
  218. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
  219. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
  220. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
  221. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
  222. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
  223. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
  224. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
  225. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
  226. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
  227. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
  228. data/guides/output/testing.html +0 -1350
  229. data/guides/output/upgrading_ruby_on_rails.html +0 -1135
  230. data/guides/output/working_with_javascript_in_rails.html +0 -587
@@ -1,2092 +0,0 @@
1
- <!DOCTYPE html>
2
-
3
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
- <head>
5
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
6
- <meta name="viewport" content="width=device-width, initial-scale=1"/>
7
-
8
- <title>Getting Started with Rails — Ruby on Rails Guides</title>
9
- <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
10
- <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
11
-
12
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
13
- <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
14
-
15
- <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
16
-
17
- <link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
18
- </head>
19
- <body class="guide">
20
- <div id="topNav">
21
- <div class="wrapper">
22
- <strong class="more-info-label">More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
23
- <span class="red-button more-info-button">
24
- More Ruby on Rails
25
- </span>
26
- <ul class="more-info-links s-hidden">
27
- <li class="more-info"><a href="http://rubyonrails.org/">Overview</a></li>
28
- <li class="more-info"><a href="http://rubyonrails.org/download">Download</a></li>
29
- <li class="more-info"><a href="http://rubyonrails.org/deploy">Deploy</a></li>
30
- <li class="more-info"><a href="https://github.com/rails/rails">Code</a></li>
31
- <li class="more-info"><a href="http://rubyonrails.org/screencasts">Screencasts</a></li>
32
- <li class="more-info"><a href="http://rubyonrails.org/documentation">Documentation</a></li>
33
- <li class="more-info"><a href="http://rubyonrails.org/community">Community</a></li>
34
- <li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
35
- </ul>
36
- </div>
37
- </div>
38
- <div id="header">
39
- <div class="wrapper clearfix">
40
- <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
41
- <ul class="nav">
42
- <li><a class="nav-item" href="index.html">Home</a></li>
43
- <li class="guides-index guides-index-large">
44
- <a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guides Index</a>
45
- <div id="guides" class="clearfix" style="display: none;">
46
- <hr />
47
- <dl class="L">
48
- <dt>Start Here</dt>
49
- <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
50
- <dt>Models</dt>
51
- <dd><a href="active_record_basics.html">Active Record Basics</a></dd>
52
- <dd><a href="active_record_migrations.html">Active Record Migrations</a></dd>
53
- <dd><a href="active_record_validations.html">Active Record Validations</a></dd>
54
- <dd><a href="active_record_callbacks.html">Active Record Callbacks</a></dd>
55
- <dd><a href="association_basics.html">Active Record Associations</a></dd>
56
- <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
57
- <dt>Views</dt>
58
- <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
59
- <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
60
- <dt>Controllers</dt>
61
- <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
62
- <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
63
- </dl>
64
- <dl class="R">
65
- <dt>Digging Deeper</dt>
66
- <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
67
- <dd><a href="i18n.html">Rails Internationalization API</a></dd>
68
- <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
69
- <dd><a href="active_job_basics.html">Active Job Basics</a></dd>
70
- <dd><a href="security.html">Securing Rails Applications</a></dd>
71
- <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
72
- <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
73
- <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
74
- <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
75
- <dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd>
76
- <dt>Extending Rails</dt>
77
- <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
78
- <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
79
- <dt>Contributing to Ruby on Rails</dt>
80
- <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
81
- <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
82
- <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
83
- <dt>Maintenance Policy</dt>
84
- <dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
85
- <dt>Release Notes</dt>
86
- <dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
87
- <dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</a></dd>
88
- <dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</a></dd>
89
- <dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</a></dd>
90
- <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
91
- <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
92
- <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
93
- <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
94
- </dl>
95
- </div>
96
- </li>
97
- <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribute</a></li>
98
- <li><a class="nav-item" href="credits.html">Credits</a></li>
99
- <li class="guides-index guides-index-small">
100
- <select class="guides-index-item nav-item">
101
- <option value="index.html">Guides Index</option>
102
- <optgroup label="Start Here">
103
- <option value="getting_started.html">Getting Started with Rails</option>
104
- </optgroup>
105
- <optgroup label="Models">
106
- <option value="active_record_basics.html">Active Record Basics</option>
107
- <option value="active_record_migrations.html">Active Record Migrations</option>
108
- <option value="active_record_validations.html">Active Record Validations</option>
109
- <option value="active_record_callbacks.html">Active Record Callbacks</option>
110
- <option value="association_basics.html">Active Record Associations</option>
111
- <option value="active_record_querying.html">Active Record Query Interface</option>
112
- </optgroup>
113
- <optgroup label="Views">
114
- <option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option>
115
- <option value="form_helpers.html">Action View Form Helpers</option>
116
- </optgroup>
117
- <optgroup label="Controllers">
118
- <option value="action_controller_overview.html">Action Controller Overview</option>
119
- <option value="routing.html">Rails Routing from the Outside In</option>
120
- </optgroup>
121
- <optgroup label="Digging Deeper">
122
- <option value="active_support_core_extensions.html">Active Support Core Extensions</option>
123
- <option value="i18n.html">Rails Internationalization API</option>
124
- <option value="action_mailer_basics.html">Action Mailer Basics</option>
125
- <option value="active_job_basics.html">Active Job Basics</option>
126
- <option value="security.html">Securing Rails Applications</option>
127
- <option value="debugging_rails_applications.html">Debugging Rails Applications</option>
128
- <option value="configuring.html">Configuring Rails Applications</option>
129
- <option value="command_line.html">Rails Command Line Tools and Rake Tasks</option>
130
- <option value="asset_pipeline.html">Asset Pipeline</option>
131
- <option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option>
132
- </optgroup>
133
- <optgroup label="Extending Rails">
134
- <option value="rails_on_rack.html">Rails on Rack</option>
135
- <option value="generators.html">Creating and Customizing Rails Generators</option>
136
- </optgroup>
137
- <optgroup label="Contributing to Ruby on Rails">
138
- <option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
139
- <option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
140
- <option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</option>
141
- </optgroup>
142
- <optgroup label="Maintenance Policy">
143
- <option value="maintenance_policy.html">Maintenance Policy</option>
144
- </optgroup>
145
- <optgroup label="Release Notes">
146
- <option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
147
- <option value="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</option>
148
- <option value="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</option>
149
- <option value="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</option>
150
- <option value="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</option>
151
- <option value="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</option>
152
- <option value="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</option>
153
- <option value="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</option>
154
- </optgroup>
155
- </select>
156
- </li>
157
- </ul>
158
- </div>
159
- </div>
160
- <hr class="hide" />
161
-
162
- <div id="feature">
163
- <div class="wrapper">
164
- <h2>Getting Started with Rails</h2><p>This guide covers getting up and running with Ruby on Rails.</p><p>After reading this guide, you will know:</p>
165
- <ul>
166
- <li>How to install Rails, create a new Rails application, and connect your
167
- application to a database.</li>
168
- <li>The general layout of a Rails application.</li>
169
- <li>The basic principles of MVC (Model, View, Controller) and RESTful design.</li>
170
- <li>How to quickly generate the starting pieces of a Rails application.</li>
171
- </ul>
172
-
173
-
174
- <div id="subCol">
175
- <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
176
- <ol class="chapters">
177
- <li><a href="#guide-assumptions">Guide Assumptions</a></li>
178
- <li><a href="#what-is-rails-questionmark">What is Rails?</a></li>
179
- <li>
180
- <a href="#creating-a-new-rails-project">Creating a New Rails Project</a>
181
-
182
- <ul>
183
- <li><a href="#installing-rails">Installing Rails</a></li>
184
- <li><a href="#creating-the-blog-application">Creating the Blog Application</a></li>
185
- </ul>
186
- </li>
187
- <li>
188
- <a href="#hello,-rails-bang">Hello, Rails!</a>
189
-
190
- <ul>
191
- <li><a href="#starting-up-the-web-server">Starting up the Web Server</a></li>
192
- <li><a href="#say-%22hello%22,-rails">Say "Hello", Rails</a></li>
193
- <li><a href="#setting-the-application-home-page">Setting the Application Home Page</a></li>
194
- </ul>
195
- </li>
196
- <li>
197
- <a href="#getting-up-and-running">Getting Up and Running</a>
198
-
199
- <ul>
200
- <li><a href="#laying-down-the-ground-work">Laying down the ground work</a></li>
201
- <li><a href="#the-first-form">The first form</a></li>
202
- <li><a href="#creating-articles">Creating articles</a></li>
203
- <li><a href="#creating-the-article-model">Creating the Article model</a></li>
204
- <li><a href="#running-a-migration">Running a Migration</a></li>
205
- <li><a href="#saving-data-in-the-controller">Saving data in the controller</a></li>
206
- <li><a href="#showing-articles">Showing Articles</a></li>
207
- <li><a href="#listing-all-articles">Listing all articles</a></li>
208
- <li><a href="#adding-links">Adding links</a></li>
209
- <li><a href="#adding-some-validation">Adding Some Validation</a></li>
210
- <li><a href="#updating-articles">Updating Articles</a></li>
211
- <li><a href="#using-partials-to-clean-up-duplication-in-views">Using partials to clean up duplication in views</a></li>
212
- <li><a href="#deleting-articles">Deleting Articles</a></li>
213
- </ul>
214
- </li>
215
- <li>
216
- <a href="#adding-a-second-model">Adding a Second Model</a>
217
-
218
- <ul>
219
- <li><a href="#generating-a-model">Generating a Model</a></li>
220
- <li><a href="#associating-models">Associating Models</a></li>
221
- <li><a href="#adding-a-route-for-comments">Adding a Route for Comments</a></li>
222
- <li><a href="#generating-a-controller">Generating a Controller</a></li>
223
- </ul>
224
- </li>
225
- <li>
226
- <a href="#refactoring">Refactoring</a>
227
-
228
- <ul>
229
- <li><a href="#rendering-partial-collections">Rendering Partial Collections</a></li>
230
- <li><a href="#rendering-a-partial-form">Rendering a Partial Form</a></li>
231
- </ul>
232
- </li>
233
- <li>
234
- <a href="#deleting-comments">Deleting Comments</a>
235
-
236
- <ul>
237
- <li><a href="#deleting-associated-objects">Deleting Associated Objects</a></li>
238
- </ul>
239
- </li>
240
- <li>
241
- <a href="#security">Security</a>
242
-
243
- <ul>
244
- <li><a href="#basic-authentication">Basic Authentication</a></li>
245
- <li><a href="#other-security-considerations">Other Security Considerations</a></li>
246
- </ul>
247
- </li>
248
- <li><a href="#what's-next-questionmark">What's Next?</a></li>
249
- <li><a href="#configuration-gotchas">Configuration Gotchas</a></li>
250
- </ol>
251
-
252
- </div>
253
-
254
- </div>
255
- </div>
256
-
257
- <div id="container">
258
- <div class="wrapper">
259
- <div id="mainCol">
260
- <h3 id="guide-assumptions">1 Guide Assumptions</h3><p>This guide is designed for beginners who want to get started with a Rails
261
- application from scratch. It does not assume that you have any prior experience
262
- with Rails. However, to get the most out of it, you need to have some
263
- prerequisites installed:</p>
264
- <ul>
265
- <li>The <a href="https://www.ruby-lang.org/en/downloads">Ruby</a> language version 1.9.3 or newer.</li>
266
- <li>The <a href="https://rubygems.org">RubyGems</a> packaging system, which is installed with Ruby
267
- versions 1.9 and later. To learn more about RubyGems, please read the <a href="http://guides.rubygems.org">RubyGems Guides</a>.</li>
268
- <li>A working installation of the <a href="https://www.sqlite.org">SQLite3 Database</a>.</li>
269
- </ul>
270
- <p>Rails is a web application framework running on the Ruby programming language.
271
- If you have no prior experience with Ruby, you will find a very steep learning
272
- curve diving straight into Rails. There are several curated lists of online resources
273
- for learning Ruby:</p>
274
- <ul>
275
- <li><a href="https://www.ruby-lang.org/en/documentation/">Official Ruby Programming Language website</a></li>
276
- <li><a href="http://resrc.io/list/10/list-of-free-programming-books/#ruby">reSRC's List of Free Programming Books</a></li>
277
- </ul>
278
- <p>Be aware that some resources, while still excellent, cover versions of Ruby as old as
279
- 1.6, and commonly 1.8, and will not include some syntax that you will see in day-to-day
280
- development with Rails.</p><h3 id="what-is-rails-questionmark">2 What is Rails?</h3><p>Rails is a web application development framework written in the Ruby language.
281
- It is designed to make programming web applications easier by making assumptions
282
- about what every developer needs to get started. It allows you to write less
283
- code while accomplishing more than many other languages and frameworks.
284
- Experienced Rails developers also report that it makes web application
285
- development more fun.</p><p>Rails is opinionated software. It makes the assumption that there is the "best"
286
- way to do things, and it's designed to encourage that way - and in some cases to
287
- discourage alternatives. If you learn "The Rails Way" you'll probably discover a
288
- tremendous increase in productivity. If you persist in bringing old habits from
289
- other languages to your Rails development, and trying to use patterns you
290
- learned elsewhere, you may have a less happy experience.</p><p>The Rails philosophy includes two major guiding principles:</p>
291
- <ul>
292
- <li>
293
- <strong>Don't Repeat Yourself:</strong> DRY is a principle of software development which
294
- states that "Every piece of knowledge must have a single, unambiguous, authoritative
295
- representation within a system." By not writing the same information over and over
296
- again, our code is more maintainable, more extensible, and less buggy.</li>
297
- <li>
298
- <strong>Convention Over Configuration:</strong> Rails has opinions about the best way to do many
299
- things in a web application, and defaults to this set of conventions, rather than
300
- require that you specify every minutiae through endless configuration files.</li>
301
- </ul>
302
- <h3 id="creating-a-new-rails-project">3 Creating a New Rails Project</h3><p>The best way to use this guide is to follow each step as it happens, no code or
303
- step needed to make this example application has been left out, so you can
304
- literally follow along step by step.</p><p>By following along with this guide, you'll create a Rails project called
305
- <code>blog</code>, a (very) simple weblog. Before you can start building the application,
306
- you need to make sure that you have Rails itself installed.</p><div class="info"><p>The examples below use <code>$</code> to represent your terminal prompt in a UNIX-like OS,
307
- though it may have been customized to appear differently. If you are using Windows,
308
- your prompt will look something like <code>c:\source_code&gt;</code></p></div><h4 id="installing-rails">3.1 Installing Rails</h4><p>Open up a command line prompt. On Mac OS X open Terminal.app, on Windows choose
309
- "Run" from your Start menu and type 'cmd.exe'. Any commands prefaced with a
310
- dollar sign <code>$</code> should be run in the command line. Verify that you have a
311
- current version of Ruby installed:</p><div class="info"><p>A number of tools exist to help you quickly install Ruby and Ruby
312
- on Rails on your system. Windows users can use <a href="http://railsinstaller.org">Rails Installer</a>,
313
- while Mac OS X users can use <a href="https://github.com/tokaido/tokaidoapp">Tokaido</a>.</p></div><div class="code_container">
314
- <pre class="brush: plain; gutter: false; toolbar: false">
315
- $ ruby -v
316
- ruby 2.0.0p353
317
-
318
- </pre>
319
- </div>
320
- <p>If you don't have Ruby installed have a look at
321
- <a href="https://www.ruby-lang.org/en/installation/">ruby-lang.org</a> for possible ways to
322
- install Ruby on your platform.</p><p>Many popular UNIX-like OSes ship with an acceptable version of SQLite3. Windows
323
- users and others can find installation instructions at <a href="https://www.sqlite.org">the SQLite3 website</a>.
324
- Verify that it is correctly installed and in your PATH:</p><div class="code_container">
325
- <pre class="brush: plain; gutter: false; toolbar: false">
326
- $ sqlite3 --version
327
-
328
- </pre>
329
- </div>
330
- <p>The program should report its version.</p><p>To install Rails, use the <code>gem install</code> command provided by RubyGems:</p><div class="code_container">
331
- <pre class="brush: plain; gutter: false; toolbar: false">
332
- $ gem install rails
333
-
334
- </pre>
335
- </div>
336
- <p>To verify that you have everything installed correctly, you should be able to
337
- run the following:</p><div class="code_container">
338
- <pre class="brush: plain; gutter: false; toolbar: false">
339
- $ rails --version
340
-
341
- </pre>
342
- </div>
343
- <p>If it says something like "Rails 4.2.0", you are ready to continue.</p><h4 id="creating-the-blog-application">3.2 Creating the Blog Application</h4><p>Rails comes with a number of scripts called generators that are designed to make
344
- your development life easier by creating everything that's necessary to start
345
- working on a particular task. One of these is the new application generator,
346
- which will provide you with the foundation of a fresh Rails application so that
347
- you don't have to write it yourself.</p><p>To use this generator, open a terminal, navigate to a directory where you have
348
- rights to create files, and type:</p><div class="code_container">
349
- <pre class="brush: plain; gutter: false; toolbar: false">
350
- $ rails new blog
351
-
352
- </pre>
353
- </div>
354
- <p>This will create a Rails application called Blog in a <code>blog</code> directory and
355
- install the gem dependencies that are already mentioned in <code>Gemfile</code> using
356
- <code>bundle install</code>.</p><div class="info"><p>You can see all of the command line options that the Rails application
357
- builder accepts by running <code>rails new -h</code>.</p></div><p>After you create the blog application, switch to its folder:</p><div class="code_container">
358
- <pre class="brush: plain; gutter: false; toolbar: false">
359
- $ cd blog
360
-
361
- </pre>
362
- </div>
363
- <p>The <code>blog</code> directory has a number of auto-generated files and folders that make
364
- up the structure of a Rails application. Most of the work in this tutorial will
365
- happen in the <code>app</code> folder, but here's a basic rundown on the function of each
366
- of the files and folders that Rails created by default:</p>
367
- <table>
368
- <thead>
369
- <tr>
370
- <th>File/Folder</th>
371
- <th>Purpose</th>
372
- </tr>
373
- </thead>
374
- <tbody>
375
- <tr>
376
- <td>app/</td>
377
- <td>Contains the controllers, models, views, helpers, mailers and assets for your application. You'll focus on this folder for the remainder of this guide.</td>
378
- </tr>
379
- <tr>
380
- <td>bin/</td>
381
- <td>Contains the rails script that starts your app and can contain other scripts you use to setup, deploy or run your application.</td>
382
- </tr>
383
- <tr>
384
- <td>config/</td>
385
- <td>Configure your application's routes, database, and more. This is covered in more detail in <a href="configuring.html">Configuring Rails Applications</a>.</td>
386
- </tr>
387
- <tr>
388
- <td>config.ru</td>
389
- <td>Rack configuration for Rack based servers used to start the application.</td>
390
- </tr>
391
- <tr>
392
- <td>db/</td>
393
- <td>Contains your current database schema, as well as the database migrations.</td>
394
- </tr>
395
- <tr>
396
- <td>Gemfile<br>Gemfile.lock</td>
397
- <td>These files allow you to specify what gem dependencies are needed for your Rails application. These files are used by the Bundler gem. For more information about Bundler, see <a href="http://bundler.io">the Bundler website</a>.</td>
398
- </tr>
399
- <tr>
400
- <td>lib/</td>
401
- <td>Extended modules for your application.</td>
402
- </tr>
403
- <tr>
404
- <td>log/</td>
405
- <td>Application log files.</td>
406
- </tr>
407
- <tr>
408
- <td>public/</td>
409
- <td>The only folder seen by the world as-is. Contains static files and compiled assets.</td>
410
- </tr>
411
- <tr>
412
- <td>Rakefile</td>
413
- <td>This file locates and loads tasks that can be run from the command line. The task definitions are defined throughout the components of Rails. Rather than changing Rakefile, you should add your own tasks by adding files to the lib/tasks directory of your application.</td>
414
- </tr>
415
- <tr>
416
- <td>README.rdoc</td>
417
- <td>This is a brief instruction manual for your application. You should edit this file to tell others what your application does, how to set it up, and so on.</td>
418
- </tr>
419
- <tr>
420
- <td>test/</td>
421
- <td>Unit tests, fixtures, and other test apparatus. These are covered in <a href="testing.html">Testing Rails Applications</a>.</td>
422
- </tr>
423
- <tr>
424
- <td>tmp/</td>
425
- <td>Temporary files (like cache, pid, and session files).</td>
426
- </tr>
427
- <tr>
428
- <td>vendor/</td>
429
- <td>A place for all third-party code. In a typical Rails application this includes vendored gems.</td>
430
- </tr>
431
- </tbody>
432
- </table>
433
- <h3 id="hello,-rails-bang">4 Hello, Rails!</h3><p>To begin with, let's get some text up on screen quickly. To do this, you need to
434
- get your Rails application server running.</p><h4 id="starting-up-the-web-server">4.1 Starting up the Web Server</h4><p>You actually have a functional Rails application already. To see it, you need to
435
- start a web server on your development machine. You can do this by running the
436
- following in the <code>blog</code> directory:</p><div class="code_container">
437
- <pre class="brush: plain; gutter: false; toolbar: false">
438
- $ bin/rails server
439
-
440
- </pre>
441
- </div>
442
- <div class="info"><p>Compiling CoffeeScript and JavaScript asset compression requires you
443
- have a JavaScript runtime available on your system, in the absence
444
- of a runtime you will see an <code>execjs</code> error during asset compilation.
445
- Usually Mac OS X and Windows come with a JavaScript runtime installed.
446
- Rails adds the <code>therubyracer</code> gem to the generated <code>Gemfile</code> in a
447
- commented line for new apps and you can uncomment if you need it.
448
- <code>therubyrhino</code> is the recommended runtime for JRuby users and is added by
449
- default to the <code>Gemfile</code> in apps generated under JRuby. You can investigate
450
- all the supported runtimes at <a href="https://github.com/sstephenson/execjs#readme">ExecJS</a>.</p></div><p>This will fire up WEBrick, a web server distributed with Ruby by default. To see
451
- your application in action, open a browser window and navigate to
452
- <a href="http://localhost:3000">http://localhost:3000</a>. You should see the Rails default information page:</p><p><img src="images/getting_started/rails_welcome.png" alt="Welcome aboard screenshot"></p><div class="info"><p>To stop the web server, hit Ctrl+C in the terminal window where it's
453
- running. To verify the server has stopped you should see your command prompt
454
- cursor again. For most UNIX-like systems including Mac OS X this will be a
455
- dollar sign <code>$</code>. In development mode, Rails does not generally require you to
456
- restart the server; changes you make in files will be automatically picked up by
457
- the server.</p></div><p>The "Welcome aboard" page is the <em>smoke test</em> for a new Rails application: it
458
- makes sure that you have your software configured correctly enough to serve a
459
- page. You can also click on the <em>About your application's environment</em> link to
460
- see a summary of your application's environment.</p><h4 id='say-"hello",-rails'>4.2 Say "Hello", Rails</h4><p>To get Rails saying "Hello", you need to create at minimum a <em>controller</em> and a
461
- <em>view</em>.</p><p>A controller's purpose is to receive specific requests for the application.
462
- <em>Routing</em> decides which controller receives which requests. Often, there is more
463
- than one route to each controller, and different routes can be served by
464
- different <em>actions</em>. Each action's purpose is to collect information to provide
465
- it to a view.</p><p>A view's purpose is to display this information in a human readable format. An
466
- important distinction to make is that it is the <em>controller</em>, not the view,
467
- where information is collected. The view should just display that information.
468
- By default, view templates are written in a language called eRuby (Embedded
469
- Ruby) which is processed by the request cycle in Rails before being sent to the
470
- user.</p><p>To create a new controller, you will need to run the "controller" generator and
471
- tell it you want a controller called "welcome" with an action called "index",
472
- just like this:</p><div class="code_container">
473
- <pre class="brush: plain; gutter: false; toolbar: false">
474
- $ bin/rails generate controller welcome index
475
-
476
- </pre>
477
- </div>
478
- <p>Rails will create several files and a route for you.</p><div class="code_container">
479
- <pre class="brush: plain; gutter: false; toolbar: false">
480
- create app/controllers/welcome_controller.rb
481
- route get 'welcome/index'
482
- invoke erb
483
- create app/views/welcome
484
- create app/views/welcome/index.html.erb
485
- invoke test_unit
486
- create test/controllers/welcome_controller_test.rb
487
- invoke helper
488
- create app/helpers/welcome_helper.rb
489
- invoke assets
490
- invoke coffee
491
- create app/assets/javascripts/welcome.js.coffee
492
- invoke scss
493
- create app/assets/stylesheets/welcome.css.scss
494
-
495
- </pre>
496
- </div>
497
- <p>Most important of these are of course the controller, located at
498
- <code>app/controllers/welcome_controller.rb</code> and the view, located at
499
- <code>app/views/welcome/index.html.erb</code>.</p><p>Open the <code>app/views/welcome/index.html.erb</code> file in your text editor. Delete all
500
- of the existing code in the file, and replace it with the following single line
501
- of code:</p><div class="code_container">
502
- <pre class="brush: xml; gutter: false; toolbar: false">
503
- &lt;h1&gt;Hello, Rails!&lt;/h1&gt;
504
-
505
- </pre>
506
- </div>
507
- <h4 id="setting-the-application-home-page">4.3 Setting the Application Home Page</h4><p>Now that we have made the controller and view, we need to tell Rails when we
508
- want "Hello, Rails!" to show up. In our case, we want it to show up when we
509
- navigate to the root URL of our site, <a href="http://localhost:3000">http://localhost:3000</a>. At the moment,
510
- "Welcome aboard" is occupying that spot.</p><p>Next, you have to tell Rails where your actual home page is located.</p><p>Open the file <code>config/routes.rb</code> in your editor.</p><div class="code_container">
511
- <pre class="brush: ruby; gutter: false; toolbar: false">
512
- Rails.application.routes.draw do
513
- get 'welcome/index'
514
-
515
- # The priority is based upon order of creation:
516
- # first created -&gt; highest priority.
517
- #
518
- # You can have the root of your site routed with "root"
519
- # root 'welcome#index'
520
- #
521
- # ...
522
-
523
- </pre>
524
- </div>
525
- <p>This is your application's <em>routing file</em> which holds entries in a special DSL
526
- (domain-specific language) that tells Rails how to connect incoming requests to
527
- controllers and actions. This file contains many sample routes on commented
528
- lines, and one of them actually shows you how to connect the root of your site
529
- to a specific controller and action. Find the line beginning with <code>root</code> and
530
- uncomment it. It should look something like the following:</p><div class="code_container">
531
- <pre class="brush: ruby; gutter: false; toolbar: false">
532
- root 'welcome#index'
533
-
534
- </pre>
535
- </div>
536
- <p><code>root 'welcome#index'</code> tells Rails to map requests to the root of the
537
- application to the welcome controller's index action and <code>get 'welcome/index'</code>
538
- tells Rails to map requests to <a href="http://localhost:3000/welcome/index">http://localhost:3000/welcome/index</a> to the
539
- welcome controller's index action. This was created earlier when you ran the
540
- controller generator (<code>rails generate controller welcome index</code>).</p><p>Launch the web server again if you stopped it to generate the controller (<code>rails
541
- server</code>) and navigate to <a href="http://localhost:3000">http://localhost:3000</a> in your browser. You'll see the
542
- "Hello, Rails!" message you put into <code>app/views/welcome/index.html.erb</code>,
543
- indicating that this new route is indeed going to <code>WelcomeController</code>'s <code>index</code>
544
- action and is rendering the view correctly.</p><div class="info"><p>For more information about routing, refer to <a href="routing.html">Rails Routing from the Outside In</a>.</p></div><h3 id="getting-up-and-running">5 Getting Up and Running</h3><p>Now that you've seen how to create a controller, an action and a view, let's
545
- create something with a bit more substance.</p><p>In the Blog application, you will now create a new <em>resource</em>. A resource is the
546
- term used for a collection of similar objects, such as articles, people or
547
- animals.
548
- You can create, read, update and destroy items for a resource and these
549
- operations are referred to as <em>CRUD</em> operations.</p><p>Rails provides a <code>resources</code> method which can be used to declare a standard REST
550
- resource. Here's what <code>config/routes.rb</code> should look like after the
551
- <em>article resource</em> is declared.</p><div class="code_container">
552
- <pre class="brush: ruby; gutter: false; toolbar: false">
553
- Rails.application.routes.draw do
554
-
555
- resources :articles
556
-
557
- root 'welcome#index'
558
- end
559
-
560
- </pre>
561
- </div>
562
- <p>If you run <code>rake routes</code>, you'll see that it has defined routes for all the
563
- standard RESTful actions. The meaning of the prefix column (and other columns)
564
- will be seen later, but for now notice that Rails has inferred the
565
- singular form <code>article</code> and makes meaningful use of the distinction.</p><div class="code_container">
566
- <pre class="brush: plain; gutter: false; toolbar: false">
567
- $ bin/rake routes
568
- Prefix Verb URI Pattern Controller#Action
569
- articles GET /articles(.:format) articles#index
570
- POST /articles(.:format) articles#create
571
- new_article GET /articles/new(.:format) articles#new
572
- edit_article GET /articles/:id/edit(.:format) articles#edit
573
- article GET /articles/:id(.:format) articles#show
574
- PATCH /articles/:id(.:format) articles#update
575
- PUT /articles/:id(.:format) articles#update
576
- DELETE /articles/:id(.:format) articles#destroy
577
- root GET / welcome#index
578
-
579
- </pre>
580
- </div>
581
- <p>In the next section, you will add the ability to create new articles in your
582
- application and be able to view them. This is the "C" and the "R" from CRUD:
583
- creation and reading. The form for doing this will look like this:</p><p><img src="images/getting_started/new_article.png" alt="The new article form"></p><p>It will look a little basic for now, but that's ok. We'll look at improving the
584
- styling for it afterwards.</p><h4 id="laying-down-the-ground-work">5.1 Laying down the ground work</h4><p>Firstly, you need a place within the application to create a new article. A
585
- great place for that would be at <code>/articles/new</code>. With the route already
586
- defined, requests can now be made to <code>/articles/new</code> in the application.
587
- Navigate to <a href="http://localhost:3000/articles/new">http://localhost:3000/articles/new</a> and you'll see a routing
588
- error:</p><p><img src="images/getting_started/routing_error_no_controller.png" alt="Another routing error, uninitialized constant ArticlesController"></p><p>This error occurs because the route needs to have a controller defined in order
589
- to serve the request. The solution to this particular problem is simple: create
590
- a controller called <code>ArticlesController</code>. You can do this by running this
591
- command:</p><div class="code_container">
592
- <pre class="brush: plain; gutter: false; toolbar: false">
593
- $ bin/rails g controller articles
594
-
595
- </pre>
596
- </div>
597
- <p>If you open up the newly generated <code>app/controllers/articles_controller.rb</code>
598
- you'll see a fairly empty controller:</p><div class="code_container">
599
- <pre class="brush: ruby; gutter: false; toolbar: false">
600
- class ArticlesController &lt; ApplicationController
601
- end
602
-
603
- </pre>
604
- </div>
605
- <p>A controller is simply a class that is defined to inherit from
606
- <code>ApplicationController</code>.
607
- It's inside this class that you'll define methods that will become the actions
608
- for this controller. These actions will perform CRUD operations on the articles
609
- within our system.</p><div class="note"><p>There are <code>public</code>, <code>private</code> and <code>protected</code> methods in Ruby,
610
- but only <code>public</code> methods can be actions for controllers.
611
- For more details check out <a href="http://www.ruby-doc.org/docs/ProgrammingRuby/">Programming Ruby</a>.</p></div><p>If you refresh <a href="http://localhost:3000/articles/new">http://localhost:3000/articles/new</a> now, you'll get a new error:</p><p><img src="images/getting_started/unknown_action_new_for_articles.png" alt="Unknown action new for ArticlesController!"></p><p>This error indicates that Rails cannot find the <code>new</code> action inside the
612
- <code>ArticlesController</code> that you just generated. This is because when controllers
613
- are generated in Rails they are empty by default, unless you tell it
614
- your wanted actions during the generation process.</p><p>To manually define an action inside a controller, all you need to do is to
615
- define a new method inside the controller. Open
616
- <code>app/controllers/articles_controller.rb</code> and inside the <code>ArticlesController</code>
617
- class, define a <code>new</code> method so that the controller now looks like this:</p><div class="code_container">
618
- <pre class="brush: ruby; gutter: false; toolbar: false">
619
- class ArticlesController &lt; ApplicationController
620
- def new
621
- end
622
- end
623
-
624
- </pre>
625
- </div>
626
- <p>With the <code>new</code> method defined in <code>ArticlesController</code>, if you refresh
627
- <a href="http://localhost:3000/articles/new">http://localhost:3000/articles/new</a> you'll see another error:</p><p><img src="images/getting_started/template_is_missing_articles_new.png" alt="Template is missing for articles/new"></p><p>You're getting this error now because Rails expects plain actions like this one
628
- to have views associated with them to display their information. With no view
629
- available, Rails errors out.</p><p>In the above image, the bottom line has been truncated. Let's see what the full
630
- thing looks like:</p>
631
- <blockquote>
632
- <p>Missing template articles/new, application/new with {locale:[:en], formats:[:html], handlers:[:erb, :builder, :coffee]}. Searched in: * "/path/to/blog/app/views"</p>
633
- </blockquote>
634
- <p>That's quite a lot of text! Let's quickly go through and understand what each
635
- part of it does.</p><p>The first part identifies what template is missing. In this case, it's the
636
- <code>articles/new</code> template. Rails will first look for this template. If not found,
637
- then it will attempt to load a template called <code>application/new</code>. It looks for
638
- one here because the <code>ArticlesController</code> inherits from <code>ApplicationController</code>.</p><p>The next part of the message contains a hash. The <code>:locale</code> key in this hash
639
- simply indicates what spoken language template should be retrieved. By default,
640
- this is the English - or "en" - template. The next key, <code>:formats</code> specifies the
641
- format of template to be served in response. The default format is <code>:html</code>, and
642
- so Rails is looking for an HTML template. The final key, <code>:handlers</code>, is telling
643
- us what <em>template handlers</em> could be used to render our template. <code>:erb</code> is most
644
- commonly used for HTML templates, <code>:builder</code> is used for XML templates, and
645
- <code>:coffee</code> uses CoffeeScript to build JavaScript templates.</p><p>The final part of this message tells us where Rails has looked for the templates.
646
- Templates within a basic Rails application like this are kept in a single
647
- location, but in more complex applications it could be many different paths.</p><p>The simplest template that would work in this case would be one located at
648
- <code>app/views/articles/new.html.erb</code>. The extension of this file name is key: the
649
- first extension is the <em>format</em> of the template, and the second extension is the
650
- <em>handler</em> that will be used. Rails is attempting to find a template called
651
- <code>articles/new</code> within <code>app/views</code> for the application. The format for this
652
- template can only be <code>html</code> and the handler must be one of <code>erb</code>, <code>builder</code> or
653
- <code>coffee</code>. Because you want to create a new HTML form, you will be using the <code>ERB</code>
654
- language. Therefore the file should be called <code>articles/new.html.erb</code> and needs
655
- to be located inside the <code>app/views</code> directory of the application.</p><p>Go ahead now and create a new file at <code>app/views/articles/new.html.erb</code> and
656
- write this content in it:</p><div class="code_container">
657
- <pre class="brush: xml; gutter: false; toolbar: false">
658
- &lt;h1&gt;New Article&lt;/h1&gt;
659
-
660
- </pre>
661
- </div>
662
- <p>When you refresh <a href="http://localhost:3000/articles/new">http://localhost:3000/articles/new</a> you'll now see that the
663
- page has a title. The route, controller, action and view are now working
664
- harmoniously! It's time to create the form for a new article.</p><h4 id="the-first-form">5.2 The first form</h4><p>To create a form within this template, you will use a <em>form
665
- builder</em>. The primary form builder for Rails is provided by a helper
666
- method called <code>form_for</code>. To use this method, add this code into
667
- <code>app/views/articles/new.html.erb</code>:</p><div class="code_container">
668
- <pre class="brush: plain; gutter: false; toolbar: false">
669
- &lt;%= form_for :article do |f| %&gt;
670
- &lt;p&gt;
671
- &lt;%= f.label :title %&gt;&lt;br&gt;
672
- &lt;%= f.text_field :title %&gt;
673
- &lt;/p&gt;
674
-
675
- &lt;p&gt;
676
- &lt;%= f.label :text %&gt;&lt;br&gt;
677
- &lt;%= f.text_area :text %&gt;
678
- &lt;/p&gt;
679
-
680
- &lt;p&gt;
681
- &lt;%= f.submit %&gt;
682
- &lt;/p&gt;
683
- &lt;% end %&gt;
684
-
685
- </pre>
686
- </div>
687
- <p>If you refresh the page now, you'll see the exact same form as in the example.
688
- Building forms in Rails is really just that easy!</p><p>When you call <code>form_for</code>, you pass it an identifying object for this
689
- form. In this case, it's the symbol <code>:article</code>. This tells the <code>form_for</code>
690
- helper what this form is for. Inside the block for this method, the
691
- <code>FormBuilder</code> object - represented by <code>f</code> - is used to build two labels and two
692
- text fields, one each for the title and text of an article. Finally, a call to
693
- <code>submit</code> on the <code>f</code> object will create a submit button for the form.</p><p>There's one problem with this form though. If you inspect the HTML that is
694
- generated, by viewing the source of the page, you will see that the <code>action</code>
695
- attribute for the form is pointing at <code>/articles/new</code>. This is a problem because
696
- this route goes to the very page that you're on right at the moment, and that
697
- route should only be used to display the form for a new article.</p><p>The form needs to use a different URL in order to go somewhere else.
698
- This can be done quite simply with the <code>:url</code> option of <code>form_for</code>.
699
- Typically in Rails, the action that is used for new form submissions
700
- like this is called "create", and so the form should be pointed to that action.</p><p>Edit the <code>form_for</code> line inside <code>app/views/articles/new.html.erb</code> to look like
701
- this:</p><div class="code_container">
702
- <pre class="brush: plain; gutter: false; toolbar: false">
703
- &lt;%= form_for :article, url: articles_path do |f| %&gt;
704
-
705
- </pre>
706
- </div>
707
- <p>In this example, the <code>articles_path</code> helper is passed to the <code>:url</code> option.
708
- To see what Rails will do with this, we look back at the output of
709
- <code>rake routes</code>:</p><div class="code_container">
710
- <pre class="brush: plain; gutter: false; toolbar: false">
711
- $ bin/rake routes
712
- Prefix Verb URI Pattern Controller#Action
713
- articles GET /articles(.:format) articles#index
714
- POST /articles(.:format) articles#create
715
- new_article GET /articles/new(.:format) articles#new
716
- edit_article GET /articles/:id/edit(.:format) articles#edit
717
- article GET /articles/:id(.:format) articles#show
718
- PATCH /articles/:id(.:format) articles#update
719
- PUT /articles/:id(.:format) articles#update
720
- DELETE /articles/:id(.:format) articles#destroy
721
- root GET / welcome#index
722
-
723
- </pre>
724
- </div>
725
- <p>The <code>articles_path</code> helper tells Rails to point the form to the URI Pattern
726
- associated with the <code>articles</code> prefix; and the form will (by default) send a
727
- <code>POST</code> request to that route. This is associated with the <code>create</code> action of
728
- the current controller, the <code>ArticlesController</code>.</p><p>With the form and its associated route defined, you will be able to fill in the
729
- form and then click the submit button to begin the process of creating a new
730
- article, so go ahead and do that. When you submit the form, you should see a
731
- familiar error:</p><p><img src="images/getting_started/unknown_action_create_for_articles.png" alt="Unknown action create for ArticlesController"></p><p>You now need to create the <code>create</code> action within the <code>ArticlesController</code> for
732
- this to work.</p><h4 id="creating-articles">5.3 Creating articles</h4><p>To make the "Unknown action" go away, you can define a <code>create</code> action within
733
- the <code>ArticlesController</code> class in <code>app/controllers/articles_controller.rb</code>,
734
- underneath the <code>new</code> action, as shown:</p><div class="code_container">
735
- <pre class="brush: ruby; gutter: false; toolbar: false">
736
- class ArticlesController &lt; ApplicationController
737
- def new
738
- end
739
-
740
- def create
741
- end
742
- end
743
-
744
- </pre>
745
- </div>
746
- <p>If you re-submit the form now, you'll see another familiar error: a template is
747
- missing. That's ok, we can ignore that for now. What the <code>create</code> action should
748
- be doing is saving our new article to the database.</p><p>When a form is submitted, the fields of the form are sent to Rails as
749
- <em>parameters</em>. These parameters can then be referenced inside the controller
750
- actions, typically to perform a particular task. To see what these parameters
751
- look like, change the <code>create</code> action to this:</p><div class="code_container">
752
- <pre class="brush: ruby; gutter: false; toolbar: false">
753
- def create
754
- render plain: params[:article].inspect
755
- end
756
-
757
- </pre>
758
- </div>
759
- <p>The <code>render</code> method here is taking a very simple hash with a key of <code>plain</code> and
760
- value of <code>params[:article].inspect</code>. The <code>params</code> method is the object which
761
- represents the parameters (or fields) coming in from the form. The <code>params</code>
762
- method returns an <code>ActiveSupport::HashWithIndifferentAccess</code> object, which
763
- allows you to access the keys of the hash using either strings or symbols. In
764
- this situation, the only parameters that matter are the ones from the form.</p><div class="info"><p>Ensure you have a firm grasp of the <code>params</code> method, as you'll use it fairly regularly. Let's consider an example URL: <strong><a href="http://www.example.com/?username=dhh&amp;email=dhh@email.com">http://www.example.com/?username=dhh&amp;email=dhh@email.com</a></strong>. In this URL, <code>params[:username]</code> would equal "dhh" and <code>params[:email]</code> would equal "<a href="mailto:dhh@email.com">dhh@email.com</a>".</p></div><p>If you re-submit the form one more time you'll now no longer get the missing
765
- template error. Instead, you'll see something that looks like the following:</p><div class="code_container">
766
- <pre class="brush: ruby; gutter: false; toolbar: false">
767
- {"title"=&gt;"First article!", "text"=&gt;"This is my first article."}
768
-
769
- </pre>
770
- </div>
771
- <p>This action is now displaying the parameters for the article that are coming in
772
- from the form. However, this isn't really all that helpful. Yes, you can see the
773
- parameters but nothing in particular is being done with them.</p><h4 id="creating-the-article-model">5.4 Creating the Article model</h4><p>Models in Rails use a singular name, and their corresponding database tables
774
- use a plural name. Rails provides a generator for creating models, which most
775
- Rails developers tend to use when creating new models. To create the new model,
776
- run this command in your terminal:</p><div class="code_container">
777
- <pre class="brush: plain; gutter: false; toolbar: false">
778
- $ bin/rails generate model Article title:string text:text
779
-
780
- </pre>
781
- </div>
782
- <p>With that command we told Rails that we want a <code>Article</code> model, together
783
- with a <em>title</em> attribute of type string, and a <em>text</em> attribute
784
- of type text. Those attributes are automatically added to the <code>articles</code>
785
- table in the database and mapped to the <code>Article</code> model.</p><p>Rails responded by creating a bunch of files. For now, we're only interested
786
- in <code>app/models/article.rb</code> and <code>db/migrate/20140120191729_create_articles.rb</code>
787
- (your name could be a bit different). The latter is responsible for creating
788
- the database structure, which is what we'll look at next.</p><div class="info"><p>Active Record is smart enough to automatically map column names to model
789
- attributes, which means you don't have to declare attributes inside Rails
790
- models, as that will be done automatically by Active Record.</p></div><h4 id="running-a-migration">5.5 Running a Migration</h4><p>As we've just seen, <code>rails generate model</code> created a <em>database migration</em> file
791
- inside the <code>db/migrate</code> directory. Migrations are Ruby classes that are
792
- designed to make it simple to create and modify database tables. Rails uses
793
- rake commands to run migrations, and it's possible to undo a migration after
794
- it's been applied to your database. Migration filenames include a timestamp to
795
- ensure that they're processed in the order that they were created.</p><p>If you look in the <code>db/migrate/20140120191729_create_articles.rb</code> file (remember,
796
- yours will have a slightly different name), here's what you'll find:</p><div class="code_container">
797
- <pre class="brush: ruby; gutter: false; toolbar: false">
798
- class CreateArticles &lt; ActiveRecord::Migration
799
- def change
800
- create_table :articles do |t|
801
- t.string :title
802
- t.text :text
803
-
804
- t.timestamps
805
- end
806
- end
807
- end
808
-
809
- </pre>
810
- </div>
811
- <p>The above migration creates a method named <code>change</code> which will be called when
812
- you run this migration. The action defined in this method is also reversible,
813
- which means Rails knows how to reverse the change made by this migration,
814
- in case you want to reverse it later. When you run this migration it will create
815
- an <code>articles</code> table with one string column and a text column. It also creates
816
- two timestamp fields to allow Rails to track article creation and update times.</p><div class="info"><p>For more information about migrations, refer to <a href="migrations.html">Rails Database Migrations</a>.</p></div><p>At this point, you can use a rake command to run the migration:</p><div class="code_container">
817
- <pre class="brush: plain; gutter: false; toolbar: false">
818
- $ bin/rake db:migrate
819
-
820
- </pre>
821
- </div>
822
- <p>Rails will execute this migration command and tell you it created the Articles
823
- table.</p><div class="code_container">
824
- <pre class="brush: plain; gutter: false; toolbar: false">
825
- == CreateArticles: migrating ==================================================
826
- -- create_table(:articles)
827
- -&gt; 0.0019s
828
- == CreateArticles: migrated (0.0020s) =========================================
829
-
830
- </pre>
831
- </div>
832
- <div class="note"><p>Because you're working in the development environment by default, this
833
- command will apply to the database defined in the <code>development</code> section of your
834
- <code>config/database.yml</code> file. If you would like to execute migrations in another
835
- environment, for instance in production, you must explicitly pass it when
836
- invoking the command: <code>rake db:migrate RAILS_ENV=production</code>.</p></div><h4 id="saving-data-in-the-controller">5.6 Saving data in the controller</h4><p>Back in <code>ArticlesController</code>, we need to change the <code>create</code> action
837
- to use the new <code>Article</code> model to save the data in the database.
838
- Open <code>app/controllers/articles_controller.rb</code> and change the <code>create</code> action to
839
- look like this:</p><div class="code_container">
840
- <pre class="brush: ruby; gutter: false; toolbar: false">
841
- def create
842
- @article = Article.new(params[:article])
843
-
844
- @article.save
845
- redirect_to @article
846
- end
847
-
848
- </pre>
849
- </div>
850
- <p>Here's what's going on: every Rails model can be initialized with its
851
- respective attributes, which are automatically mapped to the respective
852
- database columns. In the first line we do just that (remember that
853
- <code>params[:article]</code> contains the attributes we're interested in). Then,
854
- <code>@article.save</code> is responsible for saving the model in the database. Finally,
855
- we redirect the user to the <code>show</code> action, which we'll define later.</p><div class="info"><p>You might be wondering why the <code>A</code> in <code>Article.new</code> is capitalized above, whereas most other references to articles in this guide have used lowercase. In this context, we are referring to the class named <code>Article</code> that is defined in <code>\models\article.rb</code>. Class names in Ruby must begin with a capital letter.</p></div><div class="info"><p>As we'll see later, <code>@article.save</code> returns a boolean indicating whether
856
- the article was saved or not.</p></div><p>If you now go to <a href="http://localhost:3000/articles/new">http://localhost:3000/articles/new</a> you'll <em>almost</em> be able
857
- to create an article. Try it! You should get an error that looks like this:</p><p><img src="images/getting_started/forbidden_attributes_for_new_article.png" alt="Forbidden attributes for new article"></p><p>Rails has several security features that help you write secure applications,
858
- and you're running into one of them now. This one is called <a href="action_controller_overview.html#strong-parameters">strong parameters</a>,
859
- which requires us to tell Rails exactly which parameters are allowed into our
860
- controller actions.</p><p>Why do you have to bother? The ability to grab and automatically assign all
861
- controller parameters to your model in one shot makes the programmer's job
862
- easier, but this convenience also allows malicious use. What if a request to
863
- the server was crafted to look like a new article form submit but also included
864
- extra fields with values that violated your applications integrity? They would
865
- be 'mass assigned' into your model and then into the database along with the
866
- good stuff - potentially breaking your application or worse.</p><p>We have to whitelist our controller parameters to prevent wrongful mass
867
- assignment. In this case, we want to both allow and require the <code>title</code> and
868
- <code>text</code> parameters for valid use of <code>create</code>. The syntax for this introduces
869
- <code>require</code> and <code>permit</code>. The change will involve one line in the <code>create</code> action:</p><div class="code_container">
870
- <pre class="brush: ruby; gutter: false; toolbar: false">
871
- @article = Article.new(params.require(:article).permit(:title, :text))
872
-
873
- </pre>
874
- </div>
875
- <p>This is often factored out into its own method so it can be reused by multiple
876
- actions in the same controller, for example <code>create</code> and <code>update</code>. Above and
877
- beyond mass assignment issues, the method is often made <code>private</code> to make sure
878
- it can't be called outside its intended context. Here is the result:</p><div class="code_container">
879
- <pre class="brush: ruby; gutter: false; toolbar: false">
880
- def create
881
- @article = Article.new(article_params)
882
-
883
- @article.save
884
- redirect_to @article
885
- end
886
-
887
- private
888
- def article_params
889
- params.require(:article).permit(:title, :text)
890
- end
891
-
892
- </pre>
893
- </div>
894
- <div class="info"><p>For more information, refer to the reference above and
895
- <a href="http://weblog.rubyonrails.org/2012/3/21/strong-parameters/">this blog article about Strong Parameters</a>.</p></div><h4 id="showing-articles">5.7 Showing Articles</h4><p>If you submit the form again now, Rails will complain about not finding the
896
- <code>show</code> action. That's not very useful though, so let's add the <code>show</code> action
897
- before proceeding.</p><p>As we have seen in the output of <code>rake routes</code>, the route for <code>show</code> action is
898
- as follows:</p><div class="code_container">
899
- <pre class="brush: plain; gutter: false; toolbar: false">
900
- article GET /articles/:id(.:format) articles#show
901
-
902
- </pre>
903
- </div>
904
- <p>The special syntax <code>:id</code> tells rails that this route expects an <code>:id</code>
905
- parameter, which in our case will be the id of the article.</p><p>As we did before, we need to add the <code>show</code> action in
906
- <code>app/controllers/articles_controller.rb</code> and its respective view.</p><div class="note"><p>A frequent practice is to place the standard CRUD actions in each
907
- controller in the following order: <code>index</code>, <code>show</code>, <code>new</code>, <code>edit</code>, <code>create</code>, <code>update</code>
908
- and <code>destroy</code>. You may use any order you choose, but keep in mind that these
909
- are public methods; as mentioned earlier in this guide, they must be placed
910
- before any private or protected method in the controller in order to work.</p></div><p>Given that, let's add the <code>show</code> action, as follows:</p><div class="code_container">
911
- <pre class="brush: ruby; gutter: false; toolbar: false">
912
- class ArticlesController &lt; ApplicationController
913
- def show
914
- @article = Article.find(params[:id])
915
- end
916
-
917
- def new
918
- end
919
-
920
- # snipped for brevity
921
-
922
- </pre>
923
- </div>
924
- <p>A couple of things to note. We use <code>Article.find</code> to find the article we're
925
- interested in, passing in <code>params[:id]</code> to get the <code>:id</code> parameter from the
926
- request. We also use an instance variable (prefixed by <code>@</code>) to hold a
927
- reference to the article object. We do this because Rails will pass all instance
928
- variables to the view.</p><p>Now, create a new file <code>app/views/articles/show.html.erb</code> with the following
929
- content:</p><div class="code_container">
930
- <pre class="brush: plain; gutter: false; toolbar: false">
931
- &lt;p&gt;
932
- &lt;strong&gt;Title:&lt;/strong&gt;
933
- &lt;%= @article.title %&gt;
934
- &lt;/p&gt;
935
-
936
- &lt;p&gt;
937
- &lt;strong&gt;Text:&lt;/strong&gt;
938
- &lt;%= @article.text %&gt;
939
- &lt;/p&gt;
940
-
941
- </pre>
942
- </div>
943
- <p>With this change, you should finally be able to create new articles.
944
- Visit <a href="http://localhost:3000/articles/new">http://localhost:3000/articles/new</a> and give it a try!</p><p><img src="images/getting_started/show_action_for_articles.png" alt="Show action for articles"></p><h4 id="listing-all-articles">5.8 Listing all articles</h4><p>We still need a way to list all our articles, so let's do that.
945
- The route for this as per output of <code>rake routes</code> is:</p><div class="code_container">
946
- <pre class="brush: plain; gutter: false; toolbar: false">
947
- articles GET /articles(.:format) articles#index
948
-
949
- </pre>
950
- </div>
951
- <p>Add the corresponding <code>index</code> action for that route inside the
952
- <code>ArticlesController</code> in the <code>app/controllers/articles_controller.rb</code> file.
953
- When we write an <code>index</code> action, the usual practice is to place it as the
954
- first method in the controller. Let's do it:</p><div class="code_container">
955
- <pre class="brush: ruby; gutter: false; toolbar: false">
956
- class ArticlesController &lt; ApplicationController
957
- def index
958
- @articles = Article.all
959
- end
960
-
961
- def show
962
- @article = Article.find(params[:id])
963
- end
964
-
965
- def new
966
- end
967
-
968
- # snipped for brevity
969
-
970
- </pre>
971
- </div>
972
- <p>And then finally, add the view for this action, located at
973
- <code>app/views/articles/index.html.erb</code>:</p><div class="code_container">
974
- <pre class="brush: plain; gutter: false; toolbar: false">
975
- &lt;h1&gt;Listing articles&lt;/h1&gt;
976
-
977
- &lt;table&gt;
978
- &lt;tr&gt;
979
- &lt;th&gt;Title&lt;/th&gt;
980
- &lt;th&gt;Text&lt;/th&gt;
981
- &lt;/tr&gt;
982
-
983
- &lt;% @articles.each do |article| %&gt;
984
- &lt;tr&gt;
985
- &lt;td&gt;&lt;%= article.title %&gt;&lt;/td&gt;
986
- &lt;td&gt;&lt;%= article.text %&gt;&lt;/td&gt;
987
- &lt;/tr&gt;
988
- &lt;% end %&gt;
989
- &lt;/table&gt;
990
-
991
- </pre>
992
- </div>
993
- <p>Now if you go to <a href="http://localhost:3000/articles">http://localhost:3000/articles</a> you will see a list of all the
994
- articles that you have created.</p><h4 id="adding-links">5.9 Adding links</h4><p>You can now create, show, and list articles. Now let's add some links to
995
- navigate through pages.</p><p>Open <code>app/views/welcome/index.html.erb</code> and modify it as follows:</p><div class="code_container">
996
- <pre class="brush: plain; gutter: false; toolbar: false">
997
- &lt;h1&gt;Hello, Rails!&lt;/h1&gt;
998
- &lt;%= link_to 'My Blog', controller: 'articles' %&gt;
999
-
1000
- </pre>
1001
- </div>
1002
- <p>The <code>link_to</code> method is one of Rails' built-in view helpers. It creates a
1003
- hyperlink based on text to display and where to go - in this case, to the path
1004
- for articles.</p><p>Let's add links to the other views as well, starting with adding this
1005
- "New Article" link to <code>app/views/articles/index.html.erb</code>, placing it above the
1006
- <code>&lt;table&gt;</code> tag:</p><div class="code_container">
1007
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1008
- &lt;%= link_to 'New article', new_article_path %&gt;
1009
-
1010
- </pre>
1011
- </div>
1012
- <p>This link will allow you to bring up the form that lets you create a new article.</p><p>Now, add another link in <code>app/views/articles/new.html.erb</code>, underneath the
1013
- form, to go back to the <code>index</code> action:</p><div class="code_container">
1014
- <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
1015
- &lt;%= form_for :article, url: articles_path do |f| %&gt;
1016
- ...
1017
- &lt;% end %&gt;
1018
-
1019
- &lt;%= link_to 'Back', articles_path %&gt;
1020
-
1021
- </pre>
1022
- </div>
1023
- <p>Finally, add a link to the <code>app/views/articles/show.html.erb</code> template to
1024
- go back to the <code>index</code> action as well, so that people who are viewing a single
1025
- article can go back and view the whole list again:</p><div class="code_container">
1026
- <pre class="brush: plain; gutter: false; toolbar: false">
1027
- &lt;p&gt;
1028
- &lt;strong&gt;Title:&lt;/strong&gt;
1029
- &lt;%= @article.title %&gt;
1030
- &lt;/p&gt;
1031
-
1032
- &lt;p&gt;
1033
- &lt;strong&gt;Text:&lt;/strong&gt;
1034
- &lt;%= @article.text %&gt;
1035
- &lt;/p&gt;
1036
-
1037
- &lt;%= link_to 'Back', articles_path %&gt;
1038
-
1039
- </pre>
1040
- </div>
1041
- <div class="info"><p>If you want to link to an action in the same controller, you don't need to
1042
- specify the <code>:controller</code> option, as Rails will use the current controller by
1043
- default.</p></div><div class="info"><p>In development mode (which is what you're working in by default), Rails
1044
- reloads your application with every browser request, so there's no need to stop
1045
- and restart the web server when a change is made.</p></div><h4 id="adding-some-validation">5.10 Adding Some Validation</h4><p>The model file, <code>app/models/article.rb</code> is about as simple as it can get:</p><div class="code_container">
1046
- <pre class="brush: ruby; gutter: false; toolbar: false">
1047
- class Article &lt; ActiveRecord::Base
1048
- end
1049
-
1050
- </pre>
1051
- </div>
1052
- <p>There isn't much to this file - but note that the <code>Article</code> class inherits from
1053
- <code>ActiveRecord::Base</code>. Active Record supplies a great deal of functionality to
1054
- your Rails models for free, including basic database CRUD (Create, Read, Update,
1055
- Destroy) operations, data validation, as well as sophisticated search support
1056
- and the ability to relate multiple models to one another.</p><p>Rails includes methods to help you validate the data that you send to models.
1057
- Open the <code>app/models/article.rb</code> file and edit it:</p><div class="code_container">
1058
- <pre class="brush: ruby; gutter: false; toolbar: false">
1059
- class Article &lt; ActiveRecord::Base
1060
- validates :title, presence: true,
1061
- length: { minimum: 5 }
1062
- end
1063
-
1064
- </pre>
1065
- </div>
1066
- <p>These changes will ensure that all articles have a title that is at least five
1067
- characters long. Rails can validate a variety of conditions in a model,
1068
- including the presence or uniqueness of columns, their format, and the
1069
- existence of associated objects. Validations are covered in detail in <a href="active_record_validations.html">Active
1070
- Record Validations</a>.</p><p>With the validation now in place, when you call <code>@article.save</code> on an invalid
1071
- article, it will return <code>false</code>. If you open
1072
- <code>app/controllers/articles_controller.rb</code> again, you'll notice that we don't
1073
- check the result of calling <code>@article.save</code> inside the <code>create</code> action.
1074
- If <code>@article.save</code> fails in this situation, we need to show the form back to the
1075
- user. To do this, change the <code>new</code> and <code>create</code> actions inside
1076
- <code>app/controllers/articles_controller.rb</code> to these:</p><div class="code_container">
1077
- <pre class="brush: ruby; gutter: false; toolbar: false">
1078
- def new
1079
- @article = Article.new
1080
- end
1081
-
1082
- def create
1083
- @article = Article.new(article_params)
1084
-
1085
- if @article.save
1086
- redirect_to @article
1087
- else
1088
- render 'new'
1089
- end
1090
- end
1091
-
1092
- private
1093
- def article_params
1094
- params.require(:article).permit(:title, :text)
1095
- end
1096
-
1097
- </pre>
1098
- </div>
1099
- <p>The <code>new</code> action is now creating a new instance variable called <code>@article</code>, and
1100
- you'll see why that is in just a few moments.</p><p>Notice that inside the <code>create</code> action we use <code>render</code> instead of <code>redirect_to</code>
1101
- when <code>save</code> returns <code>false</code>. The <code>render</code> method is used so that the <code>@article</code>
1102
- object is passed back to the <code>new</code> template when it is rendered. This rendering
1103
- is done within the same request as the form submission, whereas the
1104
- <code>redirect_to</code> will tell the browser to issue another request.</p><p>If you reload
1105
- <a href="http://localhost:3000/articles/new">http://localhost:3000/articles/new</a> and
1106
- try to save an article without a title, Rails will send you back to the
1107
- form, but that's not very useful. You need to tell the user that
1108
- something went wrong. To do that, you'll modify
1109
- <code>app/views/articles/new.html.erb</code> to check for error messages:</p><div class="code_container">
1110
- <pre class="brush: plain; gutter: false; toolbar: false">
1111
- &lt;%= form_for :article, url: articles_path do |f| %&gt;
1112
-
1113
- &lt;% if @article.errors.any? %&gt;
1114
- &lt;div id="error_explanation"&gt;
1115
- &lt;h2&gt;
1116
- &lt;%= pluralize(@article.errors.count, "error") %&gt; prohibited
1117
- this article from being saved:
1118
- &lt;/h2&gt;
1119
- &lt;ul&gt;
1120
- &lt;% @article.errors.full_messages.each do |msg| %&gt;
1121
- &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
1122
- &lt;% end %&gt;
1123
- &lt;/ul&gt;
1124
- &lt;/div&gt;
1125
- &lt;% end %&gt;
1126
-
1127
- &lt;p&gt;
1128
- &lt;%= f.label :title %&gt;&lt;br&gt;
1129
- &lt;%= f.text_field :title %&gt;
1130
- &lt;/p&gt;
1131
-
1132
- &lt;p&gt;
1133
- &lt;%= f.label :text %&gt;&lt;br&gt;
1134
- &lt;%= f.text_area :text %&gt;
1135
- &lt;/p&gt;
1136
-
1137
- &lt;p&gt;
1138
- &lt;%= f.submit %&gt;
1139
- &lt;/p&gt;
1140
-
1141
- &lt;% end %&gt;
1142
-
1143
- &lt;%= link_to 'Back', articles_path %&gt;
1144
-
1145
- </pre>
1146
- </div>
1147
- <p>A few things are going on. We check if there are any errors with
1148
- <code>@article.errors.any?</code>, and in that case we show a list of all
1149
- errors with <code>@article.errors.full_messages</code>.</p><p><code>pluralize</code> is a rails helper that takes a number and a string as its
1150
- arguments. If the number is greater than one, the string will be automatically
1151
- pluralized.</p><p>The reason why we added <code>@article = Article.new</code> in the <code>ArticlesController</code> is
1152
- that otherwise <code>@article</code> would be <code>nil</code> in our view, and calling
1153
- <code>@article.errors.any?</code> would throw an error.</p><div class="info"><p>Rails automatically wraps fields that contain an error with a div
1154
- with class <code>field_with_errors</code>. You can define a css rule to make them
1155
- standout.</p></div><p>Now you'll get a nice error message when saving an article without title when
1156
- you attempt to do just that on the new article form
1157
- <a href="http://localhost:3000/articles/new">http://localhost:3000/articles/new</a>:</p><p><img src="images/getting_started/form_with_errors.png" alt="Form With Errors"></p><h4 id="updating-articles">5.11 Updating Articles</h4><p>We've covered the "CR" part of CRUD. Now let's focus on the "U" part, updating
1158
- articles.</p><p>The first step we'll take is adding an <code>edit</code> action to the <code>ArticlesController</code>,
1159
- generally between the <code>new</code> and <code>create</code> actions, as shown:</p><div class="code_container">
1160
- <pre class="brush: ruby; gutter: false; toolbar: false">
1161
- def new
1162
- @article = Article.new
1163
- end
1164
-
1165
- def edit
1166
- @article = Article.find(params[:id])
1167
- end
1168
-
1169
- def create
1170
- @article = Article.new(article_params)
1171
-
1172
- if @article.save
1173
- redirect_to @article
1174
- else
1175
- render 'new'
1176
- end
1177
- end
1178
-
1179
- </pre>
1180
- </div>
1181
- <p>The view will contain a form similar to the one we used when creating
1182
- new articles. Create a file called <code>app/views/articles/edit.html.erb</code> and make
1183
- it look as follows:</p><div class="code_container">
1184
- <pre class="brush: plain; gutter: false; toolbar: false">
1185
- &lt;h1&gt;Editing article&lt;/h1&gt;
1186
-
1187
- &lt;%= form_for :article, url: article_path(@article), method: :patch do |f| %&gt;
1188
-
1189
- &lt;% if @article.errors.any? %&gt;
1190
- &lt;div id="error_explanation"&gt;
1191
- &lt;h2&gt;
1192
- &lt;%= pluralize(@article.errors.count, "error") %&gt; prohibited
1193
- this article from being saved:
1194
- &lt;/h2&gt;
1195
- &lt;ul&gt;
1196
- &lt;% @article.errors.full_messages.each do |msg| %&gt;
1197
- &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
1198
- &lt;% end %&gt;
1199
- &lt;/ul&gt;
1200
- &lt;/div&gt;
1201
- &lt;% end %&gt;
1202
-
1203
- &lt;p&gt;
1204
- &lt;%= f.label :title %&gt;&lt;br&gt;
1205
- &lt;%= f.text_field :title %&gt;
1206
- &lt;/p&gt;
1207
-
1208
- &lt;p&gt;
1209
- &lt;%= f.label :text %&gt;&lt;br&gt;
1210
- &lt;%= f.text_area :text %&gt;
1211
- &lt;/p&gt;
1212
-
1213
- &lt;p&gt;
1214
- &lt;%= f.submit %&gt;
1215
- &lt;/p&gt;
1216
-
1217
- &lt;% end %&gt;
1218
-
1219
- &lt;%= link_to 'Back', articles_path %&gt;
1220
-
1221
- </pre>
1222
- </div>
1223
- <p>This time we point the form to the <code>update</code> action, which is not defined yet
1224
- but will be very soon.</p><p>The <code>method: :patch</code> option tells Rails that we want this form to be submitted
1225
- via the <code>PATCH</code> HTTP method which is the HTTP method you're expected to use to
1226
- <strong>update</strong> resources according to the REST protocol.</p><p>The first parameter of <code>form_for</code> can be an object, say, <code>@article</code> which would
1227
- cause the helper to fill in the form with the fields of the object. Passing in a
1228
- symbol (<code>:article</code>) with the same name as the instance variable (<code>@article</code>)
1229
- also automagically leads to the same behavior. This is what is happening here.
1230
- More details can be found in <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for">form_for documentation</a>.</p><p>Next, we need to create the <code>update</code> action in
1231
- <code>app/controllers/articles_controller.rb</code>.
1232
- Add it between the <code>create</code> action and the <code>private</code> method:</p><div class="code_container">
1233
- <pre class="brush: ruby; gutter: false; toolbar: false">
1234
- def create
1235
- @article = Article.new(article_params)
1236
-
1237
- if @article.save
1238
- redirect_to @article
1239
- else
1240
- render 'new'
1241
- end
1242
- end
1243
-
1244
- def update
1245
- @article = Article.find(params[:id])
1246
-
1247
- if @article.update(article_params)
1248
- redirect_to @article
1249
- else
1250
- render 'edit'
1251
- end
1252
- end
1253
-
1254
- private
1255
- def article_params
1256
- params.require(:article).permit(:title, :text)
1257
- end
1258
-
1259
- </pre>
1260
- </div>
1261
- <p>The new method, <code>update</code>, is used when you want to update a record
1262
- that already exists, and it accepts a hash containing the attributes
1263
- that you want to update. As before, if there was an error updating the
1264
- article we want to show the form back to the user.</p><p>We reuse the <code>article_params</code> method that we defined earlier for the create
1265
- action.</p><div class="info"><p>You don't need to pass all attributes to <code>update</code>. For
1266
- example, if you'd call <code>@article.update(title: 'A new title')</code>
1267
- Rails would only update the <code>title</code> attribute, leaving all other
1268
- attributes untouched.</p></div><p>Finally, we want to show a link to the <code>edit</code> action in the list of all the
1269
- articles, so let's add that now to <code>app/views/articles/index.html.erb</code> to make
1270
- it appear next to the "Show" link:</p><div class="code_container">
1271
- <pre class="brush: plain; gutter: false; toolbar: false">
1272
- &lt;table&gt;
1273
- &lt;tr&gt;
1274
- &lt;th&gt;Title&lt;/th&gt;
1275
- &lt;th&gt;Text&lt;/th&gt;
1276
- &lt;th colspan="2"&gt;&lt;/th&gt;
1277
- &lt;/tr&gt;
1278
-
1279
- &lt;% @articles.each do |article| %&gt;
1280
- &lt;tr&gt;
1281
- &lt;td&gt;&lt;%= article.title %&gt;&lt;/td&gt;
1282
- &lt;td&gt;&lt;%= article.text %&gt;&lt;/td&gt;
1283
- &lt;td&gt;&lt;%= link_to 'Show', article_path(article) %&gt;&lt;/td&gt;
1284
- &lt;td&gt;&lt;%= link_to 'Edit', edit_article_path(article) %&gt;&lt;/td&gt;
1285
- &lt;/tr&gt;
1286
- &lt;% end %&gt;
1287
- &lt;/table&gt;
1288
-
1289
- </pre>
1290
- </div>
1291
- <p>And we'll also add one to the <code>app/views/articles/show.html.erb</code> template as
1292
- well, so that there's also an "Edit" link on an article's page. Add this at the
1293
- bottom of the template:</p><div class="code_container">
1294
- <pre class="brush: plain; gutter: false; toolbar: false">
1295
- ...
1296
-
1297
- &lt;%= link_to 'Back', articles_path %&gt; |
1298
- &lt;%= link_to 'Edit', edit_article_path(@article) %&gt;
1299
-
1300
- </pre>
1301
- </div>
1302
- <p>And here's how our app looks so far:</p><p><img src="images/getting_started/index_action_with_edit_link.png" alt="Index action with edit link"></p><h4 id="using-partials-to-clean-up-duplication-in-views">5.12 Using partials to clean up duplication in views</h4><p>Our <code>edit</code> page looks very similar to the <code>new</code> page; in fact, they
1303
- both share the same code for displaying the form. Let's remove this
1304
- duplication by using a view partial. By convention, partial files are
1305
- prefixed by an underscore.</p><div class="info"><p>You can read more about partials in the
1306
- <a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a> guide.</p></div><p>Create a new file <code>app/views/articles/_form.html.erb</code> with the following
1307
- content:</p><div class="code_container">
1308
- <pre class="brush: plain; gutter: false; toolbar: false">
1309
- &lt;%= form_for @article do |f| %&gt;
1310
-
1311
- &lt;% if @article.errors.any? %&gt;
1312
- &lt;div id="error_explanation"&gt;
1313
- &lt;h2&gt;
1314
- &lt;%= pluralize(@article.errors.count, "error") %&gt; prohibited
1315
- this article from being saved:
1316
- &lt;/h2&gt;
1317
- &lt;ul&gt;
1318
- &lt;% @article.errors.full_messages.each do |msg| %&gt;
1319
- &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
1320
- &lt;% end %&gt;
1321
- &lt;/ul&gt;
1322
- &lt;/div&gt;
1323
- &lt;% end %&gt;
1324
-
1325
- &lt;p&gt;
1326
- &lt;%= f.label :title %&gt;&lt;br&gt;
1327
- &lt;%= f.text_field :title %&gt;
1328
- &lt;/p&gt;
1329
-
1330
- &lt;p&gt;
1331
- &lt;%= f.label :text %&gt;&lt;br&gt;
1332
- &lt;%= f.text_area :text %&gt;
1333
- &lt;/p&gt;
1334
-
1335
- &lt;p&gt;
1336
- &lt;%= f.submit %&gt;
1337
- &lt;/p&gt;
1338
-
1339
- &lt;% end %&gt;
1340
-
1341
- </pre>
1342
- </div>
1343
- <p>Everything except for the <code>form_for</code> declaration remained the same.
1344
- The reason we can use this shorter, simpler <code>form_for</code> declaration
1345
- to stand in for either of the other forms is that <code>@article</code> is a <em>resource</em>
1346
- corresponding to a full set of RESTful routes, and Rails is able to infer
1347
- which URI and method to use.
1348
- For more information about this use of <code>form_for</code>, see <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for-label-Resource-oriented+style">Resource-oriented style</a>.</p><p>Now, let's update the <code>app/views/articles/new.html.erb</code> view to use this new
1349
- partial, rewriting it completely:</p><div class="code_container">
1350
- <pre class="brush: plain; gutter: false; toolbar: false">
1351
- &lt;h1&gt;New article&lt;/h1&gt;
1352
-
1353
- &lt;%= render 'form' %&gt;
1354
-
1355
- &lt;%= link_to 'Back', articles_path %&gt;
1356
-
1357
- </pre>
1358
- </div>
1359
- <p>Then do the same for the <code>app/views/articles/edit.html.erb</code> view:</p><div class="code_container">
1360
- <pre class="brush: plain; gutter: false; toolbar: false">
1361
- &lt;h1&gt;Edit article&lt;/h1&gt;
1362
-
1363
- &lt;%= render 'form' %&gt;
1364
-
1365
- &lt;%= link_to 'Back', articles_path %&gt;
1366
-
1367
- </pre>
1368
- </div>
1369
- <h4 id="deleting-articles">5.13 Deleting Articles</h4><p>We're now ready to cover the "D" part of CRUD, deleting articles from the
1370
- database. Following the REST convention, the route for
1371
- deleting articles as per output of <code>rake routes</code> is:</p><div class="code_container">
1372
- <pre class="brush: ruby; gutter: false; toolbar: false">
1373
- DELETE /articles/:id(.:format) articles#destroy
1374
-
1375
- </pre>
1376
- </div>
1377
- <p>The <code>delete</code> routing method should be used for routes that destroy
1378
- resources. If this was left as a typical <code>get</code> route, it could be possible for
1379
- people to craft malicious URLs like this:</p><div class="code_container">
1380
- <pre class="brush: xml; gutter: false; toolbar: false">
1381
- &lt;a href='http://example.com/articles/1/destroy'&gt;look at this cat!&lt;/a&gt;
1382
-
1383
- </pre>
1384
- </div>
1385
- <p>We use the <code>delete</code> method for destroying resources, and this route is mapped
1386
- to the <code>destroy</code> action inside <code>app/controllers/articles_controller.rb</code>, which
1387
- doesn't exist yet. The <code>destroy</code> method is generally the last CRUD action in
1388
- the controller, and like the other public CRUD actions, it must be placed
1389
- before any <code>private</code> or <code>protected</code> methods. Let's add it:</p><div class="code_container">
1390
- <pre class="brush: ruby; gutter: false; toolbar: false">
1391
- def destroy
1392
- @article = Article.find(params[:id])
1393
- @article.destroy
1394
-
1395
- redirect_to articles_path
1396
- end
1397
-
1398
- </pre>
1399
- </div>
1400
- <p>The complete <code>ArticlesController</code> in the
1401
- <code>app/controllers/articles_controller.rb</code> file should now look like this:</p><div class="code_container">
1402
- <pre class="brush: ruby; gutter: false; toolbar: false">
1403
- class ArticlesController &lt; ApplicationController
1404
- def index
1405
- @articles = Article.all
1406
- end
1407
-
1408
- def show
1409
- @article = Article.find(params[:id])
1410
- end
1411
-
1412
- def new
1413
- @article = Article.new
1414
- end
1415
-
1416
- def edit
1417
- @article = Article.find(params[:id])
1418
- end
1419
-
1420
- def create
1421
- @article = Article.new(article_params)
1422
-
1423
- if @article.save
1424
- redirect_to @article
1425
- else
1426
- render 'new'
1427
- end
1428
- end
1429
-
1430
- def update
1431
- @article = Article.find(params[:id])
1432
-
1433
- if @article.update(article_params)
1434
- redirect_to @article
1435
- else
1436
- render 'edit'
1437
- end
1438
- end
1439
-
1440
- def destroy
1441
- @article = Article.find(params[:id])
1442
- @article.destroy
1443
-
1444
- redirect_to articles_path
1445
- end
1446
-
1447
- private
1448
- def article_params
1449
- params.require(:article).permit(:title, :text)
1450
- end
1451
- end
1452
-
1453
- </pre>
1454
- </div>
1455
- <p>You can call <code>destroy</code> on Active Record objects when you want to delete
1456
- them from the database. Note that we don't need to add a view for this
1457
- action since we're redirecting to the <code>index</code> action.</p><p>Finally, add a 'Destroy' link to your <code>index</code> action template
1458
- (<code>app/views/articles/index.html.erb</code>) to wrap everything together.</p><div class="code_container">
1459
- <pre class="brush: plain; gutter: false; toolbar: false">
1460
- &lt;h1&gt;Listing Articles&lt;/h1&gt;
1461
- &lt;%= link_to 'New article', new_article_path %&gt;
1462
- &lt;table&gt;
1463
- &lt;tr&gt;
1464
- &lt;th&gt;Title&lt;/th&gt;
1465
- &lt;th&gt;Text&lt;/th&gt;
1466
- &lt;th colspan="3"&gt;&lt;/th&gt;
1467
- &lt;/tr&gt;
1468
-
1469
- &lt;% @articles.each do |article| %&gt;
1470
- &lt;tr&gt;
1471
- &lt;td&gt;&lt;%= article.title %&gt;&lt;/td&gt;
1472
- &lt;td&gt;&lt;%= article.text %&gt;&lt;/td&gt;
1473
- &lt;td&gt;&lt;%= link_to 'Show', article_path(article) %&gt;&lt;/td&gt;
1474
- &lt;td&gt;&lt;%= link_to 'Edit', edit_article_path(article) %&gt;&lt;/td&gt;
1475
- &lt;td&gt;&lt;%= link_to 'Destroy', article_path(article),
1476
- method: :delete,
1477
- data: { confirm: 'Are you sure?' } %&gt;&lt;/td&gt;
1478
- &lt;/tr&gt;
1479
- &lt;% end %&gt;
1480
- &lt;/table&gt;
1481
-
1482
- </pre>
1483
- </div>
1484
- <p>Here we're using <code>link_to</code> in a different way. We pass the named route as the
1485
- second argument, and then the options as another argument. The <code>:method</code> and
1486
- <code>:'data-confirm'</code> options are used as HTML5 attributes so that when the link is
1487
- clicked, Rails will first show a confirm dialog to the user, and then submit the
1488
- link with method <code>delete</code>. This is done via the JavaScript file <code>jquery_ujs</code>
1489
- which is automatically included into your application's layout
1490
- (<code>app/views/layouts/application.html.erb</code>) when you generated the application.
1491
- Without this file, the confirmation dialog box wouldn't appear.</p><p><img src="images/getting_started/confirm_dialog.png" alt="Confirm Dialog"></p><p>Congratulations, you can now create, show, list, update and destroy
1492
- articles.</p><div class="info"><p>In general, Rails encourages using resources objects instead of
1493
- declaring routes manually. For more information about routing, see
1494
- <a href="routing.html">Rails Routing from the Outside In</a>.</p></div><h3 id="adding-a-second-model">6 Adding a Second Model</h3><p>It's time to add a second model to the application. The second model will handle
1495
- comments on articles.</p><h4 id="generating-a-model">6.1 Generating a Model</h4><p>We're going to see the same generator that we used before when creating
1496
- the <code>Article</code> model. This time we'll create a <code>Comment</code> model to hold
1497
- reference of article comments. Run this command in your terminal:</p><div class="code_container">
1498
- <pre class="brush: plain; gutter: false; toolbar: false">
1499
- $ bin/rails generate model Comment commenter:string body:text article:references
1500
-
1501
- </pre>
1502
- </div>
1503
- <p>This command will generate four files:</p>
1504
- <table>
1505
- <thead>
1506
- <tr>
1507
- <th>File</th>
1508
- <th>Purpose</th>
1509
- </tr>
1510
- </thead>
1511
- <tbody>
1512
- <tr>
1513
- <td>db/migrate/20140120201010_create_comments.rb</td>
1514
- <td>Migration to create the comments table in your database (your name will include a different timestamp)</td>
1515
- </tr>
1516
- <tr>
1517
- <td>app/models/comment.rb</td>
1518
- <td>The Comment model</td>
1519
- </tr>
1520
- <tr>
1521
- <td>test/models/comment_test.rb</td>
1522
- <td>Testing harness for the comments model</td>
1523
- </tr>
1524
- <tr>
1525
- <td>test/fixtures/comments.yml</td>
1526
- <td>Sample comments for use in testing</td>
1527
- </tr>
1528
- </tbody>
1529
- </table>
1530
- <p>First, take a look at <code>app/models/comment.rb</code>:</p><div class="code_container">
1531
- <pre class="brush: ruby; gutter: false; toolbar: false">
1532
- class Comment &lt; ActiveRecord::Base
1533
- belongs_to :article
1534
- end
1535
-
1536
- </pre>
1537
- </div>
1538
- <p>This is very similar to the <code>Article</code> model that you saw earlier. The difference
1539
- is the line <code>belongs_to :article</code>, which sets up an Active Record <em>association</em>.
1540
- You'll learn a little about associations in the next section of this guide.</p><p>In addition to the model, Rails has also made a migration to create the
1541
- corresponding database table:</p><div class="code_container">
1542
- <pre class="brush: ruby; gutter: false; toolbar: false">
1543
- class CreateComments &lt; ActiveRecord::Migration
1544
- def change
1545
- create_table :comments do |t|
1546
- t.string :commenter
1547
- t.text :body
1548
-
1549
- # this line adds an integer column called `article_id`.
1550
- t.references :article, index: true
1551
-
1552
- t.timestamps
1553
- end
1554
- end
1555
- end
1556
-
1557
- </pre>
1558
- </div>
1559
- <p>The <code>t.references</code> line sets up a foreign key column for the association between
1560
- the two models. An index for this association is also created on this column.
1561
- Go ahead and run the migration:</p><div class="code_container">
1562
- <pre class="brush: plain; gutter: false; toolbar: false">
1563
- $ bin/rake db:migrate
1564
-
1565
- </pre>
1566
- </div>
1567
- <p>Rails is smart enough to only execute the migrations that have not already been
1568
- run against the current database, so in this case you will just see:</p><div class="code_container">
1569
- <pre class="brush: plain; gutter: false; toolbar: false">
1570
- == CreateComments: migrating =================================================
1571
- -- create_table(:comments)
1572
- -&gt; 0.0115s
1573
- == CreateComments: migrated (0.0119s) ========================================
1574
-
1575
- </pre>
1576
- </div>
1577
- <h4 id="associating-models">6.2 Associating Models</h4><p>Active Record associations let you easily declare the relationship between two
1578
- models. In the case of comments and articles, you could write out the
1579
- relationships this way:</p>
1580
- <ul>
1581
- <li>Each comment belongs to one article.</li>
1582
- <li>One article can have many comments.</li>
1583
- </ul>
1584
- <p>In fact, this is very close to the syntax that Rails uses to declare this
1585
- association. You've already seen the line of code inside the <code>Comment</code> model
1586
- (app/models/comment.rb) that makes each comment belong to an Article:</p><div class="code_container">
1587
- <pre class="brush: ruby; gutter: false; toolbar: false">
1588
- class Comment &lt; ActiveRecord::Base
1589
- belongs_to :article
1590
- end
1591
-
1592
- </pre>
1593
- </div>
1594
- <p>You'll need to edit <code>app/models/article.rb</code> to add the other side of the
1595
- association:</p><div class="code_container">
1596
- <pre class="brush: ruby; gutter: false; toolbar: false">
1597
- class Article &lt; ActiveRecord::Base
1598
- has_many :comments
1599
- validates :title, presence: true,
1600
- length: { minimum: 5 }
1601
- end
1602
-
1603
- </pre>
1604
- </div>
1605
- <p>These two declarations enable a good bit of automatic behavior. For example, if
1606
- you have an instance variable <code>@article</code> containing an article, you can retrieve
1607
- all the comments belonging to that article as an array using
1608
- <code>@article.comments</code>.</p><div class="info"><p>For more information on Active Record associations, see the <a href="association_basics.html">Active Record
1609
- Associations</a> guide.</p></div><h4 id="adding-a-route-for-comments">6.3 Adding a Route for Comments</h4><p>As with the <code>welcome</code> controller, we will need to add a route so that Rails
1610
- knows where we would like to navigate to see <code>comments</code>. Open up the
1611
- <code>config/routes.rb</code> file again, and edit it as follows:</p><div class="code_container">
1612
- <pre class="brush: ruby; gutter: false; toolbar: false">
1613
- resources :articles do
1614
- resources :comments
1615
- end
1616
-
1617
- </pre>
1618
- </div>
1619
- <p>This creates <code>comments</code> as a <em>nested resource</em> within <code>articles</code>. This is
1620
- another part of capturing the hierarchical relationship that exists between
1621
- articles and comments.</p><div class="info"><p>For more information on routing, see the <a href="routing.html">Rails Routing</a>
1622
- guide.</p></div><h4 id="generating-a-controller">6.4 Generating a Controller</h4><p>With the model in hand, you can turn your attention to creating a matching
1623
- controller. Again, we'll use the same generator we used before:</p><div class="code_container">
1624
- <pre class="brush: plain; gutter: false; toolbar: false">
1625
- $ bin/rails generate controller Comments
1626
-
1627
- </pre>
1628
- </div>
1629
- <p>This creates six files and one empty directory:</p>
1630
- <table>
1631
- <thead>
1632
- <tr>
1633
- <th>File/Directory</th>
1634
- <th>Purpose</th>
1635
- </tr>
1636
- </thead>
1637
- <tbody>
1638
- <tr>
1639
- <td>app/controllers/comments_controller.rb</td>
1640
- <td>The Comments controller</td>
1641
- </tr>
1642
- <tr>
1643
- <td>app/views/comments/</td>
1644
- <td>Views of the controller are stored here</td>
1645
- </tr>
1646
- <tr>
1647
- <td>test/controllers/comments_controller_test.rb</td>
1648
- <td>The test for the controller</td>
1649
- </tr>
1650
- <tr>
1651
- <td>app/helpers/comments_helper.rb</td>
1652
- <td>A view helper file</td>
1653
- </tr>
1654
- <tr>
1655
- <td>app/assets/javascripts/comment.js.coffee</td>
1656
- <td>CoffeeScript for the controller</td>
1657
- </tr>
1658
- <tr>
1659
- <td>app/assets/stylesheets/comment.css.scss</td>
1660
- <td>Cascading style sheet for the controller</td>
1661
- </tr>
1662
- </tbody>
1663
- </table>
1664
- <p>Like with any blog, our readers will create their comments directly after
1665
- reading the article, and once they have added their comment, will be sent back
1666
- to the article show page to see their comment now listed. Due to this, our
1667
- <code>CommentsController</code> is there to provide a method to create comments and delete
1668
- spam comments when they arrive.</p><p>So first, we'll wire up the Article show template
1669
- (<code>app/views/articles/show.html.erb</code>) to let us make a new comment:</p><div class="code_container">
1670
- <pre class="brush: plain; gutter: false; toolbar: false">
1671
- &lt;p&gt;
1672
- &lt;strong&gt;Title:&lt;/strong&gt;
1673
- &lt;%= @article.title %&gt;
1674
- &lt;/p&gt;
1675
-
1676
- &lt;p&gt;
1677
- &lt;strong&gt;Text:&lt;/strong&gt;
1678
- &lt;%= @article.text %&gt;
1679
- &lt;/p&gt;
1680
-
1681
- &lt;h2&gt;Add a comment:&lt;/h2&gt;
1682
- &lt;%= form_for([@article, @article.comments.build]) do |f| %&gt;
1683
- &lt;p&gt;
1684
- &lt;%= f.label :commenter %&gt;&lt;br&gt;
1685
- &lt;%= f.text_field :commenter %&gt;
1686
- &lt;/p&gt;
1687
- &lt;p&gt;
1688
- &lt;%= f.label :body %&gt;&lt;br&gt;
1689
- &lt;%= f.text_area :body %&gt;
1690
- &lt;/p&gt;
1691
- &lt;p&gt;
1692
- &lt;%= f.submit %&gt;
1693
- &lt;/p&gt;
1694
- &lt;% end %&gt;
1695
-
1696
- &lt;%= link_to 'Back', articles_path %&gt; |
1697
- &lt;%= link_to 'Edit', edit_article_path(@article) %&gt;
1698
-
1699
- </pre>
1700
- </div>
1701
- <p>This adds a form on the <code>Article</code> show page that creates a new comment by
1702
- calling the <code>CommentsController</code> <code>create</code> action. The <code>form_for</code> call here uses
1703
- an array, which will build a nested route, such as <code>/articles/1/comments</code>.</p><p>Let's wire up the <code>create</code> in <code>app/controllers/comments_controller.rb</code>:</p><div class="code_container">
1704
- <pre class="brush: ruby; gutter: false; toolbar: false">
1705
- class CommentsController &lt; ApplicationController
1706
- def create
1707
- @article = Article.find(params[:article_id])
1708
- @comment = @article.comments.create(comment_params)
1709
- redirect_to article_path(@article)
1710
- end
1711
-
1712
- private
1713
- def comment_params
1714
- params.require(:comment).permit(:commenter, :body)
1715
- end
1716
- end
1717
-
1718
- </pre>
1719
- </div>
1720
- <p>You'll see a bit more complexity here than you did in the controller for
1721
- articles. That's a side-effect of the nesting that you've set up. Each request
1722
- for a comment has to keep track of the article to which the comment is attached,
1723
- thus the initial call to the <code>find</code> method of the <code>Article</code> model to get the
1724
- article in question.</p><p>In addition, the code takes advantage of some of the methods available for an
1725
- association. We use the <code>create</code> method on <code>@article.comments</code> to create and
1726
- save the comment. This will automatically link the comment so that it belongs to
1727
- that particular article.</p><p>Once we have made the new comment, we send the user back to the original article
1728
- using the <code>article_path(@article)</code> helper. As we have already seen, this calls
1729
- the <code>show</code> action of the <code>ArticlesController</code> which in turn renders the
1730
- <code>show.html.erb</code> template. This is where we want the comment to show, so let's
1731
- add that to the <code>app/views/articles/show.html.erb</code>.</p><div class="code_container">
1732
- <pre class="brush: plain; gutter: false; toolbar: false">
1733
- &lt;p&gt;
1734
- &lt;strong&gt;Title:&lt;/strong&gt;
1735
- &lt;%= @article.title %&gt;
1736
- &lt;/p&gt;
1737
-
1738
- &lt;p&gt;
1739
- &lt;strong&gt;Text:&lt;/strong&gt;
1740
- &lt;%= @article.text %&gt;
1741
- &lt;/p&gt;
1742
-
1743
- &lt;h2&gt;Comments&lt;/h2&gt;
1744
- &lt;% @article.comments.each do |comment| %&gt;
1745
- &lt;p&gt;
1746
- &lt;strong&gt;Commenter:&lt;/strong&gt;
1747
- &lt;%= comment.commenter %&gt;
1748
- &lt;/p&gt;
1749
-
1750
- &lt;p&gt;
1751
- &lt;strong&gt;Comment:&lt;/strong&gt;
1752
- &lt;%= comment.body %&gt;
1753
- &lt;/p&gt;
1754
- &lt;% end %&gt;
1755
-
1756
- &lt;h2&gt;Add a comment:&lt;/h2&gt;
1757
- &lt;%= form_for([@article, @article.comments.build]) do |f| %&gt;
1758
- &lt;p&gt;
1759
- &lt;%= f.label :commenter %&gt;&lt;br&gt;
1760
- &lt;%= f.text_field :commenter %&gt;
1761
- &lt;/p&gt;
1762
- &lt;p&gt;
1763
- &lt;%= f.label :body %&gt;&lt;br&gt;
1764
- &lt;%= f.text_area :body %&gt;
1765
- &lt;/p&gt;
1766
- &lt;p&gt;
1767
- &lt;%= f.submit %&gt;
1768
- &lt;/p&gt;
1769
- &lt;% end %&gt;
1770
-
1771
- &lt;%= link_to 'Edit Article', edit_article_path(@article) %&gt; |
1772
- &lt;%= link_to 'Back to Articles', articles_path %&gt;
1773
-
1774
- </pre>
1775
- </div>
1776
- <p>Now you can add articles and comments to your blog and have them show up in the
1777
- right places.</p><p><img src="images/getting_started/article_with_comments.png" alt="Article with Comments"></p><h3 id="refactoring">7 Refactoring</h3><p>Now that we have articles and comments working, take a look at the
1778
- <code>app/views/articles/show.html.erb</code> template. It is getting long and awkward. We
1779
- can use partials to clean it up.</p><h4 id="rendering-partial-collections">7.1 Rendering Partial Collections</h4><p>First, we will make a comment partial to extract showing all the comments for
1780
- the article. Create the file <code>app/views/comments/_comment.html.erb</code> and put the
1781
- following into it:</p><div class="code_container">
1782
- <pre class="brush: plain; gutter: false; toolbar: false">
1783
- &lt;p&gt;
1784
- &lt;strong&gt;Commenter:&lt;/strong&gt;
1785
- &lt;%= comment.commenter %&gt;
1786
- &lt;/p&gt;
1787
-
1788
- &lt;p&gt;
1789
- &lt;strong&gt;Comment:&lt;/strong&gt;
1790
- &lt;%= comment.body %&gt;
1791
- &lt;/p&gt;
1792
-
1793
- </pre>
1794
- </div>
1795
- <p>Then you can change <code>app/views/articles/show.html.erb</code> to look like the
1796
- following:</p><div class="code_container">
1797
- <pre class="brush: plain; gutter: false; toolbar: false">
1798
- &lt;p&gt;
1799
- &lt;strong&gt;Title:&lt;/strong&gt;
1800
- &lt;%= @article.title %&gt;
1801
- &lt;/p&gt;
1802
-
1803
- &lt;p&gt;
1804
- &lt;strong&gt;Text:&lt;/strong&gt;
1805
- &lt;%= @article.text %&gt;
1806
- &lt;/p&gt;
1807
-
1808
- &lt;h2&gt;Comments&lt;/h2&gt;
1809
- &lt;%= render @article.comments %&gt;
1810
-
1811
- &lt;h2&gt;Add a comment:&lt;/h2&gt;
1812
- &lt;%= form_for([@article, @article.comments.build]) do |f| %&gt;
1813
- &lt;p&gt;
1814
- &lt;%= f.label :commenter %&gt;&lt;br&gt;
1815
- &lt;%= f.text_field :commenter %&gt;
1816
- &lt;/p&gt;
1817
- &lt;p&gt;
1818
- &lt;%= f.label :body %&gt;&lt;br&gt;
1819
- &lt;%= f.text_area :body %&gt;
1820
- &lt;/p&gt;
1821
- &lt;p&gt;
1822
- &lt;%= f.submit %&gt;
1823
- &lt;/p&gt;
1824
- &lt;% end %&gt;
1825
-
1826
- &lt;%= link_to 'Edit Article', edit_article_path(@article) %&gt; |
1827
- &lt;%= link_to 'Back to Articles', articles_path %&gt;
1828
-
1829
- </pre>
1830
- </div>
1831
- <p>This will now render the partial in <code>app/views/comments/_comment.html.erb</code> once
1832
- for each comment that is in the <code>@article.comments</code> collection. As the <code>render</code>
1833
- method iterates over the <code>@article.comments</code> collection, it assigns each
1834
- comment to a local variable named the same as the partial, in this case
1835
- <code>comment</code> which is then available in the partial for us to show.</p><h4 id="rendering-a-partial-form">7.2 Rendering a Partial Form</h4><p>Let us also move that new comment section out to its own partial. Again, you
1836
- create a file <code>app/views/comments/_form.html.erb</code> containing:</p><div class="code_container">
1837
- <pre class="brush: plain; gutter: false; toolbar: false">
1838
- &lt;%= form_for([@article, @article.comments.build]) do |f| %&gt;
1839
- &lt;p&gt;
1840
- &lt;%= f.label :commenter %&gt;&lt;br&gt;
1841
- &lt;%= f.text_field :commenter %&gt;
1842
- &lt;/p&gt;
1843
- &lt;p&gt;
1844
- &lt;%= f.label :body %&gt;&lt;br&gt;
1845
- &lt;%= f.text_area :body %&gt;
1846
- &lt;/p&gt;
1847
- &lt;p&gt;
1848
- &lt;%= f.submit %&gt;
1849
- &lt;/p&gt;
1850
- &lt;% end %&gt;
1851
-
1852
- </pre>
1853
- </div>
1854
- <p>Then you make the <code>app/views/articles/show.html.erb</code> look like the following:</p><div class="code_container">
1855
- <pre class="brush: plain; gutter: false; toolbar: false">
1856
- &lt;p&gt;
1857
- &lt;strong&gt;Title:&lt;/strong&gt;
1858
- &lt;%= @article.title %&gt;
1859
- &lt;/p&gt;
1860
-
1861
- &lt;p&gt;
1862
- &lt;strong&gt;Text:&lt;/strong&gt;
1863
- &lt;%= @article.text %&gt;
1864
- &lt;/p&gt;
1865
-
1866
- &lt;h2&gt;Comments&lt;/h2&gt;
1867
- &lt;%= render @article.comments %&gt;
1868
-
1869
- &lt;h2&gt;Add a comment:&lt;/h2&gt;
1870
- &lt;%= render 'comments/form' %&gt;
1871
-
1872
- &lt;%= link_to 'Edit Article', edit_article_path(@article) %&gt; |
1873
- &lt;%= link_to 'Back to Articles', articles_path %&gt;
1874
-
1875
- </pre>
1876
- </div>
1877
- <p>The second render just defines the partial template we want to render,
1878
- <code>comments/form</code>. Rails is smart enough to spot the forward slash in that
1879
- string and realize that you want to render the <code>_form.html.erb</code> file in
1880
- the <code>app/views/comments</code> directory.</p><p>The <code>@article</code> object is available to any partials rendered in the view because
1881
- we defined it as an instance variable.</p><h3 id="deleting-comments">8 Deleting Comments</h3><p>Another important feature of a blog is being able to delete spam comments. To do
1882
- this, we need to implement a link of some sort in the view and a <code>destroy</code>
1883
- action in the <code>CommentsController</code>.</p><p>So first, let's add the delete link in the
1884
- <code>app/views/comments/_comment.html.erb</code> partial:</p><div class="code_container">
1885
- <pre class="brush: plain; gutter: false; toolbar: false">
1886
- &lt;p&gt;
1887
- &lt;strong&gt;Commenter:&lt;/strong&gt;
1888
- &lt;%= comment.commenter %&gt;
1889
- &lt;/p&gt;
1890
-
1891
- &lt;p&gt;
1892
- &lt;strong&gt;Comment:&lt;/strong&gt;
1893
- &lt;%= comment.body %&gt;
1894
- &lt;/p&gt;
1895
-
1896
- &lt;p&gt;
1897
- &lt;%= link_to 'Destroy Comment', [comment.article, comment],
1898
- method: :delete,
1899
- data: { confirm: 'Are you sure?' } %&gt;
1900
- &lt;/p&gt;
1901
-
1902
- </pre>
1903
- </div>
1904
- <p>Clicking this new "Destroy Comment" link will fire off a <code>DELETE
1905
- /articles/:article_id/comments/:id</code> to our <code>CommentsController</code>, which can then
1906
- use this to find the comment we want to delete, so let's add a <code>destroy</code> action
1907
- to our controller (<code>app/controllers/comments_controller.rb</code>):</p><div class="code_container">
1908
- <pre class="brush: ruby; gutter: false; toolbar: false">
1909
- class CommentsController &lt; ApplicationController
1910
- def create
1911
- @article = Article.find(params[:article_id])
1912
- @comment = @article.comments.create(comment_params)
1913
- redirect_to article_path(@article)
1914
- end
1915
-
1916
- def destroy
1917
- @article = Article.find(params[:article_id])
1918
- @comment = @article.comments.find(params[:id])
1919
- @comment.destroy
1920
- redirect_to article_path(@article)
1921
- end
1922
-
1923
- private
1924
- def comment_params
1925
- params.require(:comment).permit(:commenter, :body)
1926
- end
1927
- end
1928
-
1929
- </pre>
1930
- </div>
1931
- <p>The <code>destroy</code> action will find the article we are looking at, locate the comment
1932
- within the <code>@article.comments</code> collection, and then remove it from the
1933
- database and send us back to the show action for the article.</p><h4 id="deleting-associated-objects">8.1 Deleting Associated Objects</h4><p>If you delete an article, its associated comments will also need to be
1934
- deleted, otherwise they would simply occupy space in the database. Rails allows
1935
- you to use the <code>dependent</code> option of an association to achieve this. Modify the
1936
- Article model, <code>app/models/article.rb</code>, as follows:</p><div class="code_container">
1937
- <pre class="brush: ruby; gutter: false; toolbar: false">
1938
- class Article &lt; ActiveRecord::Base
1939
- has_many :comments, dependent: :destroy
1940
- validates :title, presence: true,
1941
- length: { minimum: 5 }
1942
- end
1943
-
1944
- </pre>
1945
- </div>
1946
- <h3 id="security">9 Security</h3><h4 id="basic-authentication">9.1 Basic Authentication</h4><p>If you were to publish your blog online, anyone would be able to add, edit and
1947
- delete articles or delete comments.</p><p>Rails provides a very simple HTTP authentication system that will work nicely in
1948
- this situation.</p><p>In the <code>ArticlesController</code> we need to have a way to block access to the
1949
- various actions if the person is not authenticated. Here we can use the Rails
1950
- <code>http_basic_authenticate_with</code> method, which allows access to the requested
1951
- action if that method allows it.</p><p>To use the authentication system, we specify it at the top of our
1952
- <code>ArticlesController</code> in <code>app/controllers/articles_controller.rb</code>. In our case,
1953
- we want the user to be authenticated on every action except <code>index</code> and <code>show</code>,
1954
- so we write that:</p><div class="code_container">
1955
- <pre class="brush: ruby; gutter: false; toolbar: false">
1956
- class ArticlesController &lt; ApplicationController
1957
-
1958
- http_basic_authenticate_with name: "dhh", password: "secret", except: [:index, :show]
1959
-
1960
- def index
1961
- @articles = Article.all
1962
- end
1963
-
1964
- # snipped for brevity
1965
-
1966
- </pre>
1967
- </div>
1968
- <p>We also want to allow only authenticated users to delete comments, so in the
1969
- <code>CommentsController</code> (<code>app/controllers/comments_controller.rb</code>) we write:</p><div class="code_container">
1970
- <pre class="brush: ruby; gutter: false; toolbar: false">
1971
- class CommentsController &lt; ApplicationController
1972
-
1973
- http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy
1974
-
1975
- def create
1976
- @article = Article.find(params[:article_id])
1977
- # ...
1978
- end
1979
-
1980
- # snipped for brevity
1981
-
1982
- </pre>
1983
- </div>
1984
- <p>Now if you try to create a new article, you will be greeted with a basic HTTP
1985
- Authentication challenge:</p><p><img src="images/getting_started/challenge.png" alt="Basic HTTP Authentication Challenge"></p><p>Other authentication methods are available for Rails applications. Two popular
1986
- authentication add-ons for Rails are the
1987
- <a href="https://github.com/plataformatec/devise">Devise</a> rails engine and
1988
- the <a href="https://github.com/binarylogic/authlogic">Authlogic</a> gem,
1989
- along with a number of others.</p><h4 id="other-security-considerations">9.2 Other Security Considerations</h4><p>Security, especially in web applications, is a broad and detailed area. Security
1990
- in your Rails application is covered in more depth in
1991
- the <a href="security.html">Ruby on Rails Security Guide</a>.</p><h3 id="what's-next-questionmark">10 What's Next?</h3><p>Now that you've seen your first Rails application, you should feel free to
1992
- update it and experiment on your own. But you don't have to do everything
1993
- without help. As you need assistance getting up and running with Rails, feel
1994
- free to consult these support resources:</p>
1995
- <ul>
1996
- <li>The <a href="index.html">Ruby on Rails Guides</a>
1997
- </li>
1998
- <li>The <a href="http://railstutorial.org/book">Ruby on Rails Tutorial</a>
1999
- </li>
2000
- <li>The <a href="http://groups.google.com/group/rubyonrails-talk">Ruby on Rails mailing list</a>
2001
- </li>
2002
- <li>The <a href="irc://irc.freenode.net/#rubyonrails">#rubyonrails</a> channel on irc.freenode.net</li>
2003
- </ul>
2004
- <p>Rails also comes with built-in help that you can generate using the rake
2005
- command-line utility:</p>
2006
- <ul>
2007
- <li>Running <code>rake doc:guides</code> will put a full copy of the Rails Guides in the
2008
- <code>doc/guides</code> folder of your application. Open <code>doc/guides/index.html</code> in your
2009
- web browser to explore the Guides.</li>
2010
- <li>Running <code>rake doc:rails</code> will put a full copy of the API documentation for
2011
- Rails in the <code>doc/api</code> folder of your application. Open <code>doc/api/index.html</code>
2012
- in your web browser to explore the API documentation.</li>
2013
- </ul>
2014
- <div class="info"><p>To be able to generate the Rails Guides locally with the <code>doc:guides</code> rake
2015
- task you need to install the RedCloth gem. Add it to your <code>Gemfile</code> and run
2016
- <code>bundle install</code> and you're ready to go.</p></div><h3 id="configuration-gotchas">11 Configuration Gotchas</h3><p>The easiest way to work with Rails is to store all external data as UTF-8. If
2017
- you don't, Ruby libraries and Rails will often be able to convert your native
2018
- data into UTF-8, but this doesn't always work reliably, so you're better off
2019
- ensuring that all external data is UTF-8.</p><p>If you have made a mistake in this area, the most common symptom is a black
2020
- diamond with a question mark inside appearing in the browser. Another common
2021
- symptom is characters like "ü" appearing instead of "ü". Rails takes a number
2022
- of internal steps to mitigate common causes of these problems that can be
2023
- automatically detected and corrected. However, if you have external data that is
2024
- not stored as UTF-8, it can occasionally result in these kinds of issues that
2025
- cannot be automatically detected by Rails and corrected.</p><p>Two very common sources of data that are not UTF-8:</p>
2026
- <ul>
2027
- <li>Your text editor: Most text editors (such as TextMate), default to saving
2028
- files as UTF-8. If your text editor does not, this can result in special
2029
- characters that you enter in your templates (such as é) to appear as a diamond
2030
- with a question mark inside in the browser. This also applies to your i18n
2031
- translation files. Most editors that do not already default to UTF-8 (such as
2032
- some versions of Dreamweaver) offer a way to change the default to UTF-8. Do
2033
- so.</li>
2034
- <li>Your database: Rails defaults to converting data from your database into UTF-8
2035
- at the boundary. However, if your database is not using UTF-8 internally, it
2036
- may not be able to store all characters that your users enter. For instance,
2037
- if your database is using Latin-1 internally, and your user enters a Russian,
2038
- Hebrew, or Japanese character, the data will be lost forever once it enters
2039
- the database. If possible, use UTF-8 as the internal storage of your database.</li>
2040
- </ul>
2041
-
2042
-
2043
- <h3>Feedback</h3>
2044
- <p>
2045
- You're encouraged to help improve the quality of this guide.
2046
- </p>
2047
- <p>
2048
- Please contribute if you see any typos or factual errors.
2049
- To get started, you can read our <a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section.
2050
- </p>
2051
- <p>
2052
- You may also find incomplete content, or stuff that is not up to date.
2053
- Please do add any missing documentation for master. Make sure to check
2054
- <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
2055
- if the issues are already fixed or not on the master branch.
2056
- Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
2057
- for style and conventions.
2058
- </p>
2059
- <p>
2060
- If for whatever reason you spot something to fix but cannot patch it yourself, please
2061
- <a href="https://github.com/rails/rails/issues">open an issue</a>.
2062
- </p>
2063
- <p>And last but not least, any kind of discussion regarding Ruby on Rails
2064
- documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
2065
- </p>
2066
- </div>
2067
- </div>
2068
- </div>
2069
-
2070
- <hr class="hide" />
2071
- <div id="footer">
2072
- <div class="wrapper">
2073
- <p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p>
2074
- <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
2075
-
2076
- </div>
2077
- </div>
2078
-
2079
- <script type="text/javascript" src="javascripts/jquery.min.js"></script>
2080
- <script type="text/javascript" src="javascripts/responsive-tables.js"></script>
2081
- <script type="text/javascript" src="javascripts/guides.js"></script>
2082
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
2083
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
2084
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
2085
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
2086
- <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
2087
- <script type="text/javascript">
2088
- SyntaxHighlighter.all();
2089
- $(guidesIndex.bind);
2090
- </script>
2091
- </body>
2092
- </html>