railties 3.1.1 → 3.1.2.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +2382 -0
- data/guides/output/2_2_release_notes.html +565 -0
- data/guides/output/2_3_release_notes.html +713 -0
- data/guides/output/3_0_release_notes.html +652 -0
- data/guides/output/3_1_release_notes.html +670 -0
- data/guides/output/action_controller_overview.html +925 -0
- data/guides/output/action_mailer_basics.html +658 -0
- data/guides/output/action_view_overview.html +1471 -0
- data/guides/output/active_model_basics.html +349 -0
- data/guides/output/active_record_basics.html +364 -0
- data/guides/output/active_record_querying.html +1272 -0
- data/guides/output/active_record_validations_callbacks.html +1292 -0
- data/guides/output/active_resource_basics.html +252 -0
- data/guides/output/active_support_core_extensions.html +3374 -0
- data/guides/output/ajax_on_rails.html +412 -0
- data/guides/output/api_documentation_guidelines.html +317 -0
- data/guides/output/asset_pipeline.html +691 -0
- data/guides/output/association_basics.html +1742 -0
- data/guides/output/caching_with_rails.html +533 -0
- data/guides/output/command_line.html +662 -0
- data/guides/output/configuring.html +811 -0
- data/guides/output/contribute.html +216 -0
- data/guides/output/contributing_to_ruby_on_rails.html +465 -0
- data/guides/output/credits.html +210 -0
- data/guides/output/debugging_rails_applications.html +791 -0
- data/guides/output/engines.html +673 -0
- data/guides/output/form_helpers.html +850 -0
- data/guides/output/generators.html +725 -0
- data/guides/output/getting_started.html +1980 -0
- data/guides/output/i18n.html +1054 -0
- data/guides/output/images/belongs_to.png +0 -0
- data/guides/output/images/book_icon.gif +0 -0
- data/guides/output/images/bullet.gif +0 -0
- data/guides/output/images/challenge.png +0 -0
- data/guides/output/images/chapters_icon.gif +0 -0
- data/guides/output/images/check_bullet.gif +0 -0
- data/guides/output/images/credits_pic_blank.gif +0 -0
- data/guides/output/images/csrf.png +0 -0
- data/guides/output/images/customized_error_messages.png +0 -0
- data/guides/output/images/edge_badge.png +0 -0
- data/guides/output/images/error_messages.png +0 -0
- data/guides/output/images/feature_tile.gif +0 -0
- data/guides/output/images/footer_tile.gif +0 -0
- data/guides/output/images/fxn.png +0 -0
- data/guides/output/images/grey_bullet.gif +0 -0
- data/guides/output/images/habtm.png +0 -0
- data/guides/output/images/has_many.png +0 -0
- data/guides/output/images/has_many_through.png +0 -0
- data/guides/output/images/has_one.png +0 -0
- data/guides/output/images/has_one_through.png +0 -0
- data/guides/output/images/header_backdrop.png +0 -0
- data/guides/output/images/header_tile.gif +0 -0
- data/guides/output/images/i18n/demo_html_safe.png +0 -0
- data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translated_en.png +0 -0
- data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/output/images/i18n/demo_translation_missing.png +0 -0
- data/guides/output/images/i18n/demo_untranslated.png +0 -0
- data/guides/output/images/icons/README +5 -0
- data/guides/output/images/icons/callouts/1.png +0 -0
- data/guides/output/images/icons/callouts/10.png +0 -0
- data/guides/output/images/icons/callouts/11.png +0 -0
- data/guides/output/images/icons/callouts/12.png +0 -0
- data/guides/output/images/icons/callouts/13.png +0 -0
- data/guides/output/images/icons/callouts/14.png +0 -0
- data/guides/output/images/icons/callouts/15.png +0 -0
- data/guides/output/images/icons/callouts/2.png +0 -0
- data/guides/output/images/icons/callouts/3.png +0 -0
- data/guides/output/images/icons/callouts/4.png +0 -0
- data/guides/output/images/icons/callouts/5.png +0 -0
- data/guides/output/images/icons/callouts/6.png +0 -0
- data/guides/output/images/icons/callouts/7.png +0 -0
- data/guides/output/images/icons/callouts/8.png +0 -0
- data/guides/output/images/icons/callouts/9.png +0 -0
- data/guides/output/images/icons/caution.png +0 -0
- data/guides/output/images/icons/example.png +0 -0
- data/guides/output/images/icons/home.png +0 -0
- data/guides/output/images/icons/important.png +0 -0
- data/guides/output/images/icons/next.png +0 -0
- data/guides/output/images/icons/note.png +0 -0
- data/guides/output/images/icons/prev.png +0 -0
- data/guides/output/images/icons/tip.png +0 -0
- data/guides/output/images/icons/up.png +0 -0
- data/guides/output/images/icons/warning.png +0 -0
- data/guides/output/images/jaimeiniesta.jpg +0 -0
- data/guides/output/images/nav_arrow.gif +0 -0
- data/guides/output/images/polymorphic.png +0 -0
- data/guides/output/images/posts_index.png +0 -0
- data/guides/output/images/radar.png +0 -0
- data/guides/output/images/rails_guides_logo.gif +0 -0
- data/guides/output/images/rails_logo_remix.gif +0 -0
- data/guides/output/images/rails_welcome.png +0 -0
- data/guides/output/images/session_fixation.png +0 -0
- data/guides/output/images/tab_grey.gif +0 -0
- data/guides/output/images/tab_info.gif +0 -0
- data/guides/output/images/tab_note.gif +0 -0
- data/guides/output/images/tab_red.gif +0 -0
- data/guides/output/images/tab_yellow.gif +0 -0
- data/guides/output/images/tab_yellow.png +0 -0
- data/guides/output/images/validation_error_messages.png +0 -0
- data/guides/output/images/vijaydev.jpg +0 -0
- data/guides/output/index.html +300 -0
- data/guides/output/initialization.html +1087 -0
- data/guides/output/javascripts/guides.js +7 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
- data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
- data/guides/output/javascripts/syntaxhighlighter/shCore.js +17 -0
- data/guides/output/layout.html +312 -0
- data/guides/output/layouts_and_rendering.html +1257 -0
- data/guides/output/migrations.html +751 -0
- data/guides/output/nested_model_forms.html +350 -0
- data/guides/output/performance_testing.html +858 -0
- data/guides/output/plugins.html +590 -0
- data/guides/output/rails_application_templates.html +368 -0
- data/guides/output/rails_on_rack.html +408 -0
- data/guides/output/routing.html +1246 -0
- data/guides/output/ruby_on_rails_guides_guidelines.html +218 -0
- data/guides/output/security.html +968 -0
- data/guides/output/stylesheets/fixes.css +16 -0
- data/guides/output/stylesheets/main.css +445 -0
- data/guides/output/stylesheets/print.css +52 -0
- data/guides/output/stylesheets/reset.css +43 -0
- data/guides/output/stylesheets/style.css +13 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCore.css +226 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
- data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
- data/guides/output/testing.html +1182 -0
- data/guides/source/active_record_validations_callbacks.textile +3 -3
- data/guides/source/active_support_core_extensions.textile +2 -2
- data/guides/source/asset_pipeline.textile +7 -5
- data/guides/source/association_basics.textile +2 -4
- data/guides/source/command_line.textile +1 -1
- data/guides/source/configuring.textile +4 -2
- data/guides/source/contributing_to_ruby_on_rails.textile +6 -6
- data/guides/source/initialization.textile +2 -2
- data/guides/source/performance_testing.textile +1 -1
- data/guides/source/routing.textile +1 -1
- data/guides/source/security.textile +2 -2
- data/lib/rails/commands/server.rb +1 -1
- data/lib/rails/engine.rb +1 -1
- data/lib/rails/generators/app_base.rb +2 -1
- data/lib/rails/generators/base.rb +3 -3
- data/lib/rails/generators/rails/app/templates/gitignore +15 -5
- data/lib/rails/rack.rb +0 -1
- data/lib/rails/tasks/documentation.rake +7 -7
- data/lib/rails/tasks/misc.rake +0 -2
- data/lib/rails/test_unit/testing.rake +2 -0
- data/lib/rails/version.rb +2 -2
- metadata +495 -326
- data/CHANGELOG +0 -2371
- data/lib/rails/rack/content_length.rb +0 -38
@@ -0,0 +1,1246 @@
|
|
1
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
|
2
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
|
3
|
+
|
4
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
5
|
+
<head>
|
6
|
+
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
7
|
+
|
8
|
+
<title>Ruby on Rails Guides: Rails Routing from the Outside In</title>
|
9
|
+
|
10
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
|
11
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
|
12
|
+
|
13
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
|
14
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
|
15
|
+
|
16
|
+
<link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
|
17
|
+
</head>
|
18
|
+
<body class="guide">
|
19
|
+
<div id="topNav">
|
20
|
+
<div class="wrapper">
|
21
|
+
<strong>More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
|
22
|
+
<a href="http://rubyonrails.org/">Overview</a> |
|
23
|
+
<a href="http://rubyonrails.org/download">Download</a> |
|
24
|
+
<a href="http://rubyonrails.org/deploy">Deploy</a> |
|
25
|
+
<a href="https://github.com/rails/rails">Code</a> |
|
26
|
+
<a href="http://rubyonrails.org/screencasts">Screencasts</a> |
|
27
|
+
<a href="http://rubyonrails.org/documentation">Documentation</a> |
|
28
|
+
<a href="http://rubyonrails.org/ecosystem">Ecosystem</a> |
|
29
|
+
<a href="http://rubyonrails.org/community">Community</a> |
|
30
|
+
<a href="http://weblog.rubyonrails.org/">Blog</a>
|
31
|
+
</div>
|
32
|
+
</div>
|
33
|
+
<div id="header">
|
34
|
+
<div class="wrapper clearfix">
|
35
|
+
<h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
|
36
|
+
<p class="hide"><a href="#mainCol">Skip navigation</a>.</p>
|
37
|
+
<ul class="nav">
|
38
|
+
<li><a href="index.html">Home</a></li>
|
39
|
+
<li class="index"><a href="index.html" onclick="guideMenu(); return false;" id="guidesMenu">Guides Index</a>
|
40
|
+
<div id="guides" class="clearfix" style="display: none;">
|
41
|
+
<hr />
|
42
|
+
<dl class="L">
|
43
|
+
<dt>Start Here</dt>
|
44
|
+
<dd><a href="getting_started.html">Getting Started with Rails</a></dd>
|
45
|
+
<dt>Models</dt>
|
46
|
+
<dd><a href="migrations.html">Rails Database Migrations</a></dd>
|
47
|
+
<dd><a href="active_record_validations_callbacks.html">Active Record Validations and Callbacks</a></dd>
|
48
|
+
<dd><a href="association_basics.html">Active Record Associations</a></dd>
|
49
|
+
<dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
|
50
|
+
<dt>Views</dt>
|
51
|
+
<dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
|
52
|
+
<dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
|
53
|
+
<dt>Controllers</dt>
|
54
|
+
<dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
|
55
|
+
<dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
|
56
|
+
</dl>
|
57
|
+
<dl class="R">
|
58
|
+
<dt>Digging Deeper</dt>
|
59
|
+
<dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
|
60
|
+
<dd><a href="i18n.html">Rails Internationalization API</a></dd>
|
61
|
+
<dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
|
62
|
+
<dd><a href="testing.html">Testing Rails Applications</a></dd>
|
63
|
+
<dd><a href="security.html">Securing Rails Applications</a></dd>
|
64
|
+
<dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
|
65
|
+
<dd><a href="performance_testing.html">Performance Testing Rails Applications</a></dd>
|
66
|
+
<dd><a href="configuring.html">Configuring Rails Applications</a></dd>
|
67
|
+
<dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
|
68
|
+
<dd><a href="caching_with_rails.html">Caching with Rails</a></dd>
|
69
|
+
<dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
|
70
|
+
|
71
|
+
<dt>Extending Rails</dt>
|
72
|
+
<dd><a href="plugins.html">The Basics of Creating Rails Plugins</a></dd>
|
73
|
+
<dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
|
74
|
+
<dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
|
75
|
+
|
76
|
+
<dt>Contributing to Ruby on Rails</dt>
|
77
|
+
<dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
|
78
|
+
<dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
|
79
|
+
<dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
|
80
|
+
|
81
|
+
<dt>Release Notes</dt>
|
82
|
+
<dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
|
83
|
+
<dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
|
84
|
+
<dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
|
85
|
+
<dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
|
86
|
+
</dl>
|
87
|
+
</div>
|
88
|
+
</li>
|
89
|
+
<li><a href="contributing_to_ruby_on_rails.html">Contribute</a></li>
|
90
|
+
<li><a href="credits.html">Credits</a></li>
|
91
|
+
</ul>
|
92
|
+
</div>
|
93
|
+
</div>
|
94
|
+
<hr class="hide" />
|
95
|
+
|
96
|
+
<div id="feature">
|
97
|
+
<div class="wrapper">
|
98
|
+
<h2>Rails Routing from the Outside In</h2>
|
99
|
+
<p>This guide covers the user-facing features of Rails routing. By referring to this guide, you will be able to:</p>
|
100
|
+
<ul>
|
101
|
+
<li>Understand the code in <tt>routes.rb</tt></li>
|
102
|
+
<li>Construct your own routes, using either the preferred resourceful style or the <tt>match</tt> method</li>
|
103
|
+
<li>Identify what parameters to expect an action to receive</li>
|
104
|
+
<li>Automatically create paths and URLs using route helpers</li>
|
105
|
+
<li>Use advanced techniques such as constraints and Rack endpoints</li>
|
106
|
+
</ul>
|
107
|
+
|
108
|
+
<div id="subCol">
|
109
|
+
<h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
|
110
|
+
<ol class="chapters">
|
111
|
+
<li><a href="#the-purpose-of-the-rails-router">The Purpose of the Rails Router</a><ul><li><a href="#connecting-urls-to-code">Connecting URLs to Code</a></li> <li><a href="#generating-paths-and-urls-from-code">Generating Paths and URLs from Code</a></li></ul></li><li><a href="#resource-routing-the-rails-default">Resource Routing: the Rails Default</a><ul><li><a href="#resources-on-the-web">Resources on the Web</a></li> <li><a href="#crud-verbs-and-actions"><span class="caps">CRUD</span>, Verbs, and Actions</a></li> <li><a href="#paths-and-urls">Paths and URLs</a></li> <li><a href="#defining-multiple-resources-at-the-same-time">Defining Multiple Resources at the Same Time</a></li> <li><a href="#singular-resources">Singular Resources</a></li> <li><a href="#controller-namespaces-and-routing">Controller Namespaces and Routing</a></li> <li><a href="#nested-resources">Nested Resources</a></li> <li><a href="#creating-paths-and-urls-from-objects">Creating Paths and URLs From Objects</a></li> <li><a href="#adding-more-restful-actions">Adding More RESTful Actions</a></li></ul></li><li><a href="#non-resourceful-routes">Non-Resourceful Routes</a><ul><li><a href="#bound-parameters">Bound Parameters</a></li> <li><a href="#dynamic-segments">Dynamic Segments</a></li> <li><a href="#static-segments">Static Segments</a></li> <li><a href="#the-query-string">The Query String</a></li> <li><a href="#defining-defaults">Defining Defaults</a></li> <li><a href="#naming-routes">Naming Routes</a></li> <li><a href="#http-verb-constraints"><span class="caps">HTTP</span> Verb Constraints</a></li> <li><a href="#segment-constraints">Segment Constraints</a></li> <li><a href="#request-based-constraints">Request-Based Constraints</a></li> <li><a href="#advanced-constraints">Advanced Constraints</a></li> <li><a href="#route-globbing">Route Globbing</a></li> <li><a href="#redirection">Redirection</a></li> <li><a href="#routing-to-rack-applications">Routing to Rack Applications</a></li> <li><a href="#using-root">Using <tt>root</tt></a></li></ul></li><li><a href="#customizing-resourceful-routes">Customizing Resourceful Routes</a><ul><li><a href="#specifying-a-controller-to-use">Specifying a Controller to Use</a></li> <li><a href="#specifying-constraints">Specifying Constraints</a></li> <li><a href="#overriding-the-named-helpers">Overriding the Named Helpers</a></li> <li><a href="#overriding-the-new-and-edit-segments">Overriding the <tt>new</tt> and <tt>edit</tt> Segments</a></li> <li><a href="#prefixing-the-named-route-helpers">Prefixing the Named Route Helpers</a></li> <li><a href="#restricting-the-routes-created">Restricting the Routes Created</a></li> <li><a href="#translated-paths">Translated Paths</a></li> <li><a href="#overriding-the-singular-form">Overriding the Singular Form</a></li> <li><a href="#nested-names">Using <tt>:as</tt> in Nested Resources</a></li></ul></li><li><a href="#inspecting-and-testing-routes">Inspecting and Testing Routes</a><ul><li><a href="#seeing-existing-routes-with-rake">Seeing Existing Routes with <tt>rake</tt></a></li> <li><a href="#testing-routes">Testing Routes</a></li></ul></li></ol></div>
|
112
|
+
</div>
|
113
|
+
</div>
|
114
|
+
|
115
|
+
<div id="container">
|
116
|
+
<div class="wrapper">
|
117
|
+
<div id="mainCol">
|
118
|
+
<h3 id="the-purpose-of-the-rails-router">1 The Purpose of the Rails Router</h3>
|
119
|
+
<p>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.</p>
|
120
|
+
<h4 id="connecting-urls-to-code">1.1 Connecting URLs to Code</h4>
|
121
|
+
<p>When your Rails application receives an incoming request</p>
|
122
|
+
<div class="code_container">
|
123
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
124
|
+
GET /patients/17
|
125
|
+
</pre>
|
126
|
+
</div>
|
127
|
+
<p>it asks the router to match it to a controller action. If the first matching route is</p>
|
128
|
+
<div class="code_container">
|
129
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
130
|
+
match "/patients/:id" => "patients#show"
|
131
|
+
</pre>
|
132
|
+
</div>
|
133
|
+
<p>the request is dispatched to the <tt>patients</tt> controller’s <tt>show</tt> action with <tt>{ :id => “17” }</tt> in <tt>params</tt>.</p>
|
134
|
+
<h4 id="generating-paths-and-urls-from-code">1.2 Generating Paths and URLs from Code</h4>
|
135
|
+
<p>You can also generate paths and URLs. If your application contains this code:</p>
|
136
|
+
<div class="code_container">
|
137
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
138
|
+
@patient = Patient.find(17)
|
139
|
+
</pre>
|
140
|
+
</div>
|
141
|
+
<div class="code_container">
|
142
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
143
|
+
<%= link_to "Patient Record", patient_path(@patient) %>
|
144
|
+
</pre>
|
145
|
+
</div>
|
146
|
+
<p>The router will generate the path <tt>/patients/17</tt>. 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.</p>
|
147
|
+
<h3 id="resource-routing-the-rails-default">2 Resource Routing: the Rails Default</h3>
|
148
|
+
<p>Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your <tt>index</tt>, <tt>show</tt>, <tt>new</tt>, <tt>edit</tt>, <tt>create</tt>, <tt>update</tt> and <tt>destroy</tt> actions, a resourceful route declares them in a single line of code.</p>
|
149
|
+
<h4 id="resources-on-the-web">2.1 Resources on the Web</h4>
|
150
|
+
<p>Browsers request pages from Rails by making a request for a <span class="caps">URL</span> using a specific <span class="caps">HTTP</span> method, such as <tt>GET</tt>, <tt>POST</tt>, <tt>PUT</tt> and <tt>DELETE</tt>. 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.</p>
|
151
|
+
<p>When your Rails application receives an incoming request for</p>
|
152
|
+
<div class="code_container">
|
153
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
154
|
+
DELETE /photos/17
|
155
|
+
</pre>
|
156
|
+
</div>
|
157
|
+
<p>it asks the router to map it to a controller action. If the first matching route is</p>
|
158
|
+
<div class="code_container">
|
159
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
160
|
+
resources :photos
|
161
|
+
</pre>
|
162
|
+
</div>
|
163
|
+
<p>Rails would dispatch that request to the <tt>destroy</tt> method on the <tt>photos</tt> controller with <tt>{ :id => “17” }</tt> in <tt>params</tt>.</p>
|
164
|
+
<h4 id="crud-verbs-and-actions">2.2 <span class="caps">CRUD</span>, Verbs, and Actions</h4>
|
165
|
+
<p>In Rails, a resourceful route provides a mapping between <span class="caps">HTTP</span> verbs and URLs to controller actions. By convention, each action also maps to particular <span class="caps">CRUD</span> operations in a database. A single entry in the routing file, such as</p>
|
166
|
+
<div class="code_container">
|
167
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
168
|
+
resources :photos
|
169
|
+
</pre>
|
170
|
+
</div>
|
171
|
+
<p>creates seven different routes in your application, all mapping to the <tt>Photos</tt> controller:</p>
|
172
|
+
<table>
|
173
|
+
<tr>
|
174
|
+
<th><span class="caps">HTTP</span> Verb </th>
|
175
|
+
<th>Path </th>
|
176
|
+
<th>action </th>
|
177
|
+
<th>used for </th>
|
178
|
+
</tr>
|
179
|
+
<tr>
|
180
|
+
<td><span class="caps">GET</span> </td>
|
181
|
+
<td>/photos </td>
|
182
|
+
<td>index </td>
|
183
|
+
<td>display a list of all photos </td>
|
184
|
+
</tr>
|
185
|
+
<tr>
|
186
|
+
<td><span class="caps">GET</span> </td>
|
187
|
+
<td>/photos/new </td>
|
188
|
+
<td>new </td>
|
189
|
+
<td>return an <span class="caps">HTML</span> form for creating a new photo </td>
|
190
|
+
</tr>
|
191
|
+
<tr>
|
192
|
+
<td><span class="caps">POST</span> </td>
|
193
|
+
<td>/photos </td>
|
194
|
+
<td>create </td>
|
195
|
+
<td>create a new photo </td>
|
196
|
+
</tr>
|
197
|
+
<tr>
|
198
|
+
<td><span class="caps">GET</span> </td>
|
199
|
+
<td>/photos/:id </td>
|
200
|
+
<td>show </td>
|
201
|
+
<td>display a specific photo </td>
|
202
|
+
</tr>
|
203
|
+
<tr>
|
204
|
+
<td><span class="caps">GET</span> </td>
|
205
|
+
<td>/photos/:id/edit </td>
|
206
|
+
<td>edit </td>
|
207
|
+
<td>return an <span class="caps">HTML</span> form for editing a photo </td>
|
208
|
+
</tr>
|
209
|
+
<tr>
|
210
|
+
<td><span class="caps">PUT</span> </td>
|
211
|
+
<td>/photos/:id </td>
|
212
|
+
<td>update </td>
|
213
|
+
<td>update a specific photo </td>
|
214
|
+
</tr>
|
215
|
+
<tr>
|
216
|
+
<td><span class="caps">DELETE</span> </td>
|
217
|
+
<td>/photos/:id </td>
|
218
|
+
<td>destroy </td>
|
219
|
+
<td>delete a specific photo </td>
|
220
|
+
</tr>
|
221
|
+
</table>
|
222
|
+
<div class="note"><p>Rails routes are matched in the order they are specified, so if you have a <tt>resources :photos</tt> above a <tt>get 'photos/poll'</tt> the <tt>show</tt> action’s route for the <tt>resources</tt> line will be matched before the <tt>get</tt> line. To fix this, move the <tt>get</tt> line <strong>above</strong> the <tt>resources</tt> line so that it is matched first.</p></div>
|
223
|
+
<h4 id="paths-and-urls">2.3 Paths and URLs</h4>
|
224
|
+
<p>Creating a resourceful route will also expose a number of helpers to the controllers in your application. In the case of <tt>resources :photos</tt>:</p>
|
225
|
+
<ul>
|
226
|
+
<li><tt>photos_path</tt> returns <tt>/photos</tt></li>
|
227
|
+
<li><tt>new_photo_path</tt> returns <tt>/photos/new</tt></li>
|
228
|
+
<li><tt>edit_photo_path(:id)</tt> returns <tt>/photos/:id/edit</tt> (for instance, <tt>edit_photo_path(10)</tt> returns <tt>/photos/10/edit</tt>)</li>
|
229
|
+
<li><tt>photo_path(:id)</tt> returns <tt>/photos/:id</tt> (for instance, <tt>photo_path(10)</tt> returns <tt>/photos/10</tt>)</li>
|
230
|
+
</ul>
|
231
|
+
<p>Each of these helpers has a corresponding <tt>_url</tt> helper (such as <tt>photos_url</tt>) which returns the same path prefixed with the current host, port and path prefix.</p>
|
232
|
+
<div class="note"><p>Because the router uses the <span class="caps">HTTP</span> verb and <span class="caps">URL</span> to match inbound requests, four URLs map to seven different actions.</p></div>
|
233
|
+
<h4 id="defining-multiple-resources-at-the-same-time">2.4 Defining Multiple Resources at the Same Time</h4>
|
234
|
+
<p>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 <tt>resources</tt>:</p>
|
235
|
+
<div class="code_container">
|
236
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
237
|
+
resources :photos, :books, :videos
|
238
|
+
</pre>
|
239
|
+
</div>
|
240
|
+
<p>This works exactly the same as</p>
|
241
|
+
<div class="code_container">
|
242
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
243
|
+
resources :photos
|
244
|
+
resources :books
|
245
|
+
resources :videos
|
246
|
+
</pre>
|
247
|
+
</div>
|
248
|
+
<h4 id="singular-resources">2.5 Singular Resources</h4>
|
249
|
+
<p>Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like <tt>/profile</tt> to always show the profile of the currently logged in user. In this case, you can use a singular resource to map <tt>/profile</tt> (rather than <tt>/profile/:id</tt>) to the <tt>show</tt> action.</p>
|
250
|
+
<div class="code_container">
|
251
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
252
|
+
match "profile" => "users#show"
|
253
|
+
</pre>
|
254
|
+
</div>
|
255
|
+
<p>This resourceful route</p>
|
256
|
+
<div class="code_container">
|
257
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
258
|
+
resource :geocoder
|
259
|
+
</pre>
|
260
|
+
</div>
|
261
|
+
<p>creates six different routes in your application, all mapping to the <tt>Geocoders</tt> controller:</p>
|
262
|
+
<table>
|
263
|
+
<tr>
|
264
|
+
<th><span class="caps">HTTP</span> Verb </th>
|
265
|
+
<th>Path </th>
|
266
|
+
<th>action </th>
|
267
|
+
<th>used for </th>
|
268
|
+
</tr>
|
269
|
+
<tr>
|
270
|
+
<td><span class="caps">GET</span> </td>
|
271
|
+
<td>/geocoder/new </td>
|
272
|
+
<td>new </td>
|
273
|
+
<td>return an <span class="caps">HTML</span> form for creating the geocoder </td>
|
274
|
+
</tr>
|
275
|
+
<tr>
|
276
|
+
<td><span class="caps">POST</span> </td>
|
277
|
+
<td>/geocoder </td>
|
278
|
+
<td>create </td>
|
279
|
+
<td>create the new geocoder </td>
|
280
|
+
</tr>
|
281
|
+
<tr>
|
282
|
+
<td><span class="caps">GET</span> </td>
|
283
|
+
<td>/geocoder </td>
|
284
|
+
<td>show </td>
|
285
|
+
<td>display the one and only geocoder resource </td>
|
286
|
+
</tr>
|
287
|
+
<tr>
|
288
|
+
<td><span class="caps">GET</span> </td>
|
289
|
+
<td>/geocoder/edit </td>
|
290
|
+
<td>edit </td>
|
291
|
+
<td>return an <span class="caps">HTML</span> form for editing the geocoder </td>
|
292
|
+
</tr>
|
293
|
+
<tr>
|
294
|
+
<td><span class="caps">PUT</span> </td>
|
295
|
+
<td>/geocoder </td>
|
296
|
+
<td>update </td>
|
297
|
+
<td>update the one and only geocoder resource </td>
|
298
|
+
</tr>
|
299
|
+
<tr>
|
300
|
+
<td><span class="caps">DELETE</span> </td>
|
301
|
+
<td>/geocoder </td>
|
302
|
+
<td>destroy </td>
|
303
|
+
<td>delete the geocoder resource </td>
|
304
|
+
</tr>
|
305
|
+
</table>
|
306
|
+
<div class="note"><p>Because you might want to use the same controller for a singular route (<tt>/account</tt>) and a plural route (<tt>/accounts/45</tt>), singular resources map to plural controllers.</p></div>
|
307
|
+
<p>A singular resourceful route generates these helpers:</p>
|
308
|
+
<ul>
|
309
|
+
<li><tt>new_geocoder_path</tt> returns <tt>/geocoder/new</tt></li>
|
310
|
+
<li><tt>edit_geocoder_path</tt> returns <tt>/geocoder/edit</tt></li>
|
311
|
+
<li><tt>geocoder_path</tt> returns <tt>/geocoder</tt></li>
|
312
|
+
</ul>
|
313
|
+
<p>As with plural resources, the same helpers ending in <tt>_url</tt> will also include the host, port and path prefix.</p>
|
314
|
+
<h4 id="controller-namespaces-and-routing">2.6 Controller Namespaces and Routing</h4>
|
315
|
+
<p>You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an <tt>Admin::</tt> namespace. You would place these controllers under the <tt>app/controllers/admin</tt> directory, and you can group them together in your router:</p>
|
316
|
+
<div class="code_container">
|
317
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
318
|
+
namespace :admin do
|
319
|
+
resources :posts, :comments
|
320
|
+
end
|
321
|
+
</pre>
|
322
|
+
</div>
|
323
|
+
<p>This will create a number of routes for each of the <tt>posts</tt> and <tt>comments</tt> controller. For <tt>Admin::PostsController</tt>, Rails will create:</p>
|
324
|
+
<table>
|
325
|
+
<tr>
|
326
|
+
<th><span class="caps">HTTP</span> Verb </th>
|
327
|
+
<th>Path </th>
|
328
|
+
<th>action </th>
|
329
|
+
<th>named helper </th>
|
330
|
+
</tr>
|
331
|
+
<tr>
|
332
|
+
<td><span class="caps">GET</span> </td>
|
333
|
+
<td>/admin/posts </td>
|
334
|
+
<td>index </td>
|
335
|
+
<td> admin_posts_path </td>
|
336
|
+
</tr>
|
337
|
+
<tr>
|
338
|
+
<td><span class="caps">GET</span> </td>
|
339
|
+
<td>/admin/posts/new </td>
|
340
|
+
<td>new </td>
|
341
|
+
<td> new_admin_post_path </td>
|
342
|
+
</tr>
|
343
|
+
<tr>
|
344
|
+
<td><span class="caps">POST</span> </td>
|
345
|
+
<td>/admin/posts </td>
|
346
|
+
<td>create </td>
|
347
|
+
<td> admin_posts_path </td>
|
348
|
+
</tr>
|
349
|
+
<tr>
|
350
|
+
<td><span class="caps">GET</span> </td>
|
351
|
+
<td>/admin/posts/:id </td>
|
352
|
+
<td>show </td>
|
353
|
+
<td> admin_post_path(:id) </td>
|
354
|
+
</tr>
|
355
|
+
<tr>
|
356
|
+
<td><span class="caps">GET</span> </td>
|
357
|
+
<td>/admin/posts/:id/edit </td>
|
358
|
+
<td>edit </td>
|
359
|
+
<td> edit_admin_post_path(:id) </td>
|
360
|
+
</tr>
|
361
|
+
<tr>
|
362
|
+
<td><span class="caps">PUT</span> </td>
|
363
|
+
<td>/admin/posts/:id </td>
|
364
|
+
<td>update </td>
|
365
|
+
<td> admin_post_path(:id) </td>
|
366
|
+
</tr>
|
367
|
+
<tr>
|
368
|
+
<td><span class="caps">DELETE</span> </td>
|
369
|
+
<td>/admin/posts/:id </td>
|
370
|
+
<td>destroy </td>
|
371
|
+
<td> admin_post_path(:id) </td>
|
372
|
+
</tr>
|
373
|
+
</table>
|
374
|
+
<p>If you want to route <tt>/posts</tt> (without the prefix <tt>/admin</tt>) to <tt>Admin::PostsController</tt>, you could use</p>
|
375
|
+
<div class="code_container">
|
376
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
377
|
+
scope :module => "admin" do
|
378
|
+
resources :posts, :comments
|
379
|
+
end
|
380
|
+
</pre>
|
381
|
+
</div>
|
382
|
+
<p>or, for a single case</p>
|
383
|
+
<div class="code_container">
|
384
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
385
|
+
resources :posts, :module => "admin"
|
386
|
+
</pre>
|
387
|
+
</div>
|
388
|
+
<p>If you want to route <tt>/admin/posts</tt> to <tt>PostsController</tt> (without the <tt>Admin::</tt> module prefix), you could use</p>
|
389
|
+
<div class="code_container">
|
390
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
391
|
+
scope "/admin" do
|
392
|
+
resources :posts, :comments
|
393
|
+
end
|
394
|
+
</pre>
|
395
|
+
</div>
|
396
|
+
<p>or, for a single case</p>
|
397
|
+
<div class="code_container">
|
398
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
399
|
+
resources :posts, :path => "/admin/posts"
|
400
|
+
</pre>
|
401
|
+
</div>
|
402
|
+
<p>In each of these cases, the named routes remain the same as if you did not use <tt>scope</tt>. In the last case, the following paths map to <tt>PostsController</tt>:</p>
|
403
|
+
<table>
|
404
|
+
<tr>
|
405
|
+
<th><span class="caps">HTTP</span> Verb </th>
|
406
|
+
<th>Path </th>
|
407
|
+
<th>action </th>
|
408
|
+
<th>named helper </th>
|
409
|
+
</tr>
|
410
|
+
<tr>
|
411
|
+
<td><span class="caps">GET</span> </td>
|
412
|
+
<td>/admin/posts </td>
|
413
|
+
<td>index </td>
|
414
|
+
<td> posts_path </td>
|
415
|
+
</tr>
|
416
|
+
<tr>
|
417
|
+
<td><span class="caps">GET</span> </td>
|
418
|
+
<td>/admin/posts/new </td>
|
419
|
+
<td>new </td>
|
420
|
+
<td> new_post_path </td>
|
421
|
+
</tr>
|
422
|
+
<tr>
|
423
|
+
<td><span class="caps">POST</span> </td>
|
424
|
+
<td>/admin/posts </td>
|
425
|
+
<td>create </td>
|
426
|
+
<td> posts_path </td>
|
427
|
+
</tr>
|
428
|
+
<tr>
|
429
|
+
<td><span class="caps">GET</span> </td>
|
430
|
+
<td>/admin/posts/:id </td>
|
431
|
+
<td>show </td>
|
432
|
+
<td> post_path(:id) </td>
|
433
|
+
</tr>
|
434
|
+
<tr>
|
435
|
+
<td><span class="caps">GET</span> </td>
|
436
|
+
<td>/admin/posts/:id/edit</td>
|
437
|
+
<td>edit </td>
|
438
|
+
<td> edit_post_path(:id)</td>
|
439
|
+
</tr>
|
440
|
+
<tr>
|
441
|
+
<td><span class="caps">PUT</span> </td>
|
442
|
+
<td>/admin/posts/:id </td>
|
443
|
+
<td>update </td>
|
444
|
+
<td> post_path(:id) </td>
|
445
|
+
</tr>
|
446
|
+
<tr>
|
447
|
+
<td><span class="caps">DELETE</span> </td>
|
448
|
+
<td>/admin/posts/:id </td>
|
449
|
+
<td>destroy </td>
|
450
|
+
<td> post_path(:id) </td>
|
451
|
+
</tr>
|
452
|
+
</table>
|
453
|
+
<h4 id="nested-resources">2.7 Nested Resources</h4>
|
454
|
+
<p>It’s common to have resources that are logically children of other resources. For example, suppose your application includes these models:</p>
|
455
|
+
<div class="code_container">
|
456
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
457
|
+
class Magazine < ActiveRecord::Base
|
458
|
+
has_many :ads
|
459
|
+
end
|
460
|
+
|
461
|
+
class Ad < ActiveRecord::Base
|
462
|
+
belongs_to :magazine
|
463
|
+
end
|
464
|
+
</pre>
|
465
|
+
</div>
|
466
|
+
<p>Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration:</p>
|
467
|
+
<div class="code_container">
|
468
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
469
|
+
resources :magazines do
|
470
|
+
resources :ads
|
471
|
+
end
|
472
|
+
</pre>
|
473
|
+
</div>
|
474
|
+
<p>In addition to the routes for magazines, this declaration will also route ads to an <tt>AdsController</tt>. The ad URLs require a magazine:</p>
|
475
|
+
<table>
|
476
|
+
<tr>
|
477
|
+
<th><span class="caps">HTTP</span> Verb </th>
|
478
|
+
<th>Path </th>
|
479
|
+
<th>action </th>
|
480
|
+
<th>used for </th>
|
481
|
+
</tr>
|
482
|
+
<tr>
|
483
|
+
<td><span class="caps">GET</span> </td>
|
484
|
+
<td>/magazines/:id/ads </td>
|
485
|
+
<td>index </td>
|
486
|
+
<td>display a list of all ads for a specific magazine </td>
|
487
|
+
</tr>
|
488
|
+
<tr>
|
489
|
+
<td><span class="caps">GET</span> </td>
|
490
|
+
<td>/magazines/:id/ads/new </td>
|
491
|
+
<td>new </td>
|
492
|
+
<td>return an <span class="caps">HTML</span> form for creating a new ad belonging to a specific magazine </td>
|
493
|
+
</tr>
|
494
|
+
<tr>
|
495
|
+
<td><span class="caps">POST</span> </td>
|
496
|
+
<td>/magazines/:id/ads </td>
|
497
|
+
<td>create </td>
|
498
|
+
<td>create a new ad belonging to a specific magazine </td>
|
499
|
+
</tr>
|
500
|
+
<tr>
|
501
|
+
<td><span class="caps">GET</span> </td>
|
502
|
+
<td>/magazines/:id/ads/:id </td>
|
503
|
+
<td>show </td>
|
504
|
+
<td>display a specific ad belonging to a specific magazine </td>
|
505
|
+
</tr>
|
506
|
+
<tr>
|
507
|
+
<td><span class="caps">GET</span> </td>
|
508
|
+
<td>/magazines/:id/ads/:id/edit </td>
|
509
|
+
<td>edit </td>
|
510
|
+
<td>return an <span class="caps">HTML</span> form for editing an ad belonging to a specific magazine </td>
|
511
|
+
</tr>
|
512
|
+
<tr>
|
513
|
+
<td><span class="caps">PUT</span> </td>
|
514
|
+
<td>/magazines/:id/ads/:id </td>
|
515
|
+
<td>update </td>
|
516
|
+
<td>update a specific ad belonging to a specific magazine </td>
|
517
|
+
</tr>
|
518
|
+
<tr>
|
519
|
+
<td><span class="caps">DELETE</span> </td>
|
520
|
+
<td>/magazines/:id/ads/:id </td>
|
521
|
+
<td>destroy </td>
|
522
|
+
<td>delete a specific ad belonging to a specific magazine </td>
|
523
|
+
</tr>
|
524
|
+
</table>
|
525
|
+
<p>This will also create routing helpers such as <tt>magazine_ads_url</tt> and <tt>edit_magazine_ad_path</tt>. These helpers take an instance of Magazine as the first parameter (<tt>magazine_ads_url(@magazine)</tt>).</p>
|
526
|
+
<h5 id="limits-to-nesting">2.7.1 Limits to Nesting</h5>
|
527
|
+
<p>You can nest resources within other nested resources if you like. For example:</p>
|
528
|
+
<div class="code_container">
|
529
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
530
|
+
resources :publishers do
|
531
|
+
resources :magazines do
|
532
|
+
resources :photos
|
533
|
+
end
|
534
|
+
end
|
535
|
+
</pre>
|
536
|
+
</div>
|
537
|
+
<p>Deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize paths such as</p>
|
538
|
+
<pre>
|
539
|
+
/publishers/1/magazines/2/photos/3
|
540
|
+
</pre>
|
541
|
+
<p>The corresponding route helper would be <tt>publisher_magazine_photo_url</tt>, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular <a href="http://weblog.jamisbuck.org/2007/2/5/nesting-resources">article</a> by Jamis Buck proposes a rule of thumb for good Rails design:</p>
|
542
|
+
<div class="info"><p><em>Resources should never be nested more than 1 level deep.</em></p></div>
|
543
|
+
<h4 id="creating-paths-and-urls-from-objects">2.8 Creating Paths and URLs From Objects</h4>
|
544
|
+
<p>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:</p>
|
545
|
+
<div class="code_container">
|
546
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
547
|
+
resources :magazines do
|
548
|
+
resources :ads
|
549
|
+
end
|
550
|
+
</pre>
|
551
|
+
</div>
|
552
|
+
<p>When using <tt>magazine_ad_path</tt>, you can pass in instances of <tt>Magazine</tt> and <tt>Ad</tt> instead of the numeric IDs.</p>
|
553
|
+
<div class="code_container">
|
554
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
555
|
+
<%= link_to "Ad details", magazine_ad_path(@magazine, @ad) %>
|
556
|
+
</pre>
|
557
|
+
</div>
|
558
|
+
<p>You can also use <tt>url_for</tt> with a set of objects, and Rails will automatically determine which route you want:</p>
|
559
|
+
<div class="code_container">
|
560
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
561
|
+
<%= link_to "Ad details", url_for([@magazine, @ad]) %>
|
562
|
+
</pre>
|
563
|
+
</div>
|
564
|
+
<p>In this case, Rails will see that <tt>@magazine</tt> is a <tt>Magazine</tt> and <tt>@ad</tt> is an <tt>Ad</tt> and will therefore use the <tt>magazine_ad_path</tt> helper. In helpers like <tt>link_to</tt>, you can specify just the object in place of the full <tt>url_for</tt> call:</p>
|
565
|
+
<div class="code_container">
|
566
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
567
|
+
<%= link_to "Ad details", [@magazine, @ad] %>
|
568
|
+
</pre>
|
569
|
+
</div>
|
570
|
+
<p>If you wanted to link to just a magazine, you could leave out the <tt>Array</tt>:</p>
|
571
|
+
<div class="code_container">
|
572
|
+
<pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
|
573
|
+
<%= link_to "Magazine details", @magazine %>
|
574
|
+
</pre>
|
575
|
+
</div>
|
576
|
+
<p>This allows you to treat instances of your models as URLs, and is a key advantage to using the resourceful style.</p>
|
577
|
+
<h4 id="adding-more-restful-actions">2.9 Adding More RESTful Actions</h4>
|
578
|
+
<p>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.</p>
|
579
|
+
<h5 id="adding-member-routes">2.9.1 Adding Member Routes</h5>
|
580
|
+
<p>To add a member route, just add a <tt>member</tt> block into the resource block:</p>
|
581
|
+
<div class="code_container">
|
582
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
583
|
+
resources :photos do
|
584
|
+
member do
|
585
|
+
get 'preview'
|
586
|
+
end
|
587
|
+
end
|
588
|
+
</pre>
|
589
|
+
</div>
|
590
|
+
<p>This will recognize <tt>/photos/1/preview</tt> with <span class="caps">GET</span>, and route to the <tt>preview</tt> action of <tt>PhotosController</tt>. It will also create the <tt>preview_photo_url</tt> and <tt>preview_photo_path</tt> helpers.</p>
|
591
|
+
<p>Within the block of member routes, each route name specifies the <span class="caps">HTTP</span> verb that it will recognize. You can use <tt>get</tt>, <tt>put</tt>, <tt>post</tt>, or <tt>delete</tt> here. If you don’t have multiple <tt>member</tt> routes, you can also pass <tt>:on</tt> to a route, eliminating the block:</p>
|
592
|
+
<div class="code_container">
|
593
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
594
|
+
resources :photos do
|
595
|
+
get 'preview', :on => :member
|
596
|
+
end
|
597
|
+
</pre>
|
598
|
+
</div>
|
599
|
+
<h5 id="adding-collection-routes">2.9.2 Adding Collection Routes</h5>
|
600
|
+
<p>To add a route to the collection:</p>
|
601
|
+
<div class="code_container">
|
602
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
603
|
+
resources :photos do
|
604
|
+
collection do
|
605
|
+
get 'search'
|
606
|
+
end
|
607
|
+
end
|
608
|
+
</pre>
|
609
|
+
</div>
|
610
|
+
<p>This will enable Rails to recognize paths such as <tt>/photos/search</tt> with <span class="caps">GET</span>, and route to the <tt>search</tt> action of <tt>PhotosController</tt>. It will also create the <tt>search_photos_url</tt> and <tt>search_photos_path</tt> route helpers.</p>
|
611
|
+
<p>Just as with member routes, you can pass <tt>:on</tt> to a route:</p>
|
612
|
+
<div class="code_container">
|
613
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
614
|
+
resources :photos do
|
615
|
+
get 'search', :on => :collection
|
616
|
+
end
|
617
|
+
</pre>
|
618
|
+
</div>
|
619
|
+
<h5 id="a-note-of-caution">2.9.3 A Note of Caution</h5>
|
620
|
+
<p>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.</p>
|
621
|
+
<h3 id="non-resourceful-routes">3 Non-Resourceful Routes</h3>
|
622
|
+
<p>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.</p>
|
623
|
+
<p>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.</p>
|
624
|
+
<p>In particular, simple routing makes it very easy to map legacy URLs to new Rails actions.</p>
|
625
|
+
<h4 id="bound-parameters">3.1 Bound Parameters</h4>
|
626
|
+
<p>When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming <span class="caps">HTTP</span> request. Two of these symbols are special: <tt>:controller</tt> maps to the name of a controller in your application, and <tt>:action</tt> maps to the name of an action within that controller. For example, consider one of the default Rails routes:</p>
|
627
|
+
<div class="code_container">
|
628
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
629
|
+
match ':controller(/:action(/:id))'
|
630
|
+
</pre>
|
631
|
+
</div>
|
632
|
+
<p>If an incoming request of <tt>/photos/show/1</tt> is processed by this route (because it hasn’t matched any previous route in the file), then the result will be to invoke the <tt>show</tt> action of the <tt>PhotosController</tt>, and to make the final parameter <tt>"1"</tt> available as <tt>params[:id]</tt>. This route will also route the incoming request of <tt>/photos</tt> to <tt>PhotosController#index</tt>, since <tt>:action</tt> and <tt>:id</tt> are optional parameters, denoted by parentheses.</p>
|
633
|
+
<h4 id="dynamic-segments">3.2 Dynamic Segments</h4>
|
634
|
+
<p>You can set up as many dynamic segments within a regular route as you like. Anything other than <tt>:controller</tt> or <tt>:action</tt> will be available to the action as part of <tt>params</tt>. If you set up this route:</p>
|
635
|
+
<div class="code_container">
|
636
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
637
|
+
match ':controller/:action/:id/:user_id'
|
638
|
+
</pre>
|
639
|
+
</div>
|
640
|
+
<p>An incoming path of <tt>/photos/show/1/2</tt> will be dispatched to the <tt>show</tt> action of the <tt>PhotosController</tt>. <tt>params[:id]</tt> will be <tt>"1"</tt>, and <tt>params[:user_id]</tt> will be <tt>"2"</tt>.</p>
|
641
|
+
<div class="note"><p>You can’t use <tt>namespace</tt> or <tt>:module</tt> with a <tt>:controller</tt> path segment. If you need to do this then use a constraint on :controller that matches the namespace you require. e.g:</p></div>
|
642
|
+
<div class="code_container">
|
643
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
644
|
+
match ':controller(/:action(/:id))', :controller => /admin\/[^\/]+/
|
645
|
+
</pre>
|
646
|
+
</div>
|
647
|
+
<div class="info"><p>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 <tt>:id</tt> => /[^\/]+/ allows anything except a slash.</p></div>
|
648
|
+
<h4 id="static-segments">3.3 Static Segments</h4>
|
649
|
+
<p>You can specify static segments when creating a route:</p>
|
650
|
+
<div class="code_container">
|
651
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
652
|
+
match ':controller/:action/:id/with_user/:user_id'
|
653
|
+
</pre>
|
654
|
+
</div>
|
655
|
+
<p>This route would respond to paths such as <tt>/photos/show/1/with_user/2</tt>. In this case, <tt>params</tt> would be <tt>{ :controller => “photos”, :action => “show”, :id => “1”, :user_id => “2” }</tt>.</p>
|
656
|
+
<h4 id="the-query-string">3.4 The Query String</h4>
|
657
|
+
<p>The <tt>params</tt> will also include any parameters from the query string. For example, with this route:</p>
|
658
|
+
<div class="code_container">
|
659
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
660
|
+
match ':controller/:action/:id'
|
661
|
+
</pre>
|
662
|
+
</div>
|
663
|
+
<p>An incoming path of <tt>/photos/show/1?user_id=2</tt> will be dispatched to the <tt>show</tt> action of the <tt>Photos</tt> controller. <tt>params</tt> will be <tt>{ :controller => “photos”, :action => “show”, :id => “1”, :user_id => “2” }</tt>.</p>
|
664
|
+
<h4 id="defining-defaults">3.5 Defining Defaults</h4>
|
665
|
+
<p>You do not need to explicitly use the <tt>:controller</tt> and <tt>:action</tt> symbols within a route. You can supply them as defaults:</p>
|
666
|
+
<div class="code_container">
|
667
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
668
|
+
match 'photos/:id' => 'photos#show'
|
669
|
+
</pre>
|
670
|
+
</div>
|
671
|
+
<p>With this route, Rails will match an incoming path of <tt>/photos/12</tt> to the <tt>show</tt> action of <tt>PhotosController</tt>.</p>
|
672
|
+
<p>You can also define other defaults in a route by supplying a hash for the <tt>:defaults</tt> option. This even applies to parameters that you do not specify as dynamic segments. For example:</p>
|
673
|
+
<div class="code_container">
|
674
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
675
|
+
match 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
|
676
|
+
</pre>
|
677
|
+
</div>
|
678
|
+
<p>Rails would match <tt>photos/12</tt> to the <tt>show</tt> action of <tt>PhotosController</tt>, and set <tt>params[:format]</tt> to <tt>"jpg"</tt>.</p>
|
679
|
+
<h4 id="naming-routes">3.6 Naming Routes</h4>
|
680
|
+
<p>You can specify a name for any route using the <tt>:as</tt> option.</p>
|
681
|
+
<div class="code_container">
|
682
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
683
|
+
match 'exit' => 'sessions#destroy', :as => :logout
|
684
|
+
</pre>
|
685
|
+
</div>
|
686
|
+
<p>This will create <tt>logout_path</tt> and <tt>logout_url</tt> as named helpers in your application. Calling <tt>logout_path</tt> will return <tt>/exit</tt></p>
|
687
|
+
<h4 id="http-verb-constraints">3.7 <span class="caps">HTTP</span> Verb Constraints</h4>
|
688
|
+
<p>You can use the <tt>:via</tt> option to constrain the request to one or more <span class="caps">HTTP</span> methods:</p>
|
689
|
+
<div class="code_container">
|
690
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
691
|
+
match 'photos/show' => 'photos#show', :via => :get
|
692
|
+
</pre>
|
693
|
+
</div>
|
694
|
+
<p>There is a shorthand version of this as well:</p>
|
695
|
+
<div class="code_container">
|
696
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
697
|
+
get 'photos/show'
|
698
|
+
</pre>
|
699
|
+
</div>
|
700
|
+
<p>You can also permit more than one verb to a single route:</p>
|
701
|
+
<div class="code_container">
|
702
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
703
|
+
match 'photos/show' => 'photos#show', :via => [:get, :post]
|
704
|
+
</pre>
|
705
|
+
</div>
|
706
|
+
<h4 id="segment-constraints">3.8 Segment Constraints</h4>
|
707
|
+
<p>You can use the <tt>:constraints</tt> option to enforce a format for a dynamic segment:</p>
|
708
|
+
<div class="code_container">
|
709
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
710
|
+
match 'photos/:id' => 'photos#show', :constraints => { :id => /[A-Z]\d{5}/ }
|
711
|
+
</pre>
|
712
|
+
</div>
|
713
|
+
<p>This route would match paths such as <tt>/photos/A12345</tt>. You can more succinctly express the same route this way:</p>
|
714
|
+
<div class="code_container">
|
715
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
716
|
+
match 'photos/:id' => 'photos#show', :id => /[A-Z]\d{5}/
|
717
|
+
</pre>
|
718
|
+
</div>
|
719
|
+
<p><tt>:constraints</tt> takes regular expressions with the restriction that regexp anchors can’t be used. For example, the following route will not work:</p>
|
720
|
+
<div class="code_container">
|
721
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
722
|
+
match '/:id' => 'posts#show', :constraints => {:id => /^\d/}
|
723
|
+
</pre>
|
724
|
+
</div>
|
725
|
+
<p>However, note that you don’t need to use anchors because all routes are anchored at the start.</p>
|
726
|
+
<p>For example, the following routes would allow for <tt>posts</tt> with <tt>to_param</tt> values like <tt>1-hello-world</tt> that always begin with a number and <tt>users</tt> with <tt>to_param</tt> values like <tt>david</tt> that never begin with a number to share the root namespace:</p>
|
727
|
+
<div class="code_container">
|
728
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
729
|
+
match '/:id' => 'posts#show', :constraints => { :id => /\d.+/ }
|
730
|
+
match '/:username' => 'users#show'
|
731
|
+
</pre>
|
732
|
+
</div>
|
733
|
+
<h4 id="request-based-constraints">3.9 Request-Based Constraints</h4>
|
734
|
+
<p>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 <tt>String</tt>.</p>
|
735
|
+
<p>You specify a request-based constraint the same way that you specify a segment constraint:</p>
|
736
|
+
<div class="code_container">
|
737
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
738
|
+
match "photos", :constraints => {:subdomain => "admin"}
|
739
|
+
</pre>
|
740
|
+
</div>
|
741
|
+
<p>You can also specify constraints in a block form:</p>
|
742
|
+
<div class="code_container">
|
743
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
744
|
+
namespace :admin do
|
745
|
+
constraints :subdomain => "admin" do
|
746
|
+
resources :photos
|
747
|
+
end
|
748
|
+
end
|
749
|
+
</pre>
|
750
|
+
</div>
|
751
|
+
<h4 id="advanced-constraints">3.10 Advanced Constraints</h4>
|
752
|
+
<p>If you have a more advanced constraint, you can provide an object that responds to <tt>matches?</tt> that Rails should use. Let’s say you wanted to route all users on a blacklist to the <tt>BlacklistController</tt>. You could do:</p>
|
753
|
+
<div class="code_container">
|
754
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
755
|
+
class BlacklistConstraint
|
756
|
+
def initialize
|
757
|
+
@ips = Blacklist.retrieve_ips
|
758
|
+
end
|
759
|
+
|
760
|
+
def matches?(request)
|
761
|
+
@ips.include?(request.remote_ip)
|
762
|
+
end
|
763
|
+
end
|
764
|
+
|
765
|
+
TwitterClone::Application.routes.draw do
|
766
|
+
match "*path" => "blacklist#index",
|
767
|
+
:constraints => BlacklistConstraint.new
|
768
|
+
end
|
769
|
+
</pre>
|
770
|
+
</div>
|
771
|
+
<h4 id="route-globbing">3.11 Route Globbing</h4>
|
772
|
+
<p>Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example</p>
|
773
|
+
<div class="code_container">
|
774
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
775
|
+
match 'photos/*other' => 'photos#unknown'
|
776
|
+
</pre>
|
777
|
+
</div>
|
778
|
+
<p>This route would match <tt>photos/12</tt> or <tt>/photos/long/path/to/12</tt>, setting <tt>params[:other]</tt> to <tt>"12"</tt> or <tt>"long/path/to/12"</tt>.</p>
|
779
|
+
<p>Wildcard segments can occur anywhere in a route. For example,</p>
|
780
|
+
<div class="code_container">
|
781
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
782
|
+
match 'books/*section/:title' => 'books#show'
|
783
|
+
</pre>
|
784
|
+
</div>
|
785
|
+
<p>would match <tt>books/some/section/last-words-a-memoir</tt> with <tt>params[:section]</tt> equals <tt>"some/section"</tt>, and <tt>params[:title]</tt> equals <tt>"last-words-a-memoir"</tt>.</p>
|
786
|
+
<p>Technically a route can have even more than one wildcard segment. The matcher assigns segments to parameters in an intuitive way. For example,</p>
|
787
|
+
<div class="code_container">
|
788
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
789
|
+
match '*a/foo/*b' => 'test#index'
|
790
|
+
</pre>
|
791
|
+
</div>
|
792
|
+
<p>would match <tt>zoo/woo/foo/bar/baz</tt> with <tt>params[:a]</tt> equals <tt>"zoo/woo"</tt>, and <tt>params[:b]</tt> equals <tt>"bar/baz"</tt>.</p>
|
793
|
+
<div class="note"><p>Starting from Rails 3.1, wildcard routes will always match the optional format segment by default. For example if you have this route:</p></div>
|
794
|
+
<div class="code_container">
|
795
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
796
|
+
match '*pages' => 'pages#show'
|
797
|
+
</pre>
|
798
|
+
</div>
|
799
|
+
<div class="note"><p>By requesting <tt>"/foo/bar.json"</tt>, your <tt>params[:pages]</tt> will be equals to <tt>"foo/bar"</tt> with the request format of <span class="caps">JSON</span>. If you want the old 3.0.x behavior back, you could supply <tt>:format => false</tt> like this:</p></div>
|
800
|
+
<div class="code_container">
|
801
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
802
|
+
match '*pages' => 'pages#show', :format => false
|
803
|
+
</pre>
|
804
|
+
</div>
|
805
|
+
<div class="note"><p>If you want to make the format segment mandatory, so it cannot be omitted, you can supply <tt>:format => true</tt> like this:</p></div>
|
806
|
+
<div class="code_container">
|
807
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
808
|
+
match '*pages' => 'pages#show', :format => true
|
809
|
+
</pre>
|
810
|
+
</div>
|
811
|
+
<h4 id="redirection">3.12 Redirection</h4>
|
812
|
+
<p>You can redirect any path to another path using the <tt>redirect</tt> helper in your router:</p>
|
813
|
+
<div class="code_container">
|
814
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
815
|
+
match "/stories" => redirect("/posts")
|
816
|
+
</pre>
|
817
|
+
</div>
|
818
|
+
<p>You can also reuse dynamic segments from the match in the path to redirect to:</p>
|
819
|
+
<div class="code_container">
|
820
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
821
|
+
match "/stories/:name" => redirect("/posts/%{name}")
|
822
|
+
</pre>
|
823
|
+
</div>
|
824
|
+
<p>You can also provide a block to redirect, which receives the params and (optionally) the request object:</p>
|
825
|
+
<div class="code_container">
|
826
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
827
|
+
match "/stories/:name" => redirect {|params| "/posts/#{params[:name].pluralize}" }
|
828
|
+
match "/stories" => redirect {|p, req| "/posts/#{req.subdomain}" }
|
829
|
+
</pre>
|
830
|
+
</div>
|
831
|
+
<p>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.</p>
|
832
|
+
<p>In all of these cases, if you don’t provide the leading host (<tt>http://www.example.com</tt>), Rails will take those details from the current request.</p>
|
833
|
+
<h4 id="routing-to-rack-applications">3.13 Routing to Rack Applications</h4>
|
834
|
+
<p>Instead of a String, like <tt>"posts#index"</tt>, which corresponds to the <tt>index</tt> action in the <tt>PostsController</tt>, you can specify any <a href="rails_on_rack.html">Rack application</a> as the endpoint for a matcher.</p>
|
835
|
+
<div class="code_container">
|
836
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
837
|
+
match "/application.js" => Sprockets
|
838
|
+
</pre>
|
839
|
+
</div>
|
840
|
+
<p>As long as <tt>Sprockets</tt> responds to <tt>call</tt> and returns a <tt>[status, headers, body]</tt>, the router won’t know the difference between the Rack application and an action.</p>
|
841
|
+
<div class="note"><p>For the curious, <tt>"posts#index"</tt> actually expands out to <tt>PostsController.action(:index)</tt>, which returns a valid Rack application.</p></div>
|
842
|
+
<h4 id="using-root">3.14 Using <tt>root</tt></h4>
|
843
|
+
<p>You can specify what Rails should route <tt>"/"</tt> to with the <tt>root</tt> method:</p>
|
844
|
+
<div class="code_container">
|
845
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
846
|
+
root :to => 'pages#main'
|
847
|
+
</pre>
|
848
|
+
</div>
|
849
|
+
<p>You should put the <tt>root</tt> route at the end of the file. You also need to delete the <tt>public/index.html</tt> file for the root route to take effect.</p>
|
850
|
+
<h3 id="customizing-resourceful-routes">4 Customizing Resourceful Routes</h3>
|
851
|
+
<p>While the default routes and helpers generated by <tt>resources :posts</tt> 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.</p>
|
852
|
+
<h4 id="specifying-a-controller-to-use">4.1 Specifying a Controller to Use</h4>
|
853
|
+
<p>The <tt>:controller</tt> option lets you explicitly specify a controller to use for the resource. For example:</p>
|
854
|
+
<div class="code_container">
|
855
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
856
|
+
resources :photos, :controller => "images"
|
857
|
+
</pre>
|
858
|
+
</div>
|
859
|
+
<p>will recognize incoming paths beginning with <tt>/photos</tt> but route to the <tt>Images</tt> controller:</p>
|
860
|
+
<table>
|
861
|
+
<tr>
|
862
|
+
<th><span class="caps">HTTP</span> Verb </th>
|
863
|
+
<th>Path </th>
|
864
|
+
<th>action </th>
|
865
|
+
<th>named helper </th>
|
866
|
+
</tr>
|
867
|
+
<tr>
|
868
|
+
<td><span class="caps">GET</span> </td>
|
869
|
+
<td>/photos </td>
|
870
|
+
<td>index </td>
|
871
|
+
<td> photos_path </td>
|
872
|
+
</tr>
|
873
|
+
<tr>
|
874
|
+
<td><span class="caps">GET</span> </td>
|
875
|
+
<td>/photos/new </td>
|
876
|
+
<td>new </td>
|
877
|
+
<td> new_photo_path </td>
|
878
|
+
</tr>
|
879
|
+
<tr>
|
880
|
+
<td><span class="caps">POST</span> </td>
|
881
|
+
<td>/photos </td>
|
882
|
+
<td>create </td>
|
883
|
+
<td> photos_path </td>
|
884
|
+
</tr>
|
885
|
+
<tr>
|
886
|
+
<td><span class="caps">GET</span> </td>
|
887
|
+
<td>/photos/:id </td>
|
888
|
+
<td>show </td>
|
889
|
+
<td> photo_path(:id) </td>
|
890
|
+
</tr>
|
891
|
+
<tr>
|
892
|
+
<td><span class="caps">GET</span> </td>
|
893
|
+
<td>/photos/:id/edit </td>
|
894
|
+
<td>edit </td>
|
895
|
+
<td> edit_photo_path(:id) </td>
|
896
|
+
</tr>
|
897
|
+
<tr>
|
898
|
+
<td><span class="caps">PUT</span> </td>
|
899
|
+
<td>/photos/:id </td>
|
900
|
+
<td>update </td>
|
901
|
+
<td> photo_path(:id) </td>
|
902
|
+
</tr>
|
903
|
+
<tr>
|
904
|
+
<td><span class="caps">DELETE</span> </td>
|
905
|
+
<td>/photos/:id </td>
|
906
|
+
<td>destroy </td>
|
907
|
+
<td> photo_path(:id) </td>
|
908
|
+
</tr>
|
909
|
+
</table>
|
910
|
+
<div class="note"><p>Use <tt>photos_path</tt>, <tt>new_photo_path</tt>, etc. to generate paths for this resource.</p></div>
|
911
|
+
<h4 id="specifying-constraints">4.2 Specifying Constraints</h4>
|
912
|
+
<p>You can use the <tt>:constraints</tt> option to specify a required format on the implicit <tt>id</tt>. For example:</p>
|
913
|
+
<div class="code_container">
|
914
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
915
|
+
resources :photos, :constraints => {:id => /[A-Z][A-Z][0-9]+/}
|
916
|
+
</pre>
|
917
|
+
</div>
|
918
|
+
<p>This declaration constraints the <tt>:id</tt> parameter to match the supplied regular expression. So, in this case, the router would no longer match <tt>/photos/1</tt> to this route. Instead, <tt>/photos/RR27</tt> would match.</p>
|
919
|
+
<p>You can specify a single constraint to apply to a number of routes by using the block form:</p>
|
920
|
+
<div class="code_container">
|
921
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
922
|
+
constraints(:id => /[A-Z][A-Z][0-9]+/) do
|
923
|
+
resources :photos
|
924
|
+
resources :accounts
|
925
|
+
end
|
926
|
+
</pre>
|
927
|
+
</div>
|
928
|
+
<div class="note"><p>Of course, you can use the more advanced constraints available in non-resourceful routes in this context.</p></div>
|
929
|
+
<div class="info"><p>By default the <tt>:id</tt> 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 <tt>:id</tt> add a constraint which overrides this – for example <tt>:id</tt> => /[^\/]+/ allows anything except a slash.</p></div>
|
930
|
+
<h4 id="overriding-the-named-helpers">4.3 Overriding the Named Helpers</h4>
|
931
|
+
<p>The <tt>:as</tt> option lets you override the normal naming for the named route helpers. For example:</p>
|
932
|
+
<div class="code_container">
|
933
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
934
|
+
resources :photos, :as => "images"
|
935
|
+
</pre>
|
936
|
+
</div>
|
937
|
+
<p>will recognize incoming paths beginning with <tt>/photos</tt> and route the requests to <tt>PhotosController</tt>, but use the value of the :as option to name the helpers.</p>
|
938
|
+
<table>
|
939
|
+
<tr>
|
940
|
+
<th><span class="caps">HTTP</span> verb</th>
|
941
|
+
<th>Path </th>
|
942
|
+
<th>action </th>
|
943
|
+
<th>named helper </th>
|
944
|
+
</tr>
|
945
|
+
<tr>
|
946
|
+
<td><span class="caps">GET</span> </td>
|
947
|
+
<td>/photos </td>
|
948
|
+
<td>index </td>
|
949
|
+
<td> images_path </td>
|
950
|
+
</tr>
|
951
|
+
<tr>
|
952
|
+
<td><span class="caps">GET</span> </td>
|
953
|
+
<td>/photos/new </td>
|
954
|
+
<td>new </td>
|
955
|
+
<td> new_image_path </td>
|
956
|
+
</tr>
|
957
|
+
<tr>
|
958
|
+
<td><span class="caps">POST</span> </td>
|
959
|
+
<td>/photos </td>
|
960
|
+
<td>create </td>
|
961
|
+
<td> images_path </td>
|
962
|
+
</tr>
|
963
|
+
<tr>
|
964
|
+
<td><span class="caps">GET</span> </td>
|
965
|
+
<td>/photos/:id </td>
|
966
|
+
<td>show </td>
|
967
|
+
<td> image_path(:id) </td>
|
968
|
+
</tr>
|
969
|
+
<tr>
|
970
|
+
<td><span class="caps">GET</span> </td>
|
971
|
+
<td>/photos/:id/edit </td>
|
972
|
+
<td>edit </td>
|
973
|
+
<td> edit_image_path(:id) </td>
|
974
|
+
</tr>
|
975
|
+
<tr>
|
976
|
+
<td><span class="caps">PUT</span> </td>
|
977
|
+
<td>/photos/:id </td>
|
978
|
+
<td>update </td>
|
979
|
+
<td> image_path(:id) </td>
|
980
|
+
</tr>
|
981
|
+
<tr>
|
982
|
+
<td><span class="caps">DELETE</span> </td>
|
983
|
+
<td>/photos/:id </td>
|
984
|
+
<td>destroy </td>
|
985
|
+
<td> image_path(:id) </td>
|
986
|
+
</tr>
|
987
|
+
</table>
|
988
|
+
<h4 id="overriding-the-new-and-edit-segments">4.4 Overriding the <tt>new</tt> and <tt>edit</tt> Segments</h4>
|
989
|
+
<p>The <tt>:path_names</tt> option lets you override the automatically-generated “new” and “edit” segments in paths:</p>
|
990
|
+
<div class="code_container">
|
991
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
992
|
+
resources :photos, :path_names => { :new => 'make', :edit => 'change' }
|
993
|
+
</pre>
|
994
|
+
</div>
|
995
|
+
<p>This would cause the routing to recognize paths such as</p>
|
996
|
+
<div class="code_container">
|
997
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
998
|
+
/photos/make
|
999
|
+
/photos/1/change
|
1000
|
+
</pre>
|
1001
|
+
</div>
|
1002
|
+
<div class="note"><p>The actual action names aren’t changed by this option. The two paths shown would still route to the <tt>new</tt> and <tt>edit</tt> actions.</p></div>
|
1003
|
+
<div class="info"><p>If you find yourself wanting to change this option uniformly for all of your routes, you can use a scope.</p></div>
|
1004
|
+
<div class="code_container">
|
1005
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1006
|
+
scope :path_names => { :new => "make" } do
|
1007
|
+
# rest of your routes
|
1008
|
+
end
|
1009
|
+
</pre>
|
1010
|
+
</div>
|
1011
|
+
<h4 id="prefixing-the-named-route-helpers">4.5 Prefixing the Named Route Helpers</h4>
|
1012
|
+
<p>You can use the <tt>:as</tt> 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.</p>
|
1013
|
+
<div class="code_container">
|
1014
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1015
|
+
scope "admin" do
|
1016
|
+
resources :photos, :as => "admin_photos"
|
1017
|
+
end
|
1018
|
+
|
1019
|
+
resources :photos
|
1020
|
+
</pre>
|
1021
|
+
</div>
|
1022
|
+
<p>This will provide route helpers such as <tt>admin_photos_path</tt>, <tt>new_admin_photo_path</tt> etc.</p>
|
1023
|
+
<p>To prefix a group of route helpers, use <tt>:as</tt> with <tt>scope</tt>:</p>
|
1024
|
+
<div class="code_container">
|
1025
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1026
|
+
scope "admin", :as => "admin" do
|
1027
|
+
resources :photos, :accounts
|
1028
|
+
end
|
1029
|
+
|
1030
|
+
resources :photos, :accounts
|
1031
|
+
</pre>
|
1032
|
+
</div>
|
1033
|
+
<p>This will generate routes such as <tt>admin_photos_path</tt> and <tt>admin_accounts_path</tt> which map to <tt>/admin/photos</tt> and <tt>/admin/accounts</tt> respectively.</p>
|
1034
|
+
<div class="note"><p>The <tt>namespace</tt> scope will automatically add <tt>:as</tt> as well as <tt>:module</tt> and <tt>:path</tt> prefixes.</p></div>
|
1035
|
+
<p>You can prefix routes with a named parameter also:</p>
|
1036
|
+
<div class="code_container">
|
1037
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1038
|
+
scope ":username" do
|
1039
|
+
resources :posts
|
1040
|
+
end
|
1041
|
+
</pre>
|
1042
|
+
</div>
|
1043
|
+
<p>This will provide you with URLs such as <tt>/bob/posts/1</tt> and will allow you to reference the <tt>username</tt> part of the path as <tt>params[:username]</tt> in controllers, helpers and views.</p>
|
1044
|
+
<h4 id="restricting-the-routes-created">4.6 Restricting the Routes Created</h4>
|
1045
|
+
<p>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 <tt>:only</tt> and <tt>:except</tt> options to fine-tune this behavior. The <tt>:only</tt> option tells Rails to create only the specified routes:</p>
|
1046
|
+
<div class="code_container">
|
1047
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1048
|
+
resources :photos, :only => [:index, :show]
|
1049
|
+
</pre>
|
1050
|
+
</div>
|
1051
|
+
<p>Now, a <tt>GET</tt> request to <tt>/photos</tt> would succeed, but a <tt>POST</tt> request to <tt>/photos</tt> (which would ordinarily be routed to the <tt>create</tt> action) will fail.</p>
|
1052
|
+
<p>The <tt>:except</tt> option specifies a route or list of routes that Rails should <em>not</em> create:</p>
|
1053
|
+
<div class="code_container">
|
1054
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1055
|
+
resources :photos, :except => :destroy
|
1056
|
+
</pre>
|
1057
|
+
</div>
|
1058
|
+
<p>In this case, Rails will create all of the normal routes except the route for <tt>destroy</tt> (a <tt>DELETE</tt> request to <tt>/photos/:id</tt>).</p>
|
1059
|
+
<div class="info"><p>If your application has many RESTful routes, using <tt>:only</tt> and <tt>:except</tt> to generate only the routes that you actually need can cut down on memory use and speed up the routing process.</p></div>
|
1060
|
+
<h4 id="translated-paths">4.7 Translated Paths</h4>
|
1061
|
+
<p>Using <tt>scope</tt>, we can alter path names generated by resources:</p>
|
1062
|
+
<div class="code_container">
|
1063
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1064
|
+
scope(:path_names => { :new => "neu", :edit => "bearbeiten" }) do
|
1065
|
+
resources :categories, :path => "kategorien"
|
1066
|
+
end
|
1067
|
+
</pre>
|
1068
|
+
</div>
|
1069
|
+
<p>Rails now creates routes to the <tt>CategoriesController</tt>.</p>
|
1070
|
+
<table>
|
1071
|
+
<tr>
|
1072
|
+
<th><span class="caps">HTTP</span> verb</th>
|
1073
|
+
<th>Path </th>
|
1074
|
+
<th>action </th>
|
1075
|
+
<th>named helper </th>
|
1076
|
+
</tr>
|
1077
|
+
<tr>
|
1078
|
+
<td><span class="caps">GET</span> </td>
|
1079
|
+
<td>/kategorien </td>
|
1080
|
+
<td>index </td>
|
1081
|
+
<td> categories_path </td>
|
1082
|
+
</tr>
|
1083
|
+
<tr>
|
1084
|
+
<td><span class="caps">GET</span> </td>
|
1085
|
+
<td>/kategorien/neu </td>
|
1086
|
+
<td>new </td>
|
1087
|
+
<td> new_category_path </td>
|
1088
|
+
</tr>
|
1089
|
+
<tr>
|
1090
|
+
<td><span class="caps">POST</span> </td>
|
1091
|
+
<td>/kategorien </td>
|
1092
|
+
<td>create </td>
|
1093
|
+
<td> categories_path </td>
|
1094
|
+
</tr>
|
1095
|
+
<tr>
|
1096
|
+
<td><span class="caps">GET</span> </td>
|
1097
|
+
<td>/kategorien/:id </td>
|
1098
|
+
<td>show </td>
|
1099
|
+
<td> category_path(:id) </td>
|
1100
|
+
</tr>
|
1101
|
+
<tr>
|
1102
|
+
<td><span class="caps">GET</span> </td>
|
1103
|
+
<td>/kategorien/:id/bearbeiten </td>
|
1104
|
+
<td>edit </td>
|
1105
|
+
<td> edit_category_path(:id) </td>
|
1106
|
+
</tr>
|
1107
|
+
<tr>
|
1108
|
+
<td><span class="caps">PUT</span> </td>
|
1109
|
+
<td>/kategorien/:id </td>
|
1110
|
+
<td>update </td>
|
1111
|
+
<td> category_path(:id) </td>
|
1112
|
+
</tr>
|
1113
|
+
<tr>
|
1114
|
+
<td><span class="caps">DELETE</span> </td>
|
1115
|
+
<td>/kategorien/:id </td>
|
1116
|
+
<td>destroy </td>
|
1117
|
+
<td> category_path(:id) </td>
|
1118
|
+
</tr>
|
1119
|
+
</table>
|
1120
|
+
<h4 id="overriding-the-singular-form">4.8 Overriding the Singular Form</h4>
|
1121
|
+
<p>If you want to define the singular form of a resource, you should add additional rules to the <tt>Inflector</tt>.</p>
|
1122
|
+
<div class="code_container">
|
1123
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1124
|
+
ActiveSupport::Inflector.inflections do |inflect|
|
1125
|
+
inflect.irregular 'tooth', 'teeth'
|
1126
|
+
end
|
1127
|
+
</pre>
|
1128
|
+
</div>
|
1129
|
+
<h4 id="nested-names">4.9 Using <tt>:as</tt> in Nested Resources</h4>
|
1130
|
+
<p>The <tt>:as</tt> option overrides the automatically-generated name for the resource in nested route helpers. For example,</p>
|
1131
|
+
<div class="code_container">
|
1132
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1133
|
+
resources :magazines do
|
1134
|
+
resources :ads, :as => 'periodical_ads'
|
1135
|
+
end
|
1136
|
+
</pre>
|
1137
|
+
</div>
|
1138
|
+
<p>This will create routing helpers such as <tt>magazine_periodical_ads_url</tt> and <tt>edit_magazine_periodical_ad_path</tt>.</p>
|
1139
|
+
<h3 id="inspecting-and-testing-routes">5 Inspecting and Testing Routes</h3>
|
1140
|
+
<p>Rails offers facilities for inspecting and testing your routes.</p>
|
1141
|
+
<h4 id="seeing-existing-routes-with-rake">5.1 Seeing Existing Routes with <tt>rake</tt></h4>
|
1142
|
+
<p>If you want a complete list of all of the available routes in your application, run <tt>rake routes</tt> command. This will print all of your routes, in the same order that they appear in <tt>routes.rb</tt>. For each route, you’ll see:</p>
|
1143
|
+
<ul>
|
1144
|
+
<li>The route name (if any)</li>
|
1145
|
+
<li>The <span class="caps">HTTP</span> verb used (if the route doesn’t respond to all verbs)</li>
|
1146
|
+
<li>The <span class="caps">URL</span> pattern to match</li>
|
1147
|
+
<li>The routing parameters for the route</li>
|
1148
|
+
</ul>
|
1149
|
+
<p>For example, here’s a small section of the <tt>rake routes</tt> output for a RESTful route:</p>
|
1150
|
+
<pre>
|
1151
|
+
users GET /users(.:format) users#index
|
1152
|
+
POST /users(.:format) users#create
|
1153
|
+
new_user GET /users/new(.:format) users#new
|
1154
|
+
edit_user GET /users/:id/edit(.:format) users#edit
|
1155
|
+
</pre>
|
1156
|
+
<p>You may restrict the listing to the routes that map to a particular controller setting the <tt>CONTROLLER</tt> environment variable:</p>
|
1157
|
+
<div class="code_container">
|
1158
|
+
<pre class="brush: plain; gutter: false; toolbar: false">
|
1159
|
+
$ CONTROLLER=users rake routes
|
1160
|
+
</pre>
|
1161
|
+
</div>
|
1162
|
+
<div class="info"><p>You’ll find that the output from <tt>rake routes</tt> is much more readable if you widen your terminal window until the output lines don’t wrap.</p></div>
|
1163
|
+
<h4 id="testing-routes">5.2 Testing Routes</h4>
|
1164
|
+
<p>Routes should be included in your testing strategy (just like the rest of your application). Rails offers three <a href="http://api.rubyonrails.org/classes/ActionDispatch/Assertions/RoutingAssertions.html">built-in assertions</a> designed to make testing routes simpler:</p>
|
1165
|
+
<ul>
|
1166
|
+
<li><tt>assert_generates</tt></li>
|
1167
|
+
<li><tt>assert_recognizes</tt></li>
|
1168
|
+
<li><tt>assert_routing</tt></li>
|
1169
|
+
</ul>
|
1170
|
+
<h5 id="the-assert_generates-assertion">5.2.1 The <tt>assert_generates</tt> Assertion</h5>
|
1171
|
+
<p><tt>assert_generates</tt> asserts that a particular set of options generate a particular path and can be used with default routes or custom routes.</p>
|
1172
|
+
<div class="code_container">
|
1173
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1174
|
+
assert_generates "/photos/1", { :controller => "photos", :action => "show", :id => "1" }
|
1175
|
+
assert_generates "/about", :controller => "pages", :action => "about"
|
1176
|
+
</pre>
|
1177
|
+
</div>
|
1178
|
+
<h5 id="the-assert_recognizes-assertion">5.2.2 The <tt>assert_recognizes</tt> Assertion</h5>
|
1179
|
+
<p><tt>assert_recognizes</tt> is the inverse of <tt>assert_generates</tt>. It asserts that a given path is recognized and routes it to a particular spot in your application.</p>
|
1180
|
+
<div class="code_container">
|
1181
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1182
|
+
assert_recognizes({ :controller => "photos", :action => "show", :id => "1" }, "/photos/1")
|
1183
|
+
</pre>
|
1184
|
+
</div>
|
1185
|
+
<p>You can supply a <tt>:method</tt> argument to specify the <span class="caps">HTTP</span> verb:</p>
|
1186
|
+
<div class="code_container">
|
1187
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1188
|
+
assert_recognizes({ :controller => "photos", :action => "create" }, { :path => "photos", :method => :post })
|
1189
|
+
</pre>
|
1190
|
+
</div>
|
1191
|
+
<h5 id="the-assert_routing-assertion">5.2.3 The <tt>assert_routing</tt> Assertion</h5>
|
1192
|
+
<p>The <tt>assert_routing</tt> assertion checks the route both ways: it tests that the path generates the options, and that the options generate the path. Thus, it combines the functions of <tt>assert_generates</tt> and <tt>assert_recognizes</tt>.</p>
|
1193
|
+
<div class="code_container">
|
1194
|
+
<pre class="brush: ruby; gutter: false; toolbar: false">
|
1195
|
+
assert_routing({ :path => "photos", :method => :post }, { :controller => "photos", :action => "create" })
|
1196
|
+
</pre>
|
1197
|
+
</div>
|
1198
|
+
|
1199
|
+
<h3>Feedback</h3>
|
1200
|
+
<p>
|
1201
|
+
You're encouraged to help improve the quality of this guide.
|
1202
|
+
</p>
|
1203
|
+
<p>
|
1204
|
+
If you see any typos or factual errors you are confident to
|
1205
|
+
patch, please clone <a href="https://github.com/lifo/docrails">docrails</a>
|
1206
|
+
and push the change yourself. That branch of Rails has public write access.
|
1207
|
+
Commits are still reviewed, but that happens after you've submitted your
|
1208
|
+
contribution. <a href="https://github.com/lifo/docrails">docrails</a> is
|
1209
|
+
cross-merged with master periodically.
|
1210
|
+
</p>
|
1211
|
+
<p>
|
1212
|
+
You may also find incomplete content, or stuff that is not up to date.
|
1213
|
+
Please do add any missing documentation for master. Check the
|
1214
|
+
<a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
|
1215
|
+
for style and conventions.
|
1216
|
+
</p>
|
1217
|
+
<p>
|
1218
|
+
If for whatever reason you spot something to fix but cannot patch it yourself, please
|
1219
|
+
<a href="https://github.com/rails/rails/issues">open an issue</a>.
|
1220
|
+
</p>
|
1221
|
+
<p>And last but not least, any kind of discussion regarding Ruby on Rails
|
1222
|
+
documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
|
1223
|
+
</p>
|
1224
|
+
</div>
|
1225
|
+
</div>
|
1226
|
+
</div>
|
1227
|
+
|
1228
|
+
<hr class="hide" />
|
1229
|
+
<div id="footer">
|
1230
|
+
<div class="wrapper">
|
1231
|
+
<p>This work is licensed under a <a href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-Share Alike 3.0</a> License</p>
|
1232
|
+
<p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
|
1233
|
+
</div>
|
1234
|
+
</div>
|
1235
|
+
|
1236
|
+
<script type="text/javascript" src="javascripts/guides.js"></script>
|
1237
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
|
1238
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
|
1239
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
|
1240
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
|
1241
|
+
<script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
|
1242
|
+
<script type="text/javascript">
|
1243
|
+
SyntaxHighlighter.all()
|
1244
|
+
</script>
|
1245
|
+
</body>
|
1246
|
+
</html>
|