Scroll to top
【Ruby on Rails】Sử dụng tùy chọn shallow trong file routes

【Ruby on Rails】Sử dụng tùy chọn shallow trong file routes

ByAdmin 2022-12-08 15:05
3 min read

Sử dụng tùy chọn shallow trong file routes. Khi sử dụng nest resources, khai báo tùy chọn shallow trong routes sẽ làm các URL ngắn gọn dễ nhìn hơn.

Khi sử dụng nest resources, khai báo tùy chọn shallow trong routes sẽ làm các URL ngắn gọn dễ nhìn hơn.

Giả sử một application có model Category, mỗi Category bao gồm nhiều Article. Trong trường hợp này thì thường khai báo resources là:

Copy ruby
1
2
3
resources :categories do
  resources :articles
end

Lúc này URL để tạo Article mới sẽ là /categories/:category_id/articles/new
Tuy nhiên nếu như vậy, thì sau khi tạo Article xong và vào action #show của Article này, thì URL sẽ trở thành /categories/:category_id/articles/:id
Có vẻ hơi dư thừa, chỉ cần /articles/:id thôi cũng được rồi

Do đó, khai báo thêm tùy chọn shallow

Copy ruby
1
2
3
resources :categories, shallow: true do
  resources :articles
end

Lúc này routes sẽ là:

Copy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
        category_articles GET    /categories/:category_id/articles(.:format)                                              articles#index
                          POST   /categories/:category_id/articles(.:format)                                              articles#create
     new_category_article GET    /categories/:category_id/articles/new(.:format)                                          articles#new
             edit_article GET    /articles/:id/edit(.:format)                                                             articles#edit
                  article GET    /articles/:id(.:format)                                                                  articles#show
                          PATCH  /articles/:id(.:format)                                                                  articles#update
                          PUT    /articles/:id(.:format)                                                                  articles#update
                          DELETE /articles/:id(.:format)                                                                  articles#destroy
               categories GET    /categories(.:format)                                                                    categories#index
                          POST   /categories(.:format)                                                                    categories#create
             new_category GET    /categories/new(.:format)                                                                categories#new
            edit_category GET    /categories/:id/edit(.:format)                                                           categories#edit
                 category GET    /categories/:id(.:format)                                                                categories#show
                          PATCH  /categories/:id(.:format)                                                                categories#update
                          PUT    /categories/:id(.:format)                                                                categories#update
                          DELETE /categories/:id(.:format)                                                                categories#destroy
Ở 3 action #index, #new, #create, thì chưa có chỉ định article_id, do đó category_id thì cần thiết, ngoài 3 action đó ra thì chỉ cần chỉ định article_id thôi là được rồi.

Trong trường hợp thêm các action:

Copy config/routes.rb
1
2
3
4
5
6
resources :categories, shallow: true do
  resources :articles do
    get :search, on: :collection
    post :like, on: :member
  end
end

Lúc này thì:

Copy
1
2
 search_category_articles GET    /categories/:category_id/articles/search(.:format)                                       articles#search
             like_article POST   /articles/:id/like(.:format)                                                             articles#like
Như chúng ta thấy ở trên, chỉ những action nào chưa có chỉ định article_id thì mới cần thiết đến category_id.

Đối với trường hợp nest resources 2 cấp, thì cũng chỉ cần tùy chọn 1 lần shallow: true thôi là đủ.

Copy ruby
1
2
3
4
5
6
7
resources :categories, shallow: true do
  resources :articles do
    get :search, on: :collection
    post :like, on: :member
    resources :comments
  end
end

Lúc này thì comments#new sẽ là /articles/:article_id/comments/new, comments#show sẽ là /comments/:id

Đánh giá bài viết: 5/5 (15 đánh giá)
Bạn chưa đánh giá

Bình luận

Author
hoclaptrinh.io author
Tác giả:Yuto Yasunaga

Mình là một full stack developer, tốt nghiệp và làm việc tại Nhật Bản. Trang web này là nơi mình tổng hợp, đúc kết và lưu trữ lại những kiến thức trong quá trình học và làm việc liên quan đến IT.
Hy vọng những bài viết ở website này sẽ có ích cho bạn.