RESTfully Nesting Resource Routes with Single Identifiers

前端 未结 4 1908
情话喂你
情话喂你 2021-02-06 04:47

In my Rails app I have a fairly standard has_many relationship between two entities. A Foo has zero or more Bars; a Bar belongs to exactly

4条回答
  •  情话喂你
    2021-02-06 05:16

    As you already know, every item has to have a unique address. I would suggest that the shorter and easier the address is, the better for everyone involved - your routing code, your client apps, etc. If you can identify a Bar by its unique ID, I would probably specify only that in the URL.

    You don't need to lose the semantic information about the Bar's assignment to a Foo, though. That can be part of the representation.

    As an enhancement, you could allow the client to address Bar 200 as /foo/100/bar/200, but then redirect to the preferred /bar/200/ address, using, e.g., a 303 ("See Other") response.

提交回复
热议问题