How do I make the code in lib/ automatically reload when the file changes?

后端 未结 4 1243
误落风尘
误落风尘 2020-12-30 05:39

This is a follow up to this question. During development I have to restart the rails app everytime I change the code in lib/ in order for the code changes to take effect. Ho

相关标签:
4条回答
  • 2020-12-30 05:55

    If you already did the previous approaches but doesn't works (like my case), try with config.reload_classes_only_on_change in development.rb.

    Rails 4.2 here ✋

    0 讨论(0)
  • 2020-12-30 05:57
    module ActsAsReloadable
      def self.included(base)
        ActiveSupport::Dependencies.explicitly_unloadable_constants << base.name if Rails.env == 'development'
      end
    end
    

    To use it, simply include ActsAsReloadable in your lib/* files and add config.autoload_paths += %W(#{config.root}/lib) in config/application.rb

    0 讨论(0)
  • 2020-12-30 05:59

    For Rails 3, vary the instructions given in the article from @science's answer. In your environments/development.rb file, add the lines:

    ActiveSupport::Dependencies.autoload_paths << File::join( Rails.root, 'lib')
    ActiveSupport::Dependencies.explicitly_unloadable_constants << 'MyModuleInLibFolder'
    

    Of course, substitute the name of your module for MyModuleInLibFolder.

    0 讨论(0)
  • 2020-12-30 06:13

    why not just enter

    load Rails.root + '/lib/your_lib.rb'
    
    0 讨论(0)
提交回复
热议问题