railties 3.2.22.5 → 4.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +186 -78
- data/README.rdoc +4 -2
- data/lib/rails.rb +11 -44
- data/lib/rails/all.rb +0 -1
- data/lib/rails/app_rails_loader.rb +37 -0
- data/lib/rails/application.rb +176 -124
- data/lib/rails/application/bootstrap.rb +29 -23
- data/lib/rails/application/configuration.rb +54 -48
- data/lib/rails/application/finisher.rb +8 -12
- data/lib/rails/application/routes_reloader.rb +2 -2
- data/lib/rails/backtrace_cleaner.rb +1 -16
- data/lib/rails/cli.rb +2 -2
- data/lib/rails/code_statistics.rb +30 -45
- data/lib/rails/code_statistics_calculator.rb +79 -0
- data/lib/rails/commands.rb +44 -37
- data/lib/rails/commands/application.rb +14 -9
- data/lib/rails/commands/console.rb +77 -33
- data/lib/rails/commands/dbconsole.rb +115 -64
- data/lib/rails/commands/destroy.rb +2 -3
- data/lib/rails/commands/generate.rb +2 -3
- data/lib/rails/commands/plugin_new.rb +0 -2
- data/lib/rails/commands/runner.rb +4 -5
- data/lib/rails/commands/server.rb +28 -9
- data/lib/rails/commands/update.rb +1 -1
- data/lib/rails/configuration.rb +35 -8
- data/lib/rails/console/app.rb +0 -4
- data/lib/rails/deprecation.rb +19 -0
- data/lib/rails/engine.rb +111 -92
- data/lib/rails/engine/commands.rb +1 -3
- data/lib/rails/engine/configuration.rb +26 -19
- data/lib/rails/engine/railties.rb +14 -18
- data/lib/rails/generators.rb +30 -50
- data/lib/rails/generators/actions.rb +38 -103
- data/lib/rails/generators/active_model.rb +14 -6
- data/lib/rails/generators/app_base.rb +70 -83
- data/lib/rails/generators/base.rb +23 -35
- data/lib/rails/generators/css/assets/assets_generator.rb +3 -3
- data/lib/rails/generators/css/scaffold/scaffold_generator.rb +3 -3
- data/lib/rails/generators/erb.rb +2 -2
- data/lib/rails/generators/erb/controller/controller_generator.rb +4 -4
- data/lib/rails/generators/erb/mailer/mailer_generator.rb +3 -3
- data/lib/rails/generators/erb/scaffold/scaffold_generator.rb +4 -4
- data/lib/rails/generators/erb/scaffold/templates/index.html.erb +18 -14
- data/lib/rails/generators/erb/scaffold/templates/show.html.erb +1 -1
- data/lib/rails/generators/generated_attribute.rb +51 -16
- data/lib/rails/generators/js/assets/assets_generator.rb +3 -3
- data/lib/rails/generators/migration.rb +1 -5
- data/lib/rails/generators/named_base.rb +20 -25
- data/lib/rails/generators/rails/app/app_generator.rb +40 -48
- data/lib/rails/generators/rails/app/templates/Gemfile +8 -7
- data/lib/rails/generators/rails/app/templates/README.rdoc +28 -0
- data/lib/rails/generators/rails/app/templates/Rakefile +0 -1
- data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +2 -1
- data/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt +5 -0
- data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +2 -2
- data/lib/rails/generators/rails/app/templates/bin/bundle +2 -0
- data/lib/rails/generators/rails/app/templates/bin/rails +3 -0
- data/lib/rails/generators/rails/app/templates/bin/rake +3 -0
- data/lib/rails/generators/rails/app/templates/config/application.rb +5 -43
- data/lib/rails/generators/rails/app/templates/config/boot.rb +0 -2
- data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +2 -2
- data/lib/rails/generators/rails/app/templates/config/databases/mysql.yml +1 -4
- data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +8 -3
- data/lib/rails/generators/rails/app/templates/config/databases/sqlserver.yml +57 -0
- data/lib/rails/generators/rails/app/templates/config/environment.rb +2 -2
- data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +9 -19
- data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +40 -29
- data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +12 -15
- data/lib/rails/generators/rails/app/templates/config/initializers/filter_parameter_logging.rb +4 -0
- data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb +6 -5
- data/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt +6 -1
- data/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +1 -6
- data/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +6 -6
- data/lib/rails/generators/rails/app/templates/config/locales/en.yml +20 -2
- data/lib/rails/generators/rails/app/templates/config/routes.rb +15 -24
- data/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +2 -2
- data/lib/rails/generators/rails/app/templates/gitignore +3 -2
- data/lib/rails/generators/rails/app/templates/public/404.html +2 -1
- data/lib/rails/generators/rails/app/templates/public/422.html +1 -1
- data/lib/rails/generators/rails/app/templates/public/500.html +2 -1
- data/lib/rails/generators/rails/app/templates/public/robots.txt +1 -1
- data/lib/rails/generators/rails/app/templates/test/test_helper.rb +3 -1
- data/lib/rails/generators/rails/assets/assets_generator.rb +5 -5
- data/lib/rails/generators/rails/controller/USAGE +7 -7
- data/lib/rails/generators/rails/controller/controller_generator.rb +3 -3
- data/lib/rails/generators/rails/controller/templates/controller.rb +1 -1
- data/lib/rails/generators/rails/generator/generator_generator.rb +4 -4
- data/lib/rails/generators/rails/helper/USAGE +1 -1
- data/lib/rails/generators/rails/helper/helper_generator.rb +2 -2
- data/lib/rails/generators/rails/integration_test/integration_test_generator.rb +2 -2
- data/lib/rails/generators/rails/migration/USAGE +12 -6
- data/lib/rails/generators/rails/migration/migration_generator.rb +3 -3
- data/lib/rails/generators/rails/model/USAGE +53 -4
- data/lib/rails/generators/rails/model/model_generator.rb +3 -3
- data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +81 -39
- data/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec +2 -2
- data/lib/rails/generators/rails/plugin_new/templates/Gemfile +16 -1
- data/lib/rails/generators/rails/plugin_new/templates/Rakefile +3 -9
- data/lib/rails/generators/rails/plugin_new/templates/app/controllers/%name%/application_controller.rb.tt +0 -1
- data/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt +1 -1
- data/lib/rails/generators/rails/plugin_new/templates/{script → bin}/rails.tt +0 -0
- data/lib/rails/generators/rails/plugin_new/templates/gitignore +3 -0
- data/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb +1 -1
- data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +0 -1
- data/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb +0 -1
- data/lib/rails/generators/rails/resource/resource_generator.rb +5 -5
- data/lib/rails/generators/rails/resource_route/resource_route_generator.rb +2 -2
- data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +11 -7
- data/lib/rails/generators/rails/scaffold_controller/USAGE +4 -4
- data/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +9 -7
- data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +27 -48
- data/lib/rails/generators/rails/task/task_generator.rb +2 -2
- data/lib/rails/generators/rails/task/templates/task.rb +1 -1
- data/lib/rails/generators/resource_helpers.rb +3 -5
- data/lib/rails/generators/test_case.rb +22 -28
- data/lib/rails/generators/test_unit.rb +3 -3
- data/lib/rails/generators/test_unit/controller/controller_generator.rb +6 -6
- data/lib/rails/generators/test_unit/helper/helper_generator.rb +5 -5
- data/lib/rails/generators/test_unit/integration/integration_generator.rb +4 -4
- data/lib/rails/generators/test_unit/mailer/mailer_generator.rb +6 -6
- data/lib/rails/generators/test_unit/model/model_generator.rb +7 -7
- data/lib/rails/generators/test_unit/model/templates/fixtures.yml +8 -2
- data/lib/rails/generators/test_unit/plugin/plugin_generator.rb +4 -4
- data/lib/rails/generators/test_unit/plugin/templates/test_helper.rb +1 -2
- data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +10 -19
- data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +5 -5
- data/lib/rails/info.rb +3 -3
- data/lib/rails/info_controller.rb +26 -8
- data/lib/rails/initializable.rb +2 -2
- data/lib/rails/paths.rb +59 -54
- data/lib/rails/rack/debugger.rb +2 -2
- data/lib/rails/rack/logger.rb +33 -8
- data/lib/rails/railtie.rb +24 -24
- data/lib/rails/railtie/configurable.rb +1 -1
- data/lib/rails/railtie/configuration.rb +15 -5
- data/lib/rails/ruby_version_check.rb +3 -13
- data/lib/rails/rubyprof_ext.rb +1 -1
- data/lib/rails/source_annotation_extractor.rb +40 -17
- data/lib/rails/tasks/annotations.rake +1 -1
- data/lib/rails/tasks/documentation.rake +10 -57
- data/lib/rails/tasks/engine.rake +1 -1
- data/lib/rails/tasks/framework.rake +9 -9
- data/lib/rails/tasks/log.rake +18 -4
- data/lib/rails/tasks/middleware.rake +1 -1
- data/lib/rails/tasks/misc.rake +2 -9
- data/lib/rails/tasks/routes.rake +4 -6
- data/lib/rails/tasks/statistics.rake +8 -2
- data/lib/rails/tasks/tmp.rake +12 -4
- data/lib/rails/templates/layouts/application.html.erb +36 -0
- data/lib/rails/templates/rails/info/properties.html.erb +1 -0
- data/lib/rails/templates/rails/info/routes.html.erb +9 -0
- data/lib/rails/{generators/rails/app/templates/public/index.html → templates/rails/welcome/index.html.erb} +18 -14
- data/lib/rails/test_help.rb +6 -29
- data/lib/rails/test_unit/railtie.rb +2 -3
- data/lib/rails/test_unit/testing.rake +45 -33
- data/lib/rails/version.rb +4 -4
- data/lib/rails/welcome_controller.rb +7 -0
- metadata +86 -378
- data/guides/assets/images/belongs_to.png +0 -0
- data/guides/assets/images/book_icon.gif +0 -0
- data/guides/assets/images/bullet.gif +0 -0
- data/guides/assets/images/challenge.png +0 -0
- data/guides/assets/images/chapters_icon.gif +0 -0
- data/guides/assets/images/check_bullet.gif +0 -0
- data/guides/assets/images/credits_pic_blank.gif +0 -0
- data/guides/assets/images/csrf.png +0 -0
- data/guides/assets/images/customized_error_messages.png +0 -0
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/error_messages.png +0 -0
- data/guides/assets/images/feature_tile.gif +0 -0
- data/guides/assets/images/footer_tile.gif +0 -0
- data/guides/assets/images/fxn.png +0 -0
- data/guides/assets/images/grey_bullet.gif +0 -0
- data/guides/assets/images/habtm.png +0 -0
- data/guides/assets/images/has_many.png +0 -0
- data/guides/assets/images/has_many_through.png +0 -0
- data/guides/assets/images/has_one.png +0 -0
- data/guides/assets/images/has_one_through.png +0 -0
- data/guides/assets/images/header_backdrop.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/i18n/demo_html_safe.png +0 -0
- data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/assets/images/i18n/demo_translated_en.png +0 -0
- data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
- data/guides/assets/images/i18n/demo_untranslated.png +0 -0
- data/guides/assets/images/icons/README +0 -5
- data/guides/assets/images/icons/callouts/1.png +0 -0
- data/guides/assets/images/icons/callouts/10.png +0 -0
- data/guides/assets/images/icons/callouts/11.png +0 -0
- data/guides/assets/images/icons/callouts/12.png +0 -0
- data/guides/assets/images/icons/callouts/13.png +0 -0
- data/guides/assets/images/icons/callouts/14.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/callouts/2.png +0 -0
- data/guides/assets/images/icons/callouts/3.png +0 -0
- data/guides/assets/images/icons/callouts/4.png +0 -0
- data/guides/assets/images/icons/callouts/5.png +0 -0
- data/guides/assets/images/icons/callouts/6.png +0 -0
- data/guides/assets/images/icons/callouts/7.png +0 -0
- data/guides/assets/images/icons/callouts/8.png +0 -0
- data/guides/assets/images/icons/callouts/9.png +0 -0
- data/guides/assets/images/icons/caution.png +0 -0
- data/guides/assets/images/icons/example.png +0 -0
- data/guides/assets/images/icons/home.png +0 -0
- data/guides/assets/images/icons/important.png +0 -0
- data/guides/assets/images/icons/next.png +0 -0
- data/guides/assets/images/icons/note.png +0 -0
- data/guides/assets/images/icons/prev.png +0 -0
- data/guides/assets/images/icons/tip.png +0 -0
- data/guides/assets/images/icons/up.png +0 -0
- data/guides/assets/images/icons/warning.png +0 -0
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- data/guides/assets/images/nav_arrow.gif +0 -0
- data/guides/assets/images/polymorphic.png +0 -0
- data/guides/assets/images/posts_index.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/images/rails_guides_logo.gif +0 -0
- data/guides/assets/images/rails_logo_remix.gif +0 -0
- data/guides/assets/images/rails_welcome.png +0 -0
- data/guides/assets/images/session_fixation.png +0 -0
- data/guides/assets/images/tab_grey.gif +0 -0
- data/guides/assets/images/tab_info.gif +0 -0
- data/guides/assets/images/tab_note.gif +0 -0
- data/guides/assets/images/tab_red.gif +0 -0
- data/guides/assets/images/tab_yellow.gif +0 -0
- data/guides/assets/images/tab_yellow.png +0 -0
- data/guides/assets/images/validation_error_messages.png +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +0 -7
- data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
- data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
- data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
- data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
- data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
- data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
- data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
- data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
- data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
- data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
- data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
- data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
- data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
- data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
- data/guides/assets/javascripts/syntaxhighlighter/shCore.js +0 -17
- data/guides/assets/stylesheets/fixes.css +0 -16
- data/guides/assets/stylesheets/kindle.css +0 -11
- data/guides/assets/stylesheets/main.css +0 -453
- data/guides/assets/stylesheets/print.css +0 -52
- data/guides/assets/stylesheets/reset.css +0 -43
- data/guides/assets/stylesheets/style.css +0 -13
- data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +0 -226
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
- data/guides/code/getting_started/Gemfile +0 -38
- data/guides/code/getting_started/README.rdoc +0 -261
- data/guides/code/getting_started/Rakefile +0 -7
- data/guides/code/getting_started/app/assets/images/rails.png +0 -0
- data/guides/code/getting_started/app/assets/javascripts/application.js +0 -15
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/home.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/home.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss +0 -56
- data/guides/code/getting_started/app/controllers/application_controller.rb +0 -3
- data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -16
- data/guides/code/getting_started/app/controllers/home_controller.rb +0 -5
- data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -84
- data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/home_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -5
- data/guides/code/getting_started/app/models/comment.rb +0 -3
- data/guides/code/getting_started/app/models/post.rb +0 -11
- data/guides/code/getting_started/app/models/tag.rb +0 -3
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
- data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
- data/guides/code/getting_started/app/views/home/index.html.erb +0 -2
- data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
- data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -32
- data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -6
- data/guides/code/getting_started/app/views/posts/index.html.erb +0 -27
- data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/show.html.erb +0 -31
- data/guides/code/getting_started/app/views/tags/_form.html.erb +0 -12
- data/guides/code/getting_started/config.ru +0 -4
- data/guides/code/getting_started/config/application.rb +0 -59
- data/guides/code/getting_started/config/boot.rb +0 -6
- data/guides/code/getting_started/config/database.yml +0 -25
- data/guides/code/getting_started/config/environment.rb +0 -5
- data/guides/code/getting_started/config/environments/development.rb +0 -37
- data/guides/code/getting_started/config/environments/production.rb +0 -67
- data/guides/code/getting_started/config/environments/test.rb +0 -37
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
- data/guides/code/getting_started/config/initializers/inflections.rb +0 -15
- data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
- data/guides/code/getting_started/config/initializers/secret_token.rb +0 -7
- data/guides/code/getting_started/config/initializers/session_store.rb +0 -8
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
- data/guides/code/getting_started/config/locales/en.yml +0 -5
- data/guides/code/getting_started/config/routes.rb +0 -64
- data/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +0 -11
- data/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb +0 -12
- data/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb +0 -11
- data/guides/code/getting_started/db/schema.rb +0 -43
- data/guides/code/getting_started/db/seeds.rb +0 -7
- data/guides/code/getting_started/doc/README_FOR_APP +0 -2
- data/guides/code/getting_started/public/404.html +0 -26
- data/guides/code/getting_started/public/422.html +0 -26
- data/guides/code/getting_started/public/500.html +0 -25
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +0 -5
- data/guides/code/getting_started/script/rails +0 -6
- data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
- data/guides/code/getting_started/test/fixtures/posts.yml +0 -11
- data/guides/code/getting_started/test/fixtures/tags.yml +0 -9
- data/guides/code/getting_started/test/functional/comments_controller_test.rb +0 -7
- data/guides/code/getting_started/test/functional/home_controller_test.rb +0 -9
- data/guides/code/getting_started/test/functional/posts_controller_test.rb +0 -49
- data/guides/code/getting_started/test/performance/browsing_test.rb +0 -12
- data/guides/code/getting_started/test/test_helper.rb +0 -13
- data/guides/code/getting_started/test/unit/comment_test.rb +0 -7
- data/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb +0 -4
- data/guides/code/getting_started/test/unit/helpers/home_helper_test.rb +0 -4
- data/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb +0 -4
- data/guides/code/getting_started/test/unit/post_test.rb +0 -7
- data/guides/code/getting_started/test/unit/tag_test.rb +0 -7
- data/guides/rails_guides.rb +0 -50
- data/guides/rails_guides/generator.rb +0 -310
- data/guides/rails_guides/helpers.rb +0 -45
- data/guides/rails_guides/indexer.rb +0 -69
- data/guides/rails_guides/levenshtein.rb +0 -31
- data/guides/rails_guides/textile_extensions.rb +0 -63
- data/guides/source/2_2_release_notes.textile +0 -422
- data/guides/source/2_3_release_notes.textile +0 -610
- data/guides/source/3_0_release_notes.textile +0 -595
- data/guides/source/3_1_release_notes.textile +0 -553
- data/guides/source/3_2_release_notes.textile +0 -540
- data/guides/source/_license.html.erb +0 -2
- data/guides/source/_welcome.html.erb +0 -19
- data/guides/source/action_controller_overview.textile +0 -820
- data/guides/source/action_mailer_basics.textile +0 -516
- data/guides/source/action_view_overview.textile +0 -1497
- data/guides/source/active_model_basics.textile +0 -205
- data/guides/source/active_record_basics.textile +0 -218
- data/guides/source/active_record_querying.textile +0 -1433
- data/guides/source/active_record_validations_callbacks.textile +0 -1287
- data/guides/source/active_resource_basics.textile +0 -120
- data/guides/source/active_support_core_extensions.textile +0 -3715
- data/guides/source/api_documentation_guidelines.textile +0 -185
- data/guides/source/asset_pipeline.textile +0 -707
- data/guides/source/association_basics.textile +0 -1959
- data/guides/source/caching_with_rails.textile +0 -437
- data/guides/source/command_line.textile +0 -574
- data/guides/source/configuring.textile +0 -643
- data/guides/source/contributing_to_ruby_on_rails.textile +0 -448
- data/guides/source/credits.html.erb +0 -72
- data/guides/source/debugging_rails_applications.textile +0 -714
- data/guides/source/documents.yaml +0 -157
- data/guides/source/engines.textile +0 -618
- data/guides/source/form_helpers.textile +0 -798
- data/guides/source/generators.textile +0 -621
- data/guides/source/getting_started.textile +0 -1931
- data/guides/source/i18n.textile +0 -931
- data/guides/source/index.html.erb +0 -30
- data/guides/source/initialization.textile +0 -1116
- data/guides/source/kindle/KINDLE.md +0 -26
- data/guides/source/kindle/copyright.html.erb +0 -1
- data/guides/source/kindle/layout.html.erb +0 -27
- data/guides/source/kindle/rails_guides.opf.erb +0 -52
- data/guides/source/kindle/toc.html.erb +0 -24
- data/guides/source/kindle/toc.ncx.erb +0 -64
- data/guides/source/kindle/welcome.html.erb +0 -5
- data/guides/source/layout.html.erb +0 -124
- data/guides/source/layouts_and_rendering.textile +0 -1237
- data/guides/source/migrations.textile +0 -900
- data/guides/source/nested_model_forms.textile +0 -222
- data/guides/source/performance_testing.textile +0 -597
- data/guides/source/plugins.textile +0 -464
- data/guides/source/rails_application_templates.textile +0 -240
- data/guides/source/rails_on_rack.textile +0 -236
- data/guides/source/routing.textile +0 -885
- data/guides/source/ruby_on_rails_guides_guidelines.textile +0 -79
- data/guides/source/security.textile +0 -1004
- data/guides/source/testing.textile +0 -947
- data/guides/w3c_validator.rb +0 -91
- data/lib/rails/application/railties.rb +0 -13
- data/lib/rails/application/route_inspector.rb +0 -84
- data/lib/rails/commands/benchmarker.rb +0 -34
- data/lib/rails/commands/plugin.rb +0 -544
- data/lib/rails/commands/profiler.rb +0 -33
- data/lib/rails/generators/rails/app/templates/README +0 -261
- data/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb +0 -3
- data/lib/rails/generators/rails/app/templates/app/mailers/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/app/models/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/doc/README_FOR_APP +0 -2
- data/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/script/rails +0 -5
- data/lib/rails/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/test/functional/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/test/integration/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb +0 -12
- data/lib/rails/generators/rails/app/templates/test/unit/.empty_directory +0 -0
- data/lib/rails/generators/rails/generator/templates/templates/.empty_directory +0 -0
- data/lib/rails/generators/rails/observer/USAGE +0 -12
- data/lib/rails/generators/rails/observer/observer_generator.rb +0 -7
- data/lib/rails/generators/rails/performance_test/USAGE +0 -10
- data/lib/rails/generators/rails/performance_test/performance_test_generator.rb +0 -7
- data/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory +0 -0
- data/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory +0 -0
- data/lib/rails/generators/rails/session_migration/USAGE +0 -8
- data/lib/rails/generators/rails/session_migration/session_migration_generator.rb +0 -8
- data/lib/rails/generators/test_unit/observer/observer_generator.rb +0 -13
- data/lib/rails/generators/test_unit/observer/templates/unit_test.rb +0 -9
- data/lib/rails/generators/test_unit/performance/performance_generator.rb +0 -13
- data/lib/rails/generators/test_unit/performance/templates/performance_test.rb +0 -12
- data/lib/rails/performance_test_help.rb +0 -3
- data/lib/rails/plugin.rb +0 -92
- data/lib/rails/script_rails_loader.rb +0 -29
@@ -1,885 +0,0 @@
|
|
1
|
-
h2. Rails Routing from the Outside In
|
2
|
-
|
3
|
-
This guide covers the user-facing features of Rails routing. By referring to this guide, you will be able to:
|
4
|
-
|
5
|
-
* Understand the code in +routes.rb+
|
6
|
-
* Construct your own routes, using either the preferred resourceful style or the <tt>match</tt> method
|
7
|
-
* Identify what parameters to expect an action to receive
|
8
|
-
* Automatically create paths and URLs using route helpers
|
9
|
-
* Use advanced techniques such as constraints and Rack endpoints
|
10
|
-
|
11
|
-
endprologue.
|
12
|
-
|
13
|
-
h3. The Purpose of the Rails Router
|
14
|
-
|
15
|
-
The Rails router recognizes URLs and dispatches them to a controller's action. It can also generate paths and URLs, avoiding the need to hardcode strings in your views.
|
16
|
-
|
17
|
-
h4. Connecting URLs to Code
|
18
|
-
|
19
|
-
When your Rails application receives an incoming request
|
20
|
-
|
21
|
-
<plain>
|
22
|
-
GET /patients/17
|
23
|
-
</plain>
|
24
|
-
|
25
|
-
it asks the router to match it to a controller action. If the first matching route is
|
26
|
-
|
27
|
-
<ruby>
|
28
|
-
match "/patients/:id" => "patients#show"
|
29
|
-
</ruby>
|
30
|
-
|
31
|
-
the request is dispatched to the +patients+ controller's +show+ action with <tt>{ :id => "17" }</tt> in +params+.
|
32
|
-
|
33
|
-
h4. Generating Paths and URLs from Code
|
34
|
-
|
35
|
-
You can also generate paths and URLs. If your application contains this code:
|
36
|
-
|
37
|
-
<ruby>
|
38
|
-
@patient = Patient.find(17)
|
39
|
-
</ruby>
|
40
|
-
|
41
|
-
<erb>
|
42
|
-
<%= link_to "Patient Record", patient_path(@patient) %>
|
43
|
-
</erb>
|
44
|
-
|
45
|
-
The router will generate the path +/patients/17+. This reduces the brittleness of your view and makes your code easier to understand. Note that the id does not need to be specified in the route helper.
|
46
|
-
|
47
|
-
h3. Resource Routing: the Rails Default
|
48
|
-
|
49
|
-
Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your +index+, +show+, +new+, +edit+, +create+, +update+ and +destroy+ actions, a resourceful route declares them in a single line of code.
|
50
|
-
|
51
|
-
h4. Resources on the Web
|
52
|
-
|
53
|
-
Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as +GET+, +POST+, +PUT+ and +DELETE+. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller.
|
54
|
-
|
55
|
-
When your Rails application receives an incoming request for
|
56
|
-
|
57
|
-
<plain>
|
58
|
-
DELETE /photos/17
|
59
|
-
</plain>
|
60
|
-
|
61
|
-
it asks the router to map it to a controller action. If the first matching route is
|
62
|
-
|
63
|
-
<ruby>
|
64
|
-
resources :photos
|
65
|
-
</ruby>
|
66
|
-
|
67
|
-
Rails would dispatch that request to the +destroy+ method on the +photos+ controller with <tt>{ :id => "17" }</tt> in +params+.
|
68
|
-
|
69
|
-
h4. CRUD, Verbs, and Actions
|
70
|
-
|
71
|
-
In Rails, a resourceful route provides a mapping between HTTP verbs and URLs to controller actions. By convention, each action also maps to particular CRUD operations in a database. A single entry in the routing file, such as
|
72
|
-
|
73
|
-
<ruby>
|
74
|
-
resources :photos
|
75
|
-
</ruby>
|
76
|
-
|
77
|
-
creates seven different routes in your application, all mapping to the +Photos+ controller:
|
78
|
-
|
79
|
-
|_. HTTP Verb |_.Path |_.action |_.used for |
|
80
|
-
|GET |/photos |index |display a list of all photos |
|
81
|
-
|GET |/photos/new |new |return an HTML form for creating a new photo |
|
82
|
-
|POST |/photos |create |create a new photo |
|
83
|
-
|GET |/photos/:id |show |display a specific photo |
|
84
|
-
|GET |/photos/:id/edit |edit |return an HTML form for editing a photo |
|
85
|
-
|PUT |/photos/:id |update |update a specific photo |
|
86
|
-
|DELETE |/photos/:id |destroy |delete a specific photo |
|
87
|
-
|
88
|
-
|
89
|
-
NOTE: Rails routes are matched in the order they are specified, so if you have a +resources :photos+ above a +get 'photos/poll'+ the +show+ action's route for the +resources+ line will be matched before the +get+ line. To fix this, move the +get+ line *above* the +resources+ line so that it is matched first.
|
90
|
-
|
91
|
-
h4. Paths and URLs
|
92
|
-
|
93
|
-
Creating a resourceful route will also expose a number of helpers to the controllers in your application. In the case of +resources :photos+:
|
94
|
-
|
95
|
-
* +photos_path+ returns +/photos+
|
96
|
-
* +new_photo_path+ returns +/photos/new+
|
97
|
-
* +edit_photo_path(:id)+ returns +/photos/:id/edit+ (for instance, +edit_photo_path(10)+ returns +/photos/10/edit+)
|
98
|
-
* +photo_path(:id)+ returns +/photos/:id+ (for instance, +photo_path(10)+ returns +/photos/10+)
|
99
|
-
|
100
|
-
Each of these helpers has a corresponding +_url+ helper (such as +photos_url+) which returns the same path prefixed with the current host, port and path prefix.
|
101
|
-
|
102
|
-
NOTE: Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions.
|
103
|
-
|
104
|
-
h4. Defining Multiple Resources at the Same Time
|
105
|
-
|
106
|
-
If you need to create routes for more than one resource, you can save a bit of typing by defining them all with a single call to +resources+:
|
107
|
-
|
108
|
-
<ruby>
|
109
|
-
resources :photos, :books, :videos
|
110
|
-
</ruby>
|
111
|
-
|
112
|
-
This works exactly the same as
|
113
|
-
|
114
|
-
<ruby>
|
115
|
-
resources :photos
|
116
|
-
resources :books
|
117
|
-
resources :videos
|
118
|
-
</ruby>
|
119
|
-
|
120
|
-
h4. Singular Resources
|
121
|
-
|
122
|
-
Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like +/profile+ to always show the profile of the currently logged in user. In this case, you can use a singular resource to map +/profile+ (rather than +/profile/:id+) to the +show+ action.
|
123
|
-
|
124
|
-
<ruby>
|
125
|
-
match "profile" => "users#show"
|
126
|
-
</ruby>
|
127
|
-
|
128
|
-
This resourceful route
|
129
|
-
|
130
|
-
<ruby>
|
131
|
-
resource :geocoder
|
132
|
-
</ruby>
|
133
|
-
|
134
|
-
creates six different routes in your application, all mapping to the +Geocoders+ controller:
|
135
|
-
|
136
|
-
|_.HTTP Verb |_.Path |_.action |_.used for |
|
137
|
-
|GET |/geocoder/new |new |return an HTML form for creating the geocoder |
|
138
|
-
|POST |/geocoder |create |create the new geocoder |
|
139
|
-
|GET |/geocoder |show |display the one and only geocoder resource |
|
140
|
-
|GET |/geocoder/edit |edit |return an HTML form for editing the geocoder |
|
141
|
-
|PUT |/geocoder |update |update the one and only geocoder resource |
|
142
|
-
|DELETE |/geocoder |destroy |delete the geocoder resource |
|
143
|
-
|
144
|
-
NOTE: Because you might want to use the same controller for a singular route (+/account+) and a plural route (+/accounts/45+), singular resources map to plural controllers.
|
145
|
-
|
146
|
-
A singular resourceful route generates these helpers:
|
147
|
-
|
148
|
-
* +new_geocoder_path+ returns +/geocoder/new+
|
149
|
-
* +edit_geocoder_path+ returns +/geocoder/edit+
|
150
|
-
* +geocoder_path+ returns +/geocoder+
|
151
|
-
|
152
|
-
As with plural resources, the same helpers ending in +_url+ will also include the host, port and path prefix.
|
153
|
-
|
154
|
-
h4. Controller Namespaces and Routing
|
155
|
-
|
156
|
-
You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an +Admin::+ namespace. You would place these controllers under the +app/controllers/admin+ directory, and you can group them together in your router:
|
157
|
-
|
158
|
-
<ruby>
|
159
|
-
namespace :admin do
|
160
|
-
resources :posts, :comments
|
161
|
-
end
|
162
|
-
</ruby>
|
163
|
-
|
164
|
-
This will create a number of routes for each of the +posts+ and +comments+ controller. For +Admin::PostsController+, Rails will create:
|
165
|
-
|
166
|
-
|_.HTTP Verb |_.Path |_.action |_.named helper |
|
167
|
-
|GET |/admin/posts |index | admin_posts_path |
|
168
|
-
|GET |/admin/posts/new |new | new_admin_post_path |
|
169
|
-
|POST |/admin/posts |create | admin_posts_path |
|
170
|
-
|GET |/admin/posts/:id |show | admin_post_path(:id) |
|
171
|
-
|GET |/admin/posts/:id/edit |edit | edit_admin_post_path(:id) |
|
172
|
-
|PUT |/admin/posts/:id |update | admin_post_path(:id) |
|
173
|
-
|DELETE |/admin/posts/:id |destroy | admin_post_path(:id) |
|
174
|
-
|
175
|
-
If you want to route +/posts+ (without the prefix +/admin+) to +Admin::PostsController+, you could use
|
176
|
-
|
177
|
-
<ruby>
|
178
|
-
scope :module => "admin" do
|
179
|
-
resources :posts, :comments
|
180
|
-
end
|
181
|
-
</ruby>
|
182
|
-
|
183
|
-
or, for a single case
|
184
|
-
|
185
|
-
<ruby>
|
186
|
-
resources :posts, :module => "admin"
|
187
|
-
</ruby>
|
188
|
-
|
189
|
-
If you want to route +/admin/posts+ to +PostsController+ (without the +Admin::+ module prefix), you could use
|
190
|
-
|
191
|
-
<ruby>
|
192
|
-
scope "/admin" do
|
193
|
-
resources :posts, :comments
|
194
|
-
end
|
195
|
-
</ruby>
|
196
|
-
|
197
|
-
or, for a single case
|
198
|
-
|
199
|
-
<ruby>
|
200
|
-
resources :posts, :path => "/admin/posts"
|
201
|
-
</ruby>
|
202
|
-
|
203
|
-
In each of these cases, the named routes remain the same as if you did not use +scope+. In the last case, the following paths map to +PostsController+:
|
204
|
-
|
205
|
-
|_.HTTP Verb |_.Path |_.action |_.named helper |
|
206
|
-
|GET |/admin/posts |index | posts_path |
|
207
|
-
|GET |/admin/posts/new |new | new_post_path |
|
208
|
-
|POST |/admin/posts |create | posts_path |
|
209
|
-
|GET |/admin/posts/:id |show | post_path(:id) |
|
210
|
-
|GET |/admin/posts/:id/edit|edit | edit_post_path(:id)|
|
211
|
-
|PUT |/admin/posts/:id |update | post_path(:id) |
|
212
|
-
|DELETE |/admin/posts/:id |destroy | post_path(:id) |
|
213
|
-
|
214
|
-
h4. Nested Resources
|
215
|
-
|
216
|
-
It's common to have resources that are logically children of other resources. For example, suppose your application includes these models:
|
217
|
-
|
218
|
-
<ruby>
|
219
|
-
class Magazine < ActiveRecord::Base
|
220
|
-
has_many :ads
|
221
|
-
end
|
222
|
-
|
223
|
-
class Ad < ActiveRecord::Base
|
224
|
-
belongs_to :magazine
|
225
|
-
end
|
226
|
-
</ruby>
|
227
|
-
|
228
|
-
Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration:
|
229
|
-
|
230
|
-
<ruby>
|
231
|
-
resources :magazines do
|
232
|
-
resources :ads
|
233
|
-
end
|
234
|
-
</ruby>
|
235
|
-
|
236
|
-
In addition to the routes for magazines, this declaration will also route ads to an +AdsController+. The ad URLs require a magazine:
|
237
|
-
|
238
|
-
|_.HTTP Verb |_.Path |_.action |_.used for |
|
239
|
-
|GET |/magazines/:magazine_id/ads |index |display a list of all ads for a specific magazine |
|
240
|
-
|GET |/magazines/:magazine_id/ads/new |new |return an HTML form for creating a new ad belonging to a specific magazine |
|
241
|
-
|POST |/magazines/:magazine_id/ads |create |create a new ad belonging to a specific magazine |
|
242
|
-
|GET |/magazines/:magazine_id/ads/:id |show |display a specific ad belonging to a specific magazine |
|
243
|
-
|GET |/magazines/:magazine_id/ads/:id/edit |edit |return an HTML form for editing an ad belonging to a specific magazine |
|
244
|
-
|PUT |/magazines/:magazine_id/ads/:id |update |update a specific ad belonging to a specific magazine |
|
245
|
-
|DELETE |/magazines/:magazine_id/ads/:id |destroy |delete a specific ad belonging to a specific magazine |
|
246
|
-
|
247
|
-
|
248
|
-
This will also create routing helpers such as +magazine_ads_url+ and +edit_magazine_ad_path+. These helpers take an instance of Magazine as the first parameter (+magazine_ads_url(@magazine)+).
|
249
|
-
|
250
|
-
h5. Limits to Nesting
|
251
|
-
|
252
|
-
You can nest resources within other nested resources if you like. For example:
|
253
|
-
|
254
|
-
<ruby>
|
255
|
-
resources :publishers do
|
256
|
-
resources :magazines do
|
257
|
-
resources :photos
|
258
|
-
end
|
259
|
-
end
|
260
|
-
</ruby>
|
261
|
-
|
262
|
-
Deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize paths such as
|
263
|
-
|
264
|
-
<pre>
|
265
|
-
/publishers/1/magazines/2/photos/3
|
266
|
-
</pre>
|
267
|
-
|
268
|
-
The corresponding route helper would be +publisher_magazine_photo_url+, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular "article":http://weblog.jamisbuck.org/2007/2/5/nesting-resources by Jamis Buck proposes a rule of thumb for good Rails design:
|
269
|
-
|
270
|
-
TIP: _Resources should never be nested more than 1 level deep._
|
271
|
-
|
272
|
-
h4. Creating Paths and URLs From Objects
|
273
|
-
|
274
|
-
In addition to using the routing helpers, Rails can also create paths and URLs from an array of parameters. For example, suppose you have this set of routes:
|
275
|
-
|
276
|
-
<ruby>
|
277
|
-
resources :magazines do
|
278
|
-
resources :ads
|
279
|
-
end
|
280
|
-
</ruby>
|
281
|
-
|
282
|
-
When using +magazine_ad_path+, you can pass in instances of +Magazine+ and +Ad+ instead of the numeric IDs.
|
283
|
-
|
284
|
-
<erb>
|
285
|
-
<%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %>
|
286
|
-
</erb>
|
287
|
-
|
288
|
-
You can also use +url_for+ with a set of objects, and Rails will automatically determine which route you want:
|
289
|
-
|
290
|
-
<erb>
|
291
|
-
<%= link_to "Ad details", url_for([@magazine, @ad]) %>
|
292
|
-
</erb>
|
293
|
-
|
294
|
-
In this case, Rails will see that +@magazine+ is a +Magazine+ and +@ad+ is an +Ad+ and will therefore use the +magazine_ad_path+ helper. In helpers like +link_to+, you can specify just the object in place of the full +url_for+ call:
|
295
|
-
|
296
|
-
<erb>
|
297
|
-
<%= link_to "Ad details", [@magazine, @ad] %>
|
298
|
-
</erb>
|
299
|
-
|
300
|
-
If you wanted to link to just a magazine, you could leave out the +Array+:
|
301
|
-
|
302
|
-
<erb>
|
303
|
-
<%= link_to "Magazine details", @magazine %>
|
304
|
-
</erb>
|
305
|
-
|
306
|
-
This allows you to treat instances of your models as URLs, and is a key advantage to using the resourceful style.
|
307
|
-
|
308
|
-
h4. Adding More RESTful Actions
|
309
|
-
|
310
|
-
You are not limited to the seven routes that RESTful routing creates by default. If you like, you may add additional routes that apply to the collection or individual members of the collection.
|
311
|
-
|
312
|
-
h5. Adding Member Routes
|
313
|
-
|
314
|
-
To add a member route, just add a +member+ block into the resource block:
|
315
|
-
|
316
|
-
<ruby>
|
317
|
-
resources :photos do
|
318
|
-
member do
|
319
|
-
get 'preview'
|
320
|
-
end
|
321
|
-
end
|
322
|
-
</ruby>
|
323
|
-
|
324
|
-
This will recognize +/photos/1/preview+ with GET, and route to the +preview+ action of +PhotosController+. It will also create the +preview_photo_url+ and +preview_photo_path+ helpers.
|
325
|
-
|
326
|
-
Within the block of member routes, each route name specifies the HTTP verb that it will recognize. You can use +get+, +put+, +post+, or +delete+ here. If you don't have multiple +member+ routes, you can also pass +:on+ to a route, eliminating the block:
|
327
|
-
|
328
|
-
<ruby>
|
329
|
-
resources :photos do
|
330
|
-
get 'preview', :on => :member
|
331
|
-
end
|
332
|
-
</ruby>
|
333
|
-
|
334
|
-
h5. Adding Collection Routes
|
335
|
-
|
336
|
-
To add a route to the collection:
|
337
|
-
|
338
|
-
<ruby>
|
339
|
-
resources :photos do
|
340
|
-
collection do
|
341
|
-
get 'search'
|
342
|
-
end
|
343
|
-
end
|
344
|
-
</ruby>
|
345
|
-
|
346
|
-
This will enable Rails to recognize paths such as +/photos/search+ with GET, and route to the +search+ action of +PhotosController+. It will also create the +search_photos_url+ and +search_photos_path+ route helpers.
|
347
|
-
|
348
|
-
Just as with member routes, you can pass +:on+ to a route:
|
349
|
-
|
350
|
-
<ruby>
|
351
|
-
resources :photos do
|
352
|
-
get 'search', :on => :collection
|
353
|
-
end
|
354
|
-
</ruby>
|
355
|
-
|
356
|
-
h5. A Note of Caution
|
357
|
-
|
358
|
-
If you find yourself adding many extra actions to a resourceful route, it's time to stop and ask yourself whether you're disguising the presence of another resource.
|
359
|
-
|
360
|
-
h3. Non-Resourceful Routes
|
361
|
-
|
362
|
-
In addition to resource routing, Rails has powerful support for routing arbitrary URLs to actions. Here, you don't get groups of routes automatically generated by resourceful routing. Instead, you set up each route within your application separately.
|
363
|
-
|
364
|
-
While you should usually use resourceful routing, there are still many places where the simpler routing is more appropriate. There's no need to try to shoehorn every last piece of your application into a resourceful framework if that's not a good fit.
|
365
|
-
|
366
|
-
In particular, simple routing makes it very easy to map legacy URLs to new Rails actions.
|
367
|
-
|
368
|
-
h4. Bound Parameters
|
369
|
-
|
370
|
-
When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming HTTP request. Two of these symbols are special: +:controller+ maps to the name of a controller in your application, and +:action+ maps to the name of an action within that controller. For example, consider one of the default Rails routes:
|
371
|
-
|
372
|
-
<ruby>
|
373
|
-
match ':controller(/:action(/:id))'
|
374
|
-
</ruby>
|
375
|
-
|
376
|
-
If an incoming request of +/photos/show/1+ is processed by this route (because it hasn't matched any previous route in the file), then the result will be to invoke the +show+ action of the +PhotosController+, and to make the final parameter +"1"+ available as +params[:id]+. This route will also route the incoming request of +/photos+ to +PhotosController#index+, since +:action+ and +:id+ are optional parameters, denoted by parentheses.
|
377
|
-
|
378
|
-
h4. Dynamic Segments
|
379
|
-
|
380
|
-
You can set up as many dynamic segments within a regular route as you like. Anything other than +:controller+ or +:action+ will be available to the action as part of +params+. If you set up this route:
|
381
|
-
|
382
|
-
<ruby>
|
383
|
-
match ':controller/:action/:id/:user_id'
|
384
|
-
</ruby>
|
385
|
-
|
386
|
-
An incoming path of +/photos/show/1/2+ will be dispatched to the +show+ action of the +PhotosController+. +params[:id]+ will be +"1"+, and +params[:user_id]+ will be +"2"+.
|
387
|
-
|
388
|
-
NOTE: You can't use +:namespace+ or +:module+ with a +:controller+ path segment. If you need to do this then use a constraint on :controller that matches the namespace you require. e.g:
|
389
|
-
|
390
|
-
<ruby>
|
391
|
-
match ':controller(/:action(/:id))', :controller => /admin\/[^\/]+/
|
392
|
-
</ruby>
|
393
|
-
|
394
|
-
TIP: By default dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment add a constraint which overrides this - for example +:id+ => /[^\/]+/ allows anything except a slash.
|
395
|
-
|
396
|
-
h4. Static Segments
|
397
|
-
|
398
|
-
You can specify static segments when creating a route:
|
399
|
-
|
400
|
-
<ruby>
|
401
|
-
match ':controller/:action/:id/with_user/:user_id'
|
402
|
-
</ruby>
|
403
|
-
|
404
|
-
This route would respond to paths such as +/photos/show/1/with_user/2+. In this case, +params+ would be <tt>{ :controller => "photos", :action => "show", :id => "1", :user_id => "2" }</tt>.
|
405
|
-
|
406
|
-
h4. The Query String
|
407
|
-
|
408
|
-
The +params+ will also include any parameters from the query string. For example, with this route:
|
409
|
-
|
410
|
-
<ruby>
|
411
|
-
match ':controller/:action/:id'
|
412
|
-
</ruby>
|
413
|
-
|
414
|
-
An incoming path of +/photos/show/1?user_id=2+ will be dispatched to the +show+ action of the +Photos+ controller. +params+ will be <tt>{ :controller => "photos", :action => "show", :id => "1", :user_id => "2" }</tt>.
|
415
|
-
|
416
|
-
h4. Defining Defaults
|
417
|
-
|
418
|
-
You do not need to explicitly use the +:controller+ and +:action+ symbols within a route. You can supply them as defaults:
|
419
|
-
|
420
|
-
<ruby>
|
421
|
-
match 'photos/:id' => 'photos#show'
|
422
|
-
</ruby>
|
423
|
-
|
424
|
-
With this route, Rails will match an incoming path of +/photos/12+ to the +show+ action of +PhotosController+.
|
425
|
-
|
426
|
-
You can also define other defaults in a route by supplying a hash for the +:defaults+ option. This even applies to parameters that you do not specify as dynamic segments. For example:
|
427
|
-
|
428
|
-
<ruby>
|
429
|
-
match 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
|
430
|
-
</ruby>
|
431
|
-
|
432
|
-
Rails would match +photos/12+ to the +show+ action of +PhotosController+, and set +params[:format]+ to +"jpg"+.
|
433
|
-
|
434
|
-
h4. Naming Routes
|
435
|
-
|
436
|
-
You can specify a name for any route using the +:as+ option.
|
437
|
-
|
438
|
-
<ruby>
|
439
|
-
match 'exit' => 'sessions#destroy', :as => :logout
|
440
|
-
</ruby>
|
441
|
-
|
442
|
-
This will create +logout_path+ and +logout_url+ as named helpers in your application. Calling +logout_path+ will return +/exit+
|
443
|
-
|
444
|
-
h4. HTTP Verb Constraints
|
445
|
-
|
446
|
-
You can use the +:via+ option to constrain the request to one or more HTTP methods:
|
447
|
-
|
448
|
-
<ruby>
|
449
|
-
match 'photos/show' => 'photos#show', :via => :get
|
450
|
-
</ruby>
|
451
|
-
|
452
|
-
There is a shorthand version of this as well:
|
453
|
-
|
454
|
-
<ruby>
|
455
|
-
get 'photos/show'
|
456
|
-
</ruby>
|
457
|
-
|
458
|
-
You can also permit more than one verb to a single route:
|
459
|
-
|
460
|
-
<ruby>
|
461
|
-
match 'photos/show' => 'photos#show', :via => [:get, :post]
|
462
|
-
</ruby>
|
463
|
-
|
464
|
-
h4. Segment Constraints
|
465
|
-
|
466
|
-
You can use the +:constraints+ option to enforce a format for a dynamic segment:
|
467
|
-
|
468
|
-
<ruby>
|
469
|
-
match 'photos/:id' => 'photos#show', :constraints => { :id => /[A-Z]\d{5}/ }
|
470
|
-
</ruby>
|
471
|
-
|
472
|
-
This route would match paths such as +/photos/A12345+. You can more succinctly express the same route this way:
|
473
|
-
|
474
|
-
<ruby>
|
475
|
-
match 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/
|
476
|
-
</ruby>
|
477
|
-
|
478
|
-
+:constraints+ takes regular expressions with the restriction that regexp anchors can't be used. For example, the following route will not work:
|
479
|
-
|
480
|
-
<ruby>
|
481
|
-
match '/:id' => 'posts#show', :constraints => {:id => /^\d/}
|
482
|
-
</ruby>
|
483
|
-
|
484
|
-
However, note that you don't need to use anchors because all routes are anchored at the start.
|
485
|
-
|
486
|
-
For example, the following routes would allow for +posts+ with +to_param+ values like +1-hello-world+ that always begin with a number and +users+ with +to_param+ values like +david+ that never begin with a number to share the root namespace:
|
487
|
-
|
488
|
-
<ruby>
|
489
|
-
match '/:id' => 'posts#show', :constraints => { :id => /\d.+/ }
|
490
|
-
match '/:username' => 'users#show'
|
491
|
-
</ruby>
|
492
|
-
|
493
|
-
h4. Request-Based Constraints
|
494
|
-
|
495
|
-
You can also constrain a route based on any method on the <a href="action_controller_overview.html#the-request-object">Request</a> object that returns a +String+.
|
496
|
-
|
497
|
-
You specify a request-based constraint the same way that you specify a segment constraint:
|
498
|
-
|
499
|
-
<ruby>
|
500
|
-
match "photos", :constraints => {:subdomain => "admin"}
|
501
|
-
</ruby>
|
502
|
-
|
503
|
-
You can also specify constraints in a block form:
|
504
|
-
|
505
|
-
<ruby>
|
506
|
-
namespace :admin do
|
507
|
-
constraints :subdomain => "admin" do
|
508
|
-
resources :photos
|
509
|
-
end
|
510
|
-
end
|
511
|
-
</ruby>
|
512
|
-
|
513
|
-
h4. Advanced Constraints
|
514
|
-
|
515
|
-
If you have a more advanced constraint, you can provide an object that responds to +matches?+ that Rails should use. Let's say you wanted to route all users on a blacklist to the +BlacklistController+. You could do:
|
516
|
-
|
517
|
-
<ruby>
|
518
|
-
class BlacklistConstraint
|
519
|
-
def initialize
|
520
|
-
@ips = Blacklist.retrieve_ips
|
521
|
-
end
|
522
|
-
|
523
|
-
def matches?(request)
|
524
|
-
@ips.include?(request.remote_ip)
|
525
|
-
end
|
526
|
-
end
|
527
|
-
|
528
|
-
TwitterClone::Application.routes.draw do
|
529
|
-
match "*path" => "blacklist#index",
|
530
|
-
:constraints => BlacklistConstraint.new
|
531
|
-
end
|
532
|
-
</ruby>
|
533
|
-
|
534
|
-
h4. Route Globbing
|
535
|
-
|
536
|
-
Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example
|
537
|
-
|
538
|
-
<ruby>
|
539
|
-
match 'photos/*other' => 'photos#unknown'
|
540
|
-
</ruby>
|
541
|
-
|
542
|
-
This route would match +photos/12+ or +/photos/long/path/to/12+, setting +params[:other]+ to +"12"+ or +"long/path/to/12"+.
|
543
|
-
|
544
|
-
Wildcard segments can occur anywhere in a route. For example,
|
545
|
-
|
546
|
-
<ruby>
|
547
|
-
match 'books/*section/:title' => 'books#show'
|
548
|
-
</ruby>
|
549
|
-
|
550
|
-
would match +books/some/section/last-words-a-memoir+ with +params[:section]+ equals +"some/section"+, and +params[:title]+ equals +"last-words-a-memoir"+.
|
551
|
-
|
552
|
-
Technically a route can have even more than one wildcard segment. The matcher assigns segments to parameters in an intuitive way. For example,
|
553
|
-
|
554
|
-
<ruby>
|
555
|
-
match '*a/foo/*b' => 'test#index'
|
556
|
-
</ruby>
|
557
|
-
|
558
|
-
would match +zoo/woo/foo/bar/baz+ with +params[:a]+ equals +"zoo/woo"+, and +params[:b]+ equals +"bar/baz"+.
|
559
|
-
|
560
|
-
NOTE: Starting from Rails 3.1, wildcard routes will always match the optional format segment by default. For example if you have this route:
|
561
|
-
|
562
|
-
<ruby>
|
563
|
-
match '*pages' => 'pages#show'
|
564
|
-
</ruby>
|
565
|
-
|
566
|
-
NOTE: By requesting +"/foo/bar.json"+, your +params[:pages]+ will be equals to +"foo/bar"+ with the request format of JSON. If you want the old 3.0.x behavior back, you could supply +:format => false+ like this:
|
567
|
-
|
568
|
-
<ruby>
|
569
|
-
match '*pages' => 'pages#show', :format => false
|
570
|
-
</ruby>
|
571
|
-
|
572
|
-
NOTE: If you want to make the format segment mandatory, so it cannot be omitted, you can supply +:format => true+ like this:
|
573
|
-
|
574
|
-
<ruby>
|
575
|
-
match '*pages' => 'pages#show', :format => true
|
576
|
-
</ruby>
|
577
|
-
|
578
|
-
h4. Redirection
|
579
|
-
|
580
|
-
You can redirect any path to another path using the +redirect+ helper in your router:
|
581
|
-
|
582
|
-
<ruby>
|
583
|
-
match "/stories" => redirect("/posts")
|
584
|
-
</ruby>
|
585
|
-
|
586
|
-
You can also reuse dynamic segments from the match in the path to redirect to:
|
587
|
-
|
588
|
-
<ruby>
|
589
|
-
match "/stories/:name" => redirect("/posts/%{name}")
|
590
|
-
</ruby>
|
591
|
-
|
592
|
-
You can also provide a block to redirect, which receives the params and (optionally) the request object:
|
593
|
-
|
594
|
-
<ruby>
|
595
|
-
match "/stories/:name" => redirect {|params| "/posts/#{params[:name].pluralize}" }
|
596
|
-
match "/stories" => redirect {|p, req| "/posts/#{req.subdomain}" }
|
597
|
-
</ruby>
|
598
|
-
|
599
|
-
Please note that this redirection is a 301 "Moved Permanently" redirect. Keep in mind that some web browsers or proxy servers will cache this type of redirect, making the old page inaccessible.
|
600
|
-
|
601
|
-
In all of these cases, if you don't provide the leading host (+http://www.example.com+), Rails will take those details from the current request.
|
602
|
-
|
603
|
-
h4. Routing to Rack Applications
|
604
|
-
|
605
|
-
Instead of a String, like +"posts#index"+, which corresponds to the +index+ action in the +PostsController+, you can specify any <a href="rails_on_rack.html">Rack application</a> as the endpoint for a matcher.
|
606
|
-
|
607
|
-
<ruby>
|
608
|
-
match "/application.js" => Sprockets
|
609
|
-
</ruby>
|
610
|
-
|
611
|
-
As long as +Sprockets+ responds to +call+ and returns a <tt>[status, headers, body]</tt>, the router won't know the difference between the Rack application and an action.
|
612
|
-
|
613
|
-
NOTE: For the curious, +"posts#index"+ actually expands out to +PostsController.action(:index)+, which returns a valid Rack application.
|
614
|
-
|
615
|
-
h4. Using +root+
|
616
|
-
|
617
|
-
You can specify what Rails should route +"/"+ to with the +root+ method:
|
618
|
-
|
619
|
-
<ruby>
|
620
|
-
root :to => 'pages#main'
|
621
|
-
</ruby>
|
622
|
-
|
623
|
-
You should put the +root+ route at the top of the file, because it is the most popular route and should be matched first. You also need to delete the +public/index.html+ file for the root route to take effect.
|
624
|
-
|
625
|
-
h3. Customizing Resourceful Routes
|
626
|
-
|
627
|
-
While the default routes and helpers generated by +resources :posts+ will usually serve you well, you may want to customize them in some way. Rails allows you to customize virtually any generic part of the resourceful helpers.
|
628
|
-
|
629
|
-
h4. Specifying a Controller to Use
|
630
|
-
|
631
|
-
The +:controller+ option lets you explicitly specify a controller to use for the resource. For example:
|
632
|
-
|
633
|
-
<ruby>
|
634
|
-
resources :photos, :controller => "images"
|
635
|
-
</ruby>
|
636
|
-
|
637
|
-
will recognize incoming paths beginning with +/photos+ but route to the +Images+ controller:
|
638
|
-
|
639
|
-
|_.HTTP Verb |_.Path |_.action |_.named helper |
|
640
|
-
|GET |/photos |index | photos_path |
|
641
|
-
|GET |/photos/new |new | new_photo_path |
|
642
|
-
|POST |/photos |create | photos_path |
|
643
|
-
|GET |/photos/:id |show | photo_path(:id) |
|
644
|
-
|GET |/photos/:id/edit |edit | edit_photo_path(:id) |
|
645
|
-
|PUT |/photos/:id |update | photo_path(:id) |
|
646
|
-
|DELETE |/photos/:id |destroy | photo_path(:id) |
|
647
|
-
|
648
|
-
NOTE: Use +photos_path+, +new_photo_path+, etc. to generate paths for this resource.
|
649
|
-
|
650
|
-
h4. Specifying Constraints
|
651
|
-
|
652
|
-
You can use the +:constraints+ option to specify a required format on the implicit +id+. For example:
|
653
|
-
|
654
|
-
<ruby>
|
655
|
-
resources :photos, :constraints => {:id => /[A-Z][A-Z][0-9]+/}
|
656
|
-
</ruby>
|
657
|
-
|
658
|
-
This declaration constrains the +:id+ parameter to match the supplied regular expression. So, in this case, the router would no longer match +/photos/1+ to this route. Instead, +/photos/RR27+ would match.
|
659
|
-
|
660
|
-
You can specify a single constraint to apply to a number of routes by using the block form:
|
661
|
-
|
662
|
-
<ruby>
|
663
|
-
constraints(:id => /[A-Z][A-Z][0-9]+/) do
|
664
|
-
resources :photos
|
665
|
-
resources :accounts
|
666
|
-
end
|
667
|
-
</ruby>
|
668
|
-
|
669
|
-
NOTE: Of course, you can use the more advanced constraints available in non-resourceful routes in this context.
|
670
|
-
|
671
|
-
TIP: By default the +:id+ parameter doesn't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within an +:id+ add a constraint which overrides this - for example +:id+ => /[^\/]+/ allows anything except a slash.
|
672
|
-
|
673
|
-
h4. Overriding the Named Helpers
|
674
|
-
|
675
|
-
The +:as+ option lets you override the normal naming for the named route helpers. For example:
|
676
|
-
|
677
|
-
<ruby>
|
678
|
-
resources :photos, :as => "images"
|
679
|
-
</ruby>
|
680
|
-
|
681
|
-
will recognize incoming paths beginning with +/photos+ and route the requests to +PhotosController+, but use the value of the :as option to name the helpers.
|
682
|
-
|
683
|
-
|_.HTTP verb|_.Path |_.action |_.named helper |
|
684
|
-
|GET |/photos |index | images_path |
|
685
|
-
|GET |/photos/new |new | new_image_path |
|
686
|
-
|POST |/photos |create | images_path |
|
687
|
-
|GET |/photos/:id |show | image_path(:id) |
|
688
|
-
|GET |/photos/:id/edit |edit | edit_image_path(:id) |
|
689
|
-
|PUT |/photos/:id |update | image_path(:id) |
|
690
|
-
|DELETE |/photos/:id |destroy | image_path(:id) |
|
691
|
-
|
692
|
-
h4. Overriding the +new+ and +edit+ Segments
|
693
|
-
|
694
|
-
The +:path_names+ option lets you override the automatically-generated "new" and "edit" segments in paths:
|
695
|
-
|
696
|
-
<ruby>
|
697
|
-
resources :photos, :path_names => { :new => 'make', :edit => 'change' }
|
698
|
-
</ruby>
|
699
|
-
|
700
|
-
This would cause the routing to recognize paths such as
|
701
|
-
|
702
|
-
<plain>
|
703
|
-
/photos/make
|
704
|
-
/photos/1/change
|
705
|
-
</plain>
|
706
|
-
|
707
|
-
NOTE: The actual action names aren't changed by this option. The two paths shown would still route to the +new+ and +edit+ actions.
|
708
|
-
|
709
|
-
TIP: If you find yourself wanting to change this option uniformly for all of your routes, you can use a scope.
|
710
|
-
|
711
|
-
<ruby>
|
712
|
-
scope :path_names => { :new => "make" } do
|
713
|
-
# rest of your routes
|
714
|
-
end
|
715
|
-
</ruby>
|
716
|
-
|
717
|
-
h4. Prefixing the Named Route Helpers
|
718
|
-
|
719
|
-
You can use the +:as+ option to prefix the named route helpers that Rails generates for a route. Use this option to prevent name collisions between routes using a path scope.
|
720
|
-
|
721
|
-
<ruby>
|
722
|
-
scope "admin" do
|
723
|
-
resources :photos, :as => "admin_photos"
|
724
|
-
end
|
725
|
-
|
726
|
-
resources :photos
|
727
|
-
</ruby>
|
728
|
-
|
729
|
-
This will provide route helpers such as +admin_photos_path+, +new_admin_photo_path+ etc.
|
730
|
-
|
731
|
-
To prefix a group of route helpers, use +:as+ with +scope+:
|
732
|
-
|
733
|
-
<ruby>
|
734
|
-
scope "admin", :as => "admin" do
|
735
|
-
resources :photos, :accounts
|
736
|
-
end
|
737
|
-
|
738
|
-
resources :photos, :accounts
|
739
|
-
</ruby>
|
740
|
-
|
741
|
-
This will generate routes such as +admin_photos_path+ and +admin_accounts_path+ which map to +/admin/photos+ and +/admin/accounts+ respectively.
|
742
|
-
|
743
|
-
NOTE: The +namespace+ scope will automatically add +:as+ as well as +:module+ and +:path+ prefixes.
|
744
|
-
|
745
|
-
You can prefix routes with a named parameter also:
|
746
|
-
|
747
|
-
<ruby>
|
748
|
-
scope ":username" do
|
749
|
-
resources :posts
|
750
|
-
end
|
751
|
-
</ruby>
|
752
|
-
|
753
|
-
This will provide you with URLs such as +/bob/posts/1+ and will allow you to reference the +username+ part of the path as +params[:username]+ in controllers, helpers and views.
|
754
|
-
|
755
|
-
h4. Restricting the Routes Created
|
756
|
-
|
757
|
-
By default, Rails creates routes for the seven default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application. You can use the +:only+ and +:except+ options to fine-tune this behavior. The +:only+ option tells Rails to create only the specified routes:
|
758
|
-
|
759
|
-
<ruby>
|
760
|
-
resources :photos, :only => [:index, :show]
|
761
|
-
</ruby>
|
762
|
-
|
763
|
-
Now, a +GET+ request to +/photos+ would succeed, but a +POST+ request to +/photos+ (which would ordinarily be routed to the +create+ action) will fail.
|
764
|
-
|
765
|
-
The +:except+ option specifies a route or list of routes that Rails should _not_ create:
|
766
|
-
|
767
|
-
<ruby>
|
768
|
-
resources :photos, :except => :destroy
|
769
|
-
</ruby>
|
770
|
-
|
771
|
-
In this case, Rails will create all of the normal routes except the route for +destroy+ (a +DELETE+ request to +/photos/:id+).
|
772
|
-
|
773
|
-
TIP: If your application has many RESTful routes, using +:only+ and +:except+ to generate only the routes that you actually need can cut down on memory use and speed up the routing process.
|
774
|
-
|
775
|
-
h4. Translated Paths
|
776
|
-
|
777
|
-
Using +scope+, we can alter path names generated by resources:
|
778
|
-
|
779
|
-
<ruby>
|
780
|
-
scope(:path_names => { :new => "neu", :edit => "bearbeiten" }) do
|
781
|
-
resources :categories, :path => "kategorien"
|
782
|
-
end
|
783
|
-
</ruby>
|
784
|
-
|
785
|
-
Rails now creates routes to the +CategoriesController+.
|
786
|
-
|
787
|
-
|_.HTTP verb|_.Path |_.action |_.named helper |
|
788
|
-
|GET |/kategorien |index | categories_path |
|
789
|
-
|GET |/kategorien/neu |new | new_category_path |
|
790
|
-
|POST |/kategorien |create | categories_path |
|
791
|
-
|GET |/kategorien/:id |show | category_path(:id) |
|
792
|
-
|GET |/kategorien/:id/bearbeiten |edit | edit_category_path(:id) |
|
793
|
-
|PUT |/kategorien/:id |update | category_path(:id) |
|
794
|
-
|DELETE |/kategorien/:id |destroy | category_path(:id) |
|
795
|
-
|
796
|
-
h4. Overriding the Singular Form
|
797
|
-
|
798
|
-
If you want to define the singular form of a resource, you should add additional rules to the +Inflector+.
|
799
|
-
|
800
|
-
<ruby>
|
801
|
-
ActiveSupport::Inflector.inflections do |inflect|
|
802
|
-
inflect.irregular 'tooth', 'teeth'
|
803
|
-
end
|
804
|
-
</ruby>
|
805
|
-
|
806
|
-
h4(#nested-names). Using +:as+ in Nested Resources
|
807
|
-
|
808
|
-
The +:as+ option overrides the automatically-generated name for the resource in nested route helpers. For example,
|
809
|
-
|
810
|
-
<ruby>
|
811
|
-
resources :magazines do
|
812
|
-
resources :ads, :as => 'periodical_ads'
|
813
|
-
end
|
814
|
-
</ruby>
|
815
|
-
|
816
|
-
This will create routing helpers such as +magazine_periodical_ads_url+ and +edit_magazine_periodical_ad_path+.
|
817
|
-
|
818
|
-
h3. Inspecting and Testing Routes
|
819
|
-
|
820
|
-
Rails offers facilities for inspecting and testing your routes.
|
821
|
-
|
822
|
-
h4. Seeing Existing Routes with +rake+
|
823
|
-
|
824
|
-
If you want a complete list of all of the available routes in your application, run +rake routes+ command. This will print all of your routes, in the same order that they appear in +routes.rb+. For each route, you'll see:
|
825
|
-
|
826
|
-
* The route name (if any)
|
827
|
-
* The HTTP verb used (if the route doesn't respond to all verbs)
|
828
|
-
* The URL pattern to match
|
829
|
-
* The routing parameters for the route
|
830
|
-
|
831
|
-
For example, here's a small section of the +rake routes+ output for a RESTful route:
|
832
|
-
|
833
|
-
<pre>
|
834
|
-
users GET /users(.:format) users#index
|
835
|
-
POST /users(.:format) users#create
|
836
|
-
new_user GET /users/new(.:format) users#new
|
837
|
-
edit_user GET /users/:id/edit(.:format) users#edit
|
838
|
-
</pre>
|
839
|
-
|
840
|
-
You may restrict the listing to the routes that map to a particular controller setting the +CONTROLLER+ environment variable:
|
841
|
-
|
842
|
-
<shell>
|
843
|
-
$ CONTROLLER=users rake routes
|
844
|
-
</shell>
|
845
|
-
|
846
|
-
TIP: You'll find that the output from +rake routes+ is much more readable if you widen your terminal window until the output lines don't wrap.
|
847
|
-
|
848
|
-
h4. Testing Routes
|
849
|
-
|
850
|
-
Routes should be included in your testing strategy (just like the rest of your application). Rails offers three "built-in assertions":http://api.rubyonrails.org/classes/ActionDispatch/Assertions/RoutingAssertions.html designed to make testing routes simpler:
|
851
|
-
|
852
|
-
* +assert_generates+
|
853
|
-
* +assert_recognizes+
|
854
|
-
* +assert_routing+
|
855
|
-
|
856
|
-
h5. The +assert_generates+ Assertion
|
857
|
-
|
858
|
-
+assert_generates+ asserts that a particular set of options generate a particular path and can be used with default routes or custom routes.
|
859
|
-
|
860
|
-
<ruby>
|
861
|
-
assert_generates "/photos/1", { :controller => "photos", :action => "show", :id => "1" }
|
862
|
-
assert_generates "/about", :controller => "pages", :action => "about"
|
863
|
-
</ruby>
|
864
|
-
|
865
|
-
h5. The +assert_recognizes+ Assertion
|
866
|
-
|
867
|
-
+assert_recognizes+ is the inverse of +assert_generates+. It asserts that a given path is recognized and routes it to a particular spot in your application.
|
868
|
-
|
869
|
-
<ruby>
|
870
|
-
assert_recognizes({ :controller => "photos", :action => "show", :id => "1" }, "/photos/1")
|
871
|
-
</ruby>
|
872
|
-
|
873
|
-
You can supply a +:method+ argument to specify the HTTP verb:
|
874
|
-
|
875
|
-
<ruby>
|
876
|
-
assert_recognizes({ :controller => "photos", :action => "create" }, { :path => "photos", :method => :post })
|
877
|
-
</ruby>
|
878
|
-
|
879
|
-
h5. The +assert_routing+ Assertion
|
880
|
-
|
881
|
-
The +assert_routing+ assertion checks the route both ways: it tests that the path generates the options, and that the options generate the path. Thus, it combines the functions of +assert_generates+ and +assert_recognizes+.
|
882
|
-
|
883
|
-
<ruby>
|
884
|
-
assert_routing({ :path => "photos", :method => :post }, { :controller => "photos", :action => "create" })
|
885
|
-
</ruby>
|