如何包含App/Lib文件夹中的模块

发布于 2025-02-11 18:12:56 字数 1603 浏览 1 评论 0原文

尝试从App/Lib文件夹中存在的模块加载类时,我会遇到错误。

类进行更改

我根据模块下的IT控制器

class SchemasController < ApplicationController
  # include Schemas
  # require './app/lib/api'
  include Operator
end

,我必须包括:

# frozen_string_literal: true

module A
  module B
    class C
      @url = "https://something.com"

      def self.where(params = {})
        response = A::Connection.connect(@url).get('headers', params)

        response.body
      end
    end
  end
end

application.rb:

require_relative "boot"


# Pick the frameworks you want:
require 'action_controller/railtie'
require 'active_job/railtie'
require 'action_cable/engine'
require 'action_mailer/railtie'


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

module AnalyticsQueryBuilder
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 7.0
    config.autoload_paths += %W(#{config.root}/lib)
    # config.autoload_paths += %W(#{config.root}/lib)
    config.autoload_paths += Dir["#{config.root}/lib/**/"]
    config.before_configuration do
      env_file = File.join(Rails.root, 'config', 'local_env.yml')
      if File.exist?(env_file)
        YAML.safe_load(File.open(env_file)).each do |key, value|
          ENV[key.to_s] = value
        end
      end
    end
    Rails.logger = Logger.new($stdout)

    config.exceptions_app = routes
 
  end
end

如何将其包括

I am getting an error while trying to load a class from module which is present in app/lib folder.

I had made changes according to it

Controller

class SchemasController < ApplicationController
  # include Schemas
  # require './app/lib/api'
  include Operator
end

class under module which I have to include:

# frozen_string_literal: true

module A
  module B
    class C
      @url = "https://something.com"

      def self.where(params = {})
        response = A::Connection.connect(@url).get('headers', params)

        response.body
      end
    end
  end
end

application.rb:

require_relative "boot"


# Pick the frameworks you want:
require 'action_controller/railtie'
require 'active_job/railtie'
require 'action_cable/engine'
require 'action_mailer/railtie'


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

module AnalyticsQueryBuilder
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 7.0
    config.autoload_paths += %W(#{config.root}/lib)
    # config.autoload_paths += %W(#{config.root}/lib)
    config.autoload_paths += Dir["#{config.root}/lib/**/"]
    config.before_configuration do
      env_file = File.join(Rails.root, 'config', 'local_env.yml')
      if File.exist?(env_file)
        YAML.safe_load(File.open(env_file)).each do |key, value|
          ENV[key.to_s] = value
        end
      end
    end
    Rails.logger = Logger.new($stdout)

    config.exceptions_app = routes
 
  end
end

How to include it in Controller

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍孤女 2025-02-18 18:12:56

您是否尝试过之类的操作,包括API :: AnalyticsQueryBuilderMetadataTaService ::操作员

Have you tried doing something like include Api::AnalyticsQueryBuilderMetadataService::Operator ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文