Permalink
Browse files

remove side effects on :controller option in #url_for

Bug Description:

If the :controller option in an url_for call contains a leading slash it
will be removed, which causes side-effects:

my_controller = '/Example'
url_for(:controller => my_controller)
puts my_controller # => 'Example'

Resolution:

The route-set does not directly manipulate the string passed into #url_for.
It creates a copy without the leading slash if necessary.
  • Loading branch information...
1 parent 0e8a4b3 commit 3edf53f2c7e55924632a27d08318b62848ba1735 @senny committed May 18, 2012
@@ -412,7 +412,7 @@ def initialize(options, recall, set, extras = false)
normalize_options!
normalize_controller_action_id!
use_relative_controller!
- controller.sub!(%r{^/}, '') if controller
+ normalize_controller_leading_slash
handle_nil_action!
end
@@ -478,6 +478,11 @@ def use_relative_controller!
end
end
+ # remove leading slashes from the controller: '/foo/bar' becomes 'foo/bar'
+ def normalize_controller_leading_slash
+ @controller = @options[:controller] = controller.sub(%r{^/}, '') if controller
+ end
+
# This handles the case of :action => nil being explicitly passed.
# It is identical to :action => "index"
def handle_nil_action!
@@ -333,6 +333,14 @@ def test_false_url_params_are_included_in_query
assert_equal("/c/a?show=false", W.new.url_for(:only_path => true, :controller => 'c', :action => 'a', :show => false))
end
+ def test_url_for_has_no_side_effects
+ add_host!
+
+ controller_name = '/C'
+ assert_equal('http://www.basecamphq.com/C', W.new.url_for(:controller => controller_name))
+ assert_equal('/C', controller_name)
+ end
+
private
def extract_params(url)
url.split('?', 2).last.split('&').sort

0 comments on commit 3edf53f

Please sign in to comment.