5

When trying to access the following URL, I get a 404 error page:

dev.mydomain.com/api

whereas my routes.rb file mentions that this route does exist:

routes.rb

constraints :subdomain => 'dev' do
  root :to => 'developers/main#index', :as => :developers
  namespace 'api', :as => :developers_api do
    root :to => 'developers/apidoc/main#index'
  end
end

rake routes

         developers   /(.:format)      {:subdomain=>"dev", :controller=>"developers/main", :action=>"index"}
developers_api_root   /api(.:format)   {:subdomain=>"dev", :controller=>"api/developers/apidoc/main", :action=>"index"}

controller

/app/controllers/developers/apidoc/main_controller.rb

class Developers::Apidoc::MainController < Developers::BaseController
  def index
  end
end

logs

[router]: GET dev.mydomain.com/api dyno=web.1 queue=0 wait=0ms service=14ms status=404 bytes=0
[web.1]: Started GET "/api"
[web.1]: ActionController::RoutingError (uninitialized constant Api::Developers)

3 Answers 3

5

I'm guessing the problem is that your route points to api/developers/apidoc/main but your class is only Developers::Apidoc::MainController. You should either not namespace that route with api or add Api to the namespace of the controller - Api::Developers::Apidoc::MainController.

Sign up to request clarification or add additional context in comments.

Comments

3

Another important factor to keep in mind is that route namespaces need accompanying directory paths to be consistent. Getting this wrong will also cause an error like this:

Routing Error

uninitialized constant Api::Developers

In my case, I had a route structure like this:

namespace "api" do
  namespace "developers" do
    ...
  end
end

and the folder/directory structure should have been app/controllers/api/developers/

Comments

1

TL;DR Replace namespace with scope

Given the following folder structure

 Rails.root
  |
  +-- app/
  |    |
  |    +-- controllers/
  |         |
  |         +-- jobs_controller.rb
  +-- config/
       |
       +-- routes.rb

the snippet below in routes.rb gives an error ActionController::RoutingError: uninitialized constant Api:

namespace :api do
  namespace :v1 do
    resources :jobs
  end
end

while the following works:

scope :api do
  scope :v1 do
    resources :jobs 
  end
end

The reason is briefly mentioned at Rails Routing from the Outside In:

The namespace scope will automatically add :as as well as :module and :path prefixes.

And indeed, namespace is just a wrapper around scope with a bunch of predefined options:

# File actionpack/lib/action_dispatch/routing/mapper.rb, line 871
def namespace(path, options = {})
  path = path.to_s

  defaults = {
    module:         path,
    path:           options.fetch(:path, path),
    as:             options.fetch(:as, path),
    shallow_path:   options.fetch(:path, path),
    shallow_prefix: options.fetch(:as, path)
  }

  scope(defaults.merge!(options)) { yield }
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.