Rails 4-资源ID路由的显式模型名称 [英] Rails 4 - explicit model name for resource id route
问题描述
在 routes.rb
中定义以下路由时:
resources :streams
Rails生成以下网址:
Rails generates the following urls:
streams GET /streams(.:format)
POST /streams(.:format)
new_stream GET /streams/new(.:format)
edit_stream GET /streams/:id/edit(.:format)
stream GET /streams/:id(.:format)
PATCH /streams/:id(.:format)
PUT /streams/:id(.:format)
DELETE /streams/:id(.:format)
我想有一个明确的资源ID,即:stream_id
而不是:id
。
I would like to have an explicit resource id, i.e. :stream_id
instead of :id
.
编辑:
对于简单资源,解决方案类似于@ user2262149和@ vimsha提到了:
For simple resources the solution is like @user2262149 and @vimsha mentioned:
resources :streams, :param => :stream_id
问题出在嵌套资源上。如果我这样做:
The problem is with nested resources. If I do this:
resources :streams do
resource :comment, :param => :comment_id
end
我会得到这条路线(没关系):
I will get this route (which is ok):
stream_comments GET streams/:stream_id/comments(.:format)
,但另一方面是父资源(同样,:id
而不是:stream_id
):
but on the other hand for the parent resource (again, :id
instead of :stream_id
):
streams GET streams/:id(.:format)
所以...。
如果我尝试解决它,请添加:param => :stream_id
到父资源:
So....
If I try to solve it adding :param => :stream_id
to the parent resource:
resources :streams, :param => :stream_id do
resource :comment, :param => :comment_id
end
然后为父资源确定路由:
Then for the parent resource the route is ok:
stream GET /api/streams/:stream_id(.:format)
但是我对子资源真是一团糟:
but I get a real mess for the child resource:
stream_comments GET /api/streams/:stream_stream_id/comments(.:format)
您是否知道如何解决这个问题?
Do you have an idea how to solve this problem??
推荐答案
我不确定这是否是您要查找的内容,但是在您的 routes.rb
,
I'm not sure if this is what you are looking for but, in your routes.rb
,
如果您使用
resources :streams, param: :stream_id
Rails将生成以下URL:
Rails will generate the following urls:
streams GET /streams(.:format) streams#index
POST /streams(.:format) streams#create
new_stream GET /streams/new(.:format) streams#new
edit_stream GET /streams/:stream_id/edit(.:format) streams#edit
stream GET /streams/:stream_id(.:format) streams#show
PATCH /streams/:stream_id(.:format) streams#update
PUT /streams/:stream_id(.:format) streams#update
DELETE /streams/:stream_id(.:format) streams#destroy
希望这会有所帮助
更新:
我不确定这是否是最佳做法,或者是否有更好的方法,但是如果您尝试尝试怎么做:
I'm not sure if this is best practices or not or if there is a better way but what if you try:
resources :streams, param: :stream_id
resources :streams, only: [] do
resource :comment, param: :comment_id
end
轨道会生成以下网址:
streams GET /streams(.:format) streams#index
POST /streams(.:format) streams#create
new_stream GET /streams/new(.:format) streams#new
edit_stream GET /streams/:stream_id/edit(.:format) streams#edit
stream GET /streams/:stream_id(.:format) streams#show
PATCH /streams/:stream_id(.:format) streams#update
PUT /streams/:stream_id(.:format) streams#update
DELETE /streams/:stream_id(.:format) streams#destroy
stream_comment POST /streams/:stream_id/comment(.:format) comments#create
new_stream_comment GET /streams/:stream_id/comment/new(.:format) comments#new
edit_stream_comment GET /streams/:stream_id/comment/edit(.:format) comments#edit
GET /streams/:stream_id/comment(.:format) comments#show
PATCH /streams/:stream_id/comment(.:format) comments#update
PUT /streams/:stream_id/comment(.:format) comments#update
DELETE /streams/:stream_id/comment(.:format) comments#destroy
希望这会有所帮助
这篇关于Rails 4-资源ID路由的显式模型名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!