Ruby on rails 无法在模块中的Rails 4模型中自动加载常量API控制器

Ruby on rails 无法在模块中的Rails 4模型中自动加载常量API控制器,ruby-on-rails,ruby,api,ruby-on-rails-4,Ruby On Rails,Ruby,Api,Ruby On Rails 4,我正在尝试为模块中定义的模型创建api控制器 模型类: module Reports class Report < ActiveRecord::Base end end 尝试调用api/v2/reports时出现的错误是: LoadError (Unable to autoload constant Report, expected /.../app/models/reports/report.rb to define it): 有没有办法解决这个问题,让api控制器查找Rep

我正在尝试为模块中定义的模型创建api控制器

模型类:

module Reports
  class Report < ActiveRecord::Base
  end
end
尝试调用api/v2/reports时出现的错误是:

LoadError (Unable to autoload constant Report, expected /.../app/models/reports/report.rb to define it):

有没有办法解决这个问题,让api控制器查找Reports::Report而不是Report?

您的控制器需要以某种方式包含
报告

module Api
  module V2
    module Reports
      class Report < API::V2::BaseController

        your controller actions

      end
    end
  end
end
模块Api
模块V2
模块报告
类报告

除此之外,我认为如果您使用
Reports::Report

这是无法实现的。你需要一直写报告。但我想知道为什么你需要有名称空间的模型?@andriybaran我必须隔离模型以避免名称冲突。所以你是说所有的模型都应该定义为根?请阅读这个问题,我也试过这个,但没有运气。它给我同样的错误你的报告的路径文件是什么:报告?应该是
/app/models/reports/report.rb
,因为您得到的错误提示路径是,我刚刚意识到您的路由也缺少一个嵌套的
报告命名空间,但整个命名在我看来很奇怪,不确定rails是否会混淆。模块的名称应该是单数,然后您可能会有一个财务报告或其他什么,
Reports::report
在我看来很混乱。我还尝试了不同的命名,将Reports::report更改为Reporting::report,从而更改所有类和控制器。我还尝试添加报告名称空间,但错误总是一样的
LoadError (Unable to autoload constant Report, expected /.../app/models/reports/report.rb to define it):
module Api
  module V2
    module Reports
      class Report < API::V2::BaseController

        your controller actions

      end
    end
  end
end