简体   繁体   中英

Rails - HTTParty How to access params?

So i'm using a pretty standard setup to test HTTParty out:

  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

this works. I get 20 items back.

I iterate through them in the view like so:

<% @hotels.each do |game| %>
    <p>game name is:<%= game.to_query %> </p>
<% end %>

which results on the html file inside the browser to the following: (just 1 example taken)

game name is:id=69359&name=Sunken+Secrets

Obviously, i would like to render the name and the id like normal params, just like normally in Rails: <=% game.name %> for example

//Update:

When i run byebug, i get the following result back for @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"]}>

Any ideas if there is way to do this?

Thanks in advance!

I would expect HTTParty to return a hash. Therefore IMO this should work:

<% @hotels.each do |game| %>
    <p>game name is: <%= game['name'] %> </p>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM