Phoenix Framework 是否允许在另一个嵌套资源中嵌套资源?
Does Phoenix Framework allow nested resource inside another nested resource?
只要是嵌套资源,凤凰能不能有这样的东西
resources "/landlords", LandlordController do
resources "/houses", HouseController do
resources "/units", UnitController
end
end
房东那里我有has_many房,房子也有has_many套。或者知道如何实现这一点?
是的,这是可能的,而且很常见。这是实现这一目标的最佳方法。请阅读 doc 以获取更多信息,还有在这种情况下路由的示例。
resources "/users", UserController do
resources "/posts", PostController
end
生成路线
user_post_path GET /users/:user_id/posts PostController :index
user_post_path GET /users/:user_id/posts/:id/edit PostController :edit
user_post_path GET /users/:user_id/posts/new PostController :new
user_post_path GET /users/:user_id/posts/:id PostController :show
user_post_path POST /users/:user_id/posts PostController :create
user_post_path PATCH /users/:user_id/posts/:id PostController :update
PUT /users/:user_id/posts/:id PostController :update
user_post_path DELETE /users/:user_id/posts/:id PostController :delete
我可以通过这样做让它工作
resources "/landlords", LandlordController do
resources "/houses", HouseController
end
resources "/houses", HouseController do
resources "/units", UnitController
end
只要是嵌套资源,凤凰能不能有这样的东西
resources "/landlords", LandlordController do
resources "/houses", HouseController do
resources "/units", UnitController
end
end
房东那里我有has_many房,房子也有has_many套。或者知道如何实现这一点?
是的,这是可能的,而且很常见。这是实现这一目标的最佳方法。请阅读 doc 以获取更多信息,还有在这种情况下路由的示例。
resources "/users", UserController do
resources "/posts", PostController
end
生成路线
user_post_path GET /users/:user_id/posts PostController :index
user_post_path GET /users/:user_id/posts/:id/edit PostController :edit
user_post_path GET /users/:user_id/posts/new PostController :new
user_post_path GET /users/:user_id/posts/:id PostController :show
user_post_path POST /users/:user_id/posts PostController :create
user_post_path PATCH /users/:user_id/posts/:id PostController :update
PUT /users/:user_id/posts/:id PostController :update
user_post_path DELETE /users/:user_id/posts/:id PostController :delete
我可以通过这样做让它工作
resources "/landlords", LandlordController do
resources "/houses", HouseController
end
resources "/houses", HouseController do
resources "/units", UnitController
end