grape icon indicating copy to clipboard operation
grape copied to clipboard

Nested Resources

Open daddz opened this issue 15 years ago • 3 comments

Is there a way to have nested resources to get routes like this:

 foo id        bar id
      |        |
/foo/:id/bars/:id

Something along those lines:

resource :foo do
  resource :bar do
    get '/:id' do
       #...this obviously misses the foo id

That would make it easy to map (model) relations to the API.

daddz avatar Aug 15 '11 14:08 daddz

I don't believe this is currently supported. It looks like resource is just an alias to namespace: https://github.com/intridea/grape/blob/master/lib/grape/api.rb#L214

bmorton avatar Aug 25 '11 19:08 bmorton

@bmorton is correct. The proper way to do this in grape is simply to declare a namespace (or use the just-committed segment alias):

resource :foo
  segment '/:foo_id' do
    resource '/bar' do
      get '/:id' do
        # this has :foo_id and :id set
      end
    end
  end
end

mbleigh avatar Sep 02 '11 17:09 mbleigh

@mbleigh could you point out where exactly the code that supports this type of nesting?

zotherstupidguy avatar Feb 26 '15 09:02 zotherstupidguy