Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Force given path to http methods in mapper to skip canonical action c…
…hecking

This fixes the following scenario:

    resources :contacts do
      post 'new', action: 'new', on: :collection, as: :new
    end

Where the /new path is not generated because it's considered a canonical
action, part of the normal resource actions:

    new_contacts POST   /contacts(.:format)          contacts#new

Fixes #2999
  • Loading branch information
carlosantoniodasilva committed May 4, 2012
1 parent 319db7b commit d03aa10
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
5 changes: 3 additions & 2 deletions actionpack/lib/action_dispatch/routing/mapper.rb
Expand Up @@ -535,7 +535,8 @@ def delete(*args, &block)
private private
def map_method(method, args, &block) def map_method(method, args, &block)
options = args.extract_options! options = args.extract_options!
options[:via] = method options[:via] = method
options[:path] ||= args.first if args.first.is_a?(String)
match(*args, options, &block) match(*args, options, &block)
self self
end end
Expand Down Expand Up @@ -1509,7 +1510,7 @@ def path_for_action(action, path) #:nodoc:
prefix = shallow_scoping? ? prefix = shallow_scoping? ?
"#{@scope[:shallow_path]}/#{parent_resource.path}/:id" : @scope[:path] "#{@scope[:shallow_path]}/#{parent_resource.path}/:id" : @scope[:path]


path = if canonical_action?(action, path.blank?) if canonical_action?(action, path.blank?)
prefix.to_s prefix.to_s
else else
"#{prefix}/#{action_path(action, path)}" "#{prefix}/#{action_path(action, path)}"
Expand Down
4 changes: 2 additions & 2 deletions actionpack/test/controller/routing_test.rb
Expand Up @@ -1405,7 +1405,7 @@ def test_route_constraints_with_supported_options_must_not_error
end end
end end
end end

def test_route_with_subdomain_and_constraints_must_receive_params def test_route_with_subdomain_and_constraints_must_receive_params
name_param = nil name_param = nil
set.draw do set.draw do
Expand All @@ -1418,7 +1418,7 @@ def test_route_with_subdomain_and_constraints_must_receive_params
set.recognize_path('http://subdomain.example.org/page/mypage')) set.recognize_path('http://subdomain.example.org/page/mypage'))
assert_equal(name_param, 'mypage') assert_equal(name_param, 'mypage')
end end

def test_route_requirement_recognize_with_ignore_case def test_route_requirement_recognize_with_ignore_case
set.draw do set.draw do
get 'page/:name' => 'pages#show', get 'page/:name' => 'pages#show',
Expand Down
9 changes: 8 additions & 1 deletion actionpack/test/dispatch/routing_test.rb
Expand Up @@ -171,6 +171,8 @@ def self.call(params, request)
post :preview, :on => :collection post :preview, :on => :collection
end end
end end

post 'new', :action => 'new', :on => :collection, :as => :new
end end


resources :replies do resources :replies do
Expand Down Expand Up @@ -876,6 +878,12 @@ def test_projects
assert_equal '/projects/1/edit', edit_project_path(:id => '1') assert_equal '/projects/1/edit', edit_project_path(:id => '1')
end end


def test_projects_with_post_action_and_new_path_on_collection
post '/projects/new'
assert_equal "project#new", @response.body
assert_equal "/projects/new", new_projects_path
end

def test_projects_involvements def test_projects_involvements
get '/projects/1/involvements' get '/projects/1/involvements'
assert_equal 'involvements#index', @response.body assert_equal 'involvements#index', @response.body
Expand Down Expand Up @@ -2450,7 +2458,6 @@ def app; Routes end
get "/foo/bar/baz" get "/foo/bar/baz"
assert_equal "/pooh", @response.body assert_equal "/pooh", @response.body
end end

end end


class TestTildeAndMinusPaths < ActionDispatch::IntegrationTest class TestTildeAndMinusPaths < ActionDispatch::IntegrationTest
Expand Down

0 comments on commit d03aa10

Please sign in to comment.