[英]Static nested resource params name on Rails routes
I'm playing on Rails routing but I can't figure out how to manage this. 我在玩Rails路由,但不知道如何管理它。 The problem is at
user_posts(1)
and user(4)
routes, the params have different names - id
and user_id
问题出在
user_posts(1)
和user(4)
路由上,参数具有不同的名称id
和user_id
What I'm trying to achieve is a static param name for the same resource. 我想要实现的是针对同一资源的静态参数名称。
I have this route file 我有这个路线文件
Rails.application.routes.draw do
shallow do
resources :users, module: :users, only: [:index, :show] do
resources :posts, module: :posts, only: [:index, :show]
end
end
end
The routes generated are 生成的路线是
user_posts(1) GET /users/:user_id/posts(.:format) users/posts/posts#index
post(2) GET /posts/:id(.:format) users/posts/posts#show
users(3) GET /users(.:format) users/users#index
user(4) GET /users/:id(.:format) users/users#show
I tried resources :user param: :user
but the generated route at user_posts
is /users/:user_user_id/posts
我尝试了
resources :user param: :user
但在user_posts
生成的路由是/users/:user_user_id/posts
Is possible to achieve a :user_id
param and :post_id
param for every route using resources
? 是否可以使用
resources
为每条路线实现:user_id
参数和:post_id
参数?
Try this 尝试这个
resources :users, param: :user_id
resources :users, only: [] do
resources :posts, param: :post_id
end
It is not possible 'cause it is useless. 这是不可能的,因为它没有用。 Your only option is to nest post into the user resources in such manner:
您唯一的选择是以这种方式将帖子嵌套到用户资源中:
resources :users, only: [:index, :show] do
resources :posts, only: [:index, :show]
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.