rails 4.1.4 → 6.0.0

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

Potentially problematic release.


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

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