Phoenix框架中的浅层嵌套路由

时间:2016-05-30 00:09:44

标签: phoenix-framework

看看凤凰城,是否有相当于shallow路由的路由,类似于rails如何实现这一点?

这样您就可以引用/posts/1代替/users/2/posts/1

如,

resources "/users", UserController do resources "/posts", PostController, shallow: true end

1 个答案:

答案 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