看看凤凰城,是否有相当于shallow
路由的路由,类似于rails如何实现这一点?
这样您就可以引用/posts/1
代替/users/2/posts/1
如,
resources "/users", UserController do
resources "/posts", PostController, shallow: true
end
答案 0 :(得分:5)
看起来不像Phoenix.Router.resources/4
支持:shallow
选项,但这可以完成工作:
resources "/users", UserController do
resources "/posts", PostController, only: [:index, :new, :create]
end
resources "/posts", PostController, except: [:index, :new, :create], as: :user_post
在其他路线mix phoenix.routes
之后,您会看到:
user_post_path GET /users/:user_id/posts YourApp.PostController :index
user_post_path GET /users/:user_id/posts/new YourApp.PostController :new
user_post_path POST /users/:user_id/posts YourApp.PostController :create
user_post_path GET /posts/:id/edit YourApp.PostController :edit
user_post_path GET /posts/:id YourApp.PostController :show
user_post_path PATCH /posts/:id YourApp.PostController :update
PUT /posts/:id YourApp.PostController :update
user_post_path DELETE /posts/:id YourApp.PostController :delete