2016-05-30 39 views
1

看着Phoenix,是否有相当于shallow的路由,类似于rails如何实现此目的?Phoenix框架中的浅层嵌套路由

这将允许您引用的/posts/1代替/users/2/posts/1

例如,

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

回答

4

看起来不像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