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??
I'm not sure if this is what you are looking for but, in your
routes.rb
,If you use
Rails will generate the following urls:
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:
Rails would generate the following urls:
Hope this helps
Similar to the post above but without the []: I used:
Try
UPDATE:
What happens when you do this?