【问题标题】:config.autoload_paths not working, unable to include modules in rails 4config.autoload_paths 不起作用,无法在 rails 4 中包含模块
【发布时间】:2017-01-24 07:45:58
【问题描述】:

我创建了一个简单的模块并将其放在 lib 目录中,并已包含在控制器文件中。

下面是控制器代码。

class UserController < ApplicationController
  include Departments
  def create
    user_data = Hash.new
    user_data["data"] = "hello world!"
    user_data["price"] = 12
    render :json => user_data
  end
end

当我尝试执行它时,我看到以下错误

ActionController::RoutingError (uninitialized constant UserController:: Departments):

我搜索了论坛并看到添加

config.autoload_paths += %W(#{config.root}/lib)

解决了这个问题,但在我的情况下没有。我正在使用 Rails 4.2.7.1 和 ruby​​ ruby​​ 1.9.3p547。

谁能指出可能是什么问题,谢谢。

【问题讨论】:

  • 你能发布完整的错误吗?
  • 添加了完整的错误信息
  • 你可以尝试用config.autoload_paths &lt;&lt; Rails.root.join('lib')替换它吗?
  • 还是一样,使用上述两种语法,当我执行 "bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths'" 时,我看到包含的 lib 目录

标签: ruby-on-rails ruby ruby-on-rails-4 autoload


【解决方案1】:

我的命名转换错误,我在 lib 文件夹下以我的模块名称创建了一个子目录,然后使用类名.rb 创建了文件并且它起作用了。

参考:"Uninitialized constant" error when including a module

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-05
    • 1970-01-01
    • 2021-12-25
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多