mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #14986 from dlangevin/trailing-slash-url-generation
Fixes URL generation with trailing_slash: true Conflicts: actionpack/lib/action_dispatch/http/url.rb
This commit is contained in:
commit
ade105be40
3 changed files with 37 additions and 7 deletions
|
@ -1,3 +1,8 @@
|
|||
* Fix URL generation with `:trailing_slash` such that it does not add
|
||||
a trailing slash after `.:format`
|
||||
|
||||
*Dan Langevin*
|
||||
|
||||
* Build full URI as string when processing path in integration tests for
|
||||
performance reasons.
|
||||
|
||||
|
@ -131,4 +136,5 @@
|
|||
|
||||
*Tony Wooster*
|
||||
|
||||
|
||||
Please check [4-1-stable](https://github.com/rails/rails/blob/4-1-stable/actionpack/CHANGELOG.md) for previous changes.
|
||||
|
|
|
@ -37,13 +37,7 @@ module ActionDispatch
|
|||
path = options[:script_name].to_s.chomp("/")
|
||||
path << options[:path].to_s
|
||||
|
||||
if options[:trailing_slash]
|
||||
if path.include?('?')
|
||||
path.sub!(/\?/, '/\&')
|
||||
else
|
||||
path.sub!(/[^\/]\z|\A\z/, '\&/')
|
||||
end
|
||||
end
|
||||
add_trailing_slash(path) if options[:trailing_slash]
|
||||
|
||||
result = path
|
||||
|
||||
|
@ -66,6 +60,18 @@ module ActionDispatch
|
|||
|
||||
private
|
||||
|
||||
def add_trailing_slash(path)
|
||||
# includes querysting
|
||||
if path.include?('?')
|
||||
path.sub!(/\?/, '/\&')
|
||||
# does not have a .format
|
||||
elsif !path.include?(".")
|
||||
path.sub!(/[^\/]\z|\A\z/, '\&/')
|
||||
end
|
||||
|
||||
path
|
||||
end
|
||||
|
||||
def build_host_url(options)
|
||||
if match = options[:host].match(HOST_REGEXP)
|
||||
options[:protocol] ||= match[1] unless options[:protocol] == false
|
||||
|
|
|
@ -15,6 +15,8 @@ module TestUrlGeneration
|
|||
Routes.draw do
|
||||
get "/foo", :to => "my_route_generating#index", :as => :foo
|
||||
|
||||
resources :bars
|
||||
|
||||
mount MyRouteGeneratingController.action(:index), at: '/bar'
|
||||
end
|
||||
|
||||
|
@ -109,6 +111,22 @@ module TestUrlGeneration
|
|||
test "omit subdomain when key is blank" do
|
||||
assert_equal "http://example.com/foo", foo_url(subdomain: "")
|
||||
end
|
||||
|
||||
test "generating URLs with trailing slashes" do
|
||||
assert_equal "/bars.json", bars_path(
|
||||
trailing_slash: true,
|
||||
format: 'json'
|
||||
)
|
||||
end
|
||||
|
||||
test "generating URLS with querystring and trailing slashes" do
|
||||
assert_equal "/bars.json?a=b", bars_path(
|
||||
trailing_slash: true,
|
||||
a: 'b',
|
||||
format: 'json'
|
||||
)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue