Rails Routing - :resource_id vs :id
In my routes.rb:
resources :posts do
get "test"
end
This produces the usual RESTful routes with /post/:id/...
. However, I also get /post/:post_id/test
.
Now my problem开发者_开发问答 is that sometimes the parameter is named :id and sometimes it is :post_id. How can I make it uniform?
Thank you!
Specify :on => :member, otherwise it is acting as a nested resource.
resources :posts do
get 'test', :on => :member
end
You shouldn't make it uniform. It's :id
when it's the target resource and :post_id
when it is the parent of some other target resource (i.e. nested resources). This is a Rails convention.
精彩评论