在开发模式下,Rails3不会在lib中重新加载代码

时间:2022-01-29 23:34:30

THE SITUATION:

情况:

  1. I have code in lib/foo/bar.rb with a simple method defined as such:

    我在lib / foo / bar.rb中有一个代码,其定义如下:

    module Foo
      class Bar
        def test
          "FooBar"
        end
      end
    end
    
  2. In my helper, FooBarHelper, I have:

    在我的助手FooBarHelper中,我有:

    require `lib/foo/bar`
    module FooBarHelper
      def test_foo_bar
        fb = Foo::Bar.new
        fb.test
      end
    end
    
  3. In my view, I call this helper method like so:

    在我看来,我这样调用这个辅助方法:

    <%= test_foo_bar =>
    
  4. In my config/environments/development.rb, I added the directory to my config.autoload_paths:

    在我的config / environments / development.rb中,我将目录添加到了config.autoload_paths:

    config.autoload_paths += ["#{config.root}/lib/foo"]
    

THE PROBLEM:

问题:

When I change the return value of Foo::Bar.test to, for example, "MODIFIED FOOBAR", the original return value, "FooBar", is still being displayed on the view and not the new value.

当我将Foo :: Bar.test的返回值更改为例如“MODIFIED FOOBAR”时,原始返回值“FooBar”仍然显示在视图上而不是新值。

Since I'm in development mode, shouldn't the code reload the code on every request?

由于我处于开发模式,代码不应该在每个请求上重新加载代码吗?

Could someone tell me what I'm missing?

有人能告诉我我错过了什么吗?

Thanks!

谢谢!

4 个解决方案

#1


9  

They removed the lib folder the app root in Rails 3.

他们删除了Rails 3中app根的lib文件夹。

You can either add it back
config.autoload_paths << 'lib'
or you can use `require_dependency` in your helper.
module FooBarHelper
  require_dependency 'foo/bar'

  def test_foo_bar
    fb = Foo::Bar.new
    fb.test
  end
end

Both ways tell Rails that your file lib/foo/bar.rb should be autoloaded and subsequently, reloaded each request.

两种方式都告诉Rails你的文件lib / foo / bar.rb应该被自动加载,然后重新加载每个请求。

#2


25  

Previous answers does not work. Here is a working one: http://ileitch.github.com/2012/03/24/rails-32-code-reloading-from-lib.html

以前的答案不起作用。这是一个有效的工作:http://ileitch.github.com/2012/03/24/rails-32-code-reloading-from-lib.html

You have to use both:

你必须使用两者:

config.watchable_dirs['lib'] = [:rb]

and

require_dependency

but any config.autoload_paths based solution won't work in Rails ~> 3.2

但任何基于config.autoload_paths的解决方案都不适用于Rails~> 3.2

#3


2  

Autoloading code from the lib folder was intentionally disabled in rails3, see this ticket for more details.

在rails3中有意禁用lib文件夹中的自动加载代码,有关详细信息,请参阅此票证。

The workaround suggested by Samuel is a great start, however I found that certain environments still had difficulty finding the libraries in a testing environment (say being invoked from a cucumber scenario), and that including the root path, as suggested within the ticket and hinted by the original comment in application.rb was a more robust approach:

Samuel建议的解决方法是一个很好的开始,但我发现某些环境仍然很难在测试环境中找到库(比如从黄瓜场景中调用),并且包括根路径,如故障单中所示并暗示通过application.rb中的原始评论是一种更强大的方法:

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

#4


-1  

Why are you putting the require into the module, when using autoload_path you should not need to require the file at all, it should be working without, I think if you manually require the file afterwards, rails does not know when to load it again?

为什么要将require放入模块中,当使用autoload_path时根本不需要文件,它应该没有工作,我想如果你之后手动要求文件,rails不知道何时再加载它?

Something like this:

像这样的东西:

require `bar`

module FooBarHelper

  def test_foo_bar
    fb = Foo::Bar.new
    fb.test
  end

end

should work, no need for having the require inside your module.

应该工作,不需要在您的模块内有要求。

#1


9  

They removed the lib folder the app root in Rails 3.

他们删除了Rails 3中app根的lib文件夹。

You can either add it back
config.autoload_paths << 'lib'
or you can use `require_dependency` in your helper.
module FooBarHelper
  require_dependency 'foo/bar'

  def test_foo_bar
    fb = Foo::Bar.new
    fb.test
  end
end

Both ways tell Rails that your file lib/foo/bar.rb should be autoloaded and subsequently, reloaded each request.

两种方式都告诉Rails你的文件lib / foo / bar.rb应该被自动加载,然后重新加载每个请求。

#2


25  

Previous answers does not work. Here is a working one: http://ileitch.github.com/2012/03/24/rails-32-code-reloading-from-lib.html

以前的答案不起作用。这是一个有效的工作:http://ileitch.github.com/2012/03/24/rails-32-code-reloading-from-lib.html

You have to use both:

你必须使用两者:

config.watchable_dirs['lib'] = [:rb]

and

require_dependency

but any config.autoload_paths based solution won't work in Rails ~> 3.2

但任何基于config.autoload_paths的解决方案都不适用于Rails~> 3.2

#3


2  

Autoloading code from the lib folder was intentionally disabled in rails3, see this ticket for more details.

在rails3中有意禁用lib文件夹中的自动加载代码,有关详细信息,请参阅此票证。

The workaround suggested by Samuel is a great start, however I found that certain environments still had difficulty finding the libraries in a testing environment (say being invoked from a cucumber scenario), and that including the root path, as suggested within the ticket and hinted by the original comment in application.rb was a more robust approach:

Samuel建议的解决方法是一个很好的开始,但我发现某些环境仍然很难在测试环境中找到库(比如从黄瓜场景中调用),并且包括根路径,如故障单中所示并暗示通过application.rb中的原始评论是一种更强大的方法:

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

#4


-1  

Why are you putting the require into the module, when using autoload_path you should not need to require the file at all, it should be working without, I think if you manually require the file afterwards, rails does not know when to load it again?

为什么要将require放入模块中,当使用autoload_path时根本不需要文件,它应该没有工作,我想如果你之后手动要求文件,rails不知道何时再加载它?

Something like this:

像这样的东西:

require `bar`

module FooBarHelper

  def test_foo_bar
    fb = Foo::Bar.new
    fb.test
  end

end

should work, no need for having the require inside your module.

应该工作,不需要在您的模块内有要求。