9

我希望我的路线命名约定保持一致。默认参数适用:id于任何资源。但是,如果在其中嵌套另一个资源,则父资源的参数将更改为:parent_id. 我的路由看起来像:

resources :users do
  resources :projects do
    resources :issues
  end
end

对于用户模型,它会生成类似的 url :id,对于项目 url 将是:user_id/:id,对于问题 url 将是:user_id/:project_id/:id。我试图在此之后重载默认参数

resources :users, param: :user_id do
  resources :projects, param: :project_id do
    resources :issues, param: :issue_id
  end
end

它生成像这样的 url :user_user_id/:project_project_id/:issue_id:。由于我要覆盖它,因此我希望关闭父资源的默认附加。我希望我的路线看起来像:user_id/:project_id/:issue_id,并且我希望它对于所有模型都是一致的。即项目应映射到:user_id/:project_id.

4

3 回答 3

12

我就是这样做的...不确定是否有更好的方法,但它应该可以解决问题...

resources :users, param: :user_id

resources :users, only: [] do
  resources :projects, param: :project_id
end

resources :users, only: [] do
  resources :projects, only: [] do
    resources :issues, param: :issue_id
 end
end
于 2015-09-19T04:33:23.057 回答
8

尝试member

resources :users, param: :user_id do
  member do
    resources :projects, param: :project_id do
      member do
        resources :issues, param: :issue_id
      end
    end
  end
end

此代码将生成以下路径:

      issues GET    /users/:user_id/projects/:project_id/issues(.:format)                issues#index
             POST   /users/:user_id/projects/:project_id/issues(.:format)                issues#create
   new_issue GET    /users/:user_id/projects/:project_id/issues/new(.:format)            issues#new
  edit_issue GET    /users/:user_id/projects/:project_id/issues/:issue_id/edit(.:format) issues#edit
       issue GET    /users/:user_id/projects/:project_id/issues/:issue_id(.:format)      issues#show
             PATCH  /users/:user_id/projects/:project_id/issues/:issue_id(.:format)      issues#update
             PUT    /users/:user_id/projects/:project_id/issues/:issue_id(.:format)      issues#update
             DELETE /users/:user_id/projects/:project_id/issues/:issue_id(.:format)      issues#destroy
    projects GET    /users/:user_id/projects(.:format)                                   projects#index
             POST   /users/:user_id/projects(.:format)                                   projects#create
 new_project GET    /users/:user_id/projects/new(.:format)                               projects#new
edit_project GET    /users/:user_id/projects/:project_id/edit(.:format)                  projects#edit
     project GET    /users/:user_id/projects/:project_id(.:format)                       projects#show
             PATCH  /users/:user_id/projects/:project_id(.:format)                       projects#update
             PUT    /users/:user_id/projects/:project_id(.:format)                       projects#update
             DELETE /users/:user_id/projects/:project_id(.:format)                       projects#destroy
       users GET    /users(.:format)                                                     users#index
             POST   /users(.:format)                                                     users#create
    new_user GET    /users/new(.:format)                                                 users#new
   edit_user GET    /users/:user_id/edit(.:format)                                       users#edit
        user GET    /users/:user_id(.:format)                                            users#show
             PATCH  /users/:user_id(.:format)                                            users#update
             PUT    /users/:user_id(.:format)                                            users#update
             DELETE /users/:user_id(.:format)                                            users#destroy

但是,请注意,助手与接受的答案不同。

您可以使用“as”选项来避免这种情况。更敏捷的解决方案在这里:https ://stackoverflow.com/a/41323489/2405850

于 2016-12-25T18:40:10.160 回答
-1

在 routes.rb 中尝试这些路线

  resources :users, :key => :user_id do
    resources :projects, :key => :project_id do
      resources :issues, :key => :issue_id
    end
  end
于 2015-08-29T11:39:10.167 回答