From 4af50dfd95a5d3dc998a9ca53c67d574dbeed7b5 Mon Sep 17 00:00:00 2001 From: 9sako6 <31821663+9sako6@users.noreply.github.com> Date: Mon, 13 Sep 2021 15:30:55 +0900 Subject: [PATCH] Fix `NameError` when an invalid `:on` option is given to a route Fix the following error. ``` /Users/9sako6/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/bundler/gems/rails-366c8f081d8b/actionpack/lib/action_dispatch/routing/mapper.rb:1865:in `map_match': undefined local variable or method `on' for # (NameError) ``` --- actionpack/lib/action_dispatch/routing/mapper.rb | 2 +- actionpack/test/dispatch/mapper_test.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 7851b9eb1e5d6..3da6ab1e13f2e 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1861,7 +1861,7 @@ def path_scope(path) end def map_match(paths, options) - if options[:on] && !VALID_ON_OPTIONS.include?(options[:on]) + if (on = options[:on]) && !VALID_ON_OPTIONS.include?(on) raise ArgumentError, "Unknown scope #{on.inspect} given to :on" end diff --git a/actionpack/test/dispatch/mapper_test.rb b/actionpack/test/dispatch/mapper_test.rb index 969a08efed4ae..3f2841bae8805 100644 --- a/actionpack/test/dispatch/mapper_test.rb +++ b/actionpack/test/dispatch/mapper_test.rb @@ -193,6 +193,16 @@ def test_raising_error_when_rack_app_is_not_passed end end + def test_raising_error_when_invalid_on_option_is_given + fakeset = FakeSet.new + mapper = Mapper.new fakeset + error = assert_raise ArgumentError do + mapper.get "/foo", on: :invalid_option + end + + assert_equal "Unknown scope :invalid_option given to :on", error.message + end + def test_scope_does_not_destructively_mutate_default_options fakeset = FakeSet.new mapper = Mapper.new fakeset