自动重新加载 rails 模块 [英] Automatically reload rails module
问题描述
我正在开发一个包含在我的 rails 应用程序中的 ruby 模块.我希望它在更改时自动重新加载.我已经进行了大量的谷歌搜索,并查看了此处讨论它的各种问题,但它们似乎都已过时或错误.
I'm developing a ruby module that I include in my rails app. I want it to be reloaded automatically when it changes. I've done extensive googling, and looked at the various questions here that discuss it, but they all seem out of date or wrong.
当外部模块发生变化时,如何让其重新加载到 rails 中?我曾尝试将其名称添加到 ActiveSupport::Dependencies.unloadable_constants
,但在我在控制台中键入 reload!
后,如果没有 NameError: uninitialized constant foo
,即使我做了另一个require 'foo_module'
.有谁知道如何让这个工作?
How do I get an external module to be reloaded in rails when it changes? I've tried adding its name to ActiveSupport::Dependencies.unloadable_constants
, but after I type reload!
in the console, I can no longer refer to that symbol without a NameError: uninitialized constant foo
, even if I do another require 'foo_module'
. Does anyone know how to get this working?
注意:这里是一个可能的重复,但请注意对答案"的评论说它从未解决模块的问题.还有 这个问题 在答案中有一个死链接,最后 这个,这也解决不了.
Note: here is one possible dup, but note in the comments to the 'answer' that it never solved the problem for modules. There's also this question which has a dead link in the answer, and finally this one, which also doesn't solve it.
推荐答案
我找到了方法:
- 确保
FooModule
在lib/foo_module.rb
中. - 使用
require_dependency
在lib/foo_module.rb
中要求你的外部库.
- Make sure
FooModule
is inlib/foo_module.rb
. - Use
require_dependency
to require your external library inlib/foo_module.rb
.
这两个步骤都是必需的,不需要其他步骤.
These steps are both required, and no others are required.
这篇关于自动重新加载 rails 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!