开发者

How can I reload files in /lib on every request?

Rails loads controllers, helpers and models on each request.

My controllers have a bunch of modules which include methods for shared actions

Each time I change the modules, I have to restart Rails for the changes in the actions to take effect

Any idea how I can tell rails to reload these modules too?


Update:

My directory structure is like so:

  app/
    controllers/
      app1/
        users_controller.rb
      app2/
        users_controller.rb

  lib/
    templates/
      controllers/
        users_controller_template.rb

Both App1::UsersController and App2::UsersController load UsersControllerTeplate like so:

# app/controllers/app1/users_controller.rb
class App1::UsersController < App1::ApplicationController
  require "templates/controllers/users_controller_template"
  include Templates::Controllers::UsersControllerTemplate
end

# templates/controllers/users_controller_template.rb
module Templates::Controllers::UsersControllerTemplate

  def self.included(base)
    base.class_eval do
      # some class macros called here
    end
  end
  
  # actions defined here
  def index
  end

end

In application.rb I've added:

config.autoload_paths += %W{ #{config.root}/lib/templates/ }

But I still have to reload the server to see changes made in 开发者_StackOverflow中文版users_controller_template.rb

Any ideas?


You can add them to your autoload_paths in your application.rb file and they will be re-loaded automatically along with the models and controllers.

config.autoload_paths << "#{config.root}/lib"


In config/application.rb:

config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]

This will reload any Ruby files in your lib folder as well as any Ruby files within subdirectories of lib.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜