我的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 /拥有时,我明白了:
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"}
解决此路由冲突的最惯用方法是什么?
答案 0 :(得分:6)
Rails匹配从第一个定义开始到结束的路由,直到找到匹配为止;使用第一个匹配的路由,忽略所有其他路由。
解决问题的方法是将get '/baskets/owned'
路线放在resources
路线前。