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

时间:2019-01-10 20:59:27

标签: ruby-on-rails ruby api unirest

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

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

stock.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

stocks_controller.rb

class StocksController < ApplicationController
  def search
  end

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

非常感谢!

1 个答案:

答案 0 :(得分: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] }