Skip to content

How to reload a mounted rack app without killing rails server in development

pifleo edited this page Jan 3, 2013 · 1 revision

If you get the following error when editing files in your rails application:

 ArgumentError at xxxxxxxx
 A copy of MyResource has been removed from the module tree but is still active!

And dav4rack is mounted as a route in your rails app

  # config/routes.rb
  mount DAV4Rack::Handler.new(
    :root => Rails.root.to_s + "/public/uploads",
    :root_uri_path => '/',
    :resource_class => MyResource
  ), :at => '/', :constraints => {:subdomain => "docs"}

You can add an initializer to force reload routes on each request if a file has changed

# config/initializers/lib_reload.rb
#
# Clear Dav4rack ressource on development
# http://stackoverflow.com/questions/3282655/ruby-on-rails-3-reload-lib-directory-for-each-request
if Rails.env.development?

  lib_ruby_files = Dir.glob(File.join("app/**", "*.rb"))
  lib_reloader ||= ActiveSupport::FileUpdateChecker.new(lib_ruby_files) do
    Rails.application.reload_routes!
    # lib_ruby_files.each do |lib_file|
    #  silence_warnings { require_dependency(lib_file) }
    # end
  end

  ActionDispatch::Callbacks.to_prepare do
    lib_reloader.execute_if_updated
  end

end