[英]How can I convert Facebooks Graph API JSON data to find a user that already exists in my database?
好吧,我做了一些研究,但還是沒有運氣...
我將首先解釋我要做的事情:
我有一個僅通過Facebook登錄的應用程序。 我希望這些用戶也可以看到他們的朋友列表,這些朋友也在使用我的應用程序。
我已經路由users#index
返回當前在應用程序上的用戶朋友列表。 這顯示在JSON Data
。 該數據按以下方式返回: "name"=>"Example Name", "id"=>"32938293828393"
。 在我的數據庫中,我存儲了所有用戶的Facebook uid's
。 現在,我希望能夠通過匹配返回的JSON id
,通過將其與存儲在數據庫中的uid
進行匹配來查找用戶。
這是我當前的users_controller.rb
:
def index
@users = current_user.facebook.get_connections("me", "friends")
@value = '{"id":"name"}'
json = JSON.parse(@value).with_indifferent_access
@users.find(params[json[:id] == :uid])
end
這是我的index.html.erb
:
<%= @users.each do |user| %>
<%= link_to user.first_name, user %>
<% end %>
在過去的一天里,我一直在努力做到這一點,但是運氣不好……做了很多研究。 如果在線上有資源說明如何處理,請在下面放置一個鏈接。 任何幫助是極大的贊賞。 謝謝 :)
找到了解決方案! 將為將來可能需要類似問題的其他任何人提供答案。
在我的users_controller.rb
我現在有
class UsersController < ApplicationController
def index
@friends = current_user.facebook.get_connections("me", "friends")
uids = @friends.collect { |f| f["id"] }
registered_friends = User.where('uid IN (?)', uids)
@pals = registered_friends
end
end
現在在我的Users/index.html.erb
我有了
<% @pals.each do |pal| %>
<%= link_to pal.first_name, wall_path(pal.id) %>
<% end %>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.