Reputation: 219
Two quite similar routing settings really is confusing.
resources :authors do
resources :books
end
and
resources :authors do
member do
resources :books
end
end
As we all know, rails will generate the following routings :
writer_book GET /writers/:writer_id/books/:id(.:format) books#show
and
book GET /writers/:id/books/:id(.:format) books#show
How is this member option useful? One can just not using member option and set params[:writer_id] in books_controller and be done with it right? Does this will have a bad affect when the application gets bigger? What are the consequences?
Upvotes: 1
Views: 586
Reputation: 102443
The member
and collection
methods are meant to add additional RESTful actions to resources
resources :writers do
member do
post :favorite
end
collection do
get :unpublished
end
end
They are not intended for nesting resources
# bad
resources :writers do
member do
resources :books
end
end
# good
resources :writers do
resources :books
end
What are the consequences?
Using member here will result in the route
GET /writers/:id/books/:id(.:format)
Which means that the id
param is ambigous! It could be either the id of the book or the author! Not good! Not using member
would give us params[:writer_id]
which we can use to fetch the parent record.
GET /writers/:writer_id/books/:id(.:format) books#show
See:
Upvotes: 2