I am trying to pull data from IEX Trading API via RapidAPI in my Rails app. I have created a search form where the query will be used to obtain the data, however the following error message appears even though it works fine in the console (with the query being "FB" in this example):
undefined method 'get_company' for "FB":String
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
Thanks very much in advance!
Try
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] }