Rails 会根据每个请求加载 Controller 、助手和模型。
我的 Controller 有一堆模块,其中包括共享操作的方法
每次更改模块时,我都必须重新启动 Rails 以使操作中的更改生效
知道如何告诉 Rails 重新加载这些模块吗?
更新:
我的目录结构是这样的:
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
在 application.rb 我添加了:
config.autoload_paths += %W{ #{config.root}/lib/templates/ }
但我仍然需要重新加载服务器才能看到 users_controller_template.rb 中所做的更改
有什么想法吗?
最佳答案
您可以将它们添加到您的 application.rb 文件中的 autoload_paths
,它们将与模型和 Controller 一起自动重新加载。
config.autoload_paths << "#{config.root}/lib"
关于ruby - 如何根据每个请求重新加载/lib 中的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6216970/