undefined method name for #<StockQuote::NoDataForStockError:0x007fea46e5fd70>
我正在使用此处的'stock_quote'宝石来获取有关各种股票的数据https://github.com/tyrauber/stock_quote
我有时在刷新浏览器时会出现错误,但有时候应用程序运行正常而我没有更改任何代码。我认为主要问题是错误的请求错误,但我不知道该怎么做。我是编程新手,所以我不确定解决错误需要哪一个,所以我只提供了所有这些。就像我说的那样,错误有时只会出现,这非常令人沮丧。希望有人可以帮助我,谢谢!
以下是我的浏览器中显示的错误页面:
Showing /Users/frank/Desktop/projects/final-investing/app/views/stock/index.html.erb where line #37 raised:
undefined method 'name' for #<StockQuote::NoDataForStockError:0x007fea433235e0>
Extracted source (around line #9):
7. def self.new_stock_lookup(ticker)
8. looked_up_stock = StockQuote::Stock.quote(ticker)
9. return nil unless looked_up_stock.name
10.
11. new_stock = new(ticker:looked_up_stock.symbol,
12. name:looked_up_stock.name,
以下是控制台错误:
BAD REQUEST] https://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20yahoo.finance.quotes%20WHERE%20symbol%20IN%20('FB')&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=
Rendered stock/_popular_stocks.html.erb (2271.7ms)
Rendered stock/index.html.erb within layouts/application (2277.7ms)
Completed 500 Internal Server Error in 2283ms (ActiveRecord: 0.0ms)
这是我的股票模型
class Stock < ApplicationRecord
def self.new_stock_lookup(ticker)
looked_up_stock = StockQuote::Stock.quote(ticker)
return nil unless looked_up_stock.name
new_stock = new(ticker:looked_up_stock.symbol,
name:looked_up_stock.name,
current_price:looked_up_stock.last_trade_price_only,
one_year_estimate:looked_up_stock.oneyr_target_price,
percent_change: (looked_up_stock.oneyr_target_price -
looked_up_stock.last_trade_price_only) / looked_up_stock.last_trade_price_only)
new_stock
end