rails 4.2.0.beta3 → 4.2.0.beta4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -7
  3. data/guides/output/2_2_release_notes.html +724 -0
  4. data/guides/output/2_3_release_notes.html +870 -0
  5. data/guides/output/3_0_release_notes.html +773 -0
  6. data/guides/output/3_1_release_notes.html +740 -0
  7. data/guides/output/3_2_release_notes.html +797 -0
  8. data/guides/output/4_0_release_notes.html +523 -0
  9. data/guides/output/4_1_release_notes.html +806 -0
  10. data/guides/output/4_2_release_notes.html +728 -0
  11. data/guides/output/Gemfile +6 -0
  12. data/guides/output/_license.html +226 -0
  13. data/guides/output/_welcome.html +233 -0
  14. data/guides/output/action_controller_overview.html +1335 -0
  15. data/guides/output/action_mailer_basics.html +928 -0
  16. data/guides/output/action_view_overview.html +1509 -0
  17. data/guides/output/active_job_basics.html +546 -0
  18. data/guides/output/active_model_basics.html +438 -0
  19. data/guides/output/active_record_basics.html +594 -0
  20. data/guides/output/active_record_callbacks.html +592 -0
  21. data/guides/output/active_record_migrations.html +1123 -0
  22. data/guides/output/active_record_postgresql.html +675 -0
  23. data/guides/output/active_record_querying.html +1796 -0
  24. data/guides/output/active_record_validations.html +1301 -0
  25. data/guides/output/active_support_core_extensions.html +3450 -0
  26. data/guides/output/active_support_instrumentation.html +1121 -0
  27. data/guides/output/api_documentation_guidelines.html +498 -0
  28. data/guides/output/asset_pipeline.html +1167 -0
  29. data/guides/output/association_basics.html +2086 -0
  30. data/guides/output/caching_with_rails.html +553 -0
  31. data/guides/output/command_line.html +791 -0
  32. data/guides/output/configuring.html +1055 -0
  33. data/guides/output/contributing_to_ruby_on_rails.html +657 -0
  34. data/guides/output/credits.html +284 -0
  35. data/guides/output/debugging_rails_applications.html +1014 -0
  36. data/guides/output/development_dependencies_install.html +478 -0
  37. data/guides/output/engines.html +1438 -0
  38. data/guides/output/form_helpers.html +1074 -0
  39. data/guides/output/generators.html +838 -0
  40. data/guides/output/getting_started.html +2092 -0
  41. data/guides/output/i18n.html +1198 -0
  42. data/guides/output/images/akshaysurve.jpg +0 -0
  43. data/guides/output/images/belongs_to.png +0 -0
  44. data/guides/output/images/book_icon.gif +0 -0
  45. data/guides/output/images/bullet.gif +0 -0
  46. data/guides/output/images/chapters_icon.gif +0 -0
  47. data/guides/output/images/check_bullet.gif +0 -0
  48. data/guides/output/images/credits_pic_blank.gif +0 -0
  49. data/guides/output/images/csrf.png +0 -0
  50. data/guides/output/images/edge_badge.png +0 -0
  51. data/guides/output/images/favicon.ico +0 -0
  52. data/guides/output/images/feature_tile.gif +0 -0
  53. data/guides/output/images/footer_tile.gif +0 -0
  54. data/guides/output/images/fxn.png +0 -0
  55. data/guides/output/images/getting_started/article_with_comments.png +0 -0
  56. data/guides/output/images/getting_started/challenge.png +0 -0
  57. data/guides/output/images/getting_started/confirm_dialog.png +0 -0
  58. data/guides/output/images/getting_started/forbidden_attributes_for_new_article.png +0 -0
  59. data/guides/output/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  60. data/guides/output/images/getting_started/form_with_errors.png +0 -0
  61. data/guides/output/images/getting_started/index_action_with_edit_link.png +0 -0
  62. data/guides/output/images/getting_started/new_article.png +0 -0
  63. data/guides/output/images/getting_started/new_post.png +0 -0
  64. data/guides/output/images/getting_started/post_with_comments.png +0 -0
  65. data/guides/output/images/getting_started/rails_welcome.png +0 -0
  66. data/guides/output/images/getting_started/routing_error_no_controller.png +0 -0
  67. data/guides/output/images/getting_started/routing_error_no_route_matches.png +0 -0
  68. data/guides/output/images/getting_started/show_action_for_articles.png +0 -0
  69. data/guides/output/images/getting_started/show_action_for_posts.png +0 -0
  70. data/guides/output/images/getting_started/template_is_missing_articles_new.png +0 -0
  71. data/guides/output/images/getting_started/template_is_missing_posts_new.png +0 -0
  72. data/guides/output/images/getting_started/undefined_method_post_path.png +0 -0
  73. data/guides/output/images/getting_started/unknown_action_create_for_articles.png +0 -0
  74. data/guides/output/images/getting_started/unknown_action_create_for_posts.png +0 -0
  75. data/guides/output/images/getting_started/unknown_action_new_for_articles.png +0 -0
  76. data/guides/output/images/getting_started/unknown_action_new_for_posts.png +0 -0
  77. data/guides/output/images/grey_bullet.gif +0 -0
  78. data/guides/output/images/habtm.png +0 -0
  79. data/guides/output/images/has_many.png +0 -0
  80. data/guides/output/images/has_many_through.png +0 -0
  81. data/guides/output/images/has_one.png +0 -0
  82. data/guides/output/images/has_one_through.png +0 -0
  83. data/guides/output/images/header_backdrop.png +0 -0
  84. data/guides/output/images/header_tile.gif +0 -0
  85. data/guides/output/images/i18n/demo_html_safe.png +0 -0
  86. data/guides/output/images/i18n/demo_localized_pirate.png +0 -0
  87. data/guides/output/images/i18n/demo_translated_en.png +0 -0
  88. data/guides/output/images/i18n/demo_translated_pirate.png +0 -0
  89. data/guides/output/images/i18n/demo_translation_missing.png +0 -0
  90. data/guides/output/images/i18n/demo_untranslated.png +0 -0
  91. data/guides/output/images/icons/README +5 -0
  92. data/guides/output/images/icons/callouts/1.png +0 -0
  93. data/guides/output/images/icons/callouts/10.png +0 -0
  94. data/guides/output/images/icons/callouts/11.png +0 -0
  95. data/guides/output/images/icons/callouts/12.png +0 -0
  96. data/guides/output/images/icons/callouts/13.png +0 -0
  97. data/guides/output/images/icons/callouts/14.png +0 -0
  98. data/guides/output/images/icons/callouts/15.png +0 -0
  99. data/guides/output/images/icons/callouts/2.png +0 -0
  100. data/guides/output/images/icons/callouts/3.png +0 -0
  101. data/guides/output/images/icons/callouts/4.png +0 -0
  102. data/guides/output/images/icons/callouts/5.png +0 -0
  103. data/guides/output/images/icons/callouts/6.png +0 -0
  104. data/guides/output/images/icons/callouts/7.png +0 -0
  105. data/guides/output/images/icons/callouts/8.png +0 -0
  106. data/guides/output/images/icons/callouts/9.png +0 -0
  107. data/guides/output/images/icons/caution.png +0 -0
  108. data/guides/output/images/icons/example.png +0 -0
  109. data/guides/output/images/icons/home.png +0 -0
  110. data/guides/output/images/icons/important.png +0 -0
  111. data/guides/output/images/icons/next.png +0 -0
  112. data/guides/output/images/icons/note.png +0 -0
  113. data/guides/output/images/icons/prev.png +0 -0
  114. data/guides/output/images/icons/tip.png +0 -0
  115. data/guides/output/images/icons/up.png +0 -0
  116. data/guides/output/images/icons/warning.png +0 -0
  117. data/guides/output/images/nav_arrow.gif +0 -0
  118. data/guides/output/images/oscardelben.jpg +0 -0
  119. data/guides/output/images/polymorphic.png +0 -0
  120. data/guides/output/images/radar.png +0 -0
  121. data/guides/output/images/rails4_features.png +0 -0
  122. data/guides/output/images/rails_guides_kindle_cover.jpg +0 -0
  123. data/guides/output/images/rails_guides_logo.gif +0 -0
  124. data/guides/output/images/rails_logo_remix.gif +0 -0
  125. data/guides/output/images/session_fixation.png +0 -0
  126. data/guides/output/images/tab_grey.gif +0 -0
  127. data/guides/output/images/tab_info.gif +0 -0
  128. data/guides/output/images/tab_note.gif +0 -0
  129. data/guides/output/images/tab_red.gif +0 -0
  130. data/guides/output/images/tab_yellow.gif +0 -0
  131. data/guides/output/images/tab_yellow.png +0 -0
  132. data/guides/output/images/vijaydev.jpg +0 -0
  133. data/guides/output/index.html +354 -0
  134. data/guides/output/initialization.html +876 -0
  135. data/guides/output/javascripts/guides.js +59 -0
  136. data/guides/output/javascripts/jquery.min.js +4 -0
  137. data/guides/output/javascripts/responsive-tables.js +43 -0
  138. data/guides/output/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  139. data/guides/output/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  140. data/guides/output/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  141. data/guides/output/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  142. data/guides/output/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  143. data/guides/output/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  144. data/guides/output/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  145. data/guides/output/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  146. data/guides/output/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  147. data/guides/output/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  148. data/guides/output/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  149. data/guides/output/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  150. data/guides/output/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  151. data/guides/output/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  152. data/guides/output/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  153. data/guides/output/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  154. data/guides/output/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  155. data/guides/output/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  156. data/guides/output/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  157. data/guides/output/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  158. data/guides/output/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  159. data/guides/output/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  160. data/guides/output/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  161. data/guides/output/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  162. data/guides/output/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  163. data/guides/output/javascripts/syntaxhighlighter/shCore.js +17 -0
  164. data/guides/output/layout.html +448 -0
  165. data/guides/output/layouts_and_rendering.html +1541 -0
  166. data/guides/output/maintenance_policy.html +257 -0
  167. data/guides/output/migrations.html +1360 -0
  168. data/guides/output/nested_model_forms.html +412 -0
  169. data/guides/output/plugins.html +644 -0
  170. data/guides/output/rails_application_templates.html +450 -0
  171. data/guides/output/rails_on_rack.html +547 -0
  172. data/guides/output/routing.html +1631 -0
  173. data/guides/output/ruby_on_rails_guides_guidelines.html +329 -0
  174. data/guides/output/security.html +935 -0
  175. data/guides/output/stylesheets/fixes.css +16 -0
  176. data/guides/output/stylesheets/kindle.css +11 -0
  177. data/guides/output/stylesheets/main.css +713 -0
  178. data/guides/output/stylesheets/print.css +52 -0
  179. data/guides/output/stylesheets/reset.css +43 -0
  180. data/guides/output/stylesheets/responsive-tables.css +50 -0
  181. data/guides/output/stylesheets/style.css +13 -0
  182. data/guides/output/stylesheets/syntaxhighlighter/shCore.css +226 -0
  183. data/guides/output/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  184. data/guides/output/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  185. data/guides/output/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  186. data/guides/output/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  187. data/guides/output/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  188. data/guides/output/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  189. data/guides/output/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  190. data/guides/output/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  191. data/guides/output/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  192. data/guides/output/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  193. data/guides/output/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  194. data/guides/output/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  195. data/guides/output/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  196. data/guides/output/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  197. data/guides/output/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  198. data/guides/output/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  199. data/guides/output/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  200. data/guides/output/testing.html +1350 -0
  201. data/guides/output/upgrading_ruby_on_rails.html +1135 -0
  202. data/guides/output/working_with_javascript_in_rails.html +587 -0
  203. data/guides/source/4_0_release_notes.md +2 -2
  204. data/guides/source/4_2_release_notes.md +9 -2
  205. data/guides/source/action_controller_overview.md +3 -1
  206. data/guides/source/action_mailer_basics.md +1 -2
  207. data/guides/source/active_job_basics.md +25 -2
  208. data/guides/source/active_model_basics.md +350 -19
  209. data/guides/source/active_record_basics.md +2 -2
  210. data/guides/source/active_record_migrations.md +1 -1
  211. data/guides/source/active_record_validations.md +12 -4
  212. data/guides/source/active_support_core_extensions.md +7 -9
  213. data/guides/source/active_support_instrumentation.md +2 -0
  214. data/guides/source/association_basics.md +7 -7
  215. data/guides/source/configuring.md +24 -0
  216. data/guides/source/contributing_to_ruby_on_rails.md +1 -1
  217. data/guides/source/credits.html.erb +1 -1
  218. data/guides/source/documents.yaml +5 -0
  219. data/guides/source/engines.md +2 -2
  220. data/guides/source/form_helpers.md +6 -0
  221. data/guides/source/i18n.md +6 -9
  222. data/guides/source/layouts_and_rendering.md +1 -1
  223. data/guides/source/plugins.md +2 -2
  224. data/guides/source/rails_on_rack.md +4 -0
  225. data/guides/source/testing.md +1 -0
  226. data/guides/source/upgrading_ruby_on_rails.md +5 -5
  227. metadata +227 -27
@@ -0,0 +1,1631 @@
1
+ <!DOCTYPE html>
2
+
3
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4
+ <head>
5
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
6
+ <meta name="viewport" content="width=device-width, initial-scale=1"/>
7
+
8
+ <title>Rails Routing from the Outside In — Ruby on Rails Guides</title>
9
+ <link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
10
+ <link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print" />
11
+
12
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shCore.css" />
13
+ <link rel="stylesheet" type="text/css" href="stylesheets/syntaxhighlighter/shThemeRailsGuides.css" />
14
+
15
+ <link rel="stylesheet" type="text/css" href="stylesheets/fixes.css" />
16
+
17
+ <link href="images/favicon.ico" rel="shortcut icon" type="image/x-icon" />
18
+ </head>
19
+ <body class="guide">
20
+ <div id="topNav">
21
+ <div class="wrapper">
22
+ <strong class="more-info-label">More at <a href="http://rubyonrails.org/">rubyonrails.org:</a> </strong>
23
+ <span class="red-button more-info-button">
24
+ More Ruby on Rails
25
+ </span>
26
+ <ul class="more-info-links s-hidden">
27
+ <li class="more-info"><a href="http://rubyonrails.org/">Overview</a></li>
28
+ <li class="more-info"><a href="http://rubyonrails.org/download">Download</a></li>
29
+ <li class="more-info"><a href="http://rubyonrails.org/deploy">Deploy</a></li>
30
+ <li class="more-info"><a href="https://github.com/rails/rails">Code</a></li>
31
+ <li class="more-info"><a href="http://rubyonrails.org/screencasts">Screencasts</a></li>
32
+ <li class="more-info"><a href="http://rubyonrails.org/documentation">Documentation</a></li>
33
+ <li class="more-info"><a href="http://rubyonrails.org/community">Community</a></li>
34
+ <li class="more-info"><a href="http://weblog.rubyonrails.org/">Blog</a></li>
35
+ </ul>
36
+ </div>
37
+ </div>
38
+ <div id="header">
39
+ <div class="wrapper clearfix">
40
+ <h1><a href="index.html" title="Return to home page">Guides.rubyonrails.org</a></h1>
41
+ <ul class="nav">
42
+ <li><a class="nav-item" href="index.html">Home</a></li>
43
+ <li class="guides-index guides-index-large">
44
+ <a href="index.html" id="guidesMenu" class="guides-index-item nav-item">Guides Index</a>
45
+ <div id="guides" class="clearfix" style="display: none;">
46
+ <hr />
47
+ <dl class="L">
48
+ <dt>Start Here</dt>
49
+ <dd><a href="getting_started.html">Getting Started with Rails</a></dd>
50
+ <dt>Models</dt>
51
+ <dd><a href="active_record_basics.html">Active Record Basics</a></dd>
52
+ <dd><a href="active_record_migrations.html">Active Record Migrations</a></dd>
53
+ <dd><a href="active_record_validations.html">Active Record Validations</a></dd>
54
+ <dd><a href="active_record_callbacks.html">Active Record Callbacks</a></dd>
55
+ <dd><a href="association_basics.html">Active Record Associations</a></dd>
56
+ <dd><a href="active_record_querying.html">Active Record Query Interface</a></dd>
57
+ <dt>Views</dt>
58
+ <dd><a href="layouts_and_rendering.html">Layouts and Rendering in Rails</a></dd>
59
+ <dd><a href="form_helpers.html">Action View Form Helpers</a></dd>
60
+ <dt>Controllers</dt>
61
+ <dd><a href="action_controller_overview.html">Action Controller Overview</a></dd>
62
+ <dd><a href="routing.html">Rails Routing from the Outside In</a></dd>
63
+ </dl>
64
+ <dl class="R">
65
+ <dt>Digging Deeper</dt>
66
+ <dd><a href="active_support_core_extensions.html">Active Support Core Extensions</a></dd>
67
+ <dd><a href="i18n.html">Rails Internationalization API</a></dd>
68
+ <dd><a href="action_mailer_basics.html">Action Mailer Basics</a></dd>
69
+ <dd><a href="active_job_basics.html">Active Job Basics</a></dd>
70
+ <dd><a href="security.html">Securing Rails Applications</a></dd>
71
+ <dd><a href="debugging_rails_applications.html">Debugging Rails Applications</a></dd>
72
+ <dd><a href="configuring.html">Configuring Rails Applications</a></dd>
73
+ <dd><a href="command_line.html">Rails Command Line Tools and Rake Tasks</a></dd>
74
+ <dd><a href="asset_pipeline.html">Asset Pipeline</a></dd>
75
+ <dd><a href="working_with_javascript_in_rails.html">Working with JavaScript in Rails</a></dd>
76
+ <dt>Extending Rails</dt>
77
+ <dd><a href="rails_on_rack.html">Rails on Rack</a></dd>
78
+ <dd><a href="generators.html">Creating and Customizing Rails Generators</a></dd>
79
+ <dt>Contributing to Ruby on Rails</dt>
80
+ <dd><a href="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</a></dd>
81
+ <dd><a href="api_documentation_guidelines.html">API Documentation Guidelines</a></dd>
82
+ <dd><a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a></dd>
83
+ <dt>Maintenance Policy</dt>
84
+ <dd><a href="maintenance_policy.html">Maintenance Policy</a></dd>
85
+ <dt>Release Notes</dt>
86
+ <dd><a href="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</a></dd>
87
+ <dd><a href="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</a></dd>
88
+ <dd><a href="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</a></dd>
89
+ <dd><a href="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</a></dd>
90
+ <dd><a href="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</a></dd>
91
+ <dd><a href="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</a></dd>
92
+ <dd><a href="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</a></dd>
93
+ <dd><a href="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</a></dd>
94
+ </dl>
95
+ </div>
96
+ </li>
97
+ <li><a class="nav-item" href="contributing_to_ruby_on_rails.html">Contribute</a></li>
98
+ <li><a class="nav-item" href="credits.html">Credits</a></li>
99
+ <li class="guides-index guides-index-small">
100
+ <select class="guides-index-item nav-item">
101
+ <option value="index.html">Guides Index</option>
102
+ <optgroup label="Start Here">
103
+ <option value="getting_started.html">Getting Started with Rails</option>
104
+ </optgroup>
105
+ <optgroup label="Models">
106
+ <option value="active_record_basics.html">Active Record Basics</option>
107
+ <option value="active_record_migrations.html">Active Record Migrations</option>
108
+ <option value="active_record_validations.html">Active Record Validations</option>
109
+ <option value="active_record_callbacks.html">Active Record Callbacks</option>
110
+ <option value="association_basics.html">Active Record Associations</option>
111
+ <option value="active_record_querying.html">Active Record Query Interface</option>
112
+ </optgroup>
113
+ <optgroup label="Views">
114
+ <option value="layouts_and_rendering.html">Layouts and Rendering in Rails</option>
115
+ <option value="form_helpers.html">Action View Form Helpers</option>
116
+ </optgroup>
117
+ <optgroup label="Controllers">
118
+ <option value="action_controller_overview.html">Action Controller Overview</option>
119
+ <option value="routing.html">Rails Routing from the Outside In</option>
120
+ </optgroup>
121
+ <optgroup label="Digging Deeper">
122
+ <option value="active_support_core_extensions.html">Active Support Core Extensions</option>
123
+ <option value="i18n.html">Rails Internationalization API</option>
124
+ <option value="action_mailer_basics.html">Action Mailer Basics</option>
125
+ <option value="active_job_basics.html">Active Job Basics</option>
126
+ <option value="security.html">Securing Rails Applications</option>
127
+ <option value="debugging_rails_applications.html">Debugging Rails Applications</option>
128
+ <option value="configuring.html">Configuring Rails Applications</option>
129
+ <option value="command_line.html">Rails Command Line Tools and Rake Tasks</option>
130
+ <option value="asset_pipeline.html">Asset Pipeline</option>
131
+ <option value="working_with_javascript_in_rails.html">Working with JavaScript in Rails</option>
132
+ </optgroup>
133
+ <optgroup label="Extending Rails">
134
+ <option value="rails_on_rack.html">Rails on Rack</option>
135
+ <option value="generators.html">Creating and Customizing Rails Generators</option>
136
+ </optgroup>
137
+ <optgroup label="Contributing to Ruby on Rails">
138
+ <option value="contributing_to_ruby_on_rails.html">Contributing to Ruby on Rails</option>
139
+ <option value="api_documentation_guidelines.html">API Documentation Guidelines</option>
140
+ <option value="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</option>
141
+ </optgroup>
142
+ <optgroup label="Maintenance Policy">
143
+ <option value="maintenance_policy.html">Maintenance Policy</option>
144
+ </optgroup>
145
+ <optgroup label="Release Notes">
146
+ <option value="upgrading_ruby_on_rails.html">Upgrading Ruby on Rails</option>
147
+ <option value="4_1_release_notes.html">Ruby on Rails 4.1 Release Notes</option>
148
+ <option value="4_0_release_notes.html">Ruby on Rails 4.0 Release Notes</option>
149
+ <option value="3_2_release_notes.html">Ruby on Rails 3.2 Release Notes</option>
150
+ <option value="3_1_release_notes.html">Ruby on Rails 3.1 Release Notes</option>
151
+ <option value="3_0_release_notes.html">Ruby on Rails 3.0 Release Notes</option>
152
+ <option value="2_3_release_notes.html">Ruby on Rails 2.3 Release Notes</option>
153
+ <option value="2_2_release_notes.html">Ruby on Rails 2.2 Release Notes</option>
154
+ </optgroup>
155
+ </select>
156
+ </li>
157
+ </ul>
158
+ </div>
159
+ </div>
160
+ <hr class="hide" />
161
+
162
+ <div id="feature">
163
+ <div class="wrapper">
164
+ <h2>Rails Routing from the Outside In</h2><p>This guide covers the user-facing features of Rails routing.</p><p>After reading this guide, you will know:</p>
165
+ <ul>
166
+ <li>How to interpret the code in <code>routes.rb</code>.</li>
167
+ <li>How to construct your own routes, using either the preferred resourceful style or the <code>match</code> method.</li>
168
+ <li>What parameters to expect an action to receive.</li>
169
+ <li>How to automatically create paths and URLs using route helpers.</li>
170
+ <li>Advanced techniques such as constraints and Rack endpoints.</li>
171
+ </ul>
172
+
173
+
174
+ <div id="subCol">
175
+ <h3 class="chapter"><img src="images/chapters_icon.gif" alt="" />Chapters</h3>
176
+ <ol class="chapters">
177
+ <li>
178
+ <a href="#the-purpose-of-the-rails-router">The Purpose of the Rails Router</a>
179
+
180
+ <ul>
181
+ <li><a href="#connecting-urls-to-code">Connecting URLs to Code</a></li>
182
+ <li><a href="#generating-paths-and-urls-from-code">Generating Paths and URLs from Code</a></li>
183
+ </ul>
184
+ </li>
185
+ <li>
186
+ <a href="#resource-routing:-the-rails-default">Resource Routing: the Rails Default</a>
187
+
188
+ <ul>
189
+ <li><a href="#resources-on-the-web">Resources on the Web</a></li>
190
+ <li><a href="#crud,-verbs,-and-actions">CRUD, Verbs, and Actions</a></li>
191
+ <li><a href="#path-and-url-helpers">Path and URL Helpers</a></li>
192
+ <li><a href="#defining-multiple-resources-at-the-same-time">Defining Multiple Resources at the Same Time</a></li>
193
+ <li><a href="#singular-resources">Singular Resources</a></li>
194
+ <li><a href="#controller-namespaces-and-routing">Controller Namespaces and Routing</a></li>
195
+ <li><a href="#nested-resources">Nested Resources</a></li>
196
+ <li><a href="#routing-concerns">Routing concerns</a></li>
197
+ <li><a href="#creating-paths-and-urls-from-objects">Creating Paths and URLs From Objects</a></li>
198
+ <li><a href="#adding-more-restful-actions">Adding More RESTful Actions</a></li>
199
+ </ul>
200
+ </li>
201
+ <li>
202
+ <a href="#non-resourceful-routes">Non-Resourceful Routes</a>
203
+
204
+ <ul>
205
+ <li><a href="#bound-parameters">Bound Parameters</a></li>
206
+ <li><a href="#dynamic-segments">Dynamic Segments</a></li>
207
+ <li><a href="#static-segments">Static Segments</a></li>
208
+ <li><a href="#the-query-string">The Query String</a></li>
209
+ <li><a href="#defining-defaults">Defining Defaults</a></li>
210
+ <li><a href="#naming-routes">Naming Routes</a></li>
211
+ <li><a href="#http-verb-constraints">HTTP Verb Constraints</a></li>
212
+ <li><a href="#segment-constraints">Segment Constraints</a></li>
213
+ <li><a href="#request-based-constraints">Request-Based Constraints</a></li>
214
+ <li><a href="#advanced-constraints">Advanced Constraints</a></li>
215
+ <li><a href="#route-globbing-and-wildcard-segments">Route Globbing and Wildcard Segments</a></li>
216
+ <li><a href="#redirection">Redirection</a></li>
217
+ <li><a href="#routing-to-rack-applications">Routing to Rack Applications</a></li>
218
+ <li><a href="#using-root">Using <code>root</code></a></li>
219
+ <li><a href="#unicode-character-routes">Unicode character routes</a></li>
220
+ </ul>
221
+ </li>
222
+ <li>
223
+ <a href="#customizing-resourceful-routes">Customizing Resourceful Routes</a>
224
+
225
+ <ul>
226
+ <li><a href="#specifying-a-controller-to-use">Specifying a Controller to Use</a></li>
227
+ <li><a href="#specifying-constraints">Specifying Constraints</a></li>
228
+ <li><a href="#overriding-the-named-helpers">Overriding the Named Helpers</a></li>
229
+ <li><a href="#overriding-the-new-and-edit-segments">Overriding the <code>new</code> and <code>edit</code> Segments</a></li>
230
+ <li><a href="#prefixing-the-named-route-helpers">Prefixing the Named Route Helpers</a></li>
231
+ <li><a href="#restricting-the-routes-created">Restricting the Routes Created</a></li>
232
+ <li><a href="#translated-paths">Translated Paths</a></li>
233
+ <li><a href="#overriding-the-singular-form">Overriding the Singular Form</a></li>
234
+ <li><a href="#using-:as-in-nested-resources">Using <code>:as</code> in Nested Resources</a></li>
235
+ <li><a href="#overriding-named-route-parameters">Overriding Named Route Parameters</a></li>
236
+ </ul>
237
+ </li>
238
+ <li>
239
+ <a href="#inspecting-and-testing-routes">Inspecting and Testing Routes</a>
240
+
241
+ <ul>
242
+ <li><a href="#listing-existing-routes">Listing Existing Routes</a></li>
243
+ <li><a href="#testing-routes">Testing Routes</a></li>
244
+ </ul>
245
+ </li>
246
+ </ol>
247
+
248
+ </div>
249
+
250
+ </div>
251
+ </div>
252
+
253
+ <div id="container">
254
+ <div class="wrapper">
255
+ <div id="mainCol">
256
+ <h3 id="the-purpose-of-the-rails-router">1 The Purpose of the Rails Router</h3><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><h4 id="connecting-urls-to-code">1.1 Connecting URLs to Code</h4><p>When your Rails application receives an incoming request for:</p><div class="code_container">
257
+ <pre class="brush: plain; gutter: false; toolbar: false">
258
+ GET /patients/17
259
+
260
+ </pre>
261
+ </div>
262
+ <p>it asks the router to match it to a controller action. If the first matching route is:</p><div class="code_container">
263
+ <pre class="brush: ruby; gutter: false; toolbar: false">
264
+ get '/patients/:id', to: 'patients#show'
265
+
266
+ </pre>
267
+ </div>
268
+ <p>the request is dispatched to the <code>patients</code> controller's <code>show</code> action with <code>{ id: '17' }</code> in <code>params</code>.</p><h4 id="generating-paths-and-urls-from-code">1.2 Generating Paths and URLs from Code</h4><p>You can also generate paths and URLs. If the route above is modified to be:</p><div class="code_container">
269
+ <pre class="brush: ruby; gutter: false; toolbar: false">
270
+ get '/patients/:id', to: 'patients#show', as: 'patient'
271
+
272
+ </pre>
273
+ </div>
274
+ <p>and your application contains this code in the controller:</p><div class="code_container">
275
+ <pre class="brush: ruby; gutter: false; toolbar: false">
276
+ @patient = Patient.find(17)
277
+
278
+ </pre>
279
+ </div>
280
+ <p>and this in the corresponding view:</p><div class="code_container">
281
+ <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
282
+ &lt;%= link_to 'Patient Record', patient_path(@patient) %&gt;
283
+
284
+ </pre>
285
+ </div>
286
+ <p>then the router will generate the path <code>/patients/17</code>. 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><h3 id="resource-routing:-the-rails-default">2 Resource Routing: the Rails Default</h3><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 <code>index</code>, <code>show</code>, <code>new</code>, <code>edit</code>, <code>create</code>, <code>update</code> and <code>destroy</code> actions, a resourceful route declares them in a single line of code.</p><h4 id="resources-on-the-web">2.1 Resources on the Web</h4><p>Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as <code>GET</code>, <code>POST</code>, <code>PATCH</code>, <code>PUT</code> and <code>DELETE</code>. 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><p>When your Rails application receives an incoming request for:</p><div class="code_container">
287
+ <pre class="brush: plain; gutter: false; toolbar: false">
288
+ DELETE /photos/17
289
+
290
+ </pre>
291
+ </div>
292
+ <p>it asks the router to map it to a controller action. If the first matching route is:</p><div class="code_container">
293
+ <pre class="brush: ruby; gutter: false; toolbar: false">
294
+ resources :photos
295
+
296
+ </pre>
297
+ </div>
298
+ <p>Rails would dispatch that request to the <code>destroy</code> method on the <code>photos</code> controller with <code>{ id: '17' }</code> in <code>params</code>.</p><h4 id="crud,-verbs,-and-actions">2.2 CRUD, Verbs, and Actions</h4><p>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:</p><div class="code_container">
299
+ <pre class="brush: ruby; gutter: false; toolbar: false">
300
+ resources :photos
301
+
302
+ </pre>
303
+ </div>
304
+ <p>creates seven different routes in your application, all mapping to the <code>Photos</code> controller:</p>
305
+ <table>
306
+ <thead>
307
+ <tr>
308
+ <th>HTTP Verb</th>
309
+ <th>Path</th>
310
+ <th>Controller#Action</th>
311
+ <th>Used for</th>
312
+ </tr>
313
+ </thead>
314
+ <tbody>
315
+ <tr>
316
+ <td>GET</td>
317
+ <td>/photos</td>
318
+ <td>photos#index</td>
319
+ <td>display a list of all photos</td>
320
+ </tr>
321
+ <tr>
322
+ <td>GET</td>
323
+ <td>/photos/new</td>
324
+ <td>photos#new</td>
325
+ <td>return an HTML form for creating a new photo</td>
326
+ </tr>
327
+ <tr>
328
+ <td>POST</td>
329
+ <td>/photos</td>
330
+ <td>photos#create</td>
331
+ <td>create a new photo</td>
332
+ </tr>
333
+ <tr>
334
+ <td>GET</td>
335
+ <td>/photos/:id</td>
336
+ <td>photos#show</td>
337
+ <td>display a specific photo</td>
338
+ </tr>
339
+ <tr>
340
+ <td>GET</td>
341
+ <td>/photos/:id/edit</td>
342
+ <td>photos#edit</td>
343
+ <td>return an HTML form for editing a photo</td>
344
+ </tr>
345
+ <tr>
346
+ <td>PATCH/PUT</td>
347
+ <td>/photos/:id</td>
348
+ <td>photos#update</td>
349
+ <td>update a specific photo</td>
350
+ </tr>
351
+ <tr>
352
+ <td>DELETE</td>
353
+ <td>/photos/:id</td>
354
+ <td>photos#destroy</td>
355
+ <td>delete a specific photo</td>
356
+ </tr>
357
+ </tbody>
358
+ </table>
359
+ <div class="note"><p>Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions.</p></div><div class="note"><p>Rails routes are matched in the order they are specified, so if you have a <code>resources :photos</code> above a <code>get 'photos/poll'</code> the <code>show</code> action's route for the <code>resources</code> line will be matched before the <code>get</code> line. To fix this, move the <code>get</code> line <strong>above</strong> the <code>resources</code> line so that it is matched first.</p></div><h4 id="path-and-url-helpers">2.3 Path and URL Helpers</h4><p>Creating a resourceful route will also expose a number of helpers to the controllers in your application. In the case of <code>resources :photos</code>:</p>
360
+ <ul>
361
+ <li>
362
+ <code>photos_path</code> returns <code>/photos</code>
363
+ </li>
364
+ <li>
365
+ <code>new_photo_path</code> returns <code>/photos/new</code>
366
+ </li>
367
+ <li>
368
+ <code>edit_photo_path(:id)</code> returns <code>/photos/:id/edit</code> (for instance, <code>edit_photo_path(10)</code> returns <code>/photos/10/edit</code>)</li>
369
+ <li>
370
+ <code>photo_path(:id)</code> returns <code>/photos/:id</code> (for instance, <code>photo_path(10)</code> returns <code>/photos/10</code>)</li>
371
+ </ul>
372
+ <p>Each of these helpers has a corresponding <code>_url</code> helper (such as <code>photos_url</code>) which returns the same path prefixed with the current host, port and path prefix.</p><h4 id="defining-multiple-resources-at-the-same-time">2.4 Defining Multiple Resources at the Same Time</h4><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 <code>resources</code>:</p><div class="code_container">
373
+ <pre class="brush: ruby; gutter: false; toolbar: false">
374
+ resources :photos, :books, :videos
375
+
376
+ </pre>
377
+ </div>
378
+ <p>This works exactly the same as:</p><div class="code_container">
379
+ <pre class="brush: ruby; gutter: false; toolbar: false">
380
+ resources :photos
381
+ resources :books
382
+ resources :videos
383
+
384
+ </pre>
385
+ </div>
386
+ <h4 id="singular-resources">2.5 Singular Resources</h4><p>Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like <code>/profile</code> to always show the profile of the currently logged in user. In this case, you can use a singular resource to map <code>/profile</code> (rather than <code>/profile/:id</code>) to the <code>show</code> action:</p><div class="code_container">
387
+ <pre class="brush: ruby; gutter: false; toolbar: false">
388
+ get 'profile', to: 'users#show'
389
+
390
+ </pre>
391
+ </div>
392
+ <p>Passing a <code>String</code> to <code>get</code> will expect a <code>controller#action</code> format, while passing a <code>Symbol</code> will map directly to an action:</p><div class="code_container">
393
+ <pre class="brush: ruby; gutter: false; toolbar: false">
394
+ get 'profile', to: :show
395
+
396
+ </pre>
397
+ </div>
398
+ <p>This resourceful route:</p><div class="code_container">
399
+ <pre class="brush: ruby; gutter: false; toolbar: false">
400
+ resource :geocoder
401
+
402
+ </pre>
403
+ </div>
404
+ <p>creates six different routes in your application, all mapping to the <code>Geocoders</code> controller:</p>
405
+ <table>
406
+ <thead>
407
+ <tr>
408
+ <th>HTTP Verb</th>
409
+ <th>Path</th>
410
+ <th>Controller#Action</th>
411
+ <th>Used for</th>
412
+ </tr>
413
+ </thead>
414
+ <tbody>
415
+ <tr>
416
+ <td>GET</td>
417
+ <td>/geocoder/new</td>
418
+ <td>geocoders#new</td>
419
+ <td>return an HTML form for creating the geocoder</td>
420
+ </tr>
421
+ <tr>
422
+ <td>POST</td>
423
+ <td>/geocoder</td>
424
+ <td>geocoders#create</td>
425
+ <td>create the new geocoder</td>
426
+ </tr>
427
+ <tr>
428
+ <td>GET</td>
429
+ <td>/geocoder</td>
430
+ <td>geocoders#show</td>
431
+ <td>display the one and only geocoder resource</td>
432
+ </tr>
433
+ <tr>
434
+ <td>GET</td>
435
+ <td>/geocoder/edit</td>
436
+ <td>geocoders#edit</td>
437
+ <td>return an HTML form for editing the geocoder</td>
438
+ </tr>
439
+ <tr>
440
+ <td>PATCH/PUT</td>
441
+ <td>/geocoder</td>
442
+ <td>geocoders#update</td>
443
+ <td>update the one and only geocoder resource</td>
444
+ </tr>
445
+ <tr>
446
+ <td>DELETE</td>
447
+ <td>/geocoder</td>
448
+ <td>geocoders#destroy</td>
449
+ <td>delete the geocoder resource</td>
450
+ </tr>
451
+ </tbody>
452
+ </table>
453
+ <div class="note"><p>Because you might want to use the same controller for a singular route (<code>/account</code>) and a plural route (<code>/accounts/45</code>), singular resources map to plural controllers. So that, for example, <code>resource :photo</code> and <code>resources :photos</code> creates both singular and plural routes that map to the same controller (<code>PhotosController</code>).</p></div><p>A singular resourceful route generates these helpers:</p>
454
+ <ul>
455
+ <li>
456
+ <code>new_geocoder_path</code> returns <code>/geocoder/new</code>
457
+ </li>
458
+ <li>
459
+ <code>edit_geocoder_path</code> returns <code>/geocoder/edit</code>
460
+ </li>
461
+ <li>
462
+ <code>geocoder_path</code> returns <code>/geocoder</code>
463
+ </li>
464
+ </ul>
465
+ <p>As with plural resources, the same helpers ending in <code>_url</code> will also include the host, port and path prefix.</p><div class="warning"><p>A <a href="https://github.com/rails/rails/issues/1769">long-standing bug</a> prevents <code>form_for</code> from working automatically with singular resources. As a workaround, specify the URL for the form directly, like so:</p></div><div class="code_container">
466
+ <pre class="brush: ruby; gutter: false; toolbar: false">
467
+ form_for @geocoder, url: geocoder_path do |f|
468
+
469
+ </pre>
470
+ </div>
471
+ <h4 id="controller-namespaces-and-routing">2.6 Controller Namespaces and Routing</h4><p>You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an <code>Admin::</code> namespace. You would place these controllers under the <code>app/controllers/admin</code> directory, and you can group them together in your router:</p><div class="code_container">
472
+ <pre class="brush: ruby; gutter: false; toolbar: false">
473
+ namespace :admin do
474
+ resources :articles, :comments
475
+ end
476
+
477
+ </pre>
478
+ </div>
479
+ <p>This will create a number of routes for each of the <code>articles</code> and <code>comments</code> controller. For <code>Admin::ArticlesController</code>, Rails will create:</p>
480
+ <table>
481
+ <thead>
482
+ <tr>
483
+ <th>HTTP Verb</th>
484
+ <th>Path</th>
485
+ <th>Controller#Action</th>
486
+ <th>Named Helper</th>
487
+ </tr>
488
+ </thead>
489
+ <tbody>
490
+ <tr>
491
+ <td>GET</td>
492
+ <td>/admin/articles</td>
493
+ <td>admin/articles#index</td>
494
+ <td>admin_articles_path</td>
495
+ </tr>
496
+ <tr>
497
+ <td>GET</td>
498
+ <td>/admin/articles/new</td>
499
+ <td>admin/articles#new</td>
500
+ <td>new_admin_article_path</td>
501
+ </tr>
502
+ <tr>
503
+ <td>POST</td>
504
+ <td>/admin/articles</td>
505
+ <td>admin/articles#create</td>
506
+ <td>admin_articles_path</td>
507
+ </tr>
508
+ <tr>
509
+ <td>GET</td>
510
+ <td>/admin/articles/:id</td>
511
+ <td>admin/articles#show</td>
512
+ <td>admin_article_path(:id)</td>
513
+ </tr>
514
+ <tr>
515
+ <td>GET</td>
516
+ <td>/admin/articles/:id/edit</td>
517
+ <td>admin/articles#edit</td>
518
+ <td>edit_admin_article_path(:id)</td>
519
+ </tr>
520
+ <tr>
521
+ <td>PATCH/PUT</td>
522
+ <td>/admin/articles/:id</td>
523
+ <td>admin/articles#update</td>
524
+ <td>admin_article_path(:id)</td>
525
+ </tr>
526
+ <tr>
527
+ <td>DELETE</td>
528
+ <td>/admin/articles/:id</td>
529
+ <td>admin/articles#destroy</td>
530
+ <td>admin_article_path(:id)</td>
531
+ </tr>
532
+ </tbody>
533
+ </table>
534
+ <p>If you want to route <code>/articles</code> (without the prefix <code>/admin</code>) to <code>Admin::ArticlesController</code>, you could use:</p><div class="code_container">
535
+ <pre class="brush: ruby; gutter: false; toolbar: false">
536
+ scope module: 'admin' do
537
+ resources :articles, :comments
538
+ end
539
+
540
+ </pre>
541
+ </div>
542
+ <p>or, for a single case:</p><div class="code_container">
543
+ <pre class="brush: ruby; gutter: false; toolbar: false">
544
+ resources :articles, module: 'admin'
545
+
546
+ </pre>
547
+ </div>
548
+ <p>If you want to route <code>/admin/articles</code> to <code>ArticlesController</code> (without the <code>Admin::</code> module prefix), you could use:</p><div class="code_container">
549
+ <pre class="brush: ruby; gutter: false; toolbar: false">
550
+ scope '/admin' do
551
+ resources :articles, :comments
552
+ end
553
+
554
+ </pre>
555
+ </div>
556
+ <p>or, for a single case:</p><div class="code_container">
557
+ <pre class="brush: ruby; gutter: false; toolbar: false">
558
+ resources :articles, path: '/admin/articles'
559
+
560
+ </pre>
561
+ </div>
562
+ <p>In each of these cases, the named routes remain the same as if you did not use <code>scope</code>. In the last case, the following paths map to <code>PostsController</code>:</p>
563
+ <table>
564
+ <thead>
565
+ <tr>
566
+ <th>HTTP Verb</th>
567
+ <th>Path</th>
568
+ <th>Controller#Action</th>
569
+ <th>Named Helper</th>
570
+ </tr>
571
+ </thead>
572
+ <tbody>
573
+ <tr>
574
+ <td>GET</td>
575
+ <td>/admin/articles</td>
576
+ <td>articles#index</td>
577
+ <td>articles_path</td>
578
+ </tr>
579
+ <tr>
580
+ <td>GET</td>
581
+ <td>/admin/articles/new</td>
582
+ <td>articles#new</td>
583
+ <td>new_article_path</td>
584
+ </tr>
585
+ <tr>
586
+ <td>POST</td>
587
+ <td>/admin/articles</td>
588
+ <td>articles#create</td>
589
+ <td>articles_path</td>
590
+ </tr>
591
+ <tr>
592
+ <td>GET</td>
593
+ <td>/admin/articles/:id</td>
594
+ <td>articles#show</td>
595
+ <td>article_path(:id)</td>
596
+ </tr>
597
+ <tr>
598
+ <td>GET</td>
599
+ <td>/admin/articles/:id/edit</td>
600
+ <td>articles#edit</td>
601
+ <td>edit_article_path(:id)</td>
602
+ </tr>
603
+ <tr>
604
+ <td>PATCH/PUT</td>
605
+ <td>/admin/articles/:id</td>
606
+ <td>articles#update</td>
607
+ <td>article_path(:id)</td>
608
+ </tr>
609
+ <tr>
610
+ <td>DELETE</td>
611
+ <td>/admin/articles/:id</td>
612
+ <td>articles#destroy</td>
613
+ <td>article_path(:id)</td>
614
+ </tr>
615
+ </tbody>
616
+ </table>
617
+ <div class="info"><p><em>If you need to use a different controller namespace inside a <code>namespace</code> block you can specify an absolute controller path, e.g: <code>get '/foo' =&gt; '/foo#index'</code>.</em></p></div><h4 id="nested-resources">2.7 Nested Resources</h4><p>It's common to have resources that are logically children of other resources. For example, suppose your application includes these models:</p><div class="code_container">
618
+ <pre class="brush: ruby; gutter: false; toolbar: false">
619
+ class Magazine &lt; ActiveRecord::Base
620
+ has_many :ads
621
+ end
622
+
623
+ class Ad &lt; ActiveRecord::Base
624
+ belongs_to :magazine
625
+ end
626
+
627
+ </pre>
628
+ </div>
629
+ <p>Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration:</p><div class="code_container">
630
+ <pre class="brush: ruby; gutter: false; toolbar: false">
631
+ resources :magazines do
632
+ resources :ads
633
+ end
634
+
635
+ </pre>
636
+ </div>
637
+ <p>In addition to the routes for magazines, this declaration will also route ads to an <code>AdsController</code>. The ad URLs require a magazine:</p>
638
+ <table>
639
+ <thead>
640
+ <tr>
641
+ <th>HTTP Verb</th>
642
+ <th>Path</th>
643
+ <th>Controller#Action</th>
644
+ <th>Used for</th>
645
+ </tr>
646
+ </thead>
647
+ <tbody>
648
+ <tr>
649
+ <td>GET</td>
650
+ <td>/magazines/:magazine_id/ads</td>
651
+ <td>ads#index</td>
652
+ <td>display a list of all ads for a specific magazine</td>
653
+ </tr>
654
+ <tr>
655
+ <td>GET</td>
656
+ <td>/magazines/:magazine_id/ads/new</td>
657
+ <td>ads#new</td>
658
+ <td>return an HTML form for creating a new ad belonging to a specific magazine</td>
659
+ </tr>
660
+ <tr>
661
+ <td>POST</td>
662
+ <td>/magazines/:magazine_id/ads</td>
663
+ <td>ads#create</td>
664
+ <td>create a new ad belonging to a specific magazine</td>
665
+ </tr>
666
+ <tr>
667
+ <td>GET</td>
668
+ <td>/magazines/:magazine_id/ads/:id</td>
669
+ <td>ads#show</td>
670
+ <td>display a specific ad belonging to a specific magazine</td>
671
+ </tr>
672
+ <tr>
673
+ <td>GET</td>
674
+ <td>/magazines/:magazine_id/ads/:id/edit</td>
675
+ <td>ads#edit</td>
676
+ <td>return an HTML form for editing an ad belonging to a specific magazine</td>
677
+ </tr>
678
+ <tr>
679
+ <td>PATCH/PUT</td>
680
+ <td>/magazines/:magazine_id/ads/:id</td>
681
+ <td>ads#update</td>
682
+ <td>update a specific ad belonging to a specific magazine</td>
683
+ </tr>
684
+ <tr>
685
+ <td>DELETE</td>
686
+ <td>/magazines/:magazine_id/ads/:id</td>
687
+ <td>ads#destroy</td>
688
+ <td>delete a specific ad belonging to a specific magazine</td>
689
+ </tr>
690
+ </tbody>
691
+ </table>
692
+ <p>This will also create routing helpers such as <code>magazine_ads_url</code> and <code>edit_magazine_ad_path</code>. These helpers take an instance of Magazine as the first parameter (<code>magazine_ads_url(@magazine)</code>).</p><h5 id="limits-to-nesting">2.7.1 Limits to Nesting</h5><p>You can nest resources within other nested resources if you like. For example:</p><div class="code_container">
693
+ <pre class="brush: ruby; gutter: false; toolbar: false">
694
+ resources :publishers do
695
+ resources :magazines do
696
+ resources :photos
697
+ end
698
+ end
699
+
700
+ </pre>
701
+ </div>
702
+ <p>Deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize paths such as:</p><div class="code_container">
703
+ <pre class="brush: plain; gutter: false; toolbar: false">
704
+ /publishers/1/magazines/2/photos/3
705
+
706
+ </pre>
707
+ </div>
708
+ <p>The corresponding route helper would be <code>publisher_magazine_photo_url</code>, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular <a href="http://weblog.jamisbuck.org/2007/2/5/nesting-resources">article</a> by Jamis Buck proposes a rule of thumb for good Rails design:</p><div class="info"><p><em>Resources should never be nested more than 1 level deep.</em></p></div><h5 id="shallow-nesting">2.7.2 Shallow Nesting</h5><p>One way to avoid deep nesting (as recommended above) is to generate the collection actions scoped under the parent, so as to get a sense of the hierarchy, but to not nest the member actions. In other words, to only build routes with the minimal amount of information to uniquely identify the resource, like this:</p><div class="code_container">
709
+ <pre class="brush: ruby; gutter: false; toolbar: false">
710
+ resources :articles do
711
+ resources :comments, only: [:index, :new, :create]
712
+ end
713
+ resources :comments, only: [:show, :edit, :update, :destroy]
714
+
715
+ </pre>
716
+ </div>
717
+ <p>This idea strikes a balance between descriptive routes and deep nesting. There exists shorthand syntax to achieve just that, via the <code>:shallow</code> option:</p><div class="code_container">
718
+ <pre class="brush: ruby; gutter: false; toolbar: false">
719
+ resources :articles do
720
+ resources :comments, shallow: true
721
+ end
722
+
723
+ </pre>
724
+ </div>
725
+ <p>This will generate the exact same routes as the first example. You can also specify the <code>:shallow</code> option in the parent resource, in which case all of the nested resources will be shallow:</p><div class="code_container">
726
+ <pre class="brush: ruby; gutter: false; toolbar: false">
727
+ resources :articles, shallow: true do
728
+ resources :comments
729
+ resources :quotes
730
+ resources :drafts
731
+ end
732
+
733
+ </pre>
734
+ </div>
735
+ <p>The <code>shallow</code> method of the DSL creates a scope inside of which every nesting is shallow. This generates the same routes as the previous example:</p><div class="code_container">
736
+ <pre class="brush: ruby; gutter: false; toolbar: false">
737
+ shallow do
738
+ resources :articles do
739
+ resources :comments
740
+ resources :quotes
741
+ resources :drafts
742
+ end
743
+ end
744
+
745
+ </pre>
746
+ </div>
747
+ <p>There exist two options for <code>scope</code> to customize shallow routes. <code>:shallow_path</code> prefixes member paths with the specified parameter:</p><div class="code_container">
748
+ <pre class="brush: ruby; gutter: false; toolbar: false">
749
+ scope shallow_path: "sekret" do
750
+ resources :articles do
751
+ resources :comments, shallow: true
752
+ end
753
+ end
754
+
755
+ </pre>
756
+ </div>
757
+ <p>The comments resource here will have the following routes generated for it:</p>
758
+ <table>
759
+ <thead>
760
+ <tr>
761
+ <th>HTTP Verb</th>
762
+ <th>Path</th>
763
+ <th>Controller#Action</th>
764
+ <th>Named Helper</th>
765
+ </tr>
766
+ </thead>
767
+ <tbody>
768
+ <tr>
769
+ <td>GET</td>
770
+ <td>/articles/:article_id/comments(.:format)</td>
771
+ <td>comments#index</td>
772
+ <td>article_comments_path</td>
773
+ </tr>
774
+ <tr>
775
+ <td>POST</td>
776
+ <td>/articles/:article_id/comments(.:format)</td>
777
+ <td>comments#create</td>
778
+ <td>article_comments_path</td>
779
+ </tr>
780
+ <tr>
781
+ <td>GET</td>
782
+ <td>/articles/:article_id/comments/new(.:format)</td>
783
+ <td>comments#new</td>
784
+ <td>new_article_comment_path</td>
785
+ </tr>
786
+ <tr>
787
+ <td>GET</td>
788
+ <td>/sekret/comments/:id/edit(.:format)</td>
789
+ <td>comments#edit</td>
790
+ <td>edit_comment_path</td>
791
+ </tr>
792
+ <tr>
793
+ <td>GET</td>
794
+ <td>/sekret/comments/:id(.:format)</td>
795
+ <td>comments#show</td>
796
+ <td>comment_path</td>
797
+ </tr>
798
+ <tr>
799
+ <td>PATCH/PUT</td>
800
+ <td>/sekret/comments/:id(.:format)</td>
801
+ <td>comments#update</td>
802
+ <td>comment_path</td>
803
+ </tr>
804
+ <tr>
805
+ <td>DELETE</td>
806
+ <td>/sekret/comments/:id(.:format)</td>
807
+ <td>comments#destroy</td>
808
+ <td>comment_path</td>
809
+ </tr>
810
+ </tbody>
811
+ </table>
812
+ <p>The <code>:shallow_prefix</code> option adds the specified parameter to the named helpers:</p><div class="code_container">
813
+ <pre class="brush: ruby; gutter: false; toolbar: false">
814
+ scope shallow_prefix: "sekret" do
815
+ resources :articles do
816
+ resources :comments, shallow: true
817
+ end
818
+ end
819
+
820
+ </pre>
821
+ </div>
822
+ <p>The comments resource here will have the following routes generated for it:</p>
823
+ <table>
824
+ <thead>
825
+ <tr>
826
+ <th>HTTP Verb</th>
827
+ <th>Path</th>
828
+ <th>Controller#Action</th>
829
+ <th>Named Helper</th>
830
+ </tr>
831
+ </thead>
832
+ <tbody>
833
+ <tr>
834
+ <td>GET</td>
835
+ <td>/articles/:article_id/comments(.:format)</td>
836
+ <td>comments#index</td>
837
+ <td>article_comments_path</td>
838
+ </tr>
839
+ <tr>
840
+ <td>POST</td>
841
+ <td>/articles/:article_id/comments(.:format)</td>
842
+ <td>comments#create</td>
843
+ <td>article_comments_path</td>
844
+ </tr>
845
+ <tr>
846
+ <td>GET</td>
847
+ <td>/articles/:article_id/comments/new(.:format)</td>
848
+ <td>comments#new</td>
849
+ <td>new_article_comment_path</td>
850
+ </tr>
851
+ <tr>
852
+ <td>GET</td>
853
+ <td>/comments/:id/edit(.:format)</td>
854
+ <td>comments#edit</td>
855
+ <td>edit_sekret_comment_path</td>
856
+ </tr>
857
+ <tr>
858
+ <td>GET</td>
859
+ <td>/comments/:id(.:format)</td>
860
+ <td>comments#show</td>
861
+ <td>sekret_comment_path</td>
862
+ </tr>
863
+ <tr>
864
+ <td>PATCH/PUT</td>
865
+ <td>/comments/:id(.:format)</td>
866
+ <td>comments#update</td>
867
+ <td>sekret_comment_path</td>
868
+ </tr>
869
+ <tr>
870
+ <td>DELETE</td>
871
+ <td>/comments/:id(.:format)</td>
872
+ <td>comments#destroy</td>
873
+ <td>sekret_comment_path</td>
874
+ </tr>
875
+ </tbody>
876
+ </table>
877
+ <h4 id="routing-concerns">2.8 Routing concerns</h4><p>Routing Concerns allows you to declare common routes that can be reused inside other resources and routes. To define a concern:</p><div class="code_container">
878
+ <pre class="brush: ruby; gutter: false; toolbar: false">
879
+ concern :commentable do
880
+ resources :comments
881
+ end
882
+
883
+ concern :image_attachable do
884
+ resources :images, only: :index
885
+ end
886
+
887
+ </pre>
888
+ </div>
889
+ <p>These concerns can be used in resources to avoid code duplication and share behavior across routes:</p><div class="code_container">
890
+ <pre class="brush: ruby; gutter: false; toolbar: false">
891
+ resources :messages, concerns: :commentable
892
+
893
+ resources :articles, concerns: [:commentable, :image_attachable]
894
+
895
+ </pre>
896
+ </div>
897
+ <p>The above is equivalent to:</p><div class="code_container">
898
+ <pre class="brush: ruby; gutter: false; toolbar: false">
899
+ resources :messages do
900
+ resources :comments
901
+ end
902
+
903
+ resources :articles do
904
+ resources :comments
905
+ resources :images, only: :index
906
+ end
907
+
908
+ </pre>
909
+ </div>
910
+ <p>Also you can use them in any place that you want inside the routes, for example in a scope or namespace call:</p><div class="code_container">
911
+ <pre class="brush: ruby; gutter: false; toolbar: false">
912
+ namespace :articles do
913
+ concerns :commentable
914
+ end
915
+
916
+ </pre>
917
+ </div>
918
+ <h4 id="creating-paths-and-urls-from-objects">2.9 Creating Paths and URLs From Objects</h4><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><div class="code_container">
919
+ <pre class="brush: ruby; gutter: false; toolbar: false">
920
+ resources :magazines do
921
+ resources :ads
922
+ end
923
+
924
+ </pre>
925
+ </div>
926
+ <p>When using <code>magazine_ad_path</code>, you can pass in instances of <code>Magazine</code> and <code>Ad</code> instead of the numeric IDs:</p><div class="code_container">
927
+ <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
928
+ &lt;%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %&gt;
929
+
930
+ </pre>
931
+ </div>
932
+ <p>You can also use <code>url_for</code> with a set of objects, and Rails will automatically determine which route you want:</p><div class="code_container">
933
+ <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
934
+ &lt;%= link_to 'Ad details', url_for([@magazine, @ad]) %&gt;
935
+
936
+ </pre>
937
+ </div>
938
+ <p>In this case, Rails will see that <code>@magazine</code> is a <code>Magazine</code> and <code>@ad</code> is an <code>Ad</code> and will therefore use the <code>magazine_ad_path</code> helper. In helpers like <code>link_to</code>, you can specify just the object in place of the full <code>url_for</code> call:</p><div class="code_container">
939
+ <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
940
+ &lt;%= link_to 'Ad details', [@magazine, @ad] %&gt;
941
+
942
+ </pre>
943
+ </div>
944
+ <p>If you wanted to link to just a magazine:</p><div class="code_container">
945
+ <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
946
+ &lt;%= link_to 'Magazine details', @magazine %&gt;
947
+
948
+ </pre>
949
+ </div>
950
+ <p>For other actions, you just need to insert the action name as the first element of the array:</p><div class="code_container">
951
+ <pre class="brush: ruby; html-script: true; gutter: false; toolbar: false">
952
+ &lt;%= link_to 'Edit Ad', [:edit, @magazine, @ad] %&gt;
953
+
954
+ </pre>
955
+ </div>
956
+ <p>This allows you to treat instances of your models as URLs, and is a key advantage to using the resourceful style.</p><h4 id="adding-more-restful-actions">2.10 Adding More RESTful Actions</h4><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><h5 id="adding-member-routes">2.10.1 Adding Member Routes</h5><p>To add a member route, just add a <code>member</code> block into the resource block:</p><div class="code_container">
957
+ <pre class="brush: ruby; gutter: false; toolbar: false">
958
+ resources :photos do
959
+ member do
960
+ get 'preview'
961
+ end
962
+ end
963
+
964
+ </pre>
965
+ </div>
966
+ <p>This will recognize <code>/photos/1/preview</code> with GET, and route to the <code>preview</code> action of <code>PhotosController</code>, with the resource id value passed in <code>params[:id]</code>. It will also create the <code>preview_photo_url</code> and <code>preview_photo_path</code> helpers.</p><p>Within the block of member routes, each route name specifies the HTTP verb
967
+ will be recognized. You can use <code>get</code>, <code>patch</code>, <code>put</code>, <code>post</code>, or <code>delete</code> here
968
+ . If you don't have multiple <code>member</code> routes, you can also pass <code>:on</code> to a
969
+ route, eliminating the block:</p><div class="code_container">
970
+ <pre class="brush: ruby; gutter: false; toolbar: false">
971
+ resources :photos do
972
+ get 'preview', on: :member
973
+ end
974
+
975
+ </pre>
976
+ </div>
977
+ <p>You can leave out the <code>:on</code> option, this will create the same member route except that the resource id value will be available in <code>params[:photo_id]</code> instead of <code>params[:id]</code>.</p><h5 id="adding-collection-routes">2.10.2 Adding Collection Routes</h5><p>To add a route to the collection:</p><div class="code_container">
978
+ <pre class="brush: ruby; gutter: false; toolbar: false">
979
+ resources :photos do
980
+ collection do
981
+ get 'search'
982
+ end
983
+ end
984
+
985
+ </pre>
986
+ </div>
987
+ <p>This will enable Rails to recognize paths such as <code>/photos/search</code> with GET, and route to the <code>search</code> action of <code>PhotosController</code>. It will also create the <code>search_photos_url</code> and <code>search_photos_path</code> route helpers.</p><p>Just as with member routes, you can pass <code>:on</code> to a route:</p><div class="code_container">
988
+ <pre class="brush: ruby; gutter: false; toolbar: false">
989
+ resources :photos do
990
+ get 'search', on: :collection
991
+ end
992
+
993
+ </pre>
994
+ </div>
995
+ <h5 id="adding-routes-for-additional-new-actions">2.10.3 Adding Routes for Additional New Actions</h5><p>To add an alternate new action using the <code>:on</code> shortcut:</p><div class="code_container">
996
+ <pre class="brush: ruby; gutter: false; toolbar: false">
997
+ resources :comments do
998
+ get 'preview', on: :new
999
+ end
1000
+
1001
+ </pre>
1002
+ </div>
1003
+ <p>This will enable Rails to recognize paths such as <code>/comments/new/preview</code> with GET, and route to the <code>preview</code> action of <code>CommentsController</code>. It will also create the <code>preview_new_comment_url</code> and <code>preview_new_comment_path</code> route helpers.</p><div class="info"><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></div><h3 id="non-resourceful-routes">3 Non-Resourceful Routes</h3><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><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><p>In particular, simple routing makes it very easy to map legacy URLs to new Rails actions.</p><h4 id="bound-parameters">3.1 Bound Parameters</h4><p>When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming HTTP request. Two of these symbols are special: <code>:controller</code> maps to the name of a controller in your application, and <code>:action</code> maps to the name of an action within that controller. For example, consider this route:</p><div class="code_container">
1004
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1005
+ get ':controller(/:action(/:id))'
1006
+
1007
+ </pre>
1008
+ </div>
1009
+ <p>If an incoming request of <code>/photos/show/1</code> is processed by this route (because it hasn't matched any previous route in the file), then the result will be to invoke the <code>show</code> action of the <code>PhotosController</code>, and to make the final parameter <code>"1"</code> available as <code>params[:id]</code>. This route will also route the incoming request of <code>/photos</code> to <code>PhotosController#index</code>, since <code>:action</code> and <code>:id</code> are optional parameters, denoted by parentheses.</p><h4 id="dynamic-segments">3.2 Dynamic Segments</h4><p>You can set up as many dynamic segments within a regular route as you like. Anything other than <code>:controller</code> or <code>:action</code> will be available to the action as part of <code>params</code>. If you set up this route:</p><div class="code_container">
1010
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1011
+ get ':controller/:action/:id/:user_id'
1012
+
1013
+ </pre>
1014
+ </div>
1015
+ <p>An incoming path of <code>/photos/show/1/2</code> will be dispatched to the <code>show</code> action of the <code>PhotosController</code>. <code>params[:id]</code> will be <code>"1"</code>, and <code>params[:user_id]</code> will be <code>"2"</code>.</p><div class="note"><p>You can't use <code>:namespace</code> or <code>:module</code> with a <code>:controller</code> path segment. If you need to do this then use a constraint on :controller that matches the namespace you require. e.g:</p></div><div class="code_container">
1016
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1017
+ get ':controller(/:action(/:id))', controller: /admin\/[^\/]+/
1018
+
1019
+ </pre>
1020
+ </div>
1021
+ <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 that overrides this – for example, <code>id: /[^\/]+/</code> allows anything except a slash.</p></div><h4 id="static-segments">3.3 Static Segments</h4><p>You can specify static segments when creating a route by not prepending a colon to a fragment:</p><div class="code_container">
1022
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1023
+ get ':controller/:action/:id/with_user/:user_id'
1024
+
1025
+ </pre>
1026
+ </div>
1027
+ <p>This route would respond to paths such as <code>/photos/show/1/with_user/2</code>. In this case, <code>params</code> would be <code>{ controller: 'photos', action: 'show', id: '1', user_id: '2' }</code>.</p><h4 id="the-query-string">3.4 The Query String</h4><p>The <code>params</code> will also include any parameters from the query string. For example, with this route:</p><div class="code_container">
1028
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1029
+ get ':controller/:action/:id'
1030
+
1031
+ </pre>
1032
+ </div>
1033
+ <p>An incoming path of <code>/photos/show/1?user_id=2</code> will be dispatched to the <code>show</code> action of the <code>Photos</code> controller. <code>params</code> will be <code>{ controller: 'photos', action: 'show', id: '1', user_id: '2' }</code>.</p><h4 id="defining-defaults">3.5 Defining Defaults</h4><p>You do not need to explicitly use the <code>:controller</code> and <code>:action</code> symbols within a route. You can supply them as defaults:</p><div class="code_container">
1034
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1035
+ get 'photos/:id', to: 'photos#show'
1036
+
1037
+ </pre>
1038
+ </div>
1039
+ <p>With this route, Rails will match an incoming path of <code>/photos/12</code> to the <code>show</code> action of <code>PhotosController</code>.</p><p>You can also define other defaults in a route by supplying a hash for the <code>:defaults</code> option. This even applies to parameters that you do not specify as dynamic segments. For example:</p><div class="code_container">
1040
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1041
+ get 'photos/:id', to: 'photos#show', defaults: { format: 'jpg' }
1042
+
1043
+ </pre>
1044
+ </div>
1045
+ <p>Rails would match <code>photos/12</code> to the <code>show</code> action of <code>PhotosController</code>, and set <code>params[:format]</code> to <code>"jpg"</code>.</p><h4 id="naming-routes">3.6 Naming Routes</h4><p>You can specify a name for any route using the <code>:as</code> option:</p><div class="code_container">
1046
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1047
+ get 'exit', to: 'sessions#destroy', as: :logout
1048
+
1049
+ </pre>
1050
+ </div>
1051
+ <p>This will create <code>logout_path</code> and <code>logout_url</code> as named helpers in your application. Calling <code>logout_path</code> will return <code>/exit</code></p><p>You can also use this to override routing methods defined by resources, like this:</p><div class="code_container">
1052
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1053
+ get ':username', to: 'users#show', as: :user
1054
+
1055
+ </pre>
1056
+ </div>
1057
+ <p>This will define a <code>user_path</code> method that will be available in controllers, helpers and views that will go to a route such as <code>/bob</code>. Inside the <code>show</code> action of <code>UsersController</code>, <code>params[:username]</code> will contain the username for the user. Change <code>:username</code> in the route definition if you do not want your parameter name to be <code>:username</code>.</p><h4 id="http-verb-constraints">3.7 HTTP Verb Constraints</h4><p>In general, you should use the <code>get</code>, <code>post</code>, <code>put</code>, <code>patch</code> and <code>delete</code> methods to constrain a route to a particular verb. You can use the <code>match</code> method with the <code>:via</code> option to match multiple verbs at once:</p><div class="code_container">
1058
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1059
+ match 'photos', to: 'photos#show', via: [:get, :post]
1060
+
1061
+ </pre>
1062
+ </div>
1063
+ <p>You can match all verbs to a particular route using <code>via: :all</code>:</p><div class="code_container">
1064
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1065
+ match 'photos', to: 'photos#show', via: :all
1066
+
1067
+ </pre>
1068
+ </div>
1069
+ <div class="note"><p>Routing both <code>GET</code> and <code>POST</code> requests to a single action has security implications. In general, you should avoid routing all verbs to an action unless you have a good reason to.</p></div><div class="note"><p>'GET' in Rails won't check for CSRF token. You should never write to the database from 'GET' requests, for more information see the <a href="security.html#csrf-countermeasures">security guide</a> on CSRF countermeasures.</p></div><h4 id="segment-constraints">3.8 Segment Constraints</h4><p>You can use the <code>:constraints</code> option to enforce a format for a dynamic segment:</p><div class="code_container">
1070
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1071
+ get 'photos/:id', to: 'photos#show', constraints: { id: /[A-Z]\d{5}/ }
1072
+
1073
+ </pre>
1074
+ </div>
1075
+ <p>This route would match paths such as <code>/photos/A12345</code>, but not <code>/photos/893</code>. You can more succinctly express the same route this way:</p><div class="code_container">
1076
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1077
+ get 'photos/:id', to: 'photos#show', id: /[A-Z]\d{5}/
1078
+
1079
+ </pre>
1080
+ </div>
1081
+ <p><code>:constraints</code> takes regular expressions with the restriction that regexp anchors can't be used. For example, the following route will not work:</p><div class="code_container">
1082
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1083
+ get '/:id', to: 'articles#show', constraints: { id: /^\d/ }
1084
+
1085
+ </pre>
1086
+ </div>
1087
+ <p>However, note that you don't need to use anchors because all routes are anchored at the start.</p><p>For example, the following routes would allow for <code>articles</code> with <code>to_param</code> values like <code>1-hello-world</code> that always begin with a number and <code>users</code> with <code>to_param</code> values like <code>david</code> that never begin with a number to share the root namespace:</p><div class="code_container">
1088
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1089
+ get '/:id', to: 'articles#show', constraints: { id: /\d.+/ }
1090
+ get '/:username', to: 'users#show'
1091
+
1092
+ </pre>
1093
+ </div>
1094
+ <h4 id="request-based-constraints">3.9 Request-Based Constraints</h4><p>You can also constrain a route based on any method on the <a href="action_controller_overview.html#the-request-object">Request object</a> that returns a <code>String</code>.</p><p>You specify a request-based constraint the same way that you specify a segment constraint:</p><div class="code_container">
1095
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1096
+ get 'photos', to: 'photos#index', constraints: { subdomain: 'admin' }
1097
+
1098
+ </pre>
1099
+ </div>
1100
+ <p>You can also specify constraints in a block form:</p><div class="code_container">
1101
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1102
+ namespace :admin do
1103
+ constraints subdomain: 'admin' do
1104
+ resources :photos
1105
+ end
1106
+ end
1107
+
1108
+ </pre>
1109
+ </div>
1110
+ <div class="note"><p>Request constraints work by calling a method on the <a href="action_controller_overview.html#the-request-object">Request object</a> with the same name as the hash key and then compare the return value with the hash value. Therefore, constraint values should match the corresponding Request object method return type. For example: <code>constraints: { subdomain: 'api' }</code> will match an <code>api</code> subdomain as expected, however using a symbol <code>constraints: { subdomain: :api }</code> will not, because <code>request.subdomain</code> returns <code>'api'</code> as a String.</p></div><h4 id="advanced-constraints">3.10 Advanced Constraints</h4><p>If you have a more advanced constraint, you can provide an object that responds to <code>matches?</code> that Rails should use. Let's say you wanted to route all users on a blacklist to the <code>BlacklistController</code>. You could do:</p><div class="code_container">
1111
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1112
+ class BlacklistConstraint
1113
+ def initialize
1114
+ @ips = Blacklist.retrieve_ips
1115
+ end
1116
+
1117
+ def matches?(request)
1118
+ @ips.include?(request.remote_ip)
1119
+ end
1120
+ end
1121
+
1122
+ Rails.application.routes.draw do
1123
+ get '*path', to: 'blacklist#index',
1124
+ constraints: BlacklistConstraint.new
1125
+ end
1126
+
1127
+ </pre>
1128
+ </div>
1129
+ <p>You can also specify constraints as a lambda:</p><div class="code_container">
1130
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1131
+ Rails.application.routes.draw do
1132
+ get '*path', to: 'blacklist#index',
1133
+ constraints: lambda { |request| Blacklist.retrieve_ips.include?(request.remote_ip) }
1134
+ end
1135
+
1136
+ </pre>
1137
+ </div>
1138
+ <p>Both the <code>matches?</code> method and the lambda gets the <code>request</code> object as an argument.</p><h4 id="route-globbing-and-wildcard-segments">3.11 Route Globbing and Wildcard Segments</h4><p>Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example:</p><div class="code_container">
1139
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1140
+ get 'photos/*other', to: 'photos#unknown'
1141
+
1142
+ </pre>
1143
+ </div>
1144
+ <p>This route would match <code>photos/12</code> or <code>/photos/long/path/to/12</code>, setting <code>params[:other]</code> to <code>"12"</code> or <code>"long/path/to/12"</code>. The fragments prefixed with a star are called "wildcard segments".</p><p>Wildcard segments can occur anywhere in a route. For example:</p><div class="code_container">
1145
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1146
+ get 'books/*section/:title', to: 'books#show'
1147
+
1148
+ </pre>
1149
+ </div>
1150
+ <p>would match <code>books/some/section/last-words-a-memoir</code> with <code>params[:section]</code> equals <code>'some/section'</code>, and <code>params[:title]</code> equals <code>'last-words-a-memoir'</code>.</p><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><div class="code_container">
1151
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1152
+ get '*a/foo/*b', to: 'test#index'
1153
+
1154
+ </pre>
1155
+ </div>
1156
+ <p>would match <code>zoo/woo/foo/bar/baz</code> with <code>params[:a]</code> equals <code>'zoo/woo'</code>, and <code>params[:b]</code> equals <code>'bar/baz'</code>.</p><div class="note"><p>By requesting <code>'/foo/bar.json'</code>, your <code>params[:pages]</code> will be equals to <code>'foo/bar'</code> with the request format of JSON. If you want the old 3.0.x behavior back, you could supply <code>format: false</code> like this:</p></div><div class="code_container">
1157
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1158
+ get '*pages', to: 'pages#show', format: false
1159
+
1160
+ </pre>
1161
+ </div>
1162
+ <div class="note"><p>If you want to make the format segment mandatory, so it cannot be omitted, you can supply <code>format: true</code> like this:</p></div><div class="code_container">
1163
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1164
+ get '*pages', to: 'pages#show', format: true
1165
+
1166
+ </pre>
1167
+ </div>
1168
+ <h4 id="redirection">3.12 Redirection</h4><p>You can redirect any path to another path using the <code>redirect</code> helper in your router:</p><div class="code_container">
1169
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1170
+ get '/stories', to: redirect('/articles')
1171
+
1172
+ </pre>
1173
+ </div>
1174
+ <p>You can also reuse dynamic segments from the match in the path to redirect to:</p><div class="code_container">
1175
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1176
+ get '/stories/:name', to: redirect('/articles/%{name}')
1177
+
1178
+ </pre>
1179
+ </div>
1180
+ <p>You can also provide a block to redirect, which receives the symbolized path parameters and the request object:</p><div class="code_container">
1181
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1182
+ get '/stories/:name', to: redirect { |path_params, req| "/articles/#{path_params[:name].pluralize}" }
1183
+ get '/stories', to: redirect { |path_params, req| "/articles/#{req.subdomain}" }
1184
+
1185
+ </pre>
1186
+ </div>
1187
+ <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><p>In all of these cases, if you don't provide the leading host (<code>http://www.example.com</code>), Rails will take those details from the current request.</p><h4 id="routing-to-rack-applications">3.13 Routing to Rack Applications</h4><p>Instead of a String like <code>'articles#index'</code>, which corresponds to the <code>index</code> action in the <code>ArticlesController</code>, you can specify any <a href="rails_on_rack.html">Rack application</a> as the endpoint for a matcher:</p><div class="code_container">
1188
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1189
+ match '/application.js', to: Sprockets, via: :all
1190
+
1191
+ </pre>
1192
+ </div>
1193
+ <p>As long as <code>Sprockets</code> responds to <code>call</code> and returns a <code>[status, headers, body]</code>, the router won't know the difference between the Rack application and an action. This is an appropriate use of <code>via: :all</code>, as you will want to allow your Rack application to handle all verbs as it considers appropriate.</p><div class="note"><p>For the curious, <code>'articles#index'</code> actually expands out to <code>ArticlesController.action(:index)</code>, which returns a valid Rack application.</p></div><h4 id="using-root">3.14 Using <code>root</code>
1194
+ </h4><p>You can specify what Rails should route <code>'/'</code> to with the <code>root</code> method:</p><div class="code_container">
1195
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1196
+ root to: 'pages#main'
1197
+ root 'pages#main' # shortcut for the above
1198
+
1199
+ </pre>
1200
+ </div>
1201
+ <p>You should put the <code>root</code> route at the top of the file, because it is the most popular route and should be matched first.</p><div class="note"><p>The <code>root</code> route only routes <code>GET</code> requests to the action.</p></div><p>You can also use root inside namespaces and scopes as well. For example:</p><div class="code_container">
1202
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1203
+ namespace :admin do
1204
+ root to: "admin#index"
1205
+ end
1206
+
1207
+ root to: "home#index"
1208
+
1209
+ </pre>
1210
+ </div>
1211
+ <h4 id="unicode-character-routes">3.15 Unicode character routes</h4><p>You can specify unicode character routes directly. For example:</p><div class="code_container">
1212
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1213
+ get 'こんにちは', to: 'welcome#index'
1214
+
1215
+ </pre>
1216
+ </div>
1217
+ <h3 id="customizing-resourceful-routes">4 Customizing Resourceful Routes</h3><p>While the default routes and helpers generated by <code>resources :articles</code> 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><h4 id="specifying-a-controller-to-use">4.1 Specifying a Controller to Use</h4><p>The <code>:controller</code> option lets you explicitly specify a controller to use for the resource. For example:</p><div class="code_container">
1218
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1219
+ resources :photos, controller: 'images'
1220
+
1221
+ </pre>
1222
+ </div>
1223
+ <p>will recognize incoming paths beginning with <code>/photos</code> but route to the <code>Images</code> controller:</p>
1224
+ <table>
1225
+ <thead>
1226
+ <tr>
1227
+ <th>HTTP Verb</th>
1228
+ <th>Path</th>
1229
+ <th>Controller#Action</th>
1230
+ <th>Named Helper</th>
1231
+ </tr>
1232
+ </thead>
1233
+ <tbody>
1234
+ <tr>
1235
+ <td>GET</td>
1236
+ <td>/photos</td>
1237
+ <td>images#index</td>
1238
+ <td>photos_path</td>
1239
+ </tr>
1240
+ <tr>
1241
+ <td>GET</td>
1242
+ <td>/photos/new</td>
1243
+ <td>images#new</td>
1244
+ <td>new_photo_path</td>
1245
+ </tr>
1246
+ <tr>
1247
+ <td>POST</td>
1248
+ <td>/photos</td>
1249
+ <td>images#create</td>
1250
+ <td>photos_path</td>
1251
+ </tr>
1252
+ <tr>
1253
+ <td>GET</td>
1254
+ <td>/photos/:id</td>
1255
+ <td>images#show</td>
1256
+ <td>photo_path(:id)</td>
1257
+ </tr>
1258
+ <tr>
1259
+ <td>GET</td>
1260
+ <td>/photos/:id/edit</td>
1261
+ <td>images#edit</td>
1262
+ <td>edit_photo_path(:id)</td>
1263
+ </tr>
1264
+ <tr>
1265
+ <td>PATCH/PUT</td>
1266
+ <td>/photos/:id</td>
1267
+ <td>images#update</td>
1268
+ <td>photo_path(:id)</td>
1269
+ </tr>
1270
+ <tr>
1271
+ <td>DELETE</td>
1272
+ <td>/photos/:id</td>
1273
+ <td>images#destroy</td>
1274
+ <td>photo_path(:id)</td>
1275
+ </tr>
1276
+ </tbody>
1277
+ </table>
1278
+ <div class="note"><p>Use <code>photos_path</code>, <code>new_photo_path</code>, etc. to generate paths for this resource.</p></div><p>For namespaced controllers you can use the directory notation. For example:</p><div class="code_container">
1279
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1280
+ resources :user_permissions, controller: 'admin/user_permissions'
1281
+
1282
+ </pre>
1283
+ </div>
1284
+ <p>This will route to the <code>Admin::UserPermissions</code> controller.</p><div class="note"><p>Only the directory notation is supported. Specifying the
1285
+ controller with Ruby constant notation (eg. <code>controller: 'Admin::UserPermissions'</code>)
1286
+ can lead to routing problems and results in
1287
+ a warning.</p></div><h4 id="specifying-constraints">4.2 Specifying Constraints</h4><p>You can use the <code>:constraints</code> option to specify a required format on the implicit <code>id</code>. For example:</p><div class="code_container">
1288
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1289
+ resources :photos, constraints: { id: /[A-Z][A-Z][0-9]+/ }
1290
+
1291
+ </pre>
1292
+ </div>
1293
+ <p>This declaration constrains the <code>:id</code> parameter to match the supplied regular expression. So, in this case, the router would no longer match <code>/photos/1</code> to this route. Instead, <code>/photos/RR27</code> would match.</p><p>You can specify a single constraint to apply to a number of routes by using the block form:</p><div class="code_container">
1294
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1295
+ constraints(id: /[A-Z][A-Z][0-9]+/) do
1296
+ resources :photos
1297
+ resources :accounts
1298
+ end
1299
+
1300
+ </pre>
1301
+ </div>
1302
+ <div class="note"><p>Of course, you can use the more advanced constraints available in non-resourceful routes in this context.</p></div><div class="info"><p>By default the <code>:id</code> 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 <code>:id</code> add a constraint which overrides this - for example <code>id: /[^\/]+/</code> allows anything except a slash.</p></div><h4 id="overriding-the-named-helpers">4.3 Overriding the Named Helpers</h4><p>The <code>:as</code> option lets you override the normal naming for the named route helpers. For example:</p><div class="code_container">
1303
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1304
+ resources :photos, as: 'images'
1305
+
1306
+ </pre>
1307
+ </div>
1308
+ <p>will recognize incoming paths beginning with <code>/photos</code> and route the requests to <code>PhotosController</code>, but use the value of the :as option to name the helpers.</p>
1309
+ <table>
1310
+ <thead>
1311
+ <tr>
1312
+ <th>HTTP Verb</th>
1313
+ <th>Path</th>
1314
+ <th>Controller#Action</th>
1315
+ <th>Named Helper</th>
1316
+ </tr>
1317
+ </thead>
1318
+ <tbody>
1319
+ <tr>
1320
+ <td>GET</td>
1321
+ <td>/photos</td>
1322
+ <td>photos#index</td>
1323
+ <td>images_path</td>
1324
+ </tr>
1325
+ <tr>
1326
+ <td>GET</td>
1327
+ <td>/photos/new</td>
1328
+ <td>photos#new</td>
1329
+ <td>new_image_path</td>
1330
+ </tr>
1331
+ <tr>
1332
+ <td>POST</td>
1333
+ <td>/photos</td>
1334
+ <td>photos#create</td>
1335
+ <td>images_path</td>
1336
+ </tr>
1337
+ <tr>
1338
+ <td>GET</td>
1339
+ <td>/photos/:id</td>
1340
+ <td>photos#show</td>
1341
+ <td>image_path(:id)</td>
1342
+ </tr>
1343
+ <tr>
1344
+ <td>GET</td>
1345
+ <td>/photos/:id/edit</td>
1346
+ <td>photos#edit</td>
1347
+ <td>edit_image_path(:id)</td>
1348
+ </tr>
1349
+ <tr>
1350
+ <td>PATCH/PUT</td>
1351
+ <td>/photos/:id</td>
1352
+ <td>photos#update</td>
1353
+ <td>image_path(:id)</td>
1354
+ </tr>
1355
+ <tr>
1356
+ <td>DELETE</td>
1357
+ <td>/photos/:id</td>
1358
+ <td>photos#destroy</td>
1359
+ <td>image_path(:id)</td>
1360
+ </tr>
1361
+ </tbody>
1362
+ </table>
1363
+ <h4 id="overriding-the-new-and-edit-segments">4.4 Overriding the <code>new</code> and <code>edit</code> Segments</h4><p>The <code>:path_names</code> option lets you override the automatically-generated <code>new</code> and <code>edit</code> segments in paths:</p><div class="code_container">
1364
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1365
+ resources :photos, path_names: { new: 'make', edit: 'change' }
1366
+
1367
+ </pre>
1368
+ </div>
1369
+ <p>This would cause the routing to recognize paths such as:</p><div class="code_container">
1370
+ <pre class="brush: plain; gutter: false; toolbar: false">
1371
+ /photos/make
1372
+ /photos/1/change
1373
+
1374
+ </pre>
1375
+ </div>
1376
+ <div class="note"><p>The actual action names aren't changed by this option. The two paths shown would still route to the <code>new</code> and <code>edit</code> actions.</p></div><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><div class="code_container">
1377
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1378
+ scope path_names: { new: 'make' } do
1379
+ # rest of your routes
1380
+ end
1381
+
1382
+ </pre>
1383
+ </div>
1384
+ <h4 id="prefixing-the-named-route-helpers">4.5 Prefixing the Named Route Helpers</h4><p>You can use the <code>:as</code> 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. For example:</p><div class="code_container">
1385
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1386
+ scope 'admin' do
1387
+ resources :photos, as: 'admin_photos'
1388
+ end
1389
+
1390
+ resources :photos
1391
+
1392
+ </pre>
1393
+ </div>
1394
+ <p>This will provide route helpers such as <code>admin_photos_path</code>, <code>new_admin_photo_path</code>, etc.</p><p>To prefix a group of route helpers, use <code>:as</code> with <code>scope</code>:</p><div class="code_container">
1395
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1396
+ scope 'admin', as: 'admin' do
1397
+ resources :photos, :accounts
1398
+ end
1399
+
1400
+ resources :photos, :accounts
1401
+
1402
+ </pre>
1403
+ </div>
1404
+ <p>This will generate routes such as <code>admin_photos_path</code> and <code>admin_accounts_path</code> which map to <code>/admin/photos</code> and <code>/admin/accounts</code> respectively.</p><div class="note"><p>The <code>namespace</code> scope will automatically add <code>:as</code> as well as <code>:module</code> and <code>:path</code> prefixes.</p></div><p>You can prefix routes with a named parameter also:</p><div class="code_container">
1405
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1406
+ scope ':username' do
1407
+ resources :articles
1408
+ end
1409
+
1410
+ </pre>
1411
+ </div>
1412
+ <p>This will provide you with URLs such as <code>/bob/articles/1</code> and will allow you to reference the <code>username</code> part of the path as <code>params[:username]</code> in controllers, helpers and views.</p><h4 id="restricting-the-routes-created">4.6 Restricting the Routes Created</h4><p>By default, Rails creates routes for the seven default actions (<code>index</code>, <code>show</code>, <code>new</code>, <code>create</code>, <code>edit</code>, <code>update</code>, and <code>destroy</code>) for every RESTful route in your application. You can use the <code>:only</code> and <code>:except</code> options to fine-tune this behavior. The <code>:only</code> option tells Rails to create only the specified routes:</p><div class="code_container">
1413
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1414
+ resources :photos, only: [:index, :show]
1415
+
1416
+ </pre>
1417
+ </div>
1418
+ <p>Now, a <code>GET</code> request to <code>/photos</code> would succeed, but a <code>POST</code> request to <code>/photos</code> (which would ordinarily be routed to the <code>create</code> action) will fail.</p><p>The <code>:except</code> option specifies a route or list of routes that Rails should <em>not</em> create:</p><div class="code_container">
1419
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1420
+ resources :photos, except: :destroy
1421
+
1422
+ </pre>
1423
+ </div>
1424
+ <p>In this case, Rails will create all of the normal routes except the route for <code>destroy</code> (a <code>DELETE</code> request to <code>/photos/:id</code>).</p><div class="info"><p>If your application has many RESTful routes, using <code>:only</code> and <code>:except</code> to generate only the routes that you actually need can cut down on memory use and speed up the routing process.</p></div><h4 id="translated-paths">4.7 Translated Paths</h4><p>Using <code>scope</code>, we can alter path names generated by resources:</p><div class="code_container">
1425
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1426
+ scope(path_names: { new: 'neu', edit: 'bearbeiten' }) do
1427
+ resources :categories, path: 'kategorien'
1428
+ end
1429
+
1430
+ </pre>
1431
+ </div>
1432
+ <p>Rails now creates routes to the <code>CategoriesController</code>.</p>
1433
+ <table>
1434
+ <thead>
1435
+ <tr>
1436
+ <th>HTTP Verb</th>
1437
+ <th>Path</th>
1438
+ <th>Controller#Action</th>
1439
+ <th>Named Helper</th>
1440
+ </tr>
1441
+ </thead>
1442
+ <tbody>
1443
+ <tr>
1444
+ <td>GET</td>
1445
+ <td>/kategorien</td>
1446
+ <td>categories#index</td>
1447
+ <td>categories_path</td>
1448
+ </tr>
1449
+ <tr>
1450
+ <td>GET</td>
1451
+ <td>/kategorien/neu</td>
1452
+ <td>categories#new</td>
1453
+ <td>new_category_path</td>
1454
+ </tr>
1455
+ <tr>
1456
+ <td>POST</td>
1457
+ <td>/kategorien</td>
1458
+ <td>categories#create</td>
1459
+ <td>categories_path</td>
1460
+ </tr>
1461
+ <tr>
1462
+ <td>GET</td>
1463
+ <td>/kategorien/:id</td>
1464
+ <td>categories#show</td>
1465
+ <td>category_path(:id)</td>
1466
+ </tr>
1467
+ <tr>
1468
+ <td>GET</td>
1469
+ <td>/kategorien/:id/bearbeiten</td>
1470
+ <td>categories#edit</td>
1471
+ <td>edit_category_path(:id)</td>
1472
+ </tr>
1473
+ <tr>
1474
+ <td>PATCH/PUT</td>
1475
+ <td>/kategorien/:id</td>
1476
+ <td>categories#update</td>
1477
+ <td>category_path(:id)</td>
1478
+ </tr>
1479
+ <tr>
1480
+ <td>DELETE</td>
1481
+ <td>/kategorien/:id</td>
1482
+ <td>categories#destroy</td>
1483
+ <td>category_path(:id)</td>
1484
+ </tr>
1485
+ </tbody>
1486
+ </table>
1487
+ <h4 id="overriding-the-singular-form">4.8 Overriding the Singular Form</h4><p>If you want to define the singular form of a resource, you should add additional rules to the <code>Inflector</code>:</p><div class="code_container">
1488
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1489
+ ActiveSupport::Inflector.inflections do |inflect|
1490
+ inflect.irregular 'tooth', 'teeth'
1491
+ end
1492
+
1493
+ </pre>
1494
+ </div>
1495
+ <h4 id="using-:as-in-nested-resources">4.9 Using <code>:as</code> in Nested Resources</h4><p>The <code>:as</code> option overrides the automatically-generated name for the resource in nested route helpers. For example:</p><div class="code_container">
1496
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1497
+ resources :magazines do
1498
+ resources :ads, as: 'periodical_ads'
1499
+ end
1500
+
1501
+ </pre>
1502
+ </div>
1503
+ <p>This will create routing helpers such as <code>magazine_periodical_ads_url</code> and <code>edit_magazine_periodical_ad_path</code>.</p><h4 id="overriding-named-route-parameters">4.10 Overriding Named Route Parameters</h4><p>The <code>:param</code> option overrides the default resource identifier <code>:id</code> (name of
1504
+ the <a href="routing.html#dynamic-segments">dynamic segment</a> used to generate the
1505
+ routes). You can access that segment from your controller using
1506
+ <code>params[&lt;:param&gt;]</code>.</p><div class="code_container">
1507
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1508
+ resources :videos, param: :identifier
1509
+
1510
+ </pre>
1511
+ </div>
1512
+ <div class="code_container">
1513
+ <pre class="brush: plain; gutter: false; toolbar: false">
1514
+ videos GET /videos(.:format) videos#index
1515
+ POST /videos(.:format) videos#create
1516
+ new_videos GET /videos/new(.:format) videos#new
1517
+ edit_videos GET /videos/:identifier/edit(.:format) videos#edit
1518
+
1519
+ </pre>
1520
+ </div>
1521
+ <div class="code_container">
1522
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1523
+ Video.find_by(identifier: params[:identifier])
1524
+
1525
+ </pre>
1526
+ </div>
1527
+ <h3 id="inspecting-and-testing-routes">5 Inspecting and Testing Routes</h3><p>Rails offers facilities for inspecting and testing your routes.</p><h4 id="listing-existing-routes">5.1 Listing Existing Routes</h4><p>To get a complete list of the available routes in your application, visit <code>http://localhost:3000/rails/info/routes</code> in your browser while your server is running in the <strong>development</strong> environment. You can also execute the <code>rake routes</code> command in your terminal to produce the same output.</p><p>Both methods will list all of your routes, in the same order that they appear in <code>routes.rb</code>. For each route, you'll see:</p>
1528
+ <ul>
1529
+ <li>The route name (if any)</li>
1530
+ <li>The HTTP verb used (if the route doesn't respond to all verbs)</li>
1531
+ <li>The URL pattern to match</li>
1532
+ <li>The routing parameters for the route</li>
1533
+ </ul>
1534
+ <p>For example, here's a small section of the <code>rake routes</code> output for a RESTful route:</p><div class="code_container">
1535
+ <pre class="brush: plain; gutter: false; toolbar: false">
1536
+ users GET /users(.:format) users#index
1537
+ POST /users(.:format) users#create
1538
+ new_user GET /users/new(.:format) users#new
1539
+ edit_user GET /users/:id/edit(.:format) users#edit
1540
+
1541
+ </pre>
1542
+ </div>
1543
+ <p>You may restrict the listing to the routes that map to a particular controller setting the <code>CONTROLLER</code> environment variable:</p><div class="code_container">
1544
+ <pre class="brush: plain; gutter: false; toolbar: false">
1545
+ $ CONTROLLER=users bin/rake routes
1546
+
1547
+ </pre>
1548
+ </div>
1549
+ <div class="info"><p>You'll find that the output from <code>rake routes</code> is much more readable if you widen your terminal window until the output lines don't wrap.</p></div><h4 id="testing-routes">5.2 Testing Routes</h4><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>
1550
+ <ul>
1551
+ <li><code>assert_generates</code></li>
1552
+ <li><code>assert_recognizes</code></li>
1553
+ <li><code>assert_routing</code></li>
1554
+ </ul>
1555
+ <h5 id="the-assert_generates-assertion">5.2.1 The <code>assert_generates</code> Assertion</h5><p><code>assert_generates</code> asserts that a particular set of options generate a particular path and can be used with default routes or custom routes. For example:</p><div class="code_container">
1556
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1557
+ assert_generates '/photos/1', { controller: 'photos', action: 'show', id: '1' }
1558
+ assert_generates '/about', controller: 'pages', action: 'about'
1559
+
1560
+ </pre>
1561
+ </div>
1562
+ <h5 id="the-assert_recognizes-assertion">5.2.2 The <code>assert_recognizes</code> Assertion</h5><p><code>assert_recognizes</code> is the inverse of <code>assert_generates</code>. It asserts that a given path is recognized and routes it to a particular spot in your application. For example:</p><div class="code_container">
1563
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1564
+ assert_recognizes({ controller: 'photos', action: 'show', id: '1' }, '/photos/1')
1565
+
1566
+ </pre>
1567
+ </div>
1568
+ <p>You can supply a <code>:method</code> argument to specify the HTTP verb:</p><div class="code_container">
1569
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1570
+ assert_recognizes({ controller: 'photos', action: 'create' }, { path: 'photos', method: :post })
1571
+
1572
+ </pre>
1573
+ </div>
1574
+ <h5 id="the-assert_routing-assertion">5.2.3 The <code>assert_routing</code> Assertion</h5><p>The <code>assert_routing</code> 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 <code>assert_generates</code> and <code>assert_recognizes</code>:</p><div class="code_container">
1575
+ <pre class="brush: ruby; gutter: false; toolbar: false">
1576
+ assert_routing({ path: 'photos', method: :post }, { controller: 'photos', action: 'create' })
1577
+
1578
+ </pre>
1579
+ </div>
1580
+
1581
+
1582
+ <h3>Feedback</h3>
1583
+ <p>
1584
+ You're encouraged to help improve the quality of this guide.
1585
+ </p>
1586
+ <p>
1587
+ Please contribute if you see any typos or factual errors.
1588
+ To get started, you can read our <a href="http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#contributing-to-the-rails-documentation">documentation contributions</a> section.
1589
+ </p>
1590
+ <p>
1591
+ You may also find incomplete content, or stuff that is not up to date.
1592
+ Please do add any missing documentation for master. Make sure to check
1593
+ <a href="http://edgeguides.rubyonrails.org">Edge Guides</a> first to verify
1594
+ if the issues are already fixed or not on the master branch.
1595
+ Check the <a href="ruby_on_rails_guides_guidelines.html">Ruby on Rails Guides Guidelines</a>
1596
+ for style and conventions.
1597
+ </p>
1598
+ <p>
1599
+ If for whatever reason you spot something to fix but cannot patch it yourself, please
1600
+ <a href="https://github.com/rails/rails/issues">open an issue</a>.
1601
+ </p>
1602
+ <p>And last but not least, any kind of discussion regarding Ruby on Rails
1603
+ documentation is very welcome in the <a href="http://groups.google.com/group/rubyonrails-docs">rubyonrails-docs mailing list</a>.
1604
+ </p>
1605
+ </div>
1606
+ </div>
1607
+ </div>
1608
+
1609
+ <hr class="hide" />
1610
+ <div id="footer">
1611
+ <div class="wrapper">
1612
+ <p>This work is licensed under a <a href="https://creativecommons.org/licenses/by-sa/4.0/">Creative Commons Attribution-ShareAlike 4.0 International</a> License</p>
1613
+ <p>"Rails", "Ruby on Rails", and the Rails logo are trademarks of David Heinemeier Hansson. All rights reserved.</p>
1614
+
1615
+ </div>
1616
+ </div>
1617
+
1618
+ <script type="text/javascript" src="javascripts/jquery.min.js"></script>
1619
+ <script type="text/javascript" src="javascripts/responsive-tables.js"></script>
1620
+ <script type="text/javascript" src="javascripts/guides.js"></script>
1621
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shCore.js"></script>
1622
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushRuby.js"></script>
1623
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushXml.js"></script>
1624
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushSql.js"></script>
1625
+ <script type="text/javascript" src="javascripts/syntaxhighlighter/shBrushPlain.js"></script>
1626
+ <script type="text/javascript">
1627
+ SyntaxHighlighter.all();
1628
+ $(guidesIndex.bind);
1629
+ </script>
1630
+ </body>
1631
+ </html>