我正在尝试通过Rails应用程序中的RapidAPI从IEX交易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
stocks_controller.rb
class StocksController < ApplicationController
def search
end
def result
@stock = params[:stock]
@stock.get_company unless @stock.nil?
end
end
提前非常感谢!
尝试
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] }