无法在 Rails 4 中自动加载常量 API 控制器
Unable to autoload constant API Controller in Rails 4
我正在我的 Rails 4.2.6 应用程序中创建一个简单的 api 端点,但我遇到了问题。
当我点击 url: http://lvh.me:9077/api/v1/grubs 我收到以下错误:
Unable to autoload constant Api::V1::GrubsController, expected /Users/shakycode/code/grubs/app/controllers/api/v1/grubs_controller.rb to define it
这是我定义端点的 routes.rb 文件。
namespace :api do
namespace :v1 do
resources :grubs, only: [:index]
end
end
这是我的 app/controllers/api/v1/grubs_controller.rb
class API::V1::GrubsController < ApplicationController
protect_from_forgery with: :null_session
before_action :destroy_session
def destroy_session
request.session_options[:skip] = true
end
def index
@grubs = Grub.all
respond_to do |format|
format.json { render json: @grubs}
end
end
end
我有一个 Rails 4.2.1 应用程序,我在其中使用了相同的策略,但在 4.2.6 中,当我尝试拉动 API 时出现此错误。
提前致谢!
更新:这是在浏览器中使用 better_errors 引发的异常:
load_missing_constantactivesupport (4.2.6) lib/active_support/dependencies.rb
490
491
492
493
494
495
496
497
498
499
500
if loading.include?(expanded)
raise "Circular dependency detected while autoloading constant #{qualified_name}"
else
require_or_load(expanded, qualified_name)
raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false)
return from_mod.const_get(const_name)
end
elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
return mod
elsif (parent = from_mod.parent) && parent != from_mod &&
Rails 通常只将模块的名字大写。换句话说,Rails 需要命名空间 Api::V1::GrubsController
,但您将其定义为 API::V1::GrubsController
。
你的class名字是
class API::V1::GrubsController < ApplicationController
而在您的错误中它试图寻找 Api::V1::GrubsController
。将 class 中的名称更改为 Api
我在处理 rails 6 应用程序时遇到了类似的问题。
问题是我像往常一样定义了我的控制器
class ProductsController < ApplicationController
def index
end
与此同时,我已将版本控制添加到 API,这导致了这种命名空间 Api/V1
。
我是这样解决的:
我只需要将命名空间添加到控制器 class 定义中即可。
class Api::V1::ProductsController < ApplicationController
def index
end
就这些了。
希望对您有所帮助
我正在我的 Rails 4.2.6 应用程序中创建一个简单的 api 端点,但我遇到了问题。
当我点击 url: http://lvh.me:9077/api/v1/grubs 我收到以下错误:
Unable to autoload constant Api::V1::GrubsController, expected /Users/shakycode/code/grubs/app/controllers/api/v1/grubs_controller.rb to define it
这是我定义端点的 routes.rb 文件。
namespace :api do
namespace :v1 do
resources :grubs, only: [:index]
end
end
这是我的 app/controllers/api/v1/grubs_controller.rb
class API::V1::GrubsController < ApplicationController
protect_from_forgery with: :null_session
before_action :destroy_session
def destroy_session
request.session_options[:skip] = true
end
def index
@grubs = Grub.all
respond_to do |format|
format.json { render json: @grubs}
end
end
end
我有一个 Rails 4.2.1 应用程序,我在其中使用了相同的策略,但在 4.2.6 中,当我尝试拉动 API 时出现此错误。
提前致谢!
更新:这是在浏览器中使用 better_errors 引发的异常:
load_missing_constantactivesupport (4.2.6) lib/active_support/dependencies.rb
490
491
492
493
494
495
496
497
498
499
500
if loading.include?(expanded)
raise "Circular dependency detected while autoloading constant #{qualified_name}"
else
require_or_load(expanded, qualified_name)
raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false)
return from_mod.const_get(const_name)
end
elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
return mod
elsif (parent = from_mod.parent) && parent != from_mod &&
Rails 通常只将模块的名字大写。换句话说,Rails 需要命名空间 Api::V1::GrubsController
,但您将其定义为 API::V1::GrubsController
。
你的class名字是
class API::V1::GrubsController < ApplicationController
而在您的错误中它试图寻找 Api::V1::GrubsController
。将 class 中的名称更改为 Api
我在处理 rails 6 应用程序时遇到了类似的问题。
问题是我像往常一样定义了我的控制器
class ProductsController < ApplicationController
def index
end
与此同时,我已将版本控制添加到 API,这导致了这种命名空间 Api/V1
。
我是这样解决的:
我只需要将命名空间添加到控制器 class 定义中即可。
class Api::V1::ProductsController < ApplicationController
def index
end
就这些了。
希望对您有所帮助