ruby-on-railsroutesdevisenamespacesdoorkeeper

Scope and Namespace routes in Rails


I'm following a tutorial on Dookeeper and Devise gems in Rails, in one point of the video, the author creates the following routes:

namespace :api do
 namespace :v1 do
  resources:books
 end
end

scope :api do
 scope :v1 do
  use doorkeeper do
    skip_controllers:authorizations,:applications,:authorized_applications
   end
  end
end

I don't quite understand what's the point of the namespace and scope in point... They complement each other or are separated things and why do I have to use?

Thanks a lot!


Solution

  • Here's a helpful overview.

    In short (my emphasis added):

    When you use namespace, it will prefix the URL path for the specified resources, and try to locate the controller under a module named in the same manner as the namespace.

    When using scope without any options and only a scope name, it will just change the resources path.

    So scope is useful for making a route match namespace when there aren't controllers with matching names.

    namespace :api do
     namespace :v1 do
      resources:books
     end
    end
    

    Gives you a base route of "/api/v1/books" but requires a Api::V1::BooksController

    scope :api do
     scope :v1 do
      use doorkeeper do
        skip_controllers:authorizations,:applications,:authorized_applications
       end
      end
    end
    

    Gives doorkeeper routes that start with "api/v1" but without trying to match to an Api::V1::Doorkeeper class.