rails 4.2.6.rc1 → 5.2.8.1

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 (218) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +24 -12
  3. metadata +54 -248
  4. data/guides/CHANGELOG.md +0 -73
  5. data/guides/Rakefile +0 -79
  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 -59
  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 -713
  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 -54
  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/bug_report_templates/generic_gem.rb +0 -15
  148. data/guides/bug_report_templates/generic_master.rb +0 -26
  149. data/guides/rails_guides/generator.rb +0 -248
  150. data/guides/rails_guides/helpers.rb +0 -53
  151. data/guides/rails_guides/indexer.rb +0 -68
  152. data/guides/rails_guides/kindle.rb +0 -119
  153. data/guides/rails_guides/levenshtein.rb +0 -37
  154. data/guides/rails_guides/markdown/renderer.rb +0 -82
  155. data/guides/rails_guides/markdown.rb +0 -167
  156. data/guides/rails_guides.rb +0 -63
  157. data/guides/source/2_2_release_notes.md +0 -435
  158. data/guides/source/2_3_release_notes.md +0 -621
  159. data/guides/source/3_0_release_notes.md +0 -611
  160. data/guides/source/3_1_release_notes.md +0 -559
  161. data/guides/source/3_2_release_notes.md +0 -568
  162. data/guides/source/4_0_release_notes.md +0 -279
  163. data/guides/source/4_1_release_notes.md +0 -730
  164. data/guides/source/4_2_release_notes.md +0 -877
  165. data/guides/source/_license.html.erb +0 -2
  166. data/guides/source/_welcome.html.erb +0 -23
  167. data/guides/source/action_controller_overview.md +0 -1192
  168. data/guides/source/action_mailer_basics.md +0 -757
  169. data/guides/source/action_view_overview.md +0 -1561
  170. data/guides/source/active_job_basics.md +0 -339
  171. data/guides/source/active_model_basics.md +0 -554
  172. data/guides/source/active_record_basics.md +0 -374
  173. data/guides/source/active_record_callbacks.md +0 -413
  174. data/guides/source/active_record_migrations.md +0 -1018
  175. data/guides/source/active_record_postgresql.md +0 -433
  176. data/guides/source/active_record_querying.md +0 -1781
  177. data/guides/source/active_record_validations.md +0 -1179
  178. data/guides/source/active_support_core_extensions.md +0 -3857
  179. data/guides/source/active_support_instrumentation.md +0 -488
  180. data/guides/source/api_documentation_guidelines.md +0 -361
  181. data/guides/source/asset_pipeline.md +0 -1304
  182. data/guides/source/association_basics.md +0 -2245
  183. data/guides/source/autoloading_and_reloading_constants.md +0 -1311
  184. data/guides/source/caching_with_rails.md +0 -379
  185. data/guides/source/command_line.md +0 -625
  186. data/guides/source/configuring.md +0 -1072
  187. data/guides/source/contributing_to_ruby_on_rails.md +0 -628
  188. data/guides/source/credits.html.erb +0 -80
  189. data/guides/source/debugging_rails_applications.md +0 -861
  190. data/guides/source/development_dependencies_install.md +0 -289
  191. data/guides/source/documents.yaml +0 -205
  192. data/guides/source/engines.md +0 -1412
  193. data/guides/source/form_helpers.md +0 -1024
  194. data/guides/source/generators.md +0 -676
  195. data/guides/source/getting_started.md +0 -2086
  196. data/guides/source/i18n.md +0 -1087
  197. data/guides/source/index.html.erb +0 -28
  198. data/guides/source/initialization.md +0 -704
  199. data/guides/source/kindle/copyright.html.erb +0 -1
  200. data/guides/source/kindle/layout.html.erb +0 -27
  201. data/guides/source/kindle/rails_guides.opf.erb +0 -52
  202. data/guides/source/kindle/toc.html.erb +0 -24
  203. data/guides/source/kindle/toc.ncx.erb +0 -64
  204. data/guides/source/kindle/welcome.html.erb +0 -5
  205. data/guides/source/layout.html.erb +0 -140
  206. data/guides/source/layouts_and_rendering.md +0 -1227
  207. data/guides/source/maintenance_policy.md +0 -78
  208. data/guides/source/nested_model_forms.md +0 -228
  209. data/guides/source/plugins.md +0 -444
  210. data/guides/source/rails_application_templates.md +0 -266
  211. data/guides/source/rails_on_rack.md +0 -336
  212. data/guides/source/routing.md +0 -1155
  213. data/guides/source/ruby_on_rails_guides_guidelines.md +0 -127
  214. data/guides/source/security.md +0 -1024
  215. data/guides/source/testing.md +0 -1132
  216. data/guides/source/upgrading_ruby_on_rails.md +0 -1186
  217. data/guides/source/working_with_javascript_in_rails.md +0 -407
  218. data/guides/w3c_validator.rb +0 -97
@@ -1,1155 +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 :articles, :comments
187
- end
188
- ```
189
-
190
- This will create a number of routes for each of the `articles` and `comments` controller. For `Admin::ArticlesController`, Rails will create:
191
-
192
- | HTTP Verb | Path | Controller#Action | Named Helper |
193
- | --------- | ------------------------ | ---------------------- | ---------------------------- |
194
- | GET | /admin/articles | admin/articles#index | admin_articles_path |
195
- | GET | /admin/articles/new | admin/articles#new | new_admin_article_path |
196
- | POST | /admin/articles | admin/articles#create | admin_articles_path |
197
- | GET | /admin/articles/:id | admin/articles#show | admin_article_path(:id) |
198
- | GET | /admin/articles/:id/edit | admin/articles#edit | edit_admin_article_path(:id) |
199
- | PATCH/PUT | /admin/articles/:id | admin/articles#update | admin_article_path(:id) |
200
- | DELETE | /admin/articles/:id | admin/articles#destroy | admin_article_path(:id) |
201
-
202
- If you want to route `/articles` (without the prefix `/admin`) to `Admin::ArticlesController`, you could use:
203
-
204
- ```ruby
205
- scope module: 'admin' do
206
- resources :articles, :comments
207
- end
208
- ```
209
-
210
- or, for a single case:
211
-
212
- ```ruby
213
- resources :articles, module: 'admin'
214
- ```
215
-
216
- If you want to route `/admin/articles` to `ArticlesController` (without the `Admin::` module prefix), you could use:
217
-
218
- ```ruby
219
- scope '/admin' do
220
- resources :articles, :comments
221
- end
222
- ```
223
-
224
- or, for a single case:
225
-
226
- ```ruby
227
- resources :articles, path: '/admin/articles'
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 `ArticlesController`:
231
-
232
- | HTTP Verb | Path | Controller#Action | Named Helper |
233
- | --------- | ------------------------ | -------------------- | ---------------------- |
234
- | GET | /admin/articles | articles#index | articles_path |
235
- | GET | /admin/articles/new | articles#new | new_article_path |
236
- | POST | /admin/articles | articles#create | articles_path |
237
- | GET | /admin/articles/:id | articles#show | article_path(:id) |
238
- | GET | /admin/articles/:id/edit | articles#edit | edit_article_path(:id) |
239
- | PATCH/PUT | /admin/articles/:id | articles#update | article_path(:id) |
240
- | DELETE | /admin/articles/:id | articles#destroy | article_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 :articles 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 :articles 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 :articles, 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 :articles 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 :articles 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 | /articles/:article_id/comments(.:format) | comments#index | article_comments_path |
358
- | POST | /articles/:article_id/comments(.:format) | comments#create | article_comments_path |
359
- | GET | /articles/:article_id/comments/new(.:format) | comments#new | new_article_comment_path |
360
- | GET | /sekret/comments/:id/edit(.:format) | comments#edit | edit_comment_path |
361
- | GET | /sekret/comments/:id(.:format) | comments#show | comment_path |
362
- | PATCH/PUT | /sekret/comments/:id(.:format) | comments#update | comment_path |
363
- | DELETE | /sekret/comments/:id(.:format) | comments#destroy | comment_path |
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 :articles 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 | /articles/:article_id/comments(.:format) | comments#index | article_comments_path |
380
- | POST | /articles/:article_id/comments(.:format) | comments#create | article_comments_path |
381
- | GET | /articles/:article_id/comments/new(.:format) | comments#new | new_article_comment_path |
382
- | GET | /comments/:id/edit(.:format) | comments#edit | edit_sekret_comment_path |
383
- | GET | /comments/:id(.:format) | comments#show | sekret_comment_path |
384
- | PATCH/PUT | /comments/:id(.:format) | comments#update | sekret_comment_path |
385
- | DELETE | /comments/:id(.:format) | comments#destroy | sekret_comment_path |
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 :articles, 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 :articles 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 :articles 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
- NOTE: You cannot override defaults via query parameters - this is for security reasons. The only defaults that can be overridden are dynamic segments via substitution in the URL path.
615
-
616
- ### Naming Routes
617
-
618
- You can specify a name for any route using the `:as` option:
619
-
620
- ```ruby
621
- get 'exit', to: 'sessions#destroy', as: :logout
622
- ```
623
-
624
- This will create `logout_path` and `logout_url` as named helpers in your application. Calling `logout_path` will return `/exit`
625
-
626
- You can also use this to override routing methods defined by resources, like this:
627
-
628
- ```ruby
629
- get ':username', to: 'users#show', as: :user
630
- ```
631
-
632
- 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`.
633
-
634
- ### HTTP Verb Constraints
635
-
636
- 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:
637
-
638
- ```ruby
639
- match 'photos', to: 'photos#show', via: [:get, :post]
640
- ```
641
-
642
- You can match all verbs to a particular route using `via: :all`:
643
-
644
- ```ruby
645
- match 'photos', to: 'photos#show', via: :all
646
- ```
647
-
648
- 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.
649
-
650
- NOTE: 'GET' in Rails won't check for CSRF token. You should never write to the database from 'GET' requests, for more information see the [security guide](security.html#csrf-countermeasures) on CSRF countermeasures.
651
-
652
- ### Segment Constraints
653
-
654
- You can use the `:constraints` option to enforce a format for a dynamic segment:
655
-
656
- ```ruby
657
- get 'photos/:id', to: 'photos#show', constraints: { id: /[A-Z]\d{5}/ }
658
- ```
659
-
660
- This route would match paths such as `/photos/A12345`, but not `/photos/893`. You can more succinctly express the same route this way:
661
-
662
- ```ruby
663
- get 'photos/:id', to: 'photos#show', id: /[A-Z]\d{5}/
664
- ```
665
-
666
- `:constraints` takes regular expressions with the restriction that regexp anchors can't be used. For example, the following route will not work:
667
-
668
- ```ruby
669
- get '/:id', to: 'articles#show', constraints: { id: /^\d/ }
670
- ```
671
-
672
- However, note that you don't need to use anchors because all routes are anchored at the start.
673
-
674
- For example, the following routes would allow for `articles` 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:
675
-
676
- ```ruby
677
- get '/:id', to: 'articles#show', constraints: { id: /\d.+/ }
678
- get '/:username', to: 'users#show'
679
- ```
680
-
681
- ### Request-Based Constraints
682
-
683
- You can also constrain a route based on any method on the [Request object](action_controller_overview.html#the-request-object) that returns a `String`.
684
-
685
- You specify a request-based constraint the same way that you specify a segment constraint:
686
-
687
- ```ruby
688
- get 'photos', to: 'photos#index', constraints: { subdomain: 'admin' }
689
- ```
690
-
691
- You can also specify constraints in a block form:
692
-
693
- ```ruby
694
- namespace :admin do
695
- constraints subdomain: 'admin' do
696
- resources :photos
697
- end
698
- end
699
- ```
700
-
701
- NOTE: Request constraints work by calling a method on the [Request object](action_controller_overview.html#the-request-object) with the same name as the hash key and then compare the return value with the hash value. Therefore, constraint values should match the corresponding Request object method return type. For example: `constraints: { subdomain: 'api' }` will match an `api` subdomain as expected, however using a symbol `constraints: { subdomain: :api }` will not, because `request.subdomain` returns `'api'` as a String.
702
-
703
- ### Advanced Constraints
704
-
705
- 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:
706
-
707
- ```ruby
708
- class BlacklistConstraint
709
- def initialize
710
- @ips = Blacklist.retrieve_ips
711
- end
712
-
713
- def matches?(request)
714
- @ips.include?(request.remote_ip)
715
- end
716
- end
717
-
718
- Rails.application.routes.draw do
719
- get '*path', to: 'blacklist#index',
720
- constraints: BlacklistConstraint.new
721
- end
722
- ```
723
-
724
- You can also specify constraints as a lambda:
725
-
726
- ```ruby
727
- Rails.application.routes.draw do
728
- get '*path', to: 'blacklist#index',
729
- constraints: lambda { |request| Blacklist.retrieve_ips.include?(request.remote_ip) }
730
- end
731
- ```
732
-
733
- Both the `matches?` method and the lambda gets the `request` object as an argument.
734
-
735
- ### Route Globbing and Wildcard Segments
736
-
737
- Route globbing is a way to specify that a particular parameter should be matched to all the remaining parts of a route. For example:
738
-
739
- ```ruby
740
- get 'photos/*other', to: 'photos#unknown'
741
- ```
742
-
743
- 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".
744
-
745
- Wildcard segments can occur anywhere in a route. For example:
746
-
747
- ```ruby
748
- get 'books/*section/:title', to: 'books#show'
749
- ```
750
-
751
- would match `books/some/section/last-words-a-memoir` with `params[:section]` equals `'some/section'`, and `params[:title]` equals `'last-words-a-memoir'`.
752
-
753
- Technically, a route can have even more than one wildcard segment. The matcher assigns segments to parameters in an intuitive way. For example:
754
-
755
- ```ruby
756
- get '*a/foo/*b', to: 'test#index'
757
- ```
758
-
759
- would match `zoo/woo/foo/bar/baz` with `params[:a]` equals `'zoo/woo'`, and `params[:b]` equals `'bar/baz'`.
760
-
761
- NOTE: By requesting `'/foo/bar.json'`, your `params[:pages]` will be equal 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:
762
-
763
- ```ruby
764
- get '*pages', to: 'pages#show', format: false
765
- ```
766
-
767
- NOTE: If you want to make the format segment mandatory, so it cannot be omitted, you can supply `format: true` like this:
768
-
769
- ```ruby
770
- get '*pages', to: 'pages#show', format: true
771
- ```
772
-
773
- ### Redirection
774
-
775
- You can redirect any path to another path using the `redirect` helper in your router:
776
-
777
- ```ruby
778
- get '/stories', to: redirect('/articles')
779
- ```
780
-
781
- You can also reuse dynamic segments from the match in the path to redirect to:
782
-
783
- ```ruby
784
- get '/stories/:name', to: redirect('/articles/%{name}')
785
- ```
786
-
787
- You can also provide a block to redirect, which receives the symbolized path parameters and the request object:
788
-
789
- ```ruby
790
- get '/stories/:name', to: redirect { |path_params, req| "/articles/#{path_params[:name].pluralize}" }
791
- get '/stories', to: redirect { |path_params, req| "/articles/#{req.subdomain}" }
792
- ```
793
-
794
- 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.
795
-
796
- 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.
797
-
798
- ### Routing to Rack Applications
799
-
800
- Instead of a String like `'articles#index'`, which corresponds to the `index` action in the `ArticlesController`, you can specify any [Rack application](rails_on_rack.html) as the endpoint for a matcher:
801
-
802
- ```ruby
803
- match '/application.js', to: Sprockets, via: :all
804
- ```
805
-
806
- 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.
807
-
808
- NOTE: For the curious, `'articles#index'` actually expands out to `ArticlesController.action(:index)`, which returns a valid Rack application.
809
-
810
- If you specify a rack application as the endpoint for a matcher remember that the route will be unchanged in the receiving application. With the following route your rack application should expect the route to be '/admin':
811
-
812
- ```ruby
813
- match '/admin', to: AdminApp, via: :all
814
- ```
815
-
816
- If you would prefer to have your rack application receive requests at the root path instead use mount:
817
-
818
- ```ruby
819
- mount AdminApp, at: '/admin'
820
- ```
821
-
822
- ### Using `root`
823
-
824
- You can specify what Rails should route `'/'` to with the `root` method:
825
-
826
- ```ruby
827
- root to: 'pages#main'
828
- root 'pages#main' # shortcut for the above
829
- ```
830
-
831
- You should put the `root` route at the top of the file, because it is the most popular route and should be matched first.
832
-
833
- NOTE: The `root` route only routes `GET` requests to the action.
834
-
835
- You can also use root inside namespaces and scopes as well. For example:
836
-
837
- ```ruby
838
- namespace :admin do
839
- root to: "admin#index"
840
- end
841
-
842
- root to: "home#index"
843
- ```
844
-
845
- ### Unicode character routes
846
-
847
- You can specify unicode character routes directly. For example:
848
-
849
- ```ruby
850
- get 'こんにちは', to: 'welcome#index'
851
- ```
852
-
853
- Customizing Resourceful Routes
854
- ------------------------------
855
-
856
- While the default routes and helpers generated by `resources :articles` 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.
857
-
858
- ### Specifying a Controller to Use
859
-
860
- The `:controller` option lets you explicitly specify a controller to use for the resource. For example:
861
-
862
- ```ruby
863
- resources :photos, controller: 'images'
864
- ```
865
-
866
- will recognize incoming paths beginning with `/photos` but route to the `Images` controller:
867
-
868
- | HTTP Verb | Path | Controller#Action | Named Helper |
869
- | --------- | ---------------- | ----------------- | -------------------- |
870
- | GET | /photos | images#index | photos_path |
871
- | GET | /photos/new | images#new | new_photo_path |
872
- | POST | /photos | images#create | photos_path |
873
- | GET | /photos/:id | images#show | photo_path(:id) |
874
- | GET | /photos/:id/edit | images#edit | edit_photo_path(:id) |
875
- | PATCH/PUT | /photos/:id | images#update | photo_path(:id) |
876
- | DELETE | /photos/:id | images#destroy | photo_path(:id) |
877
-
878
- NOTE: Use `photos_path`, `new_photo_path`, etc. to generate paths for this resource.
879
-
880
- For namespaced controllers you can use the directory notation. For example:
881
-
882
- ```ruby
883
- resources :user_permissions, controller: 'admin/user_permissions'
884
- ```
885
-
886
- This will route to the `Admin::UserPermissions` controller.
887
-
888
- NOTE: Only the directory notation is supported. Specifying the
889
- controller with Ruby constant notation (eg. `controller: 'Admin::UserPermissions'`)
890
- can lead to routing problems and results in
891
- a warning.
892
-
893
- ### Specifying Constraints
894
-
895
- You can use the `:constraints` option to specify a required format on the implicit `id`. For example:
896
-
897
- ```ruby
898
- resources :photos, constraints: { id: /[A-Z][A-Z][0-9]+/ }
899
- ```
900
-
901
- 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.
902
-
903
- You can specify a single constraint to apply to a number of routes by using the block form:
904
-
905
- ```ruby
906
- constraints(id: /[A-Z][A-Z][0-9]+/) do
907
- resources :photos
908
- resources :accounts
909
- end
910
- ```
911
-
912
- NOTE: Of course, you can use the more advanced constraints available in non-resourceful routes in this context.
913
-
914
- 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.
915
-
916
- ### Overriding the Named Helpers
917
-
918
- The `:as` option lets you override the normal naming for the named route helpers. For example:
919
-
920
- ```ruby
921
- resources :photos, as: 'images'
922
- ```
923
-
924
- 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.
925
-
926
- | HTTP Verb | Path | Controller#Action | Named Helper |
927
- | --------- | ---------------- | ----------------- | -------------------- |
928
- | GET | /photos | photos#index | images_path |
929
- | GET | /photos/new | photos#new | new_image_path |
930
- | POST | /photos | photos#create | images_path |
931
- | GET | /photos/:id | photos#show | image_path(:id) |
932
- | GET | /photos/:id/edit | photos#edit | edit_image_path(:id) |
933
- | PATCH/PUT | /photos/:id | photos#update | image_path(:id) |
934
- | DELETE | /photos/:id | photos#destroy | image_path(:id) |
935
-
936
- ### Overriding the `new` and `edit` Segments
937
-
938
- The `:path_names` option lets you override the automatically-generated `new` and `edit` segments in paths:
939
-
940
- ```ruby
941
- resources :photos, path_names: { new: 'make', edit: 'change' }
942
- ```
943
-
944
- This would cause the routing to recognize paths such as:
945
-
946
- ```
947
- /photos/make
948
- /photos/1/change
949
- ```
950
-
951
- NOTE: The actual action names aren't changed by this option. The two paths shown would still route to the `new` and `edit` actions.
952
-
953
- TIP: If you find yourself wanting to change this option uniformly for all of your routes, you can use a scope.
954
-
955
- ```ruby
956
- scope path_names: { new: 'make' } do
957
- # rest of your routes
958
- end
959
- ```
960
-
961
- ### Prefixing the Named Route Helpers
962
-
963
- 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:
964
-
965
- ```ruby
966
- scope 'admin' do
967
- resources :photos, as: 'admin_photos'
968
- end
969
-
970
- resources :photos
971
- ```
972
-
973
- This will provide route helpers such as `admin_photos_path`, `new_admin_photo_path`, etc.
974
-
975
- To prefix a group of route helpers, use `:as` with `scope`:
976
-
977
- ```ruby
978
- scope 'admin', as: 'admin' do
979
- resources :photos, :accounts
980
- end
981
-
982
- resources :photos, :accounts
983
- ```
984
-
985
- This will generate routes such as `admin_photos_path` and `admin_accounts_path` which map to `/admin/photos` and `/admin/accounts` respectively.
986
-
987
- NOTE: The `namespace` scope will automatically add `:as` as well as `:module` and `:path` prefixes.
988
-
989
- You can prefix routes with a named parameter also:
990
-
991
- ```ruby
992
- scope ':username' do
993
- resources :articles
994
- end
995
- ```
996
-
997
- This will provide you with URLs such as `/bob/articles/1` and will allow you to reference the `username` part of the path as `params[:username]` in controllers, helpers and views.
998
-
999
- ### Restricting the Routes Created
1000
-
1001
- 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:
1002
-
1003
- ```ruby
1004
- resources :photos, only: [:index, :show]
1005
- ```
1006
-
1007
- 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.
1008
-
1009
- The `:except` option specifies a route or list of routes that Rails should _not_ create:
1010
-
1011
- ```ruby
1012
- resources :photos, except: :destroy
1013
- ```
1014
-
1015
- In this case, Rails will create all of the normal routes except the route for `destroy` (a `DELETE` request to `/photos/:id`).
1016
-
1017
- 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.
1018
-
1019
- ### Translated Paths
1020
-
1021
- Using `scope`, we can alter path names generated by resources:
1022
-
1023
- ```ruby
1024
- scope(path_names: { new: 'neu', edit: 'bearbeiten' }) do
1025
- resources :categories, path: 'kategorien'
1026
- end
1027
- ```
1028
-
1029
- Rails now creates routes to the `CategoriesController`.
1030
-
1031
- | HTTP Verb | Path | Controller#Action | Named Helper |
1032
- | --------- | -------------------------- | ------------------ | ----------------------- |
1033
- | GET | /kategorien | categories#index | categories_path |
1034
- | GET | /kategorien/neu | categories#new | new_category_path |
1035
- | POST | /kategorien | categories#create | categories_path |
1036
- | GET | /kategorien/:id | categories#show | category_path(:id) |
1037
- | GET | /kategorien/:id/bearbeiten | categories#edit | edit_category_path(:id) |
1038
- | PATCH/PUT | /kategorien/:id | categories#update | category_path(:id) |
1039
- | DELETE | /kategorien/:id | categories#destroy | category_path(:id) |
1040
-
1041
- ### Overriding the Singular Form
1042
-
1043
- If you want to define the singular form of a resource, you should add additional rules to the `Inflector`:
1044
-
1045
- ```ruby
1046
- ActiveSupport::Inflector.inflections do |inflect|
1047
- inflect.irregular 'tooth', 'teeth'
1048
- end
1049
- ```
1050
-
1051
- ### Using `:as` in Nested Resources
1052
-
1053
- The `:as` option overrides the automatically-generated name for the resource in nested route helpers. For example:
1054
-
1055
- ```ruby
1056
- resources :magazines do
1057
- resources :ads, as: 'periodical_ads'
1058
- end
1059
- ```
1060
-
1061
- This will create routing helpers such as `magazine_periodical_ads_url` and `edit_magazine_periodical_ad_path`.
1062
-
1063
- ### Overriding Named Route Parameters
1064
-
1065
- The `:param` option overrides the default resource identifier `:id` (name of
1066
- the [dynamic segment](routing.html#dynamic-segments) used to generate the
1067
- routes). You can access that segment from your controller using
1068
- `params[<:param>]`.
1069
-
1070
- ```ruby
1071
- resources :videos, param: :identifier
1072
- ```
1073
-
1074
- ```
1075
- videos GET /videos(.:format) videos#index
1076
- POST /videos(.:format) videos#create
1077
- new_videos GET /videos/new(.:format) videos#new
1078
- edit_videos GET /videos/:identifier/edit(.:format) videos#edit
1079
- ```
1080
-
1081
- ```ruby
1082
- Video.find_by(identifier: params[:identifier])
1083
- ```
1084
-
1085
- Inspecting and Testing Routes
1086
- -----------------------------
1087
-
1088
- Rails offers facilities for inspecting and testing your routes.
1089
-
1090
- ### Listing Existing Routes
1091
-
1092
- 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.
1093
-
1094
- Both methods will list all of your routes, in the same order that they appear in `routes.rb`. For each route, you'll see:
1095
-
1096
- * The route name (if any)
1097
- * The HTTP verb used (if the route doesn't respond to all verbs)
1098
- * The URL pattern to match
1099
- * The routing parameters for the route
1100
-
1101
- For example, here's a small section of the `rake routes` output for a RESTful route:
1102
-
1103
- ```
1104
- users GET /users(.:format) users#index
1105
- POST /users(.:format) users#create
1106
- new_user GET /users/new(.:format) users#new
1107
- edit_user GET /users/:id/edit(.:format) users#edit
1108
- ```
1109
-
1110
- You may restrict the listing to the routes that map to a particular controller setting the `CONTROLLER` environment variable:
1111
-
1112
- ```bash
1113
- $ CONTROLLER=users bin/rake routes
1114
- ```
1115
-
1116
- 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.
1117
-
1118
- ### Testing Routes
1119
-
1120
- 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:
1121
-
1122
- * `assert_generates`
1123
- * `assert_recognizes`
1124
- * `assert_routing`
1125
-
1126
- #### The `assert_generates` Assertion
1127
-
1128
- `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:
1129
-
1130
- ```ruby
1131
- assert_generates '/photos/1', { controller: 'photos', action: 'show', id: '1' }
1132
- assert_generates '/about', controller: 'pages', action: 'about'
1133
- ```
1134
-
1135
- #### The `assert_recognizes` Assertion
1136
-
1137
- `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:
1138
-
1139
- ```ruby
1140
- assert_recognizes({ controller: 'photos', action: 'show', id: '1' }, '/photos/1')
1141
- ```
1142
-
1143
- You can supply a `:method` argument to specify the HTTP verb:
1144
-
1145
- ```ruby
1146
- assert_recognizes({ controller: 'photos', action: 'create' }, { path: 'photos', method: :post })
1147
- ```
1148
-
1149
- #### The `assert_routing` Assertion
1150
-
1151
- 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`:
1152
-
1153
- ```ruby
1154
- assert_routing({ path: 'photos', method: :post }, { controller: 'photos', action: 'create' })
1155
- ```