2016-09-28 24 views
2

我一直在开发API,但是当我访问端点Unable to autoload constant Api::V1::UsersController, expected /Users/toshikiinami/Desktop/billing/app/controllers/api/v1/users_controller.rb to define it出来。无法自动加载常量Api :: V1 :: UsersController

  • 我用导轨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 

回答

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 
+0

谢谢!但我现在有以下错误'缺少模板api/v1/users/testaaa {:locale => [:en],::formats => [:json],:variants => [],:handlers => [:erb ,:builder,:raw,:ruby,:coffee,:jbuilder]}。搜索:' – Tosh

+0

@Toshi google它,你一定会很好;)现在你问的问题已经解决了。如果你不能解决它,请提出一个新的问题:) –

+0

@Toshi尝试'渲染json:{test:'test'},status::ok';) –

相关问题