Rails 3.2.x:如何在开发过程中重新加载应用程序/类目录? [英] Rails 3.2.x: how to reload app/classes dir during development?

查看:23
本文介绍了Rails 3.2.x:如何在开发过程中重新加载应用程序/类目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 Rails 代码不能很好地适应模型或控制器盒.因此,根据 this answer,我创建了一个 app/classes 目录.Rails 3 似乎自动将其添加到 Rails 中的加载路径",我的应用程序正确地找到了我在其中定义的类,而无需使用 require 语句.

I have some Rails code that does not fit neatly into a model or controller box. So as per this answer, I created a app/classes directory. Rails 3 seems to automatically add this to the "load path" in Rails, and my application correctly finds the classes I define in there without needing to use require statements.

然而,app/classes 中的代码在开发模式下不会重新加载;如果我进行更改,我需要重新启动服务器才能看到更改.

However the code in app/classes does not get reloaded in development mode; if I make a change, I need to restart the server in order to see that change.

在 Rails 3.2.x 中使给定目录可重新加载"的正确方法是什么?这里的一些答案建议这样做:

What's the proper way to make a given directory "reloadable" in Rails 3.2.x? A few answers here recommend doing:

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

但我相信这只是将 app/classes 添加到初始目录集以在其中查找代码的效果;似乎并没有让它们为每个请求重新加载(而且在 3.x 中似乎自动添加了 app/*).

but I believe that this merely has the effect of adding app/classes to the initial set of directories to find code in; does not seem to make them reloadable for each request (and furthermore in 3.x it seems that app/* is automatically added).

更新:

数字,我在发布问题后仅 30 秒就偶然发现了解决方案:

Figures, I stumbled upon the solution a mere 30 seconds after posting the question:

我将我的课程包裹在一个模块中.一旦我删除了周围的MyModule",它突然变得可以重新加载.来自 Java 背景,并且被污染全局命名空间的 Ruby 代码烧毁,我养成了将所有东西都放在一个模块中的习惯.我猜 Rails应用程序"代码必须存在于任何模块之外?

I had my class wrapped inside a module. Once I removed the surrounding "MyModule", it suddenly became reloadable. Coming from a Java background, and having been burnt by Ruby code that pollutes the global namespace, I've developed a habit of putting everything inside a module. I guess Rails "app" code must live outside of any module?

推荐答案

您是在单独的文件中声明了模块,还是在类中隐式声明了它?这可能会影响自动加载行为.模块 Foo;class Barclass Foo::Bar.如果 Rails 自动加载器找不到与 Foo 模块一起使用的 foo.rb,它可能会跳过重新加载它.

Did you declare the module in a separate file, or did you declare it implicitly inside the class? This might have an effect on autoload behavior. module Foo; class Bar vs. class Foo::Bar. It might be if the Rails autoloader can't find a foo.rb to go with the Foo module it might skip reloading it.

这篇关于Rails 3.2.x:如何在开发过程中重新加载应用程序/类目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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