aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/http
diff options
context:
space:
mode:
authorDan Langevin <dan.langevin@lifebooker.com>2014-05-05 18:25:29 -0400
committerDan Langevin <dan.langevin@lifebooker.com>2014-05-22 16:03:52 -0400
commit82b4d879bf31ebf409217e2c770cedfb7c79a44a (patch)
treeb9d82dd81e9b9bf2e88cb6d30ca4f26988f7ad2e /actionpack/lib/action_dispatch/http
parentb6bab2af144d3f35ae2e1661286f2e35f43f5d3a (diff)
downloadrails-82b4d879bf31ebf409217e2c770cedfb7c79a44a.tar.gz
rails-82b4d879bf31ebf409217e2c770cedfb7c79a44a.tar.bz2
rails-82b4d879bf31ebf409217e2c770cedfb7c79a44a.zip
Fixes URL generation with trailing_slash: true
URL generation with trailing_slash: true was adding a trailing slash after .:format Routes.draw do resources :bars end bars_url(trailing_slash: true, format: 'json') # => /bars.json/ This commit removes that extra trailing slash
Diffstat (limited to 'actionpack/lib/action_dispatch/http')
-rw-r--r--actionpack/lib/action_dispatch/http/url.rb26
1 files changed, 17 insertions, 9 deletions
diff --git a/actionpack/lib/action_dispatch/http/url.rb b/actionpack/lib/action_dispatch/http/url.rb
index 6f5a52c568..ffc9fc7301 100644
--- a/actionpack/lib/action_dispatch/http/url.rb
+++ b/actionpack/lib/action_dispatch/http/url.rb
@@ -34,19 +34,15 @@ module ActionDispatch
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
@@ -54,6 +50,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 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'