rails 3.2.22.5 → 4.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of rails might be problematic. Click here for more details.

Files changed (281) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +77 -0
  3. data/guides/CHANGELOG.md +9 -0
  4. data/guides/Rakefile +77 -0
  5. data/guides/assets/images/belongs_to.png +0 -0
  6. data/guides/assets/images/book_icon.gif +0 -0
  7. data/guides/assets/images/bullet.gif +0 -0
  8. data/guides/assets/images/challenge.png +0 -0
  9. data/guides/assets/images/chapters_icon.gif +0 -0
  10. data/guides/assets/images/check_bullet.gif +0 -0
  11. data/guides/assets/images/credits_pic_blank.gif +0 -0
  12. data/guides/assets/images/csrf.png +0 -0
  13. data/guides/assets/images/edge_badge.png +0 -0
  14. data/guides/assets/images/favicon.ico +0 -0
  15. data/guides/assets/images/feature_tile.gif +0 -0
  16. data/guides/assets/images/footer_tile.gif +0 -0
  17. data/guides/assets/images/fxn.png +0 -0
  18. data/guides/assets/images/getting_started/confirm_dialog.png +0 -0
  19. data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
  20. data/guides/assets/images/getting_started/form_with_errors.png +0 -0
  21. data/guides/assets/images/getting_started/index_action_with_edit_link.png +0 -0
  22. data/guides/assets/images/getting_started/new_post.png +0 -0
  23. data/guides/assets/images/getting_started/post_with_comments.png +0 -0
  24. data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
  25. data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
  26. data/guides/assets/images/getting_started/show_action_for_posts.png +0 -0
  27. data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
  28. data/guides/assets/images/getting_started/undefined_method_post_path.png +0 -0
  29. data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
  30. data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
  31. data/guides/assets/images/grey_bullet.gif +0 -0
  32. data/guides/assets/images/habtm.png +0 -0
  33. data/guides/assets/images/has_many.png +0 -0
  34. data/guides/assets/images/has_many_through.png +0 -0
  35. data/guides/assets/images/has_one.png +0 -0
  36. data/guides/assets/images/has_one_through.png +0 -0
  37. data/guides/assets/images/header_backdrop.png +0 -0
  38. data/guides/assets/images/header_tile.gif +0 -0
  39. data/guides/assets/images/i18n/demo_html_safe.png +0 -0
  40. data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
  41. data/guides/assets/images/i18n/demo_translated_en.png +0 -0
  42. data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
  43. data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
  44. data/guides/assets/images/i18n/demo_untranslated.png +0 -0
  45. data/guides/assets/images/icons/README +5 -0
  46. data/guides/assets/images/icons/callouts/1.png +0 -0
  47. data/guides/assets/images/icons/callouts/10.png +0 -0
  48. data/guides/assets/images/icons/callouts/11.png +0 -0
  49. data/guides/assets/images/icons/callouts/12.png +0 -0
  50. data/guides/assets/images/icons/callouts/13.png +0 -0
  51. data/guides/assets/images/icons/callouts/14.png +0 -0
  52. data/guides/assets/images/icons/callouts/15.png +0 -0
  53. data/guides/assets/images/icons/callouts/2.png +0 -0
  54. data/guides/assets/images/icons/callouts/3.png +0 -0
  55. data/guides/assets/images/icons/callouts/4.png +0 -0
  56. data/guides/assets/images/icons/callouts/5.png +0 -0
  57. data/guides/assets/images/icons/callouts/6.png +0 -0
  58. data/guides/assets/images/icons/callouts/7.png +0 -0
  59. data/guides/assets/images/icons/callouts/8.png +0 -0
  60. data/guides/assets/images/icons/callouts/9.png +0 -0
  61. data/guides/assets/images/icons/caution.png +0 -0
  62. data/guides/assets/images/icons/example.png +0 -0
  63. data/guides/assets/images/icons/home.png +0 -0
  64. data/guides/assets/images/icons/important.png +0 -0
  65. data/guides/assets/images/icons/next.png +0 -0
  66. data/guides/assets/images/icons/note.png +0 -0
  67. data/guides/assets/images/icons/prev.png +0 -0
  68. data/guides/assets/images/icons/tip.png +0 -0
  69. data/guides/assets/images/icons/up.png +0 -0
  70. data/guides/assets/images/icons/warning.png +0 -0
  71. data/guides/assets/images/jaimeiniesta.jpg +0 -0
  72. data/guides/assets/images/nav_arrow.gif +0 -0
  73. data/guides/assets/images/oscardelben.jpg +0 -0
  74. data/guides/assets/images/polymorphic.png +0 -0
  75. data/guides/assets/images/radar.png +0 -0
  76. data/guides/assets/images/rails4_features.png +0 -0
  77. data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
  78. data/guides/assets/images/rails_guides_logo.gif +0 -0
  79. data/guides/assets/images/rails_logo_remix.gif +0 -0
  80. data/guides/assets/images/rails_welcome.png +0 -0
  81. data/guides/assets/images/session_fixation.png +0 -0
  82. data/guides/assets/images/tab_grey.gif +0 -0
  83. data/guides/assets/images/tab_info.gif +0 -0
  84. data/guides/assets/images/tab_note.gif +0 -0
  85. data/guides/assets/images/tab_red.gif +0 -0
  86. data/guides/assets/images/tab_yellow.gif +0 -0
  87. data/guides/assets/images/tab_yellow.png +0 -0
  88. data/guides/assets/images/vijaydev.jpg +0 -0
  89. data/guides/assets/javascripts/guides.js +57 -0
  90. data/guides/assets/javascripts/jquery.min.js +4 -0
  91. data/guides/assets/javascripts/responsive-tables.js +43 -0
  92. data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +59 -0
  93. data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +75 -0
  94. data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +59 -0
  95. data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +65 -0
  96. data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +100 -0
  97. data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +97 -0
  98. data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +91 -0
  99. data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +55 -0
  100. data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +41 -0
  101. data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +52 -0
  102. data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +67 -0
  103. data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +52 -0
  104. data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +57 -0
  105. data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +58 -0
  106. data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +72 -0
  107. data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +88 -0
  108. data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +33 -0
  109. data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +74 -0
  110. data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +64 -0
  111. data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +55 -0
  112. data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +94 -0
  113. data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +51 -0
  114. data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +66 -0
  115. data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +56 -0
  116. data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +69 -0
  117. data/guides/assets/javascripts/syntaxhighlighter/shCore.js +17 -0
  118. data/guides/assets/stylesheets/fixes.css +16 -0
  119. data/guides/assets/stylesheets/kindle.css +11 -0
  120. data/guides/assets/stylesheets/main.css +709 -0
  121. data/guides/assets/stylesheets/print.css +52 -0
  122. data/guides/assets/stylesheets/reset.css +43 -0
  123. data/guides/assets/stylesheets/responsive-tables.css +50 -0
  124. data/guides/assets/stylesheets/style.css +13 -0
  125. data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +226 -0
  126. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +328 -0
  127. data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +331 -0
  128. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +339 -0
  129. data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +324 -0
  130. data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +328 -0
  131. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +324 -0
  132. data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +324 -0
  133. data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +324 -0
  134. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +117 -0
  135. data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +120 -0
  136. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +128 -0
  137. data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +113 -0
  138. data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +117 -0
  139. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +113 -0
  140. data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +113 -0
  141. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +113 -0
  142. data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +116 -0
  143. data/guides/code/getting_started/Gemfile +38 -0
  144. data/guides/code/getting_started/Gemfile.lock +150 -0
  145. data/guides/code/getting_started/README.rdoc +28 -0
  146. data/guides/code/getting_started/Rakefile +6 -0
  147. data/guides/code/getting_started/app/assets/images/rails.png +0 -0
  148. data/guides/code/getting_started/app/assets/javascripts/application.js +16 -0
  149. data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
  150. data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
  151. data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +3 -0
  152. data/guides/code/getting_started/app/assets/stylesheets/application.css +13 -0
  153. data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
  154. data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
  155. data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +3 -0
  156. data/guides/code/getting_started/app/controllers/application_controller.rb +5 -0
  157. data/guides/code/getting_started/app/controllers/comments_controller.rb +17 -0
  158. data/guides/code/getting_started/app/controllers/posts_controller.rb +47 -0
  159. data/guides/code/getting_started/app/controllers/welcome_controller.rb +4 -0
  160. data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
  161. data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
  162. data/guides/code/getting_started/app/helpers/posts_helper.rb +2 -0
  163. data/guides/code/getting_started/app/helpers/welcome_helper.rb +2 -0
  164. data/guides/code/getting_started/app/models/comment.rb +3 -0
  165. data/guides/code/getting_started/app/models/post.rb +7 -0
  166. data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
  167. data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
  168. data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
  169. data/guides/code/getting_started/app/views/posts/_form.html.erb +27 -0
  170. data/guides/code/getting_started/app/views/posts/edit.html.erb +5 -0
  171. data/guides/code/getting_started/app/views/posts/index.html.erb +21 -0
  172. data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
  173. data/guides/code/getting_started/app/views/posts/show.html.erb +18 -0
  174. data/guides/code/getting_started/app/views/welcome/index.html.erb +3 -0
  175. data/guides/code/getting_started/bin/bundle +4 -0
  176. data/guides/code/getting_started/bin/rails +4 -0
  177. data/guides/code/getting_started/bin/rake +4 -0
  178. data/guides/code/getting_started/config.ru +4 -0
  179. data/guides/code/getting_started/config/application.rb +17 -0
  180. data/guides/code/getting_started/config/boot.rb +4 -0
  181. data/guides/code/getting_started/config/database.yml +25 -0
  182. data/guides/code/getting_started/config/environment.rb +5 -0
  183. data/guides/code/getting_started/config/environments/development.rb +30 -0
  184. data/guides/code/getting_started/config/environments/production.rb +80 -0
  185. data/guides/code/getting_started/config/environments/test.rb +36 -0
  186. data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
  187. data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +4 -0
  188. data/guides/code/getting_started/config/initializers/inflections.rb +16 -0
  189. data/guides/code/getting_started/config/initializers/locale.rb +9 -0
  190. data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
  191. data/guides/code/getting_started/config/initializers/secret_token.rb +12 -0
  192. data/guides/code/getting_started/config/initializers/session_store.rb +3 -0
  193. data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
  194. data/guides/code/getting_started/config/locales/en.yml +23 -0
  195. data/guides/code/getting_started/config/routes.rb +7 -0
  196. data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +10 -0
  197. data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +11 -0
  198. data/guides/code/getting_started/db/schema.rb +33 -0
  199. data/guides/code/getting_started/db/seeds.rb +7 -0
  200. data/guides/code/getting_started/public/404.html +27 -0
  201. data/guides/code/getting_started/public/422.html +26 -0
  202. data/guides/code/getting_started/public/500.html +26 -0
  203. data/guides/code/getting_started/public/favicon.ico +0 -0
  204. data/guides/code/getting_started/public/robots.txt +5 -0
  205. data/guides/code/getting_started/test/controllers/comments_controller_test.rb +7 -0
  206. data/guides/code/getting_started/test/controllers/posts_controller_test.rb +7 -0
  207. data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +9 -0
  208. data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
  209. data/guides/code/getting_started/test/fixtures/posts.yml +9 -0
  210. data/guides/code/getting_started/test/helpers/comments_helper_test.rb +4 -0
  211. data/guides/code/getting_started/test/helpers/posts_helper_test.rb +4 -0
  212. data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +4 -0
  213. data/guides/code/getting_started/test/models/comment_test.rb +7 -0
  214. data/guides/code/getting_started/test/models/post_test.rb +7 -0
  215. data/guides/code/getting_started/test/test_helper.rb +15 -0
  216. data/guides/rails_guides.rb +44 -0
  217. data/guides/rails_guides/generator.rb +248 -0
  218. data/guides/rails_guides/helpers.rb +51 -0
  219. data/guides/rails_guides/indexer.rb +68 -0
  220. data/guides/rails_guides/kindle.rb +119 -0
  221. data/guides/rails_guides/levenshtein.rb +31 -0
  222. data/guides/rails_guides/markdown.rb +163 -0
  223. data/guides/rails_guides/markdown/renderer.rb +82 -0
  224. data/guides/source/2_2_release_notes.md +435 -0
  225. data/guides/source/2_3_release_notes.md +621 -0
  226. data/guides/source/3_0_release_notes.md +614 -0
  227. data/guides/source/3_1_release_notes.md +556 -0
  228. data/guides/source/3_2_release_notes.md +565 -0
  229. data/guides/source/4_0_release_notes.md +228 -0
  230. data/guides/source/_license.html.erb +2 -0
  231. data/guides/source/_welcome.html.erb +19 -0
  232. data/guides/source/action_controller_overview.md +872 -0
  233. data/guides/source/action_mailer_basics.md +599 -0
  234. data/guides/source/action_view_overview.md +1565 -0
  235. data/guides/source/active_model_basics.md +200 -0
  236. data/guides/source/active_record_basics.md +370 -0
  237. data/guides/source/active_record_callbacks.md +358 -0
  238. data/guides/source/active_record_querying.md +1621 -0
  239. data/guides/source/active_record_validations.md +1128 -0
  240. data/guides/source/active_support_core_extensions.md +3791 -0
  241. data/guides/source/active_support_instrumentation.md +487 -0
  242. data/guides/source/api_documentation_guidelines.md +209 -0
  243. data/guides/source/asset_pipeline.md +832 -0
  244. data/guides/source/association_basics.md +2129 -0
  245. data/guides/source/caching_with_rails.md +350 -0
  246. data/guides/source/command_line.md +594 -0
  247. data/guides/source/configuring.md +736 -0
  248. data/guides/source/contributing_to_ruby_on_rails.md +455 -0
  249. data/guides/source/credits.html.erb +76 -0
  250. data/guides/source/debugging_rails_applications.md +675 -0
  251. data/guides/source/development_dependencies_install.md +195 -0
  252. data/guides/source/documents.yaml +179 -0
  253. data/guides/source/engines.md +961 -0
  254. data/guides/source/form_helpers.md +955 -0
  255. data/guides/source/generators.md +644 -0
  256. data/guides/source/getting_started.md +1775 -0
  257. data/guides/source/i18n.md +983 -0
  258. data/guides/source/index.html.erb +27 -0
  259. data/guides/source/initialization.md +562 -0
  260. data/guides/source/kindle/KINDLE.md +26 -0
  261. data/guides/source/kindle/copyright.html.erb +1 -0
  262. data/guides/source/kindle/layout.html.erb +27 -0
  263. data/guides/source/kindle/rails_guides.opf.erb +52 -0
  264. data/guides/source/kindle/toc.html.erb +24 -0
  265. data/guides/source/kindle/toc.ncx.erb +64 -0
  266. data/guides/source/kindle/welcome.html.erb +5 -0
  267. data/guides/source/layout.html.erb +148 -0
  268. data/guides/source/layouts_and_rendering.md +1132 -0
  269. data/guides/source/migrations.md +1059 -0
  270. data/guides/source/nested_model_forms.md +225 -0
  271. data/guides/source/plugins.md +435 -0
  272. data/guides/source/rails_application_templates.md +229 -0
  273. data/guides/source/rails_on_rack.md +342 -0
  274. data/guides/source/routing.md +1088 -0
  275. data/guides/source/ruby_on_rails_guides_guidelines.md +124 -0
  276. data/guides/source/security.md +973 -0
  277. data/guides/source/testing.md +981 -0
  278. data/guides/source/upgrading_ruby_on_rails.md +286 -0
  279. data/guides/source/working_with_javascript_in_rails.md +396 -0
  280. data/guides/w3c_validator.rb +95 -0
  281. metadata +315 -31
@@ -0,0 +1,1088 @@
1
+ Rails Routing from the Outside In
2
+ =================================
3
+
4
+ This guide covers the user-facing features of Rails routing.
5
+
6
+ After reading this guide, you will know:
7
+
8
+ * How to interpret the code in `routes.rb`.
9
+ * How to construct your own routes, using either the preferred resourceful style or the `match` method.
10
+ * What parameters to expect an action to receive.
11
+ * How to automatically create paths and URLs using route helpers.
12
+ * Advanced techniques such as constraints and Rack endpoints.
13
+
14
+ --------------------------------------------------------------------------------
15
+
16
+ The Purpose of the Rails Router
17
+ -------------------------------
18
+
19
+ 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.
20
+
21
+ ### Connecting URLs to Code
22
+
23
+ When your Rails application receives an incoming request for:
24
+
25
+ ```
26
+ GET /patients/17
27
+ ```
28
+
29
+ it asks the router to match it to a controller action. If the first matching route is:
30
+
31
+ ```ruby
32
+ get '/patients/:id', to: 'patients#show'
33
+ ```
34
+
35
+ the request is dispatched to the `patients` controller's `show` action with `{ id: '17' }` in `params`.
36
+
37
+ ### Generating Paths and URLs from Code
38
+
39
+ You can also generate paths and URLs. If the route above is modified to be:
40
+
41
+ ```ruby
42
+ get '/patients/:id', to: 'patients#show', as: 'patient'
43
+ ```
44
+
45
+ and your application contains this code in the controller:
46
+
47
+ ```ruby
48
+ @patient = Patient.find(17)
49
+ ```
50
+
51
+ and this in the corresponding view:
52
+
53
+ ```erb
54
+ <%= link_to 'Patient Record', patient_path(@patient) %>
55
+ ```
56
+
57
+ then the router will generate the path `/patients/17`. This reduces the brittleness of your view and makes your code easier to understand. Note that the id does not need to be specified in the route helper.
58
+
59
+ Resource Routing: the Rails Default
60
+ -----------------------------------
61
+
62
+ Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. Instead of declaring separate routes for your `index`, `show`, `new`, `edit`, `create`, `update` and `destroy` actions, a resourceful route declares them in a single line of code.
63
+
64
+ ### Resources on the Web
65
+
66
+ Browsers request pages from Rails by making a request for a URL using a specific HTTP method, such as `GET`, `POST`, `PATCH`, `PUT` and `DELETE`. Each method is a request to perform an operation on the resource. A resource route maps a number of related requests to actions in a single controller.
67
+
68
+ When your Rails application receives an incoming request for:
69
+
70
+ ```
71
+ DELETE /photos/17
72
+ ```
73
+
74
+ it asks the router to map it to a controller action. If the first matching route is:
75
+
76
+ ```ruby
77
+ resources :photos
78
+ ```
79
+
80
+ Rails would dispatch that request to the `destroy` method on the `photos` controller with `{ id: '17' }` in `params`.
81
+
82
+ ### CRUD, Verbs, and Actions
83
+
84
+ 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:
85
+
86
+ ```ruby
87
+ resources :photos
88
+ ```
89
+
90
+ creates seven different routes in your application, all mapping to the `Photos` controller:
91
+
92
+ | HTTP Verb | Path | Action | Used for |
93
+ | --------- | ---------------- | ------- | -------------------------------------------- |
94
+ | GET | /photos | index | display a list of all photos |
95
+ | GET | /photos/new | new | return an HTML form for creating a new photo |
96
+ | POST | /photos | create | create a new photo |
97
+ | GET | /photos/:id | show | display a specific photo |
98
+ | GET | /photos/:id/edit | edit | return an HTML form for editing a photo |
99
+ | PATCH/PUT | /photos/:id | update | update a specific photo |
100
+ | DELETE | /photos/:id | destroy | delete a specific photo |
101
+
102
+ NOTE: Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions.
103
+
104
+ NOTE: Rails routes are matched in the order they are specified, so if you have a `resources :photos` above a `get 'photos/poll'` the `show` action's route for the `resources` line will be matched before the `get` line. To fix this, move the `get` line **above** the `resources` line so that it is matched first.
105
+
106
+ ### Path and URL Helpers
107
+
108
+ Creating a resourceful route will also expose a number of helpers to the controllers in your application. In the case of `resources :photos`:
109
+
110
+ * `photos_path` returns `/photos`
111
+ * `new_photo_path` returns `/photos/new`
112
+ * `edit_photo_path(:id)` returns `/photos/:id/edit` (for instance, `edit_photo_path(10)` returns `/photos/10/edit`)
113
+ * `photo_path(:id)` returns `/photos/:id` (for instance, `photo_path(10)` returns `/photos/10`)
114
+
115
+ Each of these helpers has a corresponding `_url` helper (such as `photos_url`) which returns the same path prefixed with the current host, port and path prefix.
116
+
117
+ ### Defining Multiple Resources at the Same Time
118
+
119
+ If you need to create routes for more than one resource, you can save a bit of typing by defining them all with a single call to `resources`:
120
+
121
+ ```ruby
122
+ resources :photos, :books, :videos
123
+ ```
124
+
125
+ This works exactly the same as:
126
+
127
+ ```ruby
128
+ resources :photos
129
+ resources :books
130
+ resources :videos
131
+ ```
132
+
133
+ ### Singular Resources
134
+
135
+ Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like `/profile` to always show the profile of the currently logged in user. In this case, you can use a singular resource to map `/profile` (rather than `/profile/:id`) to the `show` action:
136
+
137
+ ```ruby
138
+ get 'profile', to: 'users#show'
139
+ ```
140
+
141
+ This resourceful route:
142
+
143
+ ```ruby
144
+ resource :geocoder
145
+ ```
146
+
147
+ creates six different routes in your application, all mapping to the `Geocoders` controller:
148
+
149
+ | HTTP Verb | Path | Action | Used for |
150
+ | --------- | -------------- | ------- | --------------------------------------------- |
151
+ | GET | /geocoder/new | new | return an HTML form for creating the geocoder |
152
+ | POST | /geocoder | create | create the new geocoder |
153
+ | GET | /geocoder | show | display the one and only geocoder resource |
154
+ | GET | /geocoder/edit | edit | return an HTML form for editing the geocoder |
155
+ | PATCH/PUT | /geocoder | update | update the one and only geocoder resource |
156
+ | DELETE | /geocoder | destroy | delete the geocoder resource |
157
+
158
+ NOTE: Because you might want to use the same controller for a singular route (`/account`) and a plural route (`/accounts/45`), singular resources map to plural controllers.
159
+
160
+ A singular resourceful route generates these helpers:
161
+
162
+ * `new_geocoder_path` returns `/geocoder/new`
163
+ * `edit_geocoder_path` returns `/geocoder/edit`
164
+ * `geocoder_path` returns `/geocoder`
165
+
166
+ As with plural resources, the same helpers ending in `_url` will also include the host, port and path prefix.
167
+
168
+ ### Controller Namespaces and Routing
169
+
170
+ You may wish to organize groups of controllers under a namespace. Most commonly, you might group a number of administrative controllers under an `Admin::` namespace. You would place these controllers under the `app/controllers/admin` directory, and you can group them together in your router:
171
+
172
+ ```ruby
173
+ namespace :admin do
174
+ resources :posts, :comments
175
+ end
176
+ ```
177
+
178
+ This will create a number of routes for each of the `posts` and `comments` controller. For `Admin::PostsController`, Rails will create:
179
+
180
+ | HTTP Verb | Path | Action | Used for |
181
+ | --------- | --------------------- | ------- | ------------------------- |
182
+ | GET | /admin/posts | index | admin_posts_path |
183
+ | GET | /admin/posts/new | new | new_admin_post_path |
184
+ | POST | /admin/posts | create | admin_posts_path |
185
+ | GET | /admin/posts/:id | show | admin_post_path(:id) |
186
+ | GET | /admin/posts/:id/edit | edit | edit_admin_post_path(:id) |
187
+ | PATCH/PUT | /admin/posts/:id | update | admin_post_path(:id) |
188
+ | DELETE | /admin/posts/:id | destroy | admin_post_path(:id) |
189
+
190
+ If you want to route `/posts` (without the prefix `/admin`) to `Admin::PostsController`, you could use:
191
+
192
+ ```ruby
193
+ scope module: 'admin' do
194
+ resources :posts, :comments
195
+ end
196
+ ```
197
+
198
+ or, for a single case:
199
+
200
+ ```ruby
201
+ resources :posts, module: 'admin'
202
+ ```
203
+
204
+ If you want to route `/admin/posts` to `PostsController` (without the `Admin::` module prefix), you could use:
205
+
206
+ ```ruby
207
+ scope '/admin' do
208
+ resources :posts, :comments
209
+ end
210
+ ```
211
+
212
+ or, for a single case:
213
+
214
+ ```ruby
215
+ resources :posts, path: '/admin/posts'
216
+ ```
217
+
218
+ In each of these cases, the named routes remain the same as if you did not use `scope`. In the last case, the following paths map to `PostsController`:
219
+
220
+ | HTTP Verb | Path | Action | Named Helper |
221
+ | --------- | --------------------- | ------- | ------------------- |
222
+ | GET | /admin/posts | index | posts_path |
223
+ | GET | /admin/posts/new | new | new_post_path |
224
+ | POST | /admin/posts | create | posts_path |
225
+ | GET | /admin/posts/:id | show | post_path(:id) |
226
+ | GET | /admin/posts/:id/edit | edit | edit_post_path(:id) |
227
+ | PATCH/PUT | /admin/posts/:id | update | post_path(:id) |
228
+ | DELETE | /admin/posts/:id | destroy | post_path(:id) |
229
+
230
+ ### Nested Resources
231
+
232
+ It's common to have resources that are logically children of other resources. For example, suppose your application includes these models:
233
+
234
+ ```ruby
235
+ class Magazine < ActiveRecord::Base
236
+ has_many :ads
237
+ end
238
+
239
+ class Ad < ActiveRecord::Base
240
+ belongs_to :magazine
241
+ end
242
+ ```
243
+
244
+ Nested routes allow you to capture this relationship in your routing. In this case, you could include this route declaration:
245
+
246
+ ```ruby
247
+ resources :magazines do
248
+ resources :ads
249
+ end
250
+ ```
251
+
252
+ In addition to the routes for magazines, this declaration will also route ads to an `AdsController`. The ad URLs require a magazine:
253
+
254
+ | HTTP Verb | Path | Action | Used for |
255
+ | --------- | ------------------------------------ | ------- | -------------------------------------------------------------------------- |
256
+ | GET | /magazines/:magazine_id/ads | index | display a list of all ads for a specific magazine |
257
+ | GET | /magazines/:magazine_id/ads/new | new | return an HTML form for creating a new ad belonging to a specific magazine |
258
+ | POST | /magazines/:magazine_id/ads | create | create a new ad belonging to a specific magazine |
259
+ | GET | /magazines/:magazine_id/ads/:id | show | display a specific ad belonging to a specific magazine |
260
+ | GET | /magazines/:magazine_id/ads/:id/edit | edit | return an HTML form for editing an ad belonging to a specific magazine |
261
+ | PATCH/PUT | /magazines/:magazine_id/ads/:id | update | update a specific ad belonging to a specific magazine |
262
+ | DELETE | /magazines/:magazine_id/ads/:id | destroy | delete a specific ad belonging to a specific magazine |
263
+
264
+ This will also create routing helpers such as `magazine_ads_url` and `edit_magazine_ad_path`. These helpers take an instance of Magazine as the first parameter (`magazine_ads_url(@magazine)`).
265
+
266
+ #### Limits to Nesting
267
+
268
+ You can nest resources within other nested resources if you like. For example:
269
+
270
+ ```ruby
271
+ resources :publishers do
272
+ resources :magazines do
273
+ resources :photos
274
+ end
275
+ end
276
+ ```
277
+
278
+ Deeply-nested resources quickly become cumbersome. In this case, for example, the application would recognize paths such as:
279
+
280
+ ```
281
+ /publishers/1/magazines/2/photos/3
282
+ ```
283
+
284
+ The corresponding route helper would be `publisher_magazine_photo_url`, requiring you to specify objects at all three levels. Indeed, this situation is confusing enough that a popular [article](http://weblog.jamisbuck.org/2007/2/5/nesting-resources) by Jamis Buck proposes a rule of thumb for good Rails design:
285
+
286
+ TIP: _Resources should never be nested more than 1 level deep._
287
+
288
+ #### Shallow Nesting
289
+
290
+ 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:
291
+
292
+ ```ruby
293
+ resources :posts do
294
+ resources :comments, only: [:index, :new, :create]
295
+ end
296
+ resources :comments, only: [:show, :edit, :update, :destroy]
297
+ ```
298
+
299
+ This idea strikes a balance between descriptive routes and deep nesting. There exists shorthand syntax to achieve just that, via the `:shallow` option:
300
+
301
+ ```ruby
302
+ resources :posts do
303
+ resources :comments, shallow: true
304
+ end
305
+ ```
306
+
307
+ This will generate the exact same routes as the first example. You can also specify the `:shallow` option in the parent resource, in which case all of the nested resources will be shallow:
308
+
309
+ ```ruby
310
+ resources :posts, shallow: true do
311
+ resources :comments
312
+ resources :quotes
313
+ resources :drafts
314
+ end
315
+ ```
316
+
317
+ The `shallow` method of the DSL creates a scope inside of which every nesting is shallow. This generates the same routes as the previous example:
318
+
319
+ ```ruby
320
+ shallow do
321
+ resources :posts do
322
+ resources :comments
323
+ resources :quotes
324
+ resources :drafts
325
+ end
326
+ end
327
+ ```
328
+
329
+ There exists two options for `scope` to customize shallow routes. `:shallow_path` prefixes member paths with the specified parameter:
330
+
331
+ ```ruby
332
+ scope shallow_path: "sekret" do
333
+ resources :posts do
334
+ resources :comments, shallow: true
335
+ end
336
+ end
337
+ ```
338
+
339
+ The comments resource here will have the following routes generated for it:
340
+
341
+ | HTTP Verb | Path | Named Helper |
342
+ | --------- | -------------------------------------- | ------------------- |
343
+ | GET | /posts/:post_id/comments(.:format) | post_comments |
344
+ | POST | /posts/:post_id/comments(.:format) | post_comments |
345
+ | GET | /posts/:post_id/comments/new(.:format) | new_post_comment |
346
+ | GET | /sekret/comments/:id/edit(.:format) | edit_comment |
347
+ | GET | /sekret/comments/:id(.:format) | comment |
348
+ | PATCH/PUT | /sekret/comments/:id(.:format) | comment |
349
+ | DELETE | /sekret/comments/:id(.:format) | comment |
350
+
351
+ The `:shallow_prefix` option adds the specified parameter to the named helpers:
352
+
353
+ ```ruby
354
+ scope shallow_prefix: "sekret" do
355
+ resources :posts do
356
+ resources :comments, shallow: true
357
+ end
358
+ end
359
+ ```
360
+
361
+ The comments resource here will have the following routes generated for it:
362
+
363
+ | HTTP Verb | Path | Named Helper |
364
+ | --------- | -------------------------------------- | ------------------- |
365
+ | GET | /posts/:post_id/comments(.:format) | post_comments |
366
+ | POST | /posts/:post_id/comments(.:format) | post_comments |
367
+ | GET | /posts/:post_id/comments/new(.:format) | new_post_comment |
368
+ | GET | /comments/:id/edit(.:format) | edit_sekret_comment |
369
+ | GET | /comments/:id(.:format) | sekret_comment |
370
+ | PATCH/PUT | /comments/:id(.:format) | sekret_comment |
371
+ | DELETE | /comments/:id(.:format) | sekret_comment |
372
+
373
+ ### Routing concerns
374
+
375
+ Routing Concerns allows you to declare common routes that can be reused inside others resources and routes. To define a concern:
376
+
377
+ ```ruby
378
+ concern :commentable do
379
+ resources :comments
380
+ end
381
+
382
+ concern :image_attachable do
383
+ resources :images, only: :index
384
+ end
385
+ ```
386
+
387
+ These concerns can be used in resources to avoid code duplication and share behavior across routes:
388
+
389
+ ```ruby
390
+ resources :messages, concerns: :commentable
391
+
392
+ resources :posts, concerns: [:commentable, :image_attachable]
393
+ ```
394
+
395
+ The above is equivalent to:
396
+
397
+ ```ruby
398
+ resources :messages do
399
+ resources :comments
400
+ end
401
+
402
+ resources :posts do
403
+ resources :comments
404
+ resources :images, only: :index
405
+ end
406
+ ```
407
+
408
+ Also you can use them in any place that you want inside the routes, for example in a scope or namespace call:
409
+
410
+ ```ruby
411
+ namespace :posts do
412
+ concerns :commentable
413
+ end
414
+ ```
415
+
416
+ ### Creating Paths and URLs From Objects
417
+
418
+ 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:
419
+
420
+ ```ruby
421
+ resources :magazines do
422
+ resources :ads
423
+ end
424
+ ```
425
+
426
+ When using `magazine_ad_path`, you can pass in instances of `Magazine` and `Ad` instead of the numeric IDs:
427
+
428
+ ```erb
429
+ <%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %>
430
+ ```
431
+
432
+ You can also use `url_for` with a set of objects, and Rails will automatically determine which route you want:
433
+
434
+ ```erb
435
+ <%= link_to 'Ad details', url_for([@magazine, @ad]) %>
436
+ ```
437
+
438
+ In this case, Rails will see that `@magazine` is a `Magazine` and `@ad` is an `Ad` and will therefore use the `magazine_ad_path` helper. In helpers like `link_to`, you can specify just the object in place of the full `url_for` call:
439
+
440
+ ```erb
441
+ <%= link_to 'Ad details', [@magazine, @ad] %>
442
+ ```
443
+
444
+ If you wanted to link to just a magazine:
445
+
446
+ ```erb
447
+ <%= link_to 'Magazine details', @magazine %>
448
+ ```
449
+
450
+ For other actions, you just need to insert the action name as the first element of the array:
451
+
452
+ ```erb
453
+ <%= link_to 'Edit Ad', [:edit, @magazine, @ad] %>
454
+ ```
455
+
456
+ This allows you to treat instances of your models as URLs, and is a key advantage to using the resourceful style.
457
+
458
+ ### Adding More RESTful Actions
459
+
460
+ 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.
461
+
462
+ #### Adding Member Routes
463
+
464
+ To add a member route, just add a `member` block into the resource block:
465
+
466
+ ```ruby
467
+ resources :photos do
468
+ member do
469
+ get 'preview'
470
+ end
471
+ end
472
+ ```
473
+
474
+ This will recognize `/photos/1/preview` with GET, and route to the `preview` action of `PhotosController`, with the resource id value passed in `params[:id]`. It will also create the `preview_photo_url` and `preview_photo_path` helpers.
475
+
476
+ Within the block of member routes, each route name specifies the HTTP verb that it will recognize. You can use `get`, `patch`, `put`, `post`, or `delete` here. If you don't have multiple `member` routes, you can also pass `:on` to a route, eliminating the block:
477
+
478
+ ```ruby
479
+ resources :photos do
480
+ get 'preview', on: :member
481
+ end
482
+ ```
483
+
484
+ You can leave out the `:on` option, this will create the same member route except that the resource id value will be available in `params[:photo_id]` instead of `params[:id]`.
485
+
486
+ #### Adding Collection Routes
487
+
488
+ To add a route to the collection:
489
+
490
+ ```ruby
491
+ resources :photos do
492
+ collection do
493
+ get 'search'
494
+ end
495
+ end
496
+ ```
497
+
498
+ This will enable Rails to recognize paths such as `/photos/search` with GET, and route to the `search` action of `PhotosController`. It will also create the `search_photos_url` and `search_photos_path` route helpers.
499
+
500
+ Just as with member routes, you can pass `:on` to a route:
501
+
502
+ ```ruby
503
+ resources :photos do
504
+ get 'search', on: :collection
505
+ end
506
+ ```
507
+
508
+ #### Adding Routes for Additional New Actions
509
+
510
+ To add an alternate new action using the `:on` shortcut:
511
+
512
+ ```ruby
513
+ resources :comments do
514
+ get 'preview', on: :new
515
+ end
516
+ ```
517
+
518
+ This will enable Rails to recognize paths such as `/comments/new/preview` with GET, and route to the `preview` action of `CommentsController`. It will also create the `preview_new_comment_url` and `preview_new_comment_path` route helpers.
519
+
520
+ TIP: 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.
521
+
522
+ Non-Resourceful Routes
523
+ ----------------------
524
+
525
+ 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.
526
+
527
+ 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.
528
+
529
+ In particular, simple routing makes it very easy to map legacy URLs to new Rails actions.
530
+
531
+ ### Bound Parameters
532
+
533
+ When you set up a regular route, you supply a series of symbols that Rails maps to parts of an incoming HTTP request. Two of these symbols are special: `:controller` maps to the name of a controller in your application, and `:action` maps to the name of an action within that controller. For example, consider one of the default Rails routes:
534
+
535
+ ```ruby
536
+ get ':controller(/:action(/:id))'
537
+ ```
538
+
539
+ If an incoming request of `/photos/show/1` is processed by this route (because it hasn't matched any previous route in the file), then the result will be to invoke the `show` action of the `PhotosController`, and to make the final parameter `"1"` available as `params[:id]`. This route will also route the incoming request of `/photos` to `PhotosController#index`, since `:action` and `:id` are optional parameters, denoted by parentheses.
540
+
541
+ ### Dynamic Segments
542
+
543
+ You can set up as many dynamic segments within a regular route as you like. Anything other than `:controller` or `:action` will be available to the action as part of `params`. If you set up this route:
544
+
545
+ ```ruby
546
+ get ':controller/:action/:id/:user_id'
547
+ ```
548
+
549
+ An incoming path of `/photos/show/1/2` will be dispatched to the `show` action of the `PhotosController`. `params[:id]` will be `"1"`, and `params[:user_id]` will be `"2"`.
550
+
551
+ NOTE: You can't use `:namespace` or `:module` with a `:controller` path segment. If you need to do this then use a constraint on :controller that matches the namespace you require. e.g:
552
+
553
+ ```ruby
554
+ get ':controller(/:action(/:id))', controller: /admin\/[^\/]+/
555
+ ```
556
+
557
+ TIP: By default, dynamic segments don't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within a dynamic segment, add a constraint that overrides this – for example, `id: /[^\/]+/` allows anything except a slash.
558
+
559
+ ### Static Segments
560
+
561
+ You can specify static segments when creating a route by not prepending a colon to a fragment:
562
+
563
+ ```ruby
564
+ get ':controller/:action/:id/with_user/:user_id'
565
+ ```
566
+
567
+ This route would respond to paths such as `/photos/show/1/with_user/2`. In this case, `params` would be `{ controller: 'photos', action: 'show', id: '1', user_id: '2' }`.
568
+
569
+ ### The Query String
570
+
571
+ The `params` will also include any parameters from the query string. For example, with this route:
572
+
573
+ ```ruby
574
+ get ':controller/:action/:id'
575
+ ```
576
+
577
+ An incoming path of `/photos/show/1?user_id=2` will be dispatched to the `show` action of the `Photos` controller. `params` will be `{ controller: 'photos', action: 'show', id: '1', user_id: '2' }`.
578
+
579
+ ### Defining Defaults
580
+
581
+ You do not need to explicitly use the `:controller` and `:action` symbols within a route. You can supply them as defaults:
582
+
583
+ ```ruby
584
+ get 'photos/:id', to: 'photos#show'
585
+ ```
586
+
587
+ With this route, Rails will match an incoming path of `/photos/12` to the `show` action of `PhotosController`.
588
+
589
+ You can also define other defaults in a route by supplying a hash for the `:defaults` option. This even applies to parameters that you do not specify as dynamic segments. For example:
590
+
591
+ ```ruby
592
+ get 'photos/:id', to: 'photos#show', defaults: { format: 'jpg' }
593
+ ```
594
+
595
+ Rails would match `photos/12` to the `show` action of `PhotosController`, and set `params[:format]` to `"jpg"`.
596
+
597
+ ### Naming Routes
598
+
599
+ You can specify a name for any route using the `:as` option:
600
+
601
+ ```ruby
602
+ get 'exit', to: 'sessions#destroy', as: :logout
603
+ ```
604
+
605
+ This will create `logout_path` and `logout_url` as named helpers in your application. Calling `logout_path` will return `/exit`
606
+
607
+ You can also use this to override routing methods defined by resources, like this:
608
+
609
+ ```ruby
610
+ get ':username', to: 'users#show', as: :user
611
+ ```
612
+
613
+ This will define a `user_path` method that will be available in controllers, helpers and views that will go to a route such as `/bob`. Inside the `show` action of `UsersController`, `params[:username]` will contain the username for the user. Change `:username` in the route definition if you do not want your parameter name to be `:username`.
614
+
615
+ ### HTTP Verb Constraints
616
+
617
+ In general, you should use the `get`, `post`, `put` and `delete` methods to constrain a route to a particular verb. You can use the `match` method with the `:via` option to match multiple verbs at once:
618
+
619
+ ```ruby
620
+ match 'photos', to: 'photos#show', via: [:get, :post]
621
+ ```
622
+
623
+ You can match all verbs to a particular route using `via: :all`:
624
+
625
+ ```ruby
626
+ match 'photos', to: 'photos#show', via: :all
627
+ ```
628
+
629
+ NOTE: Routing both `GET` and `POST` 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.
630
+
631
+ ### Segment Constraints
632
+
633
+ You can use the `:constraints` option to enforce a format for a dynamic segment:
634
+
635
+ ```ruby
636
+ get 'photos/:id', to: 'photos#show', constraints: { id: /[A-Z]\d{5}/ }
637
+ ```
638
+
639
+ This route would match paths such as `/photos/A12345`, but not `/photos/893`. You can more succinctly express the same route this way:
640
+
641
+ ```ruby
642
+ get 'photos/:id', to: 'photos#show', id: /[A-Z]\d{5}/
643
+ ```
644
+
645
+ `:constraints` takes regular expressions with the restriction that regexp anchors can't be used. For example, the following route will not work:
646
+
647
+ ```ruby
648
+ get '/:id', to: 'posts#show', constraints: {id: /^\d/}
649
+ ```
650
+
651
+ However, note that you don't need to use anchors because all routes are anchored at the start.
652
+
653
+ For example, the following routes would allow for `posts` with `to_param` values like `1-hello-world` that always begin with a number and `users` with `to_param` values like `david` that never begin with a number to share the root namespace:
654
+
655
+ ```ruby
656
+ get '/:id', to: 'posts#show', constraints: { id: /\d.+/ }
657
+ get '/:username', to: 'users#show'
658
+ ```
659
+
660
+ ### Request-Based Constraints
661
+
662
+ You can also constrain a route based on any method on the <a href="action_controller_overview.html#the-request-object">Request</a> object that returns a `String`.
663
+
664
+ You specify a request-based constraint the same way that you specify a segment constraint:
665
+
666
+ ```ruby
667
+ get 'photos', constraints: {subdomain: 'admin'}
668
+ ```
669
+
670
+ You can also specify constraints in a block form:
671
+
672
+ ```ruby
673
+ namespace :admin do
674
+ constraints subdomain: 'admin' do
675
+ resources :photos
676
+ end
677
+ end
678
+ ```
679
+
680
+ ### Advanced Constraints
681
+
682
+ If you have a more advanced constraint, you can provide an object that responds to `matches?` that Rails should use. Let's say you wanted to route all users on a blacklist to the `BlacklistController`. You could do:
683
+
684
+ ```ruby
685
+ class BlacklistConstraint
686
+ def initialize
687
+ @ips = Blacklist.retrieve_ips
688
+ end
689
+
690
+ def matches?(request)
691
+ @ips.include?(request.remote_ip)
692
+ end
693
+ end
694
+
695
+ TwitterClone::Application.routes.draw do
696
+ get '*path', to: 'blacklist#index',
697
+ constraints: BlacklistConstraint.new
698
+ end
699
+ ```
700
+
701
+ You can also specify constraints as a lambda:
702
+
703
+ ```ruby
704
+ TwitterClone::Application.routes.draw do
705
+ get '*path', to: 'blacklist#index',
706
+ constraints: lambda { |request| Blacklist.retrieve_ips.include?(request.remote_ip) }
707
+ end
708
+ ```
709
+
710
+ Both the `matches?` method and the lambda gets the `request` object as an argument.
711
+
712
+ ### Route Globbing and Wildcard Segments
713
+
714
+ Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example:
715
+
716
+ ```ruby
717
+ get 'photos/*other', to: 'photos#unknown'
718
+ ```
719
+
720
+ This route would match `photos/12` or `/photos/long/path/to/12`, setting `params[:other]` to `"12"` or `"long/path/to/12"`. The fragments prefixed with a star are called "wildcard segments".
721
+
722
+ Wildcard segments can occur anywhere in a route. For example:
723
+
724
+ ```ruby
725
+ get 'books/*section/:title', to: 'books#show'
726
+ ```
727
+
728
+ would match `books/some/section/last-words-a-memoir` with `params[:section]` equals `'some/section'`, and `params[:title]` equals `'last-words-a-memoir'`.
729
+
730
+ Technically, a route can have even more than one wildcard segment. The matcher assigns segments to parameters in an intuitive way. For example:
731
+
732
+ ```ruby
733
+ get '*a/foo/*b', to: 'test#index'
734
+ ```
735
+
736
+ would match `zoo/woo/foo/bar/baz` with `params[:a]` equals `'zoo/woo'`, and `params[:b]` equals `'bar/baz'`.
737
+
738
+ NOTE: By requesting `'/foo/bar.json'`, your `params[:pages]` will be equals to `'foo/bar'` with the request format of JSON. If you want the old 3.0.x behavior back, you could supply `format: false` like this:
739
+
740
+ ```ruby
741
+ get '*pages', to: 'pages#show', format: false
742
+ ```
743
+
744
+ NOTE: If you want to make the format segment mandatory, so it cannot be omitted, you can supply `format: true` like this:
745
+
746
+ ```ruby
747
+ get '*pages', to: 'pages#show', format: true
748
+ ```
749
+
750
+ ### Redirection
751
+
752
+ You can redirect any path to another path using the `redirect` helper in your router:
753
+
754
+ ```ruby
755
+ get '/stories', to: redirect('/posts')
756
+ ```
757
+
758
+ You can also reuse dynamic segments from the match in the path to redirect to:
759
+
760
+ ```ruby
761
+ get '/stories/:name', to: redirect('/posts/%{name}')
762
+ ```
763
+
764
+ You can also provide a block to redirect, which receives the params and the request object:
765
+
766
+ ```ruby
767
+ get '/stories/:name', to: redirect {|params, req| "/posts/#{params[:name].pluralize}" }
768
+ get '/stories', to: redirect {|p, req| "/posts/#{req.subdomain}" }
769
+ ```
770
+
771
+ 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.
772
+
773
+ In all of these cases, if you don't provide the leading host (`http://www.example.com`), Rails will take those details from the current request.
774
+
775
+ ### Routing to Rack Applications
776
+
777
+ Instead of a String like `'posts#index'`, which corresponds to the `index` action in the `PostsController`, you can specify any <a href="rails_on_rack.html">Rack application</a> as the endpoint for a matcher:
778
+
779
+ ```ruby
780
+ match '/application.js', to: Sprockets, via: :all
781
+ ```
782
+
783
+ As long as `Sprockets` responds to `call` and returns a `[status, headers, body]`, the router won't know the difference between the Rack application and an action. This is an appropriate use of `via: :all`, as you will want to allow your Rack application to handle all verbs as it considers appropriate.
784
+
785
+ NOTE: For the curious, `'posts#index'` actually expands out to `PostsController.action(:index)`, which returns a valid Rack application.
786
+
787
+ ### Using `root`
788
+
789
+ You can specify what Rails should route `'/'` to with the `root` method:
790
+
791
+ ```ruby
792
+ root to: 'pages#main'
793
+ root 'pages#main' # shortcut for the above
794
+ ```
795
+
796
+ You should put the `root` route at the top of the file, because it is the most popular route and should be matched first.
797
+
798
+ NOTE: The `root` route only routes `GET` requests to the action.
799
+
800
+ ### Unicode character routes
801
+
802
+ You can specify unicode character routes directly. For example:
803
+
804
+ ```ruby
805
+ get 'こんにちは', to: 'welcome#index'
806
+ ```
807
+
808
+ Customizing Resourceful Routes
809
+ ------------------------------
810
+
811
+ While the default routes and helpers generated by `resources :posts` will usually serve you well, you may want to customize them in some way. Rails allows you to customize virtually any generic part of the resourceful helpers.
812
+
813
+ ### Specifying a Controller to Use
814
+
815
+ The `:controller` option lets you explicitly specify a controller to use for the resource. For example:
816
+
817
+ ```ruby
818
+ resources :photos, controller: 'images'
819
+ ```
820
+
821
+ will recognize incoming paths beginning with `/photos` but route to the `Images` controller:
822
+
823
+ | HTTP Verb | Path | Action | Named Helper |
824
+ | --------- | ---------------- | ------- | -------------------- |
825
+ | GET | /photos | index | photos_path |
826
+ | GET | /photos/new | new | new_photo_path |
827
+ | POST | /photos | create | photos_path |
828
+ | GET | /photos/:id | show | photo_path(:id) |
829
+ | GET | /photos/:id/edit | edit | edit_photo_path(:id) |
830
+ | PATCH/PUT | /photos/:id | update | photo_path(:id) |
831
+ | DELETE | /photos/:id | destroy | photo_path(:id) |
832
+
833
+ NOTE: Use `photos_path`, `new_photo_path`, etc. to generate paths for this resource.
834
+
835
+ For namespaced controllers you can use the directory notation. For example:
836
+
837
+ ```ruby
838
+ resources :user_permissions, controller: 'admin/user_permissions'
839
+ ```
840
+
841
+ This will route to the `Admin::UserPermissions` controller.
842
+
843
+ NOTE: Only the directory notation is supported. specifying the
844
+ controller with ruby constant notation (eg. `:controller =>
845
+ 'Admin::UserPermissions'`) can lead to routing problems and results in
846
+ a warning.
847
+
848
+ ### Specifying Constraints
849
+
850
+ You can use the `:constraints` option to specify a required format on the implicit `id`. For example:
851
+
852
+ ```ruby
853
+ resources :photos, constraints: {id: /[A-Z][A-Z][0-9]+/}
854
+ ```
855
+
856
+ This declaration constrains the `:id` parameter to match the supplied regular expression. So, in this case, the router would no longer match `/photos/1` to this route. Instead, `/photos/RR27` would match.
857
+
858
+ You can specify a single constraint to apply to a number of routes by using the block form:
859
+
860
+ ```ruby
861
+ constraints(id: /[A-Z][A-Z][0-9]+/) do
862
+ resources :photos
863
+ resources :accounts
864
+ end
865
+ ```
866
+
867
+ NOTE: Of course, you can use the more advanced constraints available in non-resourceful routes in this context.
868
+
869
+ TIP: By default the `:id` parameter doesn't accept dots - this is because the dot is used as a separator for formatted routes. If you need to use a dot within an `:id` add a constraint which overrides this - for example `id: /[^\/]+/` allows anything except a slash.
870
+
871
+ ### Overriding the Named Helpers
872
+
873
+ The `:as` option lets you override the normal naming for the named route helpers. For example:
874
+
875
+ ```ruby
876
+ resources :photos, as: 'images'
877
+ ```
878
+
879
+ will recognize incoming paths beginning with `/photos` and route the requests to `PhotosController`, but use the value of the :as option to name the helpers.
880
+
881
+ | HTTP Verb | Path | Action | Named Helper |
882
+ | --------- | ---------------- | ------- | -------------------- |
883
+ | GET | /photos | index | images_path |
884
+ | GET | /photos/new | new | new_image_path |
885
+ | POST | /photos | create | images_path |
886
+ | GET | /photos/:id | show | image_path(:id) |
887
+ | GET | /photos/:id/edit | edit | edit_image_path(:id) |
888
+ | PATCH/PUT | /photos/:id | update | image_path(:id) |
889
+ | DELETE | /photos/:id | destroy | image_path(:id) |
890
+
891
+ ### Overriding the `new` and `edit` Segments
892
+
893
+ The `:path_names` option lets you override the automatically-generated "new" and "edit" segments in paths:
894
+
895
+ ```ruby
896
+ resources :photos, path_names: { new: 'make', edit: 'change' }
897
+ ```
898
+
899
+ This would cause the routing to recognize paths such as:
900
+
901
+ ```
902
+ /photos/make
903
+ /photos/1/change
904
+ ```
905
+
906
+ NOTE: The actual action names aren't changed by this option. The two paths shown would still route to the `new` and `edit` actions.
907
+
908
+ TIP: If you find yourself wanting to change this option uniformly for all of your routes, you can use a scope.
909
+
910
+ ```ruby
911
+ scope path_names: { new: 'make' } do
912
+ # rest of your routes
913
+ end
914
+ ```
915
+
916
+ ### Prefixing the Named Route Helpers
917
+
918
+ You can use the `:as` option to prefix the named route helpers that Rails generates for a route. Use this option to prevent name collisions between routes using a path scope. For example:
919
+
920
+ ```ruby
921
+ scope 'admin' do
922
+ resources :photos, as: 'admin_photos'
923
+ end
924
+
925
+ resources :photos
926
+ ```
927
+
928
+ This will provide route helpers such as `admin_photos_path`, `new_admin_photo_path` etc.
929
+
930
+ To prefix a group of route helpers, use `:as` with `scope`:
931
+
932
+ ```ruby
933
+ scope 'admin', as: 'admin' do
934
+ resources :photos, :accounts
935
+ end
936
+
937
+ resources :photos, :accounts
938
+ ```
939
+
940
+ This will generate routes such as `admin_photos_path` and `admin_accounts_path` which map to `/admin/photos` and `/admin/accounts` respectively.
941
+
942
+ NOTE: The `namespace` scope will automatically add `:as` as well as `:module` and `:path` prefixes.
943
+
944
+ You can prefix routes with a named parameter also:
945
+
946
+ ```ruby
947
+ scope ':username' do
948
+ resources :posts
949
+ end
950
+ ```
951
+
952
+ This will provide you with URLs such as `/bob/posts/1` and will allow you to reference the `username` part of the path as `params[:username]` in controllers, helpers and views.
953
+
954
+ ### Restricting the Routes Created
955
+
956
+ By default, Rails creates routes for the seven default actions (index, show, new, create, edit, update, and destroy) for every RESTful route in your application. You can use the `:only` and `:except` options to fine-tune this behavior. The `:only` option tells Rails to create only the specified routes:
957
+
958
+ ```ruby
959
+ resources :photos, only: [:index, :show]
960
+ ```
961
+
962
+ Now, a `GET` request to `/photos` would succeed, but a `POST` request to `/photos` (which would ordinarily be routed to the `create` action) will fail.
963
+
964
+ The `:except` option specifies a route or list of routes that Rails should _not_ create:
965
+
966
+ ```ruby
967
+ resources :photos, except: :destroy
968
+ ```
969
+
970
+ In this case, Rails will create all of the normal routes except the route for `destroy` (a `DELETE` request to `/photos/:id`).
971
+
972
+ TIP: If your application has many RESTful routes, using `:only` and `:except` to generate only the routes that you actually need can cut down on memory use and speed up the routing process.
973
+
974
+ ### Translated Paths
975
+
976
+ Using `scope`, we can alter path names generated by resources:
977
+
978
+ ```ruby
979
+ scope(path_names: { new: 'neu', edit: 'bearbeiten' }) do
980
+ resources :categories, path: 'kategorien'
981
+ end
982
+ ```
983
+
984
+ Rails now creates routes to the `CategoriesController`.
985
+
986
+ | HTTP Verb | Path | Action | Used for |
987
+ | --------- | -------------------------- | ------- | ----------------------- |
988
+ | GET | /kategorien | index | categories_path |
989
+ | GET | /kategorien/neu | new | new_category_path |
990
+ | POST | /kategorien | create | categories_path |
991
+ | GET | /kategorien/:id | show | category_path(:id) |
992
+ | GET | /kategorien/:id/bearbeiten | edit | edit_category_path(:id) |
993
+ | PATCH/PUT | /kategorien/:id | update | category_path(:id) |
994
+ | DELETE | /kategorien/:id | destroy | category_path(:id) |
995
+
996
+ ### Overriding the Singular Form
997
+
998
+ If you want to define the singular form of a resource, you should add additional rules to the `Inflector`:
999
+
1000
+ ```ruby
1001
+ ActiveSupport::Inflector.inflections do |inflect|
1002
+ inflect.irregular 'tooth', 'teeth'
1003
+ end
1004
+ ```
1005
+
1006
+ ### Using `:as` in Nested Resources
1007
+
1008
+ The `:as` option overrides the automatically-generated name for the resource in nested route helpers. For example:
1009
+
1010
+ ```ruby
1011
+ resources :magazines do
1012
+ resources :ads, as: 'periodical_ads'
1013
+ end
1014
+ ```
1015
+
1016
+ This will create routing helpers such as `magazine_periodical_ads_url` and `edit_magazine_periodical_ad_path`.
1017
+
1018
+ Inspecting and Testing Routes
1019
+ -----------------------------
1020
+
1021
+ Rails offers facilities for inspecting and testing your routes.
1022
+
1023
+ ### Listing Existing Routes
1024
+
1025
+ To get a complete list of the available routes in your application, visit `http://localhost:3000/rails/info/routes` in your browser while your server is running in the **development** environment. You can also execute the `rake routes` command in your terminal to produce the same output.
1026
+
1027
+ Both methods will list all of your routes, in the same order that they appear in `routes.rb`. For each route, you'll see:
1028
+
1029
+ * The route name (if any)
1030
+ * The HTTP verb used (if the route doesn't respond to all verbs)
1031
+ * The URL pattern to match
1032
+ * The routing parameters for the route
1033
+
1034
+ For example, here's a small section of the `rake routes` output for a RESTful route:
1035
+
1036
+ ```
1037
+ users GET /users(.:format) users#index
1038
+ POST /users(.:format) users#create
1039
+ new_user GET /users/new(.:format) users#new
1040
+ edit_user GET /users/:id/edit(.:format) users#edit
1041
+ ```
1042
+
1043
+ You may restrict the listing to the routes that map to a particular controller setting the `CONTROLLER` environment variable:
1044
+
1045
+ ```bash
1046
+ $ CONTROLLER=users rake routes
1047
+ ```
1048
+
1049
+ TIP: You'll find that the output from `rake routes` is much more readable if you widen your terminal window until the output lines don't wrap.
1050
+
1051
+ ### Testing Routes
1052
+
1053
+ Routes should be included in your testing strategy (just like the rest of your application). Rails offers three [built-in assertions](http://api.rubyonrails.org/classes/ActionDispatch/Assertions/RoutingAssertions.html) designed to make testing routes simpler:
1054
+
1055
+ * `assert_generates`
1056
+ * `assert_recognizes`
1057
+ * `assert_routing`
1058
+
1059
+ #### The `assert_generates` Assertion
1060
+
1061
+ `assert_generates` asserts that a particular set of options generate a particular path and can be used with default routes or custom routes. For example:
1062
+
1063
+ ```ruby
1064
+ assert_generates '/photos/1', { controller: 'photos', action: 'show', id: '1' }
1065
+ assert_generates '/about', controller: 'pages', action: 'about'
1066
+ ```
1067
+
1068
+ #### The `assert_recognizes` Assertion
1069
+
1070
+ `assert_recognizes` is the inverse of `assert_generates`. It asserts that a given path is recognized and routes it to a particular spot in your application. For example:
1071
+
1072
+ ```ruby
1073
+ assert_recognizes({ controller: 'photos', action: 'show', id: '1' }, '/photos/1')
1074
+ ```
1075
+
1076
+ You can supply a `:method` argument to specify the HTTP verb:
1077
+
1078
+ ```ruby
1079
+ assert_recognizes({ controller: 'photos', action: 'create' }, { path: 'photos', method: :post })
1080
+ ```
1081
+
1082
+ #### The `assert_routing` Assertion
1083
+
1084
+ The `assert_routing` assertion checks the route both ways: it tests that the path generates the options, and that the options generate the path. Thus, it combines the functions of `assert_generates` and `assert_recognizes`:
1085
+
1086
+ ```ruby
1087
+ assert_routing({ path: 'photos', method: :post }, { controller: 'photos', action: 'create' })
1088
+ ```