简体   繁体   English

Rails API-API ::: V1 :: UsersController#index中的LoadError

[英]Rails API - LoadError in API::V1::UsersController#index

I'm trying to add a simple JSON API to my APP 我正在尝试向我的APP添加一个简单的JSON API

#routes.rb
namespace :api, :format => :json do
  namespace :v1 do
    resources :users
  end
end


#controllers/api/v1/users_controller.rb
class Api::V1::UsersController < ApplicationController
  respond_to :json

  def index
    respond_with User.all
  end
end


#inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'API'
end

rake routes gives me the following: rake routes给我以下内容:

api_v1_users        GET      /api/v1/users(.:format)             api/v1/users#index
                    POST     /api/v1/users(.:format)             api/v1/users#create
new_api_v1_user     GET      /api/v1/users/new(.:format)         api/v1/users#new
edit_api_v1_user    GET      /api/v1/users/:id/edit(.:format)    api/v1/users#edit
api_v1_user         GET      /api/v1/users/:id(.:format)         api/v1/users#show
                    PATCH    /api/v1/users/:id(.:format)         api/v1/users#update
                    PUT      /api/v1/users/:id(.:format)         api/v1/users#update
                    DELETE   /api/v1/users/:id(.:format)         api/v1/users#destroy

When I visit http://localhost:3000/api/v1/users I got the following error: 当我访问http://localhost:3000/api/v1/users ,出现以下错误:

LoadError in API::V1::UsersController#index Unable to autoload constant API::V1::UsersController, expected /path/to/app/controllers/api/v1/users_controller.rb to define it API :: V1 :: UsersController#index中的LoadError无法自动加载常量API :: V1 :: UsersController,需要/path/to/app/controllers/api/v1/users_controller.rb进行定义

The error message says exactly what to do. 错误消息确切说明了要做什么。 Make sure you use API instead of Api in your controller name. 确保在控制器名称中使用API而不是Api

#  == HERE ==
class API::V1::UsersController < ApplicationController
  respond_to :json

  def index
    respond_with User.all
  end
end

Or second way declare another acronym, or remove it anyway: 或者,第二种方法声明另一个首字母缩写词,或者无论如何都将其删除:

inflections.rb : inflections.rb

ActiveSupport::Inflector.inflections(:en) do |inflect|
   inflect.acronym 'Api'
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LoadError(无法自动加载常量Api :: V1 :: UsersController) - LoadError (Unable to autoload constant Api::V1::UsersController) 在Rails 4中创建API - 未初始化的常量Api :: V1 :: UsersController - Create API in Rails 4 - Uninitialized constant Api::V1::UsersController 无法自动加载常量Api :: V1 :: UsersController - Unable to autoload constant Api::V1::UsersController UsersController#index中的ArgumentError - ArgumentError in UsersController#index Api :: V1 :: UsersController #create中的ActionController InvalidAuthenticityToken - ActionController InvalidAuthenticityToken in Api::V1::UsersController#create UsersController#index载波中的Errno :: ENOENT - Errno::ENOENT in UsersController#index carrierwave LoadError(无法自动加载常量Api :: V1 :: UserTokenController)敲Gem - LoadError (Unable to autoload constant Api::V1::UserTokenController) Knock Gem Michael Hatl的Rails教程第2章:测试/ users时,UsersController#index中的Errno :: ENOENT - Michael Hatl's Rails Tutorial Chapter 2: Errno::ENOENT in UsersController#index when testing /users Rails-API在API :: V1 :: Controller#index中获取AbstractController :: DoubleRenderError - Rails-API getting AbstractController::DoubleRenderError in API::V1::Controller#index Grape :: API - 无法自动加载常量Base,预期/app/api/v1/base.rb来定义它(LoadError) - Grape::API – Unable to autoload constant Base, expected /app/api/v1/base.rb to define it (LoadError)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM