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,638 +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>Rails form helpers</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="#_basic_forms">Basic forms</a>
|
203
|
-
<ul>
|
204
|
-
|
205
|
-
<li><a href="#_generic_search_form">Generic search form</a></li>
|
206
|
-
|
207
|
-
<li><a href="#_multiple_hashes_in_form_helper_attributes">Multiple hashes in form helper attributes</a></li>
|
208
|
-
|
209
|
-
<li><a href="#_checkboxes_radio_buttons_and_other_controls">Checkboxes, radio buttons and other controls</a></li>
|
210
|
-
|
211
|
-
<li><a href="#_how_do_forms_with_put_or_delete_methods_work">How do forms with PUT or DELETE methods work?</a></li>
|
212
|
-
|
213
|
-
</ul>
|
214
|
-
</li>
|
215
|
-
<li>
|
216
|
-
<a href="#_forms_that_deal_with_model_attributes">Forms that deal with model attributes</a>
|
217
|
-
<ul>
|
218
|
-
|
219
|
-
<li><a href="#_relying_on_record_identification">Relying on record identification</a></li>
|
220
|
-
|
221
|
-
</ul>
|
222
|
-
</li>
|
223
|
-
<li>
|
224
|
-
<a href="#_making_select_boxes_with_ease">Making select boxes with ease</a>
|
225
|
-
<ul>
|
226
|
-
|
227
|
-
<li><a href="#_the_select_tag_and_options">The select tag and options</a></li>
|
228
|
-
|
229
|
-
<li><a href="#_select_boxes_for_dealing_with_models">Select boxes for dealing with models</a></li>
|
230
|
-
|
231
|
-
</ul>
|
232
|
-
</li>
|
233
|
-
</ol>
|
234
|
-
</div>
|
235
|
-
|
236
|
-
<div id="content">
|
237
|
-
<h1>Rails form helpers</h1>
|
238
|
-
<div id="preamble">
|
239
|
-
<div class="sectionbody">
|
240
|
-
<div class="para"><p>Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of form control naming and their numerous attributes. Rails deals away with these complexities by providing view helpers for generating form markup. However, since they have different use-cases, developers are required to know all the differences between similar helper methods before putting them to use.</p></div>
|
241
|
-
<div class="para"><p>In this guide we will:</p></div>
|
242
|
-
<div class="ilist"><ul>
|
243
|
-
<li>
|
244
|
-
<p>
|
245
|
-
Create search forms and similar kind of generic forms not representing any specific model in your application;
|
246
|
-
</p>
|
247
|
-
</li>
|
248
|
-
<li>
|
249
|
-
<p>
|
250
|
-
Make model-centric forms for creation and editing of specific database records;
|
251
|
-
</p>
|
252
|
-
</li>
|
253
|
-
<li>
|
254
|
-
<p>
|
255
|
-
Generate select boxes from multiple types of data;
|
256
|
-
</p>
|
257
|
-
</li>
|
258
|
-
<li>
|
259
|
-
<p>
|
260
|
-
Learn what makes a file upload form different;
|
261
|
-
</p>
|
262
|
-
</li>
|
263
|
-
<li>
|
264
|
-
<p>
|
265
|
-
Build complex, multi-model forms.
|
266
|
-
</p>
|
267
|
-
</li>
|
268
|
-
</ul></div>
|
269
|
-
<div class="admonitionblock">
|
270
|
-
<table><tr>
|
271
|
-
<td class="icon">
|
272
|
-
<img src="./images/icons/note.png" alt="Note" />
|
273
|
-
</td>
|
274
|
-
<td class="content">This guide is not intended to be a complete documentation of available form helpers and their arguments. Please visit <a href="http://api.rubyonrails.org/">the Rails API documentation</a> for a complete reference.</td>
|
275
|
-
</tr></table>
|
276
|
-
</div>
|
277
|
-
</div>
|
278
|
-
</div>
|
279
|
-
<h2 id="_basic_forms">1. Basic forms</h2>
|
280
|
-
<div class="sectionbody">
|
281
|
-
<div class="para"><p>The most basic form helper is <tt>form_tag</tt>.</p></div>
|
282
|
-
<div class="listingblock">
|
283
|
-
<div class="content">
|
284
|
-
<pre><tt><% form_tag do %>
|
285
|
-
Form contents
|
286
|
-
<% end %></tt></pre>
|
287
|
-
</div></div>
|
288
|
-
<div class="para"><p>When called without arguments like this, it creates a form element that has the current page for action attribute and "POST" as method (some line breaks added for readability):</p></div>
|
289
|
-
<div class="listingblock">
|
290
|
-
<div class="title">Example: Sample rendering of <tt>form_tag</tt></div>
|
291
|
-
<div class="content">
|
292
|
-
<pre><tt><form action="/home/index" method="post">
|
293
|
-
<div style="margin:0;padding:0">
|
294
|
-
<input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" />
|
295
|
-
</div>
|
296
|
-
Form contents
|
297
|
-
</form></tt></pre>
|
298
|
-
</div></div>
|
299
|
-
<div class="para"><p>If you carefully observe this output, you can see that the helper generated something we didn't specify: a <tt>div</tt> element with a hidden input inside. This is a security feature of Rails called <strong>cross-site request forgery protection</strong> and form helpers generate it for every form which action isn't "GET" (provided that this security feature is enabled).</p></div>
|
300
|
-
<div class="admonitionblock">
|
301
|
-
<table><tr>
|
302
|
-
<td class="icon">
|
303
|
-
<img src="./images/icons/note.png" alt="Note" />
|
304
|
-
</td>
|
305
|
-
<td class="content">Throughout this guide, this <tt>div</tt> with the hidden input will be stripped away to have clearer code samples.</td>
|
306
|
-
</tr></table>
|
307
|
-
</div>
|
308
|
-
<h3 id="_generic_search_form">1.1. Generic search form</h3>
|
309
|
-
<div class="para"><p>Probably the most minimal form often seen on the web is a search form with a single text input for search terms. This form consists of:</p></div>
|
310
|
-
<div class="olist"><ol>
|
311
|
-
<li>
|
312
|
-
<p>
|
313
|
-
a form element with "GET" method,
|
314
|
-
</p>
|
315
|
-
</li>
|
316
|
-
<li>
|
317
|
-
<p>
|
318
|
-
a label for the input,
|
319
|
-
</p>
|
320
|
-
</li>
|
321
|
-
<li>
|
322
|
-
<p>
|
323
|
-
a text input element, and
|
324
|
-
</p>
|
325
|
-
</li>
|
326
|
-
<li>
|
327
|
-
<p>
|
328
|
-
a submit element.
|
329
|
-
</p>
|
330
|
-
</li>
|
331
|
-
</ol></div>
|
332
|
-
<div class="admonitionblock">
|
333
|
-
<table><tr>
|
334
|
-
<td class="icon">
|
335
|
-
<img src="./images/icons/important.png" alt="Important" />
|
336
|
-
</td>
|
337
|
-
<td class="content">Always use "GET" as the method for search forms. Benefits are many: users are able to bookmark a specific search and get back to it; browsers cache results of "GET" requests, but not "POST"; and other.</td>
|
338
|
-
</tr></table>
|
339
|
-
</div>
|
340
|
-
<div class="para"><p>To create that, we will use <tt>form_tag</tt>, <tt>label_tag</tt>, <tt>text_field_tag</tt> and <tt>submit_tag</tt>, respectively.</p></div>
|
341
|
-
<div class="listingblock">
|
342
|
-
<div class="title">Example: A basic search form</div>
|
343
|
-
<div class="content">
|
344
|
-
<pre><tt><% form_tag(search_path, :method => "get") do %>
|
345
|
-
<%= label_tag(:q, "Search for:") %>
|
346
|
-
<%= text_field_tag(:q) %>
|
347
|
-
<%= submit_tag("Search") %>
|
348
|
-
<% end %></tt></pre>
|
349
|
-
</div></div>
|
350
|
-
<div class="admonitionblock">
|
351
|
-
<table><tr>
|
352
|
-
<td class="icon">
|
353
|
-
<img src="./images/icons/tip.png" alt="Tip" />
|
354
|
-
</td>
|
355
|
-
<td class="content">
|
356
|
-
<div class="para"><p><tt>search_path</tt> can be a named route specified in "routes.rb":</p></div>
|
357
|
-
<div class="listingblock">
|
358
|
-
<div class="content">
|
359
|
-
<pre><tt>map.search "search", :controller => "search"</tt></pre>
|
360
|
-
</div></div>
|
361
|
-
</td>
|
362
|
-
</tr></table>
|
363
|
-
</div>
|
364
|
-
<div class="para"><p>The above view code will result in the following markup:</p></div>
|
365
|
-
<div class="listingblock">
|
366
|
-
<div class="title">Example: Search form HTML</div>
|
367
|
-
<div class="content">
|
368
|
-
<pre><tt><form action="/search" method="get">
|
369
|
-
<label for="q">Search for:</label>
|
370
|
-
<input id="q" name="q" type="text" />
|
371
|
-
<input name="commit" type="submit" value="Search" />
|
372
|
-
</form></tt></pre>
|
373
|
-
</div></div>
|
374
|
-
<div class="para"><p>Besides <tt>text_field_tag</tt> and <tt>submit_tag</tt>, there is a similar helper for <em>every</em> form control in HTML.</p></div>
|
375
|
-
<div class="admonitionblock">
|
376
|
-
<table><tr>
|
377
|
-
<td class="icon">
|
378
|
-
<img src="./images/icons/tip.png" alt="Tip" />
|
379
|
-
</td>
|
380
|
-
<td class="content">For every form input, an ID attribute is generated from its name ("q" in our example). These IDs can be very useful for CSS styling or manipulation of form controls with JavaScript.</td>
|
381
|
-
</tr></table>
|
382
|
-
</div>
|
383
|
-
<h3 id="_multiple_hashes_in_form_helper_attributes">1.2. Multiple hashes in form helper attributes</h3>
|
384
|
-
<div class="para"><p>By now we've seen that the <tt>form_tag</tt> helper accepts 2 arguments: the path for the action attribute and an options hash for parameters (like <tt>:method</tt>).</p></div>
|
385
|
-
<div class="para"><p>Identical to the <tt>link_to</tt> helper, the path argument doesn't have to be given as string or a named route. It can be a hash of URL parameters that Rails' routing mechanism will turn into a valid URL. Still, we cannot simply write this:</p></div>
|
386
|
-
<div class="listingblock">
|
387
|
-
<div class="title">Example: A bad way to pass multiple hashes as method arguments</div>
|
388
|
-
<div class="content">
|
389
|
-
<pre><tt>form_tag(:controller => "people", :action => "search", :method => "get")
|
390
|
-
# => <form action="/people/search?method=get" method="post"></tt></pre>
|
391
|
-
</div></div>
|
392
|
-
<div class="para"><p>Here we wanted to pass two hashes, but the Ruby interpreter sees only one hash, so Rails will construct a URL that we didn't want. The solution is to delimit the first hash (or both hashes) with curly brackets:</p></div>
|
393
|
-
<div class="listingblock">
|
394
|
-
<div class="title">Example: The correct way of passing multiple hashes as arguments</div>
|
395
|
-
<div class="content">
|
396
|
-
<pre><tt>form_tag({:controller => "people", :action => "search"}, :method => "get")
|
397
|
-
# => <form action="/people/search" method="get"></tt></pre>
|
398
|
-
</div></div>
|
399
|
-
<div class="para"><p>This is a common pitfall when using form helpers, since many of them accept multiple hashes. So in future, if a helper produces unexpected output, make sure that you have delimited the hash parameters properly.</p></div>
|
400
|
-
<div class="admonitionblock">
|
401
|
-
<table><tr>
|
402
|
-
<td class="icon">
|
403
|
-
<img src="./images/icons/warning.png" alt="Warning" />
|
404
|
-
</td>
|
405
|
-
<td class="content">Do not delimit the second hash without doing so with the first hash, otherwise your method invocation will result in an <tt>expecting tASSOC</tt> syntax error.</td>
|
406
|
-
</tr></table>
|
407
|
-
</div>
|
408
|
-
<h3 id="_checkboxes_radio_buttons_and_other_controls">1.3. Checkboxes, radio buttons and other controls</h3>
|
409
|
-
<div class="para"><p>Checkboxes are form controls that give the user a set of options they can enable or disable:</p></div>
|
410
|
-
<div class="listingblock">
|
411
|
-
<div class="content">
|
412
|
-
<pre><tt><%= check_box_tag(:pet_dog) %>
|
413
|
-
<%= label_tag(:pet_dog, "I own a dog") %>
|
414
|
-
<%= check_box_tag(:pet_cat) %>
|
415
|
-
<%= label_tag(:pet_cat, "I own a cat") %>
|
416
|
-
|
417
|
-
output:
|
418
|
-
|
419
|
-
<input id="pet_dog" name="pet_dog" type="checkbox" value="1" />
|
420
|
-
<label for="pet_dog">I own a dog</label>
|
421
|
-
<input id="pet_cat" name="pet_cat" type="checkbox" value="1" />
|
422
|
-
<label for="pet_cat">I own a cat</label></tt></pre>
|
423
|
-
</div></div>
|
424
|
-
<div class="para"><p>Radio buttons, while similar to checkboxes, are controls that specify a set of options in which they are mutually exclusive (user can only pick one):</p></div>
|
425
|
-
<div class="listingblock">
|
426
|
-
<div class="content">
|
427
|
-
<pre><tt><%= radio_button_tag(:age, "child") %>
|
428
|
-
<%= label_tag(:age_child, "I am younger than 21") %>
|
429
|
-
<%= radio_button_tag(:age, "adult") %>
|
430
|
-
<%= label_tag(:age_adult, "I'm over 21") %>
|
431
|
-
|
432
|
-
output:
|
433
|
-
|
434
|
-
<input id="age_child" name="age" type="radio" value="child" />
|
435
|
-
<label for="age_child">I am younger than 21</label>
|
436
|
-
<input id="age_adult" name="age" type="radio" value="adult" />
|
437
|
-
<label for="age_adult">I'm over 21</label></tt></pre>
|
438
|
-
</div></div>
|
439
|
-
<div class="admonitionblock">
|
440
|
-
<table><tr>
|
441
|
-
<td class="icon">
|
442
|
-
<img src="./images/icons/important.png" alt="Important" />
|
443
|
-
</td>
|
444
|
-
<td class="content">Always use labels for each checkbox and radio button. They associate text with a specific option and provide a larger clickable region.</td>
|
445
|
-
</tr></table>
|
446
|
-
</div>
|
447
|
-
<div class="para"><p>Other form controls we might mention are the text area, password input and hidden input:</p></div>
|
448
|
-
<div class="listingblock">
|
449
|
-
<div class="content">
|
450
|
-
<pre><tt><%= text_area_tag(:message, "Hi, nice site", :size => "24x6") %>
|
451
|
-
<%= password_field_tag(:password) %>
|
452
|
-
<%= hidden_field_tag(:parent_id, "5") %>
|
453
|
-
|
454
|
-
output:
|
455
|
-
|
456
|
-
<textarea id="message" name="message" cols="24" rows="6">Hi, nice site</textarea>
|
457
|
-
<input id="password" name="password" type="password" />
|
458
|
-
<input id="parent_id" name="parent_id" type="hidden" value="5" /></tt></pre>
|
459
|
-
</div></div>
|
460
|
-
<div class="para"><p>Hidden inputs are not shown to the user, but they hold data same as any textual input. Values inside them can be changed with JavaScript.</p></div>
|
461
|
-
<div class="admonitionblock">
|
462
|
-
<table><tr>
|
463
|
-
<td class="icon">
|
464
|
-
<img src="./images/icons/tip.png" alt="Tip" />
|
465
|
-
</td>
|
466
|
-
<td class="content">If you're using password input fields (for any purpose), you might want to prevent their values showing up in application logs by activating <tt>filter_parameter_logging(:password)</tt> in your ApplicationController.</td>
|
467
|
-
</tr></table>
|
468
|
-
</div>
|
469
|
-
<h3 id="_how_do_forms_with_put_or_delete_methods_work">1.4. How do forms with PUT or DELETE methods work?</h3>
|
470
|
-
<div class="para"><p>Rails framework encourages RESTful design of your applications, which means you'll be making a lot of "PUT" and "DELETE" requests (besides "GET" and "POST"). Still, most browsers <em>don't support</em> methods other than "GET" and "POST" when it comes to submitting forms. How does this work, then?</p></div>
|
471
|
-
<div class="para"><p>Rails works around this issue by emulating other methods over POST with a hidden input named <tt>"_method"</tt> that is set to reflect the wanted method:</p></div>
|
472
|
-
<div class="listingblock">
|
473
|
-
<div class="content">
|
474
|
-
<pre><tt>form_tag(search_path, :method => "put")
|
475
|
-
|
476
|
-
output:
|
477
|
-
|
478
|
-
<form action="/search" method="post">
|
479
|
-
<div style="margin:0;padding:0">
|
480
|
-
<input name="_method" type="hidden" value="put" />
|
481
|
-
<input name="authenticity_token" type="hidden" value="f755bb0ed134b76c432144748a6d4b7a7ddf2b71" />
|
482
|
-
</div>
|
483
|
-
...</tt></pre>
|
484
|
-
</div></div>
|
485
|
-
<div class="para"><p>When parsing POSTed data, Rails will take into account the special <tt>"_method"</tt> parameter and act as if the HTTP method was the one specified inside it ("PUT" in this example).</p></div>
|
486
|
-
</div>
|
487
|
-
<h2 id="_forms_that_deal_with_model_attributes">2. Forms that deal with model attributes</h2>
|
488
|
-
<div class="sectionbody">
|
489
|
-
<div class="para"><p>When we're dealing with an actual model, we will use a different set of form helpers and have Rails take care of some details in the background. In the following examples we will handle an Article model. First, let us have the controller create one:</p></div>
|
490
|
-
<div class="listingblock">
|
491
|
-
<div class="title">Example: articles_controller.rb</div>
|
492
|
-
<div class="content">
|
493
|
-
<pre><tt>def new
|
494
|
-
@article = Article.new
|
495
|
-
end</tt></pre>
|
496
|
-
</div></div>
|
497
|
-
<div class="para"><p>Now we switch to the view. The first thing to remember is that we should use <tt>form_for</tt> helper instead of <tt>form_tag</tt>, and that we should pass the model name and object as arguments:</p></div>
|
498
|
-
<div class="listingblock">
|
499
|
-
<div class="title">Example: articles/new.html.erb</div>
|
500
|
-
<div class="content">
|
501
|
-
<pre><tt><% form_for :article, @article, :url => { :action => "create" } do |f| %>
|
502
|
-
<%= f.text_field :title %>
|
503
|
-
<%= f.text_area :body, :size => "60x12" %>
|
504
|
-
<%= submit_tag "Create" %>
|
505
|
-
<% end %></tt></pre>
|
506
|
-
</div></div>
|
507
|
-
<div class="para"><p>There are a few things to note here:</p></div>
|
508
|
-
<div class="olist"><ol>
|
509
|
-
<li>
|
510
|
-
<p>
|
511
|
-
<tt>:article</tt> is the name of the model and <tt>@article</tt> is our record.
|
512
|
-
</p>
|
513
|
-
</li>
|
514
|
-
<li>
|
515
|
-
<p>
|
516
|
-
The URL for the action attribute is passed as a parameter named <tt>:url</tt>.
|
517
|
-
</p>
|
518
|
-
</li>
|
519
|
-
<li>
|
520
|
-
<p>
|
521
|
-
The <tt>form_for</tt> method yields <strong>a form builder</strong> object (the <tt>f</tt> variable).
|
522
|
-
</p>
|
523
|
-
</li>
|
524
|
-
<li>
|
525
|
-
<p>
|
526
|
-
Methods to create form controls are called <strong>on</strong> the form builder object <tt>f</tt> and <strong>without</strong> the <tt>"_tag"</tt> suffix (so <tt>text_field_tag</tt> becomes <tt>f.text_field</tt>).
|
527
|
-
</p>
|
528
|
-
</li>
|
529
|
-
</ol></div>
|
530
|
-
<div class="para"><p>The resulting HTML is:</p></div>
|
531
|
-
<div class="listingblock">
|
532
|
-
<div class="content">
|
533
|
-
<pre><tt><form action="/articles/create" method="post">
|
534
|
-
<input id="article_title" name="article[title]" size="30" type="text" />
|
535
|
-
<textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea>
|
536
|
-
<input name="commit" type="submit" value="Create" />
|
537
|
-
</form></tt></pre>
|
538
|
-
</div></div>
|
539
|
-
<div class="para"><p>A nice thing about <tt>f.text_field</tt> and other helper methods is that they will pre-fill the form control with the value read from the corresponding attribute in the model. For example, if we created the article instance by supplying an initial value for the title in the controller:</p></div>
|
540
|
-
<div class="listingblock">
|
541
|
-
<div class="content">
|
542
|
-
<pre><tt>@article = Article.new(:title => "Rails makes forms easy")</tt></pre>
|
543
|
-
</div></div>
|
544
|
-
<div class="para"><p>… the corresponding input will be rendered with a value:</p></div>
|
545
|
-
<div class="listingblock">
|
546
|
-
<div class="content">
|
547
|
-
<pre><tt><input id="post_title" name="post[title]" size="30" type="text" value="Rails makes forms easy" /></tt></pre>
|
548
|
-
</div></div>
|
549
|
-
<h3 id="_relying_on_record_identification">2.1. Relying on record identification</h3>
|
550
|
-
<div class="para"><p>In the previous chapter we handled the Article model. This model is directly available to users of our application and, following the best practices for developing with Rails, we should declare it <strong>a resource</strong>.</p></div>
|
551
|
-
<div class="para"><p>When dealing with RESTful resources, our calls to <tt>form_for</tt> can get significantly easier if we rely on <strong>record identification</strong>. In short, we can just pass the model instance and have Rails figure out model name and the rest:</p></div>
|
552
|
-
<div class="listingblock">
|
553
|
-
<div class="content">
|
554
|
-
<pre><tt>## Creating a new article
|
555
|
-
# long-style:
|
556
|
-
form_for(:article, @article, :url => articles_path)
|
557
|
-
# same thing, short-style (record identification gets used):
|
558
|
-
form_for(@article)
|
559
|
-
|
560
|
-
## Editing an existing article
|
561
|
-
# long-style:
|
562
|
-
form_for(:article, @article, :url => article_path(@article), :method => "put")
|
563
|
-
# short-style:
|
564
|
-
form_for(@article)</tt></pre>
|
565
|
-
</div></div>
|
566
|
-
<div class="para"><p>Notice how the short-style <tt>form_for</tt> invocation is conveniently the same, regardless of the record being new or existing. Record identification is smart enough to figure out if the record is new by asking <tt>record.new_record?</tt>.</p></div>
|
567
|
-
<div class="admonitionblock">
|
568
|
-
<table><tr>
|
569
|
-
<td class="icon">
|
570
|
-
<img src="./images/icons/warning.png" alt="Warning" />
|
571
|
-
</td>
|
572
|
-
<td class="content">When you're using STI (single-table inheritance) with your models, you can't rely on record identification on a subclass if only their parent class is declared a resource. You will have to specify the model name, <tt>:url</tt> and <tt>:method</tt> explicitly.</td>
|
573
|
-
</tr></table>
|
574
|
-
</div>
|
575
|
-
</div>
|
576
|
-
<h2 id="_making_select_boxes_with_ease">3. Making select boxes with ease</h2>
|
577
|
-
<div class="sectionbody">
|
578
|
-
<div class="para"><p>Select boxes in HTML require a significant amount of markup (one <tt>OPTION</tt> element for each option to choose from), therefore it makes the most sense for them to be dynamically generated from data stored in arrays or hashes.</p></div>
|
579
|
-
<div class="para"><p>Here is what our wanted markup might look like:</p></div>
|
580
|
-
<div class="listingblock">
|
581
|
-
<div class="content">
|
582
|
-
<pre><tt><select name="city_id" id="city_id">
|
583
|
-
<option value="1">Lisabon</option>
|
584
|
-
<option value="2">Madrid</option>
|
585
|
-
...
|
586
|
-
<option value="12">Berlin</option>
|
587
|
-
</select></tt></pre>
|
588
|
-
</div></div>
|
589
|
-
<div class="para"><p>Here we have a list of cities where their names are presented to the user, but internally we want to handle just their IDs so we keep them in value attributes. Let's see how Rails can help out here.</p></div>
|
590
|
-
<h3 id="_the_select_tag_and_options">3.1. The select tag and options</h3>
|
591
|
-
<div class="para"><p>The most generic helper is <tt>select_tag</tt>, which — as the name implies — simply generates the <tt>SELECT</tt> tag that encapsulates the options:</p></div>
|
592
|
-
<div class="listingblock">
|
593
|
-
<div class="content">
|
594
|
-
<pre><tt><%= select_tag(:city_id, '<option value="1">Lisabon</option>...') %></tt></pre>
|
595
|
-
</div></div>
|
596
|
-
<div class="para"><p>This is a start, but it doesn't dynamically create our option tags. We had to pass them in as a string.</p></div>
|
597
|
-
<div class="para"><p>We can generate option tags with the <tt>options_for_select</tt> helper:</p></div>
|
598
|
-
<div class="listingblock">
|
599
|
-
<div class="content">
|
600
|
-
<pre><tt><%= options_for_select([['Lisabon', 1], ['Madrid', 2], ...]) %>
|
601
|
-
|
602
|
-
output:
|
603
|
-
|
604
|
-
<option value="1">Lisabon</option>
|
605
|
-
<option value="2">Madrid</option>
|
606
|
-
...</tt></pre>
|
607
|
-
</div></div>
|
608
|
-
<div class="para"><p>For input data we used a nested array where each element has two elements: visible value (name) and internal value (ID).</p></div>
|
609
|
-
<div class="para"><p>Now you can combine <tt>select_tag</tt> and <tt>options_for_select</tt> to achieve the desired, complete markup:</p></div>
|
610
|
-
<div class="listingblock">
|
611
|
-
<div class="content">
|
612
|
-
<pre><tt><%= select_tag(:city_id, options_for_select(...)) %></tt></pre>
|
613
|
-
</div></div>
|
614
|
-
<div class="para"><p>Sometimes, depending on our application's needs, we also wish a specific option to be pre-selected. The <tt>options_for_select</tt> helper supports this with an optional second argument:</p></div>
|
615
|
-
<div class="listingblock">
|
616
|
-
<div class="content">
|
617
|
-
<pre><tt><%= options_for_select(cities_array, 2) %>
|
618
|
-
|
619
|
-
output:
|
620
|
-
|
621
|
-
<option value="1">Lisabon</option>
|
622
|
-
<option value="2" selected="selected">Madrid</option>
|
623
|
-
...</tt></pre>
|
624
|
-
</div></div>
|
625
|
-
<div class="para"><p>So whenever Rails sees that the internal value of an option being generated matches this value, it will add the <tt>selected</tt> attribute to that option.</p></div>
|
626
|
-
<h3 id="_select_boxes_for_dealing_with_models">3.2. Select boxes for dealing with models</h3>
|
627
|
-
<div class="para"><p>Until now we've covered how to make generic select boxes, but in most cases our form controls will be tied to a specific database model. So, to continue from our previous examples, let's assume that we have a "Person" model with a <tt>city_id</tt> attribute.</p></div>
|
628
|
-
<div class="listingblock">
|
629
|
-
<div class="content">
|
630
|
-
<pre><tt>...</tt></pre>
|
631
|
-
</div></div>
|
632
|
-
<div class="para"><p>…</p></div>
|
633
|
-
</div>
|
634
|
-
|
635
|
-
</div>
|
636
|
-
</div>
|
637
|
-
</body>
|
638
|
-
</html>
|