路径根级别的嵌套资源

发布于 2024-08-06 07:51:25 字数 1198 浏览 2 评论 0原文

我正在尝试创建一个带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晒暮凉 2024-08-13 07:51:25

本问题所述:

Rails 资源路由中的默认段名称

如 应该能够使用这个插件:

http://github.com/caring/default_routing

或者,你可以使用类似 That 或类似的东西手动指定它们,

map.users     '/users',     :controller => 'users', :action => 'index',
  :conditions => { :method => :get }
map.connect   '/users',     :controller => 'users', :action => 'create',
  :conditions => { :method => :post }
map.user      '/:id',       :controller => 'users', :action => 'show',
  :conditions => { :method => :get }
map.edit_user '/:id/edit',  :controller => 'users', :action => 'edit',
  :conditions => { :method => :get }
map.new_user  '/users/new', :controller => 'users', :action => 'new',
  :conditions => { :method => :get }
map.connect   '/:id', :controller => 'users', :action => 'update',
  :conditions => { :method => :put }
map.connect   '/:id', :controller => 'users', :action => 'destroy',
  :conditions => { :method => :delete }

map.resources :nodes, :path_prefix => '/:user_id', :name_prefix => 'user_'
# to generate user_nodes_path and user_node_path(@node) routes

它应该可以从 map.resources 中为您提供所需的内容。

map.resources 不起作用,并且命名路由似乎不支持“:has_many”或“:belongs_to”选项。

命名路由支持 has_many,它用于向 URL 路径添加另一个资源级别。例如,

map.resources :users, :has_many => :nodes

生成所有 users_pathuser_nodes_path 路由,例如 /users/users/:id/users/:id/nodes/users/:id/nodes/:id。它等同于

map.resources :users do |user|
  user.resources :nodes
end

注释掉“map.resources :users”,并在“map.user”行看起来有效后取消注释......直到到达嵌套资源。然后它会输出以下错误:

# 的未定义方法“user_nodes_path”

那是因为 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

map.users     '/users',     :controller => 'users', :action => 'index',
  :conditions => { :method => :get }
map.connect   '/users',     :controller => 'users', :action => 'create',
  :conditions => { :method => :post }
map.user      '/:id',       :controller => 'users', :action => 'show',
  :conditions => { :method => :get }
map.edit_user '/:id/edit',  :controller => 'users', :action => 'edit',
  :conditions => { :method => :get }
map.new_user  '/users/new', :controller => 'users', :action => 'new',
  :conditions => { :method => :get }
map.connect   '/:id', :controller => 'users', :action => 'update',
  :conditions => { :method => :put }
map.connect   '/:id', :controller => 'users', :action => 'destroy',
  :conditions => { :method => :delete }

map.resources :nodes, :path_prefix => '/:user_id', :name_prefix => 'user_'
# to generate user_nodes_path and user_node_path(@node) routes

That or something like it should give you what you want from map.resources.

map.resources doesn't work and it seems named routes don't support the ":has_many" or ":belongs_to" options.

Named routes supports has_many, which is used to add another resource level to the URL path. For example

map.resources :users, :has_many => :nodes

generates all the users_path and user_nodes_path routes like /users, /users/:id, /users/:id/nodes and /users/:id/nodes/:id. It's identical to

map.resources :users do |user|
  user.resources :nodes
end

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>

That's because map.resources :users, :has_many => :nodes generates these routes. Only one named route is generated by map.user '/:id', :controller => 'users', :action => 'show' and that's user_path.

分开我的手 2024-08-13 07:51:25

请参阅此相关问题中的答案

基本上,您可以通过 :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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文