我有一个Rails应用程序,它使用了我正在积极开发的gem。如何指示应用程序在每次请求时重新加载宝石?
这个解决方案几乎可以工作,但由于某种原因,我不得不将其放入application.rb
,而不是environments/development.rb
,否则无法识别自动加载路径。
我添加了一些额外的东西,可以自动获取路径。
if Rails.env.development?
reload_gems = %w(my_gem other_gem) # names of gems which should autoreload
config.autoload_paths += Gem.loaded_specs.values.inject([]){ |a,gem| a += gem.load_paths if reload_gems.include? gem.name; a }
require 'active_support/dependencies'
ActiveSupport::Dependencies.explicitly_unloadable_constants += reload_gems.map { |gem| gem.classify }
end
可以使用gem 'my_gem', :path => '../my_gem'
添加本地宝石
您可以在应用程序的autoload
路径中添加到gem的路径。
因此,在config/application.rb中,在class Application < Rails::Application ... end
块中添加:
config.autoload_paths += %W(#{config.root}/vendor/gems/my_gem/lib)
config.autoload_paths += Dir["#{config.root}/vendor/gems/my_gem/lib/**/"]
然后,在运行开发服务器时,中的所有文件都应该重新加载。
对于引擎:
module Copycat
class Engine < ::Rails::Engine
if Rails.env.development?
config.to_prepare do
Rails.logger.debug "RELOADING COPYCAT"
require_dependency Copycat::Engine.root.join('lib', 'copycat').to_s
end
config.after_initialize do
# optional, without it will call `to_prepend` only when a file changes,
# not on every request
Rails.application.config.reload_classes_only_on_change = false
end
end
end
end
我刚刚发现很棒https://github.com/colinyoung/gem_reloader-对我有用!
超级简单的修复:
In yourApp/config/envirornments/development.rb:
YourApp::Application.configure do
# Make sure both of these two settings are set to false, add them if you can't find them
config.cache_classes = false
config.action_controller.perform_caching = false
#
#
# Other config settings...
#
#
end