简体   繁体   中英

Unable to autoload constant Api::V1::UsersController

I've been developing API, but when I access the endpoint Unable to autoload constant Api::V1::UsersController, expected /Users/toshikiinami/Desktop/billing/app/controllers/api/v1/users_controller.rb to define it comes out.

  • I use rails 4.2.3

Any ideas to configure the subdomain endpoint properly?

config/routes.rb

Rails.application.routes.draw do    
  namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/'  do
    scope module: :v1 do
      resources :users, only: [:index]
    end
  end
end

controllers/api/v1/users_controller.rb

class UsersController < ActionController::Base
  def index
    render 'test' => 'json'
  end
end

/etc/hosts

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1 localhost
127.0.0.1 api.localhost.local

The problem is as stated:

controllers/api/v1/users_controller.rb defines UsersController class, not Api::V1::UsersController .

The solution looks as follows:

#controllers/api/v1/users_controller.rb
module Api
  module V1
    class UsersController < ActionController::Base
      def index
        render json: { test: 'test' }, status: :ok
      end
    end
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM