Skip to content

Commit 0d18b43

Browse files
authored
Merge pull request rails#42283 from HParker/named-routes-identifies-false
cause rails to correctly place optional path parameter booleans
2 parents 905ab3d + 98ed240 commit 0d18b43

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

actionpack/lib/action_dispatch/journey/formatter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def extract_parameterized_parts(route, options, recall)
103103
parameterized_parts = recall.merge(options)
104104

105105
keys_to_keep = route.parts.reverse_each.drop_while { |part|
106-
!(options.key?(part) || route.scope_options.key?(part)) || (options[part] || recall[part]).nil?
106+
!(options.key?(part) || route.scope_options.key?(part)) || (options[part].nil? && recall[part].nil?)
107107
} | route.required_parts
108108

109109
parameterized_parts.delete_if do |bad_key, _|
@@ -118,7 +118,7 @@ def extract_parameterized_parts(route, options, recall)
118118
end
119119
end
120120

121-
parameterized_parts.keep_if { |_, v| v }
121+
parameterized_parts.compact!
122122
parameterized_parts
123123
end
124124

actionpack/lib/action_dispatch/journey/visitors.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def evaluate(hash)
4040
@parameters.each do |index|
4141
param = parts[index]
4242
value = hash[param.name]
43-
return "" unless value
43+
return "" if value.nil?
4444
parts[index] = param.escape value
4545
end
4646

actionpack/test/dispatch/url_generation_test.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def index
1616

1717
Routes.draw do
1818
get "/foo", to: "my_route_generating#index", as: :foo
19+
get "(/optional/:optional_id)/baz", to: "my_route_generating#index", as: :baz
1920

2021
resources :bars
2122

@@ -127,6 +128,34 @@ def app
127128
assert_equal "http://example.com/foo", foo_url(subdomain: "")
128129
end
129130

131+
test "keep optional path parameter when given" do
132+
assert_equal "http://www.example.com/optional/123/baz", baz_url(optional_id: 123)
133+
end
134+
135+
test "keep optional path parameter when true" do
136+
assert_equal "http://www.example.com/optional/true/baz", baz_url(optional_id: true)
137+
end
138+
139+
test "omit optional path parameter when false" do
140+
assert_equal "http://www.example.com/optional/false/baz", baz_url(optional_id: false)
141+
end
142+
143+
test "omit optional path parameter when blank" do
144+
assert_equal "http://www.example.com/baz", baz_url(optional_id: "")
145+
end
146+
147+
test "keep positional path parameter when true" do
148+
assert_equal "http://www.example.com/optional/true/baz", baz_url(true)
149+
end
150+
151+
test "omit positional path parameter when false" do
152+
assert_equal "http://www.example.com/optional/false/baz", baz_url(false)
153+
end
154+
155+
test "omit positional path parameter when blank" do
156+
assert_equal "http://www.example.com/baz", baz_url("")
157+
end
158+
130159
test "generating URLs with trailing slashes" do
131160
assert_equal "/bars.json", bars_path(
132161
trailing_slash: true,

0 commit comments

Comments
 (0)