4

我的 routes.rb 中有一个如下所示的条目:

resources :baskets, :shallow => true, :only => :show
  resource :owned, :only => [:create, :destroy]
end

get '/baskets/owned', :to => 'owners#index'

这会创建如下所示的路线:

basket        GET    /baskets/:id(.:format)
baskets_owned GET    /baskets/owned(.:format)
# ...

但是现在,当我调用 /baskets/owned 时,我得到了这个:

Started GET "/baskets/owned" for 10.0.1.2 at 2011-06-09 10:31:33 -0400
  Processing by BasketsController#show as JSON
  Parameters: {"id"=>"owned"}

解决此路由冲突的最惯用方法是什么?

4

1 回答 1

7

Rails 匹配从第一个定义的路由开始,一直向下直到找到匹配项;使用第一个匹配的路由并忽略所有其他路由。

解决问题的方法是将路线放在get '/baskets/owned'路线之前resources

于 2011-06-09T15:07:45.867 回答