我尝试使用脸谱图api来获取用户的好友列表。但我的应用只能列出第一个用户的好友列表。例如,当我打开用户6的朋友列表6时,它将显示朋友列表1的内容。有人知道为什么吗?感谢您的时间和帮助!!
class User < ActiveRecord::Base
has_many :friends
def self.from_omniauth(auth)
where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
user.provider = auth.provider
user.uid = auth.uid
user.name = auth.info.name
user.email = auth.info.email
user.oauth_token = auth.credentials.token
user.oauth_expires_at = Time.at(auth.credentials.expires_at)
user.save!
end
end
def friendslist
facebook {|fb| fb.get_connection("me", "friends")}.each do |hash|
self.friends.where(:name => hash['name'], :uid => hash['id']).first_or_create
end
end
private def facebook
@facebook ||= Koala::Facebook::API.new(oauth_token)
end
end
**<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Friends</th>
</tr>
<% @user.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.email %></td>
<td> <%= link_to "View Friends List", friend_path(user) %></td>
</tr>
<% end %>
</table>**
**<table>
<tr>
<th>Friends</th>
</tr>
<% @friend.each do |friend| %>
<tr>
<td>
<%= friend.name %>
</td>
</tr>
<% end %>
</table>**
class FriendController < ApplicationController
def index
@friend = Friend.all
respond_to do |format|
format.html
format.json { render json: @friend }
end
end
end
答案 0 :(得分:0)
您可能只需要更改......
def friendslist
facebook {|fb| fb.get_connection("me", "friends")}.each do |hash|
self.friends.where(:name => hash['name'], :uid => hash['id']).first_or_create
end
end
to(对于Rails&lt; = 3)
def friendslist
facebook {|fb| fb.get_connection("me", "friends")}.each do |hash|
self.friends.find_or_create_by_name_and_uid(hash['name'],hash['id'])
end
end
到(对于Rails 4)
def friendslist
facebook {|fb| fb.get_connection("me", "friends")}.each do |hash|
self.friends.find_or_create_by(:name => hash['name'], :uid => hash['id'])
end
end