子文件夹 ActionController::RoutingError 中的 Rails 控制器(未初始化的常量



我在StackOverflow中看到过相关的问题,但我仍然无法使其工作。

我正在制作一个在控制器内带有子文件夹的 API,但我不断收到此错误:

LoadError (Unable to autoload constant Api::Report::ReportController, expected ... 
/controllers/api/report/report_controller.rb to define it):

或者这个:

ActionController::RoutingError (uninitialized constant Api::Report::ReportController):

这是我的文件夹结构:

->controllers
    ->api
       ->report
         infected_controller.rb
         report_controller.rb

# inflected_controller.rb
module Api
  class Report::InfectedController < ReportController
    def infected
    end
  end
end
# report_controller.rb
module Api
  class ReportController < ApplicationController
    def index
    end
  end
end

和我的路线.rb

Rails.application.routes.draw do
  apipie
  namespace :api do
    scope module: 'report' do
      get 'infected' => 'infected#infected'
      resources :report
    end
  end
end
module Api
  module Report # <============== missing module
    class ReportController < ApplicationController
      def index
      end
    end
  end
end

module Api
  class Report::InfectedController < Report::ReportController
    def infected
    end
  end
end

尝试以下代码

# report_controller.rb
module Api
  class Report::ReportController < ApplicationController
    def index
    end
  end
end

最新更新