路径根级别的嵌套资源
我正在尝试创建一个带有 url 方案的嵌套资源,其格式为:“http://example.com/username/...
”。
我目前拥有的是这样的:
ActionController::Routing::Routes.draw do |map|
map.home '/', :controller => 'home'
map.resource :session
map.resources :users, :has_many => :nodes
#map.user '/:id', :controller => 'users', :action => 'show', :has_many => :nodes
map.resources :nodes, :belongs_to => :user
end
这会产生如下 URL:
http://example.local/users/username
http://example.local/users/username/nodes
如何避免“用户”前缀超出了我的范围。将“as: => ''
”选项传递给 map.resources
不起作用,并且命名路由似乎不支持“:has_many”
”或“:belongs_to
”选项。
注释掉“map.resources :users
”并在它看起来有效后取消注释“map.user
”行......直到到达嵌套资源。然后它会抛出以下错误:
undefined method `user_nodes_path' for #<ActionView::Base:0x1052c8d18>
我知道这个问题以前已经出现过很多次了,并且总是会遇到“你为什么要这样做?”回应。坦白说,Twitter 做到了,Facebook 做到了,我也想这么做! ;-D
至于如何避免用户名与内置路径冲突的常见批评,我已将最小用户名长度设置为 6 个字符,并计划使所有内置根级路径段路径长度为 5 个字符或更短(即“/opt/...
”用于选项,“/in/...
”用于会话登录等)。
I'm trying to create a nested resource with a url scheme along the lines of: "http://example.com/username/...
".
What I currently have is this:
ActionController::Routing::Routes.draw do |map|
map.home '/', :controller => 'home'
map.resource :session
map.resources :users, :has_many => :nodes
#map.user '/:id', :controller => 'users', :action => 'show', :has_many => :nodes
map.resources :nodes, :belongs_to => :user
end
This results in URLs like:
http://example.local/users/username
http://example.local/users/username/nodes
How to avoid the "users" prefix is beyond me. Passing a "as: => ''
" option to map.resources
doesn't work and it seems named routes don't support the ":has_many
" or ":belongs_to
" options.
Commenting out the "map.resources :users
" and uncommmenting the "map.user
" line after it seems to work… until you reach a nested resource. Then it spits out the following error:
undefined method `user_nodes_path' for #<ActionView::Base:0x1052c8d18>
I know this problem has come up plenty of times before and is always met with "Why would you want to do that?" responses. Frankly, Twitter does it, Facebook does it, and I want to do it too! ;-D
As for the common criticism of how to avoid usernames from conflicting built-in paths, I've set my minimum username length to 6 characters and plan to make all built-in root-level paths segments paths 5 characters or shorter (i.e. "/opt/...
" for options, "/in/...
" for session log-in, etc.).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本问题所述:
Rails 资源路由中的默认段名称
如 应该能够使用这个插件:
http://github.com/caring/default_routing
或者,你可以使用类似 That 或类似的东西手动指定它们,
它应该可以从
map.resources
中为您提供所需的内容。命名路由支持 has_many,它用于向 URL 路径添加另一个资源级别。例如,
生成所有
users_path
和user_nodes_path
路由,例如/users
、/users/:id
、/users/:id/nodes
和/users/:id/nodes/:id
。它等同于那是因为
map.resources :users, :has_many => :nodes
生成这些路由。map.user '/:id', :controller => 仅生成一个命名路由'用户', :action => 'show'
这就是user_path
。As noted in this question:
Default segment name in rails resources routing
You should be able to use this plugin:
http://github.com/caring/default_routing
Alternatively, you could specify them manually with something like
That or something like it should give you what you want from
map.resources
.Named routes supports has_many, which is used to add another resource level to the URL path. For example
generates all the
users_path
anduser_nodes_path
routes like/users
,/users/:id
,/users/:id/nodes
and/users/:id/nodes/:id
. It's identical toThat's because
map.resources :users, :has_many => :nodes
generates these routes. Only one named route is generated bymap.user '/:id', :controller => 'users', :action => 'show'
and that'suser_path
.请参阅此相关问题中的答案
基本上,您可以通过
:path =>; ''
作为否定标识段的选项(仅在 Rails 3 中测试)请注意,这可能会与其他路由模式发生冲突,因此请小心放置它的位置和方式。See my answer in this related question
Basically, you can pass
:path => ''
as an option to negate the identifying segment (Tested in Rails 3 only) Just be aware that this may clash with other routing patterns, so be careful where and how you place it.