Вложенные ресурсы и Shallow в Rails routes - Evanto/qna GitHub Wiki

Rails Nested Routes (Вложенные ресурсы)

Ссылки по вложенным ресурсам:
Rails Routing from the Outside In ctrl+f 'nested' --> 2.7 Nested Resources
Rusrails: Роутинг в Rails ctrl+f 'вложенные' --> 2.7. Вложенные ресурсы
Всё, что нужно знать о routes, params и формах в Rails

  • Вложенные ресурсы используются, когда надо вывести ресурсы объекта (одного или нескольких), принадлежащего родителю.

Shallow Nesting (Использование опции shallow с вложенными ресурсами на примере проекта qna)

Ссылки по Shallow:
Rails 5.1.1 ActionDispatch::Routing::Mapper::Resources ctrl+f 'shallow'
Вложенный route вида /:id/:id без указания моделей

  • Без shallow (config/routes.rb):
Rails.application.routes.draw do
  resources :questions do
    resources :answers
  end
end
ubuntu@rails-dev-box:~/www/qna$ rake routes
              Prefix Verb   URI Pattern                                        Controller#Action
    question_answers GET    /questions/:question_id/answers(.:format)          answers#index
                     POST   /questions/:question_id/answers(.:format)          answers#create
 new_question_answer GET    /questions/:question_id/answers/new(.:format)      answers#new
edit_question_answer GET    /questions/:question_id/answers/:id/edit(.:format) answers#edit
     question_answer GET    /questions/:question_id/answers/:id(.:format)      answers#show
                     PATCH  /questions/:question_id/answers/:id(.:format)      answers#update
                     PUT    /questions/:question_id/answers/:id(.:format)      answers#update
                     DELETE /questions/:question_id/answers/:id(.:format)      answers#destroy
           questions GET    /questions(.:format)                               questions#index
                     POST   /questions(.:format)                               questions#create
        new_question GET    /questions/new(.:format)                           questions#new
       edit_question GET    /questions/:id/edit(.:format)                      questions#edit
            question GET    /questions/:id(.:format)                           questions#show
                     PATCH  /questions/:id(.:format)                           questions#update
                     PUT    /questions/:id(.:format)                           questions#update
                     DELETE /questions/:id(.:format)                           questions#destroy

  • С опцией shallow:
Rails.application.routes.draw do
  resources :questions, shallow: true do
    resources :answers
  end
end
ubuntu@rails-dev-box:~/www/qna$ rake routes
             Prefix Verb   URI Pattern                                   Controller#Action
   question_answers GET    /questions/:question_id/answers(.:format)     answers#index
                    POST   /questions/:question_id/answers(.:format)     answers#create
new_question_answer GET    /questions/:question_id/answers/new(.:format) answers#new
        edit_answer GET    /answers/:id/edit(.:format)                   answers#edit
             answer GET    /answers/:id(.:format)                        answers#show
                    PATCH  /answers/:id(.:format)                        answers#update
                    PUT    /answers/:id(.:format)                        answers#update
                    DELETE /answers/:id(.:format)                        answers#destroy
          questions GET    /questions(.:format)                          questions#index
                    POST   /questions(.:format)                          questions#create
       new_question GET    /questions/new(.:format)                      questions#new
      edit_question GET    /questions/:id/edit(.:format)                 questions#edit
           question GET    /questions/:id(.:format)                      questions#show
                    PATCH  /questions/:id(.:format)                      questions#update
                    PUT    /questions/:id(.:format)                      questions#update
                    DELETE /questions/:id(.:format)                      questions#destroy

Как видим, роуты с shallow отличаются только более короткими урлами и хелперами 4х экшенов (edit, show, update, destroy) - из них выкидывается все, что относится к родительскому ресурсу (в данном случае, questions):

  • Без Shallow (edit, show, update, destroy):
edit_question_answer GET    /questions/:question_id/answers/:id/edit(.:format) answers#edit
     question_answer GET    /questions/:question_id/answers/:id(.:format)      answers#show
                     PATCH  /questions/:question_id/answers/:id(.:format)      answers#update
                     PUT    /questions/:question_id/answers/:id(.:format)      answers#update
                     DELETE /questions/:question_id/answers/:id(.:format)      answers#destroy
  • C Shallow (edit, show, update, destroy):
        edit_answer GET    /answers/:id/edit(.:format)                   answers#edit
             answer GET    /answers/:id(.:format)                        answers#show
                    PATCH  /answers/:id(.:format)                        answers#update
                    PUT    /answers/:id(.:format)                        answers#update
                    DELETE /answers/:id(.:format)                        answers#destroy

⚠️ **GitHub.com Fallback** ⚠️