无法自动加载常量Api :: V1 :: UsersController

时间:2016-09-28 13:15:46

标签: ruby-on-rails ruby api ruby-on-rails-4

我一直在开发API,但是当我访问端点时出现Unable to autoload constant Api::V1::UsersController, expected /Users/toshikiinami/Desktop/billing/app/controllers/api/v1/users_controller.rb to define it

  • 我使用rails 4.2.3

正确配置子域端点的任何想法?

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

1 个答案:

答案 0 :(得分:3)

问题如前所述:

controllers/api/v1/users_controller.rb定义UsersController课程,而不是Api::V1::UsersController

解决方案如下:

#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