【发布时间】:2017-01-23 15:07:38
【问题描述】:
我正在使用 HTTParty Gem 从第三方 API 访问数据。
我已设置模型以成功检索/解析来自其他网站的数据。
我不知道的:控制器中需要什么代码才能让我在视图(最终是网站)中显示内容。
这是我的模型文件代表.rb 的代码
require 'rubygems'
require 'httparty'
class Representative < ApplicationRecord
include HTTParty
base_uri 'whoismyrepresentative.com'
default_params :output => 'json'
format :json
def self.find_by_zip(zip)
get('/getall_mems.php', :query => {:zip => zip})
end
end
puts Representative.find_by_zip(92651).inspect
这是我的 Json 结果:
<HTTParty::Response:0x7fa591c4a778 parsed_response={"results"=>[{"name"=>"Dana Rohrabacher", "party"=>"R", "state"=>"CA", "district"=>"48", "phone"=>"202-225-2415", "office"=>"2300 Rayburn House Office Building", "link"=>"http://rohrabacher.house.gov"}, {"name"=>"Darrell Issa", "party"=>"R", "state"=>"CA", "district"=>"49", "phone"=>"202-225-3906", "office"=>"2269 Rayburn House Office Building", "link"=>"http://issa.house.gov"}, {"name"=>"Barbara Boxer", "party"=>"D", "state"=>"CA", "district"=>"Junior Seat", "phone"=>"202-224-3553", "office"=>"112 Hart Senate Office Building", "link"=>"http://www.boxer.senate.gov"}, {"name"=>"Dianne Feinstein", "party"=>"D", "state"=>"CA", "district"=>"Senior Seat", "phone"=>"202-224-3841", "office"=>"331 Hart Senate Office Building", "link"=>"http://www.feinstein.senate.gov"}]}
那么此时我需要在代表控制器.rb 以及视图文件中放入什么?
谢谢
【问题讨论】:
标签: ruby-on-rails ruby httparty