开发者

Rails Routing - :resource_id vs :id

开发者 https://www.devze.com 2023-03-15 21:25 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消