【问题标题】:Nested Resource at Root Level of Path路径根级别的嵌套资源
【发布时间】:2009-09-25 00:29:41
【问题描述】:

我正在尝试使用以下行的 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/...”用于会话登录等)。

【问题讨论】:

    标签: rest ruby-on-rails-2


    【解决方案1】:

    如本问题所述:

    Default segment name in rails resources routing

    你应该可以使用这个插件:

    http://github.com/caring/default_routing

    或者,您可以使用类似的方式手动指定它们

    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”行似乎工作后取消注释......直到您到达嵌套资源。然后它吐出以下错误:

    undefined method `user_nodes_path' for #<ActionView::Base:0x1052c8d18>
    

    那是因为map.resources :users, :has_many =&gt; :nodes 生成了这些路由。 map.user '/:id', :controller =&gt; 'users', :action =&gt; 'show' 仅生成一个命名路由,即user_path

    【讨论】:

    • 太棒了!我很惊讶我之前没有找到 default_routing 插件;它看起来像是任何项目的必备品!感谢您的帮助!
    • 我应该提一下,虽然 default_routing 插件很棒,但它在 Rails 2.3.4(可能还有早期版本)中也被严重破坏了。不过,github.com/slippyd/default_routing 有一个固定版本。
    • 感谢您的链接,Slippy,您的版本非常适合我!
    【解决方案2】:

    this related question查看我的回答

    基本上,您可以传递:path =&gt; '' 作为选项来否定标识段(仅在 Rails 3 中测试)请注意,这可能与其他路由模式发生冲突,因此请注意放置它的位置和方式。

    【讨论】:

    • 这似乎是 Rails 3.x 已经消除的问题(和修复)。当我将我需要的应用程序从 Rails 2.3.x 更新到 3.x 时,我必须查看你的解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 1970-01-01
    相关资源
    最近更新 更多