railties 3.1.12 → 3.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. data/CHANGELOG.md +2292 -41
  2. data/README.rdoc +14 -5
  3. data/bin/rails +7 -0
  4. data/guides/code/getting_started/Gemfile +27 -0
  5. data/guides/code/getting_started/README.rdoc +261 -0
  6. data/guides/code/getting_started/Rakefile +7 -0
  7. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  8. data/guides/code/getting_started/app/assets/javascripts/application.js +9 -0
  9. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
  10. data/guides/code/getting_started/app/assets/javascripts/home.js.coffee +3 -0
  11. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
  12. data/guides/code/getting_started/app/assets/stylesheets/application.css +7 -0
  13. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
  14. data/guides/code/getting_started/app/assets/stylesheets/home.css.scss +3 -0
  15. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
  16. data/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss +56 -0
  17. data/guides/code/getting_started/app/controllers/application_controller.rb +3 -0
  18. data/guides/code/getting_started/app/controllers/comments_controller.rb +16 -0
  19. data/guides/code/getting_started/app/controllers/home_controller.rb +5 -0
  20. data/guides/code/getting_started/app/controllers/posts_controller.rb +84 -0
  21. data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
  22. data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
  23. data/guides/code/getting_started/app/helpers/home_helper.rb +2 -0
  24. data/guides/code/getting_started/app/helpers/posts_helper.rb +5 -0
  25. data/guides/code/getting_started/app/models/comment.rb +3 -0
  26. data/guides/code/getting_started/app/models/post.rb +11 -0
  27. data/guides/code/getting_started/app/models/tag.rb +3 -0
  28. data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
  29. data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
  30. data/guides/code/getting_started/app/views/home/index.html.erb +2 -0
  31. data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
  32. data/guides/code/getting_started/app/views/posts/_form.html.erb +32 -0
  33. data/guides/code/getting_started/app/views/posts/edit.html.erb +6 -0
  34. data/guides/code/getting_started/app/views/posts/index.html.erb +27 -0
  35. data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
  36. data/guides/code/getting_started/app/views/posts/show.html.erb +31 -0
  37. data/guides/code/getting_started/app/views/tags/_form.html.erb +12 -0
  38. data/guides/code/getting_started/config.ru +4 -0
  39. data/guides/code/getting_started/config/application.rb +53 -0
  40. data/guides/code/getting_started/config/boot.rb +6 -0
  41. data/guides/code/getting_started/config/database.yml +25 -0
  42. data/guides/code/getting_started/config/environment.rb +5 -0
  43. data/guides/code/getting_started/config/environments/development.rb +37 -0
  44. data/guides/code/getting_started/config/environments/production.rb +67 -0
  45. data/guides/code/getting_started/config/environments/test.rb +37 -0
  46. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
  47. data/guides/code/getting_started/config/initializers/inflections.rb +10 -0
  48. data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
  49. data/guides/code/getting_started/config/initializers/secret_token.rb +7 -0
  50. data/guides/code/getting_started/config/initializers/session_store.rb +8 -0
  51. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
  52. data/guides/code/getting_started/config/locales/en.yml +5 -0
  53. data/guides/code/getting_started/config/routes.rb +64 -0
  54. data/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +11 -0
  55. data/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb +12 -0
  56. data/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb +11 -0
  57. data/guides/code/getting_started/db/schema.rb +43 -0
  58. data/guides/code/getting_started/db/seeds.rb +7 -0
  59. data/guides/code/getting_started/doc/README_FOR_APP +2 -0
  60. data/guides/code/getting_started/public/404.html +26 -0
  61. data/guides/code/getting_started/public/422.html +26 -0
  62. data/guides/code/getting_started/public/500.html +26 -0
  63. data/guides/code/getting_started/public/favicon.ico +0 -0
  64. data/guides/code/getting_started/public/robots.txt +5 -0
  65. data/guides/code/getting_started/script/rails +6 -0
  66. data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
  67. data/guides/code/getting_started/test/fixtures/posts.yml +11 -0
  68. data/guides/code/getting_started/test/fixtures/tags.yml +9 -0
  69. data/guides/code/getting_started/test/functional/comments_controller_test.rb +7 -0
  70. data/guides/code/getting_started/test/functional/home_controller_test.rb +9 -0
  71. data/guides/code/getting_started/test/functional/posts_controller_test.rb +49 -0
  72. data/guides/code/getting_started/test/performance/browsing_test.rb +12 -0
  73. data/guides/code/getting_started/test/test_helper.rb +13 -0
  74. data/guides/code/getting_started/test/unit/comment_test.rb +7 -0
  75. data/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb +4 -0
  76. data/guides/code/getting_started/test/unit/helpers/home_helper_test.rb +4 -0
  77. data/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb +4 -0
  78. data/guides/code/getting_started/test/unit/post_test.rb +7 -0
  79. data/guides/code/getting_started/test/unit/tag_test.rb +7 -0
  80. data/guides/rails_guides/generator.rb +2 -1
  81. data/guides/source/3_0_release_notes.textile +2 -2
  82. data/guides/source/3_1_release_notes.textile +3 -110
  83. data/guides/source/action_controller_overview.textile +11 -13
  84. data/guides/source/action_mailer_basics.textile +7 -18
  85. data/guides/source/action_view_overview.textile +78 -9
  86. data/guides/source/active_model_basics.textile +205 -0
  87. data/guides/source/active_record_basics.textile +31 -31
  88. data/guides/source/active_record_querying.textile +288 -67
  89. data/guides/source/active_record_validations_callbacks.textile +69 -75
  90. data/guides/source/active_resource_basics.textile +48 -2
  91. data/guides/source/active_support_core_extensions.textile +145 -24
  92. data/guides/source/ajax_on_rails.textile +65 -7
  93. data/guides/source/api_documentation_guidelines.textile +0 -6
  94. data/guides/source/asset_pipeline.textile +2 -2
  95. data/guides/source/association_basics.textile +25 -34
  96. data/guides/source/caching_with_rails.textile +12 -17
  97. data/guides/source/command_line.textile +29 -19
  98. data/guides/source/configuring.textile +40 -18
  99. data/guides/source/contributing_to_ruby_on_rails.textile +11 -18
  100. data/guides/source/debugging_rails_applications.textile +10 -21
  101. data/guides/source/engines.textile +618 -0
  102. data/guides/source/form_helpers.textile +1 -12
  103. data/guides/source/generators.textile +9 -11
  104. data/guides/source/getting_started.textile +152 -152
  105. data/guides/source/i18n.textile +4 -5
  106. data/guides/source/index.html.erb +0 -1
  107. data/guides/source/initialization.textile +26 -26
  108. data/guides/source/layouts_and_rendering.textile +97 -61
  109. data/guides/source/migrations.textile +380 -161
  110. data/guides/source/performance_testing.textile +4 -10
  111. data/guides/source/plugins.textile +11 -19
  112. data/guides/source/rails_application_templates.textile +12 -4
  113. data/guides/source/rails_on_rack.textile +25 -19
  114. data/guides/source/routing.textile +6 -13
  115. data/guides/source/ruby_on_rails_guides_guidelines.textile +0 -5
  116. data/guides/source/security.textile +11 -15
  117. data/guides/source/testing.textile +1 -9
  118. data/lib/rails/application.rb +107 -42
  119. data/lib/rails/application/bootstrap.rb +12 -11
  120. data/lib/rails/application/configuration.rb +27 -21
  121. data/lib/rails/application/finisher.rb +40 -17
  122. data/lib/rails/application/route_inspector.rb +75 -0
  123. data/lib/rails/application/routes_reloader.rb +15 -4
  124. data/lib/rails/code_statistics.rb +16 -5
  125. data/lib/rails/commands.rb +6 -5
  126. data/lib/rails/commands/application.rb +8 -1
  127. data/lib/rails/commands/console.rb +2 -0
  128. data/lib/rails/commands/dbconsole.rb +2 -2
  129. data/lib/rails/commands/destroy.rb +0 -2
  130. data/lib/rails/commands/generate.rb +3 -3
  131. data/lib/rails/commands/plugin.rb +161 -159
  132. data/lib/rails/commands/plugin_new.rb +3 -2
  133. data/lib/rails/commands/runner.rb +4 -0
  134. data/lib/rails/console/app.rb +26 -22
  135. data/lib/rails/console/helpers.rb +9 -5
  136. data/lib/rails/engine.rb +70 -34
  137. data/lib/rails/engine/commands.rb +39 -0
  138. data/lib/rails/engine/configuration.rb +1 -1
  139. data/lib/rails/generators.rb +3 -14
  140. data/lib/rails/generators/actions.rb +36 -9
  141. data/lib/rails/generators/app_base.rb +34 -38
  142. data/lib/rails/generators/base.rb +4 -4
  143. data/lib/rails/generators/generated_attribute.rb +1 -1
  144. data/lib/rails/generators/named_base.rb +1 -3
  145. data/lib/rails/generators/rails/app/USAGE +6 -0
  146. data/lib/rails/generators/rails/app/app_generator.rb +6 -2
  147. data/lib/rails/generators/rails/app/templates/Gemfile +4 -3
  148. data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +9 -3
  149. data/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css +11 -5
  150. data/lib/rails/generators/rails/app/templates/app/mailers/.empty_directory +0 -0
  151. data/lib/rails/generators/rails/app/templates/app/models/.empty_directory +0 -0
  152. data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +1 -1
  153. data/lib/rails/generators/rails/app/templates/config/application.rb +11 -0
  154. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +1 -1
  155. data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +1 -1
  156. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +10 -1
  157. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +10 -1
  158. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +6 -6
  159. data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb +5 -0
  160. data/lib/rails/generators/rails/app/templates/config/routes.rb +1 -1
  161. data/lib/rails/generators/rails/app/templates/public/500.html +0 -1
  162. data/lib/rails/generators/rails/app/templates/public/index.html +1 -1
  163. data/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
  164. data/lib/rails/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
  165. data/lib/rails/generators/rails/app/templates/test/functional/.empty_directory +0 -0
  166. data/lib/rails/generators/rails/app/templates/test/integration/.empty_directory +0 -0
  167. data/lib/rails/generators/rails/app/templates/test/unit/.empty_directory +0 -0
  168. data/lib/rails/generators/rails/controller/templates/controller.rb +1 -1
  169. data/lib/rails/generators/rails/generator/templates/templates/.empty_directory +0 -0
  170. data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +17 -5
  171. data/lib/rails/generators/rails/plugin_new/templates/Rakefile +1 -0
  172. data/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory +0 -0
  173. data/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory +0 -0
  174. data/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt +1 -1
  175. data/lib/rails/generators/rails/plugin_new/templates/gitignore +4 -3
  176. data/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb +1 -1
  177. data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +1 -1
  178. data/lib/rails/generators/rails/plugin_new/templates/script/rails.tt +5 -3
  179. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +2 -2
  180. data/lib/rails/generators/rails/task/USAGE +9 -0
  181. data/lib/rails/generators/rails/task/task_generator.rb +12 -0
  182. data/lib/rails/generators/rails/task/templates/task.rb +8 -0
  183. data/lib/rails/generators/resource_helpers.rb +3 -3
  184. data/lib/rails/generators/test_unit/integration/templates/integration_test.rb +0 -2
  185. data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +4 -4
  186. data/lib/rails/paths.rb +11 -38
  187. data/lib/rails/rack/debugger.rb +3 -4
  188. data/lib/rails/rack/logger.rb +26 -12
  189. data/lib/rails/railtie.rb +6 -1
  190. data/lib/rails/railtie/configuration.rb +12 -5
  191. data/lib/rails/source_annotation_extractor.rb +12 -10
  192. data/lib/rails/tasks/documentation.rake +3 -1
  193. data/lib/rails/tasks/engine.rake +1 -0
  194. data/lib/rails/tasks/misc.rake +1 -1
  195. data/lib/rails/tasks/routes.rake +3 -23
  196. data/lib/rails/test_help.rb +1 -2
  197. data/lib/rails/test_unit/testing.rake +8 -4
  198. data/lib/rails/version.rb +3 -3
  199. metadata +131 -61
  200. checksums.yaml +0 -7
  201. data/lib/rails/generators/rails/plugin/USAGE +0 -13
  202. data/lib/rails/generators/rails/plugin/plugin_generator.rb +0 -54
  203. data/lib/rails/generators/rails/plugin/templates/MIT-LICENSE.tt +0 -20
  204. data/lib/rails/generators/rails/plugin/templates/README.tt +0 -13
  205. data/lib/rails/generators/rails/plugin/templates/Rakefile.tt +0 -23
  206. data/lib/rails/generators/rails/plugin/templates/init.rb +0 -1
  207. data/lib/rails/generators/rails/plugin/templates/install.rb +0 -1
  208. data/lib/rails/generators/rails/plugin/templates/lib/%file_name%.rb.tt +0 -1
  209. data/lib/rails/generators/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt +0 -4
  210. data/lib/rails/generators/rails/plugin/templates/uninstall.rb +0 -1
@@ -229,7 +229,7 @@ The corresponding view +app/views/articles/new.html.erb+ using +form_for+ looks
229
229
  There are a few things to note here:
230
230
 
231
231
  # +@article+ is the actual object being edited.
232
- # There is a single hash of options. Routing options are passed in the +:url+ hash, HTML options are passed in the +:html+ hash.
232
+ # There is a single hash of options. Routing options are passed in the +:url+ hash, HTML options are passed in the +:html+ hash. Also you can provide a +:namespace+ option for your form to ensure uniqueness of id attributes on form elements. The namespace attribute will be prefixed with underscore on the generated HTML id.
233
233
  # The +form_for+ method yields a *form builder* object (the +f+ variable).
234
234
  # Methods to create form controls are called *on* the form builder object +f+
235
235
 
@@ -342,7 +342,6 @@ output:
342
342
 
343
343
  When parsing POSTed data, Rails will take into account the special +_method+ parameter and acts as if the HTTP method was the one specified inside it ("PUT" in this example).
344
344
 
345
-
346
345
  h3. Making Select Boxes with Ease
347
346
 
348
347
  Select boxes in HTML require a significant amount of markup (one +OPTION+ element for each option to choose from), therefore it makes the most sense for them to be dynamically generated.
@@ -797,13 +796,3 @@ Many apps grow beyond simple forms editing a single object. For example when cre
797
796
  * Eloy Duran's "complex-forms-examples":https://github.com/alloy/complex-form-examples/ application
798
797
  * Lance Ivy's "nested_assignment":https://github.com/cainlevy/nested_assignment/tree/master plugin and "sample application":https://github.com/cainlevy/complex-form-examples/tree/cainlevy
799
798
  * James Golick's "attribute_fu":https://github.com/jamesgolick/attribute_fu plugin
800
-
801
- h3. Changelog
802
-
803
- * February 5, 2011: Added 'Forms to external resources' section. Timothy N. Tsvetkov <timothy.tsvetkov@gmail.com>
804
- * April 6, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
805
-
806
- h3. Authors
807
-
808
- * Mislav Marohnić <mislav.marohnic@gmail.com>
809
- * "Frederick Cheung":credits.html#fcheung
@@ -449,6 +449,15 @@ The above code will put the following line into +Gemfile+:
449
449
  gem "devise", :git => "git://github.com/plataformatec/devise", :branch => "master"
450
450
  </ruby>
451
451
 
452
+ h4. +gem_group+
453
+
454
+ Wraps gem entries inside a group:
455
+
456
+ <ruby>
457
+ gem_group :development, :test do
458
+ gem "rspec-rails"
459
+ end
460
+ </ruby>
452
461
 
453
462
  h4. +add_source+
454
463
 
@@ -610,14 +619,3 @@ Output the contents of a file in the template's +source_path+, usually a README.
610
619
  <ruby>
611
620
  readme("README")
612
621
  </ruby>
613
-
614
- h3. Changelog
615
-
616
- * December 1, 2010: Documenting the available methods and options for generators and templates by "Ryan Bigg":http://ryanbigg.com
617
- * December 1, 2010: Addition of Rails application templates by "Ryan Bigg":http://ryanbigg.com
618
-
619
- * August 23, 2010: Edit pass by "Xavier Noria":credits.html#fxn
620
-
621
- * April 30, 2010: Reviewed by José Valim
622
-
623
- * November 20, 2009: First version by José Valim
@@ -41,8 +41,15 @@ internet for learning Ruby, including:
41
41
  * "Programming Ruby":http://www.ruby-doc.org/docs/ProgrammingRuby/
42
42
  * "Why's (Poignant) Guide to Ruby":http://mislav.uniqpath.com/poignant-guide/
43
43
 
44
+ Also, the example code for this guide is available in the rails github:https://github.com/rails/rails repository
45
+ in rails/railties/guides/code/getting_started.
46
+
44
47
  h3. What is Rails?
45
48
 
49
+ TIP: This section goes into the background and philosophy of the Rails framework
50
+ in detail. You can safely skip this section and come back to it at a later time.
51
+ Section 3 starts you on the path to creating your first Rails application.
52
+
46
53
  Rails is a web application development framework written in the Ruby language.
47
54
  It is designed to make programming web applications easier by making assumptions
48
55
  about what every developer needs to get started. It allows you to write less
@@ -61,9 +68,9 @@ The Rails philosophy includes several guiding principles:
61
68
 
62
69
  * DRY - "Don't Repeat Yourself" - suggests that writing the same code over and over again is a bad thing.
63
70
  * Convention Over Configuration - means that Rails makes assumptions about what you want to do and how you're going to
64
- d o it, rather than requiring you to specify every little thing through endless configuration files.
71
+ do it, rather than requiring you to specify every little thing through endless configuration files.
65
72
  * REST is the best pattern for web applications - organizing your application around resources and standard HTTP verbs
66
- i s the fastest way to go.
73
+ is the fastest way to go.
67
74
 
68
75
  h4. The MVC Architecture
69
76
 
@@ -116,7 +123,6 @@ need to know anything about them to continue with this guide.
116
123
  * Active Support
117
124
  * Railties
118
125
 
119
-
120
126
  h5. Action Pack
121
127
 
122
128
  Action Pack is a single gem that contains Action Controller, Action View and
@@ -213,7 +219,11 @@ Ian Robinson
213
219
 
214
220
  h3. Creating a New Rails Project
215
221
 
216
- If you follow this guide, you'll create a Rails project called <tt>blog</tt>, a
222
+ The best way to use this guide is to follow each step as it happens, no code or
223
+ step needed to make this example application has been left out, so you can
224
+ literally follow along step by step. You can get the complete code "here":https://github.com/lifo/docrails/tree/master/railties/guides/code/getting_started.
225
+
226
+ By following along with this guide, you'll create a Rails project called <tt>blog</tt>, a
217
227
  (very) simple weblog. Before you can start building the application, you need to
218
228
  make sure that you have Rails itself installed.
219
229
 
@@ -231,13 +241,16 @@ Usually run this as the root user:
231
241
  TIP. If you're working on Windows, you can quickly install Ruby and Rails with
232
242
  "Rails Installer":http://railsinstaller.org.
233
243
 
234
- h4. Creating the Blog Application
244
+ To verify that you have everything installed correctly, you should be able to run
245
+ the following:
235
246
 
236
- The best way to use this guide is to follow each step as it happens, no code or
237
- step needed to make this example application has been left out, so you can
238
- literally follow along step by step. If you need to see the completed code, you
239
- can download it from "Getting Started
240
- Code":https://github.com/mikel/getting-started-code.
247
+ <shell>
248
+ $ rails --version
249
+ </shell>
250
+
251
+ If it says something like "Rails 3.1.3" you are ready to continue.
252
+
253
+ h4. Creating the Blog Application
241
254
 
242
255
  To begin, open a terminal, navigate to a folder where you have rights to create
243
256
  files, and type:
@@ -259,41 +272,50 @@ directly in that application:
259
272
  $ cd blog
260
273
  </shell>
261
274
 
262
- In any case, Rails will create a folder in your working directory called
263
- <tt>blog</tt>. Open up that folder and explore its contents. Most of the work in
275
+ The 'rails new blog' command we ran above created a folder in your working directory
276
+ called <tt>blog</tt>. The <tt>blog</tt> folder has a number of auto-generated folders
277
+ that make up the structure of a Rails application. Most of the work in
264
278
  this tutorial will happen in the <tt>app/</tt> folder, but here's a basic
265
- rundown on the function of each folder that Rails creates in a new application
266
- by default:
279
+ rundown on the function of each of the files and folders that Rails created by default:
267
280
 
268
281
  |_.File/Folder|_.Purpose|
269
- |Gemfile|This file allows you to specify what gem dependencies are needed for your Rails application. See section on Bundler, below.|
270
- |README|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.|
271
- |Rakefile|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.|
272
282
  |app/|Contains the controllers, models, views and assets for your application. You'll focus on this folder for the remainder of this guide.|
273
- |config/|Configure your application's runtime rules, routes, database, and more.|
283
+ |config/|Configure your application's runtime rules, routes, database, and more. This is covered in more detail in "Configuring Rails Applications":configuring.html|
274
284
  |config.ru|Rack configuration for Rack based servers used to start the application.|
275
- |db/|Shows your current database schema, as well as the database migrations. You'll learn about migrations shortly.|
285
+ |db/|Contains your current database schema, as well as the database migrations.|
276
286
  |doc/|In-depth documentation for your application.|
277
- |lib/|Extended modules for your application (not covered in this guide).|
287
+ |Gemfile<BR />Gemfile.lock|These files allow you to specify what gem dependencies are needed for your Rails application.|
288
+ |lib/|Extended modules for your application.|
278
289
  |log/|Application log files.|
279
290
  |public/|The only folder seen to the world as-is. Contains the static files and compiled assets.|
291
+ |Rakefile|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.|
292
+ |README.rdoc|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.|
280
293
  |script/|Contains the rails script that starts your app and can contain other scripts you use to deploy or run your application.|
281
294
  |test/|Unit tests, fixtures, and other test apparatus. These are covered in "Testing Rails Applications":testing.html|
282
295
  |tmp/|Temporary files|
283
- |vendor/|A place for all third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if you install it into your project) and plugins containing additional prepackaged functionality.|
296
+ |vendor/|A place for all third-party code. In a typical Rails application, this includes Ruby Gems, the Rails source code (if you optionally install it into your project) and plugins containing additional prepackaged functionality.|
284
297
 
285
298
  h4. Configuring a Database
286
299
 
287
300
  Just about every Rails application will interact with a database. The database
288
301
  to use is specified in a configuration file, +config/database.yml+. If you open
289
302
  this file in a new Rails application, you'll see a default database
290
- configuration using SQLite3. The file contains sections for three different
303
+ configured to use SQLite3. The file contains sections for three different
291
304
  environments in which Rails can run by default:
292
305
 
293
- * The +development+ environment is used on your development computer as you interact manually with the application.
294
- * The +test+ environment is used to run automated tests.
306
+ * The +development+ environment is used on your development/local computer as you interact
307
+ manually with the application.
308
+ * The +test+ environment is used when running automated tests.
295
309
  * The +production+ environment is used when you deploy your application for the world to use.
296
310
 
311
+ TIP: You don't have to update the database configurations manually. If you look at the
312
+ options of the application generator, you will see that one of the options
313
+ is named <tt>--database</tt>. This option allows you to choose an adapter from a
314
+ list of the most used relational databases. You can even run the generator
315
+ repeatedly: <tt>cd .. && rails new blog --database=mysql</tt>. When you confirm the overwriting
316
+ of the +config/database.yml+ file, your application will be configured for MySQL
317
+ instead of SQLite. Detailed examples of the common database connections are below.
318
+
297
319
  h5. Configuring an SQLite3 Database
298
320
 
299
321
  Rails comes with built-in support for "SQLite3":http://www.sqlite.org, which is
@@ -358,7 +380,7 @@ development:
358
380
 
359
381
  h5. Configuring an SQLite3 Database for JRuby Platform
360
382
 
361
- If you choose to use SQLite3 and using JRuby, your +config/database.yml+ will
383
+ If you choose to use SQLite3 and are using JRuby, your +config/database.yml+ will
362
384
  look a little different. Here's the development section:
363
385
 
364
386
  <yaml>
@@ -369,7 +391,7 @@ development:
369
391
 
370
392
  h5. Configuring a MySQL Database for JRuby Platform
371
393
 
372
- If you choose to use MySQL and using JRuby, your +config/database.yml+ will look
394
+ If you choose to use MySQL and are using JRuby, your +config/database.yml+ will look
373
395
  a little different. Here's the development section:
374
396
 
375
397
  <yaml>
@@ -382,7 +404,7 @@ development:
382
404
 
383
405
  h5. Configuring a PostgreSQL Database for JRuby Platform
384
406
 
385
- Finally if you choose to use PostgreSQL and using JRuby, your
407
+ Finally if you choose to use PostgreSQL and are using JRuby, your
386
408
  +config/database.yml+ will look a little different. Here's the development
387
409
  section:
388
410
 
@@ -397,14 +419,6 @@ development:
397
419
 
398
420
  Change the username and password in the +development+ section as appropriate.
399
421
 
400
- TIP: You don't have to update the database configurations manually. If you look at the
401
- options of the application generator, you will see that one of the options
402
- is named <tt>--database</tt>. This option allows you to choose an adapter from a
403
- list of the most used relational databases. You can even run the generator
404
- repeatedly: <tt>cd .. && rails new blog --database=mysql</tt>. When you confirm the overwriting
405
- of the +config/database.yml+ file, your application will be configured for MySQL
406
- instead of SQLite.
407
-
408
422
  h4. Creating the Database
409
423
 
410
424
  Now that you have your database configured, it's time to have Rails create an
@@ -436,6 +450,8 @@ start a web server on your development machine. You can do this by running:
436
450
  $ rails server
437
451
  </shell>
438
452
 
453
+ TIP: Compiling CoffeeScript to JavaScript requires a JavaScript runtime and the absence of a runtime will give you an +execjs+ error. Usually Mac OS X and Windows come with a JavaScript runtime installed. +therubyracer+ and +therubyrhino+ are the commonly used runtimes for Ruby and JRuby respectively. You can also investigate a list of runtimes at "ExecJS":https://github.com/sstephenson/execjs.
454
+
439
455
  This will fire up an instance of the WEBrick web server by default (Rails can
440
456
  also use several other web servers). To see your application in action, open a
441
457
  browser window and navigate to "http://localhost:3000":http://localhost:3000.
@@ -478,7 +494,7 @@ Open this file in your text editor and edit it to contain a single line of code:
478
494
  h4. Setting the Application Home Page
479
495
 
480
496
  Now that we have made the controller and view, we need to tell Rails when we
481
- want "Hello Rails" to show up. In our case, we want it to show up when we
497
+ want "Hello Rails!" to show up. In our case, we want it to show up when we
482
498
  navigate to the root URL of our site,
483
499
  "http://localhost:3000":http://localhost:3000, instead of the "Welcome Aboard"
484
500
  smoke test.
@@ -499,8 +515,7 @@ file_ which holds entries in a special DSL (domain-specific language) that tells
499
515
  Rails how to connect incoming requests to controllers and actions. This file
500
516
  contains many sample routes on commented lines, and one of them actually shows
501
517
  you how to connect the root of your site to a specific controller and action.
502
- Find the line beginning with +root :to+, uncomment it and change it like the
503
- following:
518
+ Find the line beginning with +root :to+ and uncomment it. It should look something like the following:
504
519
 
505
520
  <ruby>
506
521
  Blog::Application.routes.draw do
@@ -528,7 +543,7 @@ resource in a single operation, scaffolding is the tool for the job.
528
543
 
529
544
  h3. Creating a Resource
530
545
 
531
- In the case of the blog application, you can start by generating a scaffolded
546
+ In the case of the blog application, you can start by generating a scaffold for the
532
547
  Post resource: this will represent a single blog posting. To do this, enter this
533
548
  command in your terminal:
534
549
 
@@ -542,21 +557,21 @@ folders, and edit <tt>config/routes.rb</tt>. Here's a quick overview of what it
542
557
  |_.File |_.Purpose|
543
558
  |db/migrate/20100207214725_create_posts.rb |Migration to create the posts table in your database (your name will include a different timestamp)|
544
559
  |app/models/post.rb |The Post model|
545
- |test/fixtures/posts.yml |Dummy posts for use in testing|
560
+ |test/unit/post_test.rb |Unit testing harness for the posts model|
561
+ |test/fixtures/posts.yml |Sample posts for use in testing|
562
+ |config/routes.rb |Edited to include routing information for posts|
546
563
  |app/controllers/posts_controller.rb |The Posts controller|
547
564
  |app/views/posts/index.html.erb |A view to display an index of all posts |
548
565
  |app/views/posts/edit.html.erb |A view to edit an existing post|
549
566
  |app/views/posts/show.html.erb |A view to display a single post|
550
567
  |app/views/posts/new.html.erb |A view to create a new post|
551
568
  |app/views/posts/_form.html.erb |A partial to control the overall look and feel of the form used in edit and new views|
552
- |app/helpers/posts_helper.rb |Helper functions to be used from the post views|
553
- |app/assets/stylesheets/scaffolds.css.scss |Cascading style sheet to make the scaffolded views look better|
554
- |app/assets/stylesheets/posts.css.scss |Cascading style sheet for the posts controller|
555
- |app/assets/javascripts/posts.js.coffee |CoffeeScript for the posts controller|
556
- |test/unit/post_test.rb |Unit testing harness for the posts model|
557
569
  |test/functional/posts_controller_test.rb |Functional testing harness for the posts controller|
570
+ |app/helpers/posts_helper.rb |Helper functions to be used from the post views|
558
571
  |test/unit/helpers/posts_helper_test.rb |Unit testing harness for the posts helper|
559
- |config/routes.rb |Edited to include routing information for posts|
572
+ |app/assets/javascripts/posts.js.coffee |CoffeeScript for the posts controller|
573
+ |app/assets/stylesheets/posts.css.scss |Cascading style sheet for the posts controller|
574
+ |app/assets/stylesheets/scaffolds.css.scss |Cascading style sheet to make the scaffolded views look better|
560
575
 
561
576
  NOTE. While scaffolding will get you up and running quickly, the code it
562
577
  generates is unlikely to be a perfect fit for your application. You'll most
@@ -593,12 +608,12 @@ class CreatePosts < ActiveRecord::Migration
593
608
  end
594
609
  </ruby>
595
610
 
596
- The above migration creates a method name +change+ which will be called when you
597
- run this migration. The action defined in that method is also reversible, which
611
+ The above migration creates a method named +change+ which will be called when you
612
+ run this migration. The action defined in this method is also reversible, which
598
613
  means Rails knows how to reverse the change made by this migration, in case you
599
- want to reverse it at later date. By default, when you run this migration it
600
- will creates a +posts+ table with two string columns and a text column. It also
601
- creates two timestamp fields to track record creation and updating. More
614
+ want to reverse it later. When you run this migration it will create a
615
+ +posts+ table with two string columns and a text column. It also creates two
616
+ timestamp fields to allow Rails to track post creation and update times. More
602
617
  information about Rails migrations can be found in the "Rails Database
603
618
  Migrations":migrations.html guide.
604
619
 
@@ -620,7 +635,7 @@ table.
620
635
 
621
636
  NOTE. Because you're working in the development environment by default, this
622
637
  command will apply to the database defined in the +development+ section of your
623
- +config/database.yml+ file. If you would like to execute migrations in other
638
+ +config/database.yml+ file. If you would like to execute migrations in another
624
639
  environment, for instance in production, you must explicitly pass it when
625
640
  invoking the command: <tt>rake db:migrate RAILS_ENV=production</tt>.
626
641
 
@@ -689,7 +704,8 @@ end
689
704
  These changes will ensure that all posts have a name and a title, and that the
690
705
  title is at least five characters long. Rails can validate a variety of
691
706
  conditions in a model, including the presence or uniqueness of columns, their
692
- format, and the existence of associated objects.
707
+ format, and the existence of associated objects. Validations are covered in detail
708
+ in "Active Record Validations and Callbacks":active_record_validations_callbacks.html#validations-overview
693
709
 
694
710
  h4. Using the Console
695
711
 
@@ -702,8 +718,8 @@ $ rails console
702
718
  </shell>
703
719
 
704
720
  TIP: The default console will make changes to your database. You can instead
705
- open a console that will roll back any changes you make by using +rails console
706
- --sandbox+.
721
+ open a console that will roll back any changes you make by using <tt>rails console
722
+ --sandbox</tt>.
707
723
 
708
724
  After the console loads, you can use it to work with your application's models:
709
725
 
@@ -714,10 +730,8 @@ After the console loads, you can use it to work with your application's models:
714
730
  updated_at: nil>
715
731
  >> p.save
716
732
  => false
717
- >> p.errors
718
- => #<OrderedHash { :title=>["can't be blank",
719
- "is too short (minimum is 5 characters)"],
720
- :name=>["can't be blank"] }>
733
+ >> p.errors.full_messages
734
+ => ["Name can't be blank", "Title can't be blank", "Title is too short (minimum is 5 characters)"]
721
735
  </shell>
722
736
 
723
737
  This code shows creating a new +Post+ instance, attempting to save it and
@@ -727,13 +741,14 @@ inspecting the +errors+ of the post.
727
741
  When you're finished, type +exit+ and hit +return+ to exit the console.
728
742
 
729
743
  TIP: Unlike the development web server, the console does not automatically load
730
- your code afresh for each line. If you make changes to your models while the
731
- console is open, type +reload!+ at the console prompt to load them.
744
+ your code afresh for each line. If you make changes to your models (in your editor)
745
+ while the console is open, type +reload!+ at the console prompt to load them.
732
746
 
733
747
  h4. Listing All Posts
734
748
 
735
- The easiest place to start looking at functionality is with the code that lists
736
- all posts. Open the file +app/controllers/posts_controller.rb+ and look at the
749
+ Let's dive into the Rails code a little deeper to see how the application is
750
+ showing us the list of Posts. Open the file
751
+ +app/controllers/posts_controller.rb+ and look at the
737
752
  +index+ action:
738
753
 
739
754
  <ruby>
@@ -747,9 +762,8 @@ def index
747
762
  end
748
763
  </ruby>
749
764
 
750
- +Post.all+ calls the +Post+ model to return all of the posts currently in the
751
- database. The result of this call is an array of posts that we store in an
752
- instance variable called +@posts+.
765
+ +Post.all+ returns all of the posts currently in the database as an array
766
+ of +Post+ records that we store in an instance variable called +@posts+.
753
767
 
754
768
  TIP: For more information on finding records with Active Record, see "Active
755
769
  Record Query Interface":active_record_querying.html.
@@ -781,7 +795,8 @@ Here's +app/views/posts/index.html.erb+:
781
795
  <td><%= post.content %></td>
782
796
  <td><%= link_to 'Show', post %></td>
783
797
  <td><%= link_to 'Edit', edit_post_path(post) %></td>
784
- <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %></td>
798
+ <td><%= link_to 'Destroy', post, :confirm => 'Are you sure?',
799
+ :method => :delete %></td>
785
800
  </tr>
786
801
  <% end %>
787
802
  </table>
@@ -799,7 +814,7 @@ and links. A few things to note in the view:
799
814
 
800
815
  NOTE. In previous versions of Rails, you had to use +&lt;%=h post.name %&gt;+ so
801
816
  that any HTML would be escaped before being inserted into the page. In Rails
802
- 3.0, this is now the default. To get unescaped HTML, you now use +&lt;%= raw
817
+ 3.0+, this is now the default. To get unescaped HTML, you now use +&lt;%= raw
803
818
  post.name %&gt;+.
804
819
 
805
820
  TIP: For more details on the rendering process, see "Layouts and Rendering in
@@ -813,9 +828,10 @@ Rails renders a view to the browser, it does so by putting the view's HTML into
813
828
  a layout's HTML. In previous versions of Rails, the +rails generate scaffold+
814
829
  command would automatically create a controller specific layout, like
815
830
  +app/views/layouts/posts.html.erb+, for the posts controller. However this has
816
- been changed in Rails 3.0. An application specific +layout+ is used for all the
831
+ been changed in Rails 3.0+. An application specific +layout+ is used for all the
817
832
  controllers and can be found in +app/views/layouts/application.html.erb+. Open
818
- this layout in your editor and modify the +body+ tag:
833
+ this layout in your editor and modify the +body+ tag to include the style directive
834
+ below:
819
835
 
820
836
  <erb>
821
837
  <!DOCTYPE html>
@@ -865,10 +881,10 @@ The +new.html.erb+ view displays this empty Post to the user:
865
881
 
866
882
  The +&lt;%= render 'form' %&gt;+ line is our first introduction to _partials_ in
867
883
  Rails. A partial is a snippet of HTML and Ruby code that can be reused in
868
- multiple locations. In this case, the form used to make a new post, is basically
869
- identical to a form used to edit a post, both have text fields for the name and
870
- title and a text area for the content with a button to make a new post or update
871
- the existing post.
884
+ multiple locations. In this case, the form used to make a new post is basically
885
+ identical to the form used to edit a post, both having text fields for the name and
886
+ title, a text area for the content, and a button to create the new post or to update
887
+ the existing one.
872
888
 
873
889
  If you take a look at +views/posts/_form.html.erb+ file, you will see the
874
890
  following:
@@ -877,7 +893,8 @@ following:
877
893
  <%= form_for(@post) do |f| %>
878
894
  <% if @post.errors.any? %>
879
895
  <div id="errorExplanation">
880
- <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
896
+ <h2><%= pluralize(@post.errors.count, "error") %> prohibited
897
+ this post from being saved:</h2>
881
898
  <ul>
882
899
  <% @post.errors.full_messages.each do |msg| %>
883
900
  <li><%= msg %></li>
@@ -905,15 +922,15 @@ following:
905
922
  </erb>
906
923
 
907
924
  This partial receives all the instance variables defined in the calling view
908
- file, so in this case, the controller assigned the new Post object to +@post+
909
- and so, this is available in both the view and partial as +@post+.
925
+ file. In this case, the controller assigned the new +Post+ object to +@post+,
926
+ which will thus be available in both the view and the partial as +@post+.
910
927
 
911
928
  For more information on partials, refer to the "Layouts and Rendering in
912
929
  Rails":layouts_and_rendering.html#using-partials guide.
913
930
 
914
931
  The +form_for+ block is used to create an HTML form. Within this block, you have
915
932
  access to methods to build various controls on the form. For example,
916
- +f.text_field :name+ tells Rails to create a text input on the form, and to hook
933
+ +f.text_field :name+ tells Rails to create a text input on the form and to hook
917
934
  it up to the +name+ attribute of the instance being displayed. You can only use
918
935
  these methods with attributes of the model that the form is based on (in this
919
936
  case +name+, +title+, and +content+). Rails uses +form_for+ in preference to
@@ -929,9 +946,9 @@ to a model, you should use the +form_tag+ method, which provides shortcuts for
929
946
  building forms that are not necessarily tied to a model instance.
930
947
 
931
948
  When the user clicks the +Create Post+ button on this form, the browser will
932
- send information back to the +create+ method of the controller (Rails knows to
933
- call the +create+ method because the form is sent with an HTTP POST request;
934
- that's one of the conventions that I mentioned earlier):
949
+ send information back to the +create+ action of the controller (Rails knows to
950
+ call the +create+ action because the form is sent with an HTTP POST request;
951
+ that's one of the conventions that were mentioned earlier):
935
952
 
936
953
  <ruby>
937
954
  def create
@@ -963,12 +980,12 @@ If the post was not successfully saved, due to a validation error, then the
963
980
  controller returns the user back to the +new+ action with any error messages so
964
981
  that the user has the chance to fix the error and try again.
965
982
 
966
- The "Post was successfully created." message is stored inside of the Rails
967
- +flash+ hash, (usually just called _the flash_) so that messages can be carried
983
+ The "Post was successfully created." message is stored in the Rails
984
+ +flash+ hash (usually just called _the flash_), so that messages can be carried
968
985
  over to another action, providing the user with useful information on the status
969
986
  of their request. In the case of +create+, the user never actually sees any page
970
- rendered during the Post creation process, because it immediately redirects to
971
- the new Post as soon Rails saves the record. The Flash carries over a message to
987
+ rendered during the post creation process, because it immediately redirects to
988
+ the new +Post+ as soon as Rails saves the record. The Flash carries over a message to
972
989
  the next action, so that when the user is redirected back to the +show+ action,
973
990
  they are presented with a message saying "Post was successfully created."
974
991
 
@@ -992,7 +1009,7 @@ end
992
1009
 
993
1010
  The +show+ action uses +Post.find+ to search for a single record in the database
994
1011
  by its id value. After finding the record, Rails displays it by using
995
- +show.html.erb+:
1012
+ +app/views/posts/show.html.erb+:
996
1013
 
997
1014
  <erb>
998
1015
  <p class="notice"><%= notice %></p>
@@ -1041,9 +1058,9 @@ it:
1041
1058
  <%= link_to 'Back', posts_path %>
1042
1059
  </erb>
1043
1060
 
1044
- Again, as with the +new+ action, the +edit+ action is using the +form+ partial,
1045
- this time however, the form will do a PUT action to the PostsController and the
1046
- submit button will display "Update Post"
1061
+ Again, as with the +new+ action, the +edit+ action is using the +form+ partial.
1062
+ This time, however, the form will do a PUT action to the +PostsController+ and the
1063
+ submit button will display "Update Post".
1047
1064
 
1048
1065
  Submitting the form created by this view will invoke the +update+ action within
1049
1066
  the controller:
@@ -1056,7 +1073,7 @@ def update
1056
1073
  if @post.update_attributes(params[:post])
1057
1074
  format.html { redirect_to(@post,
1058
1075
  :notice => 'Post was successfully updated.') }
1059
- format.json { render :json => {}, :status => :ok }
1076
+ format.json { head :no_content }
1060
1077
  else
1061
1078
  format.html { render :action => "edit" }
1062
1079
  format.json { render :json => @post.errors,
@@ -1068,9 +1085,9 @@ end
1068
1085
 
1069
1086
  In the +update+ action, Rails first uses the +:id+ parameter passed back from
1070
1087
  the edit view to locate the database record that's being edited. The
1071
- +update_attributes+ call then takes the rest of the parameters from the request
1072
- and applies them to this record. If all goes well, the user is redirected to the
1073
- post's +show+ view. If there are any problems, it's back to the +edit+ view to
1088
+ +update_attributes+ call then takes the +post+ parameter (a hash) from the request
1089
+ and applies it to this record. If all goes well, the user is redirected to the
1090
+ post's +show+ action. If there are any problems, it redirects back to the +edit+ action to
1074
1091
  correct them.
1075
1092
 
1076
1093
  h4. Destroying a Post
@@ -1085,19 +1102,19 @@ def destroy
1085
1102
 
1086
1103
  respond_to do |format|
1087
1104
  format.html { redirect_to posts_url }
1088
- format.json { head :ok }
1105
+ format.json { head :no_content }
1089
1106
  end
1090
1107
  end
1091
1108
  </ruby>
1092
1109
 
1093
1110
  The +destroy+ method of an Active Record model instance removes the
1094
1111
  corresponding record from the database. After that's done, there isn't any
1095
- record to display, so Rails redirects the user's browser to the index view for
1096
- the model.
1112
+ record to display, so Rails redirects the user's browser to the index action of
1113
+ the controller.
1097
1114
 
1098
1115
  h3. Adding a Second Model
1099
1116
 
1100
- Now that you've seen how a model built with scaffolding looks like, it's time to
1117
+ Now that you've seen what a model built with scaffolding looks like, it's time to
1101
1118
  add a second model to the application. The second model will handle comments on
1102
1119
  blog posts.
1103
1120
 
@@ -1105,7 +1122,7 @@ h4. Generating a Model
1105
1122
 
1106
1123
  Models in Rails use a singular name, and their corresponding database tables use
1107
1124
  a plural name. For the model to hold comments, the convention is to use the name
1108
- Comment. Even if you don't want to use the entire apparatus set up by
1125
+ +Comment+. Even if you don't want to use the entire apparatus set up by
1109
1126
  scaffolding, most Rails developers still use generators to make things like
1110
1127
  models and controllers. To create the new model, run this command in your
1111
1128
  terminal:
@@ -1116,9 +1133,11 @@ $ rails generate model Comment commenter:string body:text post:references
1116
1133
 
1117
1134
  This command will generate four files:
1118
1135
 
1119
- * +app/models/comment.rb+ - The model
1120
- * +db/migrate/20100207235629_create_comments.rb+ - The migration
1121
- * +test/unit/comment_test.rb+ and +test/fixtures/comments.yml+ - The test harness.
1136
+ |_.File |_.Purpose|
1137
+ |db/migrate/20100207235629_create_comments.rb | Migration to create the comments table in your database (your name will include a different timestamp) |
1138
+ | app/models/comment.rb | The Comment model |
1139
+ | test/unit/comment_test.rb | Unit testing harness for the comments model |
1140
+ | test/fixtures/comments.yml | Sample comments for use in testing |
1122
1141
 
1123
1142
  First, take a look at +comment.rb+:
1124
1143
 
@@ -1165,8 +1184,10 @@ run against the current database, so in this case you will just see:
1165
1184
  <shell>
1166
1185
  == CreateComments: migrating =================================================
1167
1186
  -- create_table(:comments)
1168
- -> 0.0017s
1169
- == CreateComments: migrated (0.0018s) ========================================
1187
+ -> 0.0008s
1188
+ -- add_index(:comments, :post_id)
1189
+ -> 0.0003s
1190
+ == CreateComments: migrated (0.0012s) ========================================
1170
1191
  </shell>
1171
1192
 
1172
1193
  h4. Associating Models
@@ -1175,8 +1196,8 @@ Active Record associations let you easily declare the relationship between two
1175
1196
  models. In the case of comments and posts, you could write out the relationships
1176
1197
  this way:
1177
1198
 
1178
- * Each comment belongs to one post
1179
- * One post can have many comments
1199
+ * Each comment belongs to one post.
1200
+ * One post can have many comments.
1180
1201
 
1181
1202
  In fact, this is very close to the syntax that Rails uses to declare this
1182
1203
  association. You've already seen the line of code inside the Comment model that
@@ -1202,7 +1223,7 @@ end
1202
1223
 
1203
1224
  These two declarations enable a good bit of automatic behavior. For example, if
1204
1225
  you have an instance variable +@post+ containing a post, you can retrieve all
1205
- the comments belonging to that post as the array +@post.comments+.
1226
+ the comments belonging to that post as an array using +@post.comments+.
1206
1227
 
1207
1228
  TIP: For more information on Active Record associations, see the "Active Record
1208
1229
  Associations":association_basics.html guide.
@@ -1211,9 +1232,9 @@ h4. Adding a Route for Comments
1211
1232
 
1212
1233
  As with the +home+ controller, we will need to add a route so that Rails knows
1213
1234
  where we would like to navigate to see +comments+. Open up the
1214
- +config/routes.rb+ file again, you will see an entry that was added
1215
- automatically for +posts+ near the top by the scaffold generator, +resources
1216
- :posts+, edit it as follows:
1235
+ +config/routes.rb+ file again. Near the top, you will see the entry for +posts+
1236
+ that was added automatically by the scaffold generator: <tt>resources
1237
+ :posts</tt>. Edit it as follows:
1217
1238
 
1218
1239
  <ruby>
1219
1240
  resources :posts do
@@ -1239,19 +1260,20 @@ $ rails generate controller Comments
1239
1260
 
1240
1261
  This creates six files and one empty directory:
1241
1262
 
1242
- * +app/controllers/comments_controller.rb+ - The controller
1243
- * +app/helpers/comments_helper.rb+ - A view helper file
1244
- * +test/functional/comments_controller_test.rb+ - The functional tests for the controller
1245
- * +test/unit/helpers/comments_helper_test.rb+ - The unit tests for the helper
1246
- * +app/views/comments/+ - Views of the controller are stored here
1247
- * +app/assets/stylesheets/comment.css.scss+ - Cascading style sheet for the controller
1248
- * +app/assets/javascripts/comment.js.coffee+ - CoffeeScript for the controller
1263
+ |_.File/Directory |_.Purpose |
1264
+ | app/controllers/comments_controller.rb | The Comments controller |
1265
+ | app/views/comments/ | Views of the controller are stored here |
1266
+ | test/functional/comments_controller_test.rb | The functional tests for the controller |
1267
+ | app/helpers/comments_helper.rb | A view helper file |
1268
+ | test/unit/helpers/comments_helper_test.rb | The unit tests for the helper |
1269
+ | app/assets/javascripts/comment.js.coffee | CoffeeScript for the controller |
1270
+ | app/assets/stylesheets/comment.css.scss | Cascading style sheet for the controller |
1249
1271
 
1250
1272
  Like with any blog, our readers will create their comments directly after
1251
1273
  reading the post, and once they have added their comment, will be sent back to
1252
1274
  the post show page to see their comment now listed. Due to this, our
1253
1275
  +CommentsController+ is there to provide a method to create comments and delete
1254
- SPAM comments when they arrive.
1276
+ spam comments when they arrive.
1255
1277
 
1256
1278
  So first, we'll wire up the Post show template
1257
1279
  (+/app/views/posts/show.html.erb+) to let us make a new comment:
@@ -1293,8 +1315,8 @@ So first, we'll wire up the Post show template
1293
1315
  <%= link_to 'Back to Posts', posts_path %> |
1294
1316
  </erb>
1295
1317
 
1296
- This adds a form on the Post show page that creates a new comment, which will
1297
- call the +CommentsController+ +create+ action, so let's wire that up:
1318
+ This adds a form on the +Post+ show page that creates a new comment by
1319
+ calling the +CommentsController+ +create+ action. Let's wire that up:
1298
1320
 
1299
1321
  <ruby>
1300
1322
  class CommentsController < ApplicationController
@@ -1307,9 +1329,9 @@ end
1307
1329
  </ruby>
1308
1330
 
1309
1331
  You'll see a bit more complexity here than you did in the controller for posts.
1310
- That's a side-effect of the nesting that you've set up; each request for a
1332
+ That's a side-effect of the nesting that you've set up. Each request for a
1311
1333
  comment has to keep track of the post to which the comment is attached, thus the
1312
- initial find action to the Post model to get the post in question.
1334
+ initial call to the +find+ method of the +Post+ model to get the post in question.
1313
1335
 
1314
1336
  In addition, the code takes advantage of some of the methods available for an
1315
1337
  association. We use the +create+ method on +@post.comments+ to create and save
@@ -1379,9 +1401,9 @@ right places.
1379
1401
 
1380
1402
  h3. Refactoring
1381
1403
 
1382
- Now that we have Posts and Comments working, if we take a look at the
1383
- +app/views/posts/show.html.erb+ template, it's getting long and awkward. We can
1384
- use partials to clean this up.
1404
+ Now that we have posts and comments working, take a look at the
1405
+ +app/views/posts/show.html.erb+ template. It is getting long and awkward. We can
1406
+ use partials to clean it up.
1385
1407
 
1386
1408
  h4. Rendering Partial Collections
1387
1409
 
@@ -1401,7 +1423,7 @@ following into it:
1401
1423
  </p>
1402
1424
  </erb>
1403
1425
 
1404
- Then in the +app/views/posts/show.html.erb+ you can change it to look like the
1426
+ Then you can change +app/views/posts/show.html.erb+ to look like the
1405
1427
  following:
1406
1428
 
1407
1429
  <erb>
@@ -1454,8 +1476,8 @@ comment to a local variable named the same as the partial, in this case
1454
1476
 
1455
1477
  h4. Rendering a Partial Form
1456
1478
 
1457
- Let's also move that new comment section out to its own partial. Again, you
1458
- create a file +app/views/comments/_form.html.erb+ and in it you put:
1479
+ Let us also move that new comment section out to its own partial. Again, you
1480
+ create a file +app/views/comments/_form.html.erb+ containing:
1459
1481
 
1460
1482
  <erb>
1461
1483
  <%= form_for([@post, @post.comments.build]) do |f| %>
@@ -1506,7 +1528,7 @@ Then you make the +app/views/posts/show.html.erb+ look like the following:
1506
1528
  </erb>
1507
1529
 
1508
1530
  The second render just defines the partial template we want to render,
1509
- <tt>comments/form</tt>, Rails is smart enough to spot the forward slash in that
1531
+ <tt>comments/form</tt>. Rails is smart enough to spot the forward slash in that
1510
1532
  string and realize that you want to render the <tt>_form.html.erb</tt> file in
1511
1533
  the <tt>app/views/comments</tt> directory.
1512
1534
 
@@ -1515,7 +1537,7 @@ defined it as an instance variable.
1515
1537
 
1516
1538
  h3. Deleting Comments
1517
1539
 
1518
- Another important feature on a blog is being able to delete SPAM comments. To do
1540
+ Another important feature of a blog is being able to delete spam comments. To do
1519
1541
  this, we need to implement a link of some sort in the view and a +DELETE+ action
1520
1542
  in the +CommentsController+.
1521
1543
 
@@ -1605,7 +1627,7 @@ action, except for +index+ and +show+, so we write that:
1605
1627
  <ruby>
1606
1628
  class PostsController < ApplicationController
1607
1629
 
1608
- http_basic_authenticate_with :name => "dhh", :password => "secret", :except => :index
1630
+ http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show]
1609
1631
 
1610
1632
  # GET /posts
1611
1633
  # GET /posts.json
@@ -1633,7 +1655,6 @@ Authentication challenge
1633
1655
 
1634
1656
  !images/challenge.png(Basic HTTP Authentication Challenge)!
1635
1657
 
1636
-
1637
1658
  h3. Building a Multi-Model Form
1638
1659
 
1639
1660
  Another feature of your average blog is the ability to tag posts. To implement
@@ -1887,24 +1908,3 @@ Two very common sources of data that are not UTF-8:
1887
1908
  is using Latin-1 internally, and your user enters a Russian, Hebrew, or Japanese
1888
1909
  character, the data will be lost forever once it enters the database. If possible,
1889
1910
  use UTF-8 as the internal storage of your database.
1890
-
1891
- h3. Changelog
1892
-
1893
- * April 26, 2011: Change migration code from +up+, +down+ pair to +change+ method by "Prem Sichanugrist":http://sikachu.com
1894
- * April 11, 2011: Change scaffold_controller generator to create format block for JSON instead of XML by "Sebastian Martinez":http://www.wyeworks.com
1895
- * August 30, 2010: Minor editing after Rails 3 release by Joost Baaij
1896
- * July 12, 2010: Fixes, editing and updating of code samples by "Jaime Iniesta":http://jaimeiniesta.com
1897
- * May 16, 2010: Added a section on configuration gotchas to address common encoding problems that people might have by "Yehuda Katz":http://www.yehudakatz.com
1898
- * April 30, 2010: Fixes, editing and updating of code samples by "Rohit Arondekar":http://rohitarondekar.com
1899
- * April 25, 2010: Couple of more minor fixups by "Mikel Lindsaar":credits.html#raasdnil
1900
- * April 1, 2010: Fixed document to validate XHTML 1.0 Strict by "Jaime Iniesta":http://jaimeiniesta.com
1901
- * February 8, 2010: Full re-write for Rails 3.0-beta, added helpers and before_filters, refactored code by "Mikel Lindsaar":credits.html#raasdnil
1902
- * January 24, 2010: Re-write for Rails 3.0 by "Mikel Lindsaar":credits.html#raasdnil
1903
- * July 18, 2009: Minor cleanup in anticipation of Rails 2.3.3 by "Mike Gunderloy":credits.html#mgunderloy
1904
- * February 1, 2009: Updated for Rails 2.3 by "Mike Gunderloy":credits.html#mgunderloy
1905
- * November 3, 2008: Formatting patch from Dave Rothlisberger
1906
- * November 1, 2008: First approved version by "Mike Gunderloy":credits.html#mgunderloy
1907
- * October 16, 2008: Revised based on feedback from Pratik Naik by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
1908
- * October 13, 2008: First complete draft by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
1909
- * October 12, 2008: More detail, rearrangement, editing by "Mike Gunderloy":credits.html#mgunderloy (not yet approved for publication)
1910
- * September 8, 2008: initial version by "James Miller":credits.html#bensie (not yet approved for publication)