因此,在Coursera课程中,我需要构建这个相当简单的应用程序来从外部api获取和显示数组。我在rails框架上使用了ruby。 (我使用的是Windows 10)
控制器 -
class CoursesController < ApplicationController
def index
@search_term = params[:looking_for] || 'jhu'
@courses = Coursera.for(@search_term)
end
end
模型
class Coursera
include HTTParty
default_options.update(verify: false) # Turn off SSL verification
base_uri 'https://api.coursera.org/api/courses.v1'
default_params fields: "photoUrl,description",q: "search"
format :json
def self.for term
get("",query: {query: term}) ["elements"]
end
end
视图无关紧要,因为这样可以正常工作。 但在我的其他应用程序中,我收到此错误 -
Errno::ECONNREFUSED: Failed to open TCP connection to :80 (Connection refused - connect(2) for nil port 80)
这是我遇到问题的另一个应用程序 -
控制器 -
class RecipesController < ApplicationController
def index
@search_term = params[:search] || 'chocolate'
@recipes = Recipe.for @search_term
end
end
模型 -
class Recipe
include HTTParty
default_options.update(verify: false) # Turn off SSL verification
key_value = ENV['FOOD2FORK_KEY']
hostport = ENV['FOOD2FORK_SERVER_AND_PORT'] || 'food2fork.com'
base_uri = "https://doesntmatter.com/api" #website mentioned here
#doesn't matter , I get the error nonetheless
default_params key: ENV['FOOD2FORK_KEY'] ,q: "search"
format :json
def self.for term
get("/search",query: {query: term}) ["recipes"]
end
end
我已尝试禁用所有可能的防火墙,在Windows防火墙中使用TCP取消阻止所有端口,但我仍然遇到相同的错误。任何想法如何解决这一问题 ?因为我不认为我的代码中存在问题......
答案 0 :(得分:4)
因为我不认为我的代码中存在问题......
问题在于您的代码。
您的代码中有多余的符号:
class Recipe
# some code here
base_uri = "https://doesntmatter.com/api"
^ # the equal symbol is redundant.
# remove it and all will works as expected
# ....
end