Rails3 中的嵌套资源,没有路由匹配
我在 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"} 或类似(带有: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"}
I have a nested route in rails 3 mapped as
resources :maps do
resource :versions
member do
post 'share'
get 'share'
end
end
but when i try to connect to http://localhost:3000/maps/35/versions/2 i obtains
No route matches "/maps/35/versions/2"
and in rake routes
GET /maps/:map_id/versions/:id(.:format) {:controller=>"versions", :action=>"show"}
or similar (with :id) is missing
other routes with versions works correctly
what's wrong?
EDIT 2:
This is the full rake routes output
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"}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它必须是
资源:版本
。请注意您的案例中缺少的“s”。It has to be
resources :versions
. Note the missing "s" in your case.除了@Femaref的答案之外,您需要访问的网址是/maps/35/versions/2。如果您想要单一(单例)资源,那么您可以这样做:
然后点击 /maps/35/version (不带 id)。如果每个地图都有多个版本,您可能不想这样做。
In addition to @Femaref's answer, the url you need to access is /maps/35/versions/2. If you want the singular (singleton) resource, then you'd do:
And then hit /maps/35/version (doesn't take an id). Which, if you have multiple versions for each map, you probably don't want to do.