rails 2.2.2 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- data/bin/rails +8 -18
- metadata +118 -452
- data/CHANGELOG +0 -2071
- data/MIT-LICENSE +0 -20
- data/README +0 -256
- data/Rakefile +0 -411
- data/bin/about +0 -4
- data/bin/console +0 -3
- data/bin/dbconsole +0 -3
- data/bin/destroy +0 -3
- data/bin/generate +0 -3
- data/bin/performance/benchmarker +0 -3
- data/bin/performance/profiler +0 -3
- data/bin/performance/request +0 -3
- data/bin/plugin +0 -3
- data/bin/process/inspector +0 -3
- data/bin/process/reaper +0 -3
- data/bin/process/spawner +0 -3
- data/bin/runner +0 -3
- data/bin/server +0 -3
- data/builtin/rails_info/rails/info.rb +0 -125
- data/builtin/rails_info/rails/info_controller.rb +0 -9
- data/builtin/rails_info/rails/info_helper.rb +0 -2
- data/builtin/rails_info/rails_info_controller.rb +0 -2
- data/config.ru +0 -17
- data/configs/apache.conf +0 -40
- data/configs/databases/frontbase.yml +0 -28
- data/configs/databases/ibm_db.yml +0 -62
- data/configs/databases/mysql.yml +0 -57
- data/configs/databases/oracle.yml +0 -39
- data/configs/databases/postgresql.yml +0 -51
- data/configs/databases/sqlite2.yml +0 -19
- data/configs/databases/sqlite3.yml +0 -22
- data/configs/empty.log +0 -0
- data/configs/initializers/inflections.rb +0 -10
- data/configs/initializers/mime_types.rb +0 -5
- data/configs/initializers/new_rails_defaults.rb +0 -17
- data/configs/lighttpd.conf +0 -54
- data/configs/locales/en.yml +0 -5
- data/configs/routes.rb +0 -43
- data/dispatches/dispatch.fcgi +0 -24
- data/dispatches/dispatch.rb +0 -10
- data/dispatches/gateway.cgi +0 -97
- data/doc/README_FOR_APP +0 -5
- data/doc/guides/html/2_2_release_notes.html +0 -1185
- data/doc/guides/html/actioncontroller_basics.html +0 -1270
- data/doc/guides/html/activerecord_validations_callbacks.html +0 -749
- data/doc/guides/html/association_basics.html +0 -2585
- data/doc/guides/html/authors.html +0 -240
- data/doc/guides/html/benchmarking_and_profiling.html +0 -1018
- data/doc/guides/html/caching_with_rails.html +0 -583
- data/doc/guides/html/command_line.html +0 -434
- data/doc/guides/html/configuring.html +0 -438
- data/doc/guides/html/creating_plugins.html +0 -1594
- data/doc/guides/html/debugging_rails_applications.html +0 -1175
- data/doc/guides/html/finders.html +0 -1090
- data/doc/guides/html/form_helpers.html +0 -638
- data/doc/guides/html/getting_started_with_rails.html +0 -2066
- data/doc/guides/html/index.html +0 -349
- data/doc/guides/html/layouts_and_rendering.html +0 -1406
- data/doc/guides/html/migrations.html +0 -921
- data/doc/guides/html/routing_outside_in.html +0 -2213
- data/doc/guides/html/security.html +0 -1346
- data/doc/guides/html/testing_rails_applications.html +0 -1859
- data/doc/guides/source/2_2_release_notes.txt +0 -435
- data/doc/guides/source/actioncontroller_basics/changelog.txt +0 -5
- data/doc/guides/source/actioncontroller_basics/cookies.txt +0 -34
- data/doc/guides/source/actioncontroller_basics/csrf.txt +0 -32
- data/doc/guides/source/actioncontroller_basics/filters.txt +0 -119
- data/doc/guides/source/actioncontroller_basics/http_auth.txt +0 -24
- data/doc/guides/source/actioncontroller_basics/index.txt +0 -40
- data/doc/guides/source/actioncontroller_basics/introduction.txt +0 -9
- data/doc/guides/source/actioncontroller_basics/methods.txt +0 -39
- data/doc/guides/source/actioncontroller_basics/parameter_filtering.txt +0 -14
- data/doc/guides/source/actioncontroller_basics/params.txt +0 -93
- data/doc/guides/source/actioncontroller_basics/request_response_objects.txt +0 -43
- data/doc/guides/source/actioncontroller_basics/rescue.txt +0 -67
- data/doc/guides/source/actioncontroller_basics/session.txt +0 -187
- data/doc/guides/source/actioncontroller_basics/streaming.txt +0 -91
- data/doc/guides/source/actioncontroller_basics/verification.txt +0 -40
- data/doc/guides/source/active_record_basics.txt +0 -181
- data/doc/guides/source/activerecord_validations_callbacks.txt +0 -404
- data/doc/guides/source/association_basics.txt +0 -1840
- data/doc/guides/source/authors.txt +0 -39
- data/doc/guides/source/benchmarking_and_profiling/appendix.txt +0 -95
- data/doc/guides/source/benchmarking_and_profiling/digging_deeper.txt +0 -105
- data/doc/guides/source/benchmarking_and_profiling/edge_rails_features.txt +0 -185
- data/doc/guides/source/benchmarking_and_profiling/gameplan.txt +0 -27
- data/doc/guides/source/benchmarking_and_profiling/index.txt +0 -242
- data/doc/guides/source/benchmarking_and_profiling/rubyprof.txt +0 -179
- data/doc/guides/source/benchmarking_and_profiling/statistics.txt +0 -57
- data/doc/guides/source/caching_with_rails.txt +0 -367
- data/doc/guides/source/command_line.txt +0 -147
- data/doc/guides/source/configuring.txt +0 -225
- data/doc/guides/source/creating_plugins/acts_as_yaffle.txt +0 -191
- data/doc/guides/source/creating_plugins/appendix.txt +0 -46
- data/doc/guides/source/creating_plugins/controllers.txt +0 -59
- data/doc/guides/source/creating_plugins/core_ext.txt +0 -123
- data/doc/guides/source/creating_plugins/custom_route.txt +0 -69
- data/doc/guides/source/creating_plugins/gem.txt +0 -1
- data/doc/guides/source/creating_plugins/generator_method.txt +0 -89
- data/doc/guides/source/creating_plugins/helpers.txt +0 -51
- data/doc/guides/source/creating_plugins/index.txt +0 -52
- data/doc/guides/source/creating_plugins/migration_generator.txt +0 -156
- data/doc/guides/source/creating_plugins/models.txt +0 -76
- data/doc/guides/source/creating_plugins/odds_and_ends.txt +0 -69
- data/doc/guides/source/creating_plugins/test_setup.txt +0 -230
- data/doc/guides/source/debugging_rails_applications.txt +0 -733
- data/doc/guides/source/finders.txt +0 -668
- data/doc/guides/source/form_helpers.txt +0 -345
- data/doc/guides/source/getting_started_with_rails.txt +0 -1256
- data/doc/guides/source/images/belongs_to.png +0 -0
- data/doc/guides/source/images/bullet.gif +0 -0
- data/doc/guides/source/images/csrf.png +0 -0
- data/doc/guides/source/images/habtm.png +0 -0
- data/doc/guides/source/images/has_many.png +0 -0
- data/doc/guides/source/images/has_many_through.png +0 -0
- data/doc/guides/source/images/has_one.png +0 -0
- data/doc/guides/source/images/has_one_through.png +0 -0
- data/doc/guides/source/images/header_backdrop.png +0 -0
- data/doc/guides/source/images/icons/README +0 -5
- data/doc/guides/source/images/icons/callouts/1.png +0 -0
- data/doc/guides/source/images/icons/callouts/10.png +0 -0
- data/doc/guides/source/images/icons/callouts/11.png +0 -0
- data/doc/guides/source/images/icons/callouts/12.png +0 -0
- data/doc/guides/source/images/icons/callouts/13.png +0 -0
- data/doc/guides/source/images/icons/callouts/14.png +0 -0
- data/doc/guides/source/images/icons/callouts/15.png +0 -0
- data/doc/guides/source/images/icons/callouts/2.png +0 -0
- data/doc/guides/source/images/icons/callouts/3.png +0 -0
- data/doc/guides/source/images/icons/callouts/4.png +0 -0
- data/doc/guides/source/images/icons/callouts/5.png +0 -0
- data/doc/guides/source/images/icons/callouts/6.png +0 -0
- data/doc/guides/source/images/icons/callouts/7.png +0 -0
- data/doc/guides/source/images/icons/callouts/8.png +0 -0
- data/doc/guides/source/images/icons/callouts/9.png +0 -0
- data/doc/guides/source/images/icons/caution.png +0 -0
- data/doc/guides/source/images/icons/example.png +0 -0
- data/doc/guides/source/images/icons/home.png +0 -0
- data/doc/guides/source/images/icons/important.png +0 -0
- data/doc/guides/source/images/icons/next.png +0 -0
- data/doc/guides/source/images/icons/note.png +0 -0
- data/doc/guides/source/images/icons/prev.png +0 -0
- data/doc/guides/source/images/icons/tip.png +0 -0
- data/doc/guides/source/images/icons/up.png +0 -0
- data/doc/guides/source/images/icons/warning.png +0 -0
- data/doc/guides/source/images/polymorphic.png +0 -0
- data/doc/guides/source/images/rails_logo_remix.gif +0 -0
- data/doc/guides/source/images/ruby_on_rails_by_mike_rundle2.gif +0 -0
- data/doc/guides/source/images/session_fixation.png +0 -0
- data/doc/guides/source/index.txt +0 -118
- data/doc/guides/source/layouts_and_rendering.txt +0 -982
- data/doc/guides/source/migrations/anatomy_of_a_migration.txt +0 -85
- data/doc/guides/source/migrations/changelog.txt +0 -5
- data/doc/guides/source/migrations/creating_a_migration.txt +0 -109
- data/doc/guides/source/migrations/foreign_keys.txt +0 -8
- data/doc/guides/source/migrations/index.txt +0 -22
- data/doc/guides/source/migrations/rakeing_around.txt +0 -111
- data/doc/guides/source/migrations/scheming.txt +0 -47
- data/doc/guides/source/migrations/using_models_in_migrations.txt +0 -46
- data/doc/guides/source/migrations/writing_a_migration.txt +0 -159
- data/doc/guides/source/routing_outside_in.txt +0 -986
- data/doc/guides/source/security.txt +0 -984
- data/doc/guides/source/stylesheets/base.css +0 -358
- data/doc/guides/source/stylesheets/forms.css +0 -35
- data/doc/guides/source/stylesheets/more.css +0 -82
- data/doc/guides/source/templates/guides.html.erb +0 -97
- data/doc/guides/source/templates/inline.css +0 -165
- data/doc/guides/source/testing_rails_applications.txt +0 -995
- data/environments/boot.rb +0 -109
- data/environments/development.rb +0 -17
- data/environments/environment.rb +0 -75
- data/environments/production.rb +0 -24
- data/environments/test.rb +0 -22
- data/fresh_rakefile +0 -10
- data/helpers/application.rb +0 -15
- data/helpers/application_helper.rb +0 -3
- data/helpers/performance_test.rb +0 -9
- data/helpers/test_helper.rb +0 -38
- data/html/404.html +0 -30
- data/html/422.html +0 -30
- data/html/500.html +0 -33
- data/html/favicon.ico +0 -0
- data/html/images/rails.png +0 -0
- data/html/index.html +0 -274
- data/html/javascripts/application.js +0 -2
- data/html/javascripts/controls.js +0 -963
- data/html/javascripts/dragdrop.js +0 -973
- data/html/javascripts/effects.js +0 -1128
- data/html/javascripts/prototype.js +0 -4320
- data/html/robots.txt +0 -5
- data/lib/code_statistics.rb +0 -107
- data/lib/commands.rb +0 -17
- data/lib/commands/about.rb +0 -3
- data/lib/commands/console.rb +0 -45
- data/lib/commands/dbconsole.rb +0 -67
- data/lib/commands/destroy.rb +0 -6
- data/lib/commands/generate.rb +0 -6
- data/lib/commands/ncgi/listener +0 -86
- data/lib/commands/ncgi/tracker +0 -69
- data/lib/commands/performance/benchmarker.rb +0 -24
- data/lib/commands/performance/profiler.rb +0 -50
- data/lib/commands/performance/request.rb +0 -6
- data/lib/commands/plugin.rb +0 -967
- data/lib/commands/process/inspector.rb +0 -68
- data/lib/commands/process/reaper.rb +0 -149
- data/lib/commands/process/spawner.rb +0 -219
- data/lib/commands/process/spinner.rb +0 -57
- data/lib/commands/runner.rb +0 -48
- data/lib/commands/server.rb +0 -49
- data/lib/commands/servers/base.rb +0 -31
- data/lib/commands/servers/lighttpd.rb +0 -94
- data/lib/commands/servers/mongrel.rb +0 -69
- data/lib/commands/servers/new_mongrel.rb +0 -16
- data/lib/commands/servers/thin.rb +0 -25
- data/lib/commands/servers/webrick.rb +0 -66
- data/lib/commands/update.rb +0 -4
- data/lib/console_app.rb +0 -30
- data/lib/console_sandbox.rb +0 -6
- data/lib/console_with_helpers.rb +0 -26
- data/lib/dispatcher.rb +0 -24
- data/lib/fcgi_handler.rb +0 -237
- data/lib/initializer.rb +0 -1038
- data/lib/performance_test_help.rb +0 -5
- data/lib/rails/gem_builder.rb +0 -21
- data/lib/rails/gem_dependency.rb +0 -251
- data/lib/rails/mongrel_server/commands.rb +0 -342
- data/lib/rails/mongrel_server/handler.rb +0 -55
- data/lib/rails/plugin.rb +0 -124
- data/lib/rails/plugin/loader.rb +0 -152
- data/lib/rails/plugin/locator.rb +0 -100
- data/lib/rails/rack.rb +0 -6
- data/lib/rails/rack/logger.rb +0 -28
- data/lib/rails/rack/static.rb +0 -35
- data/lib/rails/vendor_gem_source_index.rb +0 -140
- data/lib/rails/version.rb +0 -9
- data/lib/rails_generator.rb +0 -43
- data/lib/rails_generator/base.rb +0 -263
- data/lib/rails_generator/commands.rb +0 -616
- data/lib/rails_generator/generated_attribute.rb +0 -46
- data/lib/rails_generator/generators/applications/app/USAGE +0 -9
- data/lib/rails_generator/generators/applications/app/app_generator.rb +0 -184
- data/lib/rails_generator/generators/components/controller/USAGE +0 -29
- data/lib/rails_generator/generators/components/controller/controller_generator.rb +0 -37
- data/lib/rails_generator/generators/components/controller/templates/controller.rb +0 -7
- data/lib/rails_generator/generators/components/controller/templates/functional_test.rb +0 -8
- data/lib/rails_generator/generators/components/controller/templates/helper.rb +0 -2
- data/lib/rails_generator/generators/components/controller/templates/view.html.erb +0 -2
- data/lib/rails_generator/generators/components/integration_test/USAGE +0 -8
- data/lib/rails_generator/generators/components/integration_test/integration_test_generator.rb +0 -16
- data/lib/rails_generator/generators/components/integration_test/templates/integration_test.rb +0 -10
- data/lib/rails_generator/generators/components/mailer/USAGE +0 -16
- data/lib/rails_generator/generators/components/mailer/mailer_generator.rb +0 -30
- data/lib/rails_generator/generators/components/mailer/templates/fixture.erb +0 -3
- data/lib/rails_generator/generators/components/mailer/templates/fixture.rhtml +0 -0
- data/lib/rails_generator/generators/components/mailer/templates/mailer.rb +0 -15
- data/lib/rails_generator/generators/components/mailer/templates/unit_test.rb +0 -20
- data/lib/rails_generator/generators/components/mailer/templates/view.erb +0 -3
- data/lib/rails_generator/generators/components/mailer/templates/view.rhtml +0 -0
- data/lib/rails_generator/generators/components/migration/USAGE +0 -29
- data/lib/rails_generator/generators/components/migration/migration_generator.rb +0 -20
- data/lib/rails_generator/generators/components/migration/templates/migration.rb +0 -11
- data/lib/rails_generator/generators/components/model/USAGE +0 -27
- data/lib/rails_generator/generators/components/model/model_generator.rb +0 -45
- data/lib/rails_generator/generators/components/model/templates/fixtures.yml +0 -19
- data/lib/rails_generator/generators/components/model/templates/migration.rb +0 -16
- data/lib/rails_generator/generators/components/model/templates/model.rb +0 -5
- data/lib/rails_generator/generators/components/model/templates/unit_test.rb +0 -8
- data/lib/rails_generator/generators/components/observer/USAGE +0 -13
- data/lib/rails_generator/generators/components/observer/observer_generator.rb +0 -16
- data/lib/rails_generator/generators/components/observer/templates/observer.rb +0 -2
- data/lib/rails_generator/generators/components/observer/templates/unit_test.rb +0 -8
- data/lib/rails_generator/generators/components/performance_test/USAGE +0 -8
- data/lib/rails_generator/generators/components/performance_test/performance_test_generator.rb +0 -16
- data/lib/rails_generator/generators/components/performance_test/templates/performance_test.rb +0 -9
- data/lib/rails_generator/generators/components/plugin/USAGE +0 -25
- data/lib/rails_generator/generators/components/plugin/plugin_generator.rb +0 -39
- data/lib/rails_generator/generators/components/plugin/templates/MIT-LICENSE +0 -20
- data/lib/rails_generator/generators/components/plugin/templates/README +0 -13
- data/lib/rails_generator/generators/components/plugin/templates/Rakefile +0 -23
- data/lib/rails_generator/generators/components/plugin/templates/USAGE +0 -8
- data/lib/rails_generator/generators/components/plugin/templates/generator.rb +0 -8
- data/lib/rails_generator/generators/components/plugin/templates/init.rb +0 -1
- data/lib/rails_generator/generators/components/plugin/templates/install.rb +0 -1
- data/lib/rails_generator/generators/components/plugin/templates/plugin.rb +0 -1
- data/lib/rails_generator/generators/components/plugin/templates/tasks.rake +0 -4
- data/lib/rails_generator/generators/components/plugin/templates/test_helper.rb +0 -3
- data/lib/rails_generator/generators/components/plugin/templates/uninstall.rb +0 -1
- data/lib/rails_generator/generators/components/plugin/templates/unit_test.rb +0 -8
- data/lib/rails_generator/generators/components/resource/USAGE +0 -23
- data/lib/rails_generator/generators/components/resource/resource_generator.rb +0 -74
- data/lib/rails_generator/generators/components/resource/templates/controller.rb +0 -2
- data/lib/rails_generator/generators/components/resource/templates/functional_test.rb +0 -8
- data/lib/rails_generator/generators/components/resource/templates/helper.rb +0 -2
- data/lib/rails_generator/generators/components/scaffold/USAGE +0 -29
- data/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb +0 -100
- data/lib/rails_generator/generators/components/scaffold/templates/controller.rb +0 -85
- data/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb +0 -45
- data/lib/rails_generator/generators/components/scaffold/templates/helper.rb +0 -2
- data/lib/rails_generator/generators/components/scaffold/templates/layout.html.erb +0 -17
- data/lib/rails_generator/generators/components/scaffold/templates/style.css +0 -54
- data/lib/rails_generator/generators/components/scaffold/templates/view_edit.html.erb +0 -18
- data/lib/rails_generator/generators/components/scaffold/templates/view_index.html.erb +0 -24
- data/lib/rails_generator/generators/components/scaffold/templates/view_new.html.erb +0 -17
- data/lib/rails_generator/generators/components/scaffold/templates/view_show.html.erb +0 -10
- data/lib/rails_generator/generators/components/session_migration/USAGE +0 -10
- data/lib/rails_generator/generators/components/session_migration/session_migration_generator.rb +0 -18
- data/lib/rails_generator/generators/components/session_migration/templates/migration.rb +0 -16
- data/lib/rails_generator/lookup.rb +0 -249
- data/lib/rails_generator/manifest.rb +0 -53
- data/lib/rails_generator/options.rb +0 -150
- data/lib/rails_generator/scripts.rb +0 -89
- data/lib/rails_generator/scripts/destroy.rb +0 -29
- data/lib/rails_generator/scripts/generate.rb +0 -7
- data/lib/rails_generator/scripts/update.rb +0 -12
- data/lib/rails_generator/secret_key_generator.rb +0 -22
- data/lib/rails_generator/simple_logger.rb +0 -46
- data/lib/rails_generator/spec.rb +0 -44
- data/lib/railties_path.rb +0 -1
- data/lib/ruby_version_check.rb +0 -17
- data/lib/rubyprof_ext.rb +0 -35
- data/lib/source_annotation_extractor.rb +0 -102
- data/lib/tasks/annotations.rake +0 -20
- data/lib/tasks/databases.rake +0 -416
- data/lib/tasks/documentation.rake +0 -95
- data/lib/tasks/framework.rake +0 -118
- data/lib/tasks/gems.rake +0 -80
- data/lib/tasks/log.rake +0 -9
- data/lib/tasks/misc.rake +0 -56
- data/lib/tasks/rails.rb +0 -8
- data/lib/tasks/routes.rake +0 -17
- data/lib/tasks/statistics.rake +0 -18
- data/lib/tasks/testing.rake +0 -139
- data/lib/tasks/tmp.rake +0 -37
- data/lib/test_help.rb +0 -27
- data/lib/webrick_server.rb +0 -156
@@ -1,2066 +0,0 @@
|
|
1
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
2
|
-
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
|
3
|
-
<head>
|
4
|
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
5
|
-
<title>Getting Started With Rails</title>
|
6
|
-
<!--[if lt IE 8]>
|
7
|
-
<script src="http://ie7-js.googlecode.com/svn/version/2.0(beta3)/IE8.js" type="text/javascript"></script>
|
8
|
-
<![endif]-->
|
9
|
-
<link href="stylesheets/base.css" media="screen" rel="Stylesheet" type="text/css" />
|
10
|
-
<link href="stylesheets/forms.css" media="screen" rel="Stylesheet" type="text/css" />
|
11
|
-
<link href="stylesheets/more.css" media="screen" rel="Stylesheet" type="text/css" />
|
12
|
-
<style type="text/css">
|
13
|
-
div#container {
|
14
|
-
max-width: 900px;
|
15
|
-
padding-bottom: 3em;
|
16
|
-
}
|
17
|
-
|
18
|
-
div#content {
|
19
|
-
margin-left: 200px;
|
20
|
-
}
|
21
|
-
|
22
|
-
div#container.notoc {
|
23
|
-
max-width: 600px;
|
24
|
-
}
|
25
|
-
|
26
|
-
.notoc div#content {
|
27
|
-
margin-left: 0;
|
28
|
-
}
|
29
|
-
|
30
|
-
pre {
|
31
|
-
line-height: 1.4em;
|
32
|
-
}
|
33
|
-
|
34
|
-
#content p tt {
|
35
|
-
background: #eeeeee;
|
36
|
-
border: solid 1px #cccccc;
|
37
|
-
padding: 3px;
|
38
|
-
}
|
39
|
-
|
40
|
-
dt {
|
41
|
-
font-weight: bold;
|
42
|
-
}
|
43
|
-
|
44
|
-
#content dt tt {
|
45
|
-
font-size: 10pt;
|
46
|
-
}
|
47
|
-
|
48
|
-
dd {
|
49
|
-
margin-left: 3em;
|
50
|
-
}
|
51
|
-
|
52
|
-
#content dt tt, #content pre tt {
|
53
|
-
background: none;
|
54
|
-
padding: 0;
|
55
|
-
border: 0;
|
56
|
-
}
|
57
|
-
|
58
|
-
#content .olist ol {
|
59
|
-
margin-left: 2em;
|
60
|
-
}
|
61
|
-
|
62
|
-
#header {
|
63
|
-
position: relative;
|
64
|
-
max-width: 840px;
|
65
|
-
margin-left: auto;
|
66
|
-
margin-right: auto;
|
67
|
-
}
|
68
|
-
|
69
|
-
#header.notoc {
|
70
|
-
max-width: 580px;
|
71
|
-
}
|
72
|
-
|
73
|
-
#logo {
|
74
|
-
position: absolute;
|
75
|
-
left: 10px;
|
76
|
-
top: 10px;
|
77
|
-
width: 110px;
|
78
|
-
height: 140px;
|
79
|
-
}
|
80
|
-
|
81
|
-
div#header h1#site_title {
|
82
|
-
background: url('images/ruby_on_rails_by_mike_rundle2.gif') top left no-repeat;
|
83
|
-
position: absolute;
|
84
|
-
width: 392px;
|
85
|
-
height: 55px;
|
86
|
-
left: 145px;
|
87
|
-
top: 20px;
|
88
|
-
margin: 0;
|
89
|
-
padding: 0;
|
90
|
-
}
|
91
|
-
|
92
|
-
#site_title span {
|
93
|
-
display: none;
|
94
|
-
}
|
95
|
-
|
96
|
-
#site_title_tagline {
|
97
|
-
display: none;
|
98
|
-
}
|
99
|
-
|
100
|
-
ul#navMain {
|
101
|
-
position: absolute;
|
102
|
-
margin: 0;
|
103
|
-
padding: 0;
|
104
|
-
top: 97px;
|
105
|
-
left: 145px;
|
106
|
-
}
|
107
|
-
|
108
|
-
.left-floaty, .right-floaty {
|
109
|
-
padding: 15px;
|
110
|
-
}
|
111
|
-
|
112
|
-
.admonitionblock,
|
113
|
-
.tableblock {
|
114
|
-
margin-left: 1em;
|
115
|
-
margin-right: 1em;
|
116
|
-
margin-top: 0.25em;
|
117
|
-
margin-bottom: 1em;
|
118
|
-
}
|
119
|
-
|
120
|
-
.admonitionblock .icon {
|
121
|
-
padding-right: 8px;
|
122
|
-
}
|
123
|
-
|
124
|
-
.admonitionblock .content {
|
125
|
-
border: solid 1px #ffda78;
|
126
|
-
background: #fffebd;
|
127
|
-
padding: 10px;
|
128
|
-
padding-top: 8px;
|
129
|
-
padding-bottom: 8px;
|
130
|
-
}
|
131
|
-
|
132
|
-
.admonitionblock .title {
|
133
|
-
font-size: 140%;
|
134
|
-
margin-bottom: 0.5em;
|
135
|
-
}
|
136
|
-
|
137
|
-
.tableblock table {
|
138
|
-
border: solid 1px #aaaaff;
|
139
|
-
background: #f0f0ff;
|
140
|
-
}
|
141
|
-
|
142
|
-
.tableblock th {
|
143
|
-
background: #e0e0e0;
|
144
|
-
}
|
145
|
-
|
146
|
-
.tableblock th,
|
147
|
-
.tableblock td {
|
148
|
-
padding: 3px;
|
149
|
-
padding-left: 5px;
|
150
|
-
padding-right: 5px;
|
151
|
-
}
|
152
|
-
|
153
|
-
.sidebarblock {
|
154
|
-
margin-top: 0.25em;
|
155
|
-
margin: 1em;
|
156
|
-
border: solid 1px #ccccbb;
|
157
|
-
padding: 8px;
|
158
|
-
background: #ffffe0;
|
159
|
-
}
|
160
|
-
|
161
|
-
.sidebarblock .sidebar-title {
|
162
|
-
font-size: 140%;
|
163
|
-
font-weight: 600;
|
164
|
-
margin-bottom: 0.3em;
|
165
|
-
}
|
166
|
-
|
167
|
-
.sidebarblock .sidebar-content > .para:last-child > p {
|
168
|
-
margin-bottom: 0;
|
169
|
-
}
|
170
|
-
|
171
|
-
.sidebarblock .sidebar-title a {
|
172
|
-
text-decoration: none;
|
173
|
-
}
|
174
|
-
|
175
|
-
.sidebarblock .sidebar-title a:hover {
|
176
|
-
text-decoration: underline;
|
177
|
-
}
|
178
|
-
|
179
|
-
</style>
|
180
|
-
</head>
|
181
|
-
<body>
|
182
|
-
<div id="header" >
|
183
|
-
<div id="logo">
|
184
|
-
<a href="index.html" title="Ruby on Rails"><img src="images/rails_logo_remix.gif" alt="Rails" height="140" width="110" /></a>
|
185
|
-
</div>
|
186
|
-
|
187
|
-
<h1 id="site_title"><span>Ruby on Rails</span></h1>
|
188
|
-
<h2 id="site_title_tagline">Sustainable productivity for web-application development</h2>
|
189
|
-
|
190
|
-
<ul id="navMain">
|
191
|
-
<li class="first-child"><a href="http://www.rubyonrails.org/" title="Ruby on Rails" class="ruby_on_rails">Ruby on Rails</a></li>
|
192
|
-
<li><a class="manuals" href="index.html" title="Manuals Index">Guides Index</a></li>
|
193
|
-
</ul>
|
194
|
-
</div>
|
195
|
-
|
196
|
-
<div id="container">
|
197
|
-
|
198
|
-
<div id="sidebar">
|
199
|
-
<h2>Chapters</h2>
|
200
|
-
<ol>
|
201
|
-
<li>
|
202
|
-
<a href="#_this_guide_assumes">This Guide Assumes</a>
|
203
|
-
</li>
|
204
|
-
<li>
|
205
|
-
<a href="#_what_is_rails">What is Rails?</a>
|
206
|
-
<ul>
|
207
|
-
|
208
|
-
<li><a href="#_the_mvc_architecture">The MVC Architecture</a></li>
|
209
|
-
|
210
|
-
<li><a href="#_the_components_of_rails">The Components of Rails</a></li>
|
211
|
-
|
212
|
-
<li><a href="#_rest">REST</a></li>
|
213
|
-
|
214
|
-
</ul>
|
215
|
-
</li>
|
216
|
-
<li>
|
217
|
-
<a href="#_creating_a_new_rails_project">Creating a New Rails Project</a>
|
218
|
-
<ul>
|
219
|
-
|
220
|
-
<li><a href="#_installing_rails">Installing Rails</a></li>
|
221
|
-
|
222
|
-
<li><a href="#_creating_the_blog_application">Creating the Blog Application</a></li>
|
223
|
-
|
224
|
-
<li><a href="#_configuring_a_database">Configuring a Database</a></li>
|
225
|
-
|
226
|
-
</ul>
|
227
|
-
</li>
|
228
|
-
<li>
|
229
|
-
<a href="#_hello_rails">Hello, Rails!</a>
|
230
|
-
<ul>
|
231
|
-
|
232
|
-
<li><a href="#_starting_up_the_web_server">Starting up the Web Server</a></li>
|
233
|
-
|
234
|
-
<li><a href="#_setting_the_application_home_page">Setting the Application Home Page</a></li>
|
235
|
-
|
236
|
-
</ul>
|
237
|
-
</li>
|
238
|
-
<li>
|
239
|
-
<a href="#_getting_up_and_running_quickly_with_scaffolding">Getting Up and Running Quickly With Scaffolding</a>
|
240
|
-
</li>
|
241
|
-
<li>
|
242
|
-
<a href="#_creating_a_resource">Creating a Resource</a>
|
243
|
-
<ul>
|
244
|
-
|
245
|
-
<li><a href="#_running_a_migration">Running a Migration</a></li>
|
246
|
-
|
247
|
-
<li><a href="#_adding_a_link">Adding a Link</a></li>
|
248
|
-
|
249
|
-
<li><a href="#_working_with_posts_in_the_browser">Working with Posts in the Browser</a></li>
|
250
|
-
|
251
|
-
<li><a href="#_the_model">The Model</a></li>
|
252
|
-
|
253
|
-
<li><a href="#_adding_some_validation">Adding Some Validation</a></li>
|
254
|
-
|
255
|
-
<li><a href="#_using_the_console">Using the Console</a></li>
|
256
|
-
|
257
|
-
<li><a href="#_listing_all_posts">Listing All Posts</a></li>
|
258
|
-
|
259
|
-
<li><a href="#_customizing_the_layout">Customizing the Layout</a></li>
|
260
|
-
|
261
|
-
<li><a href="#_creating_new_posts">Creating New Posts</a></li>
|
262
|
-
|
263
|
-
<li><a href="#_showing_an_individual_post">Showing an Individual Post</a></li>
|
264
|
-
|
265
|
-
<li><a href="#_editing_posts">Editing Posts</a></li>
|
266
|
-
|
267
|
-
<li><a href="#_destroying_a_post">Destroying a Post</a></li>
|
268
|
-
|
269
|
-
</ul>
|
270
|
-
</li>
|
271
|
-
<li>
|
272
|
-
<a href="#_drying_up_the_code">DRYing up the Code</a>
|
273
|
-
<ul>
|
274
|
-
|
275
|
-
<li><a href="#_using_partials_to_eliminate_view_duplication">Using Partials to Eliminate View Duplication</a></li>
|
276
|
-
|
277
|
-
<li><a href="#_using_filters_to_eliminate_controller_duplication">Using Filters to Eliminate Controller Duplication</a></li>
|
278
|
-
|
279
|
-
</ul>
|
280
|
-
</li>
|
281
|
-
<li>
|
282
|
-
<a href="#_adding_a_second_model">Adding a Second Model</a>
|
283
|
-
<ul>
|
284
|
-
|
285
|
-
<li><a href="#_generating_a_model">Generating a Model</a></li>
|
286
|
-
|
287
|
-
<li><a href="#_associating_models">Associating Models</a></li>
|
288
|
-
|
289
|
-
<li><a href="#_adding_a_route">Adding a Route</a></li>
|
290
|
-
|
291
|
-
<li><a href="#_generating_a_controller">Generating a Controller</a></li>
|
292
|
-
|
293
|
-
<li><a href="#_building_views">Building Views</a></li>
|
294
|
-
|
295
|
-
<li><a href="#_hooking_comments_to_posts">Hooking Comments to Posts</a></li>
|
296
|
-
|
297
|
-
</ul>
|
298
|
-
</li>
|
299
|
-
<li>
|
300
|
-
<a href="#_what_s_next">What's Next?</a>
|
301
|
-
</li>
|
302
|
-
<li>
|
303
|
-
<a href="#_changelog">Changelog</a>
|
304
|
-
</li>
|
305
|
-
</ol>
|
306
|
-
</div>
|
307
|
-
|
308
|
-
<div id="content">
|
309
|
-
<h1>Getting Started With Rails</h1>
|
310
|
-
<div id="preamble">
|
311
|
-
<div class="sectionbody">
|
312
|
-
<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>
|
313
|
-
<div class="ilist"><ul>
|
314
|
-
<li>
|
315
|
-
<p>
|
316
|
-
Installing Rails, creating a new Rails application, and connecting your application to a database
|
317
|
-
</p>
|
318
|
-
</li>
|
319
|
-
<li>
|
320
|
-
<p>
|
321
|
-
The general layout of a Rails application
|
322
|
-
</p>
|
323
|
-
</li>
|
324
|
-
<li>
|
325
|
-
<p>
|
326
|
-
The basic principles of MVC (Model, View Controller) and RESTful design
|
327
|
-
</p>
|
328
|
-
</li>
|
329
|
-
<li>
|
330
|
-
<p>
|
331
|
-
How to quickly generate the starting pieces of a Rails application.
|
332
|
-
</p>
|
333
|
-
</li>
|
334
|
-
</ul></div>
|
335
|
-
</div>
|
336
|
-
</div>
|
337
|
-
<h2 id="_this_guide_assumes">1. This Guide Assumes</h2>
|
338
|
-
<div class="sectionbody">
|
339
|
-
<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>
|
340
|
-
<div class="ilist"><ul>
|
341
|
-
<li>
|
342
|
-
<p>
|
343
|
-
The <a href="http://www.ruby-lang.org/en/downloads/">Ruby</a> language
|
344
|
-
</p>
|
345
|
-
</li>
|
346
|
-
<li>
|
347
|
-
<p>
|
348
|
-
The <a href="http://rubyforge.org/frs/?group_id=126">RubyGems</a> packaging system
|
349
|
-
</p>
|
350
|
-
</li>
|
351
|
-
<li>
|
352
|
-
<p>
|
353
|
-
A working installation of <a href="http://www.sqlite.org/">SQLite</a> (preferred), <a href="http://www.mysql.com/">MySQL</a>, or <a href="http://www.postgresql.org/">PostgreSQL</a>
|
354
|
-
</p>
|
355
|
-
</li>
|
356
|
-
</ul></div>
|
357
|
-
<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>
|
358
|
-
<div class="ilist"><ul>
|
359
|
-
<li>
|
360
|
-
<p>
|
361
|
-
<a href="http://www.humblelittlerubybook.com/">Mr. Neigborly’s Humble Little Ruby Book</a>
|
362
|
-
</p>
|
363
|
-
</li>
|
364
|
-
<li>
|
365
|
-
<p>
|
366
|
-
<a href="http://www.rubycentral.com/book/">Programming Ruby</a>
|
367
|
-
</p>
|
368
|
-
</li>
|
369
|
-
<li>
|
370
|
-
<p>
|
371
|
-
<a href="http://poignantguide.net/ruby/">Why's (Poignant) Guide to Ruby</a>
|
372
|
-
</p>
|
373
|
-
</li>
|
374
|
-
</ul></div>
|
375
|
-
</div>
|
376
|
-
<h2 id="_what_is_rails">2. What is Rails?</h2>
|
377
|
-
<div class="sectionbody">
|
378
|
-
<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>
|
379
|
-
<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>
|
380
|
-
<div class="para"><p>The Rails philosophy includes several guiding principles:</p></div>
|
381
|
-
<div class="ilist"><ul>
|
382
|
-
<li>
|
383
|
-
<p>
|
384
|
-
DRY - "Don't Repeat Yourself" - suggests that writing the same code over and over again is a bad thing.
|
385
|
-
</p>
|
386
|
-
</li>
|
387
|
-
<li>
|
388
|
-
<p>
|
389
|
-
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.
|
390
|
-
</p>
|
391
|
-
</li>
|
392
|
-
<li>
|
393
|
-
<p>
|
394
|
-
REST is the best pattern for web applications - organizing your application around resources and standard HTTP verbs is the fastest way to go.
|
395
|
-
</p>
|
396
|
-
</li>
|
397
|
-
</ul></div>
|
398
|
-
<h3 id="_the_mvc_architecture">2.1. The MVC Architecture</h3>
|
399
|
-
<div class="para"><p>Rails is organized around the Model, View, Controller architecture, usually just called MVC. MVC benefits include:</p></div>
|
400
|
-
<div class="ilist"><ul>
|
401
|
-
<li>
|
402
|
-
<p>
|
403
|
-
Isolation of business logic from the user interface
|
404
|
-
</p>
|
405
|
-
</li>
|
406
|
-
<li>
|
407
|
-
<p>
|
408
|
-
Ease of keeping code DRY
|
409
|
-
</p>
|
410
|
-
</li>
|
411
|
-
<li>
|
412
|
-
<p>
|
413
|
-
Making it clear where different types of code belong for easier maintenance
|
414
|
-
</p>
|
415
|
-
</li>
|
416
|
-
</ul></div>
|
417
|
-
<h4 id="_models">2.1.1. Models</h4>
|
418
|
-
<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>
|
419
|
-
<h4 id="_views">2.1.2. Views</h4>
|
420
|
-
<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>
|
421
|
-
<h4 id="_controllers">2.1.3. Controllers</h4>
|
422
|
-
<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>
|
423
|
-
<h3 id="_the_components_of_rails">2.2. The Components of Rails</h3>
|
424
|
-
<div class="para"><p>Rails provides a full stack of components for creating web applications, including:</p></div>
|
425
|
-
<div class="ilist"><ul>
|
426
|
-
<li>
|
427
|
-
<p>
|
428
|
-
Action Controller
|
429
|
-
</p>
|
430
|
-
</li>
|
431
|
-
<li>
|
432
|
-
<p>
|
433
|
-
Action View
|
434
|
-
</p>
|
435
|
-
</li>
|
436
|
-
<li>
|
437
|
-
<p>
|
438
|
-
Active Record
|
439
|
-
</p>
|
440
|
-
</li>
|
441
|
-
<li>
|
442
|
-
<p>
|
443
|
-
Action Mailer
|
444
|
-
</p>
|
445
|
-
</li>
|
446
|
-
<li>
|
447
|
-
<p>
|
448
|
-
Active Resource
|
449
|
-
</p>
|
450
|
-
</li>
|
451
|
-
<li>
|
452
|
-
<p>
|
453
|
-
Railties
|
454
|
-
</p>
|
455
|
-
</li>
|
456
|
-
<li>
|
457
|
-
<p>
|
458
|
-
Active Support
|
459
|
-
</p>
|
460
|
-
</li>
|
461
|
-
</ul></div>
|
462
|
-
<h4 id="_action_controller">2.2.1. Action Controller</h4>
|
463
|
-
<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>
|
464
|
-
<h4 id="_action_view">2.2.2. Action View</h4>
|
465
|
-
<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>
|
466
|
-
<h4 id="_active_record">2.2.3. Active Record</h4>
|
467
|
-
<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>
|
468
|
-
<h4 id="_action_mailer">2.2.4. Action Mailer</h4>
|
469
|
-
<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>
|
470
|
-
<h4 id="_active_resource">2.2.5. Active Resource</h4>
|
471
|
-
<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>
|
472
|
-
<h4 id="_railties">2.2.6. Railties</h4>
|
473
|
-
<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>
|
474
|
-
<h4 id="_active_support">2.2.7. Active Support</h4>
|
475
|
-
<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>
|
476
|
-
<h3 id="_rest">2.3. REST</h3>
|
477
|
-
<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>
|
478
|
-
<div class="ilist"><ul>
|
479
|
-
<li>
|
480
|
-
<p>
|
481
|
-
Using resource identifiers (which, for the purposes of discussion, you can think of as URLs) to represent resources
|
482
|
-
</p>
|
483
|
-
</li>
|
484
|
-
<li>
|
485
|
-
<p>
|
486
|
-
Transferring representations of the state of that resource between system components.
|
487
|
-
</p>
|
488
|
-
</li>
|
489
|
-
</ul></div>
|
490
|
-
<div class="para"><p>For example, to a Rails application a request such as this:</p></div>
|
491
|
-
<div class="para"><p><tt>DELETE /photos/17</tt></p></div>
|
492
|
-
<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>
|
493
|
-
<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>
|
494
|
-
<div class="ilist"><ul>
|
495
|
-
<li>
|
496
|
-
<p>
|
497
|
-
<a href="http://www.infoq.com/articles/rest-introduction">A Brief Introduction to REST</a> by Stefan Tilkov
|
498
|
-
</p>
|
499
|
-
</li>
|
500
|
-
<li>
|
501
|
-
<p>
|
502
|
-
<a href="http://bitworking.org/news/373/An-Introduction-to-REST">An Introduction to REST</a> (video tutorial) by Joe Gregorio
|
503
|
-
</p>
|
504
|
-
</li>
|
505
|
-
<li>
|
506
|
-
<p>
|
507
|
-
<a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">Representational State Transfer</a> article in Wikipedia
|
508
|
-
</p>
|
509
|
-
</li>
|
510
|
-
</ul></div>
|
511
|
-
</div>
|
512
|
-
<h2 id="_creating_a_new_rails_project">3. Creating a New Rails Project</h2>
|
513
|
-
<div class="sectionbody">
|
514
|
-
<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>
|
515
|
-
<h3 id="_installing_rails">3.1. Installing Rails</h3>
|
516
|
-
<div class="para"><p>In most cases, the easiest way to install Rails is to take advantage of RubyGems:</p></div>
|
517
|
-
<div class="listingblock">
|
518
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
519
|
-
by Lorenzo Bettini
|
520
|
-
http://www.lorenzobettini.it
|
521
|
-
http://www.gnu.org/software/src-highlite -->
|
522
|
-
<pre><tt>$ gem install rails
|
523
|
-
</tt></pre></div></div>
|
524
|
-
<div class="admonitionblock">
|
525
|
-
<table><tr>
|
526
|
-
<td class="icon">
|
527
|
-
<img src="./images/icons/note.png" alt="Note" />
|
528
|
-
</td>
|
529
|
-
<td class="content">There are some special circumstances in which you might want to use an alternate installation strategy:</td>
|
530
|
-
</tr></table>
|
531
|
-
</div>
|
532
|
-
<div class="ilist"><ul>
|
533
|
-
<li>
|
534
|
-
<p>
|
535
|
-
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.
|
536
|
-
</p>
|
537
|
-
</li>
|
538
|
-
<li>
|
539
|
-
<p>
|
540
|
-
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.
|
541
|
-
</p>
|
542
|
-
</li>
|
543
|
-
</ul></div>
|
544
|
-
<h3 id="_creating_the_blog_application">3.2. Creating the Blog Application</h3>
|
545
|
-
<div class="para"><p>Open a terminal, navigate to a folder where you have rights to create files, and type:</p></div>
|
546
|
-
<div class="listingblock">
|
547
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
548
|
-
by Lorenzo Bettini
|
549
|
-
http://www.lorenzobettini.it
|
550
|
-
http://www.gnu.org/software/src-highlite -->
|
551
|
-
<pre><tt>$ rails blog
|
552
|
-
</tt></pre></div></div>
|
553
|
-
<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>
|
554
|
-
<div class="listingblock">
|
555
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
556
|
-
by Lorenzo Bettini
|
557
|
-
http://www.lorenzobettini.it
|
558
|
-
http://www.gnu.org/software/src-highlite -->
|
559
|
-
<pre><tt>$ rails blog -d mysql
|
560
|
-
</tt></pre></div></div>
|
561
|
-
<div class="para"><p>And if you're using PostgreSQL for data storage, run this command:</p></div>
|
562
|
-
<div class="listingblock">
|
563
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
564
|
-
by Lorenzo Bettini
|
565
|
-
http://www.lorenzobettini.it
|
566
|
-
http://www.gnu.org/software/src-highlite -->
|
567
|
-
<pre><tt>$ rails blog -d postgresql
|
568
|
-
</tt></pre></div></div>
|
569
|
-
<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>
|
570
|
-
<div class="tableblock">
|
571
|
-
<table rules="all"
|
572
|
-
frame="hsides"
|
573
|
-
cellspacing="0" cellpadding="4">
|
574
|
-
<col width="137" />
|
575
|
-
<col width="1440" />
|
576
|
-
<thead>
|
577
|
-
<tr>
|
578
|
-
<th align="left">
|
579
|
-
File/Folder
|
580
|
-
</th>
|
581
|
-
<th align="left">
|
582
|
-
Purpose
|
583
|
-
</th>
|
584
|
-
</tr>
|
585
|
-
</thead>
|
586
|
-
<tbody valign="top">
|
587
|
-
<tr>
|
588
|
-
<td align="left">
|
589
|
-
<tt>README</tt>
|
590
|
-
</td>
|
591
|
-
<td align="left">
|
592
|
-
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.
|
593
|
-
</td>
|
594
|
-
</tr>
|
595
|
-
<tr>
|
596
|
-
<td align="left">
|
597
|
-
<tt>Rakefile</tt>
|
598
|
-
</td>
|
599
|
-
<td align="left">
|
600
|
-
This file contains batch jobs that can be run from the terminal.
|
601
|
-
</td>
|
602
|
-
</tr>
|
603
|
-
<tr>
|
604
|
-
<td align="left">
|
605
|
-
<tt>app/</tt>
|
606
|
-
</td>
|
607
|
-
<td align="left">
|
608
|
-
Contains the controllers, models, and views for your application. You'll focus on this folder for the remainder of this guide.
|
609
|
-
</td>
|
610
|
-
</tr>
|
611
|
-
<tr>
|
612
|
-
<td align="left">
|
613
|
-
<tt>config/</tt>
|
614
|
-
</td>
|
615
|
-
<td align="left">
|
616
|
-
Configure your application's runtime rules, routes, database, and more.
|
617
|
-
</td>
|
618
|
-
</tr>
|
619
|
-
<tr>
|
620
|
-
<td align="left">
|
621
|
-
<tt>db/</tt>
|
622
|
-
</td>
|
623
|
-
<td align="left">
|
624
|
-
Shows your current database schema, as well as the database migrations. You'll learn about migrations shortly.
|
625
|
-
</td>
|
626
|
-
</tr>
|
627
|
-
<tr>
|
628
|
-
<td align="left">
|
629
|
-
<tt>doc/</tt>
|
630
|
-
</td>
|
631
|
-
<td align="left">
|
632
|
-
In-depth documentation for your application.
|
633
|
-
</td>
|
634
|
-
</tr>
|
635
|
-
<tr>
|
636
|
-
<td align="left">
|
637
|
-
<tt>lib/</tt>
|
638
|
-
</td>
|
639
|
-
<td align="left">
|
640
|
-
Extended modules for your application (not covered in this guide).
|
641
|
-
</td>
|
642
|
-
</tr>
|
643
|
-
<tr>
|
644
|
-
<td align="left">
|
645
|
-
<tt>log/</tt>
|
646
|
-
</td>
|
647
|
-
<td align="left">
|
648
|
-
Application log files.
|
649
|
-
</td>
|
650
|
-
</tr>
|
651
|
-
<tr>
|
652
|
-
<td align="left">
|
653
|
-
<tt>public/</tt>
|
654
|
-
</td>
|
655
|
-
<td align="left">
|
656
|
-
The only folder seen to the world as-is. This is where your images, javascript, stylesheets (CSS), and other static files go.
|
657
|
-
</td>
|
658
|
-
</tr>
|
659
|
-
<tr>
|
660
|
-
<td align="left">
|
661
|
-
<tt>script/</tt>
|
662
|
-
</td>
|
663
|
-
<td align="left">
|
664
|
-
Scripts provided by Rails to do recurring tasks, such as benchmarking, plugin installation, and starting the console or the web server.
|
665
|
-
</td>
|
666
|
-
</tr>
|
667
|
-
<tr>
|
668
|
-
<td align="left">
|
669
|
-
<tt>test/</tt>
|
670
|
-
</td>
|
671
|
-
<td align="left">
|
672
|
-
Unit tests, fixtures, and other test apparatus. These are covered in <a href="../testing_rails_applications.html">Testing Rails Applications</a>
|
673
|
-
</td>
|
674
|
-
</tr>
|
675
|
-
<tr>
|
676
|
-
<td align="left">
|
677
|
-
<tt>tmp/</tt>
|
678
|
-
</td>
|
679
|
-
<td align="left">
|
680
|
-
Temporary files
|
681
|
-
</td>
|
682
|
-
</tr>
|
683
|
-
<tr>
|
684
|
-
<td align="left">
|
685
|
-
<tt>vendor/</tt>
|
686
|
-
</td>
|
687
|
-
<td align="left">
|
688
|
-
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.
|
689
|
-
</td>
|
690
|
-
</tr>
|
691
|
-
</tbody>
|
692
|
-
</table>
|
693
|
-
</div>
|
694
|
-
<h3 id="_configuring_a_database">3.3. Configuring a Database</h3>
|
695
|
-
<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>.
|
696
|
-
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>
|
697
|
-
<div class="ilist"><ul>
|
698
|
-
<li>
|
699
|
-
<p>
|
700
|
-
The <tt>development</tt> environment is used on your development computer as you interact manually with the application
|
701
|
-
</p>
|
702
|
-
</li>
|
703
|
-
<li>
|
704
|
-
<p>
|
705
|
-
The <tt>test</tt> environment is used to run automated tests
|
706
|
-
</p>
|
707
|
-
</li>
|
708
|
-
<li>
|
709
|
-
<p>
|
710
|
-
The <tt>production</tt> environment is used when you deploy your application for the world to use.
|
711
|
-
</p>
|
712
|
-
</li>
|
713
|
-
</ul></div>
|
714
|
-
<h4 id="_configuring_a_sqlite_database">3.3.1. Configuring a SQLite Database</h4>
|
715
|
-
<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>
|
716
|
-
<div class="para"><p>Here's the section of the default configuration file with connection information for the development environment:</p></div>
|
717
|
-
<div class="listingblock">
|
718
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
719
|
-
by Lorenzo Bettini
|
720
|
-
http://www.lorenzobettini.it
|
721
|
-
http://www.gnu.org/software/src-highlite -->
|
722
|
-
<pre><tt>development<span style="color: #990000">:</span>
|
723
|
-
adapter<span style="color: #990000">:</span> sqlite3
|
724
|
-
database<span style="color: #990000">:</span> db<span style="color: #990000">/</span>development<span style="color: #990000">.</span>sqlite3
|
725
|
-
timeout<span style="color: #990000">:</span> <span style="color: #993399">5000</span>
|
726
|
-
</tt></pre></div></div>
|
727
|
-
<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>
|
728
|
-
<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>
|
729
|
-
<div class="listingblock">
|
730
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
731
|
-
by Lorenzo Bettini
|
732
|
-
http://www.lorenzobettini.it
|
733
|
-
http://www.gnu.org/software/src-highlite -->
|
734
|
-
<pre><tt>$ gem install sqlite3-ruby
|
735
|
-
</tt></pre></div></div>
|
736
|
-
<h4 id="_configuring_a_mysql_database">3.3.2. Configuring a MySQL Database</h4>
|
737
|
-
<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>
|
738
|
-
<div class="listingblock">
|
739
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
740
|
-
by Lorenzo Bettini
|
741
|
-
http://www.lorenzobettini.it
|
742
|
-
http://www.gnu.org/software/src-highlite -->
|
743
|
-
<pre><tt>development<span style="color: #990000">:</span>
|
744
|
-
adapter<span style="color: #990000">:</span> mysql
|
745
|
-
encoding<span style="color: #990000">:</span> utf8
|
746
|
-
database<span style="color: #990000">:</span> blog_development
|
747
|
-
username<span style="color: #990000">:</span> root
|
748
|
-
password<span style="color: #990000">:</span>
|
749
|
-
socket<span style="color: #990000">:</span> <span style="color: #FF6600">/tmp/</span>mysql<span style="color: #990000">.</span>sock
|
750
|
-
</tt></pre></div></div>
|
751
|
-
<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>
|
752
|
-
<h4 id="_configuring_a_postgresql_database">3.3.3. Configuring a PostgreSQL Database</h4>
|
753
|
-
<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>
|
754
|
-
<div class="listingblock">
|
755
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
756
|
-
by Lorenzo Bettini
|
757
|
-
http://www.lorenzobettini.it
|
758
|
-
http://www.gnu.org/software/src-highlite -->
|
759
|
-
<pre><tt>development<span style="color: #990000">:</span>
|
760
|
-
adapter<span style="color: #990000">:</span> postgresql
|
761
|
-
encoding<span style="color: #990000">:</span> unicode
|
762
|
-
database<span style="color: #990000">:</span> blog_development
|
763
|
-
username<span style="color: #990000">:</span> blog
|
764
|
-
password<span style="color: #990000">:</span>
|
765
|
-
</tt></pre></div></div>
|
766
|
-
<div class="para"><p>Change the username and password in the <tt>development</tt> section as appropriate.</p></div>
|
767
|
-
</div>
|
768
|
-
<h2 id="_hello_rails">4. Hello, Rails!</h2>
|
769
|
-
<div class="sectionbody">
|
770
|
-
<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>
|
771
|
-
<div class="listingblock">
|
772
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
773
|
-
by Lorenzo Bettini
|
774
|
-
http://www.lorenzobettini.it
|
775
|
-
http://www.gnu.org/software/src-highlite -->
|
776
|
-
<pre><tt>$ script/generate controller home index
|
777
|
-
</tt></pre></div></div>
|
778
|
-
<div class="admonitionblock">
|
779
|
-
<table><tr>
|
780
|
-
<td class="icon">
|
781
|
-
<img src="./images/icons/tip.png" alt="Tip" />
|
782
|
-
</td>
|
783
|
-
<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>
|
784
|
-
</tr></table>
|
785
|
-
</div>
|
786
|
-
<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>
|
787
|
-
<div class="listingblock">
|
788
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
789
|
-
by Lorenzo Bettini
|
790
|
-
http://www.lorenzobettini.it
|
791
|
-
http://www.gnu.org/software/src-highlite -->
|
792
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF"><h1></span></span>Hello, Rails!<span style="font-weight: bold"><span style="color: #0000FF"></h1></span></span>
|
793
|
-
</tt></pre></div></div>
|
794
|
-
<h3 id="_starting_up_the_web_server">4.1. Starting up the Web Server</h3>
|
795
|
-
<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>
|
796
|
-
<div class="listingblock">
|
797
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
798
|
-
by Lorenzo Bettini
|
799
|
-
http://www.lorenzobettini.it
|
800
|
-
http://www.gnu.org/software/src-highlite -->
|
801
|
-
<pre><tt>$ script/server
|
802
|
-
</tt></pre></div></div>
|
803
|
-
<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>
|
804
|
-
<div class="para"><p><span class="image">
|
805
|
-
<img src="images/rails_welcome.png" alt="Welcome Aboard screenshot" title="Welcome Aboard screenshot" />
|
806
|
-
</span></p></div>
|
807
|
-
<div class="admonitionblock">
|
808
|
-
<table><tr>
|
809
|
-
<td class="icon">
|
810
|
-
<img src="./images/icons/tip.png" alt="Tip" />
|
811
|
-
</td>
|
812
|
-
<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>
|
813
|
-
</tr></table>
|
814
|
-
</div>
|
815
|
-
<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>
|
816
|
-
<h3 id="_setting_the_application_home_page">4.2. Setting the Application Home Page</h3>
|
817
|
-
<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>
|
818
|
-
<div class="listingblock">
|
819
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
820
|
-
by Lorenzo Bettini
|
821
|
-
http://www.lorenzobettini.it
|
822
|
-
http://www.gnu.org/software/src-highlite -->
|
823
|
-
<pre><tt>$ rm public/index<span style="color: #990000">.</span>html
|
824
|
-
</tt></pre></div></div>
|
825
|
-
<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>
|
826
|
-
<div class="listingblock">
|
827
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
828
|
-
by Lorenzo Bettini
|
829
|
-
http://www.lorenzobettini.it
|
830
|
-
http://www.gnu.org/software/src-highlite -->
|
831
|
-
<pre><tt>map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id'</span>
|
832
|
-
map<span style="color: #990000">.</span>connect <span style="color: #FF0000">':controller/:action/:id.:format'</span>
|
833
|
-
</tt></pre></div></div>
|
834
|
-
<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>
|
835
|
-
<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>
|
836
|
-
<div class="listingblock">
|
837
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
838
|
-
by Lorenzo Bettini
|
839
|
-
http://www.lorenzobettini.it
|
840
|
-
http://www.gnu.org/software/src-highlite -->
|
841
|
-
<pre><tt>map<span style="color: #990000">.</span>root <span style="color: #990000">:</span>controller <span style="color: #990000">=></span> <span style="color: #FF0000">"home"</span>
|
842
|
-
</tt></pre></div></div>
|
843
|
-
<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>
|
844
|
-
<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>
|
845
|
-
<div class="admonitionblock">
|
846
|
-
<table><tr>
|
847
|
-
<td class="icon">
|
848
|
-
<img src="./images/icons/note.png" alt="Note" />
|
849
|
-
</td>
|
850
|
-
<td class="content">For more information about routing, refer to <a href="../routing_outside_in.html">Rails Routing from the Outside In</a>.</td>
|
851
|
-
</tr></table>
|
852
|
-
</div>
|
853
|
-
</div>
|
854
|
-
<h2 id="_getting_up_and_running_quickly_with_scaffolding">5. Getting Up and Running Quickly With Scaffolding</h2>
|
855
|
-
<div class="sectionbody">
|
856
|
-
<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>
|
857
|
-
</div>
|
858
|
-
<h2 id="_creating_a_resource">6. Creating a Resource</h2>
|
859
|
-
<div class="sectionbody">
|
860
|
-
<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>
|
861
|
-
<div class="listingblock">
|
862
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
863
|
-
by Lorenzo Bettini
|
864
|
-
http://www.lorenzobettini.it
|
865
|
-
http://www.gnu.org/software/src-highlite -->
|
866
|
-
<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
|
867
|
-
</tt></pre></div></div>
|
868
|
-
<div class="admonitionblock">
|
869
|
-
<table><tr>
|
870
|
-
<td class="icon">
|
871
|
-
<img src="./images/icons/note.png" alt="Note" />
|
872
|
-
</td>
|
873
|
-
<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>
|
874
|
-
</tr></table>
|
875
|
-
</div>
|
876
|
-
<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>
|
877
|
-
<div class="tableblock">
|
878
|
-
<table rules="all"
|
879
|
-
frame="hsides"
|
880
|
-
cellspacing="0" cellpadding="4">
|
881
|
-
<col width="525" />
|
882
|
-
<col width="1062" />
|
883
|
-
<thead>
|
884
|
-
<tr>
|
885
|
-
<th align="left">
|
886
|
-
File
|
887
|
-
</th>
|
888
|
-
<th align="left">
|
889
|
-
Purpose
|
890
|
-
</th>
|
891
|
-
</tr>
|
892
|
-
</thead>
|
893
|
-
<tbody valign="top">
|
894
|
-
<tr>
|
895
|
-
<td align="left">
|
896
|
-
app/models/post.rb
|
897
|
-
</td>
|
898
|
-
<td align="left">
|
899
|
-
The Post model
|
900
|
-
</td>
|
901
|
-
</tr>
|
902
|
-
<tr>
|
903
|
-
<td align="left">
|
904
|
-
db/migrate/20081013124235_create_posts.rb
|
905
|
-
</td>
|
906
|
-
<td align="left">
|
907
|
-
Migration to create the posts table in your database (your name will include a different timestamp)
|
908
|
-
</td>
|
909
|
-
</tr>
|
910
|
-
<tr>
|
911
|
-
<td align="left">
|
912
|
-
app/views/posts/index.html.erb
|
913
|
-
</td>
|
914
|
-
<td align="left">
|
915
|
-
A view to display an index of all posts
|
916
|
-
</td>
|
917
|
-
</tr>
|
918
|
-
<tr>
|
919
|
-
<td align="left">
|
920
|
-
app/views/posts/show.html.erb
|
921
|
-
</td>
|
922
|
-
<td align="left">
|
923
|
-
A view to display a single post
|
924
|
-
</td>
|
925
|
-
</tr>
|
926
|
-
<tr>
|
927
|
-
<td align="left">
|
928
|
-
app/views/posts/new.html.erb
|
929
|
-
</td>
|
930
|
-
<td align="left">
|
931
|
-
A view to create a new post
|
932
|
-
</td>
|
933
|
-
</tr>
|
934
|
-
<tr>
|
935
|
-
<td align="left">
|
936
|
-
app/views/posts/edit.html.erb
|
937
|
-
</td>
|
938
|
-
<td align="left">
|
939
|
-
A view to edit an existing post
|
940
|
-
</td>
|
941
|
-
</tr>
|
942
|
-
<tr>
|
943
|
-
<td align="left">
|
944
|
-
app/views/layouts/posts.html.erb
|
945
|
-
</td>
|
946
|
-
<td align="left">
|
947
|
-
A view to control the overall look and feel of the other posts views
|
948
|
-
</td>
|
949
|
-
</tr>
|
950
|
-
<tr>
|
951
|
-
<td align="left">
|
952
|
-
public/stylesheets/scaffold.css
|
953
|
-
</td>
|
954
|
-
<td align="left">
|
955
|
-
Cascading style sheet to make the scaffolded views look better
|
956
|
-
</td>
|
957
|
-
</tr>
|
958
|
-
<tr>
|
959
|
-
<td align="left">
|
960
|
-
app/controllers/posts_controller.rb
|
961
|
-
</td>
|
962
|
-
<td align="left">
|
963
|
-
The Posts controller
|
964
|
-
</td>
|
965
|
-
</tr>
|
966
|
-
<tr>
|
967
|
-
<td align="left">
|
968
|
-
test/functional/posts_controller_test.rb
|
969
|
-
</td>
|
970
|
-
<td align="left">
|
971
|
-
Functional testing harness for the posts controller
|
972
|
-
</td>
|
973
|
-
</tr>
|
974
|
-
<tr>
|
975
|
-
<td align="left">
|
976
|
-
app/helpers/posts_helper.rb
|
977
|
-
</td>
|
978
|
-
<td align="left">
|
979
|
-
Helper functions to be used from the posts views
|
980
|
-
</td>
|
981
|
-
</tr>
|
982
|
-
<tr>
|
983
|
-
<td align="left">
|
984
|
-
config/routes.rb
|
985
|
-
</td>
|
986
|
-
<td align="left">
|
987
|
-
Edited to include routing information for posts
|
988
|
-
</td>
|
989
|
-
</tr>
|
990
|
-
<tr>
|
991
|
-
<td align="left">
|
992
|
-
test/fixtures/posts.yml
|
993
|
-
</td>
|
994
|
-
<td align="left">
|
995
|
-
Dummy posts for use in testing
|
996
|
-
</td>
|
997
|
-
</tr>
|
998
|
-
<tr>
|
999
|
-
<td align="left">
|
1000
|
-
test/unit/post_test.rb
|
1001
|
-
</td>
|
1002
|
-
<td align="left">
|
1003
|
-
Unit testing harness for the posts model
|
1004
|
-
</td>
|
1005
|
-
</tr>
|
1006
|
-
</tbody>
|
1007
|
-
</table>
|
1008
|
-
</div>
|
1009
|
-
<h3 id="_running_a_migration">6.1. Running a Migration</h3>
|
1010
|
-
<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>
|
1011
|
-
<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>
|
1012
|
-
<div class="listingblock">
|
1013
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1014
|
-
by Lorenzo Bettini
|
1015
|
-
http://www.lorenzobettini.it
|
1016
|
-
http://www.gnu.org/software/src-highlite -->
|
1017
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreatePosts <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Migration
|
1018
|
-
<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>up
|
1019
|
-
create_table <span style="color: #990000">:</span>posts <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
|
1020
|
-
t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>name
|
1021
|
-
t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>title
|
1022
|
-
t<span style="color: #990000">.</span>text <span style="color: #990000">:</span>content
|
1023
|
-
|
1024
|
-
t<span style="color: #990000">.</span>timestamps
|
1025
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1026
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1027
|
-
|
1028
|
-
<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
|
1029
|
-
drop_table <span style="color: #990000">:</span>posts
|
1030
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1031
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1032
|
-
</tt></pre></div></div>
|
1033
|
-
<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>
|
1034
|
-
<div class="para"><p>At this point, you need to do two things: create the database and run the migration. You can use rake commands at the terminal for both of those tasks:</p></div>
|
1035
|
-
<div class="listingblock">
|
1036
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1037
|
-
by Lorenzo Bettini
|
1038
|
-
http://www.lorenzobettini.it
|
1039
|
-
http://www.gnu.org/software/src-highlite -->
|
1040
|
-
<pre><tt>$ rake db<span style="color: #990000">:</span>create
|
1041
|
-
$ rake db<span style="color: #990000">:</span>migrate
|
1042
|
-
</tt></pre></div></div>
|
1043
|
-
<div class="admonitionblock">
|
1044
|
-
<table><tr>
|
1045
|
-
<td class="icon">
|
1046
|
-
<img src="./images/icons/note.png" alt="Note" />
|
1047
|
-
</td>
|
1048
|
-
<td class="content">Because you're working in the development environment by default, both of these commands will apply to the database defined in the <tt>development</tt> section of your <tt>config/database.yml</tt> file.</td>
|
1049
|
-
</tr></table>
|
1050
|
-
</div>
|
1051
|
-
<h3 id="_adding_a_link">6.2. Adding a Link</h3>
|
1052
|
-
<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>
|
1053
|
-
<div class="listingblock">
|
1054
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1055
|
-
by Lorenzo Bettini
|
1056
|
-
http://www.lorenzobettini.it
|
1057
|
-
http://www.gnu.org/software/src-highlite -->
|
1058
|
-
<pre><tt><span style="color: #FF0000"><h1></span>Hello<span style="color: #990000">,</span> Rails!<span style="color: #FF0000"></h1></span>
|
1059
|
-
|
1060
|
-
<span style="color: #FF0000"><%= link_to "My Blog", posts_path %></span>
|
1061
|
-
</tt></pre></div></div>
|
1062
|
-
<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>
|
1063
|
-
<h3 id="_working_with_posts_in_the_browser">6.3. Working with Posts in the Browser</h3>
|
1064
|
-
<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>
|
1065
|
-
<div class="para"><p><span class="image">
|
1066
|
-
<img src="images/posts_index.png" alt="Posts Index screenshot" title="Posts Index screenshot" />
|
1067
|
-
</span></p></div>
|
1068
|
-
<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>
|
1069
|
-
<div class="admonitionblock">
|
1070
|
-
<table><tr>
|
1071
|
-
<td class="icon">
|
1072
|
-
<img src="./images/icons/tip.png" alt="Tip" />
|
1073
|
-
</td>
|
1074
|
-
<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>
|
1075
|
-
</tr></table>
|
1076
|
-
</div>
|
1077
|
-
<div class="para"><p>Congratulations, you're riding the rails! Now it's time to see how it all works.</p></div>
|
1078
|
-
<h3 id="_the_model">6.4. The Model</h3>
|
1079
|
-
<div class="para"><p>The model file, <tt>app/models/post.rb</tt> is about as simple as it can get:</p></div>
|
1080
|
-
<div class="listingblock">
|
1081
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1082
|
-
by Lorenzo Bettini
|
1083
|
-
http://www.lorenzobettini.it
|
1084
|
-
http://www.gnu.org/software/src-highlite -->
|
1085
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
1086
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1087
|
-
</tt></pre></div></div>
|
1088
|
-
<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>
|
1089
|
-
<h3 id="_adding_some_validation">6.5. Adding Some Validation</h3>
|
1090
|
-
<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>
|
1091
|
-
<div class="listingblock">
|
1092
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1093
|
-
by Lorenzo Bettini
|
1094
|
-
http://www.lorenzobettini.it
|
1095
|
-
http://www.gnu.org/software/src-highlite -->
|
1096
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
1097
|
-
validates_presence_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>title
|
1098
|
-
validates_length_of <span style="color: #990000">:</span>title<span style="color: #990000">,</span> <span style="color: #990000">:</span>minimum <span style="color: #990000">=></span> <span style="color: #993399">5</span>
|
1099
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1100
|
-
</tt></pre></div></div>
|
1101
|
-
<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>
|
1102
|
-
<h3 id="_using_the_console">6.6. Using the Console</h3>
|
1103
|
-
<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>
|
1104
|
-
<div class="listingblock">
|
1105
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1106
|
-
by Lorenzo Bettini
|
1107
|
-
http://www.lorenzobettini.it
|
1108
|
-
http://www.gnu.org/software/src-highlite -->
|
1109
|
-
<pre><tt>$ script/console
|
1110
|
-
</tt></pre></div></div>
|
1111
|
-
<div class="para"><p>After the console loads, you can use it to work with your application's models:</p></div>
|
1112
|
-
<div class="listingblock">
|
1113
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1114
|
-
by Lorenzo Bettini
|
1115
|
-
http://www.lorenzobettini.it
|
1116
|
-
http://www.gnu.org/software/src-highlite -->
|
1117
|
-
<pre><tt><span style="color: #990000">>></span> p <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>create<span style="color: #990000">(:</span>content <span style="color: #990000">=></span> <span style="color: #FF0000">"A new post"</span><span style="color: #990000">)</span>
|
1118
|
-
<span style="color: #990000">=></span> <span style="font-style: italic"><span style="color: #9A1900">#<Post id: nil, name: nil, title: nil, content: "A new post",</span></span>
|
1119
|
-
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">></span>
|
1120
|
-
<span style="color: #990000">>></span> p<span style="color: #990000">.</span>save
|
1121
|
-
<span style="color: #990000">=></span> <span style="font-weight: bold"><span style="color: #0000FF">false</span></span>
|
1122
|
-
<span style="color: #990000">>></span> p<span style="color: #990000">.</span>errors
|
1123
|
-
<span style="color: #990000">=></span> <span style="font-style: italic"><span style="color: #9A1900">#<ActiveRecord::Errors:0x23bcf0c @base=#<Post id: nil, name: nil,</span></span>
|
1124
|
-
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">>,</span>
|
1125
|
-
@<span style="color: #009900">errors</span><span style="color: #990000">=</span>{<span style="color: #FF0000">"name"</span><span style="color: #990000">=>[</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">=>[</span><span style="color: #FF0000">"can't be blank"</span><span style="color: #990000">,</span>
|
1126
|
-
<span style="color: #FF0000">"is too short (minimum is 5 characters)"</span><span style="color: #990000">]</span>}<span style="color: #990000">></span>
|
1127
|
-
</tt></pre></div></div>
|
1128
|
-
<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>
|
1129
|
-
<div class="admonitionblock">
|
1130
|
-
<table><tr>
|
1131
|
-
<td class="icon">
|
1132
|
-
<img src="./images/icons/tip.png" alt="Tip" />
|
1133
|
-
</td>
|
1134
|
-
<td class="content">Unlike the development web server, the console does not automatically load your code afresh for each line. If you make changes, type <tt>reload!</tt> at the console prompt to load them.</td>
|
1135
|
-
</tr></table>
|
1136
|
-
</div>
|
1137
|
-
<h3 id="_listing_all_posts">6.7. Listing All Posts</h3>
|
1138
|
-
<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>
|
1139
|
-
<div class="listingblock">
|
1140
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1141
|
-
by Lorenzo Bettini
|
1142
|
-
http://www.lorenzobettini.it
|
1143
|
-
http://www.gnu.org/software/src-highlite -->
|
1144
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
|
1145
|
-
<span style="color: #009900">@posts</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>find<span style="color: #990000">(:</span>all<span style="color: #990000">)</span>
|
1146
|
-
|
1147
|
-
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>
|
1148
|
-
format<span style="color: #990000">.</span>html <span style="font-style: italic"><span style="color: #9A1900"># index.html.erb</span></span>
|
1149
|
-
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=></span> <span style="color: #009900">@posts</span> <span style="color: #FF0000">}</span>
|
1150
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1151
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1152
|
-
</tt></pre></div></div>
|
1153
|
-
<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>
|
1154
|
-
<div class="admonitionblock">
|
1155
|
-
<table><tr>
|
1156
|
-
<td class="icon">
|
1157
|
-
<img src="./images/icons/tip.png" alt="Tip" />
|
1158
|
-
</td>
|
1159
|
-
<td class="content">For more information on finding records with Active Record, see <a href="../finders.html">Active Record Finders</a>.</td>
|
1160
|
-
</tr></table>
|
1161
|
-
</div>
|
1162
|
-
<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>
|
1163
|
-
<div class="listingblock">
|
1164
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1165
|
-
by Lorenzo Bettini
|
1166
|
-
http://www.lorenzobettini.it
|
1167
|
-
http://www.gnu.org/software/src-highlite -->
|
1168
|
-
<pre><tt><span style="color: #FF0000"><h1></span>Listing posts<span style="color: #FF0000"></h1></span>
|
1169
|
-
|
1170
|
-
<span style="color: #FF0000"><table></span>
|
1171
|
-
<span style="color: #FF0000"><tr></span>
|
1172
|
-
<span style="color: #FF0000"><th></span>Name<span style="color: #FF0000"></th></span>
|
1173
|
-
<span style="color: #FF0000"><th></span>Title<span style="color: #FF0000"></th></span>
|
1174
|
-
<span style="color: #FF0000"><th></span>Content<span style="color: #FF0000"></th></span>
|
1175
|
-
<span style="color: #FF0000"></tr></span>
|
1176
|
-
|
1177
|
-
<span style="color: #FF0000"><% for post in @posts %></span>
|
1178
|
-
<span style="color: #FF0000"><tr></span>
|
1179
|
-
<span style="color: #FF0000"><td><%=h post.name %></td></span>
|
1180
|
-
<span style="color: #FF0000"><td><%=h post.title %></td></span>
|
1181
|
-
<span style="color: #FF0000"><td><%=h post.content %></td></span>
|
1182
|
-
<span style="color: #FF0000"><td><%= link_to 'Show', post %></td></span>
|
1183
|
-
<span style="color: #FF0000"><td><%= link_to 'Edit', edit_post_path(post) %></td></span>
|
1184
|
-
<span style="color: #FF0000"><td><%= link_to 'Destroy', post, :confirm =></span> <span style="color: #FF0000">'Are you sure?'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=></span> <span style="color: #990000">:</span>delete <span style="color: #990000">%></span><span style="color: #FF0000"></td></span>
|
1185
|
-
<span style="color: #FF0000"></tr></span>
|
1186
|
-
<span style="color: #FF0000"><% end %></span>
|
1187
|
-
<span style="color: #FF0000"></table></span>
|
1188
|
-
|
1189
|
-
<span style="color: #FF0000"><br /></span>
|
1190
|
-
|
1191
|
-
<span style="color: #FF0000"><%= link_to 'New post', new_post_path %></span>
|
1192
|
-
</tt></pre></div></div>
|
1193
|
-
<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>
|
1194
|
-
<div class="ilist"><ul>
|
1195
|
-
<li>
|
1196
|
-
<p>
|
1197
|
-
<tt>h</tt> is a Rails helper method to sanitize displayed data, preventing cross-site scripting attacks
|
1198
|
-
</p>
|
1199
|
-
</li>
|
1200
|
-
<li>
|
1201
|
-
<p>
|
1202
|
-
<tt>link_to</tt> builds a hyperlink to a particular destination
|
1203
|
-
</p>
|
1204
|
-
</li>
|
1205
|
-
<li>
|
1206
|
-
<p>
|
1207
|
-
<tt>edit_post_path</tt> is a helper that Rails provides as part of RESTful routing. You’ll see a variety of these helpers for the different actions that the controller includes.
|
1208
|
-
</p>
|
1209
|
-
</li>
|
1210
|
-
</ul></div>
|
1211
|
-
<div class="admonitionblock">
|
1212
|
-
<table><tr>
|
1213
|
-
<td class="icon">
|
1214
|
-
<img src="./images/icons/tip.png" alt="Tip" />
|
1215
|
-
</td>
|
1216
|
-
<td class="content">For more details on the rendering process, see <a href="../layouts_and_rendering.html">Layouts and Rendering in Rails</a>.</td>
|
1217
|
-
</tr></table>
|
1218
|
-
</div>
|
1219
|
-
<h3 id="_customizing_the_layout">6.8. Customizing the Layout</h3>
|
1220
|
-
<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>
|
1221
|
-
<div class="listingblock">
|
1222
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1223
|
-
by Lorenzo Bettini
|
1224
|
-
http://www.lorenzobettini.it
|
1225
|
-
http://www.gnu.org/software/src-highlite -->
|
1226
|
-
<pre><tt><span style="color: #990000"><!</span>DOCTYPE html PUBLIC <span style="color: #FF0000">"-//W3C//DTD XHTML 1.0 Transitional//EN"</span>
|
1227
|
-
<span style="color: #FF0000">"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span style="color: #990000">></span>
|
1228
|
-
|
1229
|
-
<span style="color: #FF0000"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"></span>
|
1230
|
-
<span style="color: #FF0000"><head></span>
|
1231
|
-
<span style="color: #FF0000"><meta http-equiv="content-type" content="text/html;charset=UTF-8" /></span>
|
1232
|
-
<span style="color: #FF0000"><title></span>Posts<span style="color: #990000">:</span> <span style="color: #FF0000"><%= controller.action_name %></title></span>
|
1233
|
-
<span style="color: #FF0000"><%= stylesheet_link_tag 'scaffold' %></span>
|
1234
|
-
<span style="color: #FF0000"></head></span>
|
1235
|
-
<span style="color: #FF0000"><body style="background: #EEEEEE;"></span>
|
1236
|
-
|
1237
|
-
<span style="color: #FF0000"><p style="color: green"><%= flash[:notice] %></p></span>
|
1238
|
-
|
1239
|
-
<span style="color: #FF0000"><%= yield %></span>
|
1240
|
-
|
1241
|
-
<span style="color: #FF0000"></body></span>
|
1242
|
-
<span style="color: #FF0000"></html></span>
|
1243
|
-
</tt></pre></div></div>
|
1244
|
-
<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>
|
1245
|
-
<h3 id="_creating_new_posts">6.9. Creating New Posts</h3>
|
1246
|
-
<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>
|
1247
|
-
<div class="listingblock">
|
1248
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1249
|
-
by Lorenzo Bettini
|
1250
|
-
http://www.lorenzobettini.it
|
1251
|
-
http://www.gnu.org/software/src-highlite -->
|
1252
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new
|
1253
|
-
<span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new
|
1254
|
-
|
1255
|
-
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>
|
1256
|
-
format<span style="color: #990000">.</span>html <span style="font-style: italic"><span style="color: #9A1900"># new.html.erb</span></span>
|
1257
|
-
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=></span> <span style="color: #009900">@post</span> <span style="color: #FF0000">}</span>
|
1258
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1259
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1260
|
-
</tt></pre></div></div>
|
1261
|
-
<div class="para"><p>The <tt>new.html.erb</tt> view displays this empty Post to the user:</p></div>
|
1262
|
-
<div class="listingblock">
|
1263
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1264
|
-
by Lorenzo Bettini
|
1265
|
-
http://www.lorenzobettini.it
|
1266
|
-
http://www.gnu.org/software/src-highlite -->
|
1267
|
-
<pre><tt><span style="color: #FF0000"><h1></span>New post<span style="color: #FF0000"></h1></span>
|
1268
|
-
|
1269
|
-
<span style="color: #FF0000"><% form_for(@post) do |f| %></span>
|
1270
|
-
<span style="color: #FF0000"><%= f.error_messages %></span>
|
1271
|
-
|
1272
|
-
<span style="color: #FF0000"><p></span>
|
1273
|
-
<span style="color: #FF0000"><%= f.label :name %><br /></span>
|
1274
|
-
<span style="color: #FF0000"><%= f.text_field :name %></span>
|
1275
|
-
<span style="color: #FF0000"></p></span>
|
1276
|
-
<span style="color: #FF0000"><p></span>
|
1277
|
-
<span style="color: #FF0000"><%= f.label :title %><br /></span>
|
1278
|
-
<span style="color: #FF0000"><%= f.text_field :title %></span>
|
1279
|
-
<span style="color: #FF0000"></p></span>
|
1280
|
-
<span style="color: #FF0000"><p></span>
|
1281
|
-
<span style="color: #FF0000"><%= f.label :content %><br /></span>
|
1282
|
-
<span style="color: #FF0000"><%= f.text_area :content %></span>
|
1283
|
-
<span style="color: #FF0000"></p></span>
|
1284
|
-
<span style="color: #FF0000"><p></span>
|
1285
|
-
<span style="color: #FF0000"><%= f.submit "Create" %></span>
|
1286
|
-
<span style="color: #FF0000"></p></span>
|
1287
|
-
<span style="color: #FF0000"><% end %></span>
|
1288
|
-
|
1289
|
-
<span style="color: #FF0000"><%= link_to 'Back', posts_path %></span>
|
1290
|
-
</tt></pre></div></div>
|
1291
|
-
<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>
|
1292
|
-
<div class="admonitionblock">
|
1293
|
-
<table><tr>
|
1294
|
-
<td class="icon">
|
1295
|
-
<img src="./images/icons/tip.png" alt="Tip" />
|
1296
|
-
</td>
|
1297
|
-
<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>
|
1298
|
-
</tr></table>
|
1299
|
-
</div>
|
1300
|
-
<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>
|
1301
|
-
<div class="listingblock">
|
1302
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1303
|
-
by Lorenzo Bettini
|
1304
|
-
http://www.lorenzobettini.it
|
1305
|
-
http://www.gnu.org/software/src-highlite -->
|
1306
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
|
1307
|
-
<span style="color: #009900">@post</span> <span style="color: #990000">=</span> Post<span style="color: #990000">.</span>new<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post<span style="color: #990000">])</span>
|
1308
|
-
|
1309
|
-
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>
|
1310
|
-
<span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>save
|
1311
|
-
flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">'Post was successfully created.'</span>
|
1312
|
-
format<span style="color: #990000">.</span>html <span style="color: #FF0000">{</span> redirect_to<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
|
1313
|
-
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=></span> <span style="color: #009900">@post</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>status <span style="color: #990000">=></span> <span style="color: #990000">:</span>created<span style="color: #990000">,</span> <span style="color: #990000">:</span>location <span style="color: #990000">=></span> <span style="color: #009900">@post</span> <span style="color: #FF0000">}</span>
|
1314
|
-
<span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
|
1315
|
-
format<span style="color: #990000">.</span>html <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>action <span style="color: #990000">=></span> <span style="color: #FF0000">"new"</span> <span style="color: #FF0000">}</span>
|
1316
|
-
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=></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">=></span> <span style="color: #990000">:</span>unprocessable_entity <span style="color: #FF0000">}</span>
|
1317
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1318
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1319
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1320
|
-
</tt></pre></div></div>
|
1321
|
-
<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>
|
1322
|
-
<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>
|
1323
|
-
<h3 id="_showing_an_individual_post">6.10. Showing an Individual Post</h3>
|
1324
|
-
<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>
|
1325
|
-
<div class="listingblock">
|
1326
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1327
|
-
by Lorenzo Bettini
|
1328
|
-
http://www.lorenzobettini.it
|
1329
|
-
http://www.gnu.org/software/src-highlite -->
|
1330
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
|
1331
|
-
<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>
|
1332
|
-
|
1333
|
-
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>
|
1334
|
-
format<span style="color: #990000">.</span>html <span style="font-style: italic"><span style="color: #9A1900"># show.html.erb</span></span>
|
1335
|
-
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=></span> <span style="color: #009900">@post</span> <span style="color: #FF0000">}</span>
|
1336
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1337
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1338
|
-
</tt></pre></div></div>
|
1339
|
-
<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>
|
1340
|
-
<div class="listingblock">
|
1341
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1342
|
-
by Lorenzo Bettini
|
1343
|
-
http://www.lorenzobettini.it
|
1344
|
-
http://www.gnu.org/software/src-highlite -->
|
1345
|
-
<pre><tt><span style="color: #FF0000"><p></span>
|
1346
|
-
<span style="color: #FF0000"><b></span>Name<span style="color: #990000">:</span><span style="color: #FF0000"></b></span>
|
1347
|
-
<span style="color: #FF0000"><%=h @post.name %></span>
|
1348
|
-
<span style="color: #FF0000"></p></span>
|
1349
|
-
|
1350
|
-
<span style="color: #FF0000"><p></span>
|
1351
|
-
<span style="color: #FF0000"><b></span>Title<span style="color: #990000">:</span><span style="color: #FF0000"></b></span>
|
1352
|
-
<span style="color: #FF0000"><%=h @post.title %></span>
|
1353
|
-
<span style="color: #FF0000"></p></span>
|
1354
|
-
|
1355
|
-
<span style="color: #FF0000"><p></span>
|
1356
|
-
<span style="color: #FF0000"><b></span>Content<span style="color: #990000">:</span><span style="color: #FF0000"></b></span>
|
1357
|
-
<span style="color: #FF0000"><%=h @post.content %></span>
|
1358
|
-
<span style="color: #FF0000"></p></span>
|
1359
|
-
|
1360
|
-
|
1361
|
-
<span style="color: #FF0000"><%= link_to 'Edit', edit_post_path(@post) %></span> <span style="color: #990000">|</span>
|
1362
|
-
<span style="color: #FF0000"><%= link_to 'Back', posts_path %></span>
|
1363
|
-
</tt></pre></div></div>
|
1364
|
-
<h3 id="_editing_posts">6.11. Editing Posts</h3>
|
1365
|
-
<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>
|
1366
|
-
<div class="listingblock">
|
1367
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1368
|
-
by Lorenzo Bettini
|
1369
|
-
http://www.lorenzobettini.it
|
1370
|
-
http://www.gnu.org/software/src-highlite -->
|
1371
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
|
1372
|
-
<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>
|
1373
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1374
|
-
</tt></pre></div></div>
|
1375
|
-
<div class="para"><p>After finding the requested post, Rails uses the <tt>edit.html.erb</tt> view to display it:</p></div>
|
1376
|
-
<div class="listingblock">
|
1377
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1378
|
-
by Lorenzo Bettini
|
1379
|
-
http://www.lorenzobettini.it
|
1380
|
-
http://www.gnu.org/software/src-highlite -->
|
1381
|
-
<pre><tt><span style="color: #FF0000"><h1></span>Editing post<span style="color: #FF0000"></h1></span>
|
1382
|
-
|
1383
|
-
<span style="color: #FF0000"><% form_for(@post) do |f| %></span>
|
1384
|
-
<span style="color: #FF0000"><%= f.error_messages %></span>
|
1385
|
-
|
1386
|
-
<span style="color: #FF0000"><p></span>
|
1387
|
-
<span style="color: #FF0000"><%= f.label :name %><br /></span>
|
1388
|
-
<span style="color: #FF0000"><%= f.text_field :name %></span>
|
1389
|
-
<span style="color: #FF0000"></p></span>
|
1390
|
-
<span style="color: #FF0000"><p></span>
|
1391
|
-
<span style="color: #FF0000"><%= f.label :title %><br /></span>
|
1392
|
-
<span style="color: #FF0000"><%= f.text_field :title %></span>
|
1393
|
-
<span style="color: #FF0000"></p></span>
|
1394
|
-
<span style="color: #FF0000"><p></span>
|
1395
|
-
<span style="color: #FF0000"><%= f.label :content %><br /></span>
|
1396
|
-
<span style="color: #FF0000"><%= f.text_area :content %></span>
|
1397
|
-
<span style="color: #FF0000"></p></span>
|
1398
|
-
<span style="color: #FF0000"><p></span>
|
1399
|
-
<span style="color: #FF0000"><%= f.submit "Update" %></span>
|
1400
|
-
<span style="color: #FF0000"></p></span>
|
1401
|
-
<span style="color: #FF0000"><% end %></span>
|
1402
|
-
|
1403
|
-
<span style="color: #FF0000"><%= link_to 'Show', @post %></span> <span style="color: #990000">|</span>
|
1404
|
-
<span style="color: #FF0000"><%= link_to 'Back', posts_path %></span>
|
1405
|
-
</tt></pre></div></div>
|
1406
|
-
<div class="para"><p>Submitting the form created by this view will invoke the <tt>update</tt> action within the controller:</p></div>
|
1407
|
-
<div class="listingblock">
|
1408
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1409
|
-
by Lorenzo Bettini
|
1410
|
-
http://www.lorenzobettini.it
|
1411
|
-
http://www.gnu.org/software/src-highlite -->
|
1412
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
|
1413
|
-
<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>
|
1414
|
-
|
1415
|
-
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>
|
1416
|
-
<span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>update_attributes<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>post<span style="color: #990000">])</span>
|
1417
|
-
flash<span style="color: #990000">[:</span>notice<span style="color: #990000">]</span> <span style="color: #990000">=</span> <span style="color: #FF0000">'Post was successfully updated.'</span>
|
1418
|
-
format<span style="color: #990000">.</span>html <span style="color: #FF0000">{</span> redirect_to<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">)</span> <span style="color: #FF0000">}</span>
|
1419
|
-
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> head <span style="color: #990000">:</span>ok <span style="color: #FF0000">}</span>
|
1420
|
-
<span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
|
1421
|
-
format<span style="color: #990000">.</span>html <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>action <span style="color: #990000">=></span> <span style="color: #FF0000">"edit"</span> <span style="color: #FF0000">}</span>
|
1422
|
-
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> render <span style="color: #990000">:</span>xml <span style="color: #990000">=></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">=></span> <span style="color: #990000">:</span>unprocessable_entity <span style="color: #FF0000">}</span>
|
1423
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1424
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1425
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1426
|
-
</tt></pre></div></div>
|
1427
|
-
<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>
|
1428
|
-
<div class="admonitionblock">
|
1429
|
-
<table><tr>
|
1430
|
-
<td class="icon">
|
1431
|
-
<img src="./images/icons/note.png" alt="Note" />
|
1432
|
-
</td>
|
1433
|
-
<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>
|
1434
|
-
</tr></table>
|
1435
|
-
</div>
|
1436
|
-
<h3 id="_destroying_a_post">6.12. Destroying a Post</h3>
|
1437
|
-
<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>
|
1438
|
-
<div class="listingblock">
|
1439
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1440
|
-
by Lorenzo Bettini
|
1441
|
-
http://www.lorenzobettini.it
|
1442
|
-
http://www.gnu.org/software/src-highlite -->
|
1443
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">def</span></span> destroy
|
1444
|
-
<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>
|
1445
|
-
<span style="color: #009900">@post</span><span style="color: #990000">.</span>destroy
|
1446
|
-
|
1447
|
-
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>
|
1448
|
-
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>
|
1449
|
-
format<span style="color: #990000">.</span>xml <span style="color: #FF0000">{</span> head <span style="color: #990000">:</span>ok <span style="color: #FF0000">}</span>
|
1450
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1451
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1452
|
-
</tt></pre></div></div>
|
1453
|
-
<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>
|
1454
|
-
</div>
|
1455
|
-
<h2 id="_drying_up_the_code">7. DRYing up the Code</h2>
|
1456
|
-
<div class="sectionbody">
|
1457
|
-
<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>
|
1458
|
-
<h3 id="_using_partials_to_eliminate_view_duplication">7.1. Using Partials to Eliminate View Duplication</h3>
|
1459
|
-
<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:</p></div>
|
1460
|
-
<div class="para"><p><tt>new.html.erb</tt>:</p></div>
|
1461
|
-
<div class="listingblock">
|
1462
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1463
|
-
by Lorenzo Bettini
|
1464
|
-
http://www.lorenzobettini.it
|
1465
|
-
http://www.gnu.org/software/src-highlite -->
|
1466
|
-
<pre><tt><span style="color: #FF0000"><h1></span>New post<span style="color: #FF0000"></h1></span>
|
1467
|
-
|
1468
|
-
<span style="color: #FF0000"><%= render :partial =></span> <span style="color: #FF0000">"form"</span> <span style="color: #990000">%></span>
|
1469
|
-
|
1470
|
-
<span style="color: #FF0000"><%= link_to 'Back', posts_path %></span>
|
1471
|
-
</tt></pre></div></div>
|
1472
|
-
<div class="para"><p><tt>edit.html.erb</tt>:</p></div>
|
1473
|
-
<div class="listingblock">
|
1474
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1475
|
-
by Lorenzo Bettini
|
1476
|
-
http://www.lorenzobettini.it
|
1477
|
-
http://www.gnu.org/software/src-highlite -->
|
1478
|
-
<pre><tt><span style="color: #FF0000"><h1></span>Editing post<span style="color: #FF0000"></h1></span>
|
1479
|
-
|
1480
|
-
<span style="color: #FF0000"><%= render :partial =></span> <span style="color: #FF0000">"form"</span> <span style="color: #990000">%></span>
|
1481
|
-
|
1482
|
-
<span style="color: #FF0000"><%= link_to 'Show', @post %></span> <span style="color: #990000">|</span>
|
1483
|
-
<span style="color: #FF0000"><%= link_to 'Back', posts_path %></span>
|
1484
|
-
</tt></pre></div></div>
|
1485
|
-
<div class="para"><p><tt>_form.html.erb</tt>:</p></div>
|
1486
|
-
<div class="listingblock">
|
1487
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1488
|
-
by Lorenzo Bettini
|
1489
|
-
http://www.lorenzobettini.it
|
1490
|
-
http://www.gnu.org/software/src-highlite -->
|
1491
|
-
<pre><tt><span style="color: #FF0000"><% form_for(@post) do |f| %></span>
|
1492
|
-
<span style="color: #FF0000"><%= f.error_messages %></span>
|
1493
|
-
|
1494
|
-
<span style="color: #FF0000"><p></span>
|
1495
|
-
<span style="color: #FF0000"><%= f.label :name %><br /></span>
|
1496
|
-
<span style="color: #FF0000"><%= f.text_field :name %></span>
|
1497
|
-
<span style="color: #FF0000"></p></span>
|
1498
|
-
<span style="color: #FF0000"><p></span>
|
1499
|
-
<span style="color: #FF0000"><%= f.label :title, "title" %><br /></span>
|
1500
|
-
<span style="color: #FF0000"><%= f.text_field :title %></span>
|
1501
|
-
<span style="color: #FF0000"></p></span>
|
1502
|
-
<span style="color: #FF0000"><p></span>
|
1503
|
-
<span style="color: #FF0000"><%= f.label :content %><br /></span>
|
1504
|
-
<span style="color: #FF0000"><%= f.text_area :content %></span>
|
1505
|
-
<span style="color: #FF0000"></p></span>
|
1506
|
-
<span style="color: #FF0000"><p></span>
|
1507
|
-
<span style="color: #FF0000"><%= f.submit "Save" %></span>
|
1508
|
-
<span style="color: #FF0000"></p></span>
|
1509
|
-
<span style="color: #FF0000"><% end %></span>
|
1510
|
-
</tt></pre></div></div>
|
1511
|
-
<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>
|
1512
|
-
<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>
|
1513
|
-
<h3 id="_using_filters_to_eliminate_controller_duplication">7.2. Using Filters to Eliminate Controller Duplication</h3>
|
1514
|
-
<div class="para"><p>At this point, if you look at the controller for posts, you’ll see some duplication:</p></div>
|
1515
|
-
<div class="listingblock">
|
1516
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1517
|
-
by Lorenzo Bettini
|
1518
|
-
http://www.lorenzobettini.it
|
1519
|
-
http://www.gnu.org/software/src-highlite -->
|
1520
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000"><</span> ApplicationController
|
1521
|
-
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
|
1522
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
|
1523
|
-
<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>
|
1524
|
-
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
|
1525
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1526
|
-
|
1527
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
|
1528
|
-
<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>
|
1529
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1530
|
-
|
1531
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
|
1532
|
-
<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>
|
1533
|
-
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
|
1534
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1535
|
-
|
1536
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> destroy
|
1537
|
-
<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>
|
1538
|
-
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
|
1539
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1540
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1541
|
-
</tt></pre></div></div>
|
1542
|
-
<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>
|
1543
|
-
<div class="listingblock">
|
1544
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1545
|
-
by Lorenzo Bettini
|
1546
|
-
http://www.lorenzobettini.it
|
1547
|
-
http://www.gnu.org/software/src-highlite -->
|
1548
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> PostsController <span style="color: #990000"><</span> ApplicationController
|
1549
|
-
before_filter <span style="color: #990000">:</span>find_post<span style="color: #990000">,</span> <span style="color: #990000">:</span>only <span style="color: #990000">=></span> <span style="color: #990000">[:</span>show<span style="color: #990000">,</span> <span style="color: #990000">:</span>edit<span style="color: #990000">,</span> <span style="color: #990000">:</span>update<span style="color: #990000">,</span> <span style="color: #990000">:</span>destroy<span style="color: #990000">]</span>
|
1550
|
-
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
|
1551
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
|
1552
|
-
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
|
1553
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1554
|
-
|
1555
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
|
1556
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1557
|
-
|
1558
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
|
1559
|
-
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
|
1560
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1561
|
-
|
1562
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> destroy
|
1563
|
-
<span style="font-style: italic"><span style="color: #9A1900"># ...</span></span>
|
1564
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1565
|
-
|
1566
|
-
private
|
1567
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> find_post
|
1568
|
-
<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>
|
1569
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1570
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1571
|
-
</tt></pre></div></div>
|
1572
|
-
<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>
|
1573
|
-
<div class="para"><p>For more information on filters, see the <a href="actioncontroller_basics.html">Action Controller Basics</a> guide.</p></div>
|
1574
|
-
</div>
|
1575
|
-
<h2 id="_adding_a_second_model">8. Adding a Second Model</h2>
|
1576
|
-
<div class="sectionbody">
|
1577
|
-
<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>
|
1578
|
-
<h3 id="_generating_a_model">8.1. Generating a Model</h3>
|
1579
|
-
<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>
|
1580
|
-
<div class="listingblock">
|
1581
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1582
|
-
by Lorenzo Bettini
|
1583
|
-
http://www.lorenzobettini.it
|
1584
|
-
http://www.gnu.org/software/src-highlite -->
|
1585
|
-
<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
|
1586
|
-
</tt></pre></div></div>
|
1587
|
-
<div class="para"><p>This command will generate four files:</p></div>
|
1588
|
-
<div class="ilist"><ul>
|
1589
|
-
<li>
|
1590
|
-
<p>
|
1591
|
-
<tt>app/models/comment.rb</tt> - The model
|
1592
|
-
</p>
|
1593
|
-
</li>
|
1594
|
-
<li>
|
1595
|
-
<p>
|
1596
|
-
+db/migrate/20081013214407_create_comments.rb - The migration
|
1597
|
-
</p>
|
1598
|
-
</li>
|
1599
|
-
<li>
|
1600
|
-
<p>
|
1601
|
-
<tt>test/unit/comment_test.rb</tt> and <tt>test/fixtures/comments.yml</tt> - The test harness.
|
1602
|
-
</p>
|
1603
|
-
</li>
|
1604
|
-
</ul></div>
|
1605
|
-
<div class="para"><p>First, take a look at <tt>comment.rb</tt>:</p></div>
|
1606
|
-
<div class="listingblock">
|
1607
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1608
|
-
by Lorenzo Bettini
|
1609
|
-
http://www.lorenzobettini.it
|
1610
|
-
http://www.gnu.org/software/src-highlite -->
|
1611
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Comment <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
1612
|
-
belongs_to <span style="color: #990000">:</span>post
|
1613
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1614
|
-
</tt></pre></div></div>
|
1615
|
-
<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>
|
1616
|
-
<div class="para"><p>In addition to the model, Rails has also made a migration to create the corresponding database table:</p></div>
|
1617
|
-
<div class="listingblock">
|
1618
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1619
|
-
by Lorenzo Bettini
|
1620
|
-
http://www.lorenzobettini.it
|
1621
|
-
http://www.gnu.org/software/src-highlite -->
|
1622
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CreateComments <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Migration
|
1623
|
-
<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>up
|
1624
|
-
create_table <span style="color: #990000">:</span>comments <span style="font-weight: bold"><span style="color: #0000FF">do</span></span> <span style="color: #990000">|</span>t<span style="color: #990000">|</span>
|
1625
|
-
t<span style="color: #990000">.</span>string <span style="color: #990000">:</span>commenter
|
1626
|
-
t<span style="color: #990000">.</span>text <span style="color: #990000">:</span>body
|
1627
|
-
t<span style="color: #990000">.</span>references <span style="color: #990000">:</span>post
|
1628
|
-
|
1629
|
-
t<span style="color: #990000">.</span>timestamps
|
1630
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1631
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1632
|
-
|
1633
|
-
<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
|
1634
|
-
drop_table <span style="color: #990000">:</span>comments
|
1635
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1636
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1637
|
-
</tt></pre></div></div>
|
1638
|
-
<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>
|
1639
|
-
<div class="listingblock">
|
1640
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1641
|
-
by Lorenzo Bettini
|
1642
|
-
http://www.lorenzobettini.it
|
1643
|
-
http://www.gnu.org/software/src-highlite -->
|
1644
|
-
<pre><tt>$ rake db<span style="color: #990000">:</span>migrate
|
1645
|
-
</tt></pre></div></div>
|
1646
|
-
<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>
|
1647
|
-
<h3 id="_associating_models">8.2. Associating Models</h3>
|
1648
|
-
<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>
|
1649
|
-
<div class="ilist"><ul>
|
1650
|
-
<li>
|
1651
|
-
<p>
|
1652
|
-
Each comment belongs to one post
|
1653
|
-
</p>
|
1654
|
-
</li>
|
1655
|
-
<li>
|
1656
|
-
<p>
|
1657
|
-
One post can have many comments
|
1658
|
-
</p>
|
1659
|
-
</li>
|
1660
|
-
</ul></div>
|
1661
|
-
<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>
|
1662
|
-
<div class="listingblock">
|
1663
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1664
|
-
by Lorenzo Bettini
|
1665
|
-
http://www.lorenzobettini.it
|
1666
|
-
http://www.gnu.org/software/src-highlite -->
|
1667
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Comment <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
1668
|
-
belongs_to <span style="color: #990000">:</span>post
|
1669
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1670
|
-
</tt></pre></div></div>
|
1671
|
-
<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>
|
1672
|
-
<div class="listingblock">
|
1673
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1674
|
-
by Lorenzo Bettini
|
1675
|
-
http://www.lorenzobettini.it
|
1676
|
-
http://www.gnu.org/software/src-highlite -->
|
1677
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> Post <span style="color: #990000"><</span> ActiveRecord<span style="color: #990000">::</span>Base
|
1678
|
-
validates_presence_of <span style="color: #990000">:</span>name<span style="color: #990000">,</span> <span style="color: #990000">:</span>title
|
1679
|
-
validates_length_of <span style="color: #990000">:</span>title<span style="color: #990000">,</span> <span style="color: #990000">:</span>minimum <span style="color: #990000">=></span> <span style="color: #993399">5</span>
|
1680
|
-
has_many <span style="color: #990000">:</span>comments
|
1681
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1682
|
-
</tt></pre></div></div>
|
1683
|
-
<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>
|
1684
|
-
<div class="admonitionblock">
|
1685
|
-
<table><tr>
|
1686
|
-
<td class="icon">
|
1687
|
-
<img src="./images/icons/tip.png" alt="Tip" />
|
1688
|
-
</td>
|
1689
|
-
<td class="content">For more information on Active Record associations, see the <a href="../association_basics.html">Active Record Associations</a> guide.</td>
|
1690
|
-
</tr></table>
|
1691
|
-
</div>
|
1692
|
-
<h3 id="_adding_a_route">8.3. Adding a Route</h3>
|
1693
|
-
<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>
|
1694
|
-
<div class="listingblock">
|
1695
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1696
|
-
by Lorenzo Bettini
|
1697
|
-
http://www.lorenzobettini.it
|
1698
|
-
http://www.gnu.org/software/src-highlite -->
|
1699
|
-
<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>
|
1700
|
-
post<span style="color: #990000">.</span>resources <span style="color: #990000">:</span>comments
|
1701
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1702
|
-
</tt></pre></div></div>
|
1703
|
-
<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>
|
1704
|
-
<div class="admonitionblock">
|
1705
|
-
<table><tr>
|
1706
|
-
<td class="icon">
|
1707
|
-
<img src="./images/icons/tip.png" alt="Tip" />
|
1708
|
-
</td>
|
1709
|
-
<td class="content">For more information on routing, see the <a href="../routing_outside_in">Rails Routing from the Outside In</a> guide.</td>
|
1710
|
-
</tr></table>
|
1711
|
-
</div>
|
1712
|
-
<h3 id="_generating_a_controller">8.4. Generating a Controller</h3>
|
1713
|
-
<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>
|
1714
|
-
<div class="listingblock">
|
1715
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1716
|
-
by Lorenzo Bettini
|
1717
|
-
http://www.lorenzobettini.it
|
1718
|
-
http://www.gnu.org/software/src-highlite -->
|
1719
|
-
<pre><tt>$ script/generate controller Comments index show new edit
|
1720
|
-
</tt></pre></div></div>
|
1721
|
-
<div class="para"><p>This creates seven files:</p></div>
|
1722
|
-
<div class="ilist"><ul>
|
1723
|
-
<li>
|
1724
|
-
<p>
|
1725
|
-
<tt>app/controllers/comments_controller.rb</tt> - The controller
|
1726
|
-
</p>
|
1727
|
-
</li>
|
1728
|
-
<li>
|
1729
|
-
<p>
|
1730
|
-
<tt>app/helpers/comments_helper.rb</tt> - A view helper file
|
1731
|
-
</p>
|
1732
|
-
</li>
|
1733
|
-
<li>
|
1734
|
-
<p>
|
1735
|
-
<tt>app/views/comments/index.html.erb</tt> - The view for the index action
|
1736
|
-
</p>
|
1737
|
-
</li>
|
1738
|
-
<li>
|
1739
|
-
<p>
|
1740
|
-
<tt>app/views/comments/show.html.erb</tt> - The view for the show action
|
1741
|
-
</p>
|
1742
|
-
</li>
|
1743
|
-
<li>
|
1744
|
-
<p>
|
1745
|
-
<tt>app/views/comments/new.html.erb</tt> - The view for the new action
|
1746
|
-
</p>
|
1747
|
-
</li>
|
1748
|
-
<li>
|
1749
|
-
<p>
|
1750
|
-
<tt>app/views/comments/edit.html.erb</tt> - The view for the edit action
|
1751
|
-
</p>
|
1752
|
-
</li>
|
1753
|
-
<li>
|
1754
|
-
<p>
|
1755
|
-
<tt>test/functional/comments_controller_test.rb</tt> - The functional tests for the controller
|
1756
|
-
</p>
|
1757
|
-
</li>
|
1758
|
-
</ul></div>
|
1759
|
-
<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>
|
1760
|
-
<div class="listingblock">
|
1761
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1762
|
-
by Lorenzo Bettini
|
1763
|
-
http://www.lorenzobettini.it
|
1764
|
-
http://www.gnu.org/software/src-highlite -->
|
1765
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CommentsController <span style="color: #990000"><</span> ApplicationController
|
1766
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
|
1767
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1768
|
-
|
1769
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
|
1770
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1771
|
-
|
1772
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new
|
1773
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1774
|
-
|
1775
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
|
1776
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1777
|
-
|
1778
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1779
|
-
</tt></pre></div></div>
|
1780
|
-
<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>
|
1781
|
-
<div class="listingblock">
|
1782
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1783
|
-
by Lorenzo Bettini
|
1784
|
-
http://www.lorenzobettini.it
|
1785
|
-
http://www.gnu.org/software/src-highlite -->
|
1786
|
-
<pre><tt><span style="font-weight: bold"><span style="color: #0000FF">class</span></span> CommentsController <span style="color: #990000"><</span> ApplicationController
|
1787
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> index
|
1788
|
-
<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>post_id<span style="color: #990000">])</span>
|
1789
|
-
<span style="color: #009900">@comments</span> <span style="color: #990000">=</span> <span style="color: #009900">@post</span><span style="color: #990000">.</span>comments
|
1790
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1791
|
-
|
1792
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> show
|
1793
|
-
<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>post_id<span style="color: #990000">])</span>
|
1794
|
-
<span style="color: #009900">@comment</span> <span style="color: #990000">=</span> Comment<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
|
1795
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1796
|
-
|
1797
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> new
|
1798
|
-
<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>post_id<span style="color: #990000">])</span>
|
1799
|
-
<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
|
1800
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1801
|
-
|
1802
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> create
|
1803
|
-
<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>post_id<span style="color: #990000">])</span>
|
1804
|
-
<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<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>comment<span style="color: #990000">])</span>
|
1805
|
-
<span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@comment</span><span style="color: #990000">.</span>save
|
1806
|
-
redirect_to post_comment_path<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">,</span> <span style="color: #009900">@comment</span><span style="color: #990000">)</span>
|
1807
|
-
<span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
|
1808
|
-
render <span style="color: #990000">:</span>action <span style="color: #990000">=></span> <span style="color: #FF0000">"new"</span>
|
1809
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1810
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1811
|
-
|
1812
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> edit
|
1813
|
-
<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>post_id<span style="color: #990000">])</span>
|
1814
|
-
<span style="color: #009900">@comment</span> <span style="color: #990000">=</span> Comment<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
|
1815
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1816
|
-
|
1817
|
-
<span style="font-weight: bold"><span style="color: #0000FF">def</span></span> update
|
1818
|
-
<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>post_id<span style="color: #990000">])</span>
|
1819
|
-
<span style="color: #009900">@comment</span> <span style="color: #990000">=</span> Comment<span style="color: #990000">.</span>find<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>id<span style="color: #990000">])</span>
|
1820
|
-
<span style="font-weight: bold"><span style="color: #0000FF">if</span></span> <span style="color: #009900">@comment</span><span style="color: #990000">.</span>update_attributes<span style="color: #990000">(</span>params<span style="color: #990000">[:</span>comment<span style="color: #990000">])</span>
|
1821
|
-
redirect_to post_comment_path<span style="color: #990000">(</span><span style="color: #009900">@post</span><span style="color: #990000">,</span> <span style="color: #009900">@comment</span><span style="color: #990000">)</span>
|
1822
|
-
<span style="font-weight: bold"><span style="color: #0000FF">else</span></span>
|
1823
|
-
render <span style="color: #990000">:</span>action <span style="color: #990000">=></span> <span style="color: #FF0000">"edit"</span>
|
1824
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1825
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1826
|
-
|
1827
|
-
<span style="font-weight: bold"><span style="color: #0000FF">end</span></span>
|
1828
|
-
</tt></pre></div></div>
|
1829
|
-
<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>
|
1830
|
-
<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>
|
1831
|
-
<div class="listingblock">
|
1832
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1833
|
-
by Lorenzo Bettini
|
1834
|
-
http://www.lorenzobettini.it
|
1835
|
-
http://www.gnu.org/software/src-highlite -->
|
1836
|
-
<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
|
1837
|
-
</tt></pre></div></div>
|
1838
|
-
<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>
|
1839
|
-
<h3 id="_building_views">8.5. Building Views</h3>
|
1840
|
-
<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>
|
1841
|
-
<div class="para"><p>The <tt>index.html.erb</tt> view:</p></div>
|
1842
|
-
<div class="listingblock">
|
1843
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1844
|
-
by Lorenzo Bettini
|
1845
|
-
http://www.lorenzobettini.it
|
1846
|
-
http://www.gnu.org/software/src-highlite -->
|
1847
|
-
<pre><tt><span style="color: #FF0000"><h1></span>Comments <span style="font-weight: bold"><span style="color: #0000FF">for</span></span> <span style="color: #FF0000"><%= @post.title %></h1></span>
|
1848
|
-
|
1849
|
-
<span style="color: #FF0000"><table></span>
|
1850
|
-
<span style="color: #FF0000"><tr></span>
|
1851
|
-
<span style="color: #FF0000"><th></span>Commenter<span style="color: #FF0000"></th></span>
|
1852
|
-
<span style="color: #FF0000"><th></span>Body<span style="color: #FF0000"></th></span>
|
1853
|
-
<span style="color: #FF0000"></tr></span>
|
1854
|
-
|
1855
|
-
<span style="color: #FF0000"><% for comment in @comments %></span>
|
1856
|
-
<span style="color: #FF0000"><tr></span>
|
1857
|
-
<span style="color: #FF0000"><td><%=h comment.commenter %></td></span>
|
1858
|
-
<span style="color: #FF0000"><td><%=h comment.body %></td></span>
|
1859
|
-
<span style="color: #FF0000"><td><%= link_to 'Show', post_comment_path(@post, comment) %></td></span>
|
1860
|
-
<span style="color: #FF0000"><td><%= link_to 'Edit', edit_post_comment_path(@post, comment) %></td></span>
|
1861
|
-
<span style="color: #FF0000"><td><%= link_to 'Destroy', post_comment_path(@post, comment), :confirm =></span> <span style="color: #FF0000">'Are you sure?'</span><span style="color: #990000">,</span> <span style="color: #990000">:</span>method <span style="color: #990000">=></span> <span style="color: #990000">:</span>delete <span style="color: #990000">%></span><span style="color: #FF0000"></td></span>
|
1862
|
-
<span style="color: #FF0000"></tr></span>
|
1863
|
-
<span style="color: #FF0000"><% end %></span>
|
1864
|
-
<span style="color: #FF0000"></table></span>
|
1865
|
-
|
1866
|
-
<span style="color: #FF0000"><br /></span>
|
1867
|
-
|
1868
|
-
<span style="color: #FF0000"><%= link_to 'New comment', new_post_comment_path(@post) %></span>
|
1869
|
-
<span style="color: #FF0000"><%= link_to 'Back to Post', @post %></span>
|
1870
|
-
</tt></pre></div></div>
|
1871
|
-
<div class="para"><p>The <tt>new.html.erb</tt> view:</p></div>
|
1872
|
-
<div class="listingblock">
|
1873
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1874
|
-
by Lorenzo Bettini
|
1875
|
-
http://www.lorenzobettini.it
|
1876
|
-
http://www.gnu.org/software/src-highlite -->
|
1877
|
-
<pre><tt><span style="color: #FF0000"><h1></span>New comment<span style="color: #FF0000"></h1></span>
|
1878
|
-
|
1879
|
-
<span style="color: #FF0000"><% form_for([@post, @comment]) do |f| %></span>
|
1880
|
-
<span style="color: #FF0000"><%= f.error_messages %></span>
|
1881
|
-
|
1882
|
-
<span style="color: #FF0000"><p></span>
|
1883
|
-
<span style="color: #FF0000"><%= f.label :commenter %><br /></span>
|
1884
|
-
<span style="color: #FF0000"><%= f.text_field :commenter %></span>
|
1885
|
-
<span style="color: #FF0000"></p></span>
|
1886
|
-
<span style="color: #FF0000"><p></span>
|
1887
|
-
<span style="color: #FF0000"><%= f.label :body %><br /></span>
|
1888
|
-
<span style="color: #FF0000"><%= f.text_area :body %></span>
|
1889
|
-
<span style="color: #FF0000"></p></span>
|
1890
|
-
<span style="color: #FF0000"><p></span>
|
1891
|
-
<span style="color: #FF0000"><%= f.submit "Create" %></span>
|
1892
|
-
<span style="color: #FF0000"></p></span>
|
1893
|
-
<span style="color: #FF0000"><% end %></span>
|
1894
|
-
|
1895
|
-
<span style="color: #FF0000"><%= link_to 'Back', post_comments_path(@post) %></span>
|
1896
|
-
</tt></pre></div></div>
|
1897
|
-
<div class="para"><p>The <tt>show.html.erb</tt> view:</p></div>
|
1898
|
-
<div class="listingblock">
|
1899
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1900
|
-
by Lorenzo Bettini
|
1901
|
-
http://www.lorenzobettini.it
|
1902
|
-
http://www.gnu.org/software/src-highlite -->
|
1903
|
-
<pre><tt><span style="color: #FF0000"><h1></span>Comment on <span style="color: #FF0000"><%= @post.title %></h1></span>
|
1904
|
-
|
1905
|
-
<span style="color: #FF0000"><p></span>
|
1906
|
-
<span style="color: #FF0000"><b></span>Commenter<span style="color: #990000">:</span><span style="color: #FF0000"></b></span>
|
1907
|
-
<span style="color: #FF0000"><%=h @comment.commenter %></span>
|
1908
|
-
<span style="color: #FF0000"></p></span>
|
1909
|
-
|
1910
|
-
<span style="color: #FF0000"><p></span>
|
1911
|
-
<span style="color: #FF0000"><b></span>Comment<span style="color: #990000">:</span><span style="color: #FF0000"></b></span>
|
1912
|
-
<span style="color: #FF0000"><%=h @comment.body %></span>
|
1913
|
-
<span style="color: #FF0000"></p></span>
|
1914
|
-
|
1915
|
-
<span style="color: #FF0000"><%= link_to 'Edit', edit_post_comment_path(@post, @comment) %></span> <span style="color: #990000">|</span>
|
1916
|
-
<span style="color: #FF0000"><%= link_to 'Back', post_comments_path(@post) %></span>
|
1917
|
-
</tt></pre></div></div>
|
1918
|
-
<div class="para"><p>The <tt>edit.html.erb</tt> view:</p></div>
|
1919
|
-
<div class="listingblock">
|
1920
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1921
|
-
by Lorenzo Bettini
|
1922
|
-
http://www.lorenzobettini.it
|
1923
|
-
http://www.gnu.org/software/src-highlite -->
|
1924
|
-
<pre><tt><span style="color: #FF0000"><h1></span>Editing comment<span style="color: #FF0000"></h1></span>
|
1925
|
-
|
1926
|
-
<span style="color: #FF0000"><% form_for([@post, @comment]) do |f| %></span>
|
1927
|
-
<span style="color: #FF0000"><%= f.error_messages %></span>
|
1928
|
-
|
1929
|
-
<span style="color: #FF0000"><p></span>
|
1930
|
-
<span style="color: #FF0000"><%= f.label :commenter %><br /></span>
|
1931
|
-
<span style="color: #FF0000"><%= f.text_field :commenter %></span>
|
1932
|
-
<span style="color: #FF0000"></p></span>
|
1933
|
-
<span style="color: #FF0000"><p></span>
|
1934
|
-
<span style="color: #FF0000"><%= f.label :body %><br /></span>
|
1935
|
-
<span style="color: #FF0000"><%= f.text_area :body %></span>
|
1936
|
-
<span style="color: #FF0000"></p></span>
|
1937
|
-
<span style="color: #FF0000"><p></span>
|
1938
|
-
<span style="color: #FF0000"><%= f.submit "Update" %></span>
|
1939
|
-
<span style="color: #FF0000"></p></span>
|
1940
|
-
<span style="color: #FF0000"><% end %></span>
|
1941
|
-
|
1942
|
-
<span style="color: #FF0000"><%= link_to 'Show', post_comment_path(@post, @comment) %></span> <span style="color: #990000">|</span>
|
1943
|
-
<span style="color: #FF0000"><%= link_to 'Back', post_comments_path(@post) %></span>
|
1944
|
-
</tt></pre></div></div>
|
1945
|
-
<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>
|
1946
|
-
<h3 id="_hooking_comments_to_posts">8.6. Hooking Comments to Posts</h3>
|
1947
|
-
<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>
|
1948
|
-
<div class="listingblock">
|
1949
|
-
<div class="content"><!-- Generator: GNU source-highlight 2.9
|
1950
|
-
by Lorenzo Bettini
|
1951
|
-
http://www.lorenzobettini.it
|
1952
|
-
http://www.gnu.org/software/src-highlite -->
|
1953
|
-
<pre><tt><span style="color: #FF0000"><p></span>
|
1954
|
-
<span style="color: #FF0000"><b></span>Name<span style="color: #990000">:</span><span style="color: #FF0000"></b></span>
|
1955
|
-
<span style="color: #FF0000"><%=h @post.name %></span>
|
1956
|
-
<span style="color: #FF0000"></p></span>
|
1957
|
-
|
1958
|
-
<span style="color: #FF0000"><p></span>
|
1959
|
-
<span style="color: #FF0000"><b></span>Title<span style="color: #990000">:</span><span style="color: #FF0000"></b></span>
|
1960
|
-
<span style="color: #FF0000"><%=h @post.title %></span>
|
1961
|
-
<span style="color: #FF0000"></p></span>
|
1962
|
-
|
1963
|
-
<span style="color: #FF0000"><p></span>
|
1964
|
-
<span style="color: #FF0000"><b></span>Content<span style="color: #990000">:</span><span style="color: #FF0000"></b></span>
|
1965
|
-
<span style="color: #FF0000"><%=h @post.content %></span>
|
1966
|
-
<span style="color: #FF0000"></p></span>
|
1967
|
-
|
1968
|
-
<span style="color: #FF0000"><h2></span>Comments<span style="color: #FF0000"></h2></span>
|
1969
|
-
<span style="color: #FF0000"><% @post.comments.each do |c| %></span>
|
1970
|
-
<span style="color: #FF0000"><p></span>
|
1971
|
-
<span style="color: #FF0000"><b></span>Commenter<span style="color: #990000">:</span><span style="color: #FF0000"></b></span>
|
1972
|
-
<span style="color: #FF0000"><%=h c.commenter %></span>
|
1973
|
-
<span style="color: #FF0000"></p></span>
|
1974
|
-
|
1975
|
-
<span style="color: #FF0000"><p></span>
|
1976
|
-
<span style="color: #FF0000"><b></span>Comment<span style="color: #990000">:</span><span style="color: #FF0000"></b></span>
|
1977
|
-
<span style="color: #FF0000"><%=h c.body %></span>
|
1978
|
-
<span style="color: #FF0000"></p></span>
|
1979
|
-
<span style="color: #FF0000"><% end %></span>
|
1980
|
-
|
1981
|
-
<span style="color: #FF0000"><%= link_to 'Edit', edit_post_path(@post) %></span> <span style="color: #990000">|</span>
|
1982
|
-
<span style="color: #FF0000"><%= link_to 'Back', posts_path %></span>
|
1983
|
-
<span style="color: #FF0000"><%= link_to 'Manage Comments', post_comments_path(@post) %></span>
|
1984
|
-
</tt></pre></div></div>
|
1985
|
-
<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>
|
1986
|
-
</div>
|
1987
|
-
<h2 id="_what_s_next">9. What's Next?</h2>
|
1988
|
-
<div class="sectionbody">
|
1989
|
-
<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>
|
1990
|
-
<div class="ilist"><ul>
|
1991
|
-
<li>
|
1992
|
-
<p>
|
1993
|
-
The <a href="http://manuals.rubyonrails.org/">Ruby On Rails guides</a>
|
1994
|
-
</p>
|
1995
|
-
</li>
|
1996
|
-
<li>
|
1997
|
-
<p>
|
1998
|
-
The <a href="http://groups.google.com/group/rubyonrails-talk">Ruby on Rails mailing list</a>
|
1999
|
-
</p>
|
2000
|
-
</li>
|
2001
|
-
<li>
|
2002
|
-
<p>
|
2003
|
-
The #rubyonrails channel on irc.freenode.net
|
2004
|
-
</p>
|
2005
|
-
</li>
|
2006
|
-
<li>
|
2007
|
-
<p>
|
2008
|
-
The <a href="http://wiki.rubyonrails.org/rails">Rails wiki</a>
|
2009
|
-
</p>
|
2010
|
-
</li>
|
2011
|
-
</ul></div>
|
2012
|
-
<div class="para"><p>Rails also comes with built-in help that you can generate using the rake command-line utility:</p></div>
|
2013
|
-
<div class="ilist"><ul>
|
2014
|
-
<li>
|
2015
|
-
<p>
|
2016
|
-
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.
|
2017
|
-
</p>
|
2018
|
-
</li>
|
2019
|
-
<li>
|
2020
|
-
<p>
|
2021
|
-
Running <tt>rake doc:rails</tt> will put a full copy of the API documentation for Rails in the <tt>/doc/api</tt> folder of your application. Open <tt>/doc/api/index.html</tt> in your web browser to explore the API documentation.
|
2022
|
-
</p>
|
2023
|
-
</li>
|
2024
|
-
</ul></div>
|
2025
|
-
</div>
|
2026
|
-
<h2 id="_changelog">10. Changelog</h2>
|
2027
|
-
<div class="sectionbody">
|
2028
|
-
<div class="para"><p><a href="http://rails.lighthouseapp.com/projects/16213-rails-guides/tickets/2">Lighthouse ticket</a></p></div>
|
2029
|
-
<div class="ilist"><ul>
|
2030
|
-
<li>
|
2031
|
-
<p>
|
2032
|
-
November 3, 2008: Formatting patch from Dave Rothlisberger
|
2033
|
-
</p>
|
2034
|
-
</li>
|
2035
|
-
<li>
|
2036
|
-
<p>
|
2037
|
-
November 1, 2008: First approved version by <a href="../authors.html#mgunderloy">Mike Gunderloy</a>
|
2038
|
-
</p>
|
2039
|
-
</li>
|
2040
|
-
<li>
|
2041
|
-
<p>
|
2042
|
-
October 16, 2008: Revised based on feedback from Pratik Naik by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
|
2043
|
-
</p>
|
2044
|
-
</li>
|
2045
|
-
<li>
|
2046
|
-
<p>
|
2047
|
-
October 13, 2008: First complete draft by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
|
2048
|
-
</p>
|
2049
|
-
</li>
|
2050
|
-
<li>
|
2051
|
-
<p>
|
2052
|
-
October 12, 2008: More detail, rearrangement, editing by <a href="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
|
2053
|
-
</p>
|
2054
|
-
</li>
|
2055
|
-
<li>
|
2056
|
-
<p>
|
2057
|
-
September 8, 2008: initial version by James Miller (not yet approved for publication)
|
2058
|
-
</p>
|
2059
|
-
</li>
|
2060
|
-
</ul></div>
|
2061
|
-
</div>
|
2062
|
-
|
2063
|
-
</div>
|
2064
|
-
</div>
|
2065
|
-
</body>
|
2066
|
-
</html>
|