简体   繁体   English

我可以仅通过调用方法从控制器访问Module类方法吗?

[英]can i access Module class method from controller just by calling the method?

I have the following module inside lib/api_client: 我在lib / api_client中有以下模块:

request.rb request.rb

module APIClient

  class Request
    require 'digest/sha1'
    require "net/http"
    require "uri"

    def self.venues_response
      ...
    end

  end
end

wich i include in my controller class like this: 我将这样包含在我的控制器类中:

class VenuesController < ApplicationController

  include APIClient

I'd like to access it's methods from inside venues_controller like this: 我想像这样从avenues_controller内部访问它的方法:

venues_response

But i get the following error: 但我得到以下错误:

undefined method `venues_response' for #<VenuesController:0x007f9ee058ad00>

And if i try this: 如果我尝试这样做:

Request.venues_response

I get: 我得到:

uninitialized constant VenuesController::Request

The only way seems to be this: 唯一的办法似乎是这样的:

APIClient::Request.venues_response

Is there a way to make the call simpler? 有没有一种方法可以简化通话? Am i missing something? 我想念什么吗?

EDIT: 编辑:

Forgot to mention autoloading is already added on application.rb: 忘记提及自动加载已添加到application.rb上:

config.autoload_paths += %W(#{config.root}/lib)

request.rb重命名为api_client.rb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM