ActionController::RoutingError: uninitialized constant Api::V1::ApiController

12,212

Solution 1

You should be using the right constant while inheriting - ::Api::ApiController:

module Api::V1
  class Team::TeamController < ::Api::ApiController

because otherwise it is searching for Api::V1::ApiController, but should search for Api::ApiController

Solution 2

Right now you have Api::ApiController.
Your app/controllers/api/v1/api_controller.rb is missing V1 in namespace

module Api::V1
  class ApiController < ApplicationController
    ..
  end
end

UPDATE

If your ApiController is outside V1 folder then you should do

module Api::V1
  class Team::TeamController < ::Api::ApiController
Share:
12,212
Adrià Carro
Author by

Adrià Carro

Updated on June 17, 2022

Comments

  • Adrià Carro
    Adrià Carro almost 2 years

    I have Rails 5 API project for controlling user tasks and I have the following error but not always for the same controller and route.

    ActionController::RoutingError: uninitialized constant Api::V1::ApiController
    

    I describe you a little bit my project to explain in more detail the error.

    App structure

    enter image description here

    Routes

    scope module: 'api' do
      namespace :v1 do
    
        # => Login routes
        scope module: 'login' do
          match 'login', to: 'sessions#login', as: 'login', via: :post
        end
    
        # => Team routes
        scope module: 'team' do
    
          # => no admin routes
          resources :tasks, except: [:index] do
            collection do
              match ':view', to: 'tasks#index', as: 'tasks', via: [:get, :post]
            end
          end
        end
    
      end
    end
    

    API Controller

    module Api
      class ApiController < ApplicationController
    
        def respond_with_errors(object)
          render json: {errors: ErrorSerializer.serialize(object)}, status: :unprocessable_entity
        end
    
      end
    end
    

    Team Controller

    module Api::V1
      class Team::TeamController < ApiController
    

    Tasks Controller

    module Api::V1
      class Team::TasksController < Team::TeamController
    

    Login Controller

    module Api::V1
      class Login::LoginController < ApiController
    

    Sessions Controller

    module Api::V1
      class Login::SessionsController < Login::LoginController
    

    When I execute login route and after tasks route, I get the error in last route and all the routes in team module. If I change the project and save it (only one blank space) and then I execute tasks route and after login route, I get the error in last route and all the routes in login module.

    It doesn't have any sense...

    Rails server in this errors enter image description here enter image description here

  • Adrià Carro
    Adrià Carro over 7 years
    My API Controller is outside V1 folder.
  • Adrià Carro
    Adrià Carro over 7 years
    My API Controller is outside V1 folder.
  • Andrey Deineko
    Andrey Deineko over 7 years
    camon, this is just stolen from my answer :)
  • Aleksey
    Aleksey over 7 years
    no, it's your who stole it. I wrote comment to the question initially and was about to answer it.
  • Andrey Deineko
    Andrey Deineko over 7 years
    it does not matter who wrote the comment, it is the answer that you have edited after I realized I was wrong with my first one. Have guts to admit
  • Andrey Deineko
    Andrey Deineko over 7 years
    @carro always make sure you use the correct constant :)
  • Aleksey
    Aleksey over 7 years
    @Andrey Deineko carro has commented my answer too (he even has done it 1 minute earlier than your answer). So I have updated it. Yes, after that I have saw that you have done it too (even in the same way).
  • Andrey Deineko
    Andrey Deineko over 7 years
    not going to discuss this further. answers' edition time speaks for itself :)
  • Aleksey
    Aleksey over 7 years
    I just have not saw it at the time. I never stole any answer.