如何自动加载要在控制器中使用的类

时间:2019-01-24 11:49:10

标签: ruby-on-rails

我在libs中有一个类,并尝试在控制器中使用它。但是我无法访问它。我尝试使用自动加载功能,但这不起作用,并且在生产模式下也不应该在Rails 5中运行,所以我想我不需要尝试这一功能。.我也尝试在我的产品中要求它控制器,但我猜语法不正确。我也想知道在哪里上课,因为我已经阅读了几种不同的看法。

config/application.rb
 require_relative 'boot'

 require 'rails/all'

 # Require the gems listed in Gemfile, including any gems
 # you've limited to :test, :development, or :production.
 Bundler.require(*Rails.groups)

 module Qipmatedevel
   class Application < Rails::Application
     # Initialize configuration defaults for originally generated Rails version.
     config.load_defaults 5.1
     config.active_job.queue_adapter = :sidekiq
     config.autoload_paths += %W(#{config.root}/lib)
     # Settings in config/environments/* take precedence over those specified here.
     # Application configuration should go into files in config/initializers
     # -- all .rb files in that directory are automatically loaded.
   end
 end

app/controller/imports_controller
class ImportsController < ApplicationController

  require 'lib/class_qip'

2 个答案:

答案 0 :(得分:1)

添加

  require './lib/class_qip.rb'

已修复

答案 1 :(得分:0)

仅将您的课程放在lib目录中,并加载为,

config.eager_load_paths << Rails.root.join('lib')

它同时适用于开发和生产环境。