Skip to content

Commit a4305e1

Browse files
committed
Add resource name to the ArgumentError that's raised when invalid :only or :except options are given to #resource or #resources
This makes it easier to locate the source of the problem, especially for routes drawn by gems. Before: :only and :except must include only [:index, :create, :new, :show, :update, :destroy, :edit], but also included [:foo, :bar] After: Route `resources :products` - :only and :except must include only [:index, :create, :new, :show, :update, :destroy, :edit], but also included [:foo, :bar] Fixes rails#54134
1 parent a4b202f commit a4305e1

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

actionpack/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
* Add resource name to the `ArgumentError` that's raised when invalid `:only` or `:except` options are given to `#resource` or `#resources`
2+
3+
This makes it easier to locate the source of the problem, especially for routes drawn by gems.
4+
5+
Before:
6+
```
7+
:only and :except must include only [:index, :create, :new, :show, :update, :destroy, :edit], but also included [:foo, :bar]
8+
```
9+
10+
After:
11+
```
12+
Route `resources :products` - :only and :except must include only [:index, :create, :new, :show, :update, :destroy, :edit], but also included [:foo, :bar]
13+
```
14+
15+
*Jeremy Green*
16+
117
* Add `check_collisions` option to `ActionDispatch::Session::CacheStore`.
218
319
Newly generated session ids use 128 bits of randomness, which is more than

actionpack/lib/action_dispatch/routing/mapper.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,8 @@ def initialize(entities, api_only, shallow, only: nil, except: nil, **options)
13281328

13291329
valid_actions = self.class.default_actions(false) # ignore api_only for this validation
13301330
if (invalid_actions = invalid_only_except_options(valid_actions, only:, except:).presence)
1331-
raise ArgumentError, ":only and :except must include only #{valid_actions}, but also included #{invalid_actions}"
1331+
error_prefix = "Route `resource#{"s" unless singleton?} :#{entities}`"
1332+
raise ArgumentError, "#{error_prefix} - :only and :except must include only #{valid_actions}, but also included #{invalid_actions}"
13321333
end
13331334

13341335
@name = entities.to_s

actionpack/test/controller/resources_test.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ def test_singleton_resource_name_is_not_singularized
11311131
end
11321132

11331133
def test_invalid_only_option_for_resources
1134-
expected_message = ":only and :except must include only [:index, :create, :new, :show, :update, :destroy, :edit], but also included [:foo, :bar]"
1134+
expected_message = "Route `resources :products` - :only and :except must include only [:index, :create, :new, :show, :update, :destroy, :edit], but also included [:foo, :bar]"
11351135
assert_raise(ArgumentError, match: expected_message) do
11361136
with_routing do |set|
11371137
set.draw do
@@ -1142,7 +1142,7 @@ def test_invalid_only_option_for_resources
11421142
end
11431143

11441144
def test_invalid_only_option_for_singleton_resource
1145-
expected_message = ":only and :except must include only [:show, :create, :update, :destroy, :new, :edit], but also included [:foo, :bar]"
1145+
expected_message = "Route `resource :products` - :only and :except must include only [:show, :create, :update, :destroy, :new, :edit], but also included [:foo, :bar]"
11461146
assert_raise(ArgumentError, match: expected_message) do
11471147
with_routing do |set|
11481148
set.draw do
@@ -1153,7 +1153,7 @@ def test_invalid_only_option_for_singleton_resource
11531153
end
11541154

11551155
def test_invalid_except_option_for_resources
1156-
expected_message = ":only and :except must include only [:index, :create, :new, :show, :update, :destroy, :edit], but also included [:foo]"
1156+
expected_message = "Route `resources :products` - :only and :except must include only [:index, :create, :new, :show, :update, :destroy, :edit], but also included [:foo]"
11571157

11581158
assert_raise(ArgumentError, match: expected_message) do
11591159
with_routing do |set|
@@ -1165,7 +1165,7 @@ def test_invalid_except_option_for_resources
11651165
end
11661166

11671167
def test_invalid_except_option_for_singleton_resource
1168-
expected_message = ":only and :except must include only [:show, :create, :update, :destroy, :new, :edit], but also included [:foo]"
1168+
expected_message = "Route `resource :products` - :only and :except must include only [:show, :create, :update, :destroy, :new, :edit], but also included [:foo]"
11691169
assert_raise(ArgumentError, match: expected_message) do
11701170
with_routing do |set|
11711171
set.draw do

0 commit comments

Comments
 (0)