rails config / initializers是针对每个请求执行的

时间:2018-04-18 11:40:56

标签: ruby-on-rails

我的rails应用程序需要初始化程序,我将它们放在config / initializers文件夹中。我需要它们运行一次,并且只在开始时运行,即服务器启动时,但rails会为来自客户端的每个请求执行它们。因此,每次调用控制器时,所有初始化程序都由rails执行。我在开发环境中工作。这就是development.rb的样子:

  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports.
  config.consider_all_requests_local = true

  config.reload_classes_only_on_change = true

  # Enable/disable caching. By default caching is disabled.
  if Rails.root.join('tmp/caching-dev.txt').exist?
    config.action_controller.perform_caching = true

    config.cache_store = :memory_store
    config.public_file_server.headers = {
      'Cache-Control' => "public, max-age=#{2.days.seconds.to_i}"
    }
  else
    config.action_controller.perform_caching = false

    config.cache_store = :null_store
  end

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  config.action_mailer.perform_caching = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Suppress logger output for asset requests.
  config.assets.quiet = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true

  # Use an evented file watcher to asynchronously detect changes in source code,
  # routes, locales, etc. This feature depends on the listen gem.
  config.file_watcher = ActiveSupport::EventedFileUpdateChecker

有什么想法吗?

示例初始化文件:

module Constants 

    GPK_window_size = 256


    Key_path = "./some_path"

    def self.gpk_window_size
        return GPK_window_size 
    end

end

Rails.logger.debug "Constants file executed.."

0 个答案:

没有答案