加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 使用path_prefix的浅路由?

发布时间:2020-12-17 03:57:29 所属栏目:百科 来源:网络整理
导读:最近我在我的一个应用程序中更改了一些嵌套资源以使用浅层路由.它工作得很好,我已经能够简化我的视图和控制器. 但是,我之前一直在使用path_prefix: map.with_options :path_prefix = "blog" do |blog| blog.resources :posts do |posts| posts.resources :c
最近我在我的一个应用程序中更改了一些嵌套资源以使用浅层路由.它工作得很好,我已经能够简化我的视图和控制器.

但是,我之前一直在使用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进行测试.

解决方法

文档似乎表明这种确切的行为是设计的:

:shallow – If true,paths for nested resources which reference a specific member (ie. those with an :id parameter) will not use the parent path prefix or name prefix.

(自http://api.rubyonrails.org/classes/ActionController/Resources.html#M000501起)

由于使用:shallow选项会导致:path_prefix在某些情况下被忽略,如果必须始终具有此前缀,则应考虑删除:shallow选项.这是一个替代解决方案,似乎可以满足您的需求:

map.with_options :path_prefix => "blog" do |blog|
  blog.resources :posts do |posts|
    posts.resources :comments,:only => [:index,:create,:new]
  end
  blog.resources :comments,:except => [:index,:new]
end

导致这些路线:

#             posts GET    /blog/posts                               {:controller=>"posts",:action=>"index"}
#                   POST   /blog/posts                               {:controller=>"posts",:action=>"create"}
#          new_post GET    /blog/posts/new                           {:controller=>"posts",:action=>"new"}
#         edit_post GET    /blog/posts/:id/edit                      {:controller=>"posts",:action=>"edit"}
#              post GET    /blog/posts/:id                           {:controller=>"posts",:action=>"show"}
#                   PUT    /blog/posts/:id                           {:controller=>"posts",:action=>"update"}
#                   DELETE /blog/posts/:id                           {:controller=>"posts",:action=>"destroy"}
#     post_comments GET    /blog/posts/:post_id/comments             {:controller=>"comments",:action=>"index"}
#                   POST   /blog/posts/:post_id/comments             {:controller=>"comments",:action=>"create"}
#  new_post_comment GET    /blog/posts/:post_id/comments/new         {:controller=>"comments",:action=>"new"}
#      edit_comment GET    /blog/comments/:id/edit                   {:controller=>"comments",:action=>"edit"}
#           comment GET    /blog/comments/:id                        {:controller=>"comments",:action=>"show"}
#                   PUT    /blog/comments/:id                        {:controller=>"comments",:action=>"update"}
#                   DELETE /blog/comments/:id                        {:controller=>"comments",:action=>"destroy"}

希望这可以帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读