Rails routing: add (shallow) concern to root

2019-07-03 19:26发布

问题:

In my routes.rb, I defined a concern like below:

concern :namespaceable do
  resources :comments do
    resources :replies
  ...

Now, I can add this concern to any resources:

resources :groups, concerns: :namespaceable, shallow: true

But how can I add this concern shallowly to the root, so that I can have following routes

  • /comments
  • /comments/{comment_id}
  • /comments/{comment_id}/replies
  • /replies/{reply_id}

Thanks a lot!

回答1:

Just write concerns: :namespaceable in the main file

Rails.application.routes.draw do

  concern :namespaceable do
    resources :comments do
    resources :replies
    ...
  end

  concerns: :namespaceable # Will add to root namespace

end