0

我正在尝试通过 Rails 应用程序中的 RapidAPI 从 IEX Trading API 中提取数据。我创建了一个搜索表单,其中将使用查询来获取数据,但是即使在控制台中运行良好,也会出现以下错误消息(在此示例中查询为“FB”):

“FB”的未定义方法“get_company”:字符串

股票.rb

class Stock < ApplicationRecord
  validates :ticker, uniqueness: true

  def self.get_company
    response = Unirest.get "https://investors-exchange-iex-trading.p.rapidapi.com/stock/#{@stock}/company",
    headers: {
      "X-RapidAPI-Key" => [xxx]
    }
    company = response.body
    @stock = Stock.create(
      ticker: company["symbol"],
      name: company["companyName"],
      exchange: company["Exchange"],
      sector: company["industry"],
      website: company["website"],
      description: company["description"]
    )
  end
end

stock_controller.rb

class StocksController < ApplicationController
  def search
  end

  def result
    @stock = params[:stock]
    @stock.get_company unless @stock.nil?
  end
end

首先十分感谢!

4

1 回答 1

0

尝试

def result
     @stock = params[:stock]
     Stock.get_company(@stock) unless @stock.nil? end

def self.get_company(stock)
@stock = stock
 response = Unirest.get "https://investors-exchange-iex-trading.p.rapidapi.com/stock/#{@stock}/company", headers: { "X-RapidAPI-Key" => [xxx] }
于 2019-01-10T21:48:55.220 回答