I'm running a Rails 5.2.0 application. This LoadError
always appears on the first request after a reboot or a recompile:
Unable to autoload constant Api::V1::ApplesController, expected /fruits_and_vegetables/app/controllers/api/v1/apples_controller.rb to define it
The pertinent files:
routes.rb
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
get 'apples', to: 'apples#get'
end
end
end
Here's the file structure:
- app
- controllers
- api
- v1
- apples_controller.rb
What's in apples_controller.rb
:
class Api::V1::ApplesController < ApplicationController
// stuff
end
A few posts on StackOverflow have suggested that this error is likely caused by a typo in my controller file, but that is not the case. Or, some folk have mentioned the case sensitivity of Rails. However, if I try to change api
and v1
to Api
or V1
in the routes.rb
file or in the controller, Rails will throw up an error.
I saw a comment suggesting that one should run rails r 'puts ActiveSupport::Dependencies.autoload_paths'
, and if I don't see /fruits_and_vegetables/app/controllers/api
in the output listing, then add config.autoload_paths << Rails.root.join("app/controllers/api")
to my config/application.rb
file, but it seems that is discouraged.
Any thoughts? I see at least a dozen similar posts on here, but no real concrete solution it seems?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…