ruby - 如何根据每个请求重新加载/lib 中的文件?

标签 ruby ruby-on-rails-3 module

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/

相关文章:

javascript - NodeJS 使用另一个文件中的类

ruby - 如何在 ruby​​ 脚本中使用 Net::FTP 将远程 FTP 服务器上的文件移动到同一 FTP 服务器上的目录

ruby-on-rails - 如何正确诊断 500 错误(Rails、Passenger、Nginx、Postgres)

ruby-on-rails - Rails Active Record 在 before_create 回调中取消(保存或创建)而不引发异常

ruby-on-rails-3 - find_in_batches 仅适用于基于整数的主键吗?

ruby-on-rails - 将网页或 nokogiri 文档保存到数据库字段

python - 如何在不在 PyCharm 中重新加载包的情况下在 Python 控制台中进行调试?

module - Julia 列出模块提供的所有函数

文件中的 Ruby 空行不会删除

ruby-on-rails - Rspec: ||= 运算符重复计算代码