Rails3 在开发模式下不重新加载 lib 中的代码 [英] Rails3 not reloading code in lib while in development mode

查看:34
本文介绍了Rails3 在开发模式下不重新加载 lib 中的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

  1. 我在 lib/foo/bar.rb 中有代码,其中定义了一个简单的方法:

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

module Foo
  class Bar
    def test
      "FooBar"
    end
  end
end

  • 在我的助手 FooBarHelper 中,我有:

    require `lib/foo/bar`
    module FooBarHelper
      def test_foo_bar
        fb = Foo::Bar.new
        fb.test
      end
    end
    

  • 在我看来,我这样称呼这个辅助方法:

  • In my view, I call this helper method like so:

    <%= test_foo_bar =>
    

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

  • In my config/environments/development.rb, I added the directory to my config.autoload_paths:

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

  • <小时>

    问题:

    当我把Foo::Bar.test的返回值改成例如"MODIFIED FOOBAR"时,原来的返回值,"FooBar",仍然显示在视图上,而不是新值.

    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.

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

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

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

    Could someone tell me what I'm missing?

    谢谢!

    推荐答案

    他们删除了 Rails 3 中应用根目录的 lib 文件夹.

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

    config.autoload_paths << 'lib'
    

    或者你可以在你的助手中使用`require_dependency`.

    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
    

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

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

    这篇关于Rails3 在开发模式下不重新加载 lib 中的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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