Muhammad Mullah
Muhammad Mullah

Reputation: 99

Does Phoenix Framework allow nested resource inside another nested resource?

Can I have such a thing in Phoenix as long as nested resource is concerned

resources "/landlords", LandlordController do
  resources "/houses", HouseController do
    resources "/units", UnitController
  end
end

Where I have landlord has_many houses and house also has_many units. Or any idea how this can be achieved?

Upvotes: 1

Views: 408

Answers (2)

Muhammad Mullah
Muhammad Mullah

Reputation: 99

I was able to make it work by doing this

resources "/landlords", LandlordController do
  resources "/houses", HouseController
end

resources "/houses", HouseController do
 resources "/units", UnitController
end

Upvotes: 0

PatNowak
PatNowak

Reputation: 5812

Yes, it's possible and it's quite common. This is the best to achieve that. Please read a doc to get more information and there's also example how routes look like in such case.

resources "/users", UserController do
  resources "/posts", PostController
end

generate routes

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

Upvotes: 1

Related Questions