Nested resources biểu diễn quan hệ cha-con trong URL: /posts/:post_id/comments — comment luôn thuộc về 1 post cụ thể.
# config/routes.rb
resources :posts do
resources :comments, only: [:index, :create, :destroy]
endRoute được tạo: GET /posts/:post_id/comments, POST /posts/:post_id/comments, ...
Trong CommentsController, lấy post qua Post.find(params[:post_id]) rồi build comment từ @post.comments.
Best practice: chỉ nest 1 cấp — /posts/:post_id/comments OK, nhưng nest sâu hơn khó đọc và khó maintain.
Nested resources express parent-child relationships in URLs: /posts/:post_id/comments — a comment always belongs to a specific post.
# config/routes.rb
resources :posts do
resources :comments, only: [:index, :create, :destroy]
endGenerated routes: GET /posts/:post_id/comments, POST /posts/:post_id/comments, etc.
In CommentsController, load the parent via Post.find(params[:post_id]) then build the child with @post.comments.build(...).
Best practice: only nest one level — /posts/:post_id/comments is fine, but going deeper is hard to read and maintain.