Rails 4 [Best practices] Nested resources and shallow: true

后端 未结 5 1581
独厮守ぢ
独厮守ぢ 2020-12-28 15:44

The following post is based on Rails 4.

I am currently looking for a best-practice about the multiple nested resources (more than 1), and the option shallow: true.

5条回答
  •  太阳男子
    2020-12-28 15:53

    Since there's an id for a Collection, it's redundant to nest the route under the Project except for the index and create actions.

    There's a rule about URL's where there's only supposed to be one URL to GET (with 200) a given resource, if there are other URL's you should redirect to it. So you might have a route /projects/:id/collections/:collection_id that redirects to /collections/:collection_id.

    In your case, a Collection is tied to a Project, but that's not necessarily true for all relationships. Once you have the :collection_id you don't need to reference the context of the Project to access it.

提交回复
热议问题