Skip to content

Commit 18f3991

Browse files
klevojonathanhefner
authored andcommitted
Improve Parametric Scopes documentation
1 parent 7db044f commit 18f3991

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

guides/source/routing.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ When using `magazine_ad_path`, you can pass in instances of `Magazine` and `Ad`
511511
<%= link_to 'Ad details', magazine_ad_path(@magazine, @ad) %>
512512
```
513513

514-
You can also use `url_for` with a set of objects, and Rails will automatically determine which route you want:
514+
You can also use [`url_for`][ActionView::RoutingUrlFor#url_for] with a set of objects, and Rails will automatically determine which route you want:
515515

516516
```erb
517517
<%= link_to 'Ad details', url_for([@magazine, @ad]) %>
@@ -537,6 +537,8 @@ For other actions, you just need to insert the action name as the first element
537537

538538
This allows you to treat instances of your models as URLs, and is a key advantage to using the resourceful style.
539539

540+
[ActionView::RoutingUrlFor#url_for]: https://api.rubyonrails.org/classes/ActionView/RoutingUrlFor.html#method-i-url_for
541+
540542
### Adding More RESTful Actions
541543

542544
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.
@@ -1164,15 +1166,29 @@ resources to use `photos_path` and `accounts_path`.
11641166

11651167
NOTE: The `namespace` scope will automatically add `:as` as well as `:module` and `:path` prefixes.
11661168

1167-
You can prefix routes with a named parameter also:
1169+
#### Parametric Scopes
1170+
1171+
You can prefix routes with a named parameter:
11681172

11691173
```ruby
1170-
scope ':username' do
1174+
scope ':account_id', as: 'account', constraints: { account_id: /\d+/ } do
11711175
resources :articles
11721176
end
11731177
```
11741178

1175-
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.
1179+
This will provide you with paths such as `/1/articles/9` and will allow you to reference the `account_id` part of the path as `params[:account_id]` in controllers, helpers, and views.
1180+
1181+
It will also generate path and URL helpers prefixed with `account_`, into which you can pass your objects as expected:
1182+
1183+
```ruby
1184+
account_article_path(@account, @article) # => /1/article/9
1185+
url_for([@account, @article]) # => /1/article/9
1186+
form_with(model: [@account, @article]) # => <form action="/1/article/9" ...>
1187+
```
1188+
1189+
We are [using a constraint](#segment-constraints) to limit the scope to only match ID-like strings. You can change the constraint to suit your needs, or omit it entirely. The `:as` option is also not strictly required, but without it, Rails will raise an error when evaluating `url_for([@account, @article])` or other helpers that rely on `url_for`, such as [`form_with`][].
1190+
1191+
[`form_with`]: https://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_with
11761192

11771193
### Restricting the Routes Created
11781194

0 commit comments

Comments
 (0)