在 Rails 4 中创建 API - 未初始化的常量 Api::V1::UsersController

Posted

技术标签:

【中文标题】在 Rails 4 中创建 API - 未初始化的常量 Api::V1::UsersController【英文标题】:Create API in Rails 4 - Uninitialized constant Api::V1::UsersController 【发布时间】:2014-09-11 03:51:01 【问题描述】:

对于个人项目,我想在 Rails 4 中创建一个 Restful Web 服务。

所以,我使用rails-api 创建了我的第一个项目并添加了以下代码:

routes.rbMyProject > config 目录中

MyProject::Application.routes.draw do
    namespace :api, defaults: format: 'json' do
        namespace :v1 do
            resources :users
        end
    end
end

users_controller.rbMyProject > app > controllers > api > v1 > users 目录中

module Api
    module V1
        class UsersController < ApplicationController
            def index
            end

            def create
            end

            def show
            end

            def update
            end

            def delete
            end
        end 
    end
end

当我使用命令行rails s 启动 Rails 服务器并转到此 URL:http://localhost:3000/api/v1/users/show 时,我遇到了此错误:

未初始化的常量 Api::V1::UsersController

Rails.root:/Users/Jean/Development/MyProject

应用程序跟踪 |框架跟踪 |全跟踪活动支持 (4.0.4) lib/active_support/inflector/methods.rb:228:in const_get' activesupport (4.0.4) lib/active_support/inflector/methods.rb:228:in block in constantize' activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:in each' activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:ininject' activesupport (4.0.4) lib/active_support/inflector/methods.rb:224:in constantize' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:76:incontroller_reference' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:66:in controller' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:44:incall' 动作包 (4.0.4) lib/action_dispatch/journey/router.rb:71:in block in call' actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:ineach' actionpack (4.0.4) lib/action_dispatch/journey/router.rb:59:in call' actionpack (4.0.4) lib/action_dispatch/routing/route_set.rb:674:in call' rack (1.5.2) lib/rack/etag.rb:23:in call' rack (1.5.2) lib/rack/conditionalget.rb:25:incall' rack (1.5. 2) lib/rack/head.rb:11:in call' actionpack (4.0.4) lib/action_dispatch/middleware/params_parser.rb:27:incall' activerecord (4.0.4) lib/active_record/query_cache.rb:36:in call' activerecord (4.0.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in call' activerecord (4.0.4) lib/active_record/migration.rb:373:in call' actionpack (4.0.4) lib/action_dispatch/middleware/callbacks.rb:29:inblock in call' activesupport (4.0.4) lib/active_support/callbacks.rb:373:in _run__4323212420903942114__call__callbacks' activesupport (4.0.4) lib/active_support/callbacks.rb:80:inrun_callbacks' 动作包 (4.0.4) lib/action_dispatch/middleware/callbacks.rb:27:in call' actionpack (4.0.4) lib/action_dispatch/middleware/reloader.rb:64:in call' actionpack (4.0.4) lib/action_dispatch/middleware/remote_ip.rb:76:in call' actionpack (4.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in call' actionpack (4.0.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in call' railties (4.0.4) lib/rails/rack/logger.rb:38:incall_app' railties (4.0.4) lib/rails/rack/logger.rb:20:in block in call' activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:inblock in 标记' activesupport (4.0.4) lib/active_support/tagged_logging.rb:26:in tagged' activesupport (4.0.4) lib/active_support/tagged_logging.rb:68:intagged' railties (4.0.4) lib/rails/rack/logger.rb:20:in call' actionpack (4.0.4) lib/action_dispatch/middleware/request_id.rb:21:incall' 机架 (1.5.2) lib/rack/runtime.rb:17:in call' activesupport (4.0.4) lib/active_support/cache/strategy/local_cache.rb:83:incall' 机架 (1.5.2) lib/rack/lock.rb:17:in call' actionpack (4.0.4) lib/action_dispatch/middleware/static.rb:64:incall' railties (4.0.4) lib/rails/engine.rb:511:in call' railties (4.0.4) lib/rails/application.rb:97:incall' 机架 (1.5.2) lib/rack/lock.rb:17:in call' rack (1.5.2) lib/rack/content_length.rb:14:incall' 机架 (1.5.2) lib/rack/handler/webrick.rb:60:in service' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in service' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in run' /Users/Jean/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in block in start_thread'

我在 *** 上发现很多帖子都有相同的错误,但答案并没有帮助我解决我的问题。

谢谢!

【问题讨论】:

【参考方案1】:

如果你有这个路径:

MyProject > 应用 > 控制器 > api > v1 > 用户

控制器应该是class Api::V1::Users::UsersController 如果这样:

MyProject > 应用 > 控制器 > api > v1 > users_controller.rb

然后class Api::V1::UsersController

对于缩写名称 API::V1::UsersController 而不是 Api::V1::Users::UsersController 使用变形器:

config/initializers/inflections.rb

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

【讨论】:

谢谢你解决了我的问题!我终于删除了user 目录并像你说的那样声明我的控制器:class Api::V1::UsersController。但是关于这篇文章:amberbit.com/blog/2014/2/19/… 博主使用module APImodule V1 而不是class Api::V1::UsersController 创建了UsersController 类。为什么? 抱歉,我的第一条评论可能不够清楚。我的意思是博主声明了它的类module Api module V1 class UsersController &lt; ApplicationController ... end end end 而不是class Api::V1::UsersController ... end 为什么? 哦,我明白了,我的错,小心这个不同的东西。 谢谢,我只是看看你的链接。因此,如果我理解得很好,那么使用 module 声明类的方式(就像我在第一条评论中发表的博文中一样)只有在 cache_classes 设置为 true 时才有效? @JeanLebrument 所以我认为你理解正确,尝试解决这个问题。

以上是关于在 Rails 4 中创建 API - 未初始化的常量 Api::V1::UsersController的主要内容,如果未能解决你的问题,请参考以下文章

未初始化的常数 Rspec

如何在 ruby​​ on rails 4 中创建高级搜索

无法在 Rails 4 中自动加载常量 API 控制器

在 Rails 应用程序的其他数据库中创建数据

如何在 rails (4.1.5) 中创建固定装置(对于 Devise 用户)作为 yml.erb?

如何在 scala 中创建未初始化的 Dataframe 变量。因此可以在 if else 条件下初始化相同的变量