“无法自动加载常量"Rails 5.2.0 中的错误 [英] "Unable to autoload constant" Bug in Rails 5.2.0

查看:18
本文介绍了“无法自动加载常量"Rails 5.2.0 中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 Rails 5.2.0 应用程序.此 LoadError 总是出现在重启或重新编译后的第一个请求中:

I'm running a Rails 5.2.0 application. This LoadError always appears on the first request after a reboot or a recompile:

无法自动加载常量 Api::V1::ApplesController,需要/fruits_and_vegetables/app/controllers/api/v1/apples_controller.rb 来定义它

相关文件:

routes.rb

Rails.application.routes.draw do
  namespace :api do
    namespace :v1 do
      get 'apples', to: 'apples#get'
    end
  end
end

文件结构如下:

 - app
  - controllers
   - api
    - v1
     - apples_controller.rb

apples_controller.rb 中的内容:

class Api::V1::ApplesController < ApplicationController
   // stuff
end

StackOverflow 上的一些帖子表明此错误可能是由我的控制器文件中的拼写错误引起的,但事实并非如此.或者,有些人提到了 Rails 的区分大小写.但是,如果我尝试在 routes.rb 中将 apiv1 更改为 ApiV1 文件或控制器中,Rails 会抛出错误.

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.

我看到一条评论建议一个人应该运行 rails r 'puts ActiveSupport::Dependencies.autoload_paths',如果我在输出列表中没有看到 /fruits_and_vegetables/app/controllers/api,则添加 config.autoload_paths <<Rails.root.join("app/controllers/api") 到我的 config/application.rb 文件,但是 似乎不鼓励.

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?

推荐答案

当我简单地将 Rails 升级到 5.2.0 版时,我的旧 Rails 5.1 应用程序代码发生了同样的 (LoadError) 错误.修复(对我而言)是添加一个缺失的源文件,该文件仅定义了一个子模块(以满足自动加载器).我将在原始帖子示例的上下文中解释修复.

This same (LoadError) error occurred for my legacy Rails 5.1 application code, when I simply upgraded Rails to version 5.2.0. The fix (for me) was to add a missing source file that solely defines a submodule (to satisfy the autoloader). I will explain the fix in the context of the original post example.

在原帖中,ApplesControllerV1 命名空间的一部分.问题是,V1 子模块没有(明确)定义.解决方案是在正确的(自动加载)路径上创建一个文件,该文件定义了 V1 模块:

In the original post, the ApplesController is part of the V1 namespace. The problem is, that the V1 submodule is not (explicitly) defined. The solution is to create a file, at the proper (autoload) path, that defines the V1 module:

app/controllers/api/v1.rb

module Api
  module V1
  end
end

显然,从 Rails 5.2 开始,自动加载器需要明确定义每个模块命名空间.您可以阅读有关 Rails 5.2 常量自动加载的技术细节.

Apparently, as of Rails 5.2, the autoloader needs every module namespace to be explicitly defined. You can read the technical details about Rails 5.2 constant autoloading.

以及 对同一问题的相关 SO 答案.

这篇关于“无法自动加载常量"Rails 5.2.0 中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆