[英]Rails3 not reloading code in lib while in development mode
情況:
我在lib/foo/bar.rb
有代碼, lib/foo/bar.rb
有一個簡單的方法定義如下:
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
在我看來,我稱之為輔助方法如下:
<%= test_foo_bar =>
在我的config/environments/development.rb
,我將目錄添加到了config.autoload_paths
:
config.autoload_paths += ["#{config.root}/lib/foo"]
問題:
當我將Foo::Bar.test
的返回值更改為例如"MODIFIED FOOBAR"
,原始返回值"FooBar"
仍然顯示在視圖上而不是新值。
由於我處於開發模式,代碼不應該在每個請求上重新加載代碼嗎?
有人能告訴我我錯過了什么嗎?
謝謝!
以前的答案不起作用。 這是一個有效的工作: http : //ileitch.github.com/2012/03/24/rails-32-code-reloading-from-lib.html
你必須使用兩者:
config.watchable_dirs['lib'] = [:rb]
和
require_dependency
但任何基於config.autoload_paths
的解決方案都不適用於Rails~> 3.2
他們刪除了Rails 3中app根的lib
文件夾。
config.autoload_paths << 'lib'
或者您可以在助手中使用`require_dependency`。
module FooBarHelper require_dependency 'foo/bar' def test_foo_bar fb = Foo::Bar.new fb.test end end
兩種方式都告訴Rails你的文件lib/foo/bar.rb
應該被自動加載,然后重新加載每個請求。
為什么要將require放入模塊中,當使用autoload_path時根本不需要文件,它應該沒有工作,我想如果你之后手動要求文件,rails不知道何時再加載它?
像這樣的東西:
require `bar`
module FooBarHelper
def test_foo_bar
fb = Foo::Bar.new
fb.test
end
end
應該工作,不需要在你的模塊內有要求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.