rails 4.0.13 → 4.1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +20 -15
- data/guides/CHANGELOG.md +5 -74
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/feature_tile.gif +0 -0
- data/guides/assets/images/footer_tile.gif +0 -0
- data/guides/assets/images/fxn.png +0 -0
- data/guides/assets/images/getting_started/challenge.png +0 -0
- data/guides/assets/images/getting_started/forbidden_attributes_for_new_post.png +0 -0
- data/guides/assets/images/getting_started/new_post.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_controller.png +0 -0
- data/guides/assets/images/getting_started/routing_error_no_route_matches.png +0 -0
- data/guides/assets/images/getting_started/template_is_missing_posts_new.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_create_for_posts.png +0 -0
- data/guides/assets/images/getting_started/unknown_action_new_for_posts.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/icons/README +1 -1
- data/guides/assets/images/icons/callouts/11.png +0 -0
- data/guides/assets/images/icons/callouts/12.png +0 -0
- data/guides/assets/images/icons/callouts/13.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/caution.png +0 -0
- data/guides/assets/images/icons/example.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails4_features.png +0 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +30 -34
- data/guides/assets/stylesheets/main.css +2 -1
- data/guides/assets/stylesheets/print.css +1 -1
- data/guides/bug_report_templates/action_controller_gem.rb +2 -0
- data/guides/bug_report_templates/action_controller_master.rb +2 -0
- data/guides/bug_report_templates/active_record_gem.rb +1 -1
- data/guides/bug_report_templates/active_record_master.rb +2 -1
- data/guides/code/getting_started/Gemfile +1 -1
- data/guides/code/getting_started/app/assets/javascripts/application.js +1 -2
- data/guides/code/getting_started/config/environments/development.rb +1 -1
- data/guides/code/getting_started/public/404.html +2 -0
- data/guides/code/getting_started/public/422.html +2 -0
- data/guides/code/getting_started/public/500.html +2 -0
- data/guides/rails_guides/helpers.rb +1 -1
- data/guides/source/2_2_release_notes.md +2 -2
- data/guides/source/2_3_release_notes.md +8 -8
- data/guides/source/3_0_release_notes.md +1 -2
- data/guides/source/3_1_release_notes.md +1 -1
- data/guides/source/3_2_release_notes.md +12 -12
- data/guides/source/4_0_release_notes.md +79 -46
- data/guides/source/4_1_release_notes.md +601 -0
- data/guides/source/_welcome.html.erb +1 -1
- data/guides/source/action_controller_overview.md +117 -31
- data/guides/source/action_mailer_basics.md +19 -19
- data/guides/source/action_view_overview.md +131 -12
- data/guides/source/active_model_basics.md +6 -6
- data/guides/source/active_record_basics.md +15 -15
- data/guides/source/active_record_callbacks.md +18 -16
- data/guides/source/active_record_querying.md +67 -39
- data/guides/source/active_record_validations.md +31 -31
- data/guides/source/active_support_core_extensions.md +63 -74
- data/guides/source/active_support_instrumentation.md +13 -4
- data/guides/source/api_documentation_guidelines.md +19 -5
- data/guides/source/asset_pipeline.md +544 -249
- data/guides/source/association_basics.md +81 -22
- data/guides/source/caching_with_rails.md +15 -6
- data/guides/source/command_line.md +28 -19
- data/guides/source/configuring.md +98 -50
- data/guides/source/contributing_to_ruby_on_rails.md +11 -11
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +36 -5
- data/guides/source/development_dependencies_install.md +89 -8
- data/guides/source/documents.yaml +7 -1
- data/guides/source/engines.md +648 -224
- data/guides/source/form_helpers.md +56 -45
- data/guides/source/generators.md +7 -3
- data/guides/source/getting_started.md +379 -164
- data/guides/source/i18n.md +59 -23
- data/guides/source/index.html.erb +1 -1
- data/guides/source/initialization.md +153 -56
- data/guides/source/kindle/toc.html.erb +1 -1
- data/guides/source/layout.html.erb +3 -3
- data/guides/source/layouts_and_rendering.md +12 -11
- data/guides/source/maintenance_policy.md +4 -23
- data/guides/source/migrations.md +41 -37
- data/guides/source/nested_model_forms.md +3 -3
- data/guides/source/plugins.md +27 -23
- data/guides/source/rails_application_templates.md +25 -6
- data/guides/source/rails_on_rack.md +35 -51
- data/guides/source/routing.md +108 -99
- data/guides/source/ruby_on_rails_guides_guidelines.md +2 -2
- data/guides/source/security.md +33 -31
- data/guides/source/testing.md +37 -34
- data/guides/source/upgrading_ruby_on_rails.md +335 -16
- data/guides/source/working_with_javascript_in_rails.md +18 -10
- metadata +66 -39
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- data/guides/source/kindle/KINDLE.md +0 -26
@@ -47,7 +47,7 @@ The following sections outline the primary methods provided by the API:
|
|
47
47
|
|
48
48
|
### gem(*args)
|
49
49
|
|
50
|
-
Adds a `gem` entry for the supplied gem to the generated application
|
50
|
+
Adds a `gem` entry for the supplied gem to the generated application's `Gemfile`.
|
51
51
|
|
52
52
|
For example, if your application depends on the gems `bj` and `nokogiri`:
|
53
53
|
|
@@ -91,14 +91,14 @@ Adds a line inside the `Application` class for `config/application.rb`.
|
|
91
91
|
If `options[:env]` is specified, the line is appended to the corresponding file in `config/environments`.
|
92
92
|
|
93
93
|
```ruby
|
94
|
-
environment 'config.action_mailer.default_url_options = {host:
|
94
|
+
environment 'config.action_mailer.default_url_options = {host: "http://yourwebsite.example.com"}', env: 'production'
|
95
95
|
```
|
96
96
|
|
97
97
|
A block can be used in place of the `data` argument.
|
98
98
|
|
99
99
|
### vendor/lib/file/initializer(filename, data = nil, &block)
|
100
100
|
|
101
|
-
Adds an initializer to the generated application
|
101
|
+
Adds an initializer to the generated application's `config/initializers` directory.
|
102
102
|
|
103
103
|
Let's say you like using `Object#not_nil?` and `Object#not_blank?`:
|
104
104
|
|
@@ -127,7 +127,7 @@ file 'app/components/foo.rb', <<-CODE
|
|
127
127
|
CODE
|
128
128
|
```
|
129
129
|
|
130
|
-
That
|
130
|
+
That'll create the `app/components` directory and put `foo.rb` in there.
|
131
131
|
|
132
132
|
### rakefile(filename, data = nil, &block)
|
133
133
|
|
@@ -197,7 +197,7 @@ end
|
|
197
197
|
|
198
198
|
### ask(question)
|
199
199
|
|
200
|
-
`ask()` gives you a chance to get some feedback from the user and use it in your templates. Let's say you want your user to name the new shiny library you
|
200
|
+
`ask()` gives you a chance to get some feedback from the user and use it in your templates. Let's say you want your user to name the new shiny library you're adding:
|
201
201
|
|
202
202
|
```ruby
|
203
203
|
lib_name = ask("What do you want to call the shiny library ?")
|
@@ -211,7 +211,7 @@ CODE
|
|
211
211
|
|
212
212
|
### yes?(question) or no?(question)
|
213
213
|
|
214
|
-
These methods let you ask questions from templates and decide the flow based on the user
|
214
|
+
These methods let you ask questions from templates and decide the flow based on the user's answer. Let's say you want to freeze rails only if the user wants to:
|
215
215
|
|
216
216
|
```ruby
|
217
217
|
rake("rails:freeze:gems") if yes?("Freeze rails gems?")
|
@@ -227,3 +227,22 @@ git :init
|
|
227
227
|
git add: "."
|
228
228
|
git commit: "-a -m 'Initial commit'"
|
229
229
|
```
|
230
|
+
|
231
|
+
Advanced Usage
|
232
|
+
--------------
|
233
|
+
|
234
|
+
The application template is evaluated in the context of a
|
235
|
+
`Rails::Generators::AppGenerator` instance. It uses the `apply` action
|
236
|
+
provided by
|
237
|
+
[Thor](https://github.com/erikhuda/thor/blob/master/lib/thor/actions.rb#L207).
|
238
|
+
This means you can extend and change the instance to match your needs.
|
239
|
+
|
240
|
+
For example by overwriting the `source_paths` method to contain the
|
241
|
+
location of your template. Now methods like `copy_file` will accept
|
242
|
+
relative paths to your template's location.
|
243
|
+
|
244
|
+
```ruby
|
245
|
+
def source_paths
|
246
|
+
[File.expand_path(File.dirname(__FILE__))]
|
247
|
+
end
|
248
|
+
```
|
@@ -5,7 +5,6 @@ This guide covers Rails integration with Rack and interfacing with other Rack co
|
|
5
5
|
|
6
6
|
After reading this guide, you will know:
|
7
7
|
|
8
|
-
* How to create Rails Metal applications.
|
9
8
|
* How to use Rack Middlewares in your Rails applications.
|
10
9
|
* Action Pack's internal Middleware stack.
|
11
10
|
* How to define a custom Middleware stack.
|
@@ -82,9 +81,9 @@ To use `rackup` instead of Rails' `rails server`, you can put the following insi
|
|
82
81
|
|
83
82
|
```ruby
|
84
83
|
# Rails.root/config.ru
|
85
|
-
require ::File.expand_path('../config/environment',
|
84
|
+
require ::File.expand_path('../config/environment', __FILE__)
|
86
85
|
|
87
|
-
use Rack::Debugger
|
86
|
+
use Rails::Rack::Debugger
|
88
87
|
use Rack::ContentLength
|
89
88
|
run Rails.application
|
90
89
|
```
|
@@ -119,6 +118,7 @@ $ rake middleware
|
|
119
118
|
For a freshly generated Rails application, this might produce something like:
|
120
119
|
|
121
120
|
```ruby
|
121
|
+
use Rack::Sendfile
|
122
122
|
use ActionDispatch::Static
|
123
123
|
use Rack::Lock
|
124
124
|
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x000000029a0838>
|
@@ -131,6 +131,7 @@ use ActionDispatch::DebugExceptions
|
|
131
131
|
use ActionDispatch::RemoteIp
|
132
132
|
use ActionDispatch::Reloader
|
133
133
|
use ActionDispatch::Callbacks
|
134
|
+
use ActiveRecord::Migration::CheckPending
|
134
135
|
use ActiveRecord::ConnectionAdapters::ConnectionManagement
|
135
136
|
use ActiveRecord::QueryCache
|
136
137
|
use ActionDispatch::Cookies
|
@@ -143,7 +144,7 @@ use Rack::ETag
|
|
143
144
|
run MyApp::Application.routes
|
144
145
|
```
|
145
146
|
|
146
|
-
|
147
|
+
The default middlewares shown here (and some others) are each summarized in the [Internal Middlewares](#internal-middleware-stack) section, below.
|
147
148
|
|
148
149
|
### Configuring Middleware Stack
|
149
150
|
|
@@ -181,18 +182,17 @@ You can swap an existing middleware in the middleware stack using `config.middle
|
|
181
182
|
config.middleware.swap ActionDispatch::ShowExceptions, Lifo::ShowExceptions
|
182
183
|
```
|
183
184
|
|
184
|
-
####
|
185
|
+
#### Deleting a Middleware
|
185
186
|
|
186
|
-
|
187
|
-
|
188
|
-
Append following lines to your application configuration:
|
187
|
+
Add the following lines to your application configuration:
|
189
188
|
|
190
189
|
```ruby
|
191
190
|
# config/application.rb
|
192
191
|
config.middleware.delete "Rack::Lock"
|
193
192
|
```
|
194
193
|
|
195
|
-
And now if you inspect the middleware stack, you'll find that `Rack::Lock`
|
194
|
+
And now if you inspect the middleware stack, you'll find that `Rack::Lock` is
|
195
|
+
not a part of it.
|
196
196
|
|
197
197
|
```bash
|
198
198
|
$ rake middleware
|
@@ -224,116 +224,100 @@ config.middleware.delete "Rack::MethodOverride"
|
|
224
224
|
|
225
225
|
Much of Action Controller's functionality is implemented as Middlewares. The following list explains the purpose of each of them:
|
226
226
|
|
227
|
-
|
227
|
+
**`Rack::Sendfile`**
|
228
228
|
|
229
229
|
* Sets server specific X-Sendfile header. Configure this via `config.action_dispatch.x_sendfile_header` option.
|
230
230
|
|
231
|
-
|
231
|
+
**`ActionDispatch::Static`**
|
232
232
|
|
233
233
|
* Used to serve static assets. Disabled if `config.serve_static_assets` is `false`.
|
234
234
|
|
235
|
-
|
235
|
+
**`Rack::Lock`**
|
236
236
|
|
237
237
|
* Sets `env["rack.multithread"]` flag to `false` and wraps the application within a Mutex.
|
238
238
|
|
239
|
-
|
239
|
+
**`ActiveSupport::Cache::Strategy::LocalCache::Middleware`**
|
240
240
|
|
241
241
|
* Used for memory caching. This cache is not thread safe.
|
242
242
|
|
243
|
-
|
243
|
+
**`Rack::Runtime`**
|
244
244
|
|
245
245
|
* Sets an X-Runtime header, containing the time (in seconds) taken to execute the request.
|
246
246
|
|
247
|
-
|
247
|
+
**`Rack::MethodOverride`**
|
248
248
|
|
249
249
|
* Allows the method to be overridden if `params[:_method]` is set. This is the middleware which supports the PUT and DELETE HTTP method types.
|
250
250
|
|
251
|
-
|
251
|
+
**`ActionDispatch::RequestId`**
|
252
252
|
|
253
253
|
* Makes a unique `X-Request-Id` header available to the response and enables the `ActionDispatch::Request#uuid` method.
|
254
254
|
|
255
|
-
|
255
|
+
**`Rails::Rack::Logger`**
|
256
256
|
|
257
257
|
* Notifies the logs that the request has began. After request is complete, flushes all the logs.
|
258
258
|
|
259
|
-
|
259
|
+
**`ActionDispatch::ShowExceptions`**
|
260
260
|
|
261
261
|
* Rescues any exception returned by the application and calls an exceptions app that will wrap it in a format for the end user.
|
262
262
|
|
263
|
-
|
263
|
+
**`ActionDispatch::DebugExceptions`**
|
264
264
|
|
265
265
|
* Responsible for logging exceptions and showing a debugging page in case the request is local.
|
266
266
|
|
267
|
-
|
267
|
+
**`ActionDispatch::RemoteIp`**
|
268
268
|
|
269
269
|
* Checks for IP spoofing attacks.
|
270
270
|
|
271
|
-
|
271
|
+
**`ActionDispatch::Reloader`**
|
272
272
|
|
273
273
|
* Provides prepare and cleanup callbacks, intended to assist with code reloading during development.
|
274
274
|
|
275
|
-
|
275
|
+
**`ActionDispatch::Callbacks`**
|
276
276
|
|
277
277
|
* Runs the prepare callbacks before serving the request.
|
278
278
|
|
279
|
-
|
279
|
+
**`ActiveRecord::Migration::CheckPending`**
|
280
|
+
|
281
|
+
* Checks pending migrations and raises `ActiveRecord::PendingMigrationError` if any migrations are pending.
|
282
|
+
|
283
|
+
**`ActiveRecord::ConnectionAdapters::ConnectionManagement`**
|
280
284
|
|
281
285
|
* Cleans active connections after each request, unless the `rack.test` key in the request environment is set to `true`.
|
282
286
|
|
283
|
-
|
287
|
+
**`ActiveRecord::QueryCache`**
|
284
288
|
|
285
289
|
* Enables the Active Record query cache.
|
286
290
|
|
287
|
-
|
291
|
+
**`ActionDispatch::Cookies`**
|
288
292
|
|
289
293
|
* Sets cookies for the request.
|
290
294
|
|
291
|
-
|
295
|
+
**`ActionDispatch::Session::CookieStore`**
|
292
296
|
|
293
297
|
* Responsible for storing the session in cookies.
|
294
298
|
|
295
|
-
|
299
|
+
**`ActionDispatch::Flash`**
|
296
300
|
|
297
301
|
* Sets up the flash keys. Only available if `config.action_controller.session_store` is set to a value.
|
298
302
|
|
299
|
-
|
303
|
+
**`ActionDispatch::ParamsParser`**
|
300
304
|
|
301
305
|
* Parses out parameters from the request into `params`.
|
302
306
|
|
303
|
-
|
307
|
+
**`ActionDispatch::Head`**
|
304
308
|
|
305
309
|
* Converts HEAD requests to `GET` requests and serves them as so.
|
306
310
|
|
307
|
-
|
311
|
+
**`Rack::ConditionalGet`**
|
308
312
|
|
309
313
|
* Adds support for "Conditional `GET`" so that server responds with nothing if page wasn't changed.
|
310
314
|
|
311
|
-
|
315
|
+
**`Rack::ETag`**
|
312
316
|
|
313
317
|
* Adds ETag header on all String bodies. ETags are used to validate cache.
|
314
318
|
|
315
319
|
TIP: It's possible to use any of the above middlewares in your custom Rack stack.
|
316
320
|
|
317
|
-
### Using Rack Builder
|
318
|
-
|
319
|
-
The following shows how to replace use `Rack::Builder` instead of the Rails supplied `MiddlewareStack`.
|
320
|
-
|
321
|
-
<strong>Clear the existing Rails middleware stack</strong>
|
322
|
-
|
323
|
-
```ruby
|
324
|
-
# config/application.rb
|
325
|
-
config.middleware.clear
|
326
|
-
```
|
327
|
-
|
328
|
-
<br />
|
329
|
-
<strong>Add a `config.ru` file to `Rails.root`</strong>
|
330
|
-
|
331
|
-
```ruby
|
332
|
-
# config.ru
|
333
|
-
use MyOwnStackFromScratch
|
334
|
-
run Rails.application
|
335
|
-
```
|
336
|
-
|
337
321
|
Resources
|
338
322
|
---------
|
339
323
|
|
data/guides/source/routing.md
CHANGED
@@ -36,7 +36,7 @@ the request is dispatched to the `patients` controller's `show` action with `{ i
|
|
36
36
|
|
37
37
|
### Generating Paths and URLs from Code
|
38
38
|
|
39
|
-
You can also generate paths and URLs.
|
39
|
+
You can also generate paths and URLs. If the route above is modified to be:
|
40
40
|
|
41
41
|
```ruby
|
42
42
|
get '/patients/:id', to: 'patients#show', as: 'patient'
|
@@ -89,15 +89,15 @@ resources :photos
|
|
89
89
|
|
90
90
|
creates seven different routes in your application, all mapping to the `Photos` controller:
|
91
91
|
|
92
|
-
| HTTP Verb | Path | Action
|
93
|
-
| --------- | ---------------- |
|
94
|
-
| GET | /photos | index
|
95
|
-
| GET | /photos/new | new
|
96
|
-
| POST | /photos | create
|
97
|
-
| GET | /photos/:id | show
|
98
|
-
| GET | /photos/:id/edit | edit
|
99
|
-
| PATCH/PUT | /photos/:id | update
|
100
|
-
| DELETE | /photos/:id | destroy
|
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
101
|
|
102
102
|
NOTE: Because the router uses the HTTP verb and URL to match inbound requests, four URLs map to seven different actions.
|
103
103
|
|
@@ -152,14 +152,14 @@ resource :geocoder
|
|
152
152
|
|
153
153
|
creates six different routes in your application, all mapping to the `Geocoders` controller:
|
154
154
|
|
155
|
-
| HTTP Verb | Path | Action
|
156
|
-
| --------- | -------------- |
|
157
|
-
| GET | /geocoder/new | new | return an HTML form for creating the geocoder |
|
158
|
-
| POST | /geocoder | create | create the new geocoder |
|
159
|
-
| GET | /geocoder | show | display the one and only geocoder resource |
|
160
|
-
| GET | /geocoder/edit | edit | return an HTML form for editing the geocoder |
|
161
|
-
| PATCH/PUT | /geocoder | update | update the one and only geocoder resource |
|
162
|
-
| DELETE | /geocoder | destroy | delete the geocoder resource |
|
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
163
|
|
164
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
165
|
|
@@ -171,6 +171,12 @@ A singular resourceful route generates these helpers:
|
|
171
171
|
|
172
172
|
As with plural resources, the same helpers ending in `_url` will also include the host, port and path prefix.
|
173
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
|
+
|
174
180
|
### Controller Namespaces and Routing
|
175
181
|
|
176
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,15 +189,15 @@ end
|
|
183
189
|
|
184
190
|
This will create a number of routes for each of the `posts` and `comments` controller. For `Admin::PostsController`, Rails will create:
|
185
191
|
|
186
|
-
| HTTP Verb | Path | Action
|
187
|
-
| --------- | --------------------- |
|
188
|
-
| GET | /admin/posts | index | admin_posts_path |
|
189
|
-
| GET | /admin/posts/new | new | new_admin_post_path |
|
190
|
-
| POST | /admin/posts | create | admin_posts_path |
|
191
|
-
| GET | /admin/posts/:id | show | admin_post_path(:id) |
|
192
|
-
| GET | /admin/posts/:id/edit | edit | edit_admin_post_path(:id) |
|
193
|
-
| PATCH/PUT | /admin/posts/:id | update | admin_post_path(:id) |
|
194
|
-
| DELETE | /admin/posts/:id | destroy | admin_post_path(:id) |
|
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) |
|
195
201
|
|
196
202
|
If you want to route `/posts` (without the prefix `/admin`) to `Admin::PostsController`, you could use:
|
197
203
|
|
@@ -223,15 +229,15 @@ resources :posts, path: '/admin/posts'
|
|
223
229
|
|
224
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`:
|
225
231
|
|
226
|
-
| HTTP Verb | Path | Action
|
227
|
-
| --------- | --------------------- |
|
228
|
-
| GET | /admin/posts | index
|
229
|
-
| GET | /admin/posts/new | new
|
230
|
-
| POST | /admin/posts | create
|
231
|
-
| GET | /admin/posts/:id | show
|
232
|
-
| GET | /admin/posts/:id/edit | edit
|
233
|
-
| PATCH/PUT | /admin/posts/:id | update
|
234
|
-
| DELETE | /admin/posts/:id | destroy
|
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) |
|
235
241
|
|
236
242
|
### Nested Resources
|
237
243
|
|
@@ -257,15 +263,15 @@ end
|
|
257
263
|
|
258
264
|
In addition to the routes for magazines, this declaration will also route ads to an `AdsController`. The ad URLs require a magazine:
|
259
265
|
|
260
|
-
| HTTP Verb | Path | Action
|
261
|
-
| --------- | ------------------------------------ |
|
262
|
-
| GET | /magazines/:magazine_id/ads | index
|
263
|
-
| GET | /magazines/:magazine_id/ads/new | new
|
264
|
-
| POST | /magazines/:magazine_id/ads | create
|
265
|
-
| GET | /magazines/:magazine_id/ads/:id | show
|
266
|
-
| GET | /magazines/:magazine_id/ads/:id/edit | edit
|
267
|
-
| PATCH/PUT | /magazines/:magazine_id/ads/:id | update
|
268
|
-
| DELETE | /magazines/:magazine_id/ads/:id | destroy
|
266
|
+
| HTTP Verb | Path | Controller#Action | Used for |
|
267
|
+
| --------- | ------------------------------------ | ----------------- | -------------------------------------------------------------------------- |
|
268
|
+
| GET | /magazines/:magazine_id/ads | ads#index | display a list of all ads for a specific magazine |
|
269
|
+
| GET | /magazines/:magazine_id/ads/new | ads#new | return an HTML form for creating a new ad belonging to a specific magazine |
|
270
|
+
| POST | /magazines/:magazine_id/ads | ads#create | create a new ad belonging to a specific magazine |
|
271
|
+
| GET | /magazines/:magazine_id/ads/:id | ads#show | display a specific ad belonging to a specific magazine |
|
272
|
+
| GET | /magazines/:magazine_id/ads/:id/edit | ads#edit | return an HTML form for editing an ad belonging to a specific magazine |
|
273
|
+
| PATCH/PUT | /magazines/:magazine_id/ads/:id | ads#update | update a specific ad belonging to a specific magazine |
|
274
|
+
| DELETE | /magazines/:magazine_id/ads/:id | ads#destroy | delete a specific ad belonging to a specific magazine |
|
269
275
|
|
270
276
|
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)`).
|
271
277
|
|
@@ -332,7 +338,7 @@ shallow do
|
|
332
338
|
end
|
333
339
|
```
|
334
340
|
|
335
|
-
There
|
341
|
+
There exist two options for `scope` to customize shallow routes. `:shallow_path` prefixes member paths with the specified parameter:
|
336
342
|
|
337
343
|
```ruby
|
338
344
|
scope shallow_path: "sekret" do
|
@@ -344,15 +350,15 @@ end
|
|
344
350
|
|
345
351
|
The comments resource here will have the following routes generated for it:
|
346
352
|
|
347
|
-
| HTTP Verb | Path | Named Helper |
|
348
|
-
| --------- | -------------------------------------- | ------------------- |
|
349
|
-
| GET | /posts/:post_id/comments(.:format) | post_comments |
|
350
|
-
| POST | /posts/:post_id/comments(.:format) | post_comments |
|
351
|
-
| GET | /posts/:post_id/comments/new(.:format) | new_post_comment |
|
352
|
-
| GET | /sekret/comments/:id/edit(.:format) | edit_comment |
|
353
|
-
| GET | /sekret/comments/:id(.:format) | comment |
|
354
|
-
| PATCH/PUT | /sekret/comments/:id(.:format) | comment |
|
355
|
-
| DELETE | /sekret/comments/:id(.:format) | comment |
|
353
|
+
| HTTP Verb | Path | Controller#Action | Named Helper |
|
354
|
+
| --------- | -------------------------------------- | ----------------- | ------------------- |
|
355
|
+
| GET | /posts/:post_id/comments(.:format) | comments#index | post_comments |
|
356
|
+
| POST | /posts/:post_id/comments(.:format) | comments#create | post_comments |
|
357
|
+
| GET | /posts/:post_id/comments/new(.:format) | comments#new | new_post_comment |
|
358
|
+
| GET | /sekret/comments/:id/edit(.:format) | comments#edit | edit_comment |
|
359
|
+
| GET | /sekret/comments/:id(.:format) | comments#show | comment |
|
360
|
+
| PATCH/PUT | /sekret/comments/:id(.:format) | comments#update | comment |
|
361
|
+
| DELETE | /sekret/comments/:id(.:format) | comments#destroy | comment |
|
356
362
|
|
357
363
|
The `:shallow_prefix` option adds the specified parameter to the named helpers:
|
358
364
|
|
@@ -366,19 +372,19 @@ end
|
|
366
372
|
|
367
373
|
The comments resource here will have the following routes generated for it:
|
368
374
|
|
369
|
-
| HTTP Verb | Path | Named Helper |
|
370
|
-
| --------- | -------------------------------------- | ------------------- |
|
371
|
-
| GET | /posts/:post_id/comments(.:format) | post_comments |
|
372
|
-
| POST | /posts/:post_id/comments(.:format) | post_comments |
|
373
|
-
| GET | /posts/:post_id/comments/new(.:format) | new_post_comment |
|
374
|
-
| GET | /comments/:id/edit(.:format) | edit_sekret_comment |
|
375
|
-
| GET | /comments/:id(.:format) | sekret_comment |
|
376
|
-
| PATCH/PUT | /comments/:id(.:format) | sekret_comment |
|
377
|
-
| DELETE | /comments/:id(.:format) | sekret_comment |
|
375
|
+
| HTTP Verb | Path | Controller#Action | Named Helper |
|
376
|
+
| --------- | -------------------------------------- | ----------------- | ------------------- |
|
377
|
+
| GET | /posts/:post_id/comments(.:format) | comments#index | post_comments |
|
378
|
+
| POST | /posts/:post_id/comments(.:format) | comments#create | post_comments |
|
379
|
+
| GET | /posts/:post_id/comments/new(.:format) | comments#new | new_post_comment |
|
380
|
+
| GET | /comments/:id/edit(.:format) | comments#edit | edit_sekret_comment |
|
381
|
+
| GET | /comments/:id(.:format) | comments#show | sekret_comment |
|
382
|
+
| PATCH/PUT | /comments/:id(.:format) | comments#update | sekret_comment |
|
383
|
+
| DELETE | /comments/:id(.:format) | comments#destroy | sekret_comment |
|
378
384
|
|
379
385
|
### Routing concerns
|
380
386
|
|
381
|
-
Routing Concerns allows you to declare common routes that can be reused inside
|
387
|
+
Routing Concerns allows you to declare common routes that can be reused inside other resources and routes. To define a concern:
|
382
388
|
|
383
389
|
```ruby
|
384
390
|
concern :commentable do
|
@@ -479,7 +485,10 @@ end
|
|
479
485
|
|
480
486
|
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.
|
481
487
|
|
482
|
-
Within the block of member routes, each route name specifies the HTTP verb
|
488
|
+
Within the block of member routes, each route name specifies the HTTP verb
|
489
|
+
will be recognized. You can use `get`, `patch`, `put`, `post`, or `delete` here
|
490
|
+
. If you don't have multiple `member` routes, you can also pass `:on` to a
|
491
|
+
route, eliminating the block:
|
483
492
|
|
484
493
|
```ruby
|
485
494
|
resources :photos do
|
@@ -767,11 +776,11 @@ You can also reuse dynamic segments from the match in the path to redirect to:
|
|
767
776
|
get '/stories/:name', to: redirect('/posts/%{name}')
|
768
777
|
```
|
769
778
|
|
770
|
-
You can also provide a block to redirect, which receives the
|
779
|
+
You can also provide a block to redirect, which receives the symbolized path parameters and the request object:
|
771
780
|
|
772
781
|
```ruby
|
773
|
-
get '/stories/:name', to: redirect {|
|
774
|
-
get '/stories', to: redirect {|
|
782
|
+
get '/stories/:name', to: redirect {|path_params, req| "/posts/#{path_params[:name].pluralize}" }
|
783
|
+
get '/stories', to: redirect {|path_params, req| "/posts/#{req.subdomain}" }
|
775
784
|
```
|
776
785
|
|
777
786
|
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.
|
@@ -803,7 +812,7 @@ You should put the `root` route at the top of the file, because it is the most p
|
|
803
812
|
|
804
813
|
NOTE: The `root` route only routes `GET` requests to the action.
|
805
814
|
|
806
|
-
You can also use root inside namespaces and scopes as well.
|
815
|
+
You can also use root inside namespaces and scopes as well. For example:
|
807
816
|
|
808
817
|
```ruby
|
809
818
|
namespace :admin do
|
@@ -836,15 +845,15 @@ resources :photos, controller: 'images'
|
|
836
845
|
|
837
846
|
will recognize incoming paths beginning with `/photos` but route to the `Images` controller:
|
838
847
|
|
839
|
-
| HTTP Verb | Path | Action
|
840
|
-
| --------- | ---------------- |
|
841
|
-
| GET | /photos | index
|
842
|
-
| GET | /photos/new | new
|
843
|
-
| POST | /photos | create
|
844
|
-
| GET | /photos/:id | show
|
845
|
-
| GET | /photos/:id/edit | edit
|
846
|
-
| PATCH/PUT | /photos/:id | update
|
847
|
-
| DELETE | /photos/:id | destroy
|
848
|
+
| HTTP Verb | Path | Controller#Action | Named Helper |
|
849
|
+
| --------- | ---------------- | ----------------- | -------------------- |
|
850
|
+
| GET | /photos | images#index | photos_path |
|
851
|
+
| GET | /photos/new | images#new | new_photo_path |
|
852
|
+
| POST | /photos | images#create | photos_path |
|
853
|
+
| GET | /photos/:id | images#show | photo_path(:id) |
|
854
|
+
| GET | /photos/:id/edit | images#edit | edit_photo_path(:id) |
|
855
|
+
| PATCH/PUT | /photos/:id | images#update | photo_path(:id) |
|
856
|
+
| DELETE | /photos/:id | images#destroy | photo_path(:id) |
|
848
857
|
|
849
858
|
NOTE: Use `photos_path`, `new_photo_path`, etc. to generate paths for this resource.
|
850
859
|
|
@@ -857,8 +866,8 @@ resources :user_permissions, controller: 'admin/user_permissions'
|
|
857
866
|
This will route to the `Admin::UserPermissions` controller.
|
858
867
|
|
859
868
|
NOTE: Only the directory notation is supported. Specifying the
|
860
|
-
controller with
|
861
|
-
|
869
|
+
controller with Ruby constant notation (eg. `controller: 'Admin::UserPermissions'`)
|
870
|
+
can lead to routing problems and results in
|
862
871
|
a warning.
|
863
872
|
|
864
873
|
### Specifying Constraints
|
@@ -894,15 +903,15 @@ resources :photos, as: 'images'
|
|
894
903
|
|
895
904
|
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.
|
896
905
|
|
897
|
-
| HTTP Verb | Path | Action
|
898
|
-
| --------- | ---------------- |
|
899
|
-
| GET | /photos | index
|
900
|
-
| GET | /photos/new | new
|
901
|
-
| POST | /photos | create
|
902
|
-
| GET | /photos/:id | show
|
903
|
-
| GET | /photos/:id/edit | edit
|
904
|
-
| PATCH/PUT | /photos/:id | update
|
905
|
-
| DELETE | /photos/:id | destroy
|
906
|
+
| HTTP Verb | Path | Controller#Action | Named Helper |
|
907
|
+
| --------- | ---------------- | ----------------- | -------------------- |
|
908
|
+
| GET | /photos | photos#index | images_path |
|
909
|
+
| GET | /photos/new | photos#new | new_image_path |
|
910
|
+
| POST | /photos | photos#create | images_path |
|
911
|
+
| GET | /photos/:id | photos#show | image_path(:id) |
|
912
|
+
| GET | /photos/:id/edit | photos#edit | edit_image_path(:id) |
|
913
|
+
| PATCH/PUT | /photos/:id | photos#update | image_path(:id) |
|
914
|
+
| DELETE | /photos/:id | photos#destroy | image_path(:id) |
|
906
915
|
|
907
916
|
### Overriding the `new` and `edit` Segments
|
908
917
|
|
@@ -999,15 +1008,15 @@ end
|
|
999
1008
|
|
1000
1009
|
Rails now creates routes to the `CategoriesController`.
|
1001
1010
|
|
1002
|
-
| HTTP Verb | Path | Action |
|
1003
|
-
| --------- | -------------------------- |
|
1004
|
-
| GET | /kategorien | index | categories_path |
|
1005
|
-
| GET | /kategorien/neu | new | new_category_path |
|
1006
|
-
| POST | /kategorien | create | categories_path |
|
1007
|
-
| GET | /kategorien/:id | show | category_path(:id) |
|
1008
|
-
| GET | /kategorien/:id/bearbeiten | edit | edit_category_path(:id) |
|
1009
|
-
| PATCH/PUT | /kategorien/:id | update | category_path(:id) |
|
1010
|
-
| DELETE | /kategorien/:id | destroy | category_path(:id) |
|
1011
|
+
| HTTP Verb | Path | Controller#Action | Named Helper |
|
1012
|
+
| --------- | -------------------------- | ------------------ | ----------------------- |
|
1013
|
+
| GET | /kategorien | categories#index | categories_path |
|
1014
|
+
| GET | /kategorien/neu | categories#new | new_category_path |
|
1015
|
+
| POST | /kategorien | categories#create | categories_path |
|
1016
|
+
| GET | /kategorien/:id | categories#show | category_path(:id) |
|
1017
|
+
| GET | /kategorien/:id/bearbeiten | categories#edit | edit_category_path(:id) |
|
1018
|
+
| PATCH/PUT | /kategorien/:id | categories#update | category_path(:id) |
|
1019
|
+
| DELETE | /kategorien/:id | categories#destroy | category_path(:id) |
|
1011
1020
|
|
1012
1021
|
### Overriding the Singular Form
|
1013
1022
|
|