如何在rails 5中将api的结果显示给您的视图

时间:2017-08-30 14:50:30

标签: ruby-on-rails

我在运行应用程序时似乎遇到错误:

getaddrinfo: Name or service not known

我认为错误是因为应用程序与使用服务检索api的服务器之间没有连接。我使用RestClient gem来访问请求。

#services/get_games.rb
class GetGames
  def self.get_games
    response = RestClient::Request.execute(
      method: :get,
      url: 'https://api-2445582011268.apicast.io.json',
      headers: {user_key: ENV["IGDB_API"], accept:'application/json'}
    )
  end
end

#games_controller
def index
    @response = JSON.parse(GetGames.get_games)
    @games = Game.all
end

1 个答案:

答案 0 :(得分:1)

我认为如果您取消.json中的url,错误就会消失。