Denny
Denny

Reputation: 1230

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!

Upvotes: 5

Views: 557

Answers (2)

Felix
Felix

Reputation: 801

Specify :on => :member, otherwise it is acting as a nested resource.

resources :posts do
    get 'test', :on => :member
end

Upvotes: 7

d11wtq
d11wtq

Reputation: 35318

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.

Upvotes: 1

Related Questions