Merge pull request #42854 from jonathanhefner/current_page-with-trailing-slash
Fix current_page? when URL has trailing slash
This commit is contained in:
commit
6abe728e1d
|
@ -573,16 +573,14 @@ module ActionView
|
||||||
request_uri = url_string.index("?") || check_parameters ? request.fullpath : request.path
|
request_uri = url_string.index("?") || check_parameters ? request.fullpath : request.path
|
||||||
request_uri = URI::DEFAULT_PARSER.unescape(request_uri).force_encoding(Encoding::BINARY)
|
request_uri = URI::DEFAULT_PARSER.unescape(request_uri).force_encoding(Encoding::BINARY)
|
||||||
|
|
||||||
if url_string.start_with?("/") && url_string != "/"
|
if %r{^\w+://}.match?(url_string)
|
||||||
url_string.chomp!("/")
|
request_uri = +"#{request.protocol}#{request.host_with_port}#{request_uri}"
|
||||||
request_uri.chomp!("/")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if %r{^\w+://}.match?(url_string)
|
remove_trailing_slash!(url_string)
|
||||||
url_string == "#{request.protocol}#{request.host_with_port}#{request_uri}"
|
remove_trailing_slash!(request_uri)
|
||||||
else
|
|
||||||
url_string == request_uri
|
url_string == request_uri
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if RUBY_VERSION.start_with?("2.7")
|
if RUBY_VERSION.start_with?("2.7")
|
||||||
|
@ -802,6 +800,11 @@ module ActionView
|
||||||
|
|
||||||
params.sort_by { |pair| pair[:name] }
|
params.sort_by { |pair| pair[:name] }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def remove_trailing_slash!(url_string)
|
||||||
|
trailing_index = (url_string.index("?") || 0) - 1
|
||||||
|
url_string[trailing_index] = "" if url_string[trailing_index] == "/"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -625,6 +625,14 @@ class UrlHelperTest < ActiveSupport::TestCase
|
||||||
@request = request_for_url("/posts")
|
@request = request_for_url("/posts")
|
||||||
|
|
||||||
assert current_page?("/posts/")
|
assert current_page?("/posts/")
|
||||||
|
assert current_page?("http://www.example.com/posts/")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_current_page_with_trailing_slash_and_params
|
||||||
|
@request = request_for_url("/posts?order=desc")
|
||||||
|
|
||||||
|
assert current_page?("/posts/?order=desc")
|
||||||
|
assert current_page?("http://www.example.com/posts/?order=desc")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_current_page_with_not_get_verb
|
def test_current_page_with_not_get_verb
|
||||||
|
|
Loading…
Reference in New Issue