问题
When defining the following route in routes.rb
:
resources :streams
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)
I would like to have an explicit resource id, i.e. :stream_id
instead of :id
.
edit:
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)
but on the other hand for the parent resource (again, :id
instead of :stream_id
):
streams GET streams/:id(.:format)
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??
回答1:
I'm not sure if this is what you are looking for but, in your routes.rb
,
If you use
resources :streams, param: :stream_id
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
Hope this helps
UPDATE:
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
Rails would 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
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
Hope this helps
回答2:
Try
resources :streams, :param => :stream_id
UPDATE:
What happens when you do this?
resources :streams, :param => :stream_id do
resource :comment, :param => :comment_id
end
回答3:
Similar to the post above but without the []: I used:
resources :tickets, param: :ticket_id
resources :tickets do
resources :entries, param: :entry_id
end
来源:https://stackoverflow.com/questions/22081845/rails-4-explicit-model-name-for-resource-id-route