Skip to content

Commit

Permalink
[Fix rails#51463] Raise an error when invalid :on or :except opti…
Browse files Browse the repository at this point in the history
…ons are given to #resource or #resouces
  • Loading branch information
joshuay03 committed Apr 2, 2024
1 parent ee4a371 commit 0b80692
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Raise an `ArgumentError` when invalid `:on` or `:except` options are passed into `#resouce` and `#resources`.

*Joshua Young*

* Request Forgery takes relative paths into account.

*Stefan Wienert*
Expand Down
5 changes: 5 additions & 0 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,11 @@ def initialize(entities, api_only, shallow, options = {})
raise ArgumentError, ":param option can't contain colons"
end

valid_actions = CANONICAL_ACTIONS.map(&:to_sym)
if (invalid_actions = (options.values_at(:only, :except).flatten.compact - valid_actions)).any?
raise ArgumentError, "expected :only and :except to be one or more of #{valid_actions}, got #{invalid_actions}"
end

@name = entities.to_s
@path = (options[:path] || @name).to_s
@controller = (options[:controller] || @name).to_s
Expand Down
22 changes: 22 additions & 0 deletions actionpack/test/controller/resources_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,28 @@ def test_singleton_resource_name_is_not_singularized
end
end

def test_invalid_only_option
expected_message = "expected :only and :except to be one or more of [:index, :create, :new, :show, :update, :destroy], got [:foo, :bar]"
assert_raise(ArgumentError, match: expected_message) do
with_routing do |set|
set.draw do
resources :products, only: [:foo, :bar]
end
end
end
end

def test_invalid_except_option
expected_message = "expected :only and :except to be one or more of [:index, :create, :new, :show, :update, :destroy], got [:foo]"
assert_raise(ArgumentError, match: expected_message) do
with_routing do |set|
set.draw do
resources :products, except: :foo
end
end
end
end

private
def with_restful_routing(*args)
options = args.extract_options!
Expand Down

0 comments on commit 0b80692

Please sign in to comment.