Skip to content
Browse files

Provide correct defaults for Named Routes which do not specify :action

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1833 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 16595c9 commit 6ed16ff60290620c87772670d312585f2431d03f @seckar seckar committed Jul 14, 2005
Showing with 39 additions and 3 deletions.
  1. +2 −0 actionpack/CHANGELOG
  2. +10 −3 actionpack/lib/action_controller/routing.rb
  3. +27 −0 actionpack/test/controller/routing_test.rb
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fixed routes so that routes which do not specify :action in the path or in the requirements have a default of :action => 'index', In addition, fixed url generation so that :action => 'index' does not need to be provided for such urls. [Nicholas Seckar, Markjuh]
+
* Worked around a Safari bug where it wouldn't pass headers through if the response was zero length by having render :nothing return ' ' instead of ''
* Fixed Request#subdomains to handle "foo.foo.com" correctly
View
13 actionpack/lib/action_controller/routing.rb
@@ -288,9 +288,16 @@ def write_generation(generator = CodeGeneration::GenerationGenerator.new)
generator.before, generator.current, generator.after = [], components.first, (components[1..-1] || [])
if known.empty? then generator.go
- else generator.if(generator.check_conditions(known)) {|gp| gp.go }
+ else
+ # Alter the conditions to allow :action => 'index' to also catch :action => nil
+ altered_known = known.collect do |k, v|
+ if k == :action && v== 'index' then [k, [nil, 'index']]
+ else [k, v]
+ end
+ end
+ generator.if(generator.check_conditions(altered_known)) {|gp| gp.go }
end
-
+
generator
end
@@ -363,7 +370,7 @@ def configure_components(defaults, conditions)
def add_default_requirements
component_keys = components.collect {|c| c.key}
- known[:action] ||= [nil, 'index'] unless component_keys.include? :action
+ known[:action] ||= 'index' unless component_keys.include? :action
end
end
View
27 actionpack/test/controller/routing_test.rb
@@ -836,6 +836,33 @@ def test_url_with_no_action_specified
assert_equal ['/', {}], rs.generate(:controller => 'content', :action => 'index')
assert_equal ['/', {}], rs.generate(:controller => 'content')
end
+
+ def test_named_url_with_no_action_specified
+ rs.draw do
+ rs.root '', :controller => 'content'
+ rs.connect ':controller/:action/:id'
+ end
+
+ assert_equal ['/', {}], rs.generate(:controller => 'content', :action => 'index')
+ assert_equal ['/', {}], rs.generate(:controller => 'content')
+
+ x = setup_for_named_route
+ assert_equal({:controller => '/content', :action => 'index'},
+ x.new.send(:root_url))
+ end
+
+ def test_url_generated_when_forgetting_action
+ [{:controller => 'content', :action => 'index'}, {:controller => 'content'}].each do |hash|
+ rs.draw do
+ rs.root '', hash
+ rs.connect ':controller/:action/:id'
+ end
+ assert_equal ['/', {}], rs.generate({:action => nil}, {:controller => 'content', :action => 'hello'})
+ assert_equal ['/', {}], rs.generate({:controller => 'content'})
+ assert_equal ['/content/hi', {}], rs.generate({:controller => 'content', :action => 'hi'})
+ end
+ end
+
end
end

0 comments on commit 6ed16ff

Please sign in to comment.
Something went wrong with that request. Please try again.