简体   繁体   中英

Accessing an API

I'm trying to build a Rails app where I have to access the Zomato API

Could anybody guide me on how to create the Controller and View. Suppose I want to get the name of the restaurant from this URL: Restaurant , how do I write the controller and views for it. Currently, I'm using HTTParty gem.

This is my controller code:

def restaurants    
  @response = HTTParty.get("https://api.zomato.com/v1/restaurant.json/773", :headers => {"X-Zomato-API-Key" => "MYAPIKEY"})     
  puts response.body, response.code, response.message, response.headers.inspect       
  @categories = Category.where(:category_name => 'Restaurants')      
end

And I'm trying to get the response object in the view:

<%= @response.each do |item| %>
  <td><%= item.id %></td>
  <td><%= item.name %></td>
  <td><%= item.url %></td>
<% end %>

So, this shows the following error:

undefined method 'id' for ["id", 773]:Array

It's a REST API, so you can write up a few ActiveResource classes and use it much like active record. Try reading up on the ActiveResource documentation.

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