我试图找出这个问题:“没有隐式的字符串转换为整数”。
问题在这一行<% if == x"symbol“%>中
代码:
<% for x in @coins %>
<% for coin in @my_coins %>
<% if coin == x["symbol"]%>
<%= x["name"]%>
<%end%>
<%end%>
<%end%>变量
class HomeController < ApplicationController
def index
require 'net/http'
require 'json'
@url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest?start=1&limit=100&CMC_PRO_API_KEY=MyKey'
@uri = URI(@url)
@response = Net::HTTP.get(@uri)
@coins = JSON.parse(@response)
@my_coins = ["BTC", "XRP", "ADA", "ETH", "USDT"]
end
def about
end
def lookup
end
endAPI的第一行和第一个示例:
"data":
[
{
"id":1,"name":"Bitcoin",
"symbol":"BTC",
"slug":"bitcoin",
"num_market_pairs":9676,
"date_added":"2013-04-28T00:00:00.000Z",
"tags":["mineable","pow","sha-256","store-of-value","state-channels","coinbase-ventures-portfolio","three-arrows-capital-portfolio","polychain-capital-portfolio"],
"max_supply":21000000,
"circulating_supply":18629531,
"total_supply":18629531,"platform":null,"cmc_rank":1,"last_updated":"2021-02-15T15:54:02.000Z",
"quote":{"USD":{"price":47907.843633498785,
"volume_24h":79376349219.00912,
"percent_change_1h":-0.13222586,
"percent_change_24h":-1.5128616,
"percent_change_7d":10.59948497,
"percent_change_30d":28.8994407,
"market_cap":892500658113.4182,
"last_updated":"2021-02-15T15:54:02.000Z"}}},发布于 2021-02-16 03:48:00
问题的根源在于x不是一个哈希。我想它是一个数组。
例如,这将给出相同的错误:
[]["symbol"]基本上,当x是一个数组时,[]方法的输入需要一个整数,因此会尝试将传入的内容转换为整数。该错误发生在转换失败时。
https://stackoverflow.com/questions/66214360
复制相似问题