【发布时间】:2019-12-25 17:18:25
【问题描述】:
所以我正在使用一个非常标准的设置来测试 HTTParty:
def index
@hotels = HTTParty.get('https://api-v3.igdb.com/games/',
:headers =>{"Content-Type" => "application/json", "user-key" => "super_secret_key"},
:query => {:fields => "name", :limit => "20"})
end
这行得通。我拿回了 20 件物品。
我像这样在视图中遍历它们:
<% @hotels.each do |game| %>
<p>game name is:<%= game.to_query %> </p>
<% end %>
浏览器内的 html 文件结果如下: (仅举 1 个例子)
游戏名称为:id=69359&name=Sunken+Secrets
显然,我想像普通参数一样呈现名称和 id,就像在 Rails 中一样:例如
//更新:
当我运行 byebug 时,@hotels 得到以下结果
> <HTTParty::Response:0x7ff207cd0270 parsed_response=[{"id"=>69359, "name"=>"Sunken Secrets"}, {"id"=>69530, "name"=>"Dai Senryaku VII:
> Modern Military Tactics"}, {"id"=>81332, "name"=>"Stick Fighter II"},
> {"id"=>50649, "name"=>"Who Wants to be a Millionaire: 1st Edition"},
> {"id"=>22618, "name"=>"Battleground 7: Bull Run"}, {"id"=>104945,
> "name"=>"Woodpunk"}, {"id"=>91579, "name"=>"Racing Live"},
> {"id"=>22636, "name"=>"Call of Duty: Black Ops - Annihilation"},
> {"id"=>88041, "name"=>"Highway Wars"}, {"id"=>31258,
> "name"=>"Slymes"}, {"id"=>90512, "name"=>"Asian Riddles 4"},
> {"id"=>105842, "name"=>"Robots Vs Zombies: Transform To Race And
> Fight"}, {"id"=>85450, "name"=>"Transformers Prime: The Game"},
> {"id"=>104748, "name"=>"Space station - build your own ISS"},
> {"id"=>111593, "name"=>"Oppai Puzzle"}, {"id"=>42610,
> "name"=>"Hebereke's Popoon"}, {"id"=>105254, "name"=>"Codenames"},
> {"id"=>91756, "name"=>"Aban Hawkins & the 1000 SPIKES"}, {"id"=>2057,
> "name"=>"Out Run 2019"}, {"id"=>93987, "name"=>"Pi Story"}],
> @response=#<Net::HTTPOK 200 OK readbody=true>,
> @headers={"date"=>["Wed, 25 Dec 2019 18:04:48 GMT"],
> "content-type"=>["application/json;charset=utf-8"],
> "content-length"=>["1259"], "connection"=>["close"],
> "set-cookie"=>["__cfduid=de604d7fc295fb415e047830a8ce304281577297088;
> expires=Fri, 24-Jan-20 18:04:48 GMT; path=/; domain=.igdb.com;
> HttpOnly; SameSite=Lax"], "x-count"=>["124726"], "via"=>["1.1 vegur"],
> "cf-cache-status"=>["DYNAMIC"],
> "strict-transport-security"=>["max-age=15552000; preload"],
> "x-content-type-options"=>["nosniff"], "expect-ct"=>["max-age=604800,
> report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""], "server"=>["cloudflare"], "cf-ray"=>["54acbd52ae077cb2-MUC"]}>
如果有办法做到这一点有什么想法吗?
提前致谢!
【问题讨论】:
-
为什么要转换成查询?
-
我没有。我只想渲染数据。删除 to_query 会导致同样的问题。
-
你调用“to_query”。您检查返回的数据了吗?
-
我已经更新了帖子并添加了 byebug 日志。也许这可能会有所帮助?
-
那么它在“parsed_response”中?
标签: ruby-on-rails json ruby