【问题标题】:ArgumentError - A copy of Api::V2 has been removed from the module tree but is still active!:ArgumentError - Api::V2 的副本已从模块树中删除,但仍处于活动状态!:
【发布时间】:2019-07-22 09:10:19
【问题描述】:

我一定是在某些方面搞砸了我的 api。我现在收到以下错误消息:

ArgumentError - Api::V2 的副本已从模块中删除 树,但仍处于活动状态!:

我不确定如何解决这个问题。我的一些 Api 控制器如下所示:

class Api::V2::UsersController < ApplicationController

但以前是这样的:

module Api
  module V2
    class Api::V2::UsersController < ApplicationController

什么是正确的格式,我该如何解决这个问题?谢谢。

编辑:

.../active_support/dependencies.rb:457: warning: already initialized constant Api
.../active_support/dependencies.rb:457: warning: previous definition of Api was here

LoadError - Unable to autoload constant Api::UsersController, expected .../app/controllers/api/users_controller.rb to define it:

我也有 api/users_controller.rbapi/v2/users_controller.rb 两者似乎都有同样的问题。

【问题讨论】:

  • 你重启了你的服务器/控制台和Spring
  • 请说明您现在遇到了什么错误:ArgumentErrorLoadError 或两者兼而有之?

标签: ruby-on-rails ruby api activesupport


【解决方案1】:

为了使class Api::V2::UsersController &lt; ApplicationController 工作,您的文件夹结构应为/controllers/api/v2/users_controller.rb,而application_controller.rb 文件应位于/controllers 下。

【讨论】:

  • 如果你重启了 spring 和 rails 仍然出现错误,这意味着你的应用仍然有旧版本的引用。您可能正在自动加载某些内容。完整的堆栈跟踪会很有帮助。无论哪种方式,您都可以这样尝试:class ::Api::V2::UsersController &lt; ApplicationController.
  • 谢谢,但问题仍然存在。
  • 编辑后,您确定您的 API 仍在版本中吗? LoadError - Unable to autoload constant Api::UsersController, expected .../app/controllers/api/users_controller.rb to define it: 表示它应该是 class Api::UsersController &lt; ApplicationController/controllers/apiapplication_controller.rb/controllers 文件夹
  • 是的,就是这样。
【解决方案2】:

Ruby 类名的部分除以:: 是命名空间,应包含在module 关键字中,因此类Api::V2::UsersController 应定义为

module Api
  module V2
    class UsersController < ApplicationController
#or
class Api::V2::UsersController < ApplicationController

正如@MihaiColceriu 所说,放在/controllers/api/v2/users_controller.rb

如果你有 /controllers/api/users_controller.rb 它应该定义 Api::UsersController

module Api
  class UsersController < ApplicationController

请特别注意类名与命名空间和文件名与目录应一致且唯一匹配

【讨论】:

    猜你喜欢
    • 2018-10-09
    • 2013-07-07
    • 2011-11-28
    • 2013-09-24
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 2019-02-15
    相关资源
    最近更新 更多