Rails3中的嵌套资源,没有路由匹配

时间:2011-10-18 21:20:37

标签: ruby-on-rails-3 nested-resources

我在rails 3中有一个嵌套路由,映射为

resources :maps do
    resource :versions
    member do
        post 'share'
        get  'share'            
    end

end

但是当我尝试连接http://localhost:3000/maps/35/versions/2时,我获得了

没有路线匹配“/ maps / 35 / versions / 2”

和rake路线 GET /maps/:map_id/versions/:id(.:format){:controller =>“versions”,:action =>“show”} 或类似(with:id)缺失 其他版本的路由正常工作

出了什么问题?

编辑2: 这是完整的rake路线输出

maps_public GET    /maps/public(.:format)                 {:controller=>"maps", :action=>"public"}
            map_versions POST   /maps/:map_id/versions(.:format)       {:controller=>"versions", :action=>"create"}
        new_map_versions GET    /maps/:map_id/versions/new(.:format)   {:controller=>"versions", :action=>"new"}
       edit_map_versions GET    /maps/:map_id/versions/edit(.:format)  {:controller=>"versions", :action=>"edit"}
                         GET    /maps/:map_id/versions(.:format)       {:controller=>"versions", :action=>"show"}
                         PUT    /maps/:map_id/versions(.:format)       {:controller=>"versions", :action=>"update"}
                         DELETE /maps/:map_id/versions(.:format)       {:controller=>"versions", :action=>"destroy"}
               share_map POST   /maps/:id/share(.:format)              {:controller=>"maps", :action=>"share"}
                         GET    /maps/:id/share(.:format)              {:controller=>"maps", :action=>"share"}
                    maps GET    /maps(.:format)                        {:controller=>"maps", :action=>"index"}
                         POST   /maps(.:format)                        {:controller=>"maps", :action=>"create"}
                 new_map GET    /maps/new(.:format)                    {:controller=>"maps", :action=>"new"}
                edit_map GET    /maps/:id/edit(.:format)               {:controller=>"maps", :action=>"edit"}
                     map GET    /maps/:id(.:format)                    {:controller=>"maps", :action=>"show"}
                         PUT    /maps/:id(.:format)                    {:controller=>"maps", :action=>"update"}
                         DELETE /maps/:id(.:format)                    {:controller=>"maps", :action=>"destroy"}

2 个答案:

答案 0 :(得分:3)

必须是resources :versions。注意你的案例中缺少的“s”。

答案 1 :(得分:1)

除了@ Femaref的回答,你需要访问的网址是/ maps / 35 / versions / 2。如果你想要单数(单例)资源,那么你可以这样做:

resources :maps do
  resource :version
end

然后点击/ maps / 35 /版本(不带id)。如果你有每个地图的多个版本,你可能不想这样做。