【发布时间】:2009-04-12 12:36:34
【问题描述】:
最近我在我的一个应用程序中更改了一些嵌套资源以使用浅层路由。它工作得很好,我已经能够简化我的视图和控制器。
不过,我之前一直在使用 path_prefix:
map.with_options :path_prefix => "blog" do |blog|
blog.resources :posts do |posts|
posts.resources :comments
end
end
请注意,所有路由都按预期以“/blog”为前缀。
# $ rake routes
# posts GET /blog/posts(.:format) {:controller=>"posts", :action=>"index"}
# POST /blog/posts(.:format) {:controller=>"posts", :action=>"create"}
# new_post GET /blog/posts/new(.:format) {:controller=>"posts", :action=>"new"}
# edit_post GET /blog/posts/:id/edit(.:format) {:controller=>"posts", :action=>"edit"}
# post GET /blog/posts/:id(.:format) {:controller=>"posts", :action=>"show"}
# PUT /blog/posts/:id(.:format) {:controller=>"posts", :action=>"update"}
# DELETE /blog/posts/:id(.:format) {:controller=>"posts", :action=>"destroy"}
# post_comments GET /blog/posts/:post_id/comments(.:format) {:controller=>"comments", :action=>"index"}
# POST /blog/posts/:post_id/comments(.:format) {:controller=>"comments", :action=>"create"}
# new_post_comment GET /blog/posts/:post_id/comments/new(.:format) {:controller=>"comments", :action=>"new"}
# edit_post_comment GET /blog/posts/:post_id/comments/:id/edit(.:format) {:controller=>"comments", :action=>"edit"}
# post_comment GET /blog/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"show"}
# PUT /blog/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"update"}
# DELETE /blog/posts/:post_id/comments/:id(.:format) {:controller=>"comments", :action=>"destroy"}
新的路由配置如下所示:
map.with_options :path_prefix => "blog", :shallow => true do |blog|
blog.resources :posts do |posts|
posts.resources :comments
end
end
现在,我的一些路由中缺少“/blog”前缀。
# $ rake routes
# posts GET /blog/posts(.:format) {:controller=>"posts", :action=>"index"}
# POST /blog/posts(.:format) {:controller=>"posts", :action=>"create"}
# new_post GET /blog/posts/new(.:format) {:controller=>"posts", :action=>"new"}
# edit_post GET /posts/:id/edit(.:format) {:controller=>"posts", :action=>"edit"}
# post GET /posts/:id(.:format) {:controller=>"posts", :action=>"show"}
# PUT /posts/:id(.:format) {:controller=>"posts", :action=>"update"}
# DELETE /posts/:id(.:format) {:controller=>"posts", :action=>"destroy"}
# post_comments GET /posts/:post_id/comments(.:format) {:controller=>"comments", :action=>"index"}
# POST /posts/:post_id/comments(.:format) {:controller=>"comments", :action=>"create"}
# new_post_comment GET /posts/:post_id/comments/new(.:format) {:controller=>"comments", :action=>"new"}
# edit_comment GET /comments/:id/edit(.:format) {:controller=>"comments", :action=>"edit"}
# comment GET /comments/:id(.:format) {:controller=>"comments", :action=>"show"}
# PUT /comments/:id(.:format) {:controller=>"comments", :action=>"update"}
# DELETE /comments/:id(.:format) {:controller=>"comments", :action=>"destroy"}
我正在寻找一种解决方案来取回所有路由的前缀。我知道它正在使用命名空间 (map.namespace :blog do),但我想防止重构我的所有控制器/视图/测试以实际使用命名空间。
所有代码示例都使用 Rails 2.3.2 版和 Ruby 1.8.7 进行了测试。
【问题讨论】:
-
嗨,克里斯托夫,如果我的回答还不够,我可能会提供更多帮助,但您能否说明一下您正在寻找的最终结果以及您对理想解决方案的任何标准?您尝试通过进行此更改来解决的原始测试问题是什么?
-
其实很简单:在我的示例中,我正在尝试使用 static 路径前缀(例如“/blog”)创建浅层路由。
-
Raimonds 的回答是否符合您的要求?如果没有,您正在寻找的具体路线是什么?
-
是的,它正在做我想做的事。
-
感谢您的信息,结果我更新了我的答案。
标签: ruby-on-rails ruby