rails 4.1.4 → 4.2.11.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +12 -10
- data/guides/CHANGELOG.md +87 -15
- data/guides/Rakefile +21 -6
- data/guides/assets/images/getting_started/article_with_comments.png +0 -0
- data/guides/assets/javascripts/guides.js +6 -0
- data/guides/assets/stylesheets/main.css +4 -1
- data/guides/bug_report_templates/action_controller_gem.rb +3 -3
- data/guides/bug_report_templates/action_controller_master.rb +3 -2
- data/guides/bug_report_templates/active_record_gem.rb +1 -1
- data/guides/bug_report_templates/generic_gem.rb +15 -0
- data/guides/bug_report_templates/generic_master.rb +26 -0
- data/guides/rails_guides/helpers.rb +1 -1
- data/guides/rails_guides/levenshtein.rb +27 -21
- data/guides/rails_guides/markdown/renderer.rb +1 -1
- data/guides/rails_guides/markdown.rb +11 -7
- data/guides/rails_guides.rb +2 -2
- data/guides/source/2_2_release_notes.md +1 -1
- data/guides/source/2_3_release_notes.md +4 -4
- data/guides/source/3_0_release_notes.md +8 -8
- data/guides/source/3_1_release_notes.md +6 -3
- data/guides/source/3_2_release_notes.md +6 -3
- data/guides/source/4_0_release_notes.md +6 -3
- data/guides/source/4_1_release_notes.md +10 -11
- data/guides/source/4_2_release_notes.md +877 -0
- data/guides/source/_license.html.erb +1 -1
- data/guides/source/_welcome.html.erb +6 -8
- data/guides/source/action_controller_overview.md +28 -11
- data/guides/source/action_mailer_basics.md +97 -29
- data/guides/source/action_view_overview.md +142 -191
- data/guides/source/active_job_basics.md +339 -0
- data/guides/source/active_model_basics.md +371 -17
- data/guides/source/active_record_basics.md +25 -24
- data/guides/source/active_record_callbacks.md +12 -9
- data/guides/source/{migrations.md → active_record_migrations.md} +135 -226
- data/guides/source/active_record_postgresql.md +433 -0
- data/guides/source/active_record_querying.md +270 -262
- data/guides/source/active_record_validations.md +24 -14
- data/guides/source/active_support_core_extensions.md +115 -123
- data/guides/source/active_support_instrumentation.md +10 -18
- data/guides/source/api_documentation_guidelines.md +63 -17
- data/guides/source/asset_pipeline.md +266 -125
- data/guides/source/association_basics.md +96 -80
- data/guides/source/autoloading_and_reloading_constants.md +1311 -0
- data/guides/source/caching_with_rails.md +32 -7
- data/guides/source/command_line.md +52 -30
- data/guides/source/configuring.md +161 -33
- data/guides/source/contributing_to_ruby_on_rails.md +198 -114
- data/guides/source/credits.html.erb +2 -2
- data/guides/source/debugging_rails_applications.md +448 -294
- data/guides/source/development_dependencies_install.md +47 -36
- data/guides/source/documents.yaml +19 -7
- data/guides/source/engines.md +217 -196
- data/guides/source/form_helpers.md +79 -56
- data/guides/source/generators.md +24 -11
- data/guides/source/getting_started.md +361 -222
- data/guides/source/i18n.md +113 -69
- data/guides/source/index.html.erb +1 -0
- data/guides/source/initialization.md +110 -63
- data/guides/source/layout.html.erb +5 -11
- data/guides/source/layouts_and_rendering.md +26 -26
- data/guides/source/maintenance_policy.md +26 -4
- data/guides/source/nested_model_forms.md +7 -4
- data/guides/source/plugins.md +27 -27
- data/guides/source/rails_application_templates.md +21 -3
- data/guides/source/rails_on_rack.md +12 -10
- data/guides/source/routing.md +115 -75
- data/guides/source/ruby_on_rails_guides_guidelines.md +11 -12
- data/guides/source/security.md +41 -35
- data/guides/source/testing.md +199 -119
- data/guides/source/upgrading_ruby_on_rails.md +319 -32
- data/guides/source/working_with_javascript_in_rails.md +19 -17
- data/guides/w3c_validator.rb +2 -0
- metadata +42 -95
- data/guides/code/getting_started/Gemfile +0 -40
- data/guides/code/getting_started/Gemfile.lock +0 -125
- data/guides/code/getting_started/README.rdoc +0 -28
- data/guides/code/getting_started/Rakefile +0 -6
- data/guides/code/getting_started/app/assets/javascripts/application.js +0 -15
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/welcome.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/welcome.css.scss +0 -3
- data/guides/code/getting_started/app/controllers/application_controller.rb +0 -5
- data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -23
- data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -53
- data/guides/code/getting_started/app/controllers/welcome_controller.rb +0 -4
- data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/welcome_helper.rb +0 -2
- data/guides/code/getting_started/app/models/comment.rb +0 -3
- data/guides/code/getting_started/app/models/post.rb +0 -7
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
- data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
- data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
- data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -27
- data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/index.html.erb +0 -21
- data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/show.html.erb +0 -18
- data/guides/code/getting_started/app/views/welcome/index.html.erb +0 -4
- data/guides/code/getting_started/bin/bundle +0 -4
- data/guides/code/getting_started/bin/rails +0 -4
- data/guides/code/getting_started/bin/rake +0 -4
- data/guides/code/getting_started/config/application.rb +0 -18
- data/guides/code/getting_started/config/boot.rb +0 -4
- data/guides/code/getting_started/config/database.yml +0 -25
- data/guides/code/getting_started/config/environment.rb +0 -5
- data/guides/code/getting_started/config/environments/development.rb +0 -30
- data/guides/code/getting_started/config/environments/production.rb +0 -80
- data/guides/code/getting_started/config/environments/test.rb +0 -36
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
- data/guides/code/getting_started/config/initializers/filter_parameter_logging.rb +0 -4
- data/guides/code/getting_started/config/initializers/inflections.rb +0 -16
- data/guides/code/getting_started/config/initializers/locale.rb +0 -9
- data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
- data/guides/code/getting_started/config/initializers/secret_token.rb +0 -12
- data/guides/code/getting_started/config/initializers/session_store.rb +0 -3
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
- data/guides/code/getting_started/config/locales/en.yml +0 -23
- data/guides/code/getting_started/config/routes.rb +0 -7
- data/guides/code/getting_started/config.ru +0 -4
- data/guides/code/getting_started/db/migrate/20130122042648_create_posts.rb +0 -10
- data/guides/code/getting_started/db/migrate/20130122045842_create_comments.rb +0 -11
- data/guides/code/getting_started/db/schema.rb +0 -33
- data/guides/code/getting_started/db/seeds.rb +0 -7
- data/guides/code/getting_started/public/404.html +0 -60
- data/guides/code/getting_started/public/422.html +0 -60
- data/guides/code/getting_started/public/500.html +0 -59
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +0 -5
- data/guides/code/getting_started/test/controllers/comments_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/posts_controller_test.rb +0 -7
- data/guides/code/getting_started/test/controllers/welcome_controller_test.rb +0 -9
- data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
- data/guides/code/getting_started/test/fixtures/posts.yml +0 -9
- data/guides/code/getting_started/test/helpers/comments_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/posts_helper_test.rb +0 -4
- data/guides/code/getting_started/test/helpers/welcome_helper_test.rb +0 -4
- data/guides/code/getting_started/test/models/comment_test.rb +0 -7
- data/guides/code/getting_started/test/models/post_test.rb +0 -7
- data/guides/code/getting_started/test/test_helper.rb +0 -12
data/guides/source/routing.md
CHANGED
@@ -183,61 +183,61 @@ You may wish to organize groups of controllers under a namespace. Most commonly,
|
|
183
183
|
|
184
184
|
```ruby
|
185
185
|
namespace :admin do
|
186
|
-
resources :
|
186
|
+
resources :articles, :comments
|
187
187
|
end
|
188
188
|
```
|
189
189
|
|
190
|
-
This will create a number of routes for each of the `
|
190
|
+
This will create a number of routes for each of the `articles` and `comments` controller. For `Admin::ArticlesController`, Rails will create:
|
191
191
|
|
192
|
-
| HTTP Verb | Path
|
193
|
-
| --------- |
|
194
|
-
| GET | /admin/
|
195
|
-
| GET | /admin/
|
196
|
-
| POST | /admin/
|
197
|
-
| GET | /admin/
|
198
|
-
| GET | /admin/
|
199
|
-
| PATCH/PUT | /admin/
|
200
|
-
| DELETE | /admin/
|
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
201
|
|
202
|
-
If you want to route `/
|
202
|
+
If you want to route `/articles` (without the prefix `/admin`) to `Admin::ArticlesController`, you could use:
|
203
203
|
|
204
204
|
```ruby
|
205
205
|
scope module: 'admin' do
|
206
|
-
resources :
|
206
|
+
resources :articles, :comments
|
207
207
|
end
|
208
208
|
```
|
209
209
|
|
210
210
|
or, for a single case:
|
211
211
|
|
212
212
|
```ruby
|
213
|
-
resources :
|
213
|
+
resources :articles, module: 'admin'
|
214
214
|
```
|
215
215
|
|
216
|
-
If you want to route `/admin/
|
216
|
+
If you want to route `/admin/articles` to `ArticlesController` (without the `Admin::` module prefix), you could use:
|
217
217
|
|
218
218
|
```ruby
|
219
219
|
scope '/admin' do
|
220
|
-
resources :
|
220
|
+
resources :articles, :comments
|
221
221
|
end
|
222
222
|
```
|
223
223
|
|
224
224
|
or, for a single case:
|
225
225
|
|
226
226
|
```ruby
|
227
|
-
resources :
|
227
|
+
resources :articles, path: '/admin/articles'
|
228
228
|
```
|
229
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 `
|
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
231
|
|
232
|
-
| HTTP Verb | Path
|
233
|
-
| --------- |
|
234
|
-
| GET | /admin/
|
235
|
-
| GET | /admin/
|
236
|
-
| POST | /admin/
|
237
|
-
| GET | /admin/
|
238
|
-
| GET | /admin/
|
239
|
-
| PATCH/PUT | /admin/
|
240
|
-
| DELETE | /admin/
|
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
241
|
|
242
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
243
|
|
@@ -304,7 +304,7 @@ TIP: _Resources should never be nested more than 1 level deep._
|
|
304
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
305
|
|
306
306
|
```ruby
|
307
|
-
resources :
|
307
|
+
resources :articles do
|
308
308
|
resources :comments, only: [:index, :new, :create]
|
309
309
|
end
|
310
310
|
resources :comments, only: [:show, :edit, :update, :destroy]
|
@@ -313,7 +313,7 @@ resources :comments, only: [:show, :edit, :update, :destroy]
|
|
313
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
314
|
|
315
315
|
```ruby
|
316
|
-
resources :
|
316
|
+
resources :articles do
|
317
317
|
resources :comments, shallow: true
|
318
318
|
end
|
319
319
|
```
|
@@ -321,7 +321,7 @@ end
|
|
321
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
322
|
|
323
323
|
```ruby
|
324
|
-
resources :
|
324
|
+
resources :articles, shallow: true do
|
325
325
|
resources :comments
|
326
326
|
resources :quotes
|
327
327
|
resources :drafts
|
@@ -332,7 +332,7 @@ The `shallow` method of the DSL creates a scope inside of which every nesting is
|
|
332
332
|
|
333
333
|
```ruby
|
334
334
|
shallow do
|
335
|
-
resources :
|
335
|
+
resources :articles do
|
336
336
|
resources :comments
|
337
337
|
resources :quotes
|
338
338
|
resources :drafts
|
@@ -344,7 +344,7 @@ There exist two options for `scope` to customize shallow routes. `:shallow_path`
|
|
344
344
|
|
345
345
|
```ruby
|
346
346
|
scope shallow_path: "sekret" do
|
347
|
-
resources :
|
347
|
+
resources :articles do
|
348
348
|
resources :comments, shallow: true
|
349
349
|
end
|
350
350
|
end
|
@@ -352,21 +352,21 @@ end
|
|
352
352
|
|
353
353
|
The comments resource here will have the following routes generated for it:
|
354
354
|
|
355
|
-
| HTTP Verb | Path
|
356
|
-
| --------- |
|
357
|
-
| GET | /
|
358
|
-
| POST | /
|
359
|
-
| GET | /
|
360
|
-
| GET | /sekret/comments/:id/edit(.:format)
|
361
|
-
| GET | /sekret/comments/:id(.:format)
|
362
|
-
| PATCH/PUT | /sekret/comments/:id(.:format)
|
363
|
-
| DELETE | /sekret/comments/:id(.:format)
|
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
364
|
|
365
365
|
The `:shallow_prefix` option adds the specified parameter to the named helpers:
|
366
366
|
|
367
367
|
```ruby
|
368
368
|
scope shallow_prefix: "sekret" do
|
369
|
-
resources :
|
369
|
+
resources :articles do
|
370
370
|
resources :comments, shallow: true
|
371
371
|
end
|
372
372
|
end
|
@@ -374,15 +374,15 @@ end
|
|
374
374
|
|
375
375
|
The comments resource here will have the following routes generated for it:
|
376
376
|
|
377
|
-
| HTTP Verb | Path
|
378
|
-
| --------- |
|
379
|
-
| GET | /
|
380
|
-
| POST | /
|
381
|
-
| GET | /
|
382
|
-
| GET | /comments/:id/edit(.:format)
|
383
|
-
| GET | /comments/:id(.:format)
|
384
|
-
| PATCH/PUT | /comments/:id(.:format)
|
385
|
-
| DELETE | /comments/:id(.:format)
|
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
386
|
|
387
387
|
### Routing concerns
|
388
388
|
|
@@ -403,7 +403,7 @@ These concerns can be used in resources to avoid code duplication and share beha
|
|
403
403
|
```ruby
|
404
404
|
resources :messages, concerns: :commentable
|
405
405
|
|
406
|
-
resources :
|
406
|
+
resources :articles, concerns: [:commentable, :image_attachable]
|
407
407
|
```
|
408
408
|
|
409
409
|
The above is equivalent to:
|
@@ -413,7 +413,7 @@ resources :messages do
|
|
413
413
|
resources :comments
|
414
414
|
end
|
415
415
|
|
416
|
-
resources :
|
416
|
+
resources :articles do
|
417
417
|
resources :comments
|
418
418
|
resources :images, only: :index
|
419
419
|
end
|
@@ -422,7 +422,7 @@ end
|
|
422
422
|
Also you can use them in any place that you want inside the routes, for example in a scope or namespace call:
|
423
423
|
|
424
424
|
```ruby
|
425
|
-
namespace :
|
425
|
+
namespace :articles do
|
426
426
|
concerns :commentable
|
427
427
|
end
|
428
428
|
```
|
@@ -611,6 +611,8 @@ get 'photos/:id', to: 'photos#show', defaults: { format: 'jpg' }
|
|
611
611
|
|
612
612
|
Rails would match `photos/12` to the `show` action of `PhotosController`, and set `params[:format]` to `"jpg"`.
|
613
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
|
+
|
614
616
|
### Naming Routes
|
615
617
|
|
616
618
|
You can specify a name for any route using the `:as` option:
|
@@ -645,6 +647,8 @@ match 'photos', to: 'photos#show', via: :all
|
|
645
647
|
|
646
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.
|
647
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
|
+
|
648
652
|
### Segment Constraints
|
649
653
|
|
650
654
|
You can use the `:constraints` option to enforce a format for a dynamic segment:
|
@@ -662,26 +666,26 @@ get 'photos/:id', to: 'photos#show', id: /[A-Z]\d{5}/
|
|
662
666
|
`:constraints` takes regular expressions with the restriction that regexp anchors can't be used. For example, the following route will not work:
|
663
667
|
|
664
668
|
```ruby
|
665
|
-
get '/:id', to: '
|
669
|
+
get '/:id', to: 'articles#show', constraints: { id: /^\d/ }
|
666
670
|
```
|
667
671
|
|
668
672
|
However, note that you don't need to use anchors because all routes are anchored at the start.
|
669
673
|
|
670
|
-
For example, the following routes would allow for `
|
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:
|
671
675
|
|
672
676
|
```ruby
|
673
|
-
get '/:id', to: '
|
677
|
+
get '/:id', to: 'articles#show', constraints: { id: /\d.+/ }
|
674
678
|
get '/:username', to: 'users#show'
|
675
679
|
```
|
676
680
|
|
677
681
|
### Request-Based Constraints
|
678
682
|
|
679
|
-
You can also constrain a route based on any method on the
|
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`.
|
680
684
|
|
681
685
|
You specify a request-based constraint the same way that you specify a segment constraint:
|
682
686
|
|
683
687
|
```ruby
|
684
|
-
get 'photos', constraints: {subdomain: 'admin'}
|
688
|
+
get 'photos', to: 'photos#index', constraints: { subdomain: 'admin' }
|
685
689
|
```
|
686
690
|
|
687
691
|
You can also specify constraints in a block form:
|
@@ -694,6 +698,8 @@ namespace :admin do
|
|
694
698
|
end
|
695
699
|
```
|
696
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
|
+
|
697
703
|
### Advanced Constraints
|
698
704
|
|
699
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:
|
@@ -709,7 +715,7 @@ class BlacklistConstraint
|
|
709
715
|
end
|
710
716
|
end
|
711
717
|
|
712
|
-
|
718
|
+
Rails.application.routes.draw do
|
713
719
|
get '*path', to: 'blacklist#index',
|
714
720
|
constraints: BlacklistConstraint.new
|
715
721
|
end
|
@@ -718,7 +724,7 @@ end
|
|
718
724
|
You can also specify constraints as a lambda:
|
719
725
|
|
720
726
|
```ruby
|
721
|
-
|
727
|
+
Rails.application.routes.draw do
|
722
728
|
get '*path', to: 'blacklist#index',
|
723
729
|
constraints: lambda { |request| Blacklist.retrieve_ips.include?(request.remote_ip) }
|
724
730
|
end
|
@@ -752,7 +758,7 @@ get '*a/foo/*b', to: 'test#index'
|
|
752
758
|
|
753
759
|
would match `zoo/woo/foo/bar/baz` with `params[:a]` equals `'zoo/woo'`, and `params[:b]` equals `'bar/baz'`.
|
754
760
|
|
755
|
-
NOTE: By requesting `'/foo/bar.json'`, your `params[:pages]` will be
|
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:
|
756
762
|
|
757
763
|
```ruby
|
758
764
|
get '*pages', to: 'pages#show', format: false
|
@@ -769,20 +775,20 @@ get '*pages', to: 'pages#show', format: true
|
|
769
775
|
You can redirect any path to another path using the `redirect` helper in your router:
|
770
776
|
|
771
777
|
```ruby
|
772
|
-
get '/stories', to: redirect('/
|
778
|
+
get '/stories', to: redirect('/articles')
|
773
779
|
```
|
774
780
|
|
775
781
|
You can also reuse dynamic segments from the match in the path to redirect to:
|
776
782
|
|
777
783
|
```ruby
|
778
|
-
get '/stories/:name', to: redirect('/
|
784
|
+
get '/stories/:name', to: redirect('/articles/%{name}')
|
779
785
|
```
|
780
786
|
|
781
787
|
You can also provide a block to redirect, which receives the symbolized path parameters and the request object:
|
782
788
|
|
783
789
|
```ruby
|
784
|
-
get '/stories/:name', to: redirect {|path_params, req| "/
|
785
|
-
get '/stories', to: redirect {|path_params, req| "/
|
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}" }
|
786
792
|
```
|
787
793
|
|
788
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.
|
@@ -791,7 +797,7 @@ In all of these cases, if you don't provide the leading host (`http://www.exampl
|
|
791
797
|
|
792
798
|
### Routing to Rack Applications
|
793
799
|
|
794
|
-
Instead of a String like `'
|
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:
|
795
801
|
|
796
802
|
```ruby
|
797
803
|
match '/application.js', to: Sprockets, via: :all
|
@@ -799,7 +805,19 @@ match '/application.js', to: Sprockets, via: :all
|
|
799
805
|
|
800
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.
|
801
807
|
|
802
|
-
NOTE: For the curious, `'
|
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
|
+
```
|
803
821
|
|
804
822
|
### Using `root`
|
805
823
|
|
@@ -835,7 +853,7 @@ get 'こんにちは', to: 'welcome#index'
|
|
835
853
|
Customizing Resourceful Routes
|
836
854
|
------------------------------
|
837
855
|
|
838
|
-
While the default routes and helpers generated by `resources :
|
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.
|
839
857
|
|
840
858
|
### Specifying a Controller to Use
|
841
859
|
|
@@ -877,7 +895,7 @@ a warning.
|
|
877
895
|
You can use the `:constraints` option to specify a required format on the implicit `id`. For example:
|
878
896
|
|
879
897
|
```ruby
|
880
|
-
resources :photos, constraints: {id: /[A-Z][A-Z][0-9]+/}
|
898
|
+
resources :photos, constraints: { id: /[A-Z][A-Z][0-9]+/ }
|
881
899
|
```
|
882
900
|
|
883
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.
|
@@ -917,7 +935,7 @@ will recognize incoming paths beginning with `/photos` and route the requests to
|
|
917
935
|
|
918
936
|
### Overriding the `new` and `edit` Segments
|
919
937
|
|
920
|
-
The `:path_names` option lets you override the automatically-generated
|
938
|
+
The `:path_names` option lets you override the automatically-generated `new` and `edit` segments in paths:
|
921
939
|
|
922
940
|
```ruby
|
923
941
|
resources :photos, path_names: { new: 'make', edit: 'change' }
|
@@ -952,7 +970,7 @@ end
|
|
952
970
|
resources :photos
|
953
971
|
```
|
954
972
|
|
955
|
-
This will provide route helpers such as `admin_photos_path`, `new_admin_photo_path
|
973
|
+
This will provide route helpers such as `admin_photos_path`, `new_admin_photo_path`, etc.
|
956
974
|
|
957
975
|
To prefix a group of route helpers, use `:as` with `scope`:
|
958
976
|
|
@@ -972,15 +990,15 @@ You can prefix routes with a named parameter also:
|
|
972
990
|
|
973
991
|
```ruby
|
974
992
|
scope ':username' do
|
975
|
-
resources :
|
993
|
+
resources :articles
|
976
994
|
end
|
977
995
|
```
|
978
996
|
|
979
|
-
This will provide you with URLs such as `/bob/
|
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.
|
980
998
|
|
981
999
|
### Restricting the Routes Created
|
982
1000
|
|
983
|
-
By default, Rails creates routes for the seven default actions (index
|
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:
|
984
1002
|
|
985
1003
|
```ruby
|
986
1004
|
resources :photos, only: [:index, :show]
|
@@ -1042,6 +1060,28 @@ end
|
|
1042
1060
|
|
1043
1061
|
This will create routing helpers such as `magazine_periodical_ads_url` and `edit_magazine_periodical_ad_path`.
|
1044
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
|
+
|
1045
1085
|
Inspecting and Testing Routes
|
1046
1086
|
-----------------------------
|
1047
1087
|
|
@@ -13,17 +13,17 @@ After reading this guide, you will know:
|
|
13
13
|
Markdown
|
14
14
|
-------
|
15
15
|
|
16
|
-
Guides are written in [GitHub Flavored Markdown](
|
16
|
+
Guides are written in [GitHub Flavored Markdown](https://help.github.com/articles/github-flavored-markdown). There is comprehensive [documentation for Markdown](http://daringfireball.net/projects/markdown/syntax), as well as a [cheatsheet](http://daringfireball.net/projects/markdown/basics).
|
17
17
|
|
18
18
|
Prologue
|
19
19
|
--------
|
20
20
|
|
21
|
-
Each guide should start with motivational text at the top (that's the little introduction in the blue area). The prologue should tell the reader what the guide is about, and what they will learn.
|
21
|
+
Each guide should start with motivational text at the top (that's the little introduction in the blue area). The prologue should tell the reader what the guide is about, and what they will learn. As an example, see the [Routing Guide](routing.html).
|
22
22
|
|
23
|
-
|
23
|
+
Headings
|
24
24
|
------
|
25
25
|
|
26
|
-
The title of every guide uses `h1
|
26
|
+
The title of every guide uses an `h1` heading; guide sections use `h2` headings; subsections use `h3` headings; etc. Note that the generated HTML output will use heading tags starting with `<h2>`.
|
27
27
|
|
28
28
|
```
|
29
29
|
Guide Title
|
@@ -35,14 +35,14 @@ Section
|
|
35
35
|
### Sub Section
|
36
36
|
```
|
37
37
|
|
38
|
-
|
38
|
+
When writing headings, capitalize all words except for prepositions, conjunctions, internal articles, and forms of the verb "to be":
|
39
39
|
|
40
40
|
```
|
41
41
|
#### Middleware Stack is an Array
|
42
42
|
#### When are Objects Saved?
|
43
43
|
```
|
44
44
|
|
45
|
-
Use the same
|
45
|
+
Use the same inline formatting as regular text:
|
46
46
|
|
47
47
|
```
|
48
48
|
##### The `:content_type` Option
|
@@ -51,25 +51,24 @@ Use the same typography as in regular text:
|
|
51
51
|
API Documentation Guidelines
|
52
52
|
----------------------------
|
53
53
|
|
54
|
-
The guides and the API should be coherent and consistent where appropriate.
|
54
|
+
The guides and the API should be coherent and consistent where appropriate. In particular, these sections of the [API Documentation Guidelines](api_documentation_guidelines.html) also apply to the guides:
|
55
55
|
|
56
56
|
* [Wording](api_documentation_guidelines.html#wording)
|
57
|
+
* [English](api_documentation_guidelines.html#english)
|
57
58
|
* [Example Code](api_documentation_guidelines.html#example-code)
|
58
|
-
* [Filenames](api_documentation_guidelines.html#
|
59
|
+
* [Filenames](api_documentation_guidelines.html#file-names)
|
59
60
|
* [Fonts](api_documentation_guidelines.html#fonts)
|
60
61
|
|
61
|
-
Those guidelines apply also to guides.
|
62
|
-
|
63
62
|
HTML Guides
|
64
63
|
-----------
|
65
64
|
|
66
65
|
Before generating the guides, make sure that you have the latest version of Bundler installed on your system. As of this writing, you must install Bundler 1.3.5 on your device.
|
67
66
|
|
68
|
-
To install the latest version of Bundler,
|
67
|
+
To install the latest version of Bundler, run `gem install bundler`.
|
69
68
|
|
70
69
|
### Generation
|
71
70
|
|
72
|
-
To generate all the guides, just `cd` into the `guides` directory, run `bundle install
|
71
|
+
To generate all the guides, just `cd` into the `guides` directory, run `bundle install`, and execute:
|
73
72
|
|
74
73
|
```
|
75
74
|
bundle exec rake guides:generate
|