Ruby on Rails 3 - 为每个请求重新加载 lib 目录 [英] Ruby on Rails 3 - Reload lib directory for each request

查看:45
本文介绍了Ruby on Rails 3 - 为每个请求重新加载 lib 目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 rails 3 应用程序创建一个新引擎.你可以猜到,这个引擎在我的应用程序的 lib 目录中.

I'm creating a new engine for a rails 3 application. As you can guess, this engine is in the lib directory of my application.

但是,我在开发它时遇到了一些问题.确实,每次更改引擎中的某些内容时,我都需要重新启动服务器.

However, i have some problems developing it. Indeed, I need to restart my server each time I change something in the engine.

有没有办法避免这种情况?

Is there a way to avoid this ?

我可以强制 rails 完全重新加载 lib 目录或特定文件及其对每个请求的要求吗?

Can I force rails to completely reload the lib directory or a specific file and his requirements for each request ?

感谢您的帮助:)

推荐答案

TL;DR

  • 把它放在config/application.rb

config.eager_load_paths += ["#{Rails.root}/lib"]

删除lib文件的require语句

去吧!

让我详细解释一下.

我不知道为什么接受这个答案,因为它对在每个请求上重新加载 lib 文件夹没有帮助.首先我认为它适用于 Rails 2,但问题明确指出它适用于 Rails 3,并且 3.0.0 的发布日期早于答案的日期.

I don't know why this answer is accepted, since it doesn't help with reloading lib folder on each request. First I thought that it works for Rails 2, but the question clearly states that it was for Rails 3 and the release date of 3.0.0 is before the date of the answer.

其他答案似乎过于复杂或没有提供真正的解决方案.

Other answers seem over-complicated or don't provide a real solution.

我决定稍微调查一下,因为它困扰着我,我什至发现人们对此有一个解决方法,它涉及在开发中将 lib 文件保存在 app/models 中,并且然后在完成后将其移动到 /lib .我们可以做得更好,对吗?

I decided to investigate things a little, because it was bothering me and I've even found out that people have a workaround for this and it involves saving lib files inside app/models in development and then moving it to /lib when done. We can do better, right?

我的解决方案经过测试:

My solution is tested against:

  • Rails 3.0.20
  • Rails 3.1.12
  • Rails 3.2.13
  • Rails 4.0.0.rc1

将其放入您的config/application.rb:

# in config/application.rb
config.eager_load_paths += ["#{Rails.root}/lib"]

就是这样!™

确保你把它放在这里,因为如果你把它放在 config/environments/development.rb 中,它会不起作用,例如.

Make sure you put it here since it will not work if you put it in config/environments/development.rb, for example.

确保为您的 /lib 代码删除所有 require 语句,因为 require 语句也会导致此解决方案不起作用.

Make sure your remove all the require statements for your /lib code since require statements will also cause this solution to not work.

这段代码隐含地需要你的代码,所以如果你做环境检查(这是不必要的)而不是上面的代码,你决定写这样的东西:

This code implicitly requires your code, so if you do environment checks (which are unnecessary) and instead of the above code, you decide to write something like this:

# in config/application.rb
config.eager_load_paths += ["#{Rails.root}/lib"] if Rails.env.development?

您应该注意旧的 require 语句,因为在这种情况下,所有非开发环境仍然需要它们.

you should watch out on the old require statements, since they are still required on all the non-development environments, in this scenario.

因此,如果您仍然决定进行环境检查,请确保对 require 语句进行逆向检查.否则你会被咬的!

So if you still decide to do environment checks, make sure you do inverse checks for require statements. Otherwise you'll get bitten!

require "beer_creator" unless Rails.env.development?

你可能认为写整段关于不必要的事情也是不必要的,但我认为在做不必要的事情时警告人们一些必要的事情也是必要的.

You might think that writing entire paragraph about something that's unnecessary is also unnecessary, but I think that warning people about something that's necessary when doing something unnecessary is also necessary.

如果您想了解有关此主题的更多信息,请查看这个小教程.

If you would like to know more about this topic, check out this little tutorial.

这篇关于Ruby on Rails 3 - 为每个请求重新加载 lib 目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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