Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何使用考拉Facebook图形API?_Ruby_Ruby On Rails 3_Facebook Graph Api_Hash_Hashmap - Fatal编程技术网

Ruby 如何使用考拉Facebook图形API?

Ruby 如何使用考拉Facebook图形API?,ruby,ruby-on-rails-3,facebook-graph-api,hash,hashmap,Ruby,Ruby On Rails 3,Facebook Graph Api,Hash,Hashmap,我是Rails新手。我想使用考拉的图形API 在我的控制器中 @graph = Koala::Facebook::API.new('myFacebookAccessToken') @hello = @graph.get_object("my.Name") 当我这样做的时候,我会得到这样的东西 { "id"=>"123456", "name"=>"First Middle Last", "first_name"=>"First", "middle_name"=>"M

我是Rails新手。我想使用考拉的图形API

在我的控制器中

@graph = Koala::Facebook::API.new('myFacebookAccessToken')
@hello = @graph.get_object("my.Name")
当我这样做的时候,我会得到这样的东西

{
"id"=>"123456", 
"name"=>"First Middle Last", 
"first_name"=>"First", 
"middle_name"=>"Middle", 
"last_name"=>"Last", 
"link"=>"http://www.facebook.com/MyName", 
"username"=>"my.name", 
"birthday"=>"12/12/1212", 
"hometown"=>{"id"=>"115200305133358163", "name"=>"City, State"}, "location"=>{"id"=>"1054648928202133335", "name"=>"City, State"}, 
"bio"=>"This is my awesome Bio.", 
"quotes"=>"I am the master of my fate; I am the captain of my soul. - William Ernest Henley\r\n\r\n"Don't go around saying the world owes you a living. The world owes you nothing. It was here first.\" - Mark Twain", 
"work"=>[{"employer"=>{"id"=>"100751133333", "name"=>"Company1"}, "position"=>{"id"=>"105763693332790962", "name"=>"Position1"}, "start_date"=>"2010-08", "end_date"=>"2011-07"}],
"sports"=>[{"id"=>"104019549633137", "name"=>"Sport1"}, {"id"=>"103992339636529", "name"=>"Sport2"}], 
"favorite_teams"=>[{"id"=>"105467226133353743", "name"=>"Fav1"}, {"id"=>"19031343444432369133", "name"=>"Fav2"}, {"id"=>"98027790139333", "name"=>"Fav3"}, {"id"=>"104055132963393331", "name"=>"Fav4"}, {"id"=>"191744431437533310", "name"=>"Fav5"}], 
"favorite_athletes"=>[{"id"=>"10836600585799922", "name"=>"Fava1"}, {"id"=>"18995689436787722", "name"=>"Fava2"}, {"id"=>"11156342219404022", "name"=>"Fava4"}, {"id"=>"11169998212279347", "name"=>"Fava5"}, {"id"=>"122326564475039", "name"=>"Fava6"}], 
"inspirational_people"=>[{"id"=>"16383141733798", "name"=>"Fava7"}, {"id"=>"113529011990793335", "name"=>"fava8"}, {"id"=>"112032333138809855566", "name"=>"Fava9"}, {"id"=>"10810367588423324", "name"=>"Fava10"}], 
"education"=>[{"school"=>{"id"=>"13478880321332322233663", "name"=>"School1"}, "type"=>"High School", "with"=>[{"id"=>"1401052755", "name"=>"Friend1"}]}, {"school"=>{"id"=>"11482777188037224", "name"=>"School2"}, "year"=>{"id"=>"138383069535219", "name"=>"2005"}, "type"=>"High School"}, {"school"=>{"id"=>"10604484633093514", "name"=>"School3"}, "year"=>{"id"=>"142963519060927", "name"=>"2010"}, "concentration"=>[{"id"=>"10407695629335773", "name"=>"c1"}], "type"=>"College"}, {"school"=>{"id"=>"22030497466330708", "name"=>"School4"}, "degree"=>{"id"=>"19233130157477979", "name"=>"c3"}, "year"=>{"id"=>"201638419856163", "name"=>"2011"}, "type"=>"Graduate School"}], 
"gender"=>"male", 
"interested_in"=>["female"], 
"relationship_status"=>"Single", 
"religion"=>"Religion1", 
"political"=>"Political1", 
"email"=>"somename@somecompany.com", 
"timezone"=>-8, 
"locale"=>"en_US", 
"languages"=>[{"id"=>"10605952233759137", "name"=>"English"}, {"id"=>"10337617475934611", "name"=>"L2"}, {"id"=>"11296944428713061", "name"=>"L3"}], 
"verified"=>true,
"updated_time"=>"2012-02-24T04:18:05+0000"
}
如何在视图中以良好的格式显示整个哈希

这就是我从中学到的。。 在我看来

<% @hello.each do |key, value| %>
<li><%=h "#{key.to_s} : #{value.to_s}" %></li>
<% end %>
如果我只是说
教育[学校][名称]
来显示就读学校的列表,也是为了教育

我得到的错误是
无法将字符串转换为整数

我也尝试在我的控制器中执行此操作,但我得到了相同的错误

@fav_teams = @hello["favorite_teams"]["name"]
另外,我如何将所有这些保存到数据库中。。就像所有学校的名单一样。。不是他们的身份证吗

更新:

我计划保存到数据库的方式是。。假设对于一个用户模型,我想将其保存到数据库中为:facebook\u id,:facebook\u name,:facebook\u firstname,,,:facebook\u homely。。这里我只想保存名字。。。说到教育。。我想节约。。学校、注意力和类型。。我不知道怎样才能做到这一点


期待帮助!谢谢

要以漂亮的打印方式显示哈希,请使用gem“”。 将此添加到您的文件:

gem'awesome\u print'

然后运行:
bundle安装

然后,在您的视图中,您可以添加:


如何存储在数据库中的问题需要更多关于您计划如何使用它的信息,但至少您可以创建一个模型,在该模型上添加一个“facebook_数据”(类型为“文本”),然后对其进行序列化(在模型文件顶部附近添加这行:
serialize:facebook_数据
)。然后,您可以将散列(
@hello
在本例中)分配给模型的“facebook\u data”属性,然后保存模型。但是,通过这种方式,您将无法很容易地查询数据库中此facebook数据的各个属性。

您只需执行
@hello[“name”]
即可,然后它将为您提供名称的值您的
@hello
对象应属于
考拉::facebook::API::GraphCollection
类或类似类。您应该能够循环遍历此对象,如您的问题所示。至于将哪些代码放入循环中以帮助您将记录保存到数据库,假设您的rails用户模型类名为
user
,请尝试以下操作:

@hello.each do |h|
  u = User.where(:facebook_id => h["id"]).first_or_initialize
  u.update_attributes(
    :name => h["name"],
    :first_name => h["first_name"],
    :hometown_city => h["hometown"]["name"].split(",").first,
    :hometown_state => h["hometown"]["name"].split(",").last.strip
    # ETC, ETC
  )
end

对于家乡和教育字段,您只需要以正确的方式遍历ruby哈希。有关更多信息,请参阅。

非常感谢您的回答!我计划保存到数据库的方式是。。假设对于一个用户模型,我想将其保存到数据库中为:facebook\u id,:facebook\u name,:facebook\u firstname,,,:facebook\u homely。。这里我只想保存名字。。。说到教育。。我想节约。。学校、注意力和类型。。我不知道怎样才能做到这一点。。如果你能帮我回答,我将不胜感激!当然,这只是普通的rails模型。例如,您可以在rails中创建一个教育模型,其中包含school、concentration和school\u类型字段(“type”字段名最好避免,因为rails在内部使用):
rails生成模型教育school:string concentration:string school\u类型:string
。然后迁移数据库:
rakedb:migrate
。在您的代码中,您可以创建一个新的教育模型并为其分配以下值:
ed=Education.new(school:@hello['Education'][0]['school'])
等。您应该查看Rails模型和关联,这里有一个指向Rails模型和关联指南的链接:您是否无意中发布了某人的(用户名为MyName)Facebook信息?可能最好改成某个测试用户。
@hello.each do |h|
  u = User.where(:facebook_id => h["id"]).first_or_initialize
  u.update_attributes(
    :name => h["name"],
    :first_name => h["first_name"],
    :hometown_city => h["hometown"]["name"].split(",").first,
    :hometown_state => h["hometown"]["name"].split(",").last.strip
    # ETC, ETC
  )
end