main:Object(NameError)的未定义局部变量或方法'api'

时间:2018-09-28 13:23:25

标签: ruby-on-rails api

大家好,我是RoR的新手,并且有疑问, 当我尝试运行 > ruby​​ api_controller.rb

时,我在终端上收到此错误

main:Object(NameError)的未定义局部变量或方法'api'

这是我的代码:

 require 'rubygems'  
 require 'httparty'

 url = https://api.coinmarketcap.com/v2/listings/ 
 response = HTTParty.get(url) 
 response.parsed_response


 class Coinmarketcap
    include HTTParty
    base_uri 'api.coinmarketcap.com'

   def listings
     self.class.get('/v2/listings/')
   end 
 end


 coinmarketcap = Coinmarketcap.new 
 puts coinmarketcap.listings

 coinmarketcap.listings.each do |post|   
  #puts "Id: #{post['id']        
 end

1 个答案:

答案 0 :(得分:1)

缺少引号的网址

应该是:

url = 'https://api.coinmarketcap.com/v2/listings'

我复制/粘贴了您的代码,并且可以进行上述修改