“NameError,未初始化的常量"修改控制器代码后出错 [英] "NameError, uninitialized constant" error after modifying controller code
问题描述
我最近能够通过将所有内容分组到文件夹中来组织我的代码.
我在 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:
我也知道,每当您更改 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.rb
在 app/controllers/admin
class Admin::MainController < ApplicationController
end
Helper main_helper.rb
在 app/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.rb
在 lib/admin
module Admin
class PagerDuty
....
end
end
推荐答案
尝试更改
require "admin/pager_duty.rb"
到
require_dependency "admin/pager_duty.rb"
在您的模块中.
这篇关于“NameError,未初始化的常量"修改控制器代码后出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!