Ruby:未定义的类局部变量或方法

时间:2022-10-08 23:54:09

I have what I assumed was a very simple method within my controller:

我假设我的控制器中有一个非常简单的方法:

class ReportsController < ApplicationController

  client = MWS.reports

  def request_all_listings
    begin
      parser = client.request_report('_GET_FLAT_FILE_OPEN_LISTINGS_DATA_', opts = {})
      @result = parser.parse["ReportRequestInfo"]["ReportProcessingStatus"]

      puts @result

    rescue Excon::Errors::ServiceUnavailable => e
      logger.warn e.response.message
      retry
    end
  end

  request_all_listings

end

This gives me the error:

这给了我一个错误:

 undefined local variable or method `request_all_listings' for ReportsController:Class

What am I doing wrong here? When I delete the def request_all_listings def and end lines and just have the begin/rescue/end my code works fine...

我在这里做错了什么?当我删除def request_all_listing def和结束行并让开始/拯救/结束时,我的代码可以正常工作……

2 个解决方案

#1


5  

request_all_listings is ambiguous. It's either a variable or a class method call. request_all_listings is an object method.

request_all_listings是模棱两可的。它要么是变量,要么是类方法调用。request_all_listing是一个对象方法。

To fix it, you need to either define request_all_listings as a class method.

要修复它,需要将request_all_listing定义为类方法。

def self.request_all_listings
    ...
end

Or create an object to call request_all_listings with.

或者创建一个对象来调用request_all_listing。

ReportsController.new.request_all_listings

In general it's bad form to do work when a class is loaded. This makes it impossible to load the class without it doing work and can slow things down and make it difficult to use and test.

一般来说,当一个类被加载时,工作是不好的。这使得在不做工作的情况下加载类是不可能的,并且会降低速度,使使用和测试变得困难。

Instead I'd suggest doing this work when an instance is loaded and caching it in a class instance variable.

相反,我建议在加载实例并将其缓存到类实例变量时进行此工作。

class Foo
  # class instance variable
  @all_listings = []

  # class method
  def self.request_all_listings
    puts "Calling request_all_listings"
    @all_listings = [1,2,3]

    return
  end

  # class method
  def self.all_listings
    request_all_listings if @all_listings.size == 0

    return @all_listings
  end

  # object method
  def all_listings
    return self.class.all_listings
  end
end

# request_all_listings is only called once for two objects
puts Foo.new.all_listings.inspect
puts Foo.new.all_listings.inspect

#2


0  

move the line request_all_listings into the constructor:

将request_all_listing行移动到构造函数中:

def initialize
    request_all_listings
end

When you create a ReportsController instance, initialize will automatically run:

当您创建一个ReportsController实例时,initialize将自动运行:

reports = ReportsController.new

#1


5  

request_all_listings is ambiguous. It's either a variable or a class method call. request_all_listings is an object method.

request_all_listings是模棱两可的。它要么是变量,要么是类方法调用。request_all_listing是一个对象方法。

To fix it, you need to either define request_all_listings as a class method.

要修复它,需要将request_all_listing定义为类方法。

def self.request_all_listings
    ...
end

Or create an object to call request_all_listings with.

或者创建一个对象来调用request_all_listing。

ReportsController.new.request_all_listings

In general it's bad form to do work when a class is loaded. This makes it impossible to load the class without it doing work and can slow things down and make it difficult to use and test.

一般来说,当一个类被加载时,工作是不好的。这使得在不做工作的情况下加载类是不可能的,并且会降低速度,使使用和测试变得困难。

Instead I'd suggest doing this work when an instance is loaded and caching it in a class instance variable.

相反,我建议在加载实例并将其缓存到类实例变量时进行此工作。

class Foo
  # class instance variable
  @all_listings = []

  # class method
  def self.request_all_listings
    puts "Calling request_all_listings"
    @all_listings = [1,2,3]

    return
  end

  # class method
  def self.all_listings
    request_all_listings if @all_listings.size == 0

    return @all_listings
  end

  # object method
  def all_listings
    return self.class.all_listings
  end
end

# request_all_listings is only called once for two objects
puts Foo.new.all_listings.inspect
puts Foo.new.all_listings.inspect

#2


0  

move the line request_all_listings into the constructor:

将request_all_listing行移动到构造函数中:

def initialize
    request_all_listings
end

When you create a ReportsController instance, initialize will automatically run:

当您创建一个ReportsController实例时,initialize将自动运行:

reports = ReportsController.new