“NameError,未初始化的常量"修改控制器代码后出错 [英] "NameError, uninitialized constant" error after modifying controller code

查看:71
本文介绍了“NameError,未初始化的常量"修改控制器代码后出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近能够通过将所有内容分组到文件夹中来组织我的代码.
我在 app/ 目录下的控制器组和 lib/ 目录下的模块具有相同的组名称"时遇到了问题,但我能够按照以下方法修复:

I was able recently to organize my code by grouping everything into folders.
I had an issue with having the same "group name" for both my group of controllers under the app/ directory and my module under the lib/ directory but I was able to fix by following this:

Rails:库模块和一组控制器的同名?

我也知道,每当您更改 lib 代码时,您都需要重新启动 rails 服务器,这对我来说完全没问题.

I also know that whenever you change your lib code, you need to restart the rails server which is totally fine by me.

但是最近重新组织之后,每次在controller里修改代码,都会出现如下错误!!!

But after the recent re-organization, every time I change the code in the controllers, I get the following error!!!

NameError at /admin
uninitialized constant Admin::PagerDuty

为了解决它,我只需重新启动服务器!

and to resolve it, I simply restart the server!!

有什么建议吗?!

结构:

控制器 main_controller.rbapp/controllers/admin

class Admin::MainController < ApplicationController
end

Helper main_helper.rbapp/helpers/admin

module Admin::MainHelper
      require "admin/pager_duty.rb"

      def pager_duty
        pagerduty = Admin::PagerDuty.new()
        @on_call = pagerduty.on_call()
        @counts = pagerduty.open_incidents()
      end
end

lib pager_duty.rblib/admin

module Admin 
  class PagerDuty
   ....
  end
end

推荐答案

尝试更改

require "admin/pager_duty.rb"

require_dependency "admin/pager_duty.rb"

在您的模块中.

这篇关于“NameError,未初始化的常量"修改控制器代码后出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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