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

查看:46
本文介绍了在开发模式下,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中,我有:

  • In my helper, FooBarHelper, I have:

    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天全站免登陆