簡體   English   中英

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

[英]Rails Controllers in subfolder ActionController::RoutingError (uninitialized constant

我在 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

還有我的routes.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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM