Skip to content

Commit

Permalink
Merge pull request #14986 from dlangevin/trailing-slash-url-generation
Browse files Browse the repository at this point in the history
Fixes URL generation with trailing_slash: true

Conflicts:
	actionpack/lib/action_dispatch/http/url.rb

Conflicts:
	actionpack/CHANGELOG.md
	actionpack/lib/action_dispatch/http/url.rb
  • Loading branch information
rafaelfranca committed May 24, 2014
1 parent 5920d58 commit f83ca9a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
5 changes: 5 additions & 0 deletions actionpack/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Rails 4.1.2 (unreleased) ##

* Fix URL generation with `:trailing_slash` such that it does not add
a trailing slash after `.:format`

*Dan Langevin*

* Fixed an issue with migrating legacy json cookies.

Previously, the `VerifyAndUpgradeLegacySignedMessage` assumes all incoming
Expand Down
26 changes: 17 additions & 9 deletions actionpack/lib/action_dispatch/http/url.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,34 @@ def url_for(options = {})
path = options.delete(:script_name).to_s.chomp("/")
path << options.delete(:path).to_s

add_trailing_slash(path) if options[:trailing_slash]

params = options[:params].is_a?(Hash) ? options[:params] : options.slice(:params)
params.reject! { |_,v| v.to_param.nil? }

result = build_host_url(options)
if options[:trailing_slash]
if path.include?('?')
result << path.sub(/\?/, '/\&')
else
result << path.sub(/[^\/]\z|\A\z/, '\&/')
end
else
result << path
end

result << path

result << "?#{params.to_query}" unless params.empty?
result << "##{Journey::Router::Utils.escape_fragment(options[:anchor].to_param.to_s)}" if options[:anchor]
result
end

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 options[:host].blank? && options[:only_path].blank?
raise ArgumentError, 'Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true'
Expand Down
18 changes: 18 additions & 0 deletions actionpack/test/dispatch/url_generation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def index
Routes.draw do
get "/foo", :to => "my_route_generating#index", :as => :foo

resources :bars

mount MyRouteGeneratingController.action(:index), at: '/bar'
end

Expand Down Expand Up @@ -109,6 +111,22 @@ def app
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

0 comments on commit f83ca9a

Please sign in to comment.