尝试通过API从站点获取数据

时间:2016-07-26 15:50:44

标签: ruby-on-rails api

我正在尝试从this site获取数据。这些是我的控制器,模型和视图:

模型:

 require 'httparty'
 class Recipe
 include HTTParty
 default_options.update(verify: false)
 ENV["FOOD2FORK_KEY"] = 'c1dfc441de0f628c2fa35f03320957a3'
 default_params key: ENV['FOOD2FORK_KEY']
 hostport = ENV['FOOD2FORK_SERVER_AND_PORT'] || 'www.food2fork.com'
 base_uri "http://#{hostport}/api/search?"
 format :json

 def self.for term
 get("", query: { query: term})["recipes"]
 end
end

控制器:

class RecipesController < ApplicationController
  def index
    @search_term = params[:looking_for] || 'chocolate'
    @recipes = Recipe.for(@search_term)
  end
end

视图:

<p>Powered By Food2Fork.com</p>
<%= @search_term%>
<% if (@recipes.nil?) %>
<h2>Cant find recipe</h2>

<% else %> 
<table border="1">
 <tr>
    <th>Image</th>
    <th>Name</th>
    <th>Rank</th>
 </tr>
 <% @recipes.each do |course| %>
 <tr class=<%= cycle('even', 'odd') %>>
    <td><%= link_to(image_tag(course["image_url"], height: '100', width: '100'), course["f2f_url"])%></td>
    <td><%= link_to(course["title"], course["f2f_url"]) %></td>
    <td><%= course["social_rank"] %></td>
 </tr>
 <% end %>
</table>
<% end %>

我的程序没有错误,但总是有一些输出,search_term并不重要,我不知道该怎么办。我使用API​​错了吗? :/

如果你给我一个MINUS,请评论我做错了什么,这样我就能提高自己。谢谢。

0 个答案:

没有答案
相关问题